Skip to content

Commit

Permalink
More retreat control optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
SMUnlimited committed Dec 8, 2024
1 parent 013e50b commit fa5e27a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 35 deletions.
15 changes: 13 additions & 2 deletions Jobs/ARMY_TRACK.eai
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ function UpdateTownThreat takes nothing returns nothing
local integer i = lastFreeNum
local real accepted_threat_level = LinearInterpolation(atl_time_start, atl_time_end, atl_time_start_val, atl_time_end_val, TimerGetElapsed(tq_timer)) * LinearInterpolation(atl_enemy_start, atl_enemy_end, atl_enemy_start_mult, atl_enemy_end_mult, c_enemy_total)
local real playerthreat = 0
local integer enemy_army = -1
if i == 0 then
//call Trace("ARMY_TRACK: Town threat update")
set most_threatened_town = -1
Expand Down Expand Up @@ -282,12 +283,22 @@ function UpdateTownThreat takes nothing returns nothing
if town_threat[most_threatened_player_town] > accepted_threat_level and town_threat[most_threatened_player_town] <= playerthreat then //and town_threat[most_threatened_town] > (I2R(c_ally_total + 1) / c_enemy_total) * town_threat[most_threatened_enemy_town] then
set town_threatened = true
set most_threatened_town = most_threatened_player_town
call SetCaptainHome(BOTH_CAPTAINS, GetLocationX(town_loc[most_threatened_player_town]), GetLocationY(town_loc[most_threatened_player_town]))
set enemy_army = town_threat_army[most_threatened_town]
if enemy_army >= 0 then
call SetCaptainHome(BOTH_CAPTAINS,GetLocationX(army_loc[enemy_army]), GetLocationY(army_loc[enemy_army]))
else
call SetCaptainHome(BOTH_CAPTAINS, GetLocationX(town_loc[most_threatened_player_town]), GetLocationY(town_loc[most_threatened_player_town]))
endif
set captain_home = town_loc[most_threatened_player_town]
set captain_tp = town_can_tp[most_threatened_player_town]
elseif town_threat[most_threatened_town] > accepted_threat_level * 1.2 and town_threat[most_threatened_town] <= playerthreat then
set town_threatened = true
call SetCaptainHome(BOTH_CAPTAINS, GetLocationX(town_loc[most_threatened_town]), GetLocationY(town_loc[most_threatened_town]))
set enemy_army = town_threat_army[most_threatened_town]
if enemy_army >= 0 then
call SetCaptainHome(BOTH_CAPTAINS,GetLocationX(army_loc[enemy_army]), GetLocationY(army_loc[enemy_army]))
else
call SetCaptainHome(BOTH_CAPTAINS, GetLocationX(town_loc[most_threatened_town]), GetLocationY(town_loc[most_threatened_town]))
endif
set captain_home = town_loc[most_threatened_town]
set captain_tp = town_can_tp[most_threatened_town]
elseif town_threat[most_threatened_player_town] > 0 and town_threat[most_threatened_player_town] <= playerthreat then
Expand Down
24 changes: 16 additions & 8 deletions Jobs/RETREAT_CONTROL.eai
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,11 @@ if not isfleeing and canflee and (CaptainRetreating() or CaptainIsHome()) then
set isfleeing = true
endif

