Goto Micro AI: prevent potential conflict between Goto MAIs

If several Goto MAIs are used on the same side and both have
unique_goals=yes set, they previously might have interfered with each
other, one preventing the other from working correctly. Now the “goal
taken” information is saved such that it only applies to the CA by
which it was set.
This commit is contained in:
mattsc 2014-04-20 13:53:23 -07:00
parent e79083c696
commit 924c42f407

View file

@ -39,13 +39,18 @@ function ca_goto:evaluation(ai, cfg, self)
local locs = {}
if cfg.unique_goals then
-- First, some cleanup of previous turn data
local str = 'GO_goals_taken_' .. (wesnoth.current.turn - 1)
self.data[str] = nil
local str = 'goal_taken_' .. (wesnoth.current.turn - 1)
local old_goals = MAISD.get_mai_self_data(self.data, cfg.ai_id)
for goal,_ in pairs(old_goals) do
if string.find(goal, str) then
old_goals[goal] = nil -- This also removes it from self.data
end
end
-- Now on to the current turn
local str = 'GO_goals_taken_' .. wesnoth.current.turn
for _,loc in ipairs(all_locs) do
if (not self.data[str]) or (not self.data[str]:get(loc[1], loc[2])) then
local str = 'goal_taken_' .. wesnoth.current.turn .. '_' .. loc[1] .. '_' .. loc[2]
if (not MAISD.get_mai_self_data(self.data, cfg.ai_id, str)) then
table.insert(locs, loc)
end
end
@ -166,9 +171,10 @@ function ca_goto:execution(ai, cfg, self)
-- If 'unique_goals' is set, mark this location as being taken
if cfg.unique_goals then
local str = 'GO_goals_taken_' .. wesnoth.current.turn
if (not self.data[str]) then self.data[str] = LS.create() end
self.data[str]:insert(closest_hex[1], closest_hex[2])
local str = 'goal_taken_' .. wesnoth.current.turn .. '_' .. closest_hex[1] .. '_' .. closest_hex[2]
local tmp_table = {}
tmp_table[str] = true
MAISD.insert_mai_self_data(self.data, cfg.ai_id, tmp_table)
end
-- If any of the non-standard path finding options were used,