[Log in / Register]
| News | Chat | Pastebin | Donations | Tutorials | Rules | Forums |
| Maps | Skins | Icons | Models | Spells | Tools | Jass | Packs | Hosted Projects | Starcraft II Modding | Starcraft II Resources | Galaxy Wiki |
(Keeps Hive Alive)
Go Back   The Hive Workshop > Spells


Reply
 
Thread Tools
The Hive Workshop Spells:
Respawn System - v1.0b
by Mr_Bean
Images
Highslide JS
Details
Uploaded:11:44, 22nd Jun 2012
Last Updated:09:14, 17th Jul 2012
Keywords:respawn, system, respawn system, creep, mr_bean, peacehagen
Type:System
Category:vJASS, GUI / Triggers

Respawn System - v1.0b
By Mr_Bean

Introduction
This system was originally requested by Peacehagen for his map Rise of the Corruptor ORPG.
He recommended that I upload it because it would help people, which I hope it does!
I added some new features to it and made it as user-friendly as possible.

Features
- Respawn all dead creeps every X seconds, or individual creeps X seconds after they die.
- Specify which players to respawn creeps for.
- Functions which allow the following:
  • Respawn all dead creeps immediately.
  • Respawn all dead creeps owned by a specific player immediately.
  • Pause and resume the system.
  • Reset and restart the system.
  • Count how many creeps there are in total, alive and dead.
- Can create an effect on respawning units.
- Can choose to respawn heroes or not.
- Written in vJASS, can be used in GUI.

Requirements
- JASS NewGen Pack [LINK]
- Latest JASSHelper [LINK]
- TimerUtils by Vexorian (included in demo map)

Available Functions
JASS Version
Jass:
function RespawnAllCreeps takes nothing returns nothing
function RespawnAllCreepsForPlayer takes player whichPlayer returns nothing
function PauseRespawnSystem takes integer howLong returns nothing
function ResumeRespawnSystem takes nothing returns nothing
function ResetRespawnSystem takes nothing returns nothing
function RestartRespawnSystem takes nothing returns nothing
function GetAliveCreeps takes nothing returns integer
function GetDeadCreeps takes nothing returns integer
function GetTotalCreeps takes nothing returns integer

GUI Version
Custom script: call RespawnAllCreeps()
Custom script: call RespawnAllCreepsForPlayer(player whichPlayer)
Custom script: call PauseRespawnSystem(integer howLong)
Custom script: call ResumeRespawnSystem()
Custom script: call ResetRespawnSystem()
Custom script: call RestartRespawnSystem()
Custom script: call GetAliveCreeps()
Custom script: call GetDeadCreeps()
Custom script: call GetTotalCreeps()

