• 🏆 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!

Small Code Snippets

Level 31
Joined
Jul 10, 2007
Messages
6,306
First 500 primes

Useful for things like items that can only be equipped by certain heroes or fit into certain slots.

id = id1*id2*id3 etc

if it fits
id%targetId = 0

For example

2*3*7=42

42%5 = 2
42%2 = 0
42%3 = 0
42%7 = 0
42%11 = 9

JASS:
library Primes
    globals
        private integer array p
    endglobals
    function GetPrimeByIndex takes integer prime returns integer
        return p[prime]
    endfunction
    private module N
        private static method onInit takes nothing returns nothing
            set p[0]=2
            set p[1]=3
            set p[2]=5
            set p[3]=7
            set p[4]=11
            set p[5]=13
            set p[6]=17
            set p[7]=19
            set p[8]=23
            set p[9]=29
            set p[10]=31
            set p[11]=37
            set p[12]=41
            set p[13]=43
            set p[14]=47
            set p[15]=53
            set p[16]=59
            set p[17]=61
            set p[18]=67
            set p[19]=71
            set p[20]=73
            set p[21]=79
            set p[22]=83
            set p[23]=89
            set p[24]=97
            set p[25]=101
            set p[26]=103
            set p[27]=107
            set p[28]=109
            set p[29]=113
            set p[30]=127
            set p[31]=131
            set p[32]=137
            set p[33]=139
            set p[34]=149
            set p[35]=151
            set p[36]=157
            set p[37]=163
            set p[38]=167
            set p[39]=173
            set p[40]=179
            set p[41]=181
            set p[42]=191
            set p[43]=193
            set p[44]=197
            set p[45]=199
            set p[46]=211
            set p[47]=223
            set p[48]=227
            set p[49]=229
            set p[50]=233
            set p[51]=239
            set p[52]=241
            set p[53]=251
            set p[54]=257
            set p[55]=263
            set p[56]=269
            set p[57]=271
            set p[58]=277
            set p[59]=281
            set p[60]=283
            set p[61]=293
            set p[62]=307
            set p[63]=311
            set p[64]=313
            set p[65]=317
            set p[66]=331
            set p[67]=337
            set p[68]=347
            set p[69]=349
            set p[70]=353
            set p[71]=359
            set p[72]=367
            set p[73]=373
            set p[74]=379
            set p[75]=383
            set p[76]=389
            set p[77]=397
            set p[78]=401
            set p[79]=409
            set p[80]=419
            set p[81]=421
            set p[82]=431
            set p[83]=433
            set p[84]=439
            set p[85]=443
            set p[86]=449
            set p[87]=457
            set p[88]=461
            set p[89]=463
            set p[90]=467
            set p[91]=479
            set p[92]=487
            set p[93]=491
            set p[94]=499
            set p[95]=503
            set p[96]=509
            set p[97]=521
            set p[98]=523
            set p[99]=541
            set p[100]=547
            set p[101]=557
            set p[102]=563
            set p[103]=569
            set p[104]=571
            set p[105]=577
            set p[106]=587
            set p[107]=593
            set p[108]=599
            set p[109]=601
            set p[110]=607
            set p[111]=613
            set p[112]=617
            set p[113]=619
            set p[114]=631
            set p[115]=641
            set p[116]=643
            set p[117]=647
            set p[118]=653
            set p[119]=659
            set p[120]=661
            set p[121]=673
            set p[122]=677
            set p[123]=683
            set p[124]=691
            set p[125]=701
            set p[126]=709
            set p[127]=719
            set p[128]=727
            set p[129]=733
            set p[130]=739
            set p[131]=743
            set p[132]=751
            set p[133]=757
            set p[134]=761
            set p[135]=769
            set p[136]=773
            set p[137]=787
            set p[138]=797
            set p[139]=809
            set p[140]=811
            set p[141]=821
            set p[142]=823
            set p[143]=827
            set p[144]=829
            set p[145]=839
            set p[146]=853
            set p[147]=857
            set p[148]=859
            set p[149]=863
            set p[150]=877
            set p[151]=881
            set p[152]=883
            set p[153]=887
            set p[154]=907
            set p[155]=911
            set p[156]=919
            set p[157]=929
            set p[158]=937
            set p[159]=941
            set p[160]=947
            set p[161]=953
            set p[162]=967
            set p[163]=971
            set p[164]=977
            set p[165]=983
            set p[166]=991
            set p[167]=997
            set p[168]=1009
            set p[169]=1013
            set p[170]=1019
            set p[171]=1021
            set p[172]=1031
            set p[173]=1033
            set p[174]=1039
            set p[175]=1049
            set p[176]=1051
            set p[177]=1061
            set p[178]=1063
            set p[179]=1069
            set p[180]=1087
            set p[181]=1091
            set p[182]=1093
            set p[183]=1097
            set p[184]=1103
            set p[185]=1109
            set p[186]=1117
            set p[187]=1123
            set p[188]=1129
            set p[189]=1151
            set p[190]=1153
            set p[191]=1163
            set p[192]=1171
            set p[193]=1181
            set p[194]=1187
            set p[195]=1193
            set p[196]=1201
            set p[197]=1213
            set p[198]=1217
            set p[199]=1223
            set p[200]=1229
            set p[201]=1231
            set p[202]=1237
            set p[203]=1249
            set p[204]=1259
            set p[205]=1277
            set p[206]=1279
            set p[207]=1283
            set p[208]=1289
            set p[209]=1291
            set p[210]=1297
            set p[211]=1301
            set p[212]=1303
            set p[213]=1307
            set p[214]=1319
            set p[215]=1321
            set p[216]=1327
            set p[217]=1361
            set p[218]=1367
            set p[219]=1373
            set p[220]=1381
            set p[221]=1399
            set p[222]=1409
            set p[223]=1423
            set p[224]=1427
            set p[225]=1429
            set p[226]=1433
            set p[227]=1439
            set p[228]=1447
            set p[229]=1451
            set p[230]=1453
            set p[231]=1459
            set p[232]=1471
            set p[233]=1481
            set p[234]=1483
            set p[235]=1487
            set p[236]=1489
            set p[237]=1493
            set p[238]=1499
            set p[239]=1511
            set p[240]=1523
            set p[241]=1531
            set p[242]=1543
            set p[243]=1549
            set p[244]=1553
            set p[245]=1559
            set p[246]=1567
            set p[247]=1571
            set p[248]=1579
            set p[249]=1583
            set p[250]=1597
            set p[251]=1601
            set p[252]=1607
            set p[253]=1609
            set p[254]=1613
            set p[255]=1619
            set p[256]=1621
            set p[257]=1627
            set p[258]=1637
            set p[259]=1657
            set p[260]=1663
            set p[261]=1667
            set p[262]=1669
            set p[263]=1693
            set p[264]=1697
            set p[265]=1699
            set p[266]=1709
            set p[267]=1721
            set p[268]=1723
            set p[269]=1733
            set p[270]=1741
            set p[271]=1747
            set p[272]=1753
            set p[273]=1759
            set p[274]=1777
            set p[275]=1783
            set p[276]=1787
            set p[277]=1789
            set p[278]=1801
            set p[279]=1811
            set p[280]=1823
            set p[281]=1831
            set p[282]=1847
            set p[283]=1861
            set p[284]=1867
            set p[285]=1871
            set p[286]=1873
            set p[287]=1877
            set p[288]=1879
            set p[289]=1889
            set p[290]=1901
            set p[291]=1907
            set p[292]=1913
            set p[293]=1931
            set p[294]=1933
            set p[295]=1949
            set p[296]=1951
            set p[297]=1973
            set p[298]=1979
            set p[299]=1987
            set p[300]=1993
            set p[301]=1997
            set p[302]=1999
            set p[303]=2003
            set p[304]=2011
            set p[305]=2017
            set p[306]=2027
            set p[307]=2029
            set p[308]=2039
            set p[309]=2053
            set p[310]=2063
            set p[311]=2069
            set p[312]=2081
            set p[313]=2083
            set p[314]=2087
            set p[315]=2089
            set p[316]=2099
            set p[317]=2111
            set p[318]=2113
            set p[319]=2129
            set p[320]=2131
            set p[321]=2137
            set p[322]=2141
            set p[323]=2143
            set p[324]=2153
            set p[325]=2161
            set p[326]=2179
            set p[327]=2203
            set p[328]=2207
            set p[329]=2213
            set p[330]=2221
            set p[331]=2237
            set p[332]=2239
            set p[333]=2243
            set p[334]=2251
            set p[335]=2267
            set p[336]=2269
            set p[337]=2273
            set p[338]=2281
            set p[339]=2287
            set p[340]=2293
            set p[341]=2297
            set p[342]=2309
            set p[343]=2311
            set p[344]=2333
            set p[345]=2339
            set p[346]=2341
            set p[347]=2347
            set p[348]=2351
            set p[349]=2357
            set p[350]=2371
            set p[351]=2377
            set p[352]=2381
            set p[353]=2383
            set p[354]=2389
            set p[355]=2393
            set p[356]=2399
            set p[357]=2411
            set p[358]=2417
            set p[359]=2423
            set p[360]=2437
            set p[361]=2441
            set p[362]=2447
            set p[363]=2459
            set p[364]=2467
            set p[365]=2473
            set p[366]=2477
            set p[367]=2503
            set p[368]=2521
            set p[369]=2531
            set p[370]=2539
            set p[371]=2543
            set p[372]=2549
            set p[373]=2551
            set p[374]=2557
            set p[375]=2579
            set p[376]=2591
            set p[377]=2593
            set p[378]=2609
            set p[379]=2617
            set p[380]=2621
            set p[381]=2633
            set p[382]=2647
            set p[383]=2657
            set p[384]=2659
            set p[385]=2663
            set p[386]=2671
            set p[387]=2677
            set p[388]=2683
            set p[389]=2687
            set p[390]=2689
            set p[391]=2693
            set p[392]=2699
            set p[393]=2707
            set p[394]=2711
            set p[395]=2713
            set p[396]=2719
            set p[397]=2729
            set p[398]=2731
            set p[399]=2741
            set p[400]=2749
            set p[401]=2753
            set p[402]=2767
            set p[403]=2777
            set p[404]=2789
            set p[405]=2791
            set p[406]=2797
            set p[407]=2801
            set p[408]=2803
            set p[409]=2819
            set p[410]=2833
            set p[411]=2837
            set p[412]=2843
            set p[413]=2851
            set p[414]=2857
            set p[415]=2861
            set p[416]=2879
            set p[417]=2887
            set p[418]=2897
            set p[419]=2903
            set p[420]=2909
            set p[421]=2917
            set p[422]=2927
            set p[423]=2939
            set p[424]=2953
            set p[425]=2957
            set p[426]=2963
            set p[427]=2969
            set p[428]=2971
            set p[439]=2999
            set p[430]=3001
            set p[431]=3011
            set p[432]=3019
            set p[433]=3023
            set p[434]=3037
            set p[435]=3041
            set p[436]=3049
            set p[437]=3061
            set p[438]=3067
            set p[439]=3079
            set p[440]=3083
            set p[441]=3089
            set p[442]=3109
            set p[443]=3119
            set p[444]=3121
            set p[445]=3137
            set p[446]=3163
            set p[447]=3167
            set p[448]=3169
            set p[449]=3181
            set p[450]=3187
            set p[451]=3191
            set p[452]=3203
            set p[453]=3209
            set p[454]=3217
            set p[455]=3221
            set p[456]=3229
            set p[457]=3251
            set p[458]=3253
            set p[459]=3257
            set p[460]=3259
            set p[461]=3271
            set p[462]=3299
            set p[463]=3301
            set p[464]=3307
            set p[465]=3313
            set p[466]=3319
            set p[467]=3323
            set p[468]=3329
            set p[469]=3331
            set p[470]=3343
            set p[471]=3347
            set p[472]=3359
            set p[473]=3361
            set p[474]=3371
            set p[475]=3373
            set p[476]=3389
            set p[477]=3391
            set p[478]=3407
            set p[479]=3413
            set p[480]=3433
            set p[481]=3449
            set p[482]=3457
            set p[483]=3461
            set p[484]=3463
            set p[485]=3467
            set p[486]=3469
            set p[487]=3491
            set p[488]=3499
            set p[489]=3511
            set p[490]=3517
            set p[491]=3527
            set p[492]=3529
            set p[493]=3533
            set p[494]=3539
            set p[495]=3541
            set p[496]=3547
            set p[497]=3557
            set p[498]=3559
            set p[499]=3571
            set p[500]=3581
        endmethod
    endmodule
    private struct I extends array
        implement N
    endstruct
