1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. Don’t forget to sign up for the Hive Cup. There’s a 555 EUR prize pool. Sign up now!
    Dismiss Notice
  4. The Hive Workshop Cup contest results have been announced! See the maps that'll be featured in the Hive Workshop Cup tournament!
    Dismiss Notice
  5. The results are out! Check them out.
    Dismiss Notice
  6. The poll for Hive's 12th Concept Art Contest is up! Go cast your vote for your favourite genie!
    Dismiss Notice
  7. The raddest synthwave tracks were chosen - Check out our Music Contest #12 - Results and congratulate the winners!
    Dismiss Notice
  8. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

In need of assistance..

Discussion in 'Triggers & Scripts' started by Maximilianx, Feb 11, 2010.

  1. Maximilianx

    Maximilianx

    Joined:
    Oct 14, 2008
    Messages:
    334
    Resources:
    0
    Resources:
    0
    Alright so here's the deal, I have no idea whats wrong with my code here, gone over it tons of times.. so I'll once again harness the helpful power of the hive. Basically "trueshot" is a passive ability that adds +1 agility for X amount of time on every successful attack. The current code works fine for one unit, but when another hero steps in.. all hell breaks loose.. Really not sure whats wrong with my code.. Can anyone help? Maybe tell me how inefficient it might be too :p

    Code (vJASS):

    library Trueshot initializer init

        private struct agibonus
            unit u
           
            static agibonus array ab
            static integer index = 0
           
            method onDestroy takes nothing returns nothing
                set .u = null
            endmethod
           
            static method timerExpire takes nothing returns nothing
                local agibonus a = agibonus.ab[0]
                local integer i = 1
                call SetHeroAgi(a.u, GetHeroAgi(a.u, false)-1, true)
                call a.destroy()
                call DestroyTimer(GetExpiredTimer())
               
                loop
                    exitwhen i > agibonus.index
                    set agibonus.ab[i-1] = agibonus.ab[i]
                    set i = i+1
                endloop
                set agibonus.index = agibonus.index-1
            endmethod
           
            static method create takes unit u, integer slvl returns agibonus
                local agibonus a = agibonus.allocate()
                local timer tim = CreateTimer()
                local real time = 3.5+(0.5*slvl)
                set a.u = u
                call SetHeroAgi(u, GetHeroAgi(u, false)+1, true)
                set agibonus.ab[agibonus.index] = a
                set agibonus.index = agibonus.index+1
                call TimerStart(tim, time, false, function agibonus.timerExpire)
               
                return a
            endmethod
        endstruct
       
        private struct data
            unit attacker
            unit attacked
            integer slvl
            trigger dmgtrigger
            triggeraction act
            timer tim
           
            static data array dat
            static integer index = 0
           
            method onDestroy takes nothing returns nothing
                call TriggerRemoveAction(.dmgtrigger, .act)
                call DestroyTrigger(.dmgtrigger)
                set .attacker = null
                set .attacked = null
            endmethod
           
            static method timerExpire takes nothing returns nothing
                local timer tim = GetExpiredTimer()
                local data d
                local integer i = 0
                local integer q = 0
                local integer z = 0
                local boolean b = false
               
                loop
                    exitwhen i > data.index
                    exitwhen b == true
                    set d = data.dat[i]
                    if d.tim == tim then
                        set b = true
                        call DestroyTimer(tim)
                        call d.destroy()
               
                        loop
                            set q = data.index
                            set z = i+1
                            set data.dat[z-1] = data.dat[z]
                            set z = z+1
                            exitwhen z > q
                        endloop
                        set data.index = data.index-1
                    endif
                    set i = i+1
                endloop
            endmethod
           
            static method dmgTriggerActions takes nothing returns nothing
                local unit src = GetEventDamageSource()
                local unit u = GetTriggerUnit()
                local integer i = 0
                local integer q = 0
                local integer z = 0
                local boolean b = false
                local data d
                loop
                    exitwhen i > data.index
                    exitwhen b == true
                    set d = data.dat[i]
                    if d.attacker == src and d.attacked == u then
                        set b = true
                        call agibonus.create(d.attacker, d.slvl)
                        call PauseTimer(d.tim)
                        call DestroyTimer(d.tim)
                        call d.destroy()
                        loop
                            set q = data.index
                            set z = i+1
                            set data.dat[z-1] = data.dat[z]
                            set z = z+1
                            exitwhen z > q
                        endloop
                        set data.index = data.index-1
                       
                    endif
                    set i = i + 1
                endloop
                set src = null
                set u = null
            endmethod
           
            static method create takes unit attacker, unit attacked returns data
                local data d = data.allocate()
                set d.attacker = attacker
                set d.attacked = attacked
                set d.slvl = GetUnitAbilityLevel(d.attacker, 'A00K')
                set d.dmgtrigger = CreateTrigger()
                set d.tim = CreateTimer()
                call TriggerRegisterUnitEvent(d.dmgtrigger, d.attacked, EVENT_UNIT_DAMAGED)
                set d.act = TriggerAddAction(d.dmgtrigger, function data.dmgTriggerActions)
                call TimerStart(d.tim, 1.0, false, function data.timerExpire)
                set data.dat[data.index] = d
                set data.index = data.index+1
               
                return d
            endmethod
        endstruct

        private function Eval takes nothing returns boolean
            if GetUnitAbilityLevel(GetAttacker(), 'A00K') > 0 then
                call data.create(GetAttacker(), GetTriggerUnit())
            endif
            return false
        endfunction

        private function init takes nothing returns nothing
            local trigger trg = CreateTrigger()
            call TriggerRegisterAnyUnitEventBJ(trg, EVENT_PLAYER_UNIT_ATTACKED)
            call TriggerAddCondition(trg, Condition(function Eval))
        endfunction
    endlibrary
     
     
  2. Maximilianx

    Maximilianx

    Joined:
    Oct 14, 2008
    Messages:
    334
    Resources:
    0
    Resources:
    0
    bump? >_>
     
  3. ikillforeyou

    ikillforeyou

    Joined:
    Jun 10, 2008
    Messages:
    1,341
    Resources:
    33
    Models:
    22
    Icons:
    9
    Packs:
    1
    Tutorials:
    1
    Resources:
    33
    Why is it a library, cant it just be a scope?

    Edit: I didn't find any fault with your code, i gave some pallies Bash neutral hostile 1 and let them loose on these peasents with your code with scope instead of library and some different abilities and it worked fine, define "Went crazy"
     
  4. Maximilianx

    Maximilianx

    Joined:
    Oct 14, 2008
    Messages:
    334
    Resources:
    0
    Resources:
    0
    What difference would that make? I prefer libraries.
     
  5. SlayerII

    SlayerII

    Joined:
    Aug 21, 2008
    Messages:
    526
    Resources:
    2
    Maps:
    1
    Tutorials:
    1
    Resources:
    2
    librarys put the code inside to the top of the whole code, scopes do not. So librarys are usefull if you want to share code, if you do it only for yourself you could jsut write at the top of your code.
    I did not find any mistake in the code too.
     
  6. Maximilianx

    Maximilianx

    Joined:
    Oct 14, 2008
    Messages:
    334
    Resources:
    0
    Resources:
    0
    Well I know what the difference is between libraries and scopes, I was asking ikill how I would benefit from using a scope instead of a library.
     
  7. SlayerII

    SlayerII

    Joined:
    Aug 21, 2008
    Messages:
    526
    Resources:
    2
    Maps:
    1
    Tutorials:
    1
    Resources:
    2
    So if you dont want the code at the top of your script, you can use scopes. Maybe you need a function which isnt in a library.. or something normally you could use a library i think.

    So if you want to execute it on map init, better use a library. But by calling the scope by a ExecuteFunc, you still may use it.
     
  8. Mooglefrooglian

    Mooglefrooglian

    Joined:
    Nov 28, 2008
    Messages:
    701
    Resources:
    0
    Resources:
    0
    The only point of using a scope is if you want to use code above it for some strange reason. It really doesnt matter.
     
  9. SlayerII

    SlayerII

    Joined:
    Aug 21, 2008
    Messages:
    526
    Resources:
    2
    Maps:
    1
    Tutorials:
    1
    Resources:
    2
    O really? i thought i already said it twice:ugly:
    Well but you said it more understandable...
     
  10. Maximilianx

    Maximilianx

    Joined:
    Oct 14, 2008
    Messages:
    334
    Resources:
    0
    Resources:
    0
    lol I really never use scopes, because libraries can have requirements for other libraries anyway.