EI S8 (Training the Ogres): add custom AI for the ogres

This commit is contained in:
mattsc 2014-02-14 10:12:43 -08:00
parent d4d9960996
commit 4980d5f1ca
2 changed files with 108 additions and 9 deletions

View file

@ -0,0 +1,97 @@
local H = wesnoth.require "lua/helper.lua"
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 ca_ogres_flee = {}
function ca_ogres_flee:evaluation(ai, cfg, self)
local units = wesnoth.get_units { side = wesnoth.current.side,
formula = '$this_unit.moves > 0'
}
if (not units[1]) then return 0 end
return 110000
end
function ca_ogres_flee:execution(ai, cfg, self)
local units = wesnoth.get_units { side = wesnoth.current.side,
formula = '$this_unit.moves > 0'
}
local units_noMP = wesnoth.get_units { side = wesnoth.current.side,
formula = '$this_unit.moves = 0'
}
local width, height = wesnoth.get_map_size()
-- Need the enemy map and enemy attack map if avoid_enemies is set
local enemies = wesnoth.get_units { { "filter_side", { {"enemy_of", {side = wesnoth.current.side} } } } }
local enemy_attack_map = BC.get_attack_map(enemies)
local best_hex, best_unit, max_rating = {}, nil, -9e99
for i,u in ipairs(units) do
local reach = wesnoth.find_reach(u)
--local rating_map = LS.create()
for j,r in ipairs(reach) do
local unit_in_way = wesnoth.get_unit(r[1], r[2])
if (not unit_in_way) or (unit_in_way == u) then
-- First rating is distance from a map edge
local dist_left = r[1] - 1
local dist_right = width - r[1]
local dist_top = r[2] - 1
local dist_bottom = height - r[2]
local dist = math.min(dist_left, dist_right, dist_top, dist_bottom)
local rating = - dist
-- If we can reach the edge, we do so
if (dist == 0) then rating = rating + 1000 end
local enemy_weight = 0.5
local enemy_rating = - (enemy_attack_map.units:get(r[1], r[2]) or 0) * enemy_weight
local enemy_rating = 0
for k,e in ipairs(enemies) do
local dist = H.distance_between(r[1], r[2], e.x, e.y)
enemy_rating = enemy_rating + math.sqrt(dist)
end
rating = rating + enemy_rating
-- Also, maximize distance from own units that have already moved
local own_unit_weight = 0.5
local own_unit_rating = 0
for k,u_noMP in ipairs(units_noMP) do
local dist = H.distance_between(r[1], r[2], u_noMP.x, u_noMP.y)
own_unit_rating = own_unit_rating + math.sqrt(dist)
end
rating = rating + own_unit_rating * own_unit_weight
--rating_map:insert(r[1], r[2], rating)
if (rating > max_rating) then
best_hex = { r[1], r[2] }
best_unit = u
max_rating = rating
end
end
end
--AH.put_labels(rating_map)
end
--print(best_unit.id, best_unit.x, best_unit.y, best_hex[1], best_hex[2], max_rating)
if best_hex then
AH.movefull_outofway_stopunit(ai, best_unit, best_hex[1], best_hex[2])
else
ai.stopunit_moves(best_unit)
end
end
return ca_ogres_flee

View file

@ -46,15 +46,17 @@
team_name=ogres
user_team_name=_"Ogres"
[ai]
{AI_SIMPLE_ALWAYS_ASPECT aggression 0.0}
[goal]
# This ought to make the ogres run for the map edges
name=target_location
[criteria]
{MAP_EDGES_SLF}
[/criteria]
value=99
[/goal]
aggression=1.0
{MODIFY_AI_ADD_CANDIDATE_ACTION 2 main_loop (
[candidate_action]
engine=lua
name=ogres_flee
id=ogres_flee
max_score=110000
location="campaigns/Eastern_Invasion/ai/ca_ogres_flee.lua"
[/candidate_action]
)}
[/ai]
[/side]