Skip to content

Commit

Permalink
Micro AIs: make loop variables more descriptive
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsc committed Apr 14, 2014
1 parent e65705a commit 8640639
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 66 deletions.
20 changes: 10 additions & 10 deletions data/ai/micro_ais/cas/ca_coward.lua
Expand Up @@ -36,13 +36,13 @@ function ca_coward:execution(ai, cfg)
return
end

for i,r in ipairs(reach) do
for i,hex in ipairs(reach) do
-- Only consider unoccupied hexes
local occ_hex = wesnoth.get_units { x = r[1], y = r[2], { "not", { id = coward.id } } }[1]
local occ_hex = wesnoth.get_units { x = hex[1], y = hex[2], { "not", { id = coward.id } } }[1]
if not occ_hex then
local rating = 0
for _,e in ipairs(enemies) do
local dist = H.distance_between(r[1], r[2], e.x, e.y)
for _,enemy in ipairs(enemies) do
local dist = H.distance_between(hex[1], hex[2], enemy.x, enemy.y)
rating = rating - 1 / dist^2
end

Expand All @@ -58,10 +58,10 @@ function ca_coward:execution(ai, cfg)
local best_pos = AH.filter(reach, function(tmp) return tmp[3] > reach[1][3] * 2 end)

-- Now take 'seek' and 'avoid' into account
for i,b in ipairs(best_pos) do
for i,pos in ipairs(best_pos) do
-- Weighting based on distance from 'seek' and 'avoid'
local dist_seek = AH.generalized_distance(b[1], b[2], cfg.seek_x, cfg.seek_y)
local dist_avoid = AH.generalized_distance(b[1], b[2], cfg.avoid_x, cfg.avoid_y)
local dist_seek = AH.generalized_distance(pos[1], pos[2], cfg.seek_x, cfg.seek_y)
local dist_avoid = AH.generalized_distance(pos[1], pos[2], cfg.avoid_x, cfg.avoid_y)
local rating = 1 / (dist_seek + 1) - 1 / (dist_avoid + 1)^2 * 0.75

best_pos[i][4] = rating
Expand All @@ -74,9 +74,9 @@ function ca_coward:execution(ai, cfg)
-- As final step, if there are more than one remaining locations,
-- we take the one with the minimum score in the distance-from-enemy criterion
local max_rating, best_hex = -9e99
for _,b in ipairs(best_overall) do
if (b[3] > max_rating) then
max_rating, best_hex = b[3], b
for _,pos in ipairs(best_overall) do
if (pos[3] > max_rating) then
max_rating, best_hex = pos[3], pos
end
end

Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_patrol.lua
Expand Up @@ -83,7 +83,7 @@ function ca_patrol:execution(ai, cfg)
MAIUV.set_mai_unit_variables(patrol, cfg.ai_id, patrol_vars)

local tmp_wp = {}
for j,wp in ipairs(waypoints) do tmp_wp[n_wp-j+1] = wp end
for j,wp2 in ipairs(waypoints) do tmp_wp[n_wp-j+1] = wp2 end
waypoints = tmp_wp
else
patrol_vars.patrol_x = waypoints[1][1]
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_simple_attack.lua
Expand Up @@ -22,7 +22,7 @@ function ca_simple_attack:evaluation(ai, cfg, self)
if (not enemies[1]) then return 0 end

enemy_map = LS.create()
for _,e in ipairs(enemies) do enemy_map:insert(e.x, e.y) end
for _,enemy in ipairs(enemies) do enemy_map:insert(enemy.x, enemy.y) end
end

-- Now find the best of the possible attacks
Expand Down
28 changes: 14 additions & 14 deletions data/ai/micro_ais/cas/ca_stationed_guardian.lua
Expand Up @@ -38,13 +38,13 @@ function ca_stationed_guardian:execution(ai, cfg)
-- Enemies must be within cfg.distance of guardian, (s_x, s_y) *and* (g_x, g_y)
-- simultaneously for guardian to attack
local target, min_dist = {}, 9e99
for _,e in ipairs(enemies) do
local dist_s = H.distance_between(cfg.station_x, cfg.station_y, e.x, e.y)
local dist_g = H.distance_between(cfg.guard_x, cfg.guard_y, e.x, e.y)
for _,enemy in ipairs(enemies) do
local dist_s = H.distance_between(cfg.station_x, cfg.station_y, enemy.x, enemy.y)
local dist_g = H.distance_between(cfg.guard_x, cfg.guard_y, enemy.x, enemy.y)

-- If valid target found, save the one with the shortest distance from (g_x, g_y)
if (dist_s <= cfg.distance) and (dist_g <= cfg.distance) and (dist_g < min_dist) then
target, min_dist = e, dist_g
target, min_dist = enemy, dist_g
end
end

