Lua AIs: use math.huge to initialize extrema variables

(cherry-picked from commit cb423b2fbd)
This commit is contained in:
mattsc 2018-08-30 07:18:04 -07:00
parent 613ccd859a
commit 37e83f1262
38 changed files with 80 additions and 80 deletions

View file

@ -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
@ -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
@ -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)
@ -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

View file

@ -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)
@ -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
@ -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)
@ -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)
@ -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
@ -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

View file

@ -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
@ -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
@ -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
@ -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)
@ -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

View file

@ -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

View file

@ -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)
@ -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

View file

@ -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 } } }

View file

@ -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
@ -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
@ -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
@ -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

View file

@ -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
@ -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
@ -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

View file

@ -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))

View file

@ -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))

View file

@ -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 =
@ -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])
@ -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

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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)
@ -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)
@ -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)

View file

@ -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

View file

@ -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
@ -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)

View file

@ -55,7 +55,7 @@ local function messenger_find_clearing_attack(messenger, goal_x, goal_y, cfg)
local attacks = AH.get_attacks(units, { simulate_combat = true })
local max_rating = -9e99
local max_rating = - math.huge
for _,attack in ipairs(attacks) do
if (attack.target.x == enemy_in_way.x) and (attack.target.y == enemy_in_way.y) then

View file

@ -35,7 +35,7 @@ function ca_messenger_escort_move:execution(cfg)
local enemies = AH.get_attackable_enemies()
local base_rating_map = LS.create()
local max_rating, best_unit, best_hex = -9e99
local max_rating, best_unit, best_hex = - math.huge
for _,unit in ipairs(escorts) do
-- Only considering hexes unoccupied by other units is good enough for this
local reach_map = AH.get_reachable_unocc(unit)
@ -51,7 +51,7 @@ function ca_messenger_escort_move:execution(cfg)
-- Distance from messenger is most important; only closest messenger counts for this
-- Give somewhat of a bonus for the messenger that has moved the farthest through the waypoints
local max_messenger_rating = -9e99
local max_messenger_rating = - math.huge
for _,m in ipairs(messengers) do
local messenger_rating = 1. / (M.distance_between(x, y, m.x, m.y) + 2.)
local wp_rating = MAIUV.get_mai_unit_variables(m, cfg.ai_id, "wp_rating")

View file

@ -22,7 +22,7 @@ return function(cfg)
-- Set the next waypoint for all messengers
-- Also find those with MP left and return the one to next move, together with the WP to move toward
local max_rating, best_messenger, x, y = -9e99
local max_rating, best_messenger, x, y = - math.huge
for _,messenger in ipairs(messengers) do
-- To avoid code duplication and ensure consistency, we store some pieces of
-- information in the messenger units, even though it could be calculated each time it is needed

View file

@ -90,12 +90,12 @@ function ca_messenger_move:execution(cfg)
local targets = AH.get_attackable_enemies { { "filter_adjacent", { id = messenger.id } } }
local max_rating, best_target, best_weapon = -9e99
local max_rating, best_target, best_weapon = - math.huge
for _,target in ipairs(targets) do
for n_weapon,weapon in ipairs(messenger.attacks) do
local att_stats, def_stats = wesnoth.simulate_combat(messenger, n_weapon, target)
local rating = -9e99
local rating = - math.huge
-- This is an acceptable attack if:
-- 1. There is no counter attack
-- 2. Probability of death is >=67% for enemy, 0% for attacker (default values)

View file

@ -29,7 +29,7 @@ function ca_protect_unit_attack:evaluation(cfg)
-- Set up a counter attack damage table, as many pairs of attacks will be the same
local counter_damage_table = {}
local max_rating = -9e99
local max_rating = - math.huge
for _,attack in pairs(attacks) do
-- Only consider attack if there is no chance to die or to be poisoned or slowed
if (attack.att_stats.hp_chance[0] == 0)
@ -70,7 +70,7 @@ function ca_protect_unit_attack:evaluation(cfg)
end
-- Add this to damage possible on this attack
local min_hp = 1000
local min_hp = math.huge
for hp,chance in pairs(attack.att_stats.hp_chance) do
if (chance > 0) and (hp < min_hp) then
min_hp = hp

View file

@ -69,7 +69,7 @@ function ca_protect_unit_move:execution(cfg, data)
terrain_weight = 0
end
local max_rating, best_hex = -9e99
local max_rating, best_hex = - math.huge
for ind,_ in pairs(reach_map.values) do
local rating =
(attack_map.values[ind] or 0) * my_unit_weight

View file