Code
vJASS Code
Jass:
library RespawnSystem initializer OnInit requires TimerUtils

    //**************************************************\\
    //*** START OF CONFIGURABLES                     ***\\
    //**************************************************\\
    
    globals
    
        // Delay between unit dying and respawning:
        private constant real    RESPAWN_DELAY      = 30.0
        // Respawn all dead units at once? If set to false, units will respawn individually.
        private constant boolean RESPAWN_TOGETHER   = true
        // Show a countdown window to all players:
        private constant boolean SHOW_TIMER_WINDOW  = false
        // Countdown window text (only applicable if SHOW_TIMER_WINDOW = true):
        private constant string  TIMER_WINDOW_TEXT  = "Creeps respawn in:"
        // Respawn heroes:
        private constant boolean RESPAWN_HEROES     = false
        // Effect created on respawning units (set to "" to have no effect):
        private constant string  RESPAWN_EFFECT     = "Abilities\\Spells\\Undead\\RaiseSkeletonWarrior\\RaiseSkeleton.mdl"
        // Attachment point of effect on unit:
        private constant string  EFFECT_ATTACHMENT  = "origin"
        
    endglobals
    
    private function CheckOwner takes nothing returns boolean
        local integer playerId = GetPlayerId(GetOwningPlayer(GetFilterUnit()))
        if /*
        
        //------------------------------------------------------\\
        // Add the players that the system respawns creeps for: \\
        // Remember to subtract 1 from GUI player indexes!      \\
        */  playerId == 10 or /*    // Player 11 in GUI.        \*
        */  playerId == 11 or /*    // Player 12 in GUI.        \*
        */  playerId == 12    /*    // Neutral Hostile in GUI.  \*
        //------------------------------------------------------\\
        
        */ then
            return true
        endif
        return false
    endfunction
    
    //**************************************************\\
    //*** END OF CONFIGURABLES                       ***\\
    //**************************************************\\
    
    globals
        private group       deadCreeps      = CreateGroup()
        private group       creeps          = CreateGroup()
        private group       enumG           = CreateGroup()
        private hashtable   ht              = InitHashtable()
        private timer       respawnTimer
        private timerdialog timerWindow
        private integer     tempInt         = 0
    endglobals
    
    private function Increment takes nothing returns nothing
        set tempInt = tempInt + 1
    endfunction
    
    private function EnumerateGroup takes group g returns integer
        set tempInt = 0
        call ForGroup(g, function Increment)
        return tempInt
    endfunction
    
    //**************************************************\\
    //*** GetTotalCreeps                             ***\\
    //**************************************************\\
    
    function GetTotalCreeps takes nothing returns integer
        return EnumerateGroup(creeps) + EnumerateGroup(deadCreeps)
    endfunction
    
    //**************************************************\\
    //*** GetAliveCreeps                             ***\\
    //**************************************************\\
    
    function GetDeadCreeps takes nothing returns integer
        return EnumerateGroup(deadCreeps)
    endfunction
    
    //**************************************************\\
    //*** GetDeadCreeps                              ***\\
    //**************************************************\\
    
    function GetAliveCreeps takes nothing returns integer
        return EnumerateGroup(creeps)
    endfunction
    
    //**************************************************\\
    
    private function RespawnCreepX takes integer handleId returns nothing
        local real x = LoadReal(ht, handleId, StringHash("x"))
        local real y = LoadReal(ht, handleId, StringHash("y"))
        local real angle = LoadReal(ht, handleId, StringHash("facing"))
        local integer owner = LoadInteger(ht, handleId, StringHash("owner"))
        local integer unitType = LoadInteger(ht, handleId, StringHash("type"))
        local unit creep
        
        set creep = CreateUnit(Player(owner), unitType, x, y, angle)
        call FlushChildHashtable(ht, handleId)
        call DestroyEffect(AddSpecialEffectTarget(RESPAWN_EFFECT, creep, EFFECT_ATTACHMENT))
        set handleId = GetHandleId(creep)
        call SaveReal(ht, handleId, StringHash("x"), GetUnitX(creep))
        call SaveReal(ht, handleId, StringHash("y"), GetUnitY(creep))
        call SaveReal(ht, handleId, StringHash("facing"), GetUnitFacing(creep))
        call SaveInteger(ht, handleId, StringHash("owner"), GetPlayerId(GetOwningPlayer(creep)))
        call SaveInteger(ht, handleId, StringHash("type"), GetUnitTypeId(creep))
        
        call GroupRemoveUnit(deadCreeps, creep)
        call GroupAddUnit(creeps, creep)
        
        set creep = null
    endfunction
    
    //**************************************************\\
    //*** ResumeRespawnSystem                        ***\\
    //**************************************************\\
    
    function ResumeRespawnSystem takes nothing returns nothing
        static if RESPAWN_TOGETHER then
            call ResumeTimer(respawnTimer)
        else
            call BJDebugMsg("|cffff0000Respawn System|r: Cannot use ResumeRespawnSystem when RESPAWN_TOGETHER = false.")
        endif
    endfunction
    
    //**************************************************\\
    //*** PauseRespawnSystem                         ***\\
    //**************************************************\\
    
    function PauseRespawnSystem takes real duration returns nothing
        static if RESPAWN_TOGETHER then
            if duration < 0 then
                call PauseTimer(respawnTimer)
            elseif duration > 0 then
                call PauseTimer(respawnTimer)
                call TimerStart(NewTimer(), duration, false, function ResumeRespawnSystem)
            endif
        else
            call BJDebugMsg("|cffff0000Respawn System|r: Cannot use PauseRespawnSystem when RESPAWN_TOGETHER = false.")
        endif
        
    endfunction
    
    //**************************************************\\
    //*** RespawnAllCreepsForPlayer                  ***\\
    //**************************************************\\
    
    function RespawnAllCreepsForPlayer takes player whichPlayer returns nothing
        local unit temp
        
        set enumG = deadCreeps
        
        loop
            set temp = FirstOfGroup(enumG)
            exitwhen temp == null
            
            if GetOwningPlayer(temp) == whichPlayer then
                call RespawnCreepX(GetHandleId(temp))
            endif
        
            call GroupRemoveUnit(enumG, temp)
        
        endloop
        
    endfunction
    
    private function GroupEnumeration takes nothing returns nothing
        call RespawnCreepX(GetHandleId(GetEnumUnit()))
    endfunction
    
    //**************************************************\\
    //*** RespawnAllCreeps                           ***\\
    //**************************************************\\
    
    function RespawnAllCreeps takes nothing returns nothing
        call ForGroup(deadCreeps, function GroupEnumeration)
    endfunction
    
    //**************************************************\\
    //*** ResetRespawnSystem                         ***\\
    //**************************************************\\
    
    function ResetRespawnSystem takes nothing returns nothing
    
        static if RESPAWN_TOGETHER then
            call TimerStart(respawnTimer, RESPAWN_DELAY, true, function RespawnAllCreeps)
            call PauseTimer(respawnTimer)
        else
            call BJDebugMsg("|cffff0000Respawn System|r: Cannot use ResetRespawnSystem when RESPAWN_TOGETHER = false.")
        endif
        
    endfunction
    
    //**************************************************\\
    //*** RestartRespawnSystem                       ***\\
    //**************************************************\\
    
    function RestartRespawnSystem takes nothing returns nothing
        static if RESPAWN_TOGETHER then
            call TimerStart(respawnTimer, RESPAWN_DELAY, true, function RespawnAllCreeps)
        else
            call BJDebugMsg("|cffff0000Respawn System|r: Cannot use RestartRespawnSystem when RESPAWN_TOGETHER = false.")
        endif
    endfunction

    //**************************************************\\
    
    private function TimerExpiration takes nothing returns nothing
        call RespawnCreepX(GetTimerData(GetExpiredTimer()))
        call ReleaseTimer(GetExpiredTimer())
    endfunction
    
    private function CreepDeath takes nothing returns boolean
        local unit u = GetTriggerUnit()
        
        if IsUnitInGroup(u, creeps) then
            call GroupRemoveUnit(creeps, u)
            call GroupAddUnit(deadCreeps, u)
            
            static if not RESPAWN_TOGETHER then
                call TimerStart(NewTimerEx(GetHandleId(u)), RESPAWN_DELAY, false, function TimerExpiration)
            endif
        endif
        
        set u = null
        return false
    endfunction
    
    private function GroupCreeps takes nothing returns nothing
        local unit temp
        local integer handleId
        
        call GroupEnumUnitsInRect(enumG, bj_mapInitialPlayableArea, Condition(function CheckOwner))
        
        loop
            set temp = FirstOfGroup(enumG)
            exitwhen temp == null
            
            if not IsUnitType(temp, UNIT_TYPE_DEAD) then
                set handleId = GetHandleId(temp)
                call SaveReal(ht, handleId, StringHash("x"), GetUnitX(temp))
                call SaveReal(ht, handleId, StringHash("y"), GetUnitY(temp))
                call SaveReal(ht, handleId, StringHash("facing"), GetUnitFacing(temp))
                call SaveInteger(ht, handleId, StringHash("owner"), GetPlayerId(GetOwningPlayer(temp)))
                call SaveInteger(ht, handleId, StringHash("type"), GetUnitTypeId(temp))
                call GroupAddUnit(creeps, temp)
            endif
            
            call GroupRemoveUnit(enumG, temp)
            
        endloop
        
    endfunction
    
    private function CreateDialog takes nothing returns nothing
        set timerWindow = CreateTimerDialog(respawnTimer)
        call TimerDialogSetTitle(timerWindow, TIMER_WINDOW_TEXT)
        call TimerDialogDisplay(timerWindow, true)
    endfunction
    
    private function OnInit takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerAddCondition(t, Condition(function CreepDeath))
        call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_DEATH)
        set t = null
        
        call GroupCreeps()
        
        static if RESPAWN_TOGETHER then
        
            set respawnTimer = CreateTimer()
            call TimerStart(respawnTimer, RESPAWN_DELAY, true, function RespawnAllCreeps)
            
            static if SHOW_TIMER_WINDOW then
                call TimerStart(NewTimer(), 0.01, false, function CreateDialog)
            endif
            
        endif
        
    endfunction
    