Expand All @@ -53,15 +53,15 @@ function ca_stationed_guardian:execution(ai, cfg)
-- Find tiles adjacent to the target
-- Save the one with the highest defense rating that guardian can reach
local best_defense, attack_loc = -9e99, {}
for x,y in H.adjacent_tiles(target.x, target.y) do
for xa,ya in H.adjacent_tiles(target.x, target.y) do
-- Only consider unoccupied hexes
local occ_hex = wesnoth.get_units { x = x, y = y, { "not", { id = guardian.id } } }[1]
local occ_hex = wesnoth.get_units { x = xa, y = ya, { "not", { id = guardian.id } } }[1]
if not occ_hex then
local defense = 100 - wesnoth.unit_defense(guardian, wesnoth.get_terrain(x, y))
local nh = AH.next_hop(guardian, x, y)
local defense = 100 - wesnoth.unit_defense(guardian, wesnoth.get_terrain(xa, ya))
local nh = AH.next_hop(guardian, xa, ya)
if nh then
if (nh[1] == x) and (nh[2] == y) and (defense > best_defense) then
best_defense, attack_loc = defense, {x, y}
if (nh[1] == xa) and (nh[2] == ya) and (defense > best_defense) then
best_defense, attack_loc = defense, { xa, ya }
end
end
end
Expand All @@ -80,13 +80,13 @@ function ca_stationed_guardian:execution(ai, cfg)
-- Go through all hexes the guardian can reach, find closest to target
-- Cannot use next_hop here since target hex is occupied by enemy
local nh, min_dist = {}, 9e99
for _,r in ipairs(reach) do
for _,hex in ipairs(reach) do
-- Only consider unoccupied hexes
local occ_hex = wesnoth.get_units { x = r[1], y = r[2], { "not", { id = guardian.id } } }[1]
local occ_hex = wesnoth.get_units { x = hex[1], y = hex[2], { "not", { id = guardian.id } } }[1]
if not occ_hex then
local dist = H.distance_between(r[1], r[2], target.x, target.y)
local dist = H.distance_between(hex[1], hex[2], target.x, target.y)
if (dist < min_dist) then
min_dist, nh = d, { r[1], r[2] }
min_dist, nh = dist, { hex[1], hex[2] }
end
end
end
Expand Down
26 changes: 13 additions & 13 deletions data/ai/micro_ais/cas/ca_swarm_move.lua
Expand Up @@ -5,8 +5,8 @@ local ca_swarm_move = {}

function ca_swarm_move:evaluation(ai, cfg)
local units = wesnoth.get_units { side = wesnoth.current.side }
for _,u in ipairs(units) do
if (u.moves > 0) then return cfg.ca_score end
for _,unit in ipairs(units) do
if (unit.moves > 0) then return cfg.ca_score end
end

return 0
Expand All @@ -19,11 +19,11 @@ 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 _,u in ipairs(all_units) do
if (u.moves > 0) then
table.insert(units, u)
for _,unit in ipairs(all_units) do
if (unit.moves > 0) then
table.insert(units, unit)
else
table.insert(units_no_moves, u)
table.insert(units_no_moves, unit)
end
end

Expand All @@ -40,24 +40,24 @@ function ca_swarm_move:execution(ai, cfg)

-- Only units within 'vision_distance' count for rejoining
local close_units_no_moves = {}
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)
for _,unit_noMP in ipairs(units_no_moves) do
if (H.distance_between(unit.x, unit.y, unit_noMP.x, unit_noMP.y) <= vision_distance) then
table.insert(close_units_no_moves, unit_noMP)
end
end

-- 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 _,u in ipairs(close_units_no_moves) do
rating = rating - H.distance_between(x, y, u.x, u.y)
for _,close_unit in ipairs(close_units_no_moves) do
rating = rating - H.distance_between(x, y, close_unit.x, close_unit.y)
end
end

-- We also try to stay out of attack range of any enemy
for _,e in ipairs(enemies) do
local dist = H.distance_between(x, y, e.x, e.y)
for _,enemy in ipairs(enemies) do
local dist = H.distance_between(x, y, enemy.x, enemy.y)
if (dist < enemy_distance) then
rating = rating - (enemy_distance - dist) * 10.
end
Expand Down
10 changes: 5 additions & 5 deletions data/ai/micro_ais/cas/ca_swarm_scatter.lua
Expand Up @@ -33,17 +33,17 @@ function ca_swarm_scatter:execution(ai, cfg)
-- but only for units that are within 'vision_distance' of one of those enemies
for _,unit in ipairs(units) do
local unit_enemies = {}
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)
for _,enemy in ipairs(enemies) do
if (H.distance_between(unit.x, unit.y, enemy.x, enemy.y) <= vision_distance) then
table.insert(unit_enemies, enemy)
end
end

if unit_enemies[1] then
local best_hex = AH.find_best_move(unit, function(x, y)
local rating = 0
for _,e in ipairs(unit_enemies) do
rating = rating + H.distance_between(x, y, e.x, e.y)
for _,enemy in ipairs(unit_enemies) do
rating = rating + H.distance_between(x, y, enemy.x, enemy.y)
end
return rating
end)
Expand Down
8 changes: 4 additions & 4 deletions data/ai/micro_ais/cas/ca_wolves_move.lua
Expand Up @@ -37,13 +37,13 @@ function ca_wolves_move:execution(ai, cfg)