@ -27,7 +27,7 @@ function ca_simple_attack:evaluation(cfg)
local attacks = AH.get_attacks(units, { include_occupied = true })
if (not attacks[1]) then return 0 end
local max_rating = -9e99
local max_rating = - math.huge
for _, att in ipairs(attacks) do
local valid_target = true
if enemy_filter and (not enemy_map:get(att.target.x, att.target.y)) then

View file

@ -37,7 +37,7 @@ function ca_stationed_guardian:execution(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 min_dist, target = 9e99
local min_dist, target = math.huge
for _,enemy in ipairs(enemies) do
local dist_s = M.distance_between(cfg.station_x, cfg.station_y, enemy.x, enemy.y)
local dist_g = M.distance_between(cfg.guard_x or cfg.station_x, cfg.guard_y or cfg.station_y, enemy.x, enemy.y)
@ -52,7 +52,7 @@ function ca_stationed_guardian:execution(cfg)
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 = - math.huge
for xa,ya in H.adjacent_tiles(target.x, target.y) do
-- Only consider unoccupied hexes
local unit_in_way = wesnoth.get_unit(xa, ya)
@ -77,7 +77,7 @@ function ca_stationed_guardian:execution(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 min_dist, nh = 9e99
local min_dist, nh = math.huge
for _,hex in ipairs(reach) do
-- Only consider unoccupied hexes
local unit_in_way = wesnoth.get_unit(hex[1], hex[2])

View file

@ -34,7 +34,7 @@ function ca_wolves_move:execution(cfg)
local avoid_map = BC.get_attack_map(avoid_units).units
-- Find prey that is closest to the wolves
local min_dist, target = 9e99
local min_dist, target = math.huge
for _,prey_unit in ipairs(prey) do
local dist = 0
for _,wolf in ipairs(wolves) do

View file

@ -76,7 +76,7 @@ function ca_wolves_multipacks_attack:execution(cfg)
end
-- Find which target can be attacked by the most units, from the most hexes; and rate by fewest HP if equal
local max_rating, best_target = -9e99
local max_rating, best_target = - math.huge
for attack_ind,attack in pairs(attack_map_wolves) do
local number_wolves, number_hexes = 0, 0
for _,w in pairs(attack) do number_wolves = number_wolves + 1 end
@ -110,7 +110,7 @@ function ca_wolves_multipacks_attack:execution(cfg)
-- Now we know the best target and need to attack
-- This is done on a wolf-by-wolf basis, the outside while loop taking care of
-- the next wolf in the pack on subsequent iterations
local max_rating, best_attack = -9e99
local max_rating, best_attack = - math.huge
for _,attack in ipairs(attacks) do
if (attack.target.x == best_target.x) and (attack.target.y == best_target.y) then
local rating = attack.att_stats.average_hp / 2. - attack.def_stats.average_hp

View file

@ -58,7 +58,7 @@ function wolves_multipacks_functions.assign_packs(cfg)
-- First, go through packs that have less than pack_size members
for pack_number,pack in pairs(packs) do
if (#pack < pack_size) then
local min_dist, best_wolf, best_ind = 9e99
local min_dist, best_wolf, best_ind = math.huge
for ind,wolf in ipairs(nopack_wolves) do
-- Criterion is distance from the first two wolves of the pack
local dist1 = M.distance_between(wolf.x, wolf.y, pack[1].x, pack[1].y)
@ -97,7 +97,7 @@ function wolves_multipacks_functions.assign_packs(cfg)
-- They form the next pack
local new_pack_wolves = {}
while (#new_pack_wolves < pack_size) do
local min_dist, best_wolf, best_wolf_ind = 9e99
local min_dist, best_wolf, best_wolf_ind = math.huge
for ind,nopack_wolf in ipairs(nopack_wolves) do
local dist = 0
for _,pack_wolf in ipairs(new_pack_wolves) do

View file

@ -81,7 +81,7 @@ function ca_wolves_multipacks_wander:execution(cfg)
-- Keep only those hexes that can be reached by all wolves in the pack
-- and add distance from goal for those
local max_rating, goto_hex = -9e99
local max_rating, goto_hex = - math.huge
reach_map:iter( function(x, y, v)
local rating = reach_map:get(x, y)
if (rating == #pack * 100) then

View file

@ -31,7 +31,7 @@ function ca_wolves_wander:execution(cfg)
local avoid_units = AH.get_attackable_enemies({ type = cfg.avoid_type })
local avoid_map = BC.get_attack_map(avoid_units).units
local max_rating, goal_hex = -9e99
local max_rating, goal_hex = - math.huge
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

View file

@ -27,7 +27,7 @@ function ca_zone_guardian:execution(cfg)
local zone_enemy = wml.get_child(cfg, "filter_location_enemy") or zone
local enemies = AH.get_attackable_enemies { { "filter_location", zone_enemy } }
if enemies[1] then
local min_dist, target = 9e99
local min_dist, target = math.huge
for _,enemy in ipairs(enemies) do
local dist = M.distance_between(guardian.x, guardian.y, enemy.x, enemy.y)
if (dist < min_dist) then
@ -39,7 +39,7 @@ function ca_zone_guardian:execution(cfg)
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 = - math.huge
for xa,ya in H.adjacent_tiles(target.x, target.y) do
-- Only consider unoccupied hexes
local unit_in_way = wesnoth.get_unit(xa, ya)
@ -64,7 +64,7 @@ function ca_zone_guardian:execution(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 min_dist, nh = 9e99
local min_dist, nh = math.huge
for _,hex in ipairs(reach) do
-- Only consider unoccupied hexes
local unit_in_way = wesnoth.get_unit(hex[1], hex[2])

View file

@ -29,7 +29,7 @@ function ca_ogres_flee:execution()
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
local best_hex, best_unit, max_rating = {}, nil, - math.huge
for i,u in ipairs(units) do
local reach = wesnoth.find_reach(u)

View file

@ -53,7 +53,7 @@ function ca_transport:execution()
}
)
local max_rating, best_unit, best_hex, best_adj_tiles = -9e99
local max_rating, best_unit, best_hex, best_adj_tiles = - math.huge
for i,u in ipairs(transports) do
local dst = { u.variables.destination_x, u.variables.destination_y }
@ -104,7 +104,7 @@ function ca_transport:execution()
end
end
if (max_rating > -9e99) then
if (max_rating > - math.huge) then
ai.move_full(best_unit, best_hex[1], best_hex[2])
-- Also unload units
@ -128,12 +128,12 @@ function ca_transport:execution()
}
)
local max_rating, best_unit, best_hex = -9e99, {}, {}
local max_rating, best_unit, best_hex = - math.huge, {}, {}
for i,u in ipairs(transports) do
local dst = { u.variables.destination_x, u.variables.destination_y }
local reach = wesnoth.find_reach(u)
local max_rating_unit, best_hex_unit = -9e99, {}
local max_rating_unit, best_hex_unit = - math.huge, {}
for i,r in ipairs(reach) do
if deep_water_map:get(r[1], r[2]) and (not blocked_hex_map:get(r[1], r[2])) then
local rating = -M.distance_between(r[1], r[2], dst[1], dst[2])
@ -149,7 +149,7 @@ function ca_transport:execution()
-- We give a penalty to hexes occupied by another transport that can still move away.
-- All ratings need to be set to the same value for this to work.
if (max_rating_unit > -9e99) then
if (max_rating_unit > - math.huge) then
max_rating_unit = 0
if transport_map:get(best_hex_unit[1], best_hex_unit[2]) then
max_rating_unit = -1

View file

@ -15,7 +15,7 @@ function ca_aggressive_attack_no_suicide:evaluation(cfg, data)
if (not attacks[1]) then return 0 end
-- Now find the best of the possible attacks
local max_rating, best_attack = -9e99, {}
local max_rating, best_attack = - math.huge, {}
for i, att in ipairs(attacks) do
local attacker = wesnoth.get_unit(att.src.x, att.src.y)
local defender = wesnoth.get_unit(att.target.x, att.target.y)
@ -44,7 +44,7 @@ function ca_aggressive_attack_no_suicide:evaluation(cfg, data)
end
end
if (max_rating > -9e99) then
if (max_rating > - math.huge) then
data.attack = best_attack
return 100000
end

View file

@ -28,10 +28,10 @@ function muff_toras_move:execution()
local rating = -10000 -- This is the base rating if no other units are left
-- Main rating is distance from the closest own unit
local min_dist
local min_dist = math.huge
for _,unit in ipairs(units) do
local dist = M.distance_between(x, y, unit.x, unit.y)
if (not min_dist) or (dist < min_dist) then
if (dist < min_dist) then
min_dist = dist
end
end
@ -75,12 +75,12 @@ function muff_toras_move:execution()
local targets = AH.get_attackable_enemies { { "filter_adjacent", { id = muff_toras.id } } }
local max_rating, best_target, best_weapon = -9e99
local max_rating, best_target, best_weapon = - math.huge
for _,target in ipairs(targets) do
for n_weapon,weapon in ipairs(muff_toras.attacks) do
local att_stats, def_stats = wesnoth.simulate_combat(muff_toras, n_weapon, target)
local rating = -9e99
local rating = - math.huge
-- This is an acceptable attack if:
-- 1. There is no counter attack
-- 2. Probability of death is >=67% for enemy, 0% for attacker (default values)