endlibrary

Changelogs
v1.0b (33 June 2012)
- Improved creep counting as per KnnO's suggestion.
- Added map name, description and author for demo map.

v1.0 (22 June 2012)
- Initial release.

Credits
- Peacehagen for requesting the system and encouraging me to upload it.
- Vexorian for TimerUtils.

Contact
If you find any bugs in the system or if something is unclear in the documentation, please let me know!
- Email
- Private Message
Rating - 5.00 (1 vote)
(Hover and click)
Moderator Comments
Not Rated
12:09, 17th Jul 2012
Magtheridon96:

Comments
  • Instead of using the ForGroup function to figure out the number of units, you can keep track of the number of units dead and alive by yourself using a couple of integers.
  • ResumeTimer -> TimerStart because it's faster :P
  • Instead of using those StringHash functions, you can use constant integers, or as the others suggested, keys.
  • CheckOwner can be shortened to 2 lines. You just need to return the conditions of the if statement. ( return playerId == 10 or playerId == 11 or playerId == 12 )

This spell is awaiting update.


Download Respawn System - v1.0b.w3x
(31.47 KB, 179 Downloads)

Old 06-22-2012, 12:18 PM   #2 (permalink)
Registered User Almia
Oh Data Structures <3
 
Almia's Avatar
 
