System Stamina System

Sevion

The DIY Ninja
Reaction score
413
Stamina System
By: Sevion
v1.4​

This is Stamina System, or SS for short. Created by me, Sevion, and idea by ReVolver. Romek also played a roll in optimizing code a bit. That's all the credits. Props to ReVolver and Romek in aiding me in making this system!

Now, ever played a game and really wanted a feel of realism to it? Saw your guy running for hours on end seeming to never get tired? Well, here's your solution! It's also easily editable!

ESRB Rating: NGPM for NewGen, PUI, MovingData required. (Included in map)

The Code:

JASS:
//                           -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-                           \\
//                           -=  Stamina System by Sevion =-                           \\
//                           -=           v1.4            =-                           \\
//+---------------------------=-=-=-=-=-=-=-=-=-=-=-=-=-=-=---------------------------+\\
//| -=-= Functions =-=-                                                               |\\
//+-----------------------------------------------------------------------------------+\\
//| Stamina takes unit whichUnit, boolean OnOffSwitch returns nothing                 |\\
//| whichUnit refers to the unit that you want to affect with stamina                 |\\
//| and OnOffSwitch refers to if you want the unit to have Stamina.                   |\\
//+-----------------------------------------------------------------------------------+\\
//| SetUnitStamina takes unit whichUnit, real staminaAmount returns boolean           |\\
//| whichUnit refers to the unit whose stamina you want to change and                 |\\
//| staminaAmount is what you want to change it to.                                   |\\
//| Returns False if failed and True if succeded.                                     |\\
//+-----------------------------------------------------------------------------------+\\
//| GetUnitStamina takes unit whichUnit returns real                                  |\\
//| whichUnit refers to the unit whose stamina you want to retrieve                   |\\
//+-----------------------------------------------------------------------------------+\\
//| AddUnitStamina takes unit whichUnit, real staminaAmount returns boolean           |\\
//| whichUnit refers to the unit that you want to add or subtract (negative)          |\\
//| stamina from and staminaAmount refers to how much stamina to add.                 |\\
//| Returns False if failed and True if succeded.                                     |\\
//+-----------------------------------------------------------------------------------+\\
//| -=-= Variables =-=-                                                               |\\
//+-----------------------------------------------------------------------------------+\\
//| SS_Stamina                                                                        |\\
//+===================================================================================+\\
//| Amount of Stamina for each unit.                                                  |\\
//+-----------------------------------------------------------------------------------+\\
//| SS_Units                                                                          |\\
//+===================================================================================+\\
//| Units currently being affected by Stamina System.                                 |\\
//+-----------------------------------------------------------------------------------+\\
//| SS_StaminaKill                                                                    |\\
//+===================================================================================+\\
//| Determines whether or not to kill units when they are Exhausted.                  |\\
//+-----------------------------------------------------------------------------------+\\
//| SS_High                                                                           |\\
//+===================================================================================+\\
//| The raw code of Stamina: High.                                                    |\\
//+-----------------------------------------------------------------------------------+\\
//| SS_Normal                                                                         |\\
//+===================================================================================+\\
//| The raw code of Stamina: Normal.                                                  |\\
//+-----------------------------------------------------------------------------------+\\
//| SS_Low                                                                            |\\
//+===================================================================================+\\
//| The raw code of Stamina: Low.                                                     |\\
//+-----------------------------------------------------------------------------------+\\
//| SS_Exhausted                                                                      |\\
//+===================================================================================+\\
//| The raw code of Exhausted.                                                        |\\
//+-----------------------------------------------------------------------------------+\\
//| SS_StaminaMax                                                                     |\\
//+===================================================================================+\\
//| Maximum amount of Stamina.                                                        |\\
//+-----------------------------------------------------------------------------------+\\
//| SS_StaminaNormal                                                                  |\\
//+===================================================================================+\\
//| Amount of Stamina at which a unit will have Stamina: Normal state.                |\\
//+-----------------------------------------------------------------------------------+\\
//| SS_StaminaLow                                                                     |\\
//+===================================================================================+\\
//| Amount of Stamina at which a unit will have Stamina: Low state.                   |\\
//+-----------------------------------------------------------------------------------+\\
//| SS_StaminaExhausted                                                               |\\
//+===================================================================================+\\
//| Amount of Stamina at which a unit will have Exhausted state.                      |\\
//+-----------------------------------------------------------------------------------+\\
//| SS_StaminaRecoverRate                                                             |\\
//+===================================================================================+\\
//| Rate of which units recover Stamina.                                              |\\
//+-----------------------------------------------------------------------------------+\\
//| SS_StaminaLoseRate                                                                |\\
//+===================================================================================+\\
//| Rate of which units lose Stamina.                                                 |\\
//+-----------------------------------------------------------------------------------+\\
//| SS_StaminaUpdateInterval                                                          |\\
//+===================================================================================+\\
//| Interval of which Stamina is updated (lost and recovered).                        |\\
//+-----------------------------------------------------------------------------------+\\
//| SS_StaminaExhaustedString                                                         |\\
//+===================================================================================+\\
//| String to show when units are Exhausted. Only shows when SS_StaminaKill is false  |\\
//+-----------------------------------------------------------------------------------+\\
//| SS_StaminaWarningString                                                           |\\
//+===================================================================================+\\
//| String to show when units are almost Exhausted.                                   |\\
//+-----------------------------------------------------------------------------------+\\
//| SS_DefaultStamina                                                                 |\\
//+===================================================================================+\\
//| Default set stamina when unit not already in the system is used in AddUnitStamina |\\
//+-----------------------------------------------------------------------------------+\\
//| SS_Attack                                                                         |\\
//+===================================================================================+\\
//| OrderId of Attack                                                                 |\\
//+-----------------------------------------------------------------------------------+\\
//| -=-= Version History =-=-                                                         |\\
//+-----------------------------------------------------------------------------------+\\
//| Version 1.0:                                                                      |\\
//|  - Created Stamina System                                                         |\\
//+-----------------------------------------------------------------------------------+\\
//| Version 1.1:                                                                      |\\
//|  - Small changes in functions, private and constant prefixes added.               |\\
//+-----------------------------------------------------------------------------------+\\
//| Version 1.2:                                                                      |\\
//|  - Inlined init function. Removed need for CloneGroup and used ForGroup instead.  |\\
//|  - SetUnitStamina and AddUnitStamina now adds unit to queue if not in it already. |\\
//|  - Added functionality for detecting attacking.                                   |\\
//+-----------------------------------------------------------------------------------+\\
//| Version 1.21:                                                                     |\\
//|  - Added SS_DefaultStamina for when a unit not in the system is used in           |\\
//|    AddUnitStamina.                                                                |\\
//+-----------------------------------------------------------------------------------+\\
//| Version 1.22:                                                                     |\\
//|  - Added SS_Attack Constant                                                       |\\
//+-----------------------------------------------------------------------------------+\\
//| Version 1.3:                                                                      |\\
//|  - Added exhaustion warning                                                       |\\
//+-----------------------------------------------------------------------------------+\\
//| Version 1.4:                                                                      |\\
//|  - Made attacking units not die of exhaustion and only moving units die.          |\\
//+-----------------------------------------------------------------------------------+\\
//| Credit: Sevion        
	
	