endlibrary
 
Last edited:
I agree with Bribe.
GetPrimeByIndex seems better :)

Man, if only recursion was ok in Jass :p
Then I'd be able to post this:

JASS:
library Fibonnacci

    function GetFibonnacciByIndex takes integer index returns integer
        if 0==index then
            return 0
        elseif index==1 then
            return 1
        endif
        return GetFibonnacciByIndex(index-1) + GetFibonnacciByIndex(index-2)
    endfunction

endlibrary

lmao

Since I can't find any use for a Fibonnacci Sequence library, I'll just post this instead:

JASS:
library GoldenRatio
    globals
        private constant real GOLDEN_RATIO = (1 + SquareRoot(5))/2
    endglobals

    function GetGoldenRatio takes nothing returns integer
        return GOLDEN_RATIO
    endfunction
endlibrary

I'm already using this in a map of mine to scale the images so that they look "Pleasing"
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
QSort optimized for integer comparisons (could put in compare and exchange methods to work with any type, but I personally needed integers so if someone wants to quickly mod this to use compare/exchange, go for it).

JASS:
library QSortI
    //static method sort takes integer size, boolean smallestToGreatest returns nothing
    //
    //Expects
    //  static integer array v
    module QSortI
        private static boolean y
        private static method is takes integer f, integer t returns nothing
            local integer i
            local integer j
            local integer m
            if (t>f) then
                set i=f+1
                loop
                    exitwhen i>t
                    set j=i
                    loop
                        exitwhen j<=f or (y and v[j]>v[j-1]) or (not y and v[j]<v[j-1])
                        set m=v[j]
                        set v[j]=v[j-1]
                        set v[j-1]=m
                        set j=j-1
                    endloop
                    set i=i+1
                endloop
            endif
        endmethod
        private static method s takes integer f, integer t returns nothing
            local integer p
            local integer a
            local integer i
            local integer m
            if (t>f) then
                if ((f+t)/2>t) then
                    set p=v[t]
                elseif ((f+t)/2>f) then
                    set p=v[(f+t)/2]
                else
                    set p=v[f]
                endif
            elseif ((f+t)/2>f) then
                set p=v[f]
            elseif ((f+t)/2>t) then
                set p=v[(f+t)/2]
            else
                set p=v[t]
            endif
            set a=t
            set i=f
            loop
                loop
                    exitwhen p>=v[a]
                    set a=a-1
                endloop
                loop
                    exitwhen p<=v[i]
                    set i=i+1
                endloop
                if (i<=a) then
                    if (i!=a) then
                        set m=v[a]
                        set v[a]=v[i]
                        set v[i]=m
                    endif
                    set a=a-1
                    set i=i+1
                endif
                exitwhen a<i
            endloop
            if (IAbsBJ(a-f)>10) then
                call s(f,a)
            else
                call is(f,a)
            endif
            if (IAbsBJ(t-i)>10) then
                call s(i,t)
            else
                call is(i,t)
            endif
        endmethod
        static method sort takes integer z, boolean l returns nothing
            set y=l
            call s(0,z-1)
        endmethod
    endmodule