Join Date: Apr 2012
Posts: 2,989
Almia is a name known to all (639)Almia is a name known to all (639)
can you post the triggers sir?
Almia is offline   Reply With Quote
Old 06-22-2012, 12:23 PM   #3 (permalink)
Registered User Mr_Bean
Where's Teddy?
 
Mr_Bean's Avatar
 
Join Date: Feb 2011
Posts: 1,593
Mr_Bean is a jewel in the rough (238)Mr_Bean is a jewel in the rough (238)Mr_Bean is a jewel in the rough (238)
Techtree Contest #7 - Winner: The Ancients and The Old One: The incarnation of the World Tree as a Super Unit commences the tides of battle. Nature has found its most precious avenger! A race by Lordkoon & Mr_Bean987. 
Done. Code uploaded.
__________________
Mr_Bean's Spell Workshop
- Looking for people to help with GUI & vJASS requests.
- Need help importing your request? Try this guide I wrote!
Mr_Bean is offline   Reply With Quote
Old 06-22-2012, 12:25 PM   #4 (permalink)
Registered User Almia
Oh Data Structures <3
 
Almia's Avatar
 
Join Date: Apr 2012
Posts: 2,989
Almia is a name known to all (639)Almia is a name known to all (639)
i thought you put the gui version :D
Almia is offline   Reply With Quote
Old 06-22-2012, 12:31 PM   #5 (permalink)
Registered User Mr_Bean
Where's Teddy?
 
Mr_Bean's Avatar
 
Join Date: Feb 2011
Posts: 1,593
Mr_Bean is a jewel in the rough (238)Mr_Bean is a jewel in the rough (238)Mr_Bean is a jewel in the rough (238)
Techtree Contest #7 - Winner: The Ancients and The Old One: The incarnation of the World Tree as a Super Unit commences the tides of battle. Nature has found its most precious avenger! A race by Lordkoon & Mr_Bean987. 
It's written in vJASS, but you can obviously use GUI actions (custom scripts).
__________________
Mr_Bean's Spell Workshop
- Looking for people to help with GUI & vJASS requests.
- Need help importing your request? Try this guide I wrote!
Mr_Bean is offline   Reply With Quote
Old 06-22-2012, 12:46 PM   #6 (permalink)
Registered User Almia
Oh Data Structures <3
 
Almia's Avatar
 
Join Date: Apr 2012
Posts: 2,989
Almia is a name known to all (639)Almia is a name known to all (639)
ohh okay ill test it tomorrow
Almia is offline   Reply With Quote
Old 06-22-2012, 12:47 PM   #7 (permalink)
Registered User Peacehagen
Peace to the People!
 
Peacehagen's Avatar
 
Join Date: Aug 2009
Posts: 478
Peacehagen has little to show at this moment (30)Peacehagen has little to show at this moment (30)Peacehagen has little to show at this moment (30)Peacehagen has little to show at this moment (30)
I recommend this system, very easy to use (and i have 0 jass knowledge) and works perfectly. All other i have tried have had an issue that some units did not respawn but this solved it :)

5/5
Peacehagen is offline   Reply With Quote
Old 06-22-2012, 05:02 PM   #8 (permalink)
Registered User KnnO
I like everything
 
KnnO's Avatar
 
Join Date: Sep 2011
Posts: 167
KnnO has little to show at this moment (54)
This function moves group units to <enum> then moves them back to <g> function EnumerateGroup which is nonsense, you can use:
Jass:
private function Inc takes nothing returns nothing
      set /*a temp private global integer*/ count = count + 1
endfunction

function EnumerateGroup takes group g returns integer
      call ForGroup(g,function Inc)
      retun count
endfunction
But I am not sure If you are using it that way because it is faster or something...are you? or because you don't want an extra function and an extra variable? IDK

Everything else is fine I guess
__________________
و ت ف
KnnO is offline   Reply With Quote
Old 06-22-2012, 07:41 PM   #9 (permalink)
Registered User Mr_Bean
Where's Teddy?
 
Mr_Bean's Avatar
 
Join Date: Feb 2011
Posts: 1,593
Mr_Bean is a jewel in the rough (238)Mr_Bean is a jewel in the rough (238)Mr_Bean is a jewel in the rough (238)
Techtree Contest #7 - Winner: The Ancients and The Old One: The incarnation of the World Tree as a Super Unit commences the tides of battle. Nature has found its most precious avenger! A race by Lordkoon & Mr_Bean987. 
Update:
Uploaded version 1.0b:
- Fixed creep counting as per KnnO's suggestion.
- Added map name, description and author for the demp map.

@KnnO: Thanks man! I guess I'm just too used to using FirstOfGroup loops in spells. Much appreciated.
__________________
Mr_Bean's Spell Workshop
- Looking for people to help with GUI & vJASS requests.
- Need help importing your request? Try this guide I wrote!
Mr_Bean is offline   Reply With Quote
Old 06-22-2012, 08:24 PM   #10 (permalink)
Registered User KnnO
I like everything
 
