I haven’t used that A* example extensively myself, but it should be pretty perfect for what you want, actually. If it works the way I think it does, the level_cost
table in main.script stores the movement cost for each type of tile (keyed by the tile index). So if you build your city with a tilemap, all you have to do is put in the movement cost for your tiles (low for roads, more for rough terrain, some very large amount for solid walls, etc.) and it will give you optimal paths through the city for your villagers.
[Edit] Hmm, it seems like there’s a couple small typos in main.script that break the movement cost settings. Maybe @Mathias_Westerdahl can check this?
Current
...
for ly = 0, h-1 do
for lx = 0, w-1 do
local tile = tilemap.get_tile("/level#tilemap", hash("ground"), lx+1, ly+1)
local tilecost = level_cost[tile]
if tilecost == nil then
tile = 10000 -- should be tilecost = 10000?
end
level[ly * w + lx + 1] = tile -- should be = tilecost?
count = count + 1
end
end
...
Fix?
...
for ly = 0, h-1 do
for lx = 0, w-1 do
local tile = tilemap.get_tile("/level#tilemap", hash("ground"), lx+1, ly+1)
local tilecost = level_cost[tile]
if tilecost == nil then
tilecost = 10000
end
level[ly * w + lx + 1] = tilecost
count = count + 1
end
end
...
After changing that, it seems to work perfectly. Here’s a screenshot where I added “roads” that are 4x cheaper to move on than regular ground.
[Edit2] . . . and if you don’t feel like figuring out someone else’s code, you can always do your own implementation. There’s an excellent intro to A* here.