endlibrary

JASS:
struct Tester extends array
    private static integer array v
    private static integer array v2
    implement QSortI
    private static method onInit takes nothing returns nothing
        local integer i
        local integer m=9
        local string s
        set v[0]=5
        set v[1]=3
        set v[2]=6
        set v[3]=9
        set v[4]=4
        set v[5]=5
        set v[6]=12
        set v[7]=13
        set v[8]=4
        /////////////////////////////////////////////////////
        set v2[0]=3
        set v2[1]=4
        set v2[2]=4
        set v2[3]=5
        set v2[4]=5
        set v2[5]=6
        set v2[6]=9
        set v2[7]=12
        set v2[8]=13
        
        set i=m
        set s=""
        loop
            exitwhen 0==i
            set i=i-1
            set s=I2S(v2[i])+","+s
        endloop
        call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,s)
        
        call sort(m,true)
        
        set i=m
        set s=""
        loop
            exitwhen 0==i
            set i=i-1
            set s=I2S(v[i])+","+s
        endloop
        call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,s)
    endmethod
endstruct
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
JASS:
library InitSave
    globals
        private trigger saveTrigger
        private trigger loadTrigger
    endglobals
    function DisableSave takes nothing returns nothing
        call DisableTrigger(saveTrigger)
    endfunction
    function EnableSave takes nothing returns nothing
        call EnableTrigger(saveTrigger)
    endfunction
    function DisableLoad takes nothing returns nothing
        call DisableTrigger(loadTrigger)
    endfunction
    function EnableLoad takes nothing returns nothing
        call EnableTrigger(loadTrigger)
    endfunction
    function InitSave takes string saveCommand, string loadCommand, conditionfunc saveFunc, conditionfunc loadFunc returns nothing
        //register save/load commands
        local integer i=11
        set saveTrigger=CreateTrigger()
        set loadTrigger=CreateTrigger()
        call TriggerAddCondition(saveTrigger,saveFunc)
        call TriggerAddCondition(loadTrigger,loadFunc)
        
        set loadCommand=loadCommand+" "
        loop
            if (GetPlayerSlotState(Player(i))==PLAYER_SLOT_STATE_PLAYING and GetPlayerController(Player(i))==MAP_CONTROL_USER) then
                call TriggerRegisterPlayerChatEvent(saveTrigger,Player(i),saveCommand,true)
                call TriggerRegisterPlayerChatEvent(loadTrigger,Player(i),loadCommand,false)
            endif
            exitwhen 0==i
            set i=i-1
        endloop
    endfunction
endlibrary
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
JASS:
library FindString
    function FindString takes string str, string strToFind, integer start returns integer
        local integer i=start
        local integer l=StringLength(str)
        local integer l2=StringLength(strToFind)
        loop
            exitwhen i>l-l2
            if (SubString(str,i,i+l2)==strToFind) then
                return i
            endif
            set i=i+1
        endloop
        return -1
    endfunction
endlibrary
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
JASS:
library RemoveString
    //removes all traces of a string from another string
    //0 means remove all
    //call RemoveString("123123","12",0,0) -> 33
    //call RemoveString("123-123","-",0,0) -> 123123
    
    //maxRemove is useful for removing an instance of a string once
    //call RemoveString("-load mau3-load -ffff","-load ",1,0) -> "mau3-load -ffff"
    function RemoveString takes string s, string str, integer maxRemove, integer start returns string
        local integer p = StringLength(str)
        local integer i = StringLength(s)
        local integer m = start
        if (0<i) then
            if (0==maxRemove) then
                set maxRemove=-1
            endif
            loop
                exitwhen m==i or 0==maxRemove
                if (str==SubString(s,m,m+p)) then
                    set maxRemove = maxRemove - 1
                    set s=SubString(s,0,m)+SubString(s,m+p,StringLength(s))
                    set i=i-1
                else
                    set m=m+1
                endif
            endloop
        endif
        return s
    endfunction
endlibrary

JASS:
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"|"+RemoveString("-load mau3-load -ffff","-load ",1,0)+"|")
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"|"+RemoveString("-123-456-","-",0,0)+"|")
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
JASS:
library SaveUnitLoc /*
*************************************************************************************
*
*   Saves unit's location
*
*************************************************************************************
*
*   */uses/*
*
*       */ NumberStack /*       hiveworkshop.com/forums/1993458-post521.html
*       */ WorldBounds /*       hiveworkshop.com/forums/jass-resources-412/snippet-worldbounds-180494/
*
************************************************************************************
*
*   function SaveUnitLoc takes NumberStack stack, unit whichUnit returns nothing
*   function LoadUnitLoc takes NumberStack stack, unit whichUnit returns nothing
*
************************************************************************************/
    function SaveUnitLoc takes NumberStack stack, unit whichUnit returns nothing
        local integer minX = WorldBounds.minX
        local integer minY = WorldBounds.minY
        local integer maxX = WorldBounds.maxX - minX
        local integer maxY = WorldBounds.maxY - minY
        local integer x = R2I(GetUnitX(whichUnit)) - minX
        local integer y = R2I(GetUnitY(whichUnit)) - minY
    
        call stack.push(x*100/maxX,100)
        call stack.push(y*100/maxY,100)
    endfunction
    function LoadUnitLoc takes NumberStack stack, unit whichUnit returns nothing
        local integer minX = WorldBounds.minX
        local integer minY = WorldBounds.minY
        local integer maxX = WorldBounds.maxX - minX
        local integer maxY = WorldBounds.maxY - minY
        local integer x = stack.pop(100)*maxX/100 + minX
        local integer y = stack.pop(100)*maxY/100 + minY
        
        call SetUnitY(whichUnit, x)
        call SetUnitX(whichUnit, y)
    endfunction