KnnO's Avatar
 
Join Date: Sep 2011
Posts: 167
KnnO has little to show at this moment (54)
Well I saw your user title so I think you are better than me with coding :P

No problem Mr_Bean987, I am glad I could help!
__________________
و ت ف
KnnO is offline   Reply With Quote
Old 06-28-2012, 04:48 AM   #11 (permalink)
Registered User Diehard@Azeroth
User
 
Diehard@Azeroth's Avatar
 
Join Date: Sep 2007
Posts: 565
Diehard@Azeroth will become famous soon enough (91)Diehard@Azeroth will become famous soon enough (91)Diehard@Azeroth will become famous soon enough (91)
@Mr_Bean987, First of all nice API you got there :) Alright lets get down to business. Instead of using hashtables, i suggest you use a struct and store the handle id of the unit on its respective timer when it dies. If you still insist on using hashtables, then i suggest you use the variable type
Jass:
key
and make it constant; this is rather than using stringhash.

Hope i helped, peace
Diehard@Azeroth is offline   Reply With Quote
Old 06-28-2012, 06:01 AM   #12 (permalink)
Registered User -Kobas-
★ SotP Project Leader ★
 
-Kobas-'s Avatar
 
Join Date: Jan 2010
Posts: 5,869
-Kobas- has a reputation beyond repute (2066)-Kobas- has a reputation beyond repute (2066)-Kobas- has a reputation beyond repute (2066)-Kobas- has a reputation beyond repute (2066)-Kobas- has a reputation beyond repute (2066)
Hero Contest #6 - Winner: Alora, the Priestess of Moonlight was found to be the most succesful defender against the Armies of the Burning Legion! A hero by -Kobas-. Former Staff Member: This user used to be on the Hive Workshop staff. Hero Contest #5 - Winner: Stormbringer was -Kobas-'s entry, who incorporated lightnings as the power of future weaponry. Despite the instability of lightnings, this entry was stable enough to be approved by judges and public. A hero by -Kobas-. Mini-Game Mapping Contest #4: -Kobas- has won the fourth edition of The Hive Workshop's Mini-Game Mapping Contest with his entry: "Hunted". Check out the Arena if you want to participate on competitions as well! 
Yeah key will generate random unique number and using is as constant will increase "security" even more.

StringHash is something we do in GUI, use integers in jass, it's faster.
__________________

New version is out!
Shadows of the Past v0.9.3.0
Monday, March 25 2013 22:20
Check it out -> ★ Shadows of the Past ★
------------------------------------------------------
We got our Writer, please, welcome Noxx-
-Kobas- is offline   Reply With Quote
Old 06-28-2012, 10:11 AM   #13 (permalink)
Registered User Mr_Bean
Where's Teddy?
 
Mr_Bean's Avatar
 
Join Date: Feb 2011
Posts: 1,593
Mr_Bean is a jewel in the rough (238)Mr_Bean is a jewel in the rough (238)Mr_Bean is a jewel in the rough (238)
Techtree Contest #7 - Winner: The Ancients and The Old One: The incarnation of the World Tree as a Super Unit commences the tides of battle. Nature has found its most precious avenger! A race by Lordkoon & Mr_Bean987. 
Can you please give me an example of how I should use key ...

If I were to use structs, would I create a new instance for each creep? Is this more efficient than hashtables?
__________________
Mr_Bean's Spell Workshop
- Looking for people to help with GUI & vJASS requests.
- Need help importing your request? Try this guide I wrote!
Mr_Bean is offline   Reply With Quote
Old 06-28-2012, 04:00 PM   #14 (permalink)
Registered User -Kobas-
★ SotP Project Leader ★
 
-Kobas-'s Avatar
 