if ((not attack_running and not isfleeing) or isfleeing or (town_threatened and town_threat_break)) then
if ((not attack_running and not isfleeing) or isfleeing or (town_threatened and town_threat_break) or player_defeated) then
set break_attack = attack_running
if CaptainIsHome() or (not attack_running and not isfleeing) then
call Trace("===Retreat control ended===")
call TraceAll("RETREAT: Pending End fleeing:" + B2S(isfleeing))
if CaptainIsHome() or (not attack_running and not isfleeing) or player_defeated or (main_army >= 0 and DistanceBetweenPoints(army_loc[main_army], captain_home) <= 750) then
call TraceAll("===Retreat control ended===")
set retreat_controlled = false
set canflee = false
set isfleeing = false // home so no need to be retreating. Also prevents bug in the captainretreating condition
Expand All @@ -134,8 +135,16 @@ if ((not attack_running and not isfleeing) or isfleeing or (town_threatened and
endif

if isfleeing or (town_threatened and town_threat_break) then
call ClearCaptainTargets()
call CaptainGoHome()
//call ClearCaptainTargets()
if not CaptainIsHome() and not CaptainRetreating() then
call TraceAll("Retreat Home Fix")
call CaptainGoHome()
endif
endif

if isfleeing then
call TQAddJob(2 * sleep_multiplier, RETREAT_CONTROL, 0)
return
endif

if attack_running then
Expand Down Expand Up @@ -211,13 +220,12 @@ if attack_running then
set ally_strength_sum = ally_sum
call Trace("RETREAT_CONTROL: Our strength: " + Real2Str(ally_strength_sum) + " Enemy strength: " + Real2Str(enemy_strength_sum))

if not desperation_assault and (enemy_strength_sum > ApplyFleeStrengthModifier(ally_strength_sum, true) or (town_threatened and town_threat_break and attack_running) or ally_strength_sum == 0) then
if CaptainInCombat(true) and not desperation_assault and (enemy_strength_sum > ApplyFleeStrengthModifier(ally_strength_sum, true) or (town_threatened and town_threat_break and attack_running) or ally_strength_sum == 0) then
call TraceAll("RETREAT_CONTROL: Flee!!!!")
set break_attack = attack_running
call ClearCaptainTargets()
call CaptainGoHome()
set isfleeing = true
elseif not desperation_assault and (enemy_strength_sum > ApplyFleeStrengthModifier(ally_strength_sum, false) or (battle_radius == creep_battle_radius and enemy_strength_sum > ally_strength_sum )) then
elseif CaptainInCombat(true) and not desperation_assault and (enemy_strength_sum > ApplyFleeStrengthModifier(ally_strength_sum, false) or (battle_radius == creep_battle_radius and enemy_strength_sum > ally_strength_sum )) then
call TraceAll("RETREAT_CONTROL: Allowed to Flee")
call SetGroupsFlee(true)
set canflee = true
Expand Down
76 changes: 51 additions & 25 deletions common.eai
Original file line number Diff line number Diff line change
Expand Up @@ -13016,15 +13016,18 @@ function SleepUntilTownDefended takes integer ai_strength returns nothing
local integer enemy_army = -1
set own_strength = ai_strength
call Trace("==Sleep Defend Town==" )
call CaptainGoHome()
call FormGroupAM(0)
if town_threatened and most_threatened_town >= 0 then
if not CaptainIsHome() and not CaptainRetreating() then
call CaptainGoHome()
endif
// Attack logic still overridden by allies, so commented out - just return to home location
//call FormGroupAM(0)
//if town_threatened and most_threatened_town >= 0 then
// We want to intercept the army
set enemy_army = town_threat_army[most_threatened_town]
call CaptainAttack(GetLocationX(army_loc[enemy_army]), GetLocationY(army_loc[enemy_army]))// Not attack move to get here fast
else
call CaptainAttack(GetLocationX(captain_home), GetLocationY(captain_home)) // Unknown where the threat is return home
endif
// set enemy_army = town_threat_army[most_threatened_town]
// call CaptainAttack(GetLocationX(army_loc[enemy_army]), GetLocationY(army_loc[enemy_army]))// Not attack move to get here fast
//else
// call CaptainAttack(GetLocationX(captain_home), GetLocationY(captain_home)) // Unknown where the threat is return home
//endif
loop
//call DisplayToAll("Town Threatened:" + B2S(town_threatened) + " HardTown Threatened:" + B2S(TownThreatened()) + " CaptainInCombat:" + B2S(CaptainInCombat(false)) + " i:" )
call CreateDebugTagLoc("Sleep until town defended", 10, GetLocationX(captain_home), GetLocationY(captain_home), 1.00, 0.80)
Expand All @@ -13033,25 +13036,32 @@ function SleepUntilTownDefended takes integer ai_strength returns nothing
set defense_length_counter = defense_length_counter + 1
if defense_length_counter > attack_reform_length then
set defense_length_counter = 0
call FormGroupAM(0)
//call FormGroupAM(0)
endif
if CaptainAtGoal() or CaptainInCombat(true) or CaptainInCombat(false) then
if not CaptainIsHome() and not CaptainRetreating() and not CaptainInCombat(false) then
call CaptainGoHome()
endif
//if CaptainAtGoal() or CaptainInCombat(true) or CaptainInCombat(false) then
// Attack move to actually defend
if town_threatened and most_threatened_town >= 0 then
set enemy_army = town_threat_army[most_threatened_town]
call AttackMoveXY(R2I(GetLocationX(army_loc[enemy_army])), R2I(GetLocationY(army_loc[enemy_army]))) // Only attack move when at target
else
call AttackMoveXY(R2I(GetLocationX(captain_home)), R2I(GetLocationY(captain_home)))
endif
else
// if town_threatened and most_threatened_town >= 0 then
// set enemy_army = town_threat_army[most_threatened_town]
// call AttackMoveXY(R2I(GetLocationX(army_loc[enemy_army])), R2I(GetLocationY(army_loc[enemy_army]))) // Only attack move when at target
//call TeleportCaptain(GetLocationX(army_loc[enemy_army]), GetLocationY(army_loc[enemy_army]))
// else
// call AttackMoveXY(R2I(GetLocationX(captain_home)), R2I(GetLocationY(captain_home)))
//call TeleportCaptain(GetLocationX(captain_home), GetLocationY(captain_home))
// endif
//else
// This ensures units return to defend location and don't wonder
if town_threatened and most_threatened_town >= 0 then
set enemy_army = town_threat_army[most_threatened_town]
call CaptainAttack(GetLocationX(army_loc[enemy_army]), GetLocationY(army_loc[enemy_army]))// Not attack move to get here fast
else
call CaptainAttack(GetLocationX(captain_home), GetLocationY(captain_home)) // Unknown where the threat is return home
endif
endif
// if town_threatened and most_threatened_town >= 0 then
// set enemy_army = town_threat_army[most_threatened_town]
//call CaptainAttack(GetLocationX(army_loc[enemy_army]), GetLocationY(army_loc[enemy_army]))// Not attack move to get here fast
// call TeleportCaptain(GetLocationX(army_loc[enemy_army]), GetLocationY(army_loc[enemy_army]))
// else
//call CaptainAttack(GetLocationX(captain_home), GetLocationY(captain_home)) // Unknown where the threat is return home
// call TeleportCaptain(GetLocationX(captain_home), GetLocationY(captain_home))
// endif
//endif
call StaggerSleep(2 * sleep_multiplier, (2 * sleep_multiplier) + 2)
endloop
call CaptainGoHome()
Expand Down Expand Up @@ -13733,6 +13743,7 @@ function universal_attack_sequence takes nothing returns nothing
local integer ai_strength = 0
//local integer ai_antiair_strength = 0
local integer exp_strength = 0
local integer defense_length_counter = attack_reform_length

if hero_unit[1] == null and ver_heroes and not desperation_assault then
call Sleep(sleep_multiplier)
Expand Down Expand Up @@ -13766,9 +13777,25 @@ function universal_attack_sequence takes nothing returns nothing
call Sleep(1)
endloop



loop
exitwhen not retreat_controlled
call CreateDebugTagLoc("Sleep until retreated", 10, GetLocationX(home_location), GetLocationY(home_location), 1.00, 0.80)
if retreat_controlled and isfleeing then
set defense_length_counter = defense_length_counter + 1
if defense_length_counter > attack_reform_length then
set defense_length_counter = 0
//call FormGroupAM(0)
//call TraceAll("Retreat FIX")
//call InitAssault()
endif
//if main_army >= 0
//if not CaptainAtGoal() then
//call CaptainAttack(GetLocationX(captain_home), GetLocationY(captain_home)) // Prevent fight and flight behaviour
//call TeleportCaptain(GetLocationX(captain_home), GetLocationY(captain_home)) // Prevent fight and flight behaviour
//endif
endif
call Sleep(1) // Retreat must have fully run its course, units are sent home via job
endloop

Expand All @@ -13782,7 +13809,6 @@ function universal_attack_sequence takes nothing returns nothing

if go_home then
call Trace("UNIVERSAL ATTACK: Going home" )
call ClearCaptainTargets()
call CaptainGoHome()
endif
set break_attack = false
Expand Down

0 comments on commit fa5e27a

Please sign in to comment.