<div class="bbCodeBlock bbCodeBlock--screenLimited bbCodeBlock--code">
	<div class="bbCodeBlock-title">
		Code:
	</div>
	<div class="bbCodeBlock-content" dir="ltr">
		<pre class="bbCodeCode" dir="ltr" data-xf-init="code-block" data-lang=""><code>                                                      |\\
//|         ReVolver      [Code Improvements]                                         |\\
//|         Romek         [Code Improvements]                                         |\\
//|         Artificial    [Code Improvements]                                         |\\
//|         Exfiltrate    [Idea]                                                      |\\
//+-----------------------------------------------------------------------------------+\\

library StaminaSystem initializer SS_Init requires PUI, MovingData
    globals
        private             real    array SS_Stamina
        private             group         SS_Units = CreateGroup()
        private constant    boolean       SS_StaminaKill = false
        private constant    integer       SS_High = &#039;SS03&#039;
        private constant    integer       SS_Normal = &#039;SS02&#039;
        private constant    integer       SS_Low = &#039;SS01&#039;
        private constant    integer       SS_Exhausted = &#039;SS07&#039;
        private constant    real          SS_StaminaMax = 100.
        private constant    real          SS_StaminaNormal = 50.
        private constant    real          SS_StaminaLow = 25.
        private constant    real          SS_StaminaExhausted = 5.
        private constant    real          SS_StaminaUpdateInterval = 1.
        private constant    real          SS_StaminaRecoverRate = 1.
        private constant    real          SS_StaminaLoseRate = 1.
        private constant    string        SS_ExhaustedString = &quot;You have driven your unit to exhaustion! As a result it has died!&quot;
        private constant    string        SS_WarningString = &quot;Your unit is approaching exhaustion!&quot;
        private constant    real          SS_DefaultStamina = 75
        private constant    integer       SS_Attack = 851983
    endglobals
    
    private function Callback takes nothing returns nothing
        if (IsUnitMovingGC(GetEnumUnit()) or GetUnitCurrentOrder(GetEnumUnit()) == SS_Attack) and SS_Stamina[GetUnitIndex(GetEnumUnit())] &gt; 0. then
            if GetUnitCurrentOrder(GetEnumUnit()) == SS_Attack and SS_Stamina[GetUnitIndex(GetEnumUnit())] &lt;= SS_StaminaExhausted + 1 then
                if SS_StaminaKill == false then
                    set SS_Stamina[GetUnitIndex(GetEnumUnit())] = SS_Stamina[GetUnitIndex(GetEnumUnit())] - SS_StaminaLoseRate
                endif
            else
                set SS_Stamina[GetUnitIndex(GetEnumUnit())] = SS_Stamina[GetUnitIndex(GetEnumUnit())] - SS_StaminaLoseRate
            endif
        elseif (IsUnitMovingGC(GetEnumUnit()) == false or GetUnitCurrentOrder(GetEnumUnit()) != SS_Attack) and SS_Stamina[GetUnitIndex(GetEnumUnit())] &lt; 100. then
            set SS_Stamina[GetUnitIndex(GetEnumUnit())] = SS_Stamina[GetUnitIndex(GetEnumUnit())] + SS_StaminaRecoverRate
        endif
        call UnitRemoveAbility(GetEnumUnit(), SS_High)
        call UnitRemoveAbility(GetEnumUnit(), SS_Normal)
        call UnitRemoveAbility(GetEnumUnit(), SS_Low)
        call UnitRemoveAbility(GetEnumUnit(), SS_Exhausted)
        if SS_Stamina[GetUnitIndex(GetEnumUnit())] &gt; SS_StaminaNormal then
            call UnitAddAbility(GetEnumUnit(), SS_High)
            debug call BJDebugMsg(R2S(SS_Stamina[GetUnitIndex(GetEnumUnit())]))
        elseif SS_Stamina[GetUnitIndex(GetEnumUnit())] &gt; SS_StaminaLow then
            call UnitAddAbility(GetEnumUnit(), SS_Normal)
            debug call BJDebugMsg(R2S(SS_Stamina[GetUnitIndex(GetEnumUnit())]))
        elseif SS_Stamina[GetUnitIndex(GetEnumUnit())] &gt; SS_StaminaExhausted then
            call UnitAddAbility(GetEnumUnit(), SS_Low)
            if SS_Stamina[GetUnitIndex(GetEnumUnit())] == 25 then
                call DisplayTextToPlayer(GetOwningPlayer(GetEnumUnit()), 0, 0, SS_WarningString)
            endif
            debug call BJDebugMsg(R2S(SS_Stamina[GetUnitIndex(GetEnumUnit())]))
        else
            if SS_StaminaKill then
                call KillUnit(GetEnumUnit())
                call DisplayTextToPlayer(GetOwningPlayer(GetEnumUnit()), 0, 0, SS_ExhaustedString)
                call Stamina.evaluate(GetEnumUnit(), false)
            else
                call UnitAddAbility(GetEnumUnit(), SS_Exhausted)
                debug call BJDebugMsg(R2S(SS_Stamina[GetUnitIndex(GetEnumUnit())]))
            endif
        endif
    endfunction
    
    function Stamina takes unit whichUnit, boolean OnOffSwitch returns nothing
        if OnOffSwitch then
            call GroupAddUnit(SS_Units, whichUnit)
            call EnableMoveCheckGCex(whichUnit)
        else
            call GroupRemoveUnit(SS_Units, whichUnit)
            call RemoveMoveCheckGC(whichUnit, true)
        endif
    endfunction
    
    function SetUnitStamina takes unit whichUnit, real staminaAmount returns boolean
        if whichUnit != null and IsUnitInGroup(whichUnit, SS_Units) then
            set SS_Stamina[GetUnitIndex(whichUnit)] = staminaAmount
            return true
        else
            call Stamina(whichUnit, true)
            set SS_Stamina[GetUnitIndex(whichUnit)] = staminaAmount
            return true
        endif
        
        return false
    endfunction
    
    function GetUnitStamina takes unit whichUnit returns real
        if IsUnitInGroup(whichUnit, SS_Units) then
            return SS_Stamina[GetUnitIndex(whichUnit)]
        endif
        
        return 0.
    endfunction
    
    function AddUnitStamina takes unit whichUnit, real staminaAmount returns boolean
        if whichUnit != null and IsUnitInGroup(whichUnit, SS_Units) then
            set SS_Stamina[GetUnitIndex(whichUnit)] = SS_Stamina[GetUnitIndex(whichUnit)] + staminaAmount
            return true
        else
            call Stamina(whichUnit, true)
            set SS_Stamina[GetUnitIndex(whichUnit)] = SS_DefaultStamina
            return true
        endif
        
        return false
    endfunction
    
    function StaminaUpdateActions takes nothing returns nothing
        call ForGroup(SS_Units, function Callback)
    endfunction
    
    private function SS_Init takes nothing returns nothing
        call TimerStart(CreateTimer(), SS_StaminaUpdateInterval, true, function StaminaUpdateActions)
    endfunction
    
