WC3JASS.com latest Snippets

[Snippet] Game Clock


Created by nel
December 21, 2018, 03:09:29 AM

Game Clock

It allows you format the time and get the elapsed time.

Code: jass
  1. //! zinc
  2. library GameClock { /* v1.0.0.7
  3. *************************************************************************************
  4. *
  5. *    Game Clock
  6. *        by nel
  7. *
  8. *       It allows you format the time and get the elapsed time.
  9. *
  10. ************************************************************************************
  11. *
  12. *   SETTING
  13. *
  14. */
  15.     private constant string DELIMITER = " : "; /* It used to seperate the hour,
  16.                                                   minute, and second */
  17. /*
  18. ************************************************************************************
  19. *
  20. *    struct GameClock extends array
  21. *
  22. *        static method FormatTime takes real seconds return string
  23. *            - It returns time as hh/mm/ss format.
  24. *
  25. *        static method operator ElapsedTime takes nothing returns real
  26. *            - It returns elapsed time.
  27. *
  28. *        static method operator ElapsedTimeEx takes nothing returns string
  29. *            - It returns elapsed time as hh/mm/ss format.
  30. *
  31. *        static method operator Delimiter takes nothing returns string
  32. *            - It returns GameClock's delimiter.
  33. *
  34. *************************************************************************************
  35. *
  36. *    Issue:
  37. *
  38. *       This library will not give you an exact current elapsed time.
  39. *
  40. ************************************************************************************/
  41.  
  42.     //! textmacro GAMECLOCK_TIMEFORMAT takes Time, delimiter
  43.         if( $Time$ < 10 ) {
  44.             format = format + "0" + I2S( $Time$ ) $delimiter$;
  45.         } else {
  46.             format = format + I2S( $Time$ ) $delimiter$;
  47.         }
  48.     //! endtextmacro
  49.  
  50.     private constant timer TIMER = CreateTimer();
  51.  
  52.     public { struct GameClock [] {
  53.         public {
  54.             static method FormatTime( real seconds ) -> string {
  55.                 integer s = R2I( ModuloReal( seconds, 60 ) );
  56.                 integer m = R2I( ModuloReal( seconds, 3600 ) / 60 );
  57.                 integer h = R2I( seconds / 3600 );
  58.                 string format = "";
  59.  
  60.                 //! runtextmacro GAMECLOCK_TIMEFORMAT( "h", "+ DELIMITER" )
  61.                 //! runtextmacro GAMECLOCK_TIMEFORMAT( "m", "+ DELIMITER" )
  62.                 //! runtextmacro GAMECLOCK_TIMEFORMAT( "s", "" )
  63.  
  64.                 return format;
  65.             }
  66.  
  67.             static method operator ElapsedTime() -> real {
  68.                 return TimerGetElapsed( TIMER );
  69.             }
  70.  
  71.             static method operator ElapsedTimeEx() -> string {
  72.                 return thistype.FormatTime( TimerGetElapsed(TIMER) );
  73.             }
  74.  
  75.             static method operator Delimiter() -> string {
  76.                 return DELIMITER;
  77.             }
  78.         }
  79.  
  80.         private static method onInit() {
  81.             TimerStart( TIMER, 100000000, true, null );
  82.         }
  83.     }}
  84. }
  85. //! endzinc

[Snippet] NoSaveGame


Created by nel
November 28, 2018, 11:50:18 PM

NoSaveGame

It prevents to use Save Game function to all players.

