Pathfinding and smart AIs?

Hello everyone,
First of all Sorry for being too much curious and asking so many questions,
And now, another question, how do we add pathfinder AIs to our game. I searched around and found some algorithm which tell about that sort of thing, but I just got confused when I tried to understand them, let alone implement. So, can anyone help me out with how to achieve this?
Thanks in advance.

I think Moku in our asset portal has pathfinding. And @Mathias_Westerdahl has an example as well.

There’s nice interactive examples on the web. Which articles have you read and what was hard to understand?

1 Like

For understanding…

5 Likes

I personnaly find the moku pathfinding hard to hunderstand how to use. I need to implement some pathfinidng in my car’s AI generator to “evaluate” which car is the closest to the finish line, i tried with moku but wasn’t able to make it work with the instructions given on the asset portal :confused: .

@ross.grams I also found Dijkstra’s algorithm and the a* algorithm, samepinch :stuck_out_tongue:
The point hard to understand was that how can we “scan” tiles? Like tilemaps are a group of tiles, but how do I “scan” them and then create a path out of it.?

The important thing is that a tilemap is a grid of tiles. Each individual tile has a horizontal and vertical position on the grid and you can move from one tile to another in discrete steps.

How you scan the grid differs depending on the algorithm you’re using. Some algorithms go breadth first while others go depth first. Try the different visualisations here to better understand the difference:

https://qiao.github.io/PathFinding.js/visual/

1 Like

I have a very simple A* demo here with source here. It might give a hint of how to use or adapt it to your situation.

2 Likes

Hey your example is awesome.
But the main.script in your example poses a quite confusion. Even after trying to understand it again and again, I was unable to grasp how it was creating the data and interacting with astar.lua. Will you please explain it to me (another please here) .

First of all @britzl Thanks for the link you shared, it really helped me understand the difference that various algorithms have.
Now another failure story. :frowning_face:
Today I made another try at creating this

I was able to go through this much, ie I created a table and stored all my tilemap’s wall tiles in it.

But I still couldn’t get how can we implement it.
Take for example that I chose to go breadth first. I take a tile, continue to scan its neighbouring tiles until I find the player. But how am I expected to get a path out of it?

The Red Blob Games article covers that (about two pages down). When you do the breadth-first search, for each tile you record which tile it was entered from. Once you’re done you can follow the trail from any tile back to the starting tile.

2 Likes

Over the past few days, I tried and could not get anything concrete. Here is what I am trying to do
(this is just a rough code typed from my mobile.)

local tiles = {}
local WALL = 44

local function make_path()
--how to save Camefrom in a table, and then make a path out of it?
end

local function scan(x,y, tile) 
local my_tile = tilemap.get_tile("#mytilemap", "walls", x,  y) 
local camefrom = tile

local started = false

If not started then 
scan(x+1, y, tile) 
scan(x+1, y+1. tile)
scan(x, y+1, tile)
scan(x, y-1, tile)
started = true 
else 
if not tile = self.player_tile then 
scan(x+1, y, tile) 
scan(x, y+1, tile)
scan(x, y-1, tile)
elseif tile == wall then 
return 
elseif tile == self.player_tile then --also take in account tilemaps size,  but I have not written here.
make_path()
return
end
end 
end

function init(self)
local player_pos = go.get_position("player")
local x, y, w, h = tilemap.get_bounds("#mytilemap")
self.player_tile = tilemap.get_tile("#mytilemap", "walls", math.floor(player_pos.x/16), math.floor(player_pos.y/16))
--similarly get the enemy tile position e
scan(e.x, e.y, e) 
end

I don’t get how to store the which tile are we scanning and from which tile we came from in the same table and make a path out of it.
Plz help me guys