endlibrary
[/JASS]

[B][SIZE=&quot;4&quot;][COLOR=&quot;Red&quot;][U]How To Use[/U][/COLOR][/SIZE][/B]

[List][*]Copy PUI, MovingData and StaminaSystem into your map.
[*]Select all of the units you wish to have Stamina.
[*]Loop through the group and use call Stamina(whichUnit, true) to add it into the Stamina System.
[*]If you wish to, set the unit&#039;s Stamina to 100 using call SetUnitStamina(whichUnit, 100.) OR call SetUnitStamina(whichUnit, SS_StaminaMax). AddUnitStamina also works.
[*]Watch your unit speed up and slow down as his Stamina is used up and recovered over time automatically.[/List]

NOTE: If you want the system to display the amount of Stamina a unit has, open it in NewGen and go under JassHelper. Enable Debug mode and save. Then test the map.

[B][SIZE=&quot;4&quot;][COLOR=&quot;Red&quot;][U]Configuration[/U][/COLOR][/SIZE][/B]

[JASS]    globals
        private             real    array SS_Stamina
        private             group         SS_Units = CreateGroup()
        private constant    boolean       SS_StaminaKill = false
        private constant    integer       SS_High = &#039;SS03&#039;
        private constant    integer       SS_Normal = &#039;SS02&#039;
        private constant    integer       SS_Low = &#039;SS01&#039;
        private constant    integer       SS_Exhausted = &#039;SS07&#039;
        private constant    real          SS_StaminaMax = 100.
        private constant    real          SS_StaminaNormal = 50.
        private constant    real          SS_StaminaLow = 25.
        private constant    real          SS_StaminaExhausted = 5.
        private constant    real          SS_StaminaUpdateInterval = 1.
        private constant    real          SS_StaminaRecoverRate = 1.
        private constant    real          SS_StaminaLoseRate = 1.
        private constant    string        SS_ExhaustedString = &quot;You have driven your unit to exhaustion! As a result it has died!&quot;
        private constant    string        SS_WarningString = &quot;Your unit is approaching exhaustion!&quot;
        private constant    real          SS_DefaultStamina = 75
        private constant    integer       SS_Attack = 851983
    endglobals[/JASS]

