Micro AIs: use the new functions for storing information in self.data

… so that it is done in a consistent way and conflicts can be avoided
when setting up a subsequent MAI.
This commit is contained in:
mattsc 2014-03-31 20:04:18 -07:00
parent 73a11634f7
commit 1dfd79c3a5
3 changed files with 20 additions and 18 deletions

View file

@ -2,6 +2,7 @@ local H = wesnoth.require "lua/helper.lua"
local LS = wesnoth.require "lua/location_set.lua"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local BC = wesnoth.require "ai/lua/battle_calcs.lua"
local MAISD = wesnoth.dofile "ai/micro_ais/micro_ai_self_data.lua"
local ca_bottleneck_move = {}
@ -213,7 +214,9 @@ end
function ca_bottleneck_move:evaluation(ai, cfg, self)
-- Check whether the side leader should be included or not
if cfg.active_side_leader and (not self.data.side_leader_activated) then
if cfg.active_side_leader and
(not MAISD.get_mai_self_data(self.data, cfg.ai_id, "side_leader_activated"))
then
local can_still_recruit = false -- enough gold left for another recruit?
local recruit_list = wesnoth.sides[wesnoth.current.side].recruit
for i,recruit_type in ipairs(recruit_list) do
@ -224,12 +227,14 @@ function ca_bottleneck_move:evaluation(ai, cfg, self)
break
end
end
if (not can_still_recruit) then self.data.side_leader_activated = true end
if (not can_still_recruit) then
MAISD.set_mai_self_data(self.data, cfg.ai_id, { side_leader_activated = true })
end
end
-- Now find all units, including the leader or not, depending on situation and settings
local units = {}
if self.data.side_leader_activated then
if MAISD.get_mai_self_data(self.data, cfg.ai_id, "side_leader_activated") then
units = wesnoth.get_units { side = wesnoth.current.side,
formula = '$this_unit.moves > 0'
}
@ -481,7 +486,7 @@ end
function ca_bottleneck_move:execution(ai, cfg, self)
if self.data.bottleneck_moves_done then
local units = {}
if self.data.side_leader_activated then
if MAISD.get_mai_self_data(self.data, cfg.ai_id, "side_leader_activated") then
units = wesnoth.get_units { side = wesnoth.current.side,
formula = '$this_unit.moves > 0'
}
@ -510,7 +515,7 @@ function ca_bottleneck_move:execution(ai, cfg, self)
end
-- Now delete almost everything
-- Keep: self.data.is_my_territory, self.data.side_leader_activated
-- Keep: self.data.is_my_territory, [micro_ai]side_leader_activated=
self.data.unit, self.data.hex = nil, nil
self.data.lu_defender, self.data.lu_weapon = nil, nil
self.data.bottleneck_moves_done = nil

View file

@ -3,6 +3,7 @@ local AH = wesnoth.require "ai/lua/ai_helper.lua"
local BC = wesnoth.require "ai/lua/battle_calcs.lua"
local LS = wesnoth.require "lua/location_set.lua"
local MAIUV = wesnoth.dofile "ai/micro_ais/micro_ai_unit_variables.lua"
local MAISD = wesnoth.dofile "ai/micro_ais/micro_ai_self_data.lua"
local ca_goto = {}
@ -21,12 +22,8 @@ function ca_goto:evaluation(ai, cfg, self)
-- If cfg.release_all_units_at_goal is set, check
-- whether the goal has already been reached, in
-- which case we do not do anything
if cfg.release_all_units_at_goal then
for rel in H.child_range(self.data, "goto_release_all") do
if (rel.id == cfg.ai_id) then
return 0
end
end
if MAISD.get_mai_self_data(self.data, cfg.ai_id, "release_all") then
return 0
end
-- For convenience, we check for locations here, and just pass that to the exec function
@ -40,7 +37,8 @@ function ca_goto:evaluation(ai, cfg, self)
--print('#locs org', #locs)
if (#locs == 0) then return 0 end
-- If 'unique_goals' is set, check whether there are locations left to go to
-- If 'unique_goals' is set, check whether there are locations left to go to.
-- This does not have to be a persistent variable
if cfg.unique_goals then
-- First, some cleanup of previous turn data
local str = 'goals_taken_' .. (wesnoth.current.turn - 1)
@ -230,7 +228,7 @@ function ca_goto:execution(ai, cfg, self)
if cfg.release_all_units_at_goal then
--print("Releasing all units")
table.insert(self.data, { "goto_release_all", { id = cfg.ai_id } } )
MAISD.insert_mai_self_data(self.data, cfg.ai_id, { release_all = true })
end
end
end

View file

@ -2,6 +2,7 @@ local H = wesnoth.require "lua/helper.lua"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local LS = wesnoth.require "lua/location_set.lua"
local MAIUV = wesnoth.dofile "ai/micro_ais/micro_ai_unit_variables.lua"
local MAISD = wesnoth.dofile "ai/micro_ais/micro_ai_self_data.lua"
local ca_hang_out = {}
@ -9,17 +10,15 @@ function ca_hang_out:evaluation(ai, cfg, self)
cfg = cfg or {}
-- Return 0 if the mobilize condition has previously been met
for mobilize in H.child_range(self.data, "hangout_mobilize_units") do
if (mobilize.id == cfg.ai_id) then
return 0
end
if MAISD.get_mai_self_data(self.data, cfg.ai_id, "mobilize_units") then
return 0
end
-- Otherwise check if any of the mobilize conditions are now met
if (cfg.mobilize_condition and wesnoth.eval_conditional(cfg.mobilize_condition))
or (cfg.mobilize_on_gold_less_than and (wesnoth.sides[wesnoth.current.side].gold < cfg.mobilize_on_gold_less_than))
then
table.insert(self.data, { "hangout_mobilize_units" , { id = cfg.ai_id } } )
MAISD.insert_mai_self_data(self.data, cfg.ai_id, { mobilize_units = true })
-- Need to unmark all units also
local units = wesnoth.get_units { side = wesnoth.current.side, { "and", cfg.filter } }