Code: jass
  1. //! zinc
  2. library NoSaveGame  /* v1.0.0.5
  3. ************************************************************************************
  4. *
  5. *    No Save Game
  6. *        by nel
  7. *
  8. *       It prevents to use Save Game function to all players.
  9. *
  10. ***********************************************************************************/
  11.  
  12.  
  13.        requires
  14.  
  15.            optional SimError,         /* http://www.wc3c.net/showthread.php?t=101260 */
  16.            optional RegisterGameEvent /* https://www.hiveworkshop.com/threads/snippet-registerevent-pack.250266/ */
  17.  
  18.  
  19. /***********************************************************************************
  20. *
  21. *   SETTINGS
  22. *
  23. */
  24. {
  25.     public {
  26.         /*
  27.         *    It's a content of the error message.
  28.         */
  29.         string  nsgErrMsg = "You are not allowed to use Save Game.";
  30.  
  31.         /*
  32.         *    Toggleable Error Message.
  33.         */
  34.         boolean nsgDisplayErrMsg = true;
  35.  
  36.         /*
  37.         *    Toggleable NoSaveGame.
  38.         */
  39.         boolean nsg = true;
  40.     }
  41.  
  42. /*
  43. ***********************************************************************************/
  44.  
  45.     private {
  46.         constant dialog DIALOG = DialogCreate();
  47.         constant timer TIMER = CreateTimer();
  48.  
  49.         struct NoSaveGame [] { private static method onInit() {
  50.             trigger Stop = CreateTrigger();
  51.  
  52.             static if( LIBRARY_RegisterGameEvent ) {
  53.                 RegisterGameEvent(EVENT_GAME_SAVE, function() {
  54.                     player pl;
  55.  
  56.                     if( nsg ) {
  57.                         pl = GetLocalPlayer();
  58.                        
  59.                         DialogDisplay(pl, DIALOG, true);
  60.                         TimerStart(TIMER, 0, false, null);
  61.  
  62.                         static if( LIBRARY_SimError ) {
  63.                             SimError(pl, nsgErrMsg);
  64.                         } else {
  65.                             DisplayTimedTextToPlayer(pl, 0.52, 0.96, 2.00,
  66.                                 "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" +
  67.                                 "|cffffcc00" + nsgErrMsg + "|r"
  68.                             );
  69.                         }
  70.  
  71.                         pl = null;
  72.                     }
  73.                 });
  74.  
  75.                 TriggerRegisterTimerExpireEvent(Stop, TIMER);
  76.                 TriggerAddCondition(Stop, Condition(function() -> boolean {
  77.                     if( nsg ) {
  78.                         DialogDisplay(GetLocalPlayer(), DIALOG, false);
  79.                     }
  80.  
  81.                     return false;
  82.                 }));
  83.  
  84.                 Stop = null;
  85.             } else {
  86.                 trigger Save = CreateTrigger();
  87.  
  88.                 TriggerRegisterGameEvent(Save, EVENT_GAME_SAVE);
  89.                 TriggerAddCondition(Save, Condition(function() -> boolean {
  90.                     player pl;
  91.  
  92.                     if( nsg ) {
  93.                         pl = GetLocalPlayer();
  94.  
  95.                         DialogDisplay(pl, DIALOG, true);
  96.                         TimerStart(TIMER, 0, false, null);
  97.  
  98.                         if( nsgDisplayErrMsg ) {
  99.                             static if( LIBRARY_SimError ) {
  100.                                 SimError(pl, nsgErrMsg);
  101.                             } else {
  102.                                 DisplayTimedTextToPlayer(pl, 0.52, 0.96, 2.00,
  103.                                     "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" +
  104.                                     "|cffffcc00" + nsgErrMsg + "|r"
  105.                                 );
  106.                             }
  107.                         }
  108.  
  109.                         pl = null;
  110.                     }
  111.  
  112.                     return false;
  113.                 }));
  114.  
  115.                 TriggerRegisterTimerExpireEvent(Stop, TIMER);
  116.                 TriggerAddCondition(Stop, Condition(function() -> boolean {
  117.                     if( nsg ) {
  118.                         DialogDisplay(GetLocalPlayer(), DIALOG, false);
  119.                     }
  120.  
  121.                     return false;
  122.                 }));
  123.  
  124.                 Save = null;
  125.                 Stop = null;
  126.             }
  127.         }}
  128.     }
  129. }
  130. //! endzinc

BoolexprUtils


