Swarm Micro AI: code cleanup

This commit is contained in:
mattsc 2014-04-12 08:34:23 -07:00
parent b6559a82e2
commit b9955c9453
2 changed files with 12 additions and 14 deletions

View file

@ -5,7 +5,7 @@ local ca_swarm_move = {}
function ca_swarm_move:evaluation(ai, cfg)
local units = wesnoth.get_units { side = wesnoth.current.side }
for i,u in ipairs(units) do
for _,u in ipairs(units) do
if (u.moves > 0) then return cfg.ca_score end
end
@ -19,7 +19,7 @@ function ca_swarm_move:execution(ai, cfg)
-- If no close enemies, swarm will move semi-randomly, staying close together, but away from enemies
local all_units = wesnoth.get_units { side = wesnoth.current.side }
local units, units_no_moves = {}, {}
for i,u in ipairs(all_units) do
for _,u in ipairs(all_units) do
if (u.moves > 0) then
table.insert(units, u)
else
@ -28,11 +28,10 @@ function ca_swarm_move:execution(ai, cfg)
end
local enemies = wesnoth.get_units {
{ "filter_side", {{"enemy_of", {side = wesnoth.current.side} }} }
{ "filter_side", { { "enemy_of", {side = wesnoth.current.side } } } }
}
--print('#units, #units_no_moves, #enemies', #units, #units_no_moves, #enemies)
-- pick one unit at random
-- Pick one unit at random, swarm does not move systematically
local unit = units[math.random(#units)]
-- Find best place for that unit to move to
@ -41,7 +40,7 @@ function ca_swarm_move:execution(ai, cfg)
-- Only units within 'vision_distance' count for rejoining
local close_units_no_moves = {}
for i,u in ipairs(units_no_moves) do
for _,u in ipairs(units_no_moves) do
if (H.distance_between(unit.x, unit.y, u.x, u.y) <= vision_distance) then
table.insert(close_units_no_moves, u)
end
@ -50,16 +49,15 @@ function ca_swarm_move:execution(ai, cfg)
-- If all units on the side have moves left, simply go to a hex far away
if (not close_units_no_moves[1]) then
rating = rating + H.distance_between(x, y, unit.x, unit.y)
else -- otherwise, minimize distance from units that have already moved
for i,u in ipairs(close_units_no_moves) do
else -- Otherwise, minimize distance from units that have already moved
for _,u in ipairs(close_units_no_moves) do
rating = rating - H.distance_between(x, y, u.x, u.y)
end
end
-- We also try to stay out of attack range of any enemy
for i,e in ipairs(enemies) do
for _,e in ipairs(enemies) do
local dist = H.distance_between(x, y, e.x, e.y)
-- If enemy is within attack range, avoid those hexes
if (dist < enemy_distance) then
rating = rating - (enemy_distance - dist) * 10.
end

View file

@ -4,7 +4,7 @@ local AH = wesnoth.require "ai/lua/ai_helper.lua"
local function get_enemies(cfg)
local scatter_distance = cfg.scatter_distance or 3
local enemies = wesnoth.get_units {
{ "filter_side", {{"enemy_of", {side = wesnoth.current.side} }} },
{ "filter_side", { { "enemy_of", { side = wesnoth.current.side } } } },
{ "filter_location",
{ radius = scatter_distance, { "filter", { side = wesnoth.current.side } } }
}
@ -31,9 +31,9 @@ function ca_swarm_scatter:execution(ai, cfg)
-- In this case we simply maximize the distance from all these close enemies
-- but only for units that are within 'vision_distance' of one of those enemies
for i,unit in ipairs(units) do
for _,unit in ipairs(units) do
local unit_enemies = {}
for i,e in ipairs(enemies) do
for _,e in ipairs(enemies) do
if (H.distance_between(unit.x, unit.y, e.x, e.y) <= vision_distance) then
table.insert(unit_enemies, e)
end
@ -42,7 +42,7 @@ function ca_swarm_scatter:execution(ai, cfg)
if unit_enemies[1] then
local best_hex = AH.find_best_move(unit, function(x, y)
local rating = 0
for i,e in ipairs(unit_enemies) do
for _,e in ipairs(unit_enemies) do
rating = rating + H.distance_between(x, y, e.x, e.y)
end
return rating