• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] problem loop

Status
Not open for further replies.
Level 2
Joined
Jun 25, 2006
Messages
18
Hi everyone. I'm trying to surpass the maximum gold of the game of 1,000,000 when recieving income or killing units. Any time that player gold exceeds 1,000,000, add every penny to an integer variable. Gold will be recovered by looping 10k gold out of the bank when a player gold falls below 200k. The loop should end when a player's bank is empty, or when they reach 800k gold.

Adding the gold to a variable works fine in both cases where gold will exceed the maximum, but when subtracting it, it somehow goes negative. My math looks good and I can't seem to figure out why it doesn't end the loop when it's supposed to.

The following two triggers are just to add the gold to the bank. This all works correctly, and doesn't seem to be causing the problem. Calling the bank value always shows the correct value.

JASS:
//When a unit dies, find the difference between bounty (pointvalue is equivalent) and remaining to bank.
    local integer inta = GetPlayerState(GetOwningPlayer(GetKillingUnit()), PLAYER_STATE_RESOURCE_GOLD)
    local integer intb = GetUnitPointValue(GetDyingUnit())
    local integer intc = inta + intb
    if (intc >= 1000000) then
        set udg_Bank[GetConvertedPlayerId(GetOwningPlayer(localunit))] = udg_Bank[GetConvertedPlayerId(GetOwningPlayer(localunit))] + (intc - 1000000)
    endif

JASS:
//When the income rolls in, find the difference between total gold+income and maximum gold, add remaining to bank.
    local integer Total
    local integer Diff
    if (GetPlayerSlotState(Player(0))==PLAYER_SLOT_STATE_PLAYING) then
        set Total = (udg_Income[1] + GetPlayerState(Player(0), PLAYER_STATE_RESOURCE_GOLD))
        if (Total > 1000000) then
            set Diff = (Total - 1000000)
            set udg_Bank[1] = (Diff + udg_Bank[1])
        endif
        call SetPlayerState(Player(0), PLAYER_STATE_RESOURCE_GOLD, GetPlayerState(Player(0), PLAYER_STATE_RESOURCE_GOLD) + udg_Income[1])
        call ConditionalTriggerExecute(gg_trg_ShowStatsRed)
    endif

Now here is where the problem lies. For some reason Bank may fall below 9999, and even go a couple ten thousands negative. It should stop after falling below 10,000, or when a player reached 800,000. Any help will be greatly appreciated.

JASS:
//When a player's gold falls below 200k, add the bank until it falls below 10k, or until the player's gold reaches 800k. 
function AddTotalBack takes nothing returns nothing
    if (udg_Bank[GetConvertedPlayerId(GetTriggerPlayer())] > 9999) then
    loop
        call SetPlayerState(GetTriggerPlayer(), PLAYER_STATE_RESOURCE_GOLD, (GetPlayerState(GetTriggerPlayer(), PLAYER_STATE_RESOURCE_GOLD) + 10000))
        set udg_Bank[GetConvertedPlayerId(GetTriggerPlayer())] = (udg_Bank[GetConvertedPlayerId(GetTriggerPlayer())] - 10000)
        exitwhen (udg_Bank[GetConvertedPlayerId(GetTriggerPlayer())] <= 9999) or (GetPlayerState(GetTriggerPlayer(), PLAYER_STATE_RESOURCE_GOLD)>=800000)
    endloop
    endif
endfunction

function InitTrig_AddTotalBack takes nothing returns nothing
    set gg_trg_AddTotalBack = CreateTrigger(  )
    call TriggerRegisterPlayerStateEvent(gg_trg_AddTotalBack, Player(0), PLAYER_STATE_RESOURCE_GOLD, LESS_THAN_OR_EQUAL, 200000.)
    call TriggerRegisterPlayerStateEvent(gg_trg_AddTotalBack, Player(1), PLAYER_STATE_RESOURCE_GOLD, LESS_THAN_OR_EQUAL, 200000.)
    call TriggerRegisterPlayerStateEvent(gg_trg_AddTotalBack, Player(2), PLAYER_STATE_RESOURCE_GOLD, LESS_THAN_OR_EQUAL, 200000.)
    call TriggerRegisterPlayerStateEvent(gg_trg_AddTotalBack, Player(3), PLAYER_STATE_RESOURCE_GOLD, LESS_THAN_OR_EQUAL, 200000.)
    call TriggerRegisterPlayerStateEvent(gg_trg_AddTotalBack, Player(4), PLAYER_STATE_RESOURCE_GOLD, LESS_THAN_OR_EQUAL, 200000.)
    call TriggerRegisterPlayerStateEvent(gg_trg_AddTotalBack, Player(5), PLAYER_STATE_RESOURCE_GOLD, LESS_THAN_OR_EQUAL, 200000.)
    call TriggerAddAction(gg_trg_AddTotalBack, function AddTotalBack)
endfunction
 
Level 5
Joined
Feb 16, 2006
Messages
151
Uh...
You are using
JASS:
GetConvertedPlayerId(whichPlayer)
which is a BJ.
It does not return the actual PlayerId but rather the "actual player ID + 1", Lol.
JASS:
function GetConvertedPlayerId takes player whichPlayer returns integer
    return GetPlayerId(whichPlayer) + 1
endfunction
Try using GetPlayerId(whichPlayer), since I see how you have been using "Player(0)".
And in your array, there is no udg_Bank[0] because of that BJ.
 
Status
Not open for further replies.
Top