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 Lich King demands your service! We've reached the 19th edition of the Icon Contest. Come along and make some chilling servants for the one true king.
    Dismiss Notice
  4. The 4th SFX Contest has started. Be sure to participate and have a fun factor in it.
    Dismiss Notice
  5. The poll for the 21st Terraining Contest is LIVE. Be sure to check out the entries and vote for one.
    Dismiss Notice
  6. The results are out! Check them out.
    Dismiss Notice
  7. Don’t forget to sign up for the Hive Cup. There’s a 555 EUR prize pool. Sign up now!
    Dismiss Notice
  8. The Hive Workshop Cup contest results have been announced! See the maps that'll be featured in the Hive Workshop Cup tournament!
    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. Troll-Brain

    Troll-Brain

    Joined:
    Apr 27, 2008
    Messages:
    2,372
    Resources:
    1
    JASS:
    1
    Resources:
    1
    Hmm, you are probably right, i was focalised on SubString usage in a loop, but on a second watch you have only few loop iterations, especially with low integers.
    And since your code is shorter i would vote for you now :p
     
  2. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,146
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    Ahem, Bribe, yours leaks way, way more strings....

    I just tested it and after like 1.5 seconds using Troll-Brain's idea of a changing variable, yours went down to <1 fps. It took mine like 25 seconds to get down to 3fps >.>.

    edit
    for constant, Bribe is right that his is faster, but it again leaks masses of strings compared to mine.

    I think the string leaks here are a bigger issue than speed.

    edit
    updated

    Code (vJASS):

    function AddCommas takes integer n returns string
        local integer m = 0
        local string s = ""
        local boolean b = false
        if (n == 0) then
            return "0"
        elseif (n < 0) then
            set n = -n
            set b = true
        endif
        loop
            set m = n
            set n = n/1000
            set m = m-n*1000
            if (n > 0) then
                if (m < 10) then
                    set s = ",00" + I2S(m) + s
                elseif (m < 100) then
                    set s = ",0" + I2S(m) + s
                else
                    set s = "," + I2S(m) + s
                endif
            elseif (b) then
                return "-" + I2S(m) + s
            else
                return I2S(m) + s
            endif
        endloop
        return ""
    endfunction
     


    Mine is now consistently faster than Bribe's + leaks way less (tested).

    Updated the snippet post as well

    gg

    edit
    1 minute 43 seconds on new version to get to 3 fps.

    Case closed.
     
    Last edited: May 22, 2011
  3. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    8,059
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    See what happens when you challenge Nestharus? He finds a way to make his script even better, building the best possible resource. Win/win situation ^^

    And he's right with the string leaks, this build won't leak nearly as much as mine! So I guess this proves that strings are still leaking even after the return bug was fixed. Why is WarCraft III still caching strings to that table when it's 100% useless now?
     
  4. Troll-Brain

    Troll-Brain

    Joined:
    Apr 27, 2008
    Messages:
    2,372
    Resources:
    1
    JASS:
    1
    Resources:
    1
    They follow Murphy's law when they add/revome jass stuff in a patch.

    Also since the begin i couldn't find about any really usefull case where typecasting string to integer to get the string "pointer" was really useful.
    Only code <-> integer is a real lost, and also the typcast widget <-> unit/destructable/item (but it can be done with an hashtable)
     
  5. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    8,059
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    It was possible to get a unique value from a string - you now must rely on the much slower StringHash native.
     
  6. Troll-Brain

    Troll-Brain

    Joined:
    Apr 27, 2008
    Messages:
    2,372
    Resources:
    1
    JASS:
    1
    Resources:
    1
    I know, but for what ?
    There wasn't the hashtable type but only gamecache.
     
  7. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    8,059
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    Getting the ordinal value of a string is a useful thing, so is getting the hex value of a string. This is because Blizzard forgot or didn't care enough (or both) to add such a useful thing.
     
  8. Troll-Brain

    Troll-Brain

    Joined:
    Apr 27, 2008
    Messages:
    2,372
    Resources:
    1
    JASS:
    1
    Resources:
    1
    Any concrete example in jass ?
     
  9. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    8,059
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
  10. Troll-Brain

    Troll-Brain

    Joined:
    Apr 27, 2008
    Messages:
    2,372
    Resources:
    1
    JASS:
    1
    Resources:
    1
    Ok, that would indeed be good to have the position in the string table of a string, for the ASCII library we would be able to use directly the integer array variable (i mean without maths).
    Hopefully even if it's not perfect StringHash is enough in most of cases, anyway it's not like we have other ways.
     
  11. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    This post used to contain a "Wait" library.
    It has been deleted due to "functionality" issues =P
     
    Last edited: May 28, 2011
  12. Weep

    Weep

    Joined:
    Jul 12, 2009
    Messages:
    316
    Resources:
    3
    Models:
    2
    Spells:
    1
    Resources:
    3
    I don't think so...considering it doesn't actually work, at least not when I tested it.

    1. Leaks a timer
    2. Example won't even work due to uninitialized integer crashing the thread
    3. Function itself won't even work due to no TriggerSleepAction meaning the loop will just run until the condition is satisfied, which will never happen since no time is passing during the loop, so it'll just terminate the thread due to an infinite loop
    3b. Even if I'm wrong about the reason, I tried your function and no actions listed after set i = Wait(0.25) ran, so no matter what it for sure doesn't work
    4. If you added TSA then it won't work for low values meaning this has no advantage over PolledWait

    I was hoping there was some kind of magic with setting a local to a function's value rather than doing call Function, but, not as far as I can tell. Did you even test this? If it worked for you, can you upload a test map?
     
  13. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    Nah, I didn't test it :p
    Thanks for telling me it doesn't work :L
    .. I guess Jass sux then ^^ lol
    I'll remove it :p ( that's gonna be an awkward post :ain:)
     
  14. Troll-Brain

    Troll-Brain

    Joined:
    Apr 27, 2008
    Messages:
    2,372
    Resources:
    1
    JASS:
    1
    Resources:
    1
    The only way to use a timer as friendly as TSA is to build a preprocessor which will split the code in as many functions as needed and also convert locals variables in globals. (and use a timer recycling or whatever)
    It wouldn't be for speedfreaks but that would still be cool.
     
  15. D4RK_G4ND4LF

    D4RK_G4ND4LF

    Joined:
    Feb 4, 2009
    Messages:
    1,196
    Resources:
    20
    Models:
    3
    Spells:
    15
    Tutorials:
    2
    Resources:
    20
    indexed globals of course
    but someone made something like that with macros already (maybe you can find it with forum search)
    edit:
    http://www.wc3c.net/showthread.php?t=101988
    even found that with hive forum search...weird
    edit2:
    and this is the one which was in my mind
    http://www.hiveworkshop.com/forums/graveyard-418/vjass-sleepaction-new-wait-178185/
    not perfect though (if it was everybody would use it and you would know about it :p)

    and to contribute something:
    rotate a vector by the angle of another vector
    A will be rotated by the angle between x-axis and B
    a is the length of A
    you can remove everything with a if you normalize A
    this is an image of what the first function does
    returns the coordinates of C
    [​IMG]
    Code (vJASS):
    function subtractVectorAngleX takes real ax, real ay, real bx, real by, real a returns real
        return ax/a*bx + ay/a*by
    endfunction

    function subtractVectorAngleY takes real ax, real ay, real bx, real by, real a returns real
        return ay/a*bx - ax/a*by
    endfunction

    function addVectorAngleX takes real ax, real ay, real bx, real by, real a returns real
        return ax/a*bx - ay/a*by
    endfunction

    function addVectorAngleY takes real ax, real ay, real bx, real by, real a returns real
        return ay/a*bx + ax/a*by
    endfunction
    probably quite simple but I had a need for it
    I also requested a 3D version and maybe someone is kind enough to answer my biddings :grin:
    you can find the derivation there
    http://www.hiveworkshop.com/forums/requests-341/formula-request-3d-vector-rotation-196220/
     
  16. Troll-Brain

    Troll-Brain

    Joined:
    Apr 27, 2008
    Messages:
    2,372
    Resources:
    1
    JASS:
    1
    Resources:
    1
    I know these solutions but that's hardly the same thing as using a timer like a TriggerSleepAction.

    Just to prove that i know what i'm talking about :

    http://www.wc3c.net/showthread.php?t=106581

    But i actually i have not the skills to make an efficient preprocessor (learning C when i'm in the mood of it), nor the desire.
     
  17. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,146
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    Useful values for dummy spell making. Max coords are the maximum possible coordinate on a map. Max range is the maximum possible distance on a map (don't need 99999 on spells).

    Code (vJASS):

    MAX_COORD = 32556
    MIN_COORD = -32556
    MAX_RANGE = 92083
     
     
  18. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    Code (vJASS):
    MAX_COORD = 32556
    MIN_COORD = -32556
    MAX_RANGE = 92083


    Yeah! :D
    Those are useful :D

    -> In library form:

    Code (vJASS):
    library Constants
        globals
            real MAX_COORD = 32556.
            real MIN_COORD = -MAX_COORD
            real MAX_RANGE = 92083.
        endglobals
    endlibrary


    This may be useful too:

    An array lookup is much faster than a function call:
    Code (vJASS):
    library Players
        globals
            player array PLAYER
        endglobals
        private module Init
            private static method onInit takes nothing returns nothing
                local integer i = 0
                loop
                    exitwhen i > 15
                    set PLAYER[i] = Player(i)
                    set i = i + 1
                endloop
            endmethod
        endmodule
        private struct Inits
            implement Init
        endstruct
    endlibrary
     
  19. xD.Schurke

    xD.Schurke

    Joined:
    Feb 22, 2006
    Messages:
    948
    Resources:
    5
    Maps:
    1
    Spells:
    4
    Resources:
    5
    doesnt matter, if you use 99999 or those values.
     
  20. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    @xD.Schurke

    Yeah, but it's better to use MAX_RANGE for ... senseless reasons :p
    MAX_COORD and MIN_COORD are VERY useful (That way you don't have to call WorldBounds() that often anymore :])