Join Date: Jan 2010
Posts: 5,869
-Kobas- has a reputation beyond repute (2066)-Kobas- has a reputation beyond repute (2066)-Kobas- has a reputation beyond repute (2066)-Kobas- has a reputation beyond repute (2066)-Kobas- has a reputation beyond repute (2066)
Hero Contest #6 - Winner: Alora, the Priestess of Moonlight was found to be the most succesful defender against the Armies of the Burning Legion! A hero by -Kobas-. Former Staff Member: This user used to be on the Hive Workshop staff. Hero Contest #5 - Winner: Stormbringer was -Kobas-'s entry, who incorporated lightnings as the power of future weaponry. Despite the instability of lightnings, this entry was stable enough to be approved by judges and public. A hero by -Kobas-. Mini-Game Mapping Contest #4: -Kobas- has won the fourth edition of The Hive Workshop's Mini-Game Mapping Contest with his entry: "Hunted". Check out the Arena if you want to participate on competitions as well! 
Jass:
/*          ======================
            Respawn System - v1.1
            ======================
           
        Coded by:       Mr_Bean (mrbean987789@gmail.com)
       
    Introduction:
    -------------
This system was originally requested by Peacehagen for his map Rise of the Corruptor ORPG.
He recommended that I upload it because it would help people, which I hope it does!
I added some new features to the system and made it as user-friendly as possible.
This system only works for units that are on the map as soon as the game starts.
Feel free to contact me if something is not clear, or if you find bugs.

    Installation:
    -------------
> Copy this trigger.
> Copy the "TimerUtils" trigger (if you don't have it already).
> Configure the "Configurables" section in this trigger as desired.

    Available Functions:
    --------------------
> RespawnAllCreeps()
    - Immediately respawns all dead creeps.
    - Does not affect countdown timer.

> RespawnAllCreepsForPlayer(player whichPlayer)
    - Immediately respawns all dead creeps for a specified player.
    - Does not affect countdown timer.

> PauseRespawnSystem(real howLong)
    - If all creeps respawn together, you can use this to pause the system.
    - The parameter is how long it is paused for. Use any negative number to pause it forever (until you resume again). Using 0 means no pause.
    - Can only use if RESPAWN_TOGETHER = true.

> ResumeRespawnSystem()
    - This will resume the system.
    - Only works if you used PauseRespawnSystem with a negative number (like PauseRespawnSystem(-1)).
    - Can only use if RESPAWN_TOGETHER = true.
   
> ResetRespawnSystem()
    - This will pause the system and reset the countdown.
    - Needs to be unpaused with ResumeRespawnSystem().
    - Can only use if RESPAWN_TOGETHER = true.
   
> RestartRespawnSystem()
    - This will reset the respawn countdown.
    - Will not pause the system.
    - Can only use if RESPAWN_TOGETHER = true.

> GetAliveCreeps()
    - Returns how many creeps are alive.
   
> GetDeadCreeps()
    - Returns how many creeps are dead.
   
> GetTotalCreeps()
    - Returns how many creeps there are in total.
   
    Contact:
    --------
> [email]mrbean987789@gmail.com[/email]
> Mr_Bean987 on the HiveWorkshop.com

*/

library RespawnSystem initializer OnInit requires TimerUtils

    //**************************************************\\
    //*** START OF CONFIGURABLES                     ***\\
    //**************************************************\\
   
    globals
   
        /* hashtable keys */
        private constant key key_x
        private constant key key_y
        private constant key key_facing
        private constant key key_owner
        private constant key key_type
        /* end hashtable keys */

        // Delay between unit dying and respawning:
        private constant real    RESPAWN_DELAY      = 30.0
        // Respawn all dead units at once? If set to false, units will respawn individually.
        private constant boolean RESPAWN_TOGETHER   = false
        // Show a countdown window to all players:
        private constant boolean SHOW_TIMER_WINDOW  = false
        // Countdown window text (only applicable if SHOW_TIMER_WINDOW = true):
        private constant string  TIMER_WINDOW_TEXT  = "Creeps respawn in:"
        // Respawn heroes:
        private constant boolean RESPAWN_HEROES     = false
        // Effect created on respawning units (set to "" to have no effect):
        private constant string  RESPAWN_EFFECT     = "Abilities\\Spells\\Undead\\RaiseSkeletonWarrior\\RaiseSkeleton.mdl"
        // Attachment point of effect on unit:
        private constant string  EFFECT_ATTACHMENT  = "origin"
       
    endglobals
   
    private function CheckOwner takes nothing returns boolean
        local integer playerId = GetPlayerId(GetOwningPlayer(GetFilterUnit()))
        if /*
       
        //------------------------------------------------------\\
        // Add the players that the system respawns creeps for: \\
        // Remember to subtract 1 from GUI player indexes!      \\
        */
 playerId == 10 or /*    // Player 11 in GUI.        \*
        */
 playerId == 11 or /*    // Player 12 in GUI.        \*
        */
 playerId == 12    /*    // Neutral Hostile in GUI.  \*
        //------------------------------------------------------\\
       
        */
