Warcraft III: MapsModelsSkinsIconsSpellsToolsTutorials
WC3 JASS: Code SnippetsJASS and vJASS Spells and SystemsJass Tutorials
Chat @Discord

Author Topic: [Snippet] GetUnitRegeneration  (Read 7171 times)

  • Awesome Global Code Moderator
  • Recognized User
  • Rookie - level 2
  • *
  • Posts: 83
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • WC3 Spells: 0
  • Reputation: 516
  • vJass Incarnate
    • View Profile
[Snippet] GetUnitRegeneration
« on: July 09, 2012, 01:49:21 PM »
The title explains it all.

Code: jass  [Select]
  1. /********************************************************
  2. *
  3. *   GetUnitRegeneration
  4. *   v2.0.0.0
  5. *   By Magtheridon96
  6. *
  7. *   - Gets unit regeneration values
  8. *
  9. *   Requirements:
  10. *   -------------
  11. *
  12. *       - UnitIndexer by Nestharus
  13. *           - hiveworkshop.com/forums/jass-resources-412/system-unit-indexer-172090/
  14. *       - DamageEvent by Nestharus
  15. *           - hiveworkshop.com/forums/jass-resources-412/snippet-damageevent-186829/
  16. *       - Heal by Magtheridon96
  17. *           - hiveworkshop.com/forums/jass-resources-412/system-heal-203662/
  18. *       - CTL by Nestharus
  19. *           - hiveworkshop.com/forums/jass-resources-412/snippet-constant-timer-loop-32-a-201381/
  20. *
  21. *       Optional:
  22. *       ---------
  23. *
  24. *       - DamageControl by PurgeandFire111
  25. *           - hiveworkshop.com/forums/jass-resources-412/system-damage-control-204672/
  26. *
  27. *   API:
  28. *   ----
  29. *
  30. *       - function IsUnitRegenerating takes unit u returns boolean
  31. *           - Tells whether a unit is regenerating or not.
  32. *       - function IsUnitRegeneratingById takes integer i returns boolean
  33. *           - Tells whether a unit is regenerating or not given the Id.
  34. *       - function GetUnitRegeneration takes unit u returns real
  35. *           - Gets the amount of regeneration for a given unit.
  36. *       - function GetUnitRegenerationById takes integer i returns real
  37. *           - Gets the amount of regeneration for a given unit given the Id.
  38. *       - function IsRegenerationPositive takes unit u returns boolean
  39. *           - Tells whether the regeneration is positive.
  40. *       - function IsRegenerationPositiveById takes integer i returns boolean
  41. *           - Tells whether the regeneration is positive given the Id.
  42. *
  43. ********************************************************/
  44. library GetUnitRegeneration requires UnitIndexer, DamageEvent, Heal, CTL, optional DamageControl
  45.    
  46.     private struct UnitRegen extends array
  47.        
  48.         static thistype array next
  49.         static thistype array prev
  50.        
  51.         static real array life
  52.         static real array max
  53.         static real array damaged
  54.         static real array amount
  55.        
  56.         static boolean array regenerating
  57.        
  58.         static method damage takes nothing returns nothing
  59.             static if LIBRARY_DamageControl then
  60.                 set damaged[DamageEvent.targetId] = damaged[DamageEvent.targetId] + DamageControl.result
  61.             else
  62.                 set damaged[DamageEvent.targetId] = damaged[DamageEvent.targetId] + DamageEvent.amount
  63.             endif
  64.         endmethod
  65.        
  66.         static method heal takes nothing returns nothing
  67.             set damaged[Heal.targetId] = damaged[Heal.targetId] - Heal.effective
  68.         endmethod
  69.        
  70.         static method index takes nothing returns nothing
  71.             // Add index to list
  72.             set next[GetIndexedUnitId()] = 0
  73.             set prev[GetIndexedUnitId()] = prev[0]
  74.             set next[prev[0]] = GetIndexedUnitId()
  75.             set prev[0] = GetIndexedUnitId()
  76.            
  77.             // Setting current life and max life
  78.             set life[GetIndexedUnitId()] = GetWidgetLife(GetIndexedUnit())
  79.             set max[GetIndexedUnitId()] = GetUnitState(GetIndexedUnit(), UNIT_STATE_MAX_LIFE)
  80.         endmethod
  81.        
  82.         static method deindex takes nothing returns nothing            
  83.             // Remove from list
  84.             set next[prev[GetIndexedUnitId()]] = next[GetIndexedUnitId()]
  85.             set prev[next[GetIndexedUnitId()]] = prev[GetIndexedUnitId()]
  86.            
  87.             // Resetting Data
  88.             set amount[GetIndexedUnitId()] = 0
  89.             set damaged[GetIndexedUnitId()] = 0
  90.            
  91.             set regenerating[GetIndexedUnitId()] = false
  92.         endmethod
  93.        
  94.         implement CT32
  95.        
  96.             local thistype this = next[0]
  97.             local real hp = 0
  98.             local real ml = 0
  99.            
  100.             loop
  101.                 exitwhen 0 == this
  102.                
  103.                 set ml = GetUnitState(GetUnitById(this), UNIT_STATE_MAX_LIFE)
  104.                
  105.                 if max[this] != ml then
  106.                     set life[this] = life[this] * (ml / max[this])
  107.                 endif
  108.                
  109.                 set hp = GetWidgetLife(GetUnitById(this)) + damaged[this]
  110.                
  111.                 if hp != life[this] then
  112.                     set amount[this] = (hp - life[this]) / 0.03125
  113.                     set regenerating[this] = true
  114.                     set life[this] = hp - damaged[this]
  115.                 else
  116.                     set regenerating[this] = false
  117.                     set amount[this] = 0
  118.                 endif
  119.                
  120.                 set damaged[this] = 0
  121.                 set max[this] = ml
  122.                
  123.                 set this = next[this]
  124.             endloop
  125.            
  126.         implement CT32End
  127.        
  128.         private static code indexCode
  129.         private static code deindexCode
  130.         private static code damageCode
  131.         private static code healCode
  132.        
  133.         private static method onInit takes nothing returns nothing
  134.        
  135.             set indexCode = function thistype.index
  136.             set deindexCode = function thistype.deindex
  137.             set damageCode = function thistype.damage
  138.             set healCode = function thistype.heal
  139.            
  140.             static if LIBRARY_DamageControl then
  141.                 call DamageControl.FINAL.register(Filter(damageCode))
  142.             else
  143.                 call DamageEvent.ANY.register(Filter(damageCode))
  144.             endif
  145.            
  146.             call RegisterAnyHealEvent(healCode)
  147.             call UnitIndexer.INDEX.register(Filter(indexCode))
  148.             call UnitIndexer.DEINDEX.register(Filter(deindexCode))
  149.         endmethod
  150.        
  151.     endstruct
  152.    
  153.     function IsUnitRegenerating takes unit u returns boolean
  154.         return UnitRegen.regenerating[GetUnitUserData(u)]
  155.     endfunction
  156.    
  157.     function IsUnitRegeneratingById takes integer i returns boolean
  158.         return UnitRegen.regenerating[i]
  159.     endfunction
  160.    
  161.     function GetUnitRegeneration takes unit u returns real
  162.         return UnitRegen.amount[GetUnitUserData(u)]
  163.     endfunction
  164.    
  165.     function GetUnitRegenerationById takes integer i returns real
  166.         return UnitRegen.amount[i]
  167.     endfunction
  168.    
  169.     function IsRegenerationPositive takes unit u returns boolean
  170.         return UnitRegen.amount[GetUnitUserData(u)] >= 0
  171.     endfunction
  172.    
  173.     function IsRegenerationPositiveById takes integer i returns boolean
  174.         return UnitRegen.amount[i] >= 0
  175.     endfunction
  176.    
  177. endlibrary
  178.  
  179. library IsUnitRegenerating requires GetUnitRegeneration
  180. endlibrary

