Skip to content

Commit

Permalink
Goto Micro AI: prevent potential conflict between Goto MAIs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mattsc committed Apr 20, 2014
1 parent 0b14f18 commit 7e8964c
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions data/ai/micro_ais/cas/ca_goto.lua
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 7e8964c

Please sign in to comment.