Update Micro AIs to use new Lua API functions for altering AI

This commit is contained in:
Celtic Minstrel 2017-03-12 21:51:06 -04:00 committed by Charles Dang
parent 55481c9949
commit 0f01f97b30
7 changed files with 49 additions and 142 deletions

View file

@ -12,25 +12,18 @@ function ca_healer_initialize:evaluation()
end end
function ca_healer_initialize:execution(cfg, data) function ca_healer_initialize:execution(cfg, data)
W.modify_ai { W.delete_ai_component(wesnoth.current.side, "aspect[attacks].facet[no_healers_attack]")
side = wesnoth.current.side,
action = "try_delete",
path = "aspect[attacks].facet[no_healers_attack]"
}
W.modify_ai { W.add_ai_component(wesnoth.current.side, "aspect[attacks].facet",
side = wesnoth.current.side, {
action = "add",
path = "aspect[attacks].facet",
{ "facet", {
name = "ai_default_rca::aspect_attacks", name = "ai_default_rca::aspect_attacks",
id = "no_healers_attack", id = "no_healers_attack",
invalidate_on_gamestate_change = "yes", invalidate_on_gamestate_change = "yes",
{ "filter_own", { { "filter_own", {
{ "not", { ability = "healing", { "and", H.get_child(cfg, "filter") } } } { "not", { ability = "healing", { "and", H.get_child(cfg, "filter") } } }
} } } }
} }
} }
)
-- We also need to set the score of healer moves to happen after -- We also need to set the score of healer moves to happen after
-- combat (of other units) at beginning of turn -- combat (of other units) at beginning of turn

View file

@ -12,11 +12,7 @@ function ca_healer_may_attack:evaluation()
end end
function ca_healer_may_attack:execution(cfg, data) function ca_healer_may_attack:execution(cfg, data)
W.modify_ai { W.delete_ai_component(wesnoth.current.side, "aspect[attacks].facet[no_healers_attack]")
side = wesnoth.current.side,
action = "try_delete",
path = "aspect[attacks].facet[no_healers_attack]"
}
-- Once combat (by other units) is done, set the healer move score so that it -- Once combat (by other units) is done, set the healer move score so that it
-- now happens before combat (of the healers which were so far excluded from combat) -- now happens before combat (of the healers which were so far excluded from combat)

View file

@ -39,44 +39,26 @@ return {
end end
-- Always delete the attacks aspect first, so that we do not end up with 100 copies of the facet -- Always delete the attacks aspect first, so that we do not end up with 100 copies of the facet
W.modify_ai { W.delete_ai_component(wesnoth.current.side, "aspect[attacks].facet[limited_attack]")
side = wesnoth.current.side,
action = "try_delete",
path = "aspect[attacks].facet[limited_attack]"
}
-- Also delete aggression, caution - for the same reason -- Also delete aggression, caution - for the same reason
W.modify_ai { W.delete_ai_component(wesnoth.current.side, "aspect[aggression].facet[*]")
side = wesnoth.current.side, W.delete_ai_component(wesnoth.current.side, "aspect[caution].facet[*]")
action = "try_delete",
path = "aspect[aggression].facet[*]"
}
W.modify_ai {
side = wesnoth.current.side,
action = "try_delete",
path = "aspect[caution].facet[*]"
}
-- If the target can be attacked, set the attacks aspect accordingly -- If the target can be attacked, set the attacks aspect accordingly
if attack_locs[1] then if attack_locs[1] then
W.modify_ai { W.add_ai_component(wesnoth.current.side, "aspect[attacks].facet",
side = wesnoth.current.side, {
action = "add",
path = "aspect[attacks].facet",
{ "facet", {
name = "ai_default_rca::aspect_attacks", name = "ai_default_rca::aspect_attacks",
id = "limited_attack", id = "limited_attack",
invalidate_on_gamestate_change = "yes", invalidate_on_gamestate_change = "yes",
{ "filter_enemy", { id = target_id } } { "filter_enemy", { id = target_id } }
} }
} }
)
-- We also want to set aggression=1 and caution=0, -- We also want to set aggression=1 and caution=0,
-- otherwise there could be turns on which nothing happens -- otherwise there could be turns on which nothing happens
W.modify_side { W.append_ai{ aggression = 1, caution = 0 }
side = wesnoth.current.side,
{ "ai", { aggression = 1, caution = 0 } }
}
end end
return 0 return 0

View file

@ -18,69 +18,49 @@ function wesnoth.micro_ais.fast_ai(cfg)
-- Also need to delete/add some default CAs -- Also need to delete/add some default CAs
if (cfg.action == 'delete') then if (cfg.action == 'delete') then
-- This can be done independently of whether these were removed earlier -- This can be done independently of whether these were removed earlier
W.modify_ai { W.add_ai_component(cfg.side, "stage[main_loop].candidate_action",
side = cfg.side, {
action = "add",
path = "stage[main_loop].candidate_action",
{ "candidate_action", {
id="combat", id="combat",
engine="cpp", engine="cpp",
name="ai_default_rca::combat_phase", name="ai_default_rca::combat_phase",
max_score=100000, max_score=100000,
score=100000 score=100000
} }
} }
)
W.modify_ai { W.add_ai_component(cfg.side, "stage[main_loop].candidate_action",
side = cfg.side, {
action = "add",
path = "stage[main_loop].candidate_action",
{ "candidate_action", {
id="villages", id="villages",
engine="cpp", engine="cpp",
name="ai_default_rca::get_villages_phase", name="ai_default_rca::get_villages_phase",
max_score=60000, max_score=60000,
score=60000 score=60000
} }
} }
)
W.modify_ai { W.add_ai_component(cfg.side, "stage[main_loop].candidate_action",
side = cfg.side, {
action = "add",
path = "stage[main_loop].candidate_action",
{ "candidate_action", {
id="retreat", id="retreat",
engine="cpp", engine="cpp",
name="ai_default_rca::retreat_phase", name="ai_default_rca::retreat_phase",
max_score=40000, max_score=40000,
score=40000 score=40000
} }
} }
)
W.modify_ai { W.add_ai_component(cfg.side, "stage[main_loop].candidate_action",
side = cfg.side, {
action = "add",
path = "stage[main_loop].candidate_action",
{ "candidate_action", {
id="move_to_targets", id="move_to_targets",
engine="cpp", engine="cpp",
name="ai_default_rca::move_to_targets_phase", name="ai_default_rca::move_to_targets_phase",
max_score=20000, max_score=20000,
score=20000 score=20000
} }
} }
)
else else
if (not cfg.skip_combat_ca) then if (not cfg.skip_combat_ca) then
W.modify_ai { W.delete_ai_component(cfg.side, "stage[main_loop].candidate_action[high_xp_attack]")
side = cfg.side, W.delete_ai_component(cfg.side, "stage[main_loop].candidate_action[combat]")
action = "try_delete",
path = "stage[main_loop].candidate_action[high_xp_attack]"
}
W.modify_ai {
side = cfg.side,
action = "try_delete",
path = "stage[main_loop].candidate_action[combat]"
}
else else
for i,parm in ipairs(CA_parms) do for i,parm in ipairs(CA_parms) do
if (parm.ca_id == 'combat') or (parm.ca_id == 'combat_leader') then if (parm.ca_id == 'combat') or (parm.ca_id == 'combat_leader') then
@ -90,23 +70,11 @@ function wesnoth.micro_ais.fast_ai(cfg)
end end
if (not cfg.skip_move_ca) then if (not cfg.skip_move_ca) then
W.modify_ai { W.delete_ai_component(cfg.side, "stage[main_loop].candidate_action[villages]")
side = cfg.side,
action = "try_delete",
path = "stage[main_loop].candidate_action[villages]"
}
W.modify_ai { W.delete_ai_component(cfg.side, "stage[main_loop].candidate_action[retreat]")
side = cfg.side,
action = "try_delete",
path = "stage[main_loop].candidate_action[retreat]"
}
W.modify_ai { W.delete_ai_component(cfg.side, "stage[main_loop].candidate_action[move_to_targets]")
side = cfg.side,
action = "try_delete",
path = "stage[main_loop].candidate_action[move_to_targets]"
}
else else
for i,parm in ipairs(CA_parms) do for i,parm in ipairs(CA_parms) do
if (parm.ca_id == 'move') then if (parm.ca_id == 'move') then

View file

@ -28,11 +28,7 @@ function wesnoth.micro_ais.protect_unit(cfg)
-- Optional key disable_move_leader_to_keep: needs to be dealt with -- Optional key disable_move_leader_to_keep: needs to be dealt with
-- separately as it affects a default CA -- separately as it affects a default CA
if cfg.disable_move_leader_to_keep then if cfg.disable_move_leader_to_keep then
W.modify_ai { W.delete_ai_component(cfg.side, "stage[main_loop].candidate_action[move_leader_to_keep]")
side = cfg.side,
action = "try_delete",
path = "stage[main_loop].candidate_action[move_leader_to_keep]"
}
end end
-- attacks aspects also needs to be set separately -- attacks aspects also needs to be set separately
@ -56,18 +52,15 @@ function wesnoth.micro_ais.protect_unit(cfg)
MAIH.delete_aspects(cfg.side, aspect_parms) MAIH.delete_aspects(cfg.side, aspect_parms)
-- We also need to add the move_leader_to_keep CA back in -- We also need to add the move_leader_to_keep CA back in
-- This works even if it was not removed, it simply overwrites the existing CA -- This works even if it was not removed, it simply overwrites the existing CA
W.modify_ai { W.add_ai_component(side, "stage[main_loop].candidate_action",
side = side, {
action = "add",
path = "stage[main_loop].candidate_action",
{ "candidate_action", {
id="move_leader_to_keep", id="move_leader_to_keep",
engine="cpp", engine="cpp",
name="ai_default_rca::move_leader_to_keep_phase", name="ai_default_rca::move_leader_to_keep_phase",
max_score=160000, max_score=160000,
score=160000 score=160000
} }
} }
)
else else
MAIH.add_aspects(cfg.side, aspect_parms) MAIH.add_aspects(cfg.side, aspect_parms)
end end

View file

@ -4,26 +4,19 @@ local W = H.set_wml_action_metatable {}
local function handle_default_recruitment(cfg) local function handle_default_recruitment(cfg)
-- Also need to delete/add the default recruitment CA -- Also need to delete/add the default recruitment CA
if cfg.action == 'add' then if cfg.action == 'add' then
W.modify_ai { W.delete_ai_component(cfg.side, "stage[main_loop].candidate_action[recruitment]")
side = cfg.side,
action = "try_delete",
path = "stage[main_loop].candidate_action[recruitment]"
}
elseif cfg.action == 'delete' then elseif cfg.action == 'delete' then
-- We need to add the recruitment CA back in -- We need to add the recruitment CA back in
-- This works even if it was not removed, it simply overwrites the existing CA -- This works even if it was not removed, it simply overwrites the existing CA
W.modify_ai { W.add_ai_component(cfg.side, "stage[main_loop].candidate_action",
side = cfg.side, {
action = "add",
path = "stage[main_loop].candidate_action",
{ "candidate_action", {
id="recruitment", id="recruitment",
engine="cpp", engine="cpp",
name="ai_default_rca::aspect_recruitment_phase", name="ai_default_rca::aspect_recruitment_phase",
max_score=180000, max_score=180000,
score=180000 score=180000
} }
} }
)
end end
end end

View file

@ -93,12 +93,7 @@ function micro_ai_helper.add_CAs(side, ca_id_core, CA_parms, CA_cfg)
CA.location = parms.location CA.location = parms.location
table.insert(CA, T.args(CA_cfg)) table.insert(CA, T.args(CA_cfg))
W.modify_ai { W.add_ai_component(side, "stage[main_loop].candidate_action", CA)
side = side,
action = "add",
path = "stage[main_loop].candidate_action",
T.candidate_action(CA)
}
end end
end end
@ -115,11 +110,7 @@ function micro_ai_helper.delete_CAs(side, ca_id_core, CA_parms)
for _,parms in ipairs(CA_parms) do for _,parms in ipairs(CA_parms) do
local ca_id = ca_id_core .. '_' .. parms.ca_id local ca_id = ca_id_core .. '_' .. parms.ca_id
W.modify_ai { W.delete_ai_component(side, "stage[main_loop].candidate_action[" .. ca_id .. "]")
side = side,
action = "try_delete",
path = "stage[main_loop].candidate_action[" .. ca_id .. "]"
}
-- Also need to delete variable stored in all units of the side, so that later MAIs can use these units -- Also need to delete variable stored in all units of the side, so that later MAIs can use these units
local units = wesnoth.get_units { side = side } local units = wesnoth.get_units { side = side }
@ -151,12 +142,7 @@ function micro_ai_helper.add_aspects(side, aspect_parms)
-- } -- }
for _,parms in ipairs(aspect_parms) do for _,parms in ipairs(aspect_parms) do
W.modify_ai { W.add_ai_component(side, "aspect[" .. parms.aspect .. "].facet", parms.facet)
side = side,
action = "add",
path = "aspect[" .. parms.aspect .. "].facet",
T.facet(parms.facet)
}
end end
end end
@ -167,11 +153,7 @@ function micro_ai_helper.delete_aspects(side, aspect_parms)
-- aspect_parms.aspect_id field is needed -- aspect_parms.aspect_id field is needed
for _,parms in ipairs(aspect_parms) do for _,parms in ipairs(aspect_parms) do
W.modify_ai { W.delete_ai_component(side, "aspect[attacks].facet[" .. parms.facet.id .. "]")
side = side,
action = "try_delete",
path = "aspect[attacks].facet[" .. parms.facet.id .. "]"
}
end end
end end