then
            return true
        endif
        return false
    endfunction
   
    //**************************************************\\
    //*** END OF CONFIGURABLES                       ***\\
    //**************************************************\\
   
    globals
        private group       deadCreeps      = CreateGroup()
        private group       creeps          = CreateGroup()
        private group       enumG           = CreateGroup()
        private hashtable   ht              = InitHashtable()
        private timer       respawnTimer
        private timerdialog timerWindow
        private integer     tempInt         = 0
    endglobals
   
    private function Increment takes nothing returns nothing
        set tempInt = tempInt + 1
    endfunction
   
    private function EnumerateGroup takes group g returns integer
        set tempInt = 0
        call ForGroup(g, function Increment)
        return tempInt
    endfunction
   
    //**************************************************\\
    //*** GetTotalCreeps                             ***\\
    //**************************************************\\
   
    function GetTotalCreeps takes nothing returns integer
        return EnumerateGroup(creeps) + EnumerateGroup(deadCreeps)
    endfunction
   
    //**************************************************\\
    //*** GetAliveCreeps                             ***\\
    //**************************************************\\
   
    function GetDeadCreeps takes nothing returns integer
        return EnumerateGroup(deadCreeps)
    endfunction
   
    //**************************************************\\
    //*** GetDeadCreeps                              ***\\
    //**************************************************\\
   
    function GetAliveCreeps takes nothing returns integer
        return EnumerateGroup(creeps)
    endfunction
   
    //**************************************************\\
   
    private function RespawnCreepX takes integer handleId returns nothing
        local real    x        = LoadReal(   ht, handleId, key_x)
        local real    y        = LoadReal(   ht, handleId, key_y)
        local real    angle    = LoadReal(   ht, handleId, key_facing)
        local integer owner    = LoadInteger(ht, handleId, key_owner)
        local integer unitType = LoadInteger(ht, handleId, key_type)
        local unit    creep
       
        set creep = CreateUnit(Player(owner), unitType, x, y, angle)
        call FlushChildHashtable(ht, handleId)
        call DestroyEffect(AddSpecialEffectTarget(RESPAWN_EFFECT, creep, EFFECT_ATTACHMENT))
        set handleId = GetHandleId(creep)
        call SaveReal(   ht, handleId, key_x, GetUnitX(creep))
        call SaveReal(   ht, handleId, key_y, GetUnitY(creep))
        call SaveReal(   ht, handleId, key_facing, GetUnitFacing(creep))
        call SaveInteger(ht, handleId, key_owner, GetPlayerId(GetOwningPlayer(creep)))
        call SaveInteger(ht, handleId, key_type, GetUnitTypeId(creep))
       
        call GroupRemoveUnit(deadCreeps, creep)
        call GroupAddUnit(creeps, creep)
       
        set creep = null
    endfunction
   
    //**************************************************\\
    //*** ResumeRespawnSystem                        ***\\
    //**************************************************\\
   
    function ResumeRespawnSystem takes nothing returns nothing
        static if RESPAWN_TOGETHER then
            call ResumeTimer(respawnTimer)
        else
            call BJDebugMsg("|cffff0000Respawn System|r: Cannot use ResumeRespawnSystem when RESPAWN_TOGETHER = false.")
        endif
    endfunction
   
    //**************************************************\\
    //*** PauseRespawnSystem                         ***\\
    //**************************************************\\
   
    function PauseRespawnSystem takes real duration returns nothing
        static if RESPAWN_TOGETHER then
            if duration < 0 then
                call PauseTimer(respawnTimer)
            elseif duration > 0 then
                call PauseTimer(respawnTimer)
                call TimerStart(NewTimer(), duration, false, function ResumeRespawnSystem)
            endif
        else
            call BJDebugMsg("|cffff0000Respawn System|r: Cannot use PauseRespawnSystem when RESPAWN_TOGETHER = false.")
        endif
       
    endfunction
   
    //**************************************************\\
    //*** RespawnAllCreepsForPlayer                  ***\\
    //**************************************************\\
   
    function RespawnAllCreepsForPlayer takes player whichPlayer returns nothing
        local unit temp
       
        set enumG = deadCreeps
       
        loop
            set temp = FirstOfGroup(enumG)
            exitwhen temp == null
           
            if GetOwningPlayer(temp) == whichPlayer then
                call RespawnCreepX(GetHandleId(temp))
            endif
       
            call GroupRemoveUnit(enumG, temp)
       
        endloop
       
    endfunction
   
    private function GroupEnumeration takes nothing returns nothing
        call RespawnCreepX(GetHandleId(GetEnumUnit()))
    endfunction
   
    //**************************************************\\
    //*** RespawnAllCreeps                           ***\\
    //**************************************************\\
   
    function RespawnAllCreeps takes nothing returns nothing
        call ForGroup(deadCreeps, function GroupEnumeration)
    endfunction
   
    //**************************************************\\
    //*** ResetRespawnSystem                         ***\\
    //**************************************************\\
   
    function ResetRespawnSystem takes nothing returns nothing
   
        static if RESPAWN_TOGETHER then
            call TimerStart(respawnTimer, RESPAWN_DELAY, true, function RespawnAllCreeps)
            call PauseTimer(respawnTimer)
        else
            call BJDebugMsg("|cffff0000Respawn System|r: Cannot use ResetRespawnSystem when RESPAWN_TOGETHER = false.")
        endif
       
    endfunction
   
    //**************************************************\\
    //*** RestartRespawnSystem                       ***\\
    //**************************************************\\
   
    function RestartRespawnSystem takes nothing returns nothing
        static if RESPAWN_TOGETHER then
            call TimerStart(respawnTimer, RESPAWN_DELAY, true, function RespawnAllCreeps)
        else
            call BJDebugMsg("|cffff0000Respawn System|r: Cannot use RestartRespawnSystem when RESPAWN_TOGETHER = false.")
        endif
    endfunction

    //**************************************************\\
   
    private function TimerExpiration takes nothing returns nothing
        call RespawnCreepX(GetTimerData(GetExpiredTimer()))
        call ReleaseTimer(GetExpiredTimer())
    endfunction
   
    private function CreepDeath takes nothing returns boolean
        local unit u = GetTriggerUnit()
       
        if IsUnitInGroup(u, creeps) then
            call GroupRemoveUnit(creeps, u)
            call GroupAddUnit(deadCreeps, u)
           
            static if not RESPAWN_TOGETHER then
                call TimerStart(NewTimerEx(GetHandleId(u)), RESPAWN_DELAY, false, function TimerExpiration)
            endif
        endif
       
        set u = null
        return false
    endfunction
   
    private function GroupCreeps takes nothing returns nothing
        local unit temp
        local integer handleId
       
        call GroupEnumUnitsInRect(enumG, bj_mapInitialPlayableArea, Condition(function CheckOwner))
       
        loop
            set temp = FirstOfGroup(enumG)
            exitwhen temp == null
           
            if not IsUnitType(temp, UNIT_TYPE_DEAD) then
                set handleId = GetHandleId(temp)
                call SaveReal(   ht, handleId, key_x, GetUnitX(temp))
                call SaveReal(   ht, handleId, key_y, GetUnitY(temp))
                call SaveReal(   ht, handleId, key_facing, GetUnitFacing(temp))
                call SaveInteger(ht, handleId, key_owner, GetPlayerId(GetOwningPlayer(temp)))
                call SaveInteger(ht, handleId, key_type, GetUnitTypeId(temp))
                call GroupAddUnit(creeps, temp)
            endif
           
            call GroupRemoveUnit(enumG, temp)
           
        endloop
       
    endfunction
   
    private function CreateDialog takes nothing returns nothing
        set timerWindow = CreateTimerDialog(respawnTimer)
        call TimerDialogSetTitle(timerWindow, TIMER_WINDOW_TEXT)
        call TimerDialogDisplay(timerWindow, true)
    endfunction
   
    private function OnInit takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerAddCondition(t, Condition(function CreepDeath))
        call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_DEATH)
        set t = null
       
        call GroupCreeps()
       
        static if RESPAWN_TOGETHER then
       
            set respawnTimer = CreateTimer()
            call TimerStart(respawnTimer, RESPAWN_DELAY, true, function RespawnAllCreeps)
           
            static if SHOW_TIMER_WINDOW then
                call TimerStart(NewTimer(), 0.01, false, function CreateDialog)
            endif
           
        endif
       
    endfunction
   
endlibrary

There is no need for struct, btw, I must suggest that you make timer library optional, so you can use this system even without it.
I say this because I myself code in that way to minimize number of external libraries I use.

For example Table library is really awesome but using it in your map only because of 1 spell is ridiculous, where it can be replaced with indexing or single hash, but when you speak about 10 spells 1 table will certainly become more powerful than 10 hashtables.
__________________

New version is out!
Shadows of the Past v0.9.3.0
Monday, March 25 2013 22:20
Check it out -> ★ Shadows of the Past ★
------------------------------------------------------
We got our Writer, please, welcome Noxx-
-Kobas- is offline   Reply With Quote
Old 06-28-2012, 08:08 PM   #15 (permalink)
Registered User Mr_Bean
Where's Teddy?
 
Mr_Bean's Avatar
 
Join Date: Feb 2011
Posts: 1,593
Mr_Bean is a jewel in the rough (238)Mr_Bean is a jewel in the rough (238)Mr_Bean is a jewel in the rough (238)
Techtree Contest #7 - Winner: The Ancients and The Old One: The incarnation of the World Tree as a Super Unit commences the tides of battle. Nature has found its most precious avenger! A race by Lordkoon & Mr_Bean987. 
But then how can I respawn a specific creep when it dies without attaching its handle id to a timer?
__________________
Mr_Bean's Spell Workshop
- Looking for people to help with GUI & vJASS requests.
- Need help importing your request? Try this guide I wrote!
Mr_Bean is offline   Reply With Quote
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


All times are GMT. The time now is 04:20 PM.





Powered by vBulletin
Copyright 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.5.1 PL2
Copyright © Ralle