Feel free to comment..
« Last Edit: July 10, 2012, 09:23:14 AM by Magtheridon96 »

  • Your Awesome Site Director
  • Recognized User
  • Starter - level 1
  • *
  • Posts: 382
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • WC3 Spells: 0
  • Reputation: 677
    • View Profile
Re: [Snippet] GetUnitRegeneration
« Reply #1 on: July 10, 2012, 02:38:31 AM »
This one looks really useful, I can think of some stuffs that you can do here, but it will really nice if you can put a list of all the things that you can do with this script. ;)
Chronicles of Darkness
by: SonofJay

A BlizzMod Hosted Project

They can hate, let them hate, make them hate.

  • Awesome Global Code Moderator
  • Recognized User
  • Rookie - level 2
  • *
  • Posts: 83
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • WC3 Spells: 0
  • Reputation: 516
  • vJass Incarnate
    • View Profile
Re: [Snippet] GetUnitRegeneration
« Reply #2 on: July 10, 2012, 09:22:22 AM »
Thanks :D
Updated the code. I found a useless line in the deindex method.

 

[Snippet] Group Units in line, with distance and optional spread-value

Started by rvonsonsnadtzBoard Codes & Snippets

Replies: 5
Views: 11935
Last post March 13, 2013, 06:52:16 PM
by rvonsonsnadtz
[Snippet] Single Player Detector

Started by moyackBoard Codes & Snippets

Replies: 12
Views: 21563
Last post March 13, 2013, 12:48:49 PM
by Magtheridon96
[Snippet] Simple Unit Indexer

Started by moyackBoard Codes & Snippets

Replies: 17
Views: 27029
Last post September 30, 2013, 08:07:19 AM
by moyack
[Snippet] Disable Transmission Skip

Started by PurgeandfireBoard Codes & Snippets

Replies: 2
Views: 9005
Last post July 02, 2013, 01:34:55 PM
by Purgeandfire
[Snippet] Timed Effects

Started by moyackBoard Codes & Snippets

Replies: 0
Views: 7770
Last post January 09, 2012, 07:03:32 PM
by moyack
Blizzard Modding Information Center Starcraft II Modding Information Center Wacraft III Modding Information Center WC3JASS.com - The JASS Vault Chronicles of Darkness - A Warcraft III mod Jetcraft - A Starcraft II mod Troll Smash - A Warcraft III Arena
  Mod DB - Change the Game Power of Corruption - A Warcraft III altered melee map Chaos Realm - The world of Game modders and wc3 addicts Follow us on Facebook!!