endlibrary

JASS:
library SaveUnitFacing /*
*************************************************************************************
*
*   Saves unit's facing
*
*************************************************************************************
*
*   */uses/*
*
*       */ NumberStack /*       hiveworkshop.com/forums/1993458-post521.html
*
************************************************************************************
*
*   function SaveUnitFacing takes NumberStack stack, unit whichUnit returns nothing
*   function LoadUnitFacing takes NumberStack stack, unit whichUnit returns nothing
*
************************************************************************************/
    function SaveUnitFacing takes NumberStack stack, unit whichUnit returns nothing
        local integer facing = R2I(GetUnitFacing(whichUnit))
        
        set facing = facing - facing/360*360
        
        if (0 > facing) then
            set facing = facing + 360
        endif
    
        call stack.push(facing*100/360,99)
    endfunction
    function LoadUnitFacing takes NumberStack stack, unit whichUnit returns nothing
        call SetUnitFacing(whichUnit, stack.pop(99)*360./100)
    endfunction
endlibrary
 
Last edited:
Level 31
Joined
Jul 10, 2007
Messages
6,306
JASS:
library SaveUnitLife /*
*************************************************************************************
*
*   Saves unit's life
*
*************************************************************************************
*
*   */uses/*
*
*       */ NumberStack /*       hiveworkshop.com/forums/1993458-post521.html
*
************************************************************************************
*
*   function SaveUnitLife takes NumberStack stack, unit whichUnit returns nothing
*   function LoadUnitLife takes NumberStack stack, unit whichUnit returns nothing
*
************************************************************************************/
    function SaveUnitLife takes NumberStack stack, unit whichUnit returns nothing
        call stack.push(R2I(GetWidgetLife(whichUnit)/GetUnitState(whichUnit, UNIT_STATE_MAX_LIFE)*100), 100)
    endfunction
    function LoadUnitLife takes NumberStack stack, unit whichUnit returns nothing
        call SetWidgetLife(whichUnit,stack.pop(100)*GetUnitState(whichUnit, UNIT_STATE_MAX_LIFE)/100)
    endfunction
endlibrary

JASS:
library SaveUnitMana /*
*************************************************************************************
*
*   Saves unit's mana if unit max mana isn't 0
*
*************************************************************************************
*
*   */uses/*
*
*       */ NumberStack /*       hiveworkshop.com/forums/1993458-post521.html
*
************************************************************************************
*
*   function SaveUnitMana takes NumberStack stack, unit whichUnit returns nothing
*   function LoadUnitMana takes NumberStack stack, unit whichUnit returns nothing
*
************************************************************************************/
    function SaveUnitMana takes NumberStack stack, unit whichUnit returns nothing
        if (0 != GetUnitState(whichUnit,UNIT_STATE_MAX_MANA)) then
            call stack.push(R2I(GetUnitState(whichUnit, UNIT_STATE_MANA)/GetUnitState(whichUnit, UNIT_STATE_MAX_MANA)*100),100)
        endif
    endfunction
    function LoadUnitMana takes NumberStack stack, unit whichUnit returns nothing
        if (0 != GetUnitState(whichUnit,UNIT_STATE_MAX_MANA)) then
            call SetUnitState(whichUnit, UNIT_STATE_MANA, stack.pop(100)*GetUnitState(whichUnit, UNIT_STATE_MAX_MANA)/100)
        endif
    endfunction
endlibrary
 
Last edited:
Level 31
Joined
Jul 10, 2007
Messages
6,306
stack.push is already pretty easy.


And for save/load hero stats, that's much too easy. Well, actually, just thought up a good way to do it but it's dependent upon how the map does stat allocation = ).


See, you'd just save allocated stat points, and the cap would be the total amount of stats points the hero has at a given level ; P.
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
JASS:
library Buffer
    //LoadToBuffer
    //  loads data from code into buffer (data not created)
    //
    //LoadFromBuffer
    //  loads data from buffer and creates it (data created)
    //
    globals
        private integer s = 0
        private integer p = 0
        private integer array v
    endglobals
    struct Buffer extends array
        static method reset takes nothing returns nothing
            set s=0
            set p=0
        endmethod
        static method write takes integer val returns integer
            set s=s+1
            set v[s]=val
            return s
        endmethod
        static method operator [] takes integer index returns integer
            return v[index]
        endmethod
        static method read takes nothing returns integer
            set p=p+1
            return v
        endmethod
    endstruct
endlibrary
 
Last edited:
Nice :D
Fast write, Fast read, Fast reset -> Awesome data structure (I know it's dynamic and limited, but it's quite useful)

edit
Nes inspired me to write a data structure based library, so I did:

JASS:
library RDS requires Table // Random Data Structure
    
    struct RDS extends array
        private static integer ic = 0
        private static integer ir = 0
        private thistype rn
        private Table data
        private integer index
        method throw takes integer i returns nothing
            set index = index + 1
            set data[index] = i
        endmethod
        method get takes nothing returns integer
            local integer i = GetRandomInt(1,index)
            local integer j = data[i]
            set data[i] = data[index]
            set index=index-1
            return j
        endmethod
        method delete takes integer k returns nothing
            local integer i = 1
            loop
                exitwhen i>index
                if k==data[i] then
                    set data[i] = data[index]
                    set index=index-1
                    return
                endif
                set i=i+1
            endloop
        endmethod
        static method create takes nothing returns thistype
            local thistype this
            if 0==ir then
                set ic=ic+1
                set this=ic
            else
                set this=ir
                set ir=.rn
            endif
            set .index = 0
            set .data = Table.create()
            return this
        endmethod
        method destroy takes nothing returns nothing
            call .data.destroy()
            set .rn=ir
            set ir=this
        endmethod
    endstruct
    
endlibrary

This is useful for a "Random Hero" system in an AoS map >:D

JASS:
struct Example
    globals
        private RDS Q
    endglobals

    static method run takes nothing returns boolean
        call CreateUnit(GetTriggerPlayer(),Q.get(),0,0,0)
    endmethod

    static method onInit takes nothing returns nothing
        local trigger t = CreateTrigger()
        set Q = RDS.create()
        call Q.throw('H000')
        call Q.throw('H001')
        call Q.throw('H002')
        call Q.throw('H003')
        call Q.throw('H004')
        call Q.throw('H005')
        call TriggerRegisterPlayerChatEvent(t,Player(0),"-random",true)
        call TriggerAddCondition(t,Condition(function thistype.run))
        set t = null
    endmethod

endstruct
 
Last edited:
Level 22
Joined
Nov 14, 2008
Messages
3,256
Last example was quite nice I have to say. Especially with the option of the delete method. Very useful for random picking although it should be created in a tutorial for GUIers/NotComplexEnoughJassers as they probably will never look here nor have a clue about the thingie.
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,464
Maybe I should move it to World Editor Help Zone?

You guys can decide.

@Mag, you should see the random data structure I built that uses
Table. Yours is static so you can't create/destroy random data lists
of large capacity, change the weight of various items, etc.

I really need to release that stuff! I don't know why I keep forgetting.
 
Level 22
Joined
Nov 14, 2008
Messages
3,256
The thing is that it's JASS and most people are afraid of it not knowing the fact that the whole game is built upon it (and JASS built upon C but that's a bit too much for people not even knowing WC3 GUI). That's how I think it is but I find this thread very amusing and creative (in a learning perspektive).

I really need to release that stuff! I don't know why I keep forgetting.

LOL I should make a reminder app in VB (haha my school don't support JAVA/Ruby/C++/Python) that will remind you in an annoying way.
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
All you really need is the assembly language (even if technically there are many assembly languages)
You also need to move this topic in all wc3 related forums, so the jass will conquer this forum and then the world.
Bad_jokes™
 

Bannar

Code Reviewer
Level 26
Joined
Mar 19, 2008
Messages
3,140
I guess that you guys are mostly right. Most ppl ingore threads that are 'underlined' (at the top of given section) because they think that those thread are for geniuses.
Although you might have a luck with catching up few random guys to learn some new things.
Like me xD. I'm reading this thread from time to time.. and I've learnt a lot. I realy appreciate what you are doing here ;<
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
Useful for when needing to add xp for something like a quest in a map that uses the Hero Reward lib.

JASS:
library GetBonusXP
    //xpRate: desired xp rate, like 350
    //awardLevel: the level of the award, like a level 5 quest
    //awardedLevel: the level of the awarded thing, like a level 10 unit
    //xpRecieveRate: the rate at which xp recieved increases relative to level (bigger means more xp)
    //xpReqRate: the rate at which xp requirement increases relative to level (bigger means more xp required)
    
    //maxes:
    //  xpBountyRate:   1.8
    //  xpReqRate:      2.1
    
    function GetBonusXP takes integer awardLevel, integer awardedLevel, real xpRate, real xpRecieveRate, real xpReqRate returns integer
        local real xp = Pow(awardLevel+1,xpRecieveRate)
        local real xpReq = Pow(awardedLevel,xpReqRate)
        
        //calculate awarded xp
        return R2I(xp/xpReq*xpRate+.5)
    endfunction
endlibrary
 
JASS:
library UnitAlive requires UnitIndexer, UnitEvent

    function UnitAlive takes unit u returns boolean
        return not IsUnitDead(GetUnitUserData(u))
    endfunction

endlibrary

This way, if you want to remove the native UnitAlive from a system (Status for example),
you don't have to touch anything but the native declaration.
 
Last edited:
Level 12
Joined
Feb 22, 2010
Messages
1,115
This function will cause desync if units have another different object editor setting other than MODEL.

JASS:
function CreateUnitWithDifferentModel takes player p, integer unitId1, integer unitId2, real x, real y, real facing returns unit
//Desync if there is another different object editor setting other than model :O
    local integer id = unitId1
    if GetLocalPlayer() == p then
        set id = unitId2
    endif
    return CreateUnit(p, id, x, y, facing)
endfunction

Edit:
•This works with icon... until a player sees icon (So you can say it doesn't work :D)

•There is something wrong with soundsets but I don't know what is it, It sometimes causes desync and sometimes not.I tested with footman and highelfswordsman soundsets.

•Anything else?
 
Last edited:
Level 12
Joined
Feb 22, 2010
Messages
1,115
I surprised when
JASS:
GetEventDamage
didn't cause desync when I did this.

JASS:
function TEST5ACT takes nothing returns nothing
    local unit u = CreateUnit(Player(0), 'hfoo', 0, 0, 0)
    local real x = 10.00
    if GetLocalPlayer() == Player(0) then
        set x = 20.00
    endif
    call TriggerRegisterUnitEvent(gg_trg_Test_5_Show_Damage, u, EVENT_UNIT_DAMAGED)
    call UnitDamageTarget(u, u, x, true, false, null, null, null)
    
endfunction

It showed 20 to me, and 10 to my friend.This works until unit is dead, or... or I don't know it causes desync when there are some other units.(maybe enemy units while checking priority for w3 AI, but there was a neutral hostile unit and it didn't cause desync until footman dies.)
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
In general it doesn't descyn until the result "game" is the same.
I've already given successfully different positions for players with SetUnitX/Y, it did cause desync only when the unit was attacked (well there are probably many other cases like enter in range, region, ....).

But your result is quite surprising, that would mean life of units is not (always) synchronized, weird.
 
Top