Skip to content

Commit

Permalink
Lua AIs: use math.huge to initialize extrema variables
Browse files Browse the repository at this point in the history
(cherry-picked from commit cb423b2)
  • Loading branch information
mattsc committed Oct 7, 2018
1 parent 613ccd8 commit 37e83f1
Show file tree
Hide file tree
Showing 38 changed files with 80 additions and 80 deletions.
8 changes: 4 additions & 4 deletions data/ai/lua/ai_helper.lua
Expand Up @@ -1052,7 +1052,7 @@ function ai_helper.get_closest_enemy(loc, side, cfg)
x, y = loc[1], loc[2]
end

local closest_distance, location = 9e99
local closest_distance, location = math.huge
for _,enemy in ipairs(enemies) do
enemy_distance = M.distance_between(x, y, enemy.x, enemy.y)
if (enemy_distance < closest_distance) then
Expand Down Expand Up @@ -1088,7 +1088,7 @@ function ai_helper.has_weapon_special(unit, special)
end

function ai_helper.get_cheapest_recruit_cost()
local cheapest_unit_cost = 9e99
local cheapest_unit_cost = math.huge
for _,recruit_id in ipairs(wesnoth.sides[wesnoth.current.side].recruit) do
if wesnoth.unit_types[recruit_id].cost < cheapest_unit_cost then
cheapest_unit_cost = wesnoth.unit_types[recruit_id].cost
Expand Down Expand Up @@ -1408,7 +1408,7 @@ function ai_helper.find_best_move(units, rating_function, cfg)
-- If this is an individual unit, turn it into an array
if units.hitpoints then units = { units } end

local max_rating, best_hex, best_unit = -9e99, {}, {}
local max_rating, best_hex, best_unit = - math.huge, {}, {}
for _,unit in ipairs(units) do
-- Hexes each unit can reach
local reach_map = ai_helper.get_reachable_unocc(unit, cfg)
Expand Down Expand Up @@ -1452,7 +1452,7 @@ function ai_helper.move_unit_out_of_way(ai, unit, cfg)
local reach = wesnoth.find_reach(unit, cfg)
local reach_map = LS.create()

local max_rating, best_hex = -9e99
local max_rating, best_hex = - math.huge
for _,loc in ipairs(reach) do
local unit_in_way = wesnoth.get_unit(loc[1], loc[2])
if (not unit_in_way) -- also excludes current hex
Expand Down
14 changes: 7 additions & 7 deletions data/ai/lua/battle_calcs.lua
Expand Up @@ -199,10 +199,10 @@ function battle_calcs.best_weapons(attacker, defender, dst, cache)
local defender_info = battle_calcs.unit_attack_info(defender, cache)

-- Best attacker weapon
local max_rating, best_att_weapon, best_def_weapon = -9e99, 0, 0
local max_rating, best_att_weapon, best_def_weapon = - math.huge, 0, 0
for att_weapon_number,att_weapon in ipairs(attacker_info.attacks) do
local att_damage = battle_calcs.strike_damage(attacker, defender, att_weapon_number, 0, { dst[1], dst[2] }, cache)
local max_def_rating, tmp_best_def_weapon = -9e99, 0
local max_def_rating, tmp_best_def_weapon = - math.huge, 0
for def_weapon_number,def_weapon in ipairs(defender_info.attacks) do
if (def_weapon.range == att_weapon.range) then
local def_damage = battle_calcs.strike_damage(defender, attacker, def_weapon_number, 0, { defender.x, defender.y }, cache)
Expand All @@ -214,7 +214,7 @@ function battle_calcs.best_weapons(attacker, defender, dst, cache)
end

local rating = att_damage * att_weapon.number
if (max_def_rating > -9e99) then rating = rating - max_def_rating / 2. end
if (max_def_rating > - math.huge) then rating = rating - max_def_rating / 2. end

