Merge pull request #3213 from mattsc/speed_up_goto_mai
Efficiency improvements to Lua AIs
This commit is contained in:
commit
6071efa67d
2 changed files with 24 additions and 48 deletions
|
@ -895,43 +895,18 @@ end
|
|||
--------- Unit related helper functions ----------
|
||||
|
||||
function ai_helper.get_live_units(filter)
|
||||
-- Same as wesnoth.get_units(), except that it only returns non-petrified units
|
||||
|
||||
local all_units = wesnoth.get_units(filter)
|
||||
|
||||
local units = {}
|
||||
for _,unit in ipairs(all_units) do
|
||||
if (not unit.status.petrified) then table.insert(units, unit) end
|
||||
end
|
||||
|
||||
return units
|
||||
-- Note: the order of the filters and the [and] tags are important for speed reasons
|
||||
return wesnoth.get_units { { "not", { status = "petrified" } }, { "and", filter } }
|
||||
end
|
||||
|
||||
function ai_helper.get_units_with_moves(filter)
|
||||
-- Using formula = '$this_unit.moves > 0' is slow, this method is much faster
|
||||
local all_units = wesnoth.get_units(filter)
|
||||
|
||||
local units = {}
|
||||
for _,unit in ipairs(all_units) do
|
||||
if (unit.moves > 0) then table.insert(units, unit) end
|
||||
end
|
||||
|
||||
return units
|
||||
-- Note: the order of the filters and the [and] tags are important for speed reasons
|
||||
return wesnoth.get_units { { "and", { formula = "moves > 0" } }, { "and", filter } }
|
||||
end
|
||||
|
||||
function ai_helper.get_units_with_attacks(filter)
|
||||
-- Using formula = '$this_unit.attacks_left > 0' is slow, this method is much faster
|
||||
-- Also need to check that units actually have attacks (as attacks_left > 0 with no attacks is possible)
|
||||
local all_units = wesnoth.get_units(filter)
|
||||
|
||||
local units = {}
|
||||
for _,unit in ipairs(all_units) do
|
||||
if (unit.attacks_left > 0) and (#unit.attacks > 0) then
|
||||
table.insert(units, unit)
|
||||
end
|
||||
end
|
||||
|
||||
return units
|
||||
-- Note: the order of the filters and the [and] tags are important for speed reasons
|
||||
return wesnoth.get_units { { "and", { formula = "attacks_left > 0 and size(attacks) > 0" } }, { "and", filter } }
|
||||
end
|
||||
|
||||
function ai_helper.get_visible_units(viewing_side, filter)
|
||||
|
|
|
@ -25,6 +25,24 @@ function ca_goto:evaluation(cfg, data)
|
|||
return 0
|
||||
end
|
||||
|
||||
local all_units = AH.get_units_with_moves {
|
||||
{ "and", { side = wesnoth.current.side } },
|
||||
{ "and", wml.get_child(cfg, "filter") }
|
||||
}
|
||||
|
||||
local units = {}
|
||||
if cfg.release_unit_at_goal then
|
||||
for _,unit in ipairs(all_units) do
|
||||
if (not MAIUV.get_mai_unit_variables(unit, cfg.ai_id, "release")) then
|
||||
table.insert(units, unit)
|
||||
end
|
||||
end
|
||||
else
|
||||
units = all_units
|
||||
end
|
||||
|
||||
if (not units[1]) then return 0 end
|
||||
|
||||
-- For convenience, we check for locations here, and just pass that to the exec function
|
||||
-- This is mostly to make the unique_goals option easier
|
||||
local width, height = wesnoth.get_map_size()
|
||||
|
@ -60,23 +78,6 @@ function ca_goto:evaluation(cfg, data)
|
|||
end
|
||||
if (not locs[1]) then return 0 end
|
||||
|
||||
local all_units = AH.get_units_with_moves {
|
||||
side = wesnoth.current.side,
|
||||
{ "and", wml.get_child(cfg, "filter") }
|
||||
}
|
||||
|
||||
local units = {}
|
||||
if cfg.release_unit_at_goal then
|
||||
for _,unit in ipairs(all_units) do
|
||||
if (not MAIUV.get_mai_unit_variables(unit, cfg.ai_id, "release")) then
|
||||
table.insert(units, unit)
|
||||
end
|
||||
end
|
||||
else
|
||||
units = all_units
|
||||
end
|
||||
if (not units[1]) then return 0 end
|
||||
|
||||
-- Now store units and locs, so that we don't need to duplicate this in the exec function
|
||||
GO_units, GO_locs = units, locs
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue