Dcrew Posted July 26, 2019 Share Posted July 26, 2019 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 More sharing options...
Ultroman Posted July 26, 2019 Share Posted July 26, 2019 (edited) Isn't it just going to be: xPos + 1, zPos - 1 xPos + 1, zPos xPos + 1, zPos + 1 xPos, zPos - 1 xPos, zPos + 1 xPos - 1, zPos - 1 xPos - 1, zPos xPos - 1, zPos + 1 ...and then work from there? Edited July 26, 2019 by Ultroman Link to comment Share on other sites More sharing options...
Dcrew Posted July 26, 2019 Author Share Posted July 26, 2019 (edited) 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 July 26, 2019 by Dcrew Link to comment Share on other sites More sharing options...
Ultroman Posted July 26, 2019 Share Posted July 26, 2019 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 More sharing options...
Dcrew Posted July 27, 2019 Author Share Posted July 27, 2019 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 More sharing options...
Developer zarklord_klei Posted July 27, 2019 Developer Share Posted July 27, 2019 local tile_x, tile_y = TheWorld.Map:GetTileCoordsAtPoint(x, y, z) local tile = TheWorld.Map:GetTile(tile_x, tile_y) Link to comment Share on other sites More sharing options...
Dcrew Posted July 27, 2019 Author Share Posted July 27, 2019 (edited) 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 July 27, 2019 by Dcrew Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now