[List][*]SS_Stamina - Change using functions SetUnitStamina and AddUnitStamina.
[*]SS_Units - Change using function Stamina.
[*]SS_StaminaKill - Change to True to kill units and False to not kill units when Exhausted.
[*]SS_High - Change to the raw code of the Stamina: High ability.
[*]SS_Normal - Change to the raw code of the Stamina: Normal ability.
[*]SS_Low - Change to the raw code of the Stamina: Low ability.
[*]SS_Exhausted - Change to the raw code of the Exhausted ability.
[*]SS_StaminaMax - Change to any number higher than SS_StaminaNormal (If you want the state Stamina: High for units. Lower works as well.)
[*]SS_StaminaNormal - Change to any number lower than SS_StaminaMax (If you want the state Stamina: High for units. Higher works as well.)
[*]SS_StaminaLow - Change to any number lower than SS_StaminaNormal (If you want the state Stamina: Normal for units. Higher works as, but there is no Stamina: Normal state.)
[*]SS_StaminaExhausted - Change this to any number lower than [*]SS_StaminaLow (If you want the state Stamina: Low for units.  Higher works as well, but there is no Stamina: Low state.)
[*]SS_StaminaUpdateInterval - Change this to any number above 0.
[*]SS_StaminaRecoverRate - Change this to any number. (Negatives result in losing Stamina when standing still.)
[*]SS_StaminaLoseRate - Change this to any number. (Negatives result in gaining Stamina when moving.)
[*]SS_ExhaustedString - Change this to whatever you want to display when units die at exhaustion. (Only works if SS_StaminaKill is true.)
[*]SS_WarningString - Change this to whatever you want to display when units approach exhaustion.
[*]SS_StaminaRecoverRate - Change this to how much stamina you want to recover every second.
[*]SS_Attack - OrderId of Attack.[/List]

[B][SIZE=&quot;4&quot;][COLOR=&quot;Red&quot;][U]FAQ[/U][/COLOR][/SIZE][/B]

None yet.</code></pre>
	</div>
</div>
 

Attachments

  • Stamina System.w3x
    30.1 KB · Views: 350

Ghostwind

o________o
Reaction score
172
Hmm, interesting. Mind if my team use this for fatigue in TES:WC3 (as soon as we actually start...)?
 

Sevion

The DIY Ninja
Reaction score
413
Go for it. Just don't forget to create the custom abilities. I based them off Endurance Aura, I'm not sure if it will clash with another, though.
 

emjlr3

Change can be a good thing
Reaction score
395
JASS:
private constant    real          SS_StaminaMax = 100.
        private constant    real          SS_StaminaNormal = 50.
        private constant    real          SS_StaminaLow = 75.


seem to be in the wrong order

JASS:
private constant    string        SS_ExhaustedString = &quot;You have driven your unit to exhaustion! As a result it has died!&quot;


is not used correctly
 

emjlr3

Change can be a good thing
Reaction score
395
I see :

JASS:
 if SS_StaminaKill then
                    call KillUnit(whichUnit)
                    call DisplayTextToPlayer(GetOwningPlayer(whichUnit), 0, 0, &quot;You have driven your unit to exhaustion! As a result it has died!&quot;)
                else


as well

and why does everything have the SS_ prefix? Having a library removes such a need, especially since everything is private
and why remove abilities the unit doesn't even have?
 

Romek

Super Moderator
Reaction score
963
You forgot to use the constant instead of that string as I told you to earlier :rolleyes:
JASS:
                    call DisplayTextToPlayer(GetOwningPlayer(whichUnit), 0, 0, &quot;You have driven your unit to exhaustion! As a result it has died!&quot;)


And:
JASS:
//| The raw code of Stamina: High.                                  .                  |\\

That random dot '.' is.. random. It also makes the |\\ at the end of the line stick out.
 

Sevion

The DIY Ninja
Reaction score
413
I see, some small mess ups in code :p I'll go and fix all that.

emjlr said:
seem to be in the wrong order

What do you mean by that?

Can you make it so instead of killing it, it slows the unit?

Setting SS_StaminaKill to false will make the unit "Exhausted" instead of kill it.

and why does everything have the SS_ prefix? Having a library removes such a need, especially since everything is private
and why remove abilities the unit doesn't even have?

Makes it look better :p
I remove them because they will have one of them.
 

emjlr3

Change can be a good thing
Reaction score
395
i think it makes it look shitty

@What do you mean by that?

to me, if max is 100 and normal is 50, shouldnt low be 25? in which case high is 75(which is closer to max hen low - as it should be)
 

Sevion

The DIY Ninja
Reaction score
413
Ah, I remember why it's like that. It was in some testing for bugs and some other stuff for the Configuration portion. It's supposed to be 25, yes. I didn't look closely at what was there :p
 

quraji

zap
Reaction score
144
Suggestion: Add in functionality for users to add functions they would like to run if a units has a certain amount of stamina. Then, you can remove your kill unit thing and high/low stamina things.

Easy way to do this would be to create triggers and attach triggeractions to it and evaluate trigger when stamina has reached designated point. Only trouble is that it would be some effort to allow use of the unit inside the function. Could be solved by some trick with GetTriggerUnit (cause the unit to be the trigger unit somehow). Or attachment. This would allow for real time addition/removal of actions to run when certain unit(s) hit a specific amount or range of stamina.

Pulling it off would make this system much more useful in my opinion.

Note: If this doesn't make sense, it's because I'm falling asleep as I type this..I can be more clear if you need me to. There might also be a better way to achieve this functionality, but I can't think much on it at the moment :p
 

Romek

Super Moderator
Reaction score
963
Suggestion: Add in functionality for users to add functions they would like to run if a units has a certain amount of stamina. Then, you can remove your kill unit thing and high/low stamina things.

Easy way to do this would be to create triggers and attach triggeractions to it and evaluate trigger when stamina has reached designated point. Only trouble is that it would be some effort to allow use of the unit inside the function. Could be solved by some trick with GetTriggerUnit (cause the unit to be the trigger unit somehow). Or attachment. This would allow for real time addition/removal of actions to run when certain unit(s) hit a specific amount or range of stamina.

Pulling it off would make this system much more useful in my opinion.

Note: If this doesn't make sense, it's because I'm falling asleep as I type this..I can be more clear if you need me to. There might also be a better way to achieve this functionality, but I can't think much on it at the moment :p
That's a really good idea.
it could be done in the constants...

JASS:
private constant code LOW_CALL = function HeHasLowStamina

// or:

private constant trigger LOW_TRIG = gg_trg_Low_Stamina
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top