Skip to content

Commit

Permalink
Fix buy units not defending themselves
Browse files Browse the repository at this point in the history
  • Loading branch information
SMUnlimited committed Dec 8, 2024
1 parent 68d0179 commit c1ef5b4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 61 deletions.
72 changes: 28 additions & 44 deletions Jobs/BUY_ITEM.eai
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
#IFDEF GLOBAL
unit shop_sent = null
boolean shop_ordered = false
real shop_buy_time_small = -1
real shop_buy_time_large = -1
integer shop_wanted = 0
unit shop_unit = null
real shop_distance_limit = 100000
real raceshop_distance_limit = 100000
real merchant_distance_limit = 5000
boolean retreat_home = false
boolean ismoving = false
real buyX
real buyY

group going_home_group = CreateGroup()
group buying_group = CreateGroup()

unit shop_sent = null
boolean shop_ordered = false
real shop_buy_time_small = -1
real shop_buy_time_large = -1
integer shop_wanted = 0
unit shop_unit = null
real shop_distance_limit = 100000
real raceshop_distance_limit = 100000
real merchant_distance_limit = 5000
boolean retreat_home = false
real buyX
real buyY
#ELSE
function IsHealingItem takes integer id returns boolean
if false then //basically check that item to build is healing item and drop item to make room for this
Expand Down Expand Up @@ -65,7 +60,6 @@ local integer detectedenemies = GetLocationNonCreepStrength(GetUnitX(shop_sent),

// moved here to prevent a job crash
if shop_sent == null or not UnitAlive(shop_sent) then
set ismoving = false
if retreat_home then
call Trace("Running Home end but item job")
set shop_ordered = false
Expand Down Expand Up @@ -112,18 +106,12 @@ local integer detectedenemies = GetLocationNonCreepStrength(GetUnitX(shop_sent),
if DistanceBetweenPoints(l, home_location) >= 1100 then
call CreateDebugTag("BUY_ITEM: Retreat to Home", 10, shop_sent, 3.00, 1.50)
call RemoveGuardPosition(shop_sent)
if not IsUnitInGroup(shop_sent, going_home_group) then
call IssuePointOrder(shop_sent, "move", GetLocationX(home_location), GetLocationY(home_location))
call GroupAddUnit(going_home_group, shop_sent)
elseif GetUnitCurrentOrder(shop_sent) == OrderId("stop") or GetUnitCurrentOrder(shop_sent) != OrderId("move") then
if GetUnitCurrentOrder(shop_sent) == OrderId("stop") or GetUnitCurrentOrder(shop_sent) != OrderId("move") then
call IssuePointOrder(shop_sent, "move", GetLocationX(home_location), GetLocationY(home_location))
endif
call TQAddJob(5, BUY_ITEM, id)
else
call GroupRemoveUnit(unit_buying_item, shop_sent)
if IsUnitInGroup(shop_sent, going_home_group) then
call GroupRemoveUnit(going_home_group, shop_sent)
endif
call RecycleGuardPosition(shop_sent)
call CreateDebugTag("BUY_ITEM: Retreated", 10, shop_sent, 3.00, 1.50)
set shop_sent = null
Expand All @@ -136,16 +124,6 @@ local integer detectedenemies = GetLocationNonCreepStrength(GetUnitX(shop_sent),
set l = null
return
endif

// Enemies are present so get home before they harm us
if detectedenemies >= GetUnitStrength(shop_sent) and GetUnitTypeId(shop_unit) != old_id[racial_shop] then
call IssuePointOrder(shop_sent, "move", GetLocationX(home_location), GetLocationY(home_location))
//call TQAddUnitJob(GetTimeToReachLoc(shop_sent, home_location), RESET_GUARD_POSITION, 1, shop_sent)
set retreat_home = true
call CreateDebugTag("BUY_ITEM: Enemies present so dont get item this time", 10, shop_sent, 3.00, 1.50)
call TQAddJob(0, BUY_ITEM, id)
return
endif

// call DisplayToAll("Item Buy Start")
if (shop_buy_time_large >= 0 and shop_buy_time_large + buy_timeout_large < current_time) or (shop_buy_time_small >= 0 and shop_buy_time_small + buy_timeout_small < current_time) then
Expand Down Expand Up @@ -185,10 +163,9 @@ local integer detectedenemies = GetLocationNonCreepStrength(GetUnitX(shop_sent),
call CreateDebugTag("buy item", 10, shop_sent, 3.00, 1.50)
if GetSlotsFreeOnUnit(shop_sent) <= 0 and healing then
call DropNonHealingItem(shop_sent)
else
elseif GetUnitCurrentOrder(shop_sent) == OrderId("move") then
call IssueImmediateOrder(shop_sent, "holdposition")
endif
set ismoving = false
if GetUnitTypeId(shop_unit) == old_id[racial_shop] then
call IssueTargetOrderById(shop_unit, 'Aall', shop_sent) // Change shop target to this hero
endif
Expand All @@ -198,15 +175,22 @@ local integer detectedenemies = GetLocationNonCreepStrength(GetUnitX(shop_sent),
endif
set time_next_try = 0.5
else
call CreateDebugTag("move to item shop", 10, shop_sent, 3.00, 1.50)
if ismoving == false then
set ismoving = true
call IssuePointOrder(shop_sent, "move", GetUnitX(shop_unit), GetUnitY(shop_unit))
elseif GetUnitCurrentOrder(shop_sent) == OrderId("stop") or GetUnitCurrentOrder(shop_sent) != OrderId("move") then
call IssuePointOrder(shop_sent, "move", GetUnitX(shop_unit), GetUnitY(shop_unit))
endif
call CreateDebugTag("move to item shop", 10, shop_sent, 3.00, 1.50)
if GetUnitCurrentOrder(shop_sent) != OrderId("move") or GetUnitCurrentOrder(shop_sent) == OrderId("stop") then
call IssuePointOrder(shop_sent, "move", GetUnitX(shop_unit), GetUnitY(shop_unit))
endif
set time_next_try = 4
endif

// Enemies are present so get home before they harm us
if detectedenemies >= GetUnitStrength(shop_sent) and GetUnitTypeId(shop_unit) != old_id[racial_shop] then
call IssuePointOrder(shop_sent, "move", GetLocationX(home_location), GetLocationY(home_location))
//call TQAddUnitJob(GetTimeToReachLoc(shop_sent, home_location), RESET_GUARD_POSITION, 1, shop_sent)
set retreat_home = true
call CreateDebugTag("BUY_ITEM: Enemies present so dont get item this time", 10, shop_sent, 3.00, 1.50)
call TQAddJob(0, BUY_ITEM, id)
return
endif

call TQAddJob(time_next_try, BUY_ITEM, id)
endfunction
Expand Down
35 changes: 19 additions & 16 deletions Jobs/BUY_NEUTRAL.eai
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#IFDEF GLOBAL
boolean retreat_home_n = false
#ELSE
function BuyNeutral takes integer id returns nothing
local real timetotravel = 0
Expand All @@ -11,13 +12,12 @@ function BuyNeutral takes integer id returns nothing
local boolean place_guarded = neutral_guarded[nn]
local integer detectedenemies = GetLocationNonCreepStrength(GetUnitX(neutral_sent[nn]), GetUnitY(neutral_sent[nn]), 1000)
local location l = null
//local boolean retreat_home = false

call DisplayToAllJobDebug("BUY_NEUTRAL JOB START")
// moved this check here to prevent a job crash if placed lower down
if neutral_sent[nn] == null or not UnitAlive(neutral_sent[nn]) then
if retreat_home then
set retreat_home = false
if retreat_home_n then
set retreat_home_n = false
set buy_place = null
return
endif
Expand All @@ -29,7 +29,7 @@ function BuyNeutral takes integer id returns nothing
endif
call GroupAddUnit(unit_buying_merc, neutral_sent[nn])
endif
if retreat_home then
if retreat_home_n then
set l = GetUnitLoc(neutral_sent[nn])
if DistanceBetweenPoints(l, home_location) >= 1100 then
call CreateDebugTag("BUY_ITEM: Retreat to Home", 10, neutral_sent[nn], 3.00, 1.50)
Expand All @@ -39,7 +39,7 @@ function BuyNeutral takes integer id returns nothing
else
call GroupRemoveUnit(unit_buying_merc, neutral_sent[nn])
call RecycleGuardPosition(neutral_sent[nn])
set retreat_home = false
set retreat_home_n = false
set neutral_sent[nn] = null
set neutral_ordered[nn] = false
set buy_time_large[nn] = -1
Expand All @@ -50,16 +50,7 @@ function BuyNeutral takes integer id returns nothing
set l = null
return
endif
// Enemies are present so get home before they harm us
if DistanceBetweenUnits(neutral_sent[nn], buy_place) <= 800 and detectedenemies >= GetUnitStrength(neutral_sent[nn]) then
call RemoveGuardPosition(neutral_sent[nn])
call IssuePointOrder(neutral_sent[nn], "move", GetLocationX(home_location), GetLocationY(home_location))
//call TQAddUnitJob(GetTimeToReachLoc(neutral_sent[nn], home_location), RESET_GUARD_POSITION, 1, neutral_sent[nn])
set retreat_home = true
call TQAddJob(1, BUY_NEUTRAL, id)
set buy_place = null
return
endif

// call DisplayToAll("Buy Neutral Started")
if (buy_time_large[nn] >= 0 and buy_time_large[nn] + buy_timeout_large < current_time) or (buy_time_small[nn] >= 0 and buy_time_small[nn] + buy_timeout_small < current_time) then
set available_time[id] = ai_time + regenerate_time[id] / 2
Expand Down Expand Up @@ -94,7 +85,9 @@ function BuyNeutral takes integer id returns nothing
set dist = DistanceBetweenUnits(neutral_sent[nn], buy_place)
if dist < buy_distance then
call CreateDebugTag("buy unit", 10, neutral_sent[nn], 3.00, 1.50)
call IssueImmediateOrder(neutral_sent[nn], "holdposition")
if GetUnitCurrentOrder(neutral_sent[nn]) == OrderId("move") then
call IssueImmediateOrder(neutral_sent[nn], "holdposition")
endif
call IssueNeutralImmediateOrderById(ai_player, buy_place, old_id[id])
if buy_time_small[nn] < 0 then
set buy_time_small[nn] = current_time
Expand All @@ -106,6 +99,16 @@ function BuyNeutral takes integer id returns nothing
set time_next_try = 4
endif
endif
// Enemies are present so get home before they harm us
if detectedenemies >= GetUnitStrength(neutral_sent[nn]) then
call RemoveGuardPosition(neutral_sent[nn])
call IssuePointOrder(neutral_sent[nn], "move", GetLocationX(home_location), GetLocationY(home_location))
//call TQAddUnitJob(GetTimeToReachLoc(neutral_sent[nn], home_location), RESET_GUARD_POSITION, 1, neutral_sent[nn])
set retreat_home_n = true
call TQAddJob(1, BUY_NEUTRAL, id)
set buy_place = null
return
endif
call TQAddJob(time_next_try, BUY_NEUTRAL, id)
set buy_place = null
endfunction
Expand Down
4 changes: 3 additions & 1 deletion Jobs/BUY_NEUTRAL_HERO.eai
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ function BuyNeutralHero takes integer id returns nothing
set time_next_try = 4
else
call CreateDebugTag("buy tavern hero", 10, buying_unit, 3.00, 1.50)
call IssueImmediateOrder(buying_unit, "holdposition")
if GetUnitCurrentOrder(buying_unit) == OrderId("move") then
call IssueImmediateOrder(buying_unit, "holdposition")
endif
call IssueNeutralImmediateOrderById(ai_player, tavern, old_id[id])
set time_next_try = 0.5
endif
Expand Down

0 comments on commit c1ef5b4

Please sign in to comment.