if (rating > max_rating) then
max_rating, best_att_weapon, best_def_weapon = rating, att_weapon_number, tmp_best_def_weapon
Expand Down Expand Up @@ -1245,7 +1245,7 @@ function battle_calcs.relative_damage_map(units, enemies, cache)
-- against any of the enemy units
local unit_ratings = {}
for i,unit in ipairs(units) do
local max_rating, best_enemy = -9e99, {}
local max_rating, best_enemy = - math.huge, {}
for _,enemy in ipairs(enemies) do
local rating, defender_rating, attacker_rating =
battle_calcs.attack_rating(unit, enemy, { unit.x, unit.y }, { enemy_leader_weight = 1 }, cache)
Expand All @@ -1262,7 +1262,7 @@ function battle_calcs.relative_damage_map(units, enemies, cache)
-- Then we want the same thing for all of the enemy units (for the counter attack on enemy turn)
local enemy_ratings = {}
for i,enemy in ipairs(enemies) do
local max_rating, best_unit = -9e99, {}
local max_rating, best_unit = - math.huge, {}
for _,unit in ipairs(units) do
local rating, defender_rating, attacker_rating =
battle_calcs.attack_rating(enemy, unit, { enemy.x, enemy.y }, { enemy_leader_weight = 1 }, cache)
Expand Down Expand Up @@ -1323,7 +1323,7 @@ function battle_calcs.best_defense_map(units, cfg)
for _,loc in ipairs(reach) do
local defense = 100 - unit:defense(wesnoth.get_terrain(loc[1], loc[2]))

if (defense > (defense_map:get(loc[1], loc[2]) or -9e99)) then
if (defense > (defense_map:get(loc[1], loc[2]) or - math.huge)) then
defense_map:insert(loc[1], loc[2], defense)
end
end
Expand Down Expand Up @@ -1373,7 +1373,7 @@ function battle_calcs.get_attack_combos_subset(units, enemy, cfg)

cfg = cfg or {}
cfg.order_matters = cfg.order_matters or false
cfg.max_combos = cfg.max_combos or 9e99
cfg.max_combos = cfg.max_combos or math.huge
cfg.max_time = cfg.max_time or false
cfg.skip_presort = cfg.skip_presort or 5

