[micro_ai] tag: make sure that each CA gets a unique name

This is important if more than one Micro AI of the same type is set up
for a side.
This commit is contained in:
mattsc 2013-05-01 07:05:43 -07:00
parent 4a559b6b94
commit a929543f4c

View file

@ -17,12 +17,37 @@ function add_CAs(side, CA_parms)
-- - max_score: maximum score the CA can return
for i,parms in ipairs(CA_parms) do
cfg_str = parms.cfg_str or ''
cfg_table = parms.cfg_table or {}
-- Make sure the id/name of each CA are unique.
-- We do this by seeing if a CA by that name exists already.
-- If yes, we use the passed id in parms.id
-- If not, we add a number to the end of parms.id until we find an id that does not exist yet
local ca_id, id_found = parms.id, true
local n = 1
while id_found do -- This is really just a precaution
id_found = false
for ai_tag in H.child_range(wesnoth.sides[side].__cfg, 'ai') do
for stage in H.child_range(ai_tag, 'stage') do
for ca in H.child_range(stage, 'candidate_action') do
if (ca.name == ca_id) then id_found = true end
--print('---> found CA:', ca.name, id_found)
end
end
end
if (id_found) then ca_id = parms.id .. n end
n = n+1
end
-- If parameter pass_ca_id is set, pass the CA id to the eval/exec functions
if parms.pass_ca_id then cfg_table.ca_id = ca_id end
local CA = {
engine = "lua",
id = parms.id,
name = parms.id,
id = ca_id,
name = ca_id,
max_score = parms.max_score, -- This works even if parms.max_score is nil
evaluation = "return (...):" .. parms.eval_name .. "(" .. cfg_str .. ")",
execution = "(...):" .. parms.exec_name .. "(" .. cfg_str .. ")"