Created by AGD
November 08, 2016, 08:04:14 AM

True and False BooleanExpressions

Credits goes to Vexorian for the first idea.


Vjass version
Code: jass
  1. library BoolexprUtils
  2.  
  3.     globals
  4.         boolexpr BOOLEXPR_TRUE
  5.         boolexpr BOOLEXPR_FALSE
  6.     endglobals
  7.  
  8.     private module Init
  9.  
  10.         private static method filterTrue takes nothing returns boolean
  11.             return true
  12.         endmethod
  13.  
  14.         private static method filterFalse takes nothing returns boolean
  15.             return false
  16.         endmethod
  17.  
  18.         private static method onInit takes nothing returns nothing
  19.             set BOOLEXPR_TRUE = Filter(function thistype.filterTrue)
  20.             set BOOLEXPR_TRUE = Filter(function thistype.filterFalse)
  21.         endmethod
  22.  
  23.     endmodule
  24.  
  25.     private struct S extends array
  26.         implement Init
  27.     endstruct
  28.  
  29. endlibrary

Zinc version
Code: jass
  1. //! zinc
  2. library BoolexprUtils {
  3.  
  4.     public boolexpr BOOLEXPR_TRUE, BOOLEXPR_FALSE;
  5.  
  6.     module Init {
  7.         static method onInit() {
  8.             BOOLEXPR_TRUE = Filter(function() -> boolean {return true;});
  9.             BOOLEXPR_FALSE = Filter(function() -> boolean {return false;});
  10.         }
  11.     }
  12.  
  13.     struct S extends array {module Init;}
  14.  
  15. }
  16. //! endzinc

UnitRecycler


Created by AGD
November 02, 2016, 03:36:36 AM

A useful library which allows you to recycle units (even dead ones, but they must leave a corpse), avoiding yet another permanent 0.04kb memory leak for each future CreateUnit() call.


