Log In  

Folks, I am sorry I am just not understanding this ASTAR code at all that is provided in the 4th Pic8Zine magazine.

  while #frontier do
    current=popend(frontier)
  local neighbors=getneighbors(current)
  for next in all(neighbors) do
    if came_from[vectoindex(next)]==nil then
      insert(frontier,next)
      camefrom[vectoindex(next)]=current
    end
  end

For those of you unfamiliar with this it is the ability in any programming language where you can have a computer-driven element intelligently find the best and shortest path to a point, even if there are walls in the way, IE:

Here I made a map. The little yellow guy is trying to find the blue flag. As you can see if we use the standard beeline approach, he will get stuck on the red square.

Using ASTAR however he will follow the brown arrows around and truly find the shortest distance to the target. The "!!" represents ways that could be traveled but are not because they will take a longer distance than the one shown here.

Could someone please post a working code example of ASTAR that is truly simple and BASIC to understand.

I suspect this will help other gamers as well trying to understand this tricky method of intelligently locating a target around obstacles.

P#114773 2022-07-25 03:02 ( Edited 2022-07-25 03:21)

I have been thinking of trying and adapting the Picozine #4 demo aiming to improve the algorithm visualization, and found out that Red Blob Games has a page about A*, which seems to me a good starting point, with interactive examples.

P#114783 2022-07-25 06:45

Hi @jneda. Unfortunately Red Blob's example uses ANGLES, something I would never do in my work. I just want straight out horizontal and vertical movements locked to an 8x8 tile as seen above.

P#114815 2022-07-25 17:06 ( Edited 2022-07-25 17:07)

Do you mean diagonals?

I don't think there's a problem here since the search algorithm is built so that at each step, it only checks horizontally and vertically.

It's the same algorithm as in Picozine #4 and here's the result of running it on your map:

The code is basically Picozine's. I've been meaning to clean it up, but have been busy trying to make a Craps game instead. :p

Cart #bunafopohu-0 | 2022-07-25 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

Edit: oh now I see, it is true he's using diagonals when building the path. I have not diven that deep yet.
At any rate, Picozine's version does produce the result you want.

P#114823 2022-07-25 17:41 ( Edited 2022-07-25 17:56)

Yep, you have the code correct, @jneda - I just really don't understand it. I guess I'll have to sit and read up on AStar from a different source or see if I can find something simpler to understand.

I guess the good news is once I finally understand it I'm certain I can explain it to others in demo and documented code in very simple terms indeed.

P#114825 2022-07-25 19:15 ( Edited 2022-07-25 19:16)

[Please log in to post a comment]