Skip to content

Commit

Permalink
ai_helper.get_live_units: don't use filter_wml
Browse files Browse the repository at this point in the history
It’s slow, the new method is between 10 and 20 times faster.
  • Loading branch information
mattsc committed Apr 26, 2014
1 parent eadfe4a commit 8e9780d
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions data/ai/lua/ai_helper.lua
Expand Up @@ -717,22 +717,14 @@ end
function ai_helper.get_live_units(filter)
-- Same as wesnoth.get_units(), except that it only returns non-petrified units

filter = filter or {}

-- So that @filter in calling function is not modified (if it's a variable):
local live_filter = ai_helper.table_copy(filter)

local filter_not_petrified = { "not", {
{ "filter_wml", {
{ "status", { petrified = "yes" } }
} }
} }
local all_units = wesnoth.get_units(filter)

-- Combine the two filters. Doing it this way around is much easier (always works, no ifs required),
-- but it means we need to make a copy of the filter above, so that the original does not get changed
table.insert(live_filter, filter_not_petrified)
local units = {}
for _,unit in ipairs(all_units) do
if (not unit.status.petrified) then table.insert(units, unit) end
end

return wesnoth.get_units(live_filter)
return units
end

function ai_helper.get_units_with_moves(filter)
Expand Down

0 comments on commit 8e9780d

Please sign in to comment.