Script
Code: jass
  1. library UnitRecycler /* v1.3b
  2.  
  3.  
  4.     |=============|
  5.     | Author: AGD |
  6.     |=============|
  7.  
  8.     */requires /*
  9.  
  10.     */ReviveUnit                        /*  https://wc3modding.info/4469/snippet-reviveunit/
  11.     */UnitDex                           /*  http://www.hiveworkshop.com/threads/system-unitdex-unit-indexer.248209/
  12.     */optional Table                    /*  https://wc3modding.info/4611/snippet-new-table/
  13.     */optional TimerUtils               /*  https://wc3modding.info/5352/hibrid-timerutils/
  14.     */optional RegisterPlayerUnitEvent  /*  https://wc3modding.info/4907/snippet-registerplayerunitevent/
  15.  
  16.     This system is important because CreateUnit() is one of the most processor-intensive function in
  17.     the game and there are reports that even after they are removed, they still leave some bit of memory
  18.     consumption (0.04 KB) on the RAM. Therefore it would be very helpful if you can minimize unit
  19.     creation or so. This system also allows you to recycle dead units to avoid permanent 0.04 KB memory
  20.     leak for each future CreateUnit() call.                                                                 */
  21.  
  22. //! novjass
  23.  
  24.     [Credits]
  25.         Aniki - For suggesting ideas on further improvements
  26.  
  27.  
  28.     |-----|
  29.     | API |
  30.     |-----|
  31.  
  32.         function GetRecycledUnit takes player owner, integer rawCode, real x, real y, real facing returns unit/*
  33.             - Returns unit of specified ID from the stock of recycled units. If there's none in the stock that
  34.               matched the specified unit's rawcode, it will create a new unit instead
  35.             - Returns null if the rawcode's unit-type is a hero or non-existent
  36.  
  37.       */function GetRecycledUnitEx takes player owner, integer rawCode, real x, real y, real facing returns unit/*
  38.             - Works similar to GetRecycledUnit() except that if the input rawcode's unit-type is a hero, it will
  39.               be created via CreateUnit() instead
  40.             - You can use this as an alternative to CreateUnit()
  41.  
  42.       */function RecycleUnit takes unit u returns boolean/*
  43.             - Recycles the specified unit and returns a boolean value depending on the success of the operation
  44.             - Does nothing to hero units
  45.  
  46.       */function RecycleUnitEx takes unit u returns boolean/*
  47.             - Works similar to RecycleUnit() except that if <u> is not recyclable, it will be removed via
  48.               RemoveUnit() instead
  49.             - You can use this as an alternative to RemoveUnit()
  50.  
  51.       */function RecycleUnitDelayed takes unit u, real delay returns nothing/*
  52.             - Recycles the specified unit after <delay> seconds
  53.  
  54.       */function RecycleUnitDelayedEx takes unit u, real delay returns nothing/*
  55.             - Works similar to RecycleUnitDelayed() except that it calls RecycleUnitEx() instead of RecycleUnit()
  56.  
  57.       */function UnitAddToStock takes integer rawCode returns boolean/*
  58.             - Creates a unit of type ID and adds it to the stock of recycled units then returns a boolean value
  59.               depending on the success of the operation
  60.  
  61. *///! endnovjass
  62.  
  63.     //CONFIGURATION SECTION
  64.  
  65.  
  66.     globals
  67.  
  68. /*      The owner of the stocked/recycled units
  69. */      private constant player OWNER               = Player(15)
  70.  
  71. /*      Determines if dead units will be automatically recycled
  72.         after a delay designated by the <constant function
  73.         DeathTime below>
  74. */      private constant boolean AUTO_RECYCLE_DEAD  = true
  75.  
  76. /*      Error debug message prefix
  77. */      private constant string ERROR_PREFIX        = "|CFFFF0000Operation Failed: "
  78.  
  79.     endglobals
  80.  
  81.     /* The delay before dead units will be recycled in case AUTO_RECYCLE_DEAD == true */
  82.     static if AUTO_RECYCLE_DEAD then
  83.         private constant function DeathTime takes unit u returns real
  84.             /*if <condition> then
  85.                   return someValue
  86.               elseif <condition> then
  87.                   return someValue
  88.               endif                 */
  89.             return 8.00
  90.         endfunction
  91.     endif
  92.  
  93.     /* When recycling a unit back to the stock, these resets will be applied to the
  94.        unit. You can add more actions to this or you can delete this textmacro if you
  95.        don't need it.                                                                       */
  96.         //! textmacro_once UNIT_RECYCLER_RESET
  97.             call SetUnitScale(u, 1, 0, 0)
  98.             call SetUnitVertexColor(u, 255, 255, 255, 255)
  99.             call SetUnitFlyHeight(u, GetUnitDefaultFlyHeight(u), 0)
  100.         //! endtextmacro
  101.  
  102.  
  103.     //END OF CONFIGURATION
  104.  
  105.     /*==== Do not do changes below this line if you're not so sure on what you're doing ====*/
  106.     native UnitAlive takes unit u returns boolean
  107.  
  108.     globals
  109.         private keyword S
  110.         private integer count = 0
  111.         private real unitCampX
  112.         private real unitCampY
  113.         private integer array stack
  114.         private boolean array stacked
  115.     endglobals
  116.  
  117.     private function GetIndex takes integer rawCode returns integer
  118.         static if LIBRARY_Table then
  119.             local integer i = S.table.integer[rawCode]
  120.             if i == 0 then
  121.                 set count = count + 1
  122.                 set S.table.integer[rawCode] = count
  123.                 set i = count
  124.             endif
  125.         else
  126.             local integer i = LoadInteger(S.hash, -1, rawCode)
  127.             if i == 0 then
  128.                 set count = count + 1
  129.                 call SaveInteger(S.hash, -1, rawCode, count)
  130.                 set i = count
  131.             endif
  132.         endif
  133.         return i
  134.     endfunction
  135.  
  136.     static if DEBUG_MODE then
  137.         private function Debug takes string msg returns nothing
  138.             call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "|CFFFFCC00[Unit Recycler]|R " + msg)
  139.         endfunction
  140.     endif
  141.  
  142.     function GetRecycledUnit takes player owner, integer rawCode, real x, real y, real facing returns unit
  143.         local integer i
  144.         if not IsHeroUnitId(rawCode) then
  145.             set i = GetIndex(rawCode)
  146.             if stack[i] == 0 then
  147.                 set bj_lastCreatedUnit = CreateUnit(owner, rawCode, x, y, facing)
  148.                 debug call Debug(GetUnitName(bj_lastCreatedUnit) + " stock is empty, creating new " + GetUnitName(bj_lastCreatedUnit))
  149.             else
  150.                 static if LIBRARY_Table then
  151.                     set bj_lastCreatedUnit = S.hash[i].unit[stack[i]]
  152.                 else
  153.                     set bj_lastCreatedUnit = LoadUnitHandle(S.hash, i, stack[i])
  154.                 endif
  155.                 set stacked[GetUnitId(bj_lastCreatedUnit)] = false
  156.                 call PauseUnit(bj_lastCreatedUnit, false)
  157.                 call SetUnitOwner(bj_lastCreatedUnit, owner, true)
  158.                 call SetUnitPosition(bj_lastCreatedUnit, x, y)
  159.                 call SetUnitFacing(bj_lastCreatedUnit, facing)
  160.                 set stack[i] = stack[i] - 1
  161.                 debug call Debug("Retrieving " + GetUnitName(bj_lastCreatedUnit) + " from stock")
  162.             endif
  163.             debug if bj_lastCreatedUnit == null then
  164.                 debug call Debug(ERROR_PREFIX + "Specified unit-type does not exist")
  165.             debug endif
  166.         else
  167.             debug call Debug(ERROR_PREFIX + "Attemp to retrieve a hero unit")
  168.             return null
  169.         endif
  170.         return bj_lastCreatedUnit
  171.     endfunction
  172.  
  173.     function GetRecycledUnitEx takes player owner, integer rawCode, real x, real y, real facing returns unit
  174.         if not IsHeroUnitId(rawCode) then
  175.             return GetRecycledUnit(owner, rawCode, x, y, facing)
  176.         endif
  177.         debug call Debug("Cannot retrieve a hero unit, creating new unit")
  178.         return CreateUnit(owner, rawCode, x, y, facing)
  179.     endfunction
  180.  
  181.     function RecycleUnit takes unit u returns boolean
  182.         local integer rawCode = GetUnitTypeId(u)
  183.         local integer uDex = GetUnitId(u)
  184.         local integer i
  185.         if not IsHeroUnitId(rawCode) and not stacked[uDex] and u != null then
  186.             set i = GetIndex(rawCode)
  187.             if not UnitAlive(u) and not ReviveUnit(u) then
  188.                 debug call Debug(ERROR_PREFIX + "Unable to recycle unit: Unable to revive dead unit")
  189.                 return false
  190.             endif
  191.             set stacked[uDex] = true
  192.             call PauseUnit(u, true)
  193.             call SetUnitOwner(u, OWNER, true)
  194.             call SetUnitX(u, unitCampX)
  195.             call SetUnitY(u, unitCampY)
  196.             call SetUnitFacing(u, 270)
  197.             call SetWidgetLife(u, GetUnitState(u, UNIT_STATE_MAX_LIFE))
  198.             call SetUnitState(u, UNIT_STATE_MANA, GetUnitState(u, UNIT_STATE_MAX_MANA))
  199.             //! runtextmacro optional UNIT_RECYCLER_RESET()
  200.             set stack[i] = stack[i] + 1
  201.             static if LIBRARY_Table then
  202.                 set S.hash[i].unit[stack[i]] = u
  203.             else
  204.                 call SaveUnitHandle(S.hash, i, stack[i], u)
  205.             endif
  206.             debug call Debug("Successfully recycled " + GetUnitName(u))
  207.             return true
  208.         debug else
  209.             debug if stacked[uDex] then
  210.                 debug call Debug(ERROR_PREFIX + "Attempt to recycle an already recycled unit")
  211.             debug elseif u == null then
  212.                 debug call Debug(ERROR_PREFIX + "Attempt to recycle a null unit")
  213.             debug else
  214.                 debug call Debug(ERROR_PREFIX + "Attempt to recycle a hero unit")
  215.             debug endif
  216.         endif
  217.         return false
  218.     endfunction
  219.  
  220.     function RecycleUnitEx takes unit u returns boolean
  221.         if not RecycleUnit(u) then
  222.             call RemoveUnit(u)
  223.             debug call Debug("Cannot recycle the specified unit, removing unit")
  224.             return false
  225.         endif
  226.         return true
  227.     endfunction
  228.  
  229.     //! textmacro DELAYED_RECYCLE_TYPE takes EX
  230.     private function RecycleTimer$EX$ takes nothing returns nothing
  231.         local timer t = GetExpiredTimer()
  232.         static if LIBRARY_TimerUtils then
  233.             call RecycleUnit$EX$(GetUnitById(GetTimerData(t)))
  234.             call ReleaseTimer(t)
  235.         else
  236.             local integer key = GetHandleId(t)
  237.             static if LIBRARY_Table then
  238.                 call RecycleUnit$EX$(S.hash[0].unit[key])
  239.                 call S.hash[0].remove(key)
  240.             else
  241.                 call RecycleUnit$EX$(LoadUnitHandle(S.hash, 0, key))
  242.                 call RemoveSavedHandle(S.hash, 0, key)
  243.             endif
  244.             call DestroyTimer(t)
  245.         endif
  246.         set t = null
  247.     endfunction
  248.  
  249.     function RecycleUnitDelayed$EX$ takes unit u, real delay returns nothing
  250.         static if LIBRARY_TimerUtils then
  251.             call TimerStart(NewTimerEx(GetUnitId(u)), delay, false, function RecycleTimer$EX$)
  252.         else
  253.             local timer t = CreateTimer()
  254.             static if LIBRARY_Table then
  255.                 set S.hash[0].unit[GetHandleId(t)] = u
  256.             else
  257.                 call SaveUnitHandle(S.hash, 0, GetHandleId(t), u)
  258.             endif
  259.             call TimerStart(t, delay, false, function RecycleTimer$EX$)
  260.             set t = null
  261.         endif
  262.     endfunction
  263.     //! endtextmacro
  264.  
  265.     //! runtextmacro DELAYED_RECYCLE_TYPE("")
  266.     //! runtextmacro DELAYED_RECYCLE_TYPE("Ex")
  267.  
  268.     function UnitAddToStock takes integer rawCode returns boolean
  269.         local unit u
  270.         local integer i
  271.         if not IsHeroUnitId(rawCode) then
  272.             set u = CreateUnit(OWNER, rawCode, unitCampX, unitCampY, 270)
  273.             if u != null then
  274.                 set i = GetIndex(rawCode)
  275.                 call SetUnitX(u, unitCampX)
  276.                 call SetUnitY(u, unitCampY)
  277.                 call PauseUnit(u, true)
  278.                 set stacked[GetUnitId(u)] = true
  279.                 set stack[i] = stack[i] + 1
  280.                 static if LIBRARY_Table then
  281.                     set S.hash[i].unit[stack[i]] = u
  282.                 else
  283.                     call SaveUnitHandle(S.hash, i, stack[i], u)
  284.                 endif
  285.                 debug call Debug("Adding " + GetUnitName(u) + " to stock")
  286.                 return true
  287.             debug else
  288.                 debug call Debug(ERROR_PREFIX + "Attemp to stock a null unit")
  289.             endif
  290.             set u = null
  291.         debug else
  292.             debug call Debug(ERROR_PREFIX + "Attemp to stock a hero unit")
  293.         endif
  294.         return false
  295.     endfunction
  296.  
  297.     static if AUTO_RECYCLE_DEAD then
  298.         private function OnDeath takes nothing returns nothing
  299.             local unit u = GetTriggerUnit()
  300.             if not IsUnitType(u, UNIT_TYPE_HERO) and not IsUnitType(u, UNIT_TYPE_STRUCTURE) then
  301.                 call RecycleUnitDelayed(u, DeathTime(u))
  302.             endif
  303.             set u = null
  304.         endfunction
  305.     endif
  306.  
  307.     private module Init
  308.  
  309.         static if LIBRARY_Table then
  310.             static TableArray hash
  311.             static Table table
  312.         else
  313.             static hashtable hash = InitHashtable()
  314.         endif
  315.  
  316.         private static method onInit takes nothing returns nothing
  317.             local rect bounds = GetWorldBounds()
  318.             static if AUTO_RECYCLE_DEAD then
  319.                 static if LIBRARY_RegisterPlayerUnitEvent then
  320.                     static if RPUE_VERSION_NEW then
  321.                         call RegisterAnyPlayerUnitEvent(EVENT_PLAYER_UNIT_DEATH, function OnDeath)
  322.                     else
  323.                         call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_DEATH, function OnDeath)
  324.                     endif
  325.                 else
  326.                     local trigger t = CreateTrigger()
  327.                     local code c = function OnDeath
  328.                     local integer i = 16
  329.                     loop
  330.                         set i = i - 1
  331.                         call TriggerRegisterPlayerUnitEvent(t, Player(i), EVENT_PLAYER_UNIT_DEATH, null)
  332.                         exitwhen i == 0
  333.                     endloop
  334.                     call TriggerAddCondition(t, Filter(c))
  335.                 endif
  336.             endif
  337.             static if LIBRARY_Table then
  338.                 set hash = TableArray[0x2000]
  339.                 set table = Table.create()
  340.             endif
  341.             // Hides recycled units at the top of the map beyond reach of the camera
  342.             set unitCampX = 0.00
  343.             set unitCampY = GetRectMaxY(bounds) + 1000.00
  344.             call RemoveRect(bounds)
  345.             set bounds = null
  346.         endmethod
  347.  
  348.     endmodule
  349.  
  350.     private struct S extends array
  351.         implement Init
  352.     endstruct
  353.  
  354.  
  355. endlibrary

