Animals MAIs: rename internal engine functions and CAs

This shouldn't affect anything else, it is simply done for consistency
(to reflect the recent syntax changes).
This commit is contained in:
mattsc 2013-10-22 09:36:10 -07:00
parent 51f1546c40
commit 227527ee61
8 changed files with 70 additions and 70 deletions

View file

@ -6,7 +6,7 @@ return {
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local LS = wesnoth.require "lua/location_set.lua"
function engine:mai_animals_big_eval(cfg)
function engine:mai_big_animals_eval(cfg)
local units = wesnoth.get_units {
side = wesnoth.current.side,
{ "and" , cfg.filter },
@ -17,7 +17,7 @@ return {
return 0
end
function engine:mai_animals_big_exec(cfg)
function engine:mai_big_animals_exec(cfg)
-- Big animals just move toward goal that gets set occasionally
-- Avoid the other big animals (bears, yetis, spiders) and the dogs, otherwise attack whatever is in their range
-- The only difference in behavior is the area in which the units move

View file

@ -9,7 +9,7 @@ return {
-- While all these CAs have been generalized to be usable with different types of
-- units, we keep their original names in order to indicate their kind of behavior
function engine:mai_animals_new_rabbit_eval(cfg)
function engine:mai_forest_animals_new_rabbit_eval(cfg)
-- Put new rabbits out the if there are fewer than cfg.rabbit_number
-- but only if cfg.rabbit_type is set, otherwise do nothing
-- If this gets executed, we'll let the CA black-list itself
@ -18,7 +18,7 @@ return {
return cfg.ca_score
end
function engine:mai_animals_new_rabbit_exec(cfg)
function engine:mai_forest_animals_new_rabbit_exec(cfg)
local number = cfg.rabbit_number or 6
local rabbit_enemy_distance = cfg.rabbit_enemy_distance or 3
@ -73,7 +73,7 @@ return {
end
end
function engine:mai_animals_tusker_attack_eval(cfg)
function engine:mai_forest_animals_tusker_attack_eval(cfg)
-- Check whether there is an enemy next to a tusklet and attack it ("protective parents" AI)
-- Both cfg.tusker_type and cfg.tusklet_type need to be set for this to kick in
@ -90,7 +90,7 @@ return {
return 0
end
function engine:mai_animals_tusker_attack_exec(cfg)
function engine:mai_forest_animals_tusker_attack_exec(cfg)
local tuskers = wesnoth.get_units { side = wesnoth.current.side, type = cfg.tusker_type, formula = '$this_unit.moves > 0' }
local adj_enemies = wesnoth.get_units {
{ "filter_side", {{"enemy_of", {side = wesnoth.current.side} }} },
@ -135,7 +135,7 @@ return {
end
end
function engine:mai_animals_forest_move_eval(cfg)
function engine:mai_forest_animals_forest_move_eval(cfg)
local deer_type = cfg.deer_type or "no_unit_of_this_type"
local rabbit_type = cfg.rabbit_type or "no_unit_of_this_type"
local tusker_type = cfg.tusker_type or "no_unit_of_this_type"
@ -153,7 +153,7 @@ return {
return 0
end
function engine:mai_animals_forest_move_exec(cfg)
function engine:mai_forest_animals_forest_move_exec(cfg)
local deer_type = cfg.deer_type or "no_unit_of_this_type"
local rabbit_type = cfg.rabbit_type or "no_unit_of_this_type"
local tusker_type = cfg.tusker_type or "no_unit_of_this_type"
@ -296,7 +296,7 @@ return {
end
end
function engine:mai_animals_tusklet_eval(cfg)
function engine:mai_forest_animals_tusklet_eval(cfg)
-- Tusklets will simply move toward the closest tusker, without regard for anything else
-- Except if no tuskers are left, in which case the previous CA takes over and does a random move
@ -310,7 +310,7 @@ return {
return 0
end
function engine:mai_animals_tusklet_exec(cfg)
function engine:mai_forest_animals_tusklet_exec(cfg)
local tusklets = wesnoth.get_units { side = wesnoth.current.side, type = cfg.tusklet_type, formula = '$this_unit.moves > 0' }
local tuskers = wesnoth.get_units { side = wesnoth.current.side, type = cfg.tusker_type }
--print('#tusklets, #tuskers', #tusklets, #tuskers)

View file

@ -8,7 +8,7 @@ return {
----- Beginning of Herding Animals AI -----
-- We'll keep a lot of things denoted as sheep/dogs, because herder/herded is too similar
function engine:mai_animals_herding_area(cfg)
function engine:mai_herding_area(cfg)
-- Find the area that the sheep can occupy
-- First, find all contiguous hexes around center hex that are inside herding_perimeter
local herding_area = LS.of_pairs(wesnoth.get_locations {
@ -27,7 +27,7 @@ return {
return herding_area
end
function engine:mai_animals_herding_attack_close_enemy_eval(cfg)
function engine:mai_herding_attack_close_enemy_eval(cfg)
-- Any enemy within attention_distance (default = 8) hexes of a sheep will get the dogs' attention
-- with enemies within attack_distance (default: 4) being attacked
local enemies = wesnoth.get_units {
@ -46,7 +46,7 @@ return {
return 0
end
function engine:mai_animals_herding_attack_close_enemy_exec(cfg)
function engine:mai_herding_attack_close_enemy_exec(cfg)
local dogs = wesnoth.get_units { side = wesnoth.current.side, {"and", cfg.filter},
formula = '$this_unit.moves > 0' }
local sheep = wesnoth.get_units { side = wesnoth.current.side, {"and", cfg.filter_second} }
@ -149,7 +149,7 @@ return {
AH.movefull_stopunit(ai, best_dog, best_hex)
end
function engine:mai_animals_sheep_runs_enemy_eval(cfg)
function engine:mai_herding_sheep_runs_enemy_eval(cfg)
-- Sheep runs from any enemy within attention_distance hexes (after the dogs have moved in)
local sheep = wesnoth.get_units { side = wesnoth.current.side, {"and", cfg.filter_second},
formula = '$this_unit.moves > 0',
@ -166,7 +166,7 @@ return {
return 0
end
function engine:mai_animals_sheep_runs_enemy_exec(cfg)
function engine:mai_herding_sheep_runs_enemy_exec(cfg)
local sheep = wesnoth.get_units { side = wesnoth.current.side, {"and", cfg.filter_second},
formula = '$this_unit.moves > 0',
{ "filter_location",
@ -197,7 +197,7 @@ return {
AH.movefull_stopunit(ai, sheep, best_hex)
end
function engine:mai_animals_sheep_runs_dog_eval(cfg)
function engine:mai_herding_sheep_runs_dog_eval(cfg)
-- Any sheep with moves left next to a dog runs aways
local sheep = wesnoth.get_units { side = wesnoth.current.side, {"and", cfg.filter_second},
formula = '$this_unit.moves > 0',
@ -208,7 +208,7 @@ return {
return 0
end
function engine:mai_animals_sheep_runs_dog_exec(cfg)
function engine:mai_herding_sheep_runs_dog_exec(cfg)
-- simply get the first sheep
local sheep = wesnoth.get_units { side = wesnoth.current.side, {"and", cfg.filter_second},
formula = '$this_unit.moves > 0',
@ -232,7 +232,7 @@ return {
AH.movefull_stopunit(ai, sheep, best_hex)
end
function engine:mai_animals_herd_sheep_eval(cfg)
function engine:mai_herding_herd_sheep_eval(cfg)
-- If dogs have moves left, and there is a sheep with moves left outside the
-- herding area, chase it back
-- We'll do a bunch of nested if's, to speed things up
@ -242,7 +242,7 @@ return {
{ "not", { { "filter_adjacent", { side = wesnoth.current.side, {"and", cfg.filter} } } } }
}
if sheep[1] then
local herding_area = self:mai_animals_herding_area(cfg)
local herding_area = self:mai_herding_area(cfg)
for i,s in ipairs(sheep) do
-- If a sheep is found outside the herding area, we want to chase it back
if (not herding_area:get(s.x, s.y)) then return cfg.ca_score end
@ -254,12 +254,12 @@ return {
return 0
end
function engine:mai_animals_herd_sheep_exec(cfg)
function engine:mai_herding_herd_sheep_exec(cfg)
local dogs = wesnoth.get_units { side = wesnoth.current.side, {"and", cfg.filter}, formula = '$this_unit.moves > 0' }
local sheep = wesnoth.get_units { side = wesnoth.current.side, {"and", cfg.filter_second},
{ "not", { { "filter_adjacent", { side = wesnoth.current.side, {"and", cfg.filter} } } } }
}
local herding_area = self:mai_animals_herding_area(cfg)
local herding_area = self:mai_herding_area(cfg)
local sheep_to_herd = {}
for i,s in ipairs(sheep) do
-- If a sheep is found outside the herding area, we want to chase it back
@ -317,14 +317,14 @@ return {
end
end
function engine:mai_animals_sheep_move_eval(cfg)
function engine:mai_herding_sheep_move_eval(cfg)
-- If nothing else is to be done, the sheep do a random move
local sheep = wesnoth.get_units { side = wesnoth.current.side, {"and", cfg.filter_second}, formula = '$this_unit.moves > 0' }
if sheep[1] then return cfg.ca_score end
return 0
end
function engine:mai_animals_sheep_move_exec(cfg)
function engine:mai_herding_sheep_move_exec(cfg)
-- We simply move the first sheep first
local sheep = wesnoth.get_units { side = wesnoth.current.side, {"and", cfg.filter_second}, formula = '$this_unit.moves > 0' }[1]
@ -349,7 +349,7 @@ return {
-- If this move remains within herding area or dogs have no moves left, or sheep doesn't move
-- make it a full move, otherwise partial move
local herding_area = self:mai_animals_herding_area(cfg)
local herding_area = self:mai_herding_area(cfg)
local dogs = wesnoth.get_units { side = wesnoth.current.side, {"and", cfg.filter}, formula = '$this_unit.moves > 0' }
if herding_area:get(x, y) or (not dogs[1]) or ((x == sheep.x) and (y == sheep.y)) then
AH.movefull_stopunit(ai, sheep, x, y)
@ -358,7 +358,7 @@ return {
end
end
function engine:mai_animals_dog_move_eval(cfg)
function engine:mai_herding_dog_move_eval(cfg)
-- As a final step, any dog not adjacent to a sheep moves within herding_perimeter
local dogs = wesnoth.get_units { side = wesnoth.current.side, {"and", cfg.filter},
formula = '$this_unit.moves > 0',
@ -368,7 +368,7 @@ return {
return 0
end
function engine:mai_animals_dog_move_exec(cfg)
function engine:mai_herding_dog_move_exec(cfg)
-- We simply move the first dog first
local dog = wesnoth.get_units { side = wesnoth.current.side, {"and", cfg.filter},
formula = '$this_unit.moves > 0',

View file

@ -6,7 +6,7 @@ return {
local W = H.set_wml_action_metatable {}
local AH = wesnoth.require "ai/lua/ai_helper.lua"
function engine:mai_animals_attack_weakest_adj_enemy(unit)
function engine:mai_hunter_unit_attack_weakest_adj_enemy(unit)
-- Attack the enemy with the fewest hitpoints adjacent to 'unit', if there is one
-- Returns status of the attack:
-- 'attacked': if a unit was attacked
@ -40,7 +40,7 @@ return {
return 'no_attack'
end
function engine:mai_animals_hunter_unit_eval(cfg)
function engine:mai_hunter_unit_eval(cfg)
local unit = wesnoth.get_units { side = wesnoth.current.side, id = cfg.id,
formula = '$this_unit.moves > 0'
}[1]
@ -50,7 +50,7 @@ return {
end
-- cfg parameters: id, hunting_ground, home_x, home_y, rest_turns, show_messages
function engine:mai_animals_hunter_unit_exec(cfg)
function engine:mai_hunter_unit_exec(cfg)
-- Unit with the given ID goes on a hunt, doing a random wander in area given by
-- hunting_ground, then retreats to
-- position given by 'home_x,home_y' for 'rest_turns' turns, or until fully healed
@ -113,7 +113,7 @@ return {
end
-- Finally, if the unit ended up next to enemies, attack the weakest of those
local attack_status = self:mai_animals_attack_weakest_adj_enemy(unit)
local attack_status = self:mai_hunter_unit_attack_weakest_adj_enemy(unit)
-- If the enemy was killed, hunter returns home
if unit.valid and (attack_status == 'killed') then
@ -151,7 +151,7 @@ return {
end
-- We also attack the weakest adjacent enemy, if still possible
self:mai_animals_attack_weakest_adj_enemy(unit)
self:mai_hunter_unit_attack_weakest_adj_enemy(unit)
-- If the unit got home, start the resting counter
if unit.valid and (unit.x == cfg.home_x) and (unit.y == cfg.home_y) then
@ -172,7 +172,7 @@ return {
ai.stopunit_moves(unit)
-- However, we do also attack the weakest adjacent enemy, if still possible
self:mai_animals_attack_weakest_adj_enemy(unit)
self:mai_hunter_unit_attack_weakest_adj_enemy(unit)
-- If this is the last turn of resting, we also remove the status and turn variable
if unit.valid and (unit.hitpoints >= unit.max_hitpoints) and (unit.variables.resting_until <= wesnoth.current.turn) then

View file

@ -5,7 +5,7 @@ return {
local H = wesnoth.require "lua/helper.lua"
local AH = wesnoth.require "ai/lua/ai_helper.lua"
function engine:mai_animals_scatter_swarm_eval(cfg)
function engine:mai_swarm_scatter_eval(cfg)
local scatter_distance = cfg.scatter_distance or 3
-- Any enemy within "scatter_distance" hexes of a unit will cause swarm to scatter
@ -26,7 +26,7 @@ return {
return 0
end
function engine:mai_animals_scatter_swarm_exec(cfg)
function engine:mai_swarm_scatter_exec(cfg)
local scatter_distance = cfg.scatter_distance or 3
local vision_distance = cfg.vision_distance or 12
@ -67,7 +67,7 @@ return {
end
end
function engine:mai_animals_move_swarm_eval(cfg)
function engine:mai_swarm_move_eval(cfg)
local units = wesnoth.get_units { side = wesnoth.current.side }
for i,u in ipairs(units) do
if (u.moves > 0) then return cfg.ca_score end
@ -76,7 +76,7 @@ return {
return 0
end
function engine:mai_animals_move_swarm_exec(cfg)
function engine:mai_swarm_move_exec(cfg)
local enemy_distance = cfg.enemy_distance or 5
local vision_distance = cfg.vision_distance or 12

View file

@ -7,7 +7,7 @@ return {
local BC = wesnoth.require "ai/lua/battle_calcs.lua"
local LS = wesnoth.require "lua/location_set.lua"
function engine:mai_animals_wolves_eval(cfg)
function engine:mai_wolves_move_eval(cfg)
local wolves = wesnoth.get_units { side = wesnoth.current.side,
formula = '$this_unit.moves > 0', { "and", cfg.filter }
}
@ -20,7 +20,7 @@ return {
return 0
end
function engine:mai_animals_wolves_exec(cfg)
function engine:mai_wolves_move_exec(cfg)
local wolves = wesnoth.get_units { side = wesnoth.current.side,
formula = '$this_unit.moves > 0', { "and", cfg.filter }
}
@ -103,7 +103,7 @@ return {
end
end
function engine:mai_animals_wolves_wander_eval(cfg)
function engine:mai_wolves_wander_eval(cfg)
-- When there's no prey left, the wolves wander and regroup
local wolves = wesnoth.get_units { side = wesnoth.current.side,
formula = '$this_unit.moves > 0', { "and", cfg.filter }
@ -113,7 +113,7 @@ return {
return 0
end
function engine:mai_animals_wolves_wander_exec(cfg)
function engine:mai_wolves_wander_exec(cfg)
local wolves = wesnoth.get_units { side = wesnoth.current.side,
formula = '$this_unit.moves > 0', { "and", cfg.filter }
}

View file

@ -7,14 +7,14 @@ return {
local AH = wesnoth.require "ai/lua/ai_helper.lua"
local LS = wesnoth.require "lua/location_set.lua"
function engine:mai_animals_color_label(x, y, text)
function engine:mai_wolves_multipacks_color_label(x, y, text)
-- For displaying the wolf pack number in color underneath each wolf
-- only using gray for the time being
text = "<span color='#c0c0c0'>" .. text .. "</span>"
W.label{ x = x, y = y, text = text }
end
function engine:mai_animals_assign_packs(cfg)
function engine:mai_wolves_multipacks_assign_packs(cfg)
local unit_type = cfg.type or "Wolf"
local pack_size = cfg.pack_size or 3
@ -134,7 +134,7 @@ return {
if cfg.show_pack_number then
for k,p in pairs(packs) do
for i,loc in ipairs(p) do
self:mai_animals_color_label(loc.x, loc.y, k)
self:mai_wolves_multipacks_color_label(loc.x, loc.y, k)
end
end
end
@ -142,7 +142,7 @@ return {
return packs
end
function engine:mai_animals_wolves_multipacks_attack_eval(cfg)
function engine:mai_wolves_multipacks_attack_eval(cfg)
local unit_type = cfg.type or "Wolf"
-- If wolves have attacks left, call this CA
@ -154,9 +154,9 @@ return {
return 0
end
function engine:mai_animals_wolves_multipacks_attack_exec(cfg)
function engine:mai_wolves_multipacks_attack_exec(cfg)
-- First get all the packs
local packs = self:mai_animals_assign_packs(cfg)
local packs = self:mai_wolves_multipacks_assign_packs(cfg)
-- Attacks are dealt with on a pack by pack basis
-- and I want all wolves in a pack to move first, before going on to the next pack
@ -268,7 +268,7 @@ return {
end
AH.movefull_stopunit(ai, attacker, best_attack.dst.x, best_attack.dst.y)
if cfg.show_pack_number then
self:mai_animals_color_label(attacker.x, attacker.y, pack_number)
self:mai_wolves_multipacks_color_label(attacker.x, attacker.y, pack_number)
end
local a_x, a_y, d_x, d_y = attacker.x, attacker.y, defender.x, defender.y
@ -319,7 +319,7 @@ return {
end
AH.movefull_stopunit(ai, w, best_hex)
if cfg.show_pack_number then
self:mai_animals_color_label(w.x, w.y, pack_number)
self:mai_wolves_multipacks_color_label(w.x, w.y, pack_number)
end
end
end
@ -329,7 +329,7 @@ return {
end
function engine:mai_animals_wolves_multipacks_wander_eval(cfg)
function engine:mai_wolves_multipacks_wander_eval(cfg)
local unit_type = cfg.type or "Wolf"
-- When there's nothing to attack, the wolves wander and regroup into their packs
@ -339,9 +339,9 @@ return {
return 0
end
function engine:mai_animals_wolves_multipacks_wander_exec(cfg)
function engine:mai_wolves_multipacks_wander_exec(cfg)
-- First get all the packs
local packs = self:mai_animals_assign_packs(cfg)
local packs = self:mai_wolves_multipacks_assign_packs(cfg)
for k,pack in pairs(packs) do
-- If any of the wolves has a goal set, this is used for the entire pack
@ -456,7 +456,7 @@ return {
end
AH.movefull_stopunit(ai, w, best_hex)
if cfg.show_pack_number then
self:mai_animals_color_label(w.x, w.y, k)
self:mai_wolves_multipacks_color_label(w.x, w.y, k)
end
end
end

View file

@ -318,15 +318,15 @@ function wesnoth.wml_actions.micro_ai(cfg)
elseif (cfg.ai_type == 'big_animals') then
required_keys = { "filter"}
optional_keys = { "avoid_unit", "filter_location", "filter_location_wander" }
CA_parms = { { ca_id = "mai_animals_big", score = cfg.ca_score or 300000 } }
CA_parms = { { ca_id = "mai_big_animals", score = cfg.ca_score or 300000 } }
elseif (cfg.ai_type == 'wolves') then
required_keys = { "filter", "filter_second" }
optional_keys = { "avoid_type" }
local score = cfg.ca_score or 90000
CA_parms = {
{ ca_id = "mai_animals_wolves", score = score },
{ ca_id = "mai_animals_wolves_wander", score = score - 1 }
{ ca_id = "mai_wolves_move", score = score },
{ ca_id = "mai_wolves_wander", score = score - 1 }
}
local wolves_aspects = {
@ -355,12 +355,12 @@ function wesnoth.wml_actions.micro_ai(cfg)
optional_keys = { "attention_distance", "attack_distance" }
local score = cfg.ca_score or 300000
CA_parms = {
{ ca_id = "mai_animals_herding_attack_close_enemy", score = score },
{ ca_id = "mai_animals_sheep_runs_enemy", score = score - 1 },
{ ca_id = "mai_animals_sheep_runs_dog", score = score - 2 },
{ ca_id = "mai_animals_herd_sheep", score = score - 3 },
{ ca_id = "mai_animals_sheep_move", score = score - 4 },
{ ca_id = "mai_animals_dog_move", score = score - 5 }
{ ca_id = "mai_herding_attack_close_enemy", score = score },
{ ca_id = "mai_herding_sheep_runs_enemy", score = score - 1 },
{ ca_id = "mai_herding_sheep_runs_dog", score = score - 2 },
{ ca_id = "mai_herding_herd_sheep", score = score - 3 },
{ ca_id = "mai_herding_sheep_move", score = score - 4 },
{ ca_id = "mai_herding_dog_move", score = score - 5 }
}
elseif (cfg.ai_type == 'forest_animals') then
@ -369,26 +369,26 @@ function wesnoth.wml_actions.micro_ai(cfg)
}
local score = cfg.ca_score or 300000
CA_parms = {
{ ca_id = "mai_animals_new_rabbit", score = score },
{ ca_id = "mai_animals_tusker_attack", score = score - 1 },
{ ca_id = "mai_animals_forest_move", score = score - 2 },
{ ca_id = "mai_animals_tusklet", score = score - 3 }
{ ca_id = "mai_forest_animals_new_rabbit", score = score },
{ ca_id = "mai_forest_animals_tusker_attack", score = score - 1 },
{ ca_id = "mai_forest_animals_forest_move", score = score - 2 },
{ ca_id = "mai_forest_animals_tusklet", score = score - 3 }
}
elseif (cfg.ai_type == 'swarm') then
optional_keys = { "scatter_distance", "vision_distance", "enemy_distance" }
local score = cfg.ca_score or 300000
CA_parms = {
{ ca_id = "mai_animals_scatter_swarm", score = score },
{ ca_id = "mai_animals_move_swarm", score = score - 1 }
{ ca_id = "mai_swarm_scatter", score = score },
{ ca_id = "mai_swarm_move", score = score - 1 }
}
elseif (cfg.ai_type == 'wolves_multipacks') then
optional_keys = { "type", "pack_size", "show_pack_number" }
local score = cfg.ca_score or 300000
CA_parms = {
{ ca_id = "mai_animals_wolves_multipacks_attack", score = score },
{ ca_id = "mai_animals_wolves_multipacks_wander", score = score - 1 }
{ ca_id = "mai_wolves_multipacks_attack", score = score },
{ ca_id = "mai_wolves_multipacks_wander", score = score - 1 }
}
elseif (cfg.ai_type == 'hunter_unit') then
@ -399,7 +399,7 @@ function wesnoth.wml_actions.micro_ai(cfg)
if (not cfg.id) then
H.wml_error("[micro_ai] tag (hunter_unit) is missing required parameter: id")
end
CA_parms = { { ca_id = "mai_animals_hunter_unit", score = cfg.ca_score or 300000, sticky = true } }
CA_parms = { { ca_id = "mai_hunter_unit", score = cfg.ca_score or 300000, sticky = true } }
--------- Patrol Micro AI - BCA AI ------------------------------------
elseif (cfg.ai_type == 'patrol_unit') then