Jump to content

Get all tiles of same type that are adjacent


Recommended Posts

I have tile and its position and I would like to get the count of any and all adjacent tiles to it and its neighbors that are of the same type.

I know how to get a tile at a position and check its type but I don't even know where to start on getting its adjacent and adjacent to its adjacent.. tiles

Link to comment
Share on other sites

Isn't it just going to be:

  1. xPos + 1, zPos - 1
  2. xPos + 1, zPos
  3. xPos + 1, zPos + 1
  4. xPos, zPos - 1
  5. xPos, zPos + 1
  6. xPos - 1, zPos - 1
  7. xPos - 1, zPos
  8. xPos - 1, zPos + 1

...and then work from there?

Edited by Ultroman
Link to comment
Share on other sites

I know how to get the tile under the world position, but am not sure about converting world position to tile position since I don't know the tile size or how either coord set works much

Edited by Dcrew
Link to comment
Share on other sites

Yeah, for all these you'll have to test it out. Per frame you can register the tiles you're on. Is there no way to get the position of a tile, instead of getting a tile from a position? Have you checked out the tile class, whatever and wherever it is?

Link to comment
Share on other sites

Well I get the position randomly, I have this in my script:

local min_x, max_x
local min_z, max_z

Then in my prepare/initialize function I have this to get the min/max coords:

local w, h = 0, 0
if not min_x or not max_x or not min_z or not max_z then
	min_x, min_z = 0, 0
	max_x, max_z = 0, 0
	local find_min = function()
		for x = -1024, -256 do
			for z = -1024, -256 do
				local tile = TheWorld.Map:GetTileAtPoint(x, 0, z)
				if tile ~= GROUND.IMPASSABLE and tile < GROUND.UNDERGROUND then
					if x < min_x then
						min_x = x
					end
					if z < min_z then
						min_z = z
					end
				end
			end
		end
	end
	local find_max = function()
		for x = 1024, 256, -1 do
			for z = 1024, 256, -1 do
				local tile = TheWorld.Map:GetTileAtPoint(x, 0, z)
				if tile ~= GROUND.IMPASSABLE and tile < GROUND.UNDERGROUND then
					if x > max_x then
						max_x = x
					end
					if z > max_z then
						max_z = z
					end
				end
			end
		end
	end
	find_min()
	find_max()
	w, h = max_x + -min_x, max_z + -min_z
	print("   ...World (generated): w: "..w..", h: "..h..", x: "..min_x.." to "..max_x..", y: "..min_z.." to "..max_z)
end

Then I use this to get a random position in the world:

local point = Vector3(math.random(min_x, max_x), 0, math.random(min_z, max_z))
local tile = TheWorld.Map:GetTileAtPoint(point:Get())

So I need to somehow convert that point to the tile position so I can get the adjacent matching tiles but I have no idea how to convert my random point to its tile coords. Heck I don't even know if what I am doing is right/good but it seems to work.

I need the position random because I am spawning prefabs randomly throughout the world, but one of the prefabs I am spawning I don't want to spawn unless there's x amount of a specific tile touching the tile I've chosen, which leads back to the original question of this thread

Link to comment
Share on other sites

Would this be good? I made it up right now but I'm not fluent in Lua so I'm not sure if it will work:

local function TilesAdjacentTo(tile_x, tile_y, tile_type, search_depth)
	local count = 0
	local searched = {}
	local function deep_search(x, y)
		if not searched[x] then
			searched[x] = {}
		elseif searched[x][y] then
			return
		end
		searched[x][y] = true
		if TheWorld.Map:GetTile(x, y) == tile_type then
			count = count + 1
			if count < search_depth then
				deep_search(x - 1, y - 1)
				deep_search(x - 1, y)
				deep_search(x - 1, y + 1)
				deep_search(x, y - 1)
				deep_search(x, y + 1)
				deep_search(x + 1, y - 1)
				deep_search(x + 1, y)
				deep_search(x + 1, y + 1)
			end
		end
	end
	deep_search(tile_x, tile_y)
	return count
end

 

Edited by Dcrew
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...