Stamina System
By: Sevion
v1.4
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;SS03039;
private constant integer SS_Normal = 039;SS02039;
private constant integer SS_Low = 039;SS01039;
private constant integer SS_Exhausted = 039;SS07039;
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 = "You have driven your unit to exhaustion! As a result it has died!"
private constant string SS_WarningString = "Your unit is approaching exhaustion!"
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())] > 0. then
if GetUnitCurrentOrder(GetEnumUnit()) == SS_Attack and SS_Stamina[GetUnitIndex(GetEnumUnit())] <= 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())] < 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())] > SS_StaminaNormal then
call UnitAddAbility(GetEnumUnit(), SS_High)
debug call BJDebugMsg(R2S(SS_Stamina[GetUnitIndex(GetEnumUnit())]))
elseif SS_Stamina[GetUnitIndex(GetEnumUnit())] > SS_StaminaLow then
call UnitAddAbility(GetEnumUnit(), SS_Normal)
debug call BJDebugMsg(R2S(SS_Stamina[GetUnitIndex(GetEnumUnit())]))
elseif SS_Stamina[GetUnitIndex(GetEnumUnit())] > 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="4"][COLOR="Red"][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 unit039;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="4"][COLOR="Red"][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;SS03039;
private constant integer SS_Normal = 039;SS02039;
private constant integer SS_Low = 039;SS01039;
private constant integer SS_Exhausted = 039;SS07039;
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 = "You have driven your unit to exhaustion! As a result it has died!"
private constant string SS_WarningString = "Your unit is approaching exhaustion!"
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="4"][COLOR="Red"][U]FAQ[/U][/COLOR][/SIZE][/B]
None yet.</code></pre>
</div>
</div>