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. The poll for Hive's 12th Concept Art Contest is up! Go cast your vote for your favourite genie!
    Dismiss Notice
  4. Travel to distant realms and encounter scenes unknown to the common folk. The Greatest of Adventures is upon us with the 8th Cinematic Contest. Join in on a fun ride.
    Dismiss Notice
  5. The 18th Icon Contest is ON! Choose any ingame unit and give him/her Hero abilities. Good luck to all.
    Dismiss Notice
  6. Contestants are to create a scene set in the Stone Age. Come and see what you can come up with. We wish you the best of luck!
    Dismiss Notice
  7. Colour outside the lines! Techtree Contest #13 is a go. The contest is optionally paired.
    Dismiss Notice
  8. Greetings cerebrates, our Swarm needs new spawners that will have numerous children. Join the HIVE's 31st Modeling Contest - Spawners and Spawned! The contest is optionally paired.
    Dismiss Notice
  9. 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.

Small Code Snippets

Discussion in 'Triggers & Scripts' started by PurplePoot, Sep 29, 2007.

  1. PurplePoot

    PurplePoot

    Joined:
    Dec 14, 2005
    Messages:
    11,162
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    To keep out a potential deluge of threads with very small functions, this thread is for you to post short functions you wish to share.

    Normal or long-length snippets still go the same place as always.

    So, if you have something worth sharing, post it here!



    Functions







    Preload Sound Path
    Code (vJASS):

    //By Silvenon
    function PreloadSoundPath takes string path returns nothing
        local sound snd = CreateSound(path, false, false, false, 10, 10, "")
        call SetSoundVolume(snd, 0)
        call StartSound(snd)
        call KillSoundWhenDone(snd)
        set snd = null
    endfunction

    Quick Timer Attach
    Code (vJASS):

    //By DiscipleOfLife and Troll-Brain
    library QuickTimer
        //=======================================================================
        // Attaches an integer to a timer and makes it expire after 0 seconds.
        // The integer passed must be a positive value.
        //
        function TimerExpireQuick takes timer t, integer data, code func returns nothing
            call TimerStart(t, -data, false, func)
        endfunction
       
        //=======================================================================
        // Returns the integer attached to a quick timer.
        //
        function QuickTimerGetData takes timer t returns integer
            return -R2I(TimerGetTimeout(t))
        endfunction
    endlibrary


    Sort Algorithms
     
    Last edited by a moderator: Jul 4, 2016
  2. Silvenon

    Silvenon

    Joined:
    Nov 22, 2006
    Messages:
    1,233
    Resources:
    1
    Tutorials:
    1
    Resources:
    1
    Code (vJASS):
    function GetClosestUnit takes real x, real y, boolexpr e returns unit
        local real md = 100000
        local real d
        local group g = CreateGroup()
        local unit u
        local real dx
        local real dy
        call GroupEnumUnitsInRect(g, bj_mapInitialPlayableArea, e)
        loop
            set u = FirstOfGroup(g)
            exitwhen u == null
            call GroupRemoveUnit(g, u)
            set dx = GetUnitX(u) - x
            set dy = GetUnitY(u) - y
            if (dx * dx + dy * dy) / 100000 < md then
                set udg_ClosestUnit = u
                set md = (dx * dx + dy * dy) / 100000
            endif
        endloop
        call DestroyGroup(g)
        call DestroyBoolExpr(e)
        set g = null
        return udg_ClosestUnit
    endfunction



    Instructions:

    Before using anything, you must create a global variable named ClosestUnit of type unit.

    Parameters:
    • real x - x coordinate of the point from where the function searches for the closest unit
    • real y - y coordinate of the point from where the function searches for the closest unit
    • boolexpr e - used for filtering (enemy, illusion, hero...), you can put null if you don't want any filter

    You can use the function in two ways:

    Code (vJASS):
    function FirstWay takes nothing returns nothing
        local unit u = SomeUnit()
        local unit c
        set c = GetClosestUnit(GetUnitX(u), GetUnitY(u))
        call DoSomethingWithUnit(c)
        set u = null
        set c = null
    endfunction



    Or:

    Code (vJASS):
    function SecondWay takes nothing returns nothing
        local unit u = SomeUnit()
        call GetClosestUnit(GetUnitX(u), GetUnitY(u))
        call DoSomethingWithUnit(udg_ClosestUnit)
        set u = null
    endfunction
     
    Last edited: Dec 9, 2007
  3. PurplePoot

    PurplePoot

    Joined:
    Dec 14, 2005
    Messages:
    11,162
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    Looks fine, but you don't need the globals. (Well, I suppose it's easier to use Closest as a global rather than the H2I trick...)

    Skip the player, let them pass a boolexpr.
     
  4. Silvenon

    Silvenon

    Joined:
    Nov 22, 2006
    Messages:
    1,233
    Resources:
    1
    Tutorials:
    1
    Resources:
    1
    Yeah, that's what I thought too, so I hope it's ok if I leave it like that.

    Yeah you're right, that way they can filter with more flexibility than just a IsUnitEnemy filter :p
     
  5. PurplePoot

    PurplePoot

    Joined:
    Dec 14, 2005
    Messages:
    11,162
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    Yeah, it's fine, it would be nice if you did an alternate version though since not everyone uses vJass (though it would be nice if they did...)

    Anyways, boolexpr e? Reading a few functions recently? -.- (That's what they use in common and blizzard)
     
  6. Fulla

    Fulla

    Joined:
    Aug 3, 2005
    Messages:
    757
    Resources:
    2
    Tutorials:
    2
    Resources:
    2
    Stun a Unit, for X seconds:
    You could add an SFX extension as well.

    (I tried using jass tags but it screws up, becoming 1 gigantic line).

    Uses a Timer Stack

    Code (vJASS):
    globals
        integer array StunArray
        integer StunTotal=0
    endglobals

    struct stundata
        unit u=null
        real d=0
        boolean destroyplease=false
    endstruct

    function StunUnit_KeepStunned takes nothing returns nothing
        local integer i = 0
        local stundata dat
        loop
            exitwhen i==Stun_Total
            set dat=StunArray[i]
            if dat.destroyplease then
                set Stun_Total=Stun_Total-1
                set StunArray[i]=StunArray[Stun_Total]
                call dat.destroy()
                set i=i-1
            else
                set dat.d=dat.d-0.01
                call SetUnitPosition(dat.u,GetUnitX(dat.u),GetUnitY(dat.u))
                set dat.destroyplease=(dat.d<=0)
            endif
            set i=i+1
        endloop
        if Stun_Total==0 then
            call ReleaseTimer(GetExpiredTimer())
        endif
    endfunction

    function StunUnit takes unit u,real d returns nothing
        local stundata dat=stundata.create()
        set dat.d=d
        set dat.u=u
        if (Stun_Total==0) then
            call TimerStart(NewTimer(),.01,true,function StunUnit_KeepStunned)
        endif
        set Stun_Total=Stun_Total+1
        set StunArray[Stun_Total-1]=dat
    endfunction
     
    Last edited by a moderator: Oct 2, 2007
  7. PandaMine

    PandaMine

    Joined:
    Sep 4, 2007
    Messages:
    49
    Resources:
    0
    Resources:
    0
    You should probably mention there is a global integer array StunArray and also that the function requires JassNewGen, also I don't see any actual stunning going on (just moving a unit).

    It also uses a timer stack (release timer) which you havent specified, other then that the function looks good and efficient
     
  8. Fulla

    Fulla

    Joined:
    Aug 3, 2005
    Messages:
    757
    Resources:
    2
    Tutorials:
    2
    Resources:
    2
    Aha yea thx, updated.

    Yea its an alternative way of stunning without using A Stormbolt spell with 100 levels.

    On my map the function also takes an effect + attachment point so you can customise the overhead buff. I'd recommend the same.
    (Id need to included the whole AddTimedSfx for that).
     
  9. PandaMine

    PandaMine

    Joined:
    Sep 4, 2007
    Messages:
    49
    Resources:
    0
    Resources:
    0
    Yeah what I was saying is that you dont have IssueTargetOrder order for the spell or whatever you need to do to stun. Someone will put this in their map and say WTF this doesnt do anything. I know how custom stun systems work, they stun forever and then a timer removes the buff. There is however none of that in what you posted
     
  10. Silvenon

    Silvenon

    Joined:
    Nov 22, 2006
    Messages:
    1,233
    Resources:
    1
    Tutorials:
    1
    Resources:
    1
    I can do that if you want. I just have to tell them to create a global.
     
  11. PurplePoot

    PurplePoot

    Joined:
    Dec 14, 2005
    Messages:
    11,162
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    Yeah, that's what I meant.

    And Fulla, I edited your post to use
    Code (vJASS):
     tags.

    Also, it'd be easier to just store the struct to a cache on a timer with the duration of the stun, and then remove the stun after the end of the duration. (However you need to Pause them this way)

    Or cast a dummy ability that stuns forever then remove the buff after long enough, thus also adding a buff.
     
  12. Fulla

    Fulla

    Joined:
    Aug 3, 2005
    Messages:
    757
    Resources:
    2
    Tutorials:
    2
    Resources:
    2
    That actually sounds like a very sensible way.
     
  13. Silvenon

    Silvenon

    Joined:
    Nov 22, 2006
    Messages:
    1,233
    Resources:
    1
    Tutorials:
    1
    Resources:
    1
    *Fixed.

    I didn't see this. Hahaha......yeah, I have been reading a few functions recently :)
     
  14. Hero12341234

    Hero12341234

    Joined:
    Dec 6, 2006
    Messages:
    73
    Resources:
    2
    Tutorials:
    2
    Resources:
    2
    Regrow Trees

    This is a script I made that will revive trees that die for some reason. Whether it be a peasant picking at it or a spell that destroys trees like flame strike.

    Code (vJASS):
    function RegrowTrees takes nothing returns nothing
        local destructable tree=GetDyingDestructable()
        call TriggerSleepAction(10)
        call DestructableRestoreLife( tree, GetDestructableMaxLife(tree), true )
        set tree=null
    endfunction

    function Trig_Int_Tree_Revival takes nothing returns nothing
        local integer d=GetDestructableTypeId(GetEnumDestructable())
        if d=='ATtr' or d=='BTtw' or d=='KTtw' or d=='YTft' or d=='YTct' or d=='YTwt' or d=='JTwt' or d=='DTsh' or d=='FTtw' or d=='CTtr' or d=='ITtw' or d=='NTtw' or d=='OTtw' or d=='ZTtw' or d=='WTst' or d=='LTlt' or d=='GTsh' or d=='Xtlt' or d=='WTtw' or d=='ATtc' or d=='BTtc' or d=='CTtc' or d=='ITtc' or d=='ZTtc' then
        call TriggerRegisterDeathEvent( gg_trg_Regrow_Trees, GetEnumDestructable() )
        call TriggerAddAction(gg_trg_Regrow_Trees,function RegrowTrees)
    endif
    endfunction

    function Int_Tree_Revive takes nothing returns nothing
        call EnumDestructablesInRect(GetPlayableMapRect(), null, function Trig_Int_Tree_Revival)
    endfunction
     


    ___________

    I will add a test map later
     
  15. Silvenon

    Silvenon

    Joined:
    Nov 22, 2006
    Messages:
    1,233
    Resources:
    1
    Tutorials:
    1
    Resources:
    1
    GetPlayableMapRect() == bj_mapInitialPlayableArea


    Also, better use timer instead of TriggerSleepAction.......hehe I can't remember the last time I used that crappy function :)

    Anyways, it's a nice idea. Maybe you should put the time-to-regrow in a global so it can be easily changeable.

    EDIT: Maybe add a GetAbilityOrderString function Pooty? I don't know how to do that efficiently.
     
    Last edited: Oct 14, 2007
  16. Unhit

    Unhit

    Joined:
    Nov 4, 2004
    Messages:
    125
    Resources:
    2
    Maps:
    2
    Resources:
    2
    When stunning a unit for x seconds, I suggest using Aerial Shackles with infinite duration and then adding an expiration timer, or any timer after casting the spell. As far as I know, aerial shackles cannot be dispelled, but the only way to cancel it is to stun/silence the caster which won't work with a locust unit. So that way you get the same effect like stormbolt, with a duration of x.xx seconds if you like, and you only need one spell level :)
    (I have tried it this way in my map "BEER BEER", and so far it worked flawless)

    //Edit: Just saw that purplepoot posted something similiar already. Well, I suggest aerial shackles as dummy!
     
  17. Silvenon

    Silvenon

    Joined:
    Nov 22, 2006
    Messages:
    1,233
    Resources:
    1
    Tutorials:
    1
    Resources:
    1
    Code (vJASS):
    function PreloadSoundPath takes string path returns nothing
        local sound snd = CreateSound(path, false, false, false, 10, 10, "")
        call SetSoundVolume(snd, 0)
        call StartSound(snd)
        call KillSoundWhenDone(snd)
        set snd = null
    endfunction



    Just a retarded function that preloads a sound in the given path. If someone doesn't know, first time you play a sound nothing happens, because the sound is loaded first, so this function does that loading part.

    Maybe we should do something about these snippets Pooty? For example do something about this:

     
  18. busterkomo

    busterkomo

    Joined:
    Jun 17, 2007
    Messages:
    1,423
    Resources:
    1
    Tutorials:
    1
    Resources:
    1
    Code (vJASS):
    function Trig_Single_Player_Defeat_Actions takes nothing returns nothing
        //do whatever//
    endfunction

    //===========================================================================
    function Trig_Single_Player_Defeat_Conditions takes nothing returns boolean
        return bj_isSinglePlayer == true
    endfunction

    function InitTrig_Single_Player_Defeat takes nothing returns nothing
        set gg_trg_Single_Player_Defeat = CreateTrigger(  )
        call TriggerRegisterTimerEventSingle( gg_trg_Single_Player_Defeat, 15.00 )
        call TriggerAddCondition( gg_trg_Single_Player_Defeat, Condition( function Trig_Single_Player_Defeat_Conditions ) )
        call TriggerAddAction( gg_trg_Single_Player_Defeat, function Trig_Single_Player_Defeat_Actions )
    endfunction


    That will detect if the game is single player at 15 seconds.
     
  19. Silvenon

    Silvenon

    Joined:
    Nov 22, 2006
    Messages:
    1,233
    Resources:
    1
    Tutorials:
    1
    Resources:
    1
    Uhmmm......what?? This is just GUI with a line of custom script converted to JASS.
     
  20. busterkomo

    busterkomo

    Joined:
    Jun 17, 2007
    Messages:
    1,423
    Resources:
    1
    Tutorials:
    1
    Resources:
    1
    No it isn't..........