The patrol unit now cycles through the multiple waypoints given to him
This commit is contained in:
parent
93cfadb5bc
commit
184105148a
1 changed files with 16 additions and 12 deletions
|
@ -321,34 +321,38 @@ function my_ai:patrol_eval()
|
|||
return 10000
|
||||
end
|
||||
|
||||
function my_ai:patrol_gen(ai, n, w1, w2) -- n is the name of the unit, like Kiressh
|
||||
-- w1, w2 -- waypoint tables of form {x,y}
|
||||
wesnoth.message("Creation of the patrol func")
|
||||
function my_ai:patrol_gen(ai, n, wp) -- n is the name of the unit, like Kiressh
|
||||
-- wp - a table of waypoint tables of form {x,y}
|
||||
--wesnoth.message("Creation of the patrol func") -- debug info
|
||||
local unit = wesnoth.get_units({name=n})[1]
|
||||
|
||||
local x, y = unit.x, unit.y
|
||||
local wpn = 1 --WayPoint Number - we have to remember which waypoint we are heading to
|
||||
|
||||
if (x == w1.x and y == w1.y) then
|
||||
w1, w2 = w2, w2 -- if we are standing on the first waypoint, swap them
|
||||
if (x == wp[1].x and y == wp[1].y) then
|
||||
wpn = wpn + 1
|
||||
--w1, w2 = w2, w2 -- if we are standing on the first waypoint, swap them
|
||||
end
|
||||
|
||||
local waypoints = {w1, w2} -- this form might be just received from the args
|
||||
wesnoth.message ("Eof creation")
|
||||
--local waypoints = {w1, w2} -- this form might be just received from the args
|
||||
local wpcount = # wp
|
||||
-- wesnoth.message ("Eof creation")-- debug info
|
||||
return
|
||||
function()
|
||||
--wesnoth.message(tostring(waypoints[1].x).." YES")
|
||||
wesnoth.message("Entering patrol execution code")
|
||||
-- wesnoth.message("Entering patrol execution code") -- debug info
|
||||
x, y = unit.x, unit.y
|
||||
if (x == waypoints[wpn].x and y == waypoints[wpn].y) then
|
||||
wpn = 3 - wpn -- hardcoded for two waypoints
|
||||
if (x == wp[wpn].x and y == wp[wpn].y) then
|
||||
wpn = wpn % wpcount + 1 -- advance by one waypoint(this construct loops in range [1, wpcount])
|
||||
-- wpn = 3 - wpn -- hardcoded for two waypoints -- deprecated
|
||||
end
|
||||
ai.move_full(unit, waypoints[wpn].x, waypoints[wpn].y)
|
||||
--wesnoth.message("Going to waypoint number " .. wpn .. " [" .. wp[wpn].x .. ";" .. wp[wpn].y .. "]")
|
||||
ai.move_full(unit, wp[wpn].x, wp[wpn].y)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
my_ai.patrol_exec = my_ai:patrol_gen(ai, "Rark", {x=16, y=15}, {x=16, y=5})
|
||||
my_ai.patrol_exec = my_ai:patrol_gen(ai, "Rark", {{x=14, y=7}, {x=15, y=7}, {x=15, y=8}, {x=14, y=8}})
|
||||
|
||||
|
||||
function my_ai:do_moves()
|
||||
|
|
Loading…
Add table
Reference in a new issue