Warcraft 3 documentation
vJASS & Zinc Documentation
For the latest documentation about how it works vJASS and Zinc language layers for Warcraft III, please follow these links:
Jasshelper documentation - Zinc documentation - WC3 Optimizer documentation

UnitRecycler No New Posts Codes & Snippets

Started by
AGD

0 Members and 1 Guest are viewing this topic.

Rating

Average Score - 5 / 5

« Created: June 30, 2017, 12:03:32 PM by moyack »

UnitRecycler
on: November 02, 2016, 03:36:36 AM
Category: Units
Language: vJASS

Related Topics or Resources



by

by

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.             //! runtextmacro optional UNIT_RECYCLER_RESET()
  199.             set stack[i] = stack[i] + 1
  200.             static if LIBRARY_Table then
  201.                 set S.hash[i].unit[stack[i]] = u
  202.             else
  203.                 call SaveUnitHandle(S.hash, i, stack[i], u)
  204.             endif
  205.             debug call Debug("Successfully recycled " + GetUnitName(u))
  206.             return true
  207.         debug else
  208.             debug if stacked[uDex] then
  209.                 debug call Debug(ERROR_PREFIX + "Attempt to recycle an already recycled unit")
  210.             debug elseif u == null then
  211.                 debug call Debug(ERROR_PREFIX + "Attempt to recycle a null unit")
  212.             debug else
  213.                 debug call Debug(ERROR_PREFIX + "Attempt to recycle a hero unit")
  214.             debug endif
  215.         endif
  216.         return false
  217.     endfunction
  218.  
  219.     function RecycleUnitEx takes unit u returns boolean
  220.         if not RecycleUnit(u) then
  221.             call RemoveUnit(u)
  222.             debug call Debug("Cannot recycle the specified unit, removing unit")
  223.             return false
  224.         endif
  225.         return true
  226.     endfunction
  227.  
  228.     //! textmacro DELAYED_RECYCLE_TYPE takes EX
  229.     private function RecycleTimer$EX$ takes nothing returns nothing
  230.         local timer t = GetExpiredTimer()
  231.         static if LIBRARY_TimerUtils then
  232.             call RecycleUnit$EX$(GetUnitById(GetTimerData(t)))
  233.             call ReleaseTimer(t)
  234.         else
  235.             local integer key = GetHandleId(t)
  236.             static if LIBRARY_Table then
  237.                 call RecycleUnit$EX$(S.hash[0].unit[key])
  238.                 call S.hash[0].remove(key)
  239.             else
  240.                 call RecycleUnit$EX$(LoadUnitHandle(S.hash, 0, key))
  241.                 call RemoveSavedHandle(S.hash, 0, key)
  242.             endif
  243.             call DestroyTimer(t)
  244.         endif
  245.         set t = null
  246.     endfunction
  247.  
  248.     function RecycleUnitDelayed$EX$ takes unit u, real delay returns nothing
  249.         static if LIBRARY_TimerUtils then
  250.             call TimerStart(NewTimerEx(GetUnitId(u)), delay, false, function RecycleTimer$EX$)
  251.         else
  252.             local timer t = CreateTimer()
  253.             static if LIBRARY_Table then
  254.                 set S.hash[0].unit[GetHandleId(t)] = u
  255.             else
  256.                 call SaveUnitHandle(S.hash, 0, GetHandleId(t), u)
  257.             endif
  258.             call TimerStart(t, delay, false, function RecycleTimer$EX$)
  259.             set t = null
  260.         endif
  261.     endfunction
  262.     //! endtextmacro
  263.  
  264.     //! runtextmacro DELAYED_RECYCLE_TYPE("")
  265.     //! runtextmacro DELAYED_RECYCLE_TYPE("Ex")
  266.  
  267.     function UnitAddToStock takes integer rawCode returns boolean
  268.         local unit u
  269.         local integer i
  270.         if not IsHeroUnitId(rawCode) then
  271.             set u = CreateUnit(OWNER, rawCode, unitCampX, unitCampY, 270)
  272.             if u != null then
  273.                 set i = GetIndex(rawCode)
  274.                 call SetUnitX(u, unitCampX)
  275.                 call SetUnitY(u, unitCampY)
  276.                 call PauseUnit(u, true)
  277.                 set stacked[GetUnitId(u)] = true
  278.                 set stack[i] = stack[i] + 1
  279.                 static if LIBRARY_Table then
  280.                     set S.hash[i].unit[stack[i]] = u
  281.                 else
  282.                     call SaveUnitHandle(S.hash, i, stack[i], u)
  283.                 endif
  284.                 debug call Debug("Adding " + GetUnitName(u) + " to stock")
  285.                 return true
  286.             debug else
  287.                 debug call Debug(ERROR_PREFIX + "Attemp to stock a null unit")
  288.             endif
  289.             set u = null
  290.         debug else
  291.             debug call Debug(ERROR_PREFIX + "Attemp to stock a hero unit")
  292.         endif
  293.         return false
  294.     endfunction
  295.  
  296.     static if AUTO_RECYCLE_DEAD then
  297.         private function OnDeath takes nothing returns nothing
  298.             local unit u = GetTriggerUnit()
  299.             if not IsUnitType(u, UNIT_TYPE_HERO) and not IsUnitType(u, UNIT_TYPE_STRUCTURE) then
  300.                 call RecycleUnitDelayed(u, DeathTime(u))
  301.             endif
  302.             set u = null
  303.         endfunction
  304.     endif
  305.  
  306.     private module Init
  307.  
  308.         static if LIBRARY_Table then
  309.             static TableArray hash
  310.             static Table table
  311.         else
  312.             static hashtable hash = InitHashtable()
  313.         endif
  314.  
  315.         private static method onInit takes nothing returns nothing
  316.             local rect bounds = GetWorldBounds()
  317.             static if AUTO_RECYCLE_DEAD then
  318.                 static if LIBRARY_RegisterPlayerUnitEvent then
  319.                     static if RPUE_VERSION_NEW then
  320.                         call RegisterAnyPlayerUnitEvent(EVENT_PLAYER_UNIT_DEATH, function OnDeath)
  321.                     else
  322.                         call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_DEATH, function OnDeath)
  323.                     endif
  324.                 else
  325.                     local trigger t = CreateTrigger()
  326.                     local code c = function OnDeath
  327.                     local integer i = 16
  328.                     loop
  329.                         set i = i - 1
  330.                         call TriggerRegisterPlayerUnitEvent(t, Player(i), EVENT_PLAYER_UNIT_DEATH, null)
  331.                         exitwhen i == 0
  332.                     endloop
  333.                     call TriggerAddCondition(t, Filter(c))
  334.                 endif
  335.             endif
  336.             static if LIBRARY_Table then
  337.                 set hash = TableArray[0x2000]
  338.                 set table = Table.create()
  339.             endif
  340.             // Hides recycled units at the top of the map beyond reach of the camera
  341.             set unitCampX = 0.00
  342.             set unitCampY = GetRectMaxY(bounds) + 1000.00
  343.             call RemoveRect(bounds)
  344.             set bounds = null
  345.         endmethod
  346.  
  347.     endmodule
  348.  
  349.     private struct S extends array
  350.         implement Init
  351.     endstruct
  352.  
  353.  
  354. endlibrary
« Last Edit: December 19, 2017, 11:32:05 PM by moyack »



Re: UnitRecycler
Reply #1 on: November 02, 2016, 10:51:45 AM

Nice!!! a unit recycler system :)

This remembers an old one I did with Damage detection integrated. Here's the link: https://wc3modding.info/4529/system-unit-recycler-simple-damage-detection-system/

One thing to know: is this system capable to manage buildings and summoned units?


Re: UnitRecycler
Reply #2 on: November 05, 2016, 09:25:50 AM

Oh, nice to know that you already have conceived the concept a long time ago =D Though the two resources have a bit different approach.
Btw, mine does not account for summoned units and structures yet.



Re: UnitRecycler
Reply #3 on: June 28, 2018, 03:46:18 AM

Great, really need a covered space for the equipment other



 

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)