Connecting all the elements

All of the pieces for our path-finding implementation are ready now. Our next step is to bind them all together into a module that can determine the shortest path when traveling "on foot" through our virtual environment.

Getting ready

Open walker.lua, if needed.

Getting on with it

The walker module will specify which neighbor and heuristic functions need to be used with the algorithm.

Assembling dependencies

The module will therefore need to load the path module so that it can call our A* function.

local path = require "path"
local function manhattan(space, goal)

Binding the components

The module function will take a start space and an end space, and use the path module to pick a step in the right direction.

  return math.abs(goal.x - space.x) + math.abs(goal.y - space.y)
end
return function(start, goal)
  return path(start, goal, start.map.Neighbors, manhattan)
end

What did we do?

We've given the A* algorithm a particular case to work on, by choosing functions that provide it with neighbors and a heuristic for a specific kind of map. If we wanted to create a flying or swimming creature, we would need to replace the neighbors function to modify the costs for obstacle spaces or areas of water.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.9.115