Expand Down
10 changes: 5 additions & 5 deletions data/ai/lua/generic_rush_engine.lua
Expand Up @@ -298,7 +298,7 @@ return {
local enemy_attack_map = BC.get_attack_map(enemies).units

-- Now we go through the villages and units
local max_rating, best_village, best_unit = -9e99, {}, {}
local max_rating, best_village, best_unit = - math.huge, {}, {}
local village_ratings = {}
for j,v in ipairs(villages) do
-- First collect all information that only depends on the village
Expand All @@ -322,7 +322,7 @@ return {
local enemy_distance_from_village = AH.get_closest_enemy(v)

-- Now we go on to the unit-dependent rating
local best_unit_rating = -9e99
local best_unit_rating = - math.huge
local reachable = false
for i,u in ipairs(units) do
-- Skip villages that have units other than 'u' itself on them
Expand Down Expand Up @@ -370,7 +370,7 @@ return {
end
end

if (max_rating > -9e99) then
if (max_rating > - math.huge) then
self.data.unit, self.data.village = best_unit, best_village
if (max_rating >= 1000) then
if AH.print_eval() then AH.done_eval_messages(start_time, ca_name) end
Expand Down Expand Up @@ -423,7 +423,7 @@ return {
end

-- Go through all possible attacks with poisoners
local max_rating, best_attack = -9e99, {}
local max_rating, best_attack = - math.huge, {}
for i,a in ipairs(attacks) do
local attacker = wesnoth.get_unit(a.src.x, a.src.y)
local defender = wesnoth.get_unit(a.target.x, a.target.y)
Expand Down Expand Up @@ -465,7 +465,7 @@ return {
end
end

if (max_rating > -9e99) then
if (max_rating > - math.huge) then
self.data.attack = best_attack
if AH.print_eval() then AH.done_eval_messages(start_time, ca_name) end
return 190000
Expand Down
2 changes: 1 addition & 1 deletion data/ai/lua/retreat.lua
Expand Up @@ -114,7 +114,7 @@ function retreat_functions.get_retreat_injured_units(healees, regenerates)

local healing_locs = retreat_functions.get_healing_locations()

local max_rating, best_loc, best_unit = -9e99, nil, nil
local max_rating, best_loc, best_unit = - math.huge, nil, nil
for i,u in ipairs(healees) do
local possible_locations = wesnoth.find_reach(u)
-- TODO: avoid ally's villages (may be preferable to lower rating so they will
Expand Down
4 changes: 2 additions & 2 deletions data/ai/micro_ais/cas/ca_big_animals.lua
Expand Up @@ -58,7 +58,7 @@ function ca_big_animals:execution(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 = - math.huge
reach_map:iter( function(x, y, v)
local rating = -wesnoth.map.distance_between(x, y, goal.goal_x, goal.goal_y)

Expand Down Expand Up @@ -95,7 +95,7 @@ function ca_big_animals:execution(cfg)
end

-- Finally, if the unit ended up next to enemies, attack the weakest of those
local min_hp, target = 9e99
local min_hp, target = math.huge
for xa,ya in H.adjacent_tiles(unit.x, unit.y) do
local enemy = wesnoth.get_unit(xa, ya)
if AH.is_attackable_enemy(enemy) then
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_bottleneck_attack.lua
Expand Up @@ -11,7 +11,7 @@ function ca_bottleneck_attack:evaluation(cfg, data)
}
if (not attackers[1]) then return 0 end

local max_rating, best_attacker, best_target, best_weapon = -9e99
local max_rating, best_attacker, best_target, best_weapon = - math.huge
for _,attacker in ipairs(attackers) do
local targets = AH.get_attackable_enemies { { "filter_adjacent", { id = attacker.id } } }

Expand Down
8 changes: 4 additions & 4 deletions data/ai/micro_ais/cas/ca_bottleneck_move.lua
Expand Up @@ -25,7 +25,7 @@ local function bottleneck_is_my_territory(map, enemy_map)
dummy_unit.x, dummy_unit.y = x, y

-- Find lowest movement cost to own front-line hexes
local min_cost, best_path = 9e99
local min_cost, best_path = math.huge
map:iter(function(xm, ym, v)
local path, cost = AH.find_path_with_shroud(dummy_unit, xm, ym, { ignore_units = true })
if (cost < min_cost) then
Expand All @@ -34,7 +34,7 @@ local function bottleneck_is_my_territory(map, enemy_map)
end)

-- And the same to the enemy front line
local min_cost_enemy, best_path_enemy = 9e99
local min_cost_enemy, best_path_enemy = math.huge
enemy_map:iter(function(xm, ym, v)
local path, cost = AH.find_path_with_shroud(dummy_unit, xm, ym, { ignore_units = true })
if (cost < min_cost_enemy) then
Expand Down Expand Up @@ -192,7 +192,7 @@ local function bottleneck_move_out_of_way(unit_in_way, data)
occ_hexes:insert(unit.x, unit.y)
end

local best_reach, best_hex = -9e99
local best_reach, best_hex = - math.huge
for _,loc in ipairs(reach) do
if data.BD_is_my_territory:get(loc[1], loc[2]) and (not occ_hexes:get(loc[1], loc[2])) then
-- Criterion: MP left after the move has been done
Expand Down Expand Up @@ -273,7 +273,7 @@ function ca_bottleneck_move:evaluation(cfg, data)
for xa,ya in H.adjacent_tiles(healer.x, healer.y) do
-- Cannot be on the line, and needs to be in own territory
if data.BD_is_my_territory:get(xa, ya) then
local min_dist = 9e99
local min_dist = math.huge
data.BD_def_map:iter( function(xd, yd, vd)
local dist_line = M.distance_between(xa, ya, xd, yd)
if (dist_line < min_dist) then min_dist = dist_line end
Expand Down
6 changes: 3 additions & 3 deletions data/ai/micro_ais/cas/ca_coward.lua
Expand Up @@ -52,7 +52,7 @@ function ca_coward:execution(cfg)
-- Store this weighting in the third field of each 'reach' element
reach[i][3] = rating
else
reach[i][3] = -9e99
reach[i][3] = - math.huge
end
end

Expand All @@ -76,7 +76,7 @@ function ca_coward:execution(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
local max_rating, best_hex = - math.huge
for _,pos in ipairs(best_overall) do
if (pos[3] > max_rating) then
max_rating, best_hex = pos[3], pos
Expand All @@ -88,7 +88,7 @@ function ca_coward:execution(cfg)

-- If 'attack_if_trapped' is set, the coward attacks the weakest unit it ends up next to
if cfg.attack_if_trapped then
local max_rating, best_target = -9e99
local max_rating, best_target = - math.huge
for xa,ya in H.adjacent_tiles(coward.x, coward.y) do
local target = wesnoth.get_unit(xa, ya)
if target and wesnoth.is_enemy(coward.side, target.side) then
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_fast_combat.lua
Expand Up @@ -84,7 +84,7 @@ function ca_fast_combat:evaluation(cfg, data)
local attacks = AH.get_attacks({ unit }, { include_occupied = cfg.include_occupied_attack_hexes, viewing_side = viewing_side })

if (#attacks > 0) then
local max_rating, best_target, best_dst = -9e99
local max_rating, best_target, best_dst = - math.huge
for _,attack in ipairs(attacks) do
if enemy_map:get(attack.target.x, attack.target.y)
and (not avoid_map:get(attack.dst.x, attack.dst.y))
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_fast_combat_leader.lua
Expand Up @@ -119,7 +119,7 @@ function ca_fast_combat_leader:evaluation(cfg, data)
local attacks = AH.get_attacks({ leader }, { include_occupied = cfg.include_occupied_attack_hexes, viewing_side = viewing_side })

if (#attacks > 0) then
local max_rating, best_target, best_dst = -9e99
local max_rating, best_target, best_dst = - math.huge
for _,attack in ipairs(attacks) do
if enemy_map:get(attack.target.x, attack.target.y)
and (not avoid_map:get(attack.dst.x, attack.dst.y))
Expand Down
6 changes: 3 additions & 3 deletions data/ai/micro_ais/cas/ca_fast_move.lua
Expand Up @@ -143,7 +143,7 @@ function ca_fast_move:execution(cfg)
if (next_goal > #goals) then next_goal = 1 end
local goal = goals[next_goal]

local max_rating, best_unit_info = -9e99
local max_rating, best_unit_info = - math.huge
for _,unit_info in ipairs(goal) do
if (not unit_info.cost) then
local _,cost =
Expand Down Expand Up @@ -198,7 +198,7 @@ function ca_fast_move:execution(cfg)
local reach = wesnoth.find_reach(unit)

local pre_ratings = {}
local max_rating, best_hex = -9e99
local max_rating, best_hex = - math.huge
for _,loc in ipairs(reach) do
if (not avoid_map:get(loc[1], loc[2])) then
local rating = -M.distance_between(loc[1], loc[2], short_goal[1], short_goal[2])
Expand Down Expand Up @@ -246,7 +246,7 @@ function ca_fast_move:execution(cfg)
unit:extract()
local old_x, old_y = unit.x, unit.y

local max_rating = -9e99
local max_rating = - math.huge
for _,pre_rating in ipairs(pre_ratings) do
-- If pre_rating is worse than the full rating, we are done because the
-- move cost can never be less than the distance, so we cannot possibly do
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_forest_animals_move.lua
Expand Up @@ -96,7 +96,7 @@ function ca_forest_animals_move:execution(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 min_dist, best_hex = 9e99
local min_dist, best_hex = math.huge
for _,loc in ipairs(wander_locs) do
local dist = M.distance_between(loc[1], loc[2], unit.x, unit.y)
if dist < min_dist then
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_forest_animals_tusker_attack.lua
Expand Up @@ -32,7 +32,7 @@ function ca_forest_animals_tusker_attack:execution(cfg)
local adjacent_enemies = get_adjacent_enemies(cfg)

-- Find the closest enemy to any tusker
local min_dist, attacker, target = 9e99
local min_dist, attacker, target = math.huge
for _,tusker in ipairs(tuskers) do
for _,enemy in ipairs(adjacent_enemies) do
local dist = M.distance_between(tusker.x, tusker.y, enemy.x, enemy.y)
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_forest_animals_tusklet_move.lua
Expand Up @@ -33,7 +33,7 @@ function ca_forest_animals_tusklet_move:execution(cfg)
local tusklet = get_tusklets(cfg)[1]
local tuskers = get_tuskers(cfg)

local goto_tusker, min_dist = {}, 9e99
local goto_tusker, min_dist = {}, math.huge
for _,tusker in ipairs(tuskers) do
local dist = M.distance_between(tusker.x, tusker.y, tusklet.x, tusklet.y)
if (dist < min_dist) then
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_goto.lua
Expand Up @@ -118,7 +118,7 @@ function ca_goto:execution(cfg, data)
enemy_attack_map = BC.get_attack_map(live_enemies)
end

local max_rating, closest_hex, best_path, best_unit = -9e99
local max_rating, closest_hex, best_path, best_unit = - math.huge
for _,unit in ipairs(units) do
for _,loc in ipairs(locs) do
-- If cfg.use_straight_line is set, we simply find the closest
Expand Down
4 changes: 2 additions & 2 deletions data/ai/micro_ais/cas/ca_hang_out.lua
Expand Up @@ -83,11 +83,11 @@ function ca_hang_out:execution(cfg)
avoid_map = LS.of_pairs(wesnoth.get_locations { terrain = 'C*,C*^*,*^C*' })
end

local max_rating, best_hex, best_unit = -9e99
local max_rating, best_hex, best_unit = - math.huge
for _,unit in ipairs(units) do
-- Only consider units that have not been marked yet
if (not MAIUV.get_mai_unit_variables(unit, cfg.ai_id, "moved")) then
local max_rating_unit, best_hex_unit = -9e99
local max_rating_unit, best_hex_unit = - math.huge

-- Check out all unoccupied hexes the unit can reach
local reach_map = AH.get_reachable_unocc(unit)
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_healer_move.lua
Expand Up @@ -63,7 +63,7 @@ function ca_healer_move:evaluation(cfg, data)

local avoid_map = LS.of_pairs(ai.aspects.avoid)

local max_rating = -9e99
local max_rating = - math.huge
for _,healer in ipairs(healers) do
local reach = wesnoth.find_reach(healer)

Expand Down
6 changes: 3 additions & 3 deletions data/ai/micro_ais/cas/ca_herding_attack_close_enemy.lua
Expand Up @@ -48,7 +48,7 @@ function ca_herding_attack_close_enemy:execution(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 = - math.huge
for _,enemy in ipairs(enemies) do
for _,dog in ipairs(dogs) do
local reach_map = AH.get_reachable_unocc(dog)
Expand Down Expand Up @@ -90,7 +90,7 @@ function ca_herding_attack_close_enemy:execution(cfg)
if (not dogs[1]) then return end

-- Find closest sheep/enemy pair first
local min_dist, closest_sheep, closest_enemy = 9e99
local min_dist, closest_sheep, closest_enemy = math.huge
for _,enemy in ipairs(enemies) do
for _,single_sheep in ipairs(sheep) do
local dist = M.distance_between(enemy.x, enemy.y, single_sheep.x, single_sheep.y)
Expand All @@ -102,7 +102,7 @@ function ca_herding_attack_close_enemy:execution(cfg)
end

-- Move dogs in between enemies and sheep
max_rating, best_dog, best_hex = -9e99
max_rating, best_dog, best_hex = - math.huge
for _,dog in ipairs(dogs) do
local reach_map = AH.get_reachable_unocc(dog)
reach_map:iter( function(x, y, v)
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/cas/ca_herding_herd_sheep.lua
Expand Up @@ -42,7 +42,7 @@ function ca_herding_herd_sheep:execution(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 = - math.huge
local c_x, c_y = cfg.herd_x, cfg.herd_y
for _,single_sheep in ipairs(sheep_to_herd) do
-- Farthest sheep goes first
Expand Down
4 changes: 2 additions & 2 deletions data/ai/micro_ais/cas/ca_hunter.lua
Expand Up @@ -12,7 +12,7 @@ local function hunter_attack_weakest_adj_enemy(ai, hunter)

if (hunter.attacks_left == 0) then return 'no_attack' end

local min_hp, target = 9e99
local min_hp, target = math.huge
for xa,ya in H.adjacent_tiles(hunter.x, hunter.y) do
local enemy = wesnoth.get_unit(xa, ya)
if AH.is_attackable_enemy(enemy) then
Expand Down Expand Up @@ -74,7 +74,7 @@ function ca_hunter:execution(cfg)
local reach_map = AH.get_reachable_unocc(hunter)

-- Now find the one of these hexes that is closest to the goal
local max_rating, best_hex = -9e99
local max_rating, best_hex = - math.huge
reach_map:iter( function(x, y, v)
-- Distance from goal is first rating
local rating = -M.distance_between(x, y, hunter_vars.goal_x, hunter_vars.goal_y)
Expand Down

0 comments on commit 37e83f1

Please sign in to comment.