Micro AIs: don't check for specific value of move rating
Don’t check whether rating equals (or doesn’t equal) -9e99 or the like, verify that a valid move was found instead. Also, avoid setting variables to empty tables if not needed.
This commit is contained in:
parent
9d440fac8e
commit
5fc48b3e88
12 changed files with 27 additions and 28 deletions
|
@ -52,7 +52,7 @@ function ca_big_animals:execution(ai, cfg)
|
|||
end)
|
||||
|
||||
-- Now find the one of these hexes that is closest to the goal
|
||||
local max_rating, best_hex = -9e99, {}
|
||||
local max_rating, best_hex = -9e99
|
||||
reach_map:iter( function(x, y, v)
|
||||
local rating = - H.distance_between(x, y, goal.goal_x, goal.goal_y)
|
||||
|
||||
|
|
|
@ -94,7 +94,7 @@ function ca_forest_animals_move:execution(ai, cfg)
|
|||
AH.checked_move(ai, unit, reachable_wander_terrain[rand][1], reachable_wander_terrain[rand][2])
|
||||
end
|
||||
else -- Or if no close reachable terrain was found, move toward the closest
|
||||
local best_hex, min_dist = {}, 9e99
|
||||
local min_dist, best_hex = 9e99
|
||||
for _,loc in ipairs(wander_locs) do
|
||||
local dist = H.distance_between(loc[1], loc[2], unit.x, unit.y)
|
||||
if dist < min_dist then
|
||||
|
@ -102,7 +102,7 @@ function ca_forest_animals_move:execution(ai, cfg)
|
|||
end
|
||||
end
|
||||
|
||||
if (best_hex[1]) then
|
||||
if (best_hex) then
|
||||
local x,y = wesnoth.find_vacant_tile(best_hex[1], best_hex[2], unit)
|
||||
local next_hop = AH.next_hop(unit, x, y)
|
||||
|
||||
|
|
|
@ -119,7 +119,7 @@ function ca_hang_out:execution(ai, cfg, self)
|
|||
|
||||
-- If no valid locations/units were found or all units are in their
|
||||
-- respective best locations already, we take moves away from all units
|
||||
if (max_rating == -9e99) then
|
||||
if (not best_unit) then
|
||||
for _,unit in ipairs(units) do
|
||||
AH.checked_stopunit_moves(ai, unit)
|
||||
|
||||
|
|
|
@ -65,7 +65,7 @@ function ca_healer_move:evaluation(ai, cfg, self)
|
|||
|
||||
local avoid_map = LS.of_pairs(ai.get_avoid())
|
||||
|
||||
local max_rating, best_hex = -9e99
|
||||
local max_rating, best_healer, best_hex = -9e99
|
||||
for _,healer in ipairs(healers) do
|
||||
local reach = wesnoth.find_reach(healer)
|
||||
|
||||
|
@ -118,7 +118,7 @@ function ca_healer_move:evaluation(ai, cfg, self)
|
|||
end
|
||||
end
|
||||
|
||||
if (max_rating > -9e99) then
|
||||
if best_healer then
|
||||
self.data.HS_unit, self.data.HS_hex = best_healer, best_hex
|
||||
return score
|
||||
end
|
||||
|
|
|
@ -49,7 +49,7 @@ function ca_herding_attack_close_enemy:execution(ai, cfg)
|
|||
local radius = cfg.attack_distance or 4
|
||||
local enemies = get_enemies(cfg, radius)
|
||||
|
||||
max_rating, best_dog, best_enemy, best_hex = -9e99, {}, {}, {}
|
||||
max_rating, best_dog, best_enemy, best_hex = -9e99
|
||||
for _,enemy in ipairs(enemies) do
|
||||
for _,dog in ipairs(dogs) do
|
||||
local reach_map = AH.get_reachable_unocc(dog)
|
||||
|
@ -73,7 +73,7 @@ function ca_herding_attack_close_enemy:execution(ai, cfg)
|
|||
end
|
||||
|
||||
-- If we found a move, we do it, and attack if possible
|
||||
if max_rating > -9e99 then
|
||||
if best_dog then
|
||||
AH.movefull_stopunit(ai, best_dog, best_hex)
|
||||
if (not best_dog) or (not best_dog.valid) then return end
|
||||
if (not best_enemy) or (not best_enemy.valid) then return end
|
||||
|
@ -89,7 +89,7 @@ function ca_herding_attack_close_enemy:execution(ai, cfg)
|
|||
local enemies = get_enemies(cfg, radius)
|
||||
|
||||
-- Find closest sheep/enemy pair first
|
||||
local min_dist, closest_sheep, closest_enemy = 9e99, {}, {}
|
||||
local min_dist, closest_sheep, closest_enemy = 9e99
|
||||
for _,enemy in ipairs(enemies) do
|
||||
for _,single_sheep in ipairs(sheep) do
|
||||
local dist = H.distance_between(enemy.x, enemy.y, single_sheep.x, single_sheep.y)
|
||||
|
@ -101,7 +101,7 @@ function ca_herding_attack_close_enemy:execution(ai, cfg)
|
|||
end
|
||||
|
||||
-- Move dogs in between enemies and sheep
|
||||
max_rating, best_dog, best_hex = -9e99, {}, {}
|
||||
max_rating, best_dog, best_hex = -9e99
|
||||
for _,dog in ipairs(dogs) do
|
||||
local reach_map = AH.get_reachable_unocc(dog)
|
||||
reach_map:iter( function(x, y, v)
|
||||
|
|
|
@ -42,7 +42,7 @@ function ca_herding_herd_sheep:execution(ai, cfg)
|
|||
local dogs = get_dogs(cfg)
|
||||
local sheep_to_herd = get_sheep_to_herd(cfg)
|
||||
|
||||
local max_rating, best_dog, best_hex = -9e99, {}, {}
|
||||
local max_rating, best_dog, best_hex = -9e99
|
||||
local c_x, c_y = cfg.herd_x, cfg.herd_y
|
||||
for _,single_sheep in ipairs(sheep_to_herd) do
|
||||
-- Farthest sheep goes first
|
||||
|
|
|
@ -29,7 +29,7 @@ function ca_simple_attack:evaluation(ai, cfg, self)
|
|||
local attacks = AH.get_attacks(units, { include_occupied = true })
|
||||
if (not attacks[1]) then return 0 end
|
||||
|
||||
local max_rating, best_attack = -9e99, {}
|
||||
local max_rating, best_attack = -9e99
|
||||
for _, att in ipairs(attacks) do
|
||||
local valid_target = true
|
||||
if cfg.filter_second and (not enemy_map:get(att.target.x, att.target.y)) then
|
||||
|
@ -44,13 +44,12 @@ function ca_simple_attack:evaluation(ai, cfg, self)
|
|||
local rating = BC.attack_rating(attacker, enemy, dst)
|
||||
|
||||
if (rating > max_rating) then
|
||||
max_rating = rating
|
||||
best_attack = att
|
||||
max_rating, best_attack = rating, att
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if (max_rating > -9e99) then
|
||||
if best_attack then
|
||||
self.data.SA_attack = best_attack
|
||||
return cfg.ca_score
|
||||
end
|
||||
|
|
|
@ -37,7 +37,7 @@ function ca_stationed_guardian:execution(ai, cfg)
|
|||
-- Otherwise, guardian will either attack or move toward station
|
||||
-- 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
|
||||
local min_dist, target = 9e99
|
||||
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)
|
||||
|
@ -49,10 +49,10 @@ function ca_stationed_guardian:execution(ai, cfg)
|
|||
end
|
||||
|
||||
-- If a valid target was found, guardian attacks this target, or moves toward it
|
||||
if (min_dist < 9e99) then
|
||||
if target then
|
||||
-- Find tiles adjacent to the target
|
||||
-- Save the one with the highest defense rating that guardian can reach
|
||||
local best_defense, attack_loc = -9e99, {}
|
||||
local best_defense, attack_loc = -9e99
|
||||
for xa,ya in H.adjacent_tiles(target.x, target.y) do
|
||||
-- Only consider unoccupied hexes
|
||||
local occ_hex = wesnoth.get_units { x = xa, y = ya, { "not", { id = guardian.id } } }[1]
|
||||
|
@ -68,7 +68,7 @@ function ca_stationed_guardian:execution(ai, cfg)
|
|||
end
|
||||
|
||||
-- If a valid hex was found: move there and attack
|
||||
if (best_defense ~= -9e99) then
|
||||
if attack_loc then
|
||||
AH.movefull_stopunit(ai, guardian, attack_loc)
|
||||
if (not guardian) or (not guardian.valid) then return end
|
||||
if (not target) or (not target.valid) then return end
|
||||
|
@ -79,7 +79,7 @@ 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
|
||||
local min_dist, nh = 9e99
|
||||
for _,hex in ipairs(reach) do
|
||||
-- Only consider unoccupied hexes
|
||||
local occ_hex = wesnoth.get_units { x = hex[1], y = hex[2], { "not", { id = guardian.id } } }[1]
|
||||
|
|
|
@ -36,7 +36,7 @@ function ca_wolves_move:execution(ai, cfg)
|
|||
local avoid_map = BC.get_attack_map(avoid_units).units
|
||||
|
||||
-- Find prey that is closest to the wolves
|
||||
local target, min_dist = {}, 9e99
|
||||
local min_dist, target = 9e99
|
||||
for _,prey_unit in ipairs(prey) do
|
||||
local dist = 0
|
||||
for _,wolf in ipairs(wolves) do
|
||||
|
|
|
@ -67,7 +67,7 @@ function wolves_multipacks_functions.assign_packs(cfg)
|
|||
best_wolf, best_ind = wolf, ind
|
||||
end
|
||||
end
|
||||
if (min_dist < 9e99) then
|
||||
if best_wolf then
|
||||
table.insert(packs[pack_number], { x = best_wolf.x, y = best_wolf.y, id = best_wolf.id })
|
||||
MAIUV.set_mai_unit_variables(best_wolf, cfg.ai_id, { pack_number = pack_number })
|
||||
table.remove(nopack_wolves, best_ind)
|
||||
|
|
|
@ -34,7 +34,7 @@ function ca_wolves_wander:execution(ai, cfg)
|
|||
}
|
||||
local avoid_map = BC.get_attack_map(avoid_units).units
|
||||
|
||||
local max_rating, goal_hex = -9e99, {}
|
||||
local max_rating, goal_hex = -9e99
|
||||
reach_map:iter( function (x, y, v)
|
||||
local rating = v + math.random(99)/100.
|
||||
if avoid_map:get(x, y) then rating = rating - 1000 end
|
||||
|
|
|
@ -28,7 +28,7 @@ function ca_zone_guardian:execution(ai, cfg)
|
|||
{ "filter_location", zone_enemy }
|
||||
}
|
||||
if enemies[1] then
|
||||
local target, min_dist = {}, 9e99
|
||||
local min_dist, target = 9e99
|
||||
for _,enemy in ipairs(enemies) do
|
||||
local dist = H.distance_between(guardian.x, guardian.y, enemy.x, enemy.y)
|
||||
if (dist < min_dist) then
|
||||
|
@ -37,10 +37,10 @@ function ca_zone_guardian:execution(ai, cfg)
|
|||
end
|
||||
|
||||
-- If a valid target was found, guardian attacks this target, or moves toward it
|
||||
if (min_dist < 9e99) then
|
||||
if target then
|
||||
-- Find tiles adjacent to the target
|
||||
-- Save the one with the highest defense rating that guardian can reach
|
||||
local best_defense, attack_loc = -9e99, {}
|
||||
local best_defense, attack_loc = -9e99
|
||||
for xa,ya in H.adjacent_tiles(target.x, target.y) do
|
||||
-- Only consider unoccupied hexes
|
||||
local occ_hex = wesnoth.get_units { x = xa, y = ya, { "not", { id = guardian.id } } }[1]
|
||||
|
@ -56,7 +56,7 @@ function ca_zone_guardian:execution(ai, cfg)
|
|||
end
|
||||
|
||||
-- If a valid hex was found: move there and attack
|
||||
if (best_defense > -9e99) then
|
||||
if attack_loc then
|
||||
AH.movefull_stopunit(ai, guardian, attack_loc)
|
||||
if (not guardian) or (not guardian.valid) then return end
|
||||
if (not target) or (not target.valid) then return end
|
||||
|
@ -67,7 +67,7 @@ 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
|
||||
local min_dist, nh = 9e99
|
||||
for _,hex in ipairs(reach) do
|
||||
-- Only consider unoccupied hexes
|
||||
local occ_hex = wesnoth.get_units { x = hex[1], y = hex[2], { "not", { id = guardian.id } } }[1]
|
||||
|
|
Loading…
Add table
Reference in a new issue