-- Find prey that is closest to the wolves
local target, min_dist = {}, 9e99
for _,p in ipairs(prey) do
for _,prey_unit in ipairs(prey) do
local dist = 0
for _,w in ipairs(wolves) do
dist = dist + H.distance_between(w.x, w.y, p.x, p.y)
for _,wolf in ipairs(wolves) do
dist = dist + H.distance_between(wolf.x, wolf.y, prey_unit.x, prey_unit.y)
end
if (dist < min_dist) then
min_dist, target = dist, p
min_dist, target = dist, prey_unit
end
end

Expand Down
10 changes: 5 additions & 5 deletions data/ai/micro_ais/cas/ca_wolves_wander.lua
Expand Up @@ -24,8 +24,8 @@ function ca_wolves_wander:execution(ai, cfg)

-- Number of wolves that can reach each hex
local reach_map = LS.create()
for _,w in ipairs(wolves) do
local r = AH.get_reachable_unocc(w)
for _,wolf in ipairs(wolves) do
local r = AH.get_reachable_unocc(wolf)
reach_map:union_merge(r, function(x, y, v1, v2) return (v1 or 0) + (v2 or 0) end)
end

Expand All @@ -46,15 +46,15 @@ function ca_wolves_wander:execution(ai, cfg)
reach_map:insert(x, y, rating)
end)

for _,w in ipairs(wolves) do
for _,wolf in ipairs(wolves) do
-- For each wolf, we need to check that goal hex is reachable, and out of harm's way
local best_hex = AH.find_best_move(w, function(x, y)
local best_hex = AH.find_best_move(wolf, function(x, y)
local rating = - H.distance_between(x, y, goal_hex[1], goal_hex[2])
if avoid_map:get(x, y) then rating = rating - 1000 end
return rating
end)

AH.movefull_stopunit(ai, w, best_hex)
AH.movefull_stopunit(ai, wolf, best_hex)
end
end

Expand Down
26 changes: 13 additions & 13 deletions data/ai/micro_ais/cas/ca_zone_guardian.lua
Expand Up @@ -29,10 +29,10 @@ function ca_zone_guardian:execution(ai, cfg)
}
if enemies[1] then
local target, min_dist = {}, 9e99
for _,e in ipairs(enemies) do
local dist = H.distance_between(guardian.x, guardian.y, e.x, e.y)
for _,enemy in ipairs(enemies) do
local dist = H.distance_between(guardian.x, guardian.y, enemy.x, enemy.y)
if (dist < min_dist) then
target, min_dist = e, dist
target, min_dist = enemy, dist
end
end

Expand All @@ -41,15 +41,15 @@ function ca_zone_guardian:execution(ai, cfg)
-- Find tiles adjacent to the target
-- Save the one with the highest defense rating that guardian can reach
local best_defense, attack_loc = -9e99, {}
for x,y in H.adjacent_tiles(target.x, target.y) do
for xa,ya in H.adjacent_tiles(target.x, target.y) do
-- Only consider unoccupied hexes
local occ_hex = wesnoth.get_units { x = x, y = y, { "not", { id = guardian.id } } }[1]
local occ_hex = wesnoth.get_units { x = xa, y = ya, { "not", { id = guardian.id } } }[1]
if not occ_hex then
local defense = 100 - wesnoth.unit_defense(guardian, wesnoth.get_terrain(x, y))
local nh = AH.next_hop(guardian, x, y)
local defense = 100 - wesnoth.unit_defense(guardian, wesnoth.get_terrain(xa, ya))
local nh = AH.next_hop(guardian, xa, ya)
if nh then
if (nh[1] == x) and (nh[2] == y) and (defense > best_defense) then
best_defense, attack_loc = defense, { x, y }
if (nh[1] == xa) and (nh[2] == ya) and (defense > best_defense) then
best_defense, attack_loc = defense, { xa, ya }
end
end
end
Expand All @@ -68,13 +68,13 @@ function ca_zone_guardian:execution(ai, cfg)
-- Go through all hexes the guardian can reach, find closest to target
-- Cannot use next_hop here since target hex is occupied by enemy
local nh, min_dist = {}, 9e99
for _,r in ipairs(reach) do
for _,hex in ipairs(reach) do
-- Only consider unoccupied hexes
local occ_hex = wesnoth.get_units { x = r[1], y = r[2], { "not", { id = guardian.id } } }[1]
local occ_hex = wesnoth.get_units { x = hex[1], y = hex[2], { "not", { id = guardian.id } } }[1]
if not occ_hex then
local dist = H.distance_between(r[1], r[2], target.x, target.y)
local dist = H.distance_between(hex[1], hex[2], target.x, target.y)
if (dist < min_dist) then
min_dist, nh = dist, { r[1], r[2] }
min_dist, nh = dist, { hex[1], hex[2] }
end
end
end
Expand Down

0 comments on commit 8640639

Please sign in to comment.