Skip to content

Commit

Permalink
Fix critical issue with retreats not going whole way home
Browse files Browse the repository at this point in the history
  • Loading branch information
SMUnlimited committed Nov 18, 2024
1 parent 32e9eba commit 5b5cc2a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Fixed an issue where ghouls would start attacking then all run back to harvest wood leaving forces on there own, now extra ghouls will reinforce ongoing attacks too just like other troops.
- Fix an issue where ranged micro could sometimes move ally computer and player units.
- Logic to build multiple unit buildings takes better account of cost to construct these to avoid building multiple when there is no resources available to do so.
- Fix critical issue where retreating forces never fully retreated home, they could just immediately go back into battle.
- Fix PERIAPT_OF_VITALITY gold cost to correct reforged value. (Slayer95)
- Fix ally attack target not being reset, particularly expansion seemed to be slower without fix. (jzy-chitong56)
- Fix Goblin Tinker Skill not upgrading past rockets on one of the trees. (jzy-chitong56)
Expand Down
51 changes: 28 additions & 23 deletions Jobs/RETREAT_CONTROL.eai
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function RetreatControlJob takes nothing returns nothing
// local variables used to calculate roughly where the current enemy army is located at
local integer ally_count = 0
local integer enemy_count = 0
local boolean usearmy = true
call DisplayToAllJobDebug("RETREAT_CONTROL JOB Start")

if major_hero == null or not UnitAlive(major_hero) then
Expand Down Expand Up @@ -108,30 +109,42 @@ function RetreatControlJob takes nothing returns nothing
// endif
// endif

if not retreat_controlled then
return
endif


if not desperation_assault and ((CaptainRetreating() and isfleeing) or town_threatened or CaptainIsHome()) then //((major_hero == null or not UnitAlive(major_hero)) and ver_heroes) or then //attack_running and
call Trace("===Retreat control ended===")
if not desperation_assault and ((not attack_running and not isfleeing) or (isfleeing and (CaptainRetreating() or CaptainIsHome())) or town_threatened) then
set break_attack = attack_running
set retreat_controlled = false
if CaptainIsHome() then
if CaptainIsHome() or (not attack_running and not isfleeing) then
call Trace("===Retreat control ended===")
set retreat_controlled = false
set isfleeing = false // home so no need to be retreating. Also prevents bug in the captainretreating condition
call SetGroupsFlee(false)
return
else
call SetGroupsFlee(true)
endif
return
endif

if not attack_running and isfleeing then
call ClearCaptainTargets()
call CaptainGoHome()
endif

if attack_running then

set g = CreateGroup()
if main_army == -1 or army_loc[main_army] == null or (GetLocationX(army_loc[main_army]) == 0 and GetLocationY(army_loc[main_army]) == 0 )then
if major_hero == null or not UnitAlive(major_hero) then
call Trace("No major hero in retreat control")
else
call GroupEnumUnitsInRange(g,GetUnitX(major_hero),GetUnitY(major_hero),battle_radius,null)
endif
set usearmy = false
else
call GroupEnumUnitsInRange(g, GetLocationX(army_loc[main_army]), GetLocationY(army_loc[main_army]), battle_radius, null)
set usearmy = true
set ally_sum = ally_sum + army_strength[main_army]
endif
set g = SelectUnittype(g, UNIT_TYPE_STRUCTURE, false)
set g = SelectByHidden(g, false)
Expand All @@ -150,7 +163,7 @@ endif
// set unit_mana = GetUnitState(u, UNIT_STATE_MANA)
if GetOwningPlayer(u) != Player(PLAYER_NEUTRAL_AGGRESSIVE) then
//call Trace("RETREAT_CONTROL: At least 1 unit detected")
if IsPlayerAlly(GetOwningPlayer(u), ai_player) then
if IsPlayerAlly(GetOwningPlayer(u), ai_player) and (not usearmy or (usearmy and ai_player != GetOwningPlayer(u))) then
//if GetUnitUserData(u) != UNIT_GOING_HOME then
if not IsUnitInGroup(u, unit_healing) then //and unit_life > RMax(flee_health_percentage * GetUnitState(u, UNIT_STATE_MAX_LIFE), flee_minimum_health) then
set ally_sum = ally_sum + unit_strength
Expand All @@ -159,7 +172,7 @@ endif
set ya = ya + GetUnitY(u)
//set ally_loc = GetSumLoc_dd(ally_loc, GetUnitLoc(u))
set ally_count = ally_count + 1
elseif not IsUnitInvisibleAM(u, ai_player) then
elseif IsPlayerEnemy(GetOwningPlayer(u), ai_player) and not IsUnitInvisibleAM(u, ai_player) then
set enemy_sum = enemy_sum + unit_strength
set xe = xe + GetUnitX(u)
set ye = ye + GetUnitY(u)
Expand Down Expand Up @@ -187,34 +200,26 @@ endif
//set ally_loc = GetDivisionLoc_d(ally_loc, I2R(Max(ally_count, 1)))
// call SetCaptainHome(0, GetLocationX(ally_loc), GetLocationY(ally_loc))

set enemy_strength_sum = enemy_sum
set enemy_strength_sum = enemy_sum + creep_sum
set ally_strength_sum = ally_sum
//call Trace("RETREAT_CONTROL: Our strength: " + Int2Str(R2I(ally_strength_sum)))
//call Trace("RETREAT_CONTROL: Enemy strength: " + Int2Str(R2I(enemy_strength_sum)))
call Trace("RETREAT_CONTROL: Our strength: " + Real2Str(ally_strength_sum) + " Enemy strength: " + Real2Str(enemy_strength_sum))

if not desperation_assault and enemy_sum > LinearInterpolation(ver_low_aggression,ver_high_aggression,ver_flee_multiple2,ver_flee_multiple3,attacking_aggression + added_aggression + added_target_aggression + added_racial_aggression)*ally_sum or (town_threatened and attack_running) then
if not desperation_assault and (enemy_strength_sum > LinearInterpolation(ver_low_aggression,ver_high_aggression,ver_flee_multiple2,ver_flee_multiple3,attacking_aggression + added_aggression + added_target_aggression + added_racial_aggression)*ally_strength_sum or (town_threatened and attack_running) or ally_strength_sum == 0) then
call Trace("RETREAT_CONTROL: Flee!!!!")
set break_attack = attack_running
set retreat_controlled = false
call ClearCaptainTargets()
call CaptainGoHome()
set isfleeing = true
return
elseif not desperation_assault and enemy_sum > LinearInterpolation(ver_low_aggression,ver_high_aggression,ver_flee_multiple1,ver_flee_multiple2,attacking_aggression + added_aggression + added_target_aggression + added_racial_aggression)*ally_sum or (battle_radius == creep_battle_radius and creep_sum - 2 > ally_sum ) then
elseif not desperation_assault and (enemy_strength_sum > LinearInterpolation(ver_low_aggression,ver_high_aggression,ver_flee_multiple1,ver_flee_multiple2,attacking_aggression + added_aggression + added_target_aggression + added_racial_aggression)*ally_strength_sum or (battle_radius == creep_battle_radius and enemy_strength_sum > ally_strength_sum )) then
call Trace("RETREAT_CONTROL: Allowed to Flee")
call SetGroupsFlee(true)
set isfleeing = true
//call ClearCaptainTargets()
//call CaptainGoHome()
else
//if town_threatened and town_threat[most_threatened_town] >= teleport_high_threat then

// else
// call Trace("REATREAT_CONTROL: Stay")
call SetGroupsFlee(false)
call SetGroupsFlee(false) // Prevent natural fleeing
set isfleeing = false
// endif
endif

endif

call TQAddJob(2 * sleep_multiplier, RETREAT_CONTROL, 0)
endfunction
Expand Down
6 changes: 6 additions & 0 deletions common.eai
Original file line number Diff line number Diff line change
Expand Up @@ -13462,6 +13462,12 @@ function universal_attack_sequence takes nothing returns nothing
//elseif not towerrush and not ancient_expanding and not militia_expanding and not item_expanding then
//call SetCaptainHome(DEFENSE_CAPTAIN, GetLocationX(home_location), GetLocationY(home_location))
endif

loop
exitwhen not retreat_controlled
call CreateDebugTagLoc("Sleep until retreated", 10, GetLocationX(home_location), GetLocationY(home_location), 1.00, 0.80)
call Sleep(1) // Retreat must have fully run its course
endloop

if go_home then
call Trace("UNIVERSAL ATTACK: Going home" )
Expand Down

0 comments on commit 5b5cc2a

Please sign in to comment.