Experimental AI: add a simple move to enemy CA

Set up to run if nothing else is available.
In a separate file for use as a stand-alone CA in an AI
This commit is contained in:
mattsc 2013-10-31 21:19:44 -07:00
parent 4cdedc11ee
commit 76e22a3d6a
3 changed files with 62 additions and 1 deletions

View file

@ -1,7 +1,9 @@
return {
init = function(ai)
local generic_rush = {}
-- Grab a useful separate CA as a starting point
local generic_rush = wesnoth.require("ai/lua/move_to_any_target.lua").init(ai)
-- More generic grunt rush (and can, in fact, be used with other unit types as well)
local H = wesnoth.require "lua/helper.lua"

View file

@ -0,0 +1,52 @@
return {
init = function(ai)
local AH = wesnoth.require "~/add-ons/AI-demos/lua/ai_helper.lua"
local move_to_any_target = {}
function move_to_any_target:move_to_enemy_eval()
local units = wesnoth.get_units {
side = wesnoth.current.side,
canrecruit = 'no',
formula = '$this_unit.moves > 0'
}
if (not units[1]) then
-- No units with moves left
return 0
end
local unit, destination
-- Find a unit that has a path to an space close to an enemy
for i,u in ipairs(units) do
local distance, target = AH.get_closest_enemy({u.x, u.y})
if target.x then
unit = u
local x, y = wesnoth.find_vacant_tile(target.x, target.y)
destination = AH.next_hop(unit, x, y)
if destination then
break
end
end
end
if (not destination) then
-- No path was found
return 0
end
self.data.destination = destination
self.data.unit = unit
return 1
end
function move_to_any_target:move_to_enemy_exec()
ai.move(self.data.unit, self.data.destination[1], self.data.destination[2])
end
return move_to_any_target
end
}

View file

@ -571,5 +571,12 @@
evaluation="return (...):place_healers_eval()"
execution="(...):place_healers_exec()"
[/candidate_action]
[candidate_action]
engine=lua
name=move_to_enemy
max_score=1
evaluation="return (...):move_to_enemy_eval()"
execution="(...):move_to_enemy_exec()"
[/candidate_action]
[/stage]
#enddef