Random Iteration


Created by AGD
September 04, 2016, 09:16:22 AM

This snippet is a replacement to the BJ function GetRandomSubGroup with the advantage of being able to directly enumerate random number of units in a group instead of creating another subgroup of an already existing group. Notice that this is not the same as GroupEnumUnitsInRangeCounted in which case the enumerated units are not random but is based on which units are picked first.


Code: jass
  1. library RandomIteration
  2.  
  3.     globals
  4.         private group tempGroup = CreateGroup()
  5.         private integer unitCount
  6.     endglobals
  7.  
  8.     private function EnumUnits takes nothing returns boolean
  9.         call GroupAddUnit(tempGroup, GetFilterUnit())
  10.         set unitCount = unitCount + 1
  11.         return false
  12.     endfunction
  13.  
  14.     function EnumRandomUnitsInRangeCounted takes group g, real x, real y, real radius, integer limit returns nothing
  15.         local real chance
  16.         local unit u
  17.         call GroupEnumUnitsInRange(g, x, y, radius, Filter(function EnumUnits))
  18.         set chance = I2R(limit)/I2R(unitCount)
  19.         loop
  20.             set u = FirstOfGroup(tempGroup)
  21.             exitwhen u == null or limit == 0
  22.             call GroupRemoveUnit(tempGroup, u)
  23.             if GetRandomReal(0, 1) <= chance then
  24.                 call GroupAddUnit(g, u)
  25.                 set limit = limit - 1
  26.             endif
  27.         endloop
  28.         set u = null
  29.     endfunction
  30.  
  31.     function EnumRandomUnitsInRectCounted takes group g, rect r, integer limit returns nothing
  32.         local real chance
  33.         local unit u
  34.         call GroupEnumUnitsInRect(g, r, Filter(function EnumUnits))
  35.         set chance = I2R(limit)/I2R(unitCount)
  36.         loop
  37.             set u = FirstOfGroup(tempGroup)
  38.             exitwhen u == null or limit == 0
  39.             call GroupRemoveUnit(tempGroup, u)
  40.             if GetRandomReal(0, 1) <= chance then
  41.                 call GroupAddUnit(g, u)
  42.                 set limit = limit - 1
  43.             endif
  44.         endloop
  45.         set u = null
  46.     endfunction
  47.  
  48. endlibrary

InvulnerabilityChecker


Created by AGD
September 04, 2016, 12:09:44 AM

A simple snippet used to check if a unit is invulnerable. False positives from mana shields are also accounted for.

Code: jass
  1. library InvulnerabilityChecker
  2.  
  3.     function CheckInvulnerability takes unit u returns boolean
  4.         local real origHP = GetWidgetLife(u)
  5.         local real origMP = GetUnitState(u, UNIT_STATE_MANA)
  6.         local boolean check
  7.         call UnitDamageTarget(u, u, 0.01, true, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_UNIVERSAL, null)
  8.         set check = GetWidgetLife(u) == origHP and GetUnitState(u, UNIT_STATE_LIFE) == origMP
  9.         call SetWidgetLife(u, origHP)
  10.         call SetUnitState(u, UNIT_STATE_MANA, origMP)
  11.         return check
  12.     endfunction
  13.  
  14. endlibrary

Child Boards

No New Posts General Jass Discussion

Talk about anything related to Jass, which does not fit into the other forums.

107 Posts17 Topics Last post by moyack
in Re: Patch 1.29 new nativ...
on June 11, 2018, 07:30:55 AM
No New Posts Coding Help

Ask questions and get answers on Jass Coding.

132 Posts10 Topics Last post by NaughtySmurf
in Re: Detecting Overdamage
on July 25, 2015, 05:57:00 AM
No New Posts Tips & Tricks

Found yourself a new fancy trick or looking to make your code more efficient? Then, this is the forum for you.

0 Posts0 Topics
No New Posts Jass Theory & Questions

Post more advanced theories and questions regarding Jass coding here.

75 Posts7 Topics Last post by moyack
in "Real"-numbers bug
on May 18, 2013, 08:40:42 AM
No New Posts Object Oriented Programming

Talk about Object Oriented Programming or OOP in Jass coding here.

19 Posts1 Topics Last post by Naoto Shirogane
in What's your favorite lan...
on September 03, 2013, 05:43:09 AM
No New Posts Jass Tutorials

Deposit your newly made Jass tutorials here. You may also view the other community-submitted tutorials.

52 Posts15 Topics Last post by WC3Mod
in Trackables (Mouse detect...
on July 09, 2017, 01:51:10 AM
  Subject / Started by Replies / Views Last post
0 Members and 1 Guest are viewing this board.
8 Replies
15829 Views
Last post August 03, 2012, 05:40:47 AM
by LembidiZ
5 Replies
2685 Views
Last post January 27, 2018, 09:03:45 AM
by Kam

* Random Spells & Systems

Illusion Editor v1.3 [vJass + GUI Support]

Views: 7307
Replies: 3
Posted by AGD
August 22, 2016, 02:06:44 AM

Dummy Caster 1.4

Views: 9162
Replies: 3
Posted by radamantus
June 27, 2013, 03:13:03 PM
Vivir aprendiendo.co - A place for learning stuff, in Spanish   Chaos Realm - The world of Game modders and wc3 addicts   Diplo, a gaming community   Power of Corruption, an altered melee featuring Naga and Demon. Play it now!!!   WC3JASS.com - The JASS Vault + vJASS and Zinc   Jetcraft - A Starcraft II mod   WormTastic Clan (wTc)