• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Unit State System v1.0

This bundle is marked as useful / simple. Simplicity is bliss, low effort and/or may contain minor bugs.
A simple system that easily modifies a unit's Armor, Damage, Max Health and Max Mana. The only requirement is the JASS NewGen Pack. Read below for a detailed explanation.

JASS:
library State initializer OnInit

//=================================================================
//                   Unit State System (USS)
//                            by
//                          Ayanami
//=================================================================
// Unit State System is a easy-to use system that modifies various
// unit properties, namely Armor, Damage, Max Health and Max Mana.
// 
// Pros:
//   - Easy to import (object merger creates all necessary objects)
//   - Easy to use
//   - Can be used in GUI via Custom script
//
// Cons:
//   - Maximum of 99999 armor & damage bonus and reduction
//=================================================================
//                         Functions
//=================================================================
// State_SetArmor(target, value)
//    - "target" is the unit that you want to give the armor bonus/reduction to
//    - "value" is the integer amount of armor bonus/reduction
//    - "value" being negative reduces armor, while "value" being positive increases armor
//    - Support up to maximum of 99999 armor bonus/reduction
//
// State_SetDamage(target, value)
//    - "target" is the unit that you want to give the damage bonus/reduction to
//    - "value" is the integer amount of damage bonus/reduction
//    - "value" being negative reduces damage, while "value" being positive increases damage
//    - Support up to maximum of 99999 damage bonus/reduction
//
// State_ChangeMaxHealth(target, value, boolean)
//    - "target" is the unit that you want to give the max health bonus/reduction to
//    - "value" is the integer amount of max health bonus/reduction
//    - "value" being negative reduces max health, while "value" being positive increases max health
//    - "boolean" is true if you want to add/subtract current health according to increased/decreased
//      max health
//
// State_SetMaxHealth(target, value, boolean)
//    - "target" is the unit that you want to set its max health
//    - "value" is the integer value of the max health that you want to set to
//    - negative or zero "value" results in the max health to be set to 1
//    - "boolean" is true if you want to add/subtract current health according to increased/decreased
//      max health
//
// State_ChangeMaxMana(target, value, boolean)
//    - "target" is the unit that you want to give the max mana bonus/reduction to
//    - "value" is the integer amount of max mana bonus/reduction
//    - "value" being negative reduces max mana, while "value" being positive increases max mana
//    - "boolean" is true if you want to add/subtract current mana according to increased/decreased
//      max mana
//
// State_SetMaxMana(target, value, boolean)
//    - "target" is the unit that you want to set its max mana
//    - "value" is the integer value of the max mana that you want to set to
//    - negative or zero "value" results in the max mana to be set to 1
//    - "boolean" is true if you want to add/subtract current mana according to increased/decreased
//      max mana
//=================================================================
//                       Implementation
//=================================================================
// Requirements:
//    - JNGP (JASS NewGen Pack)
//
// 1) Copy the whole "Unit State System" Trigger folder 
// 2) Save the map, it will take longer than usual
// 3) Close the map and re-open it, then disable or delete the trigger "Objects" 
//=================================================================
//                         Configurables
//=================================================================

// set to true if you want to preload those resources
globals
    private constant boolean ARMORP = true // preloads all armor resources if true
    private constant boolean DAMAGEP = true // preloads all damage resources if true
    private constant boolean HEALTHP = true // preloads all max health resources if true
    private constant boolean MANAP = true // preloads all max mana resources if true
endglobals

//=================================================================
//                   DO NOT TOUCH BELOW
//=================================================================

//! textmacro CreateFunc takes FUNCNAME, ID

public function $FUNCNAME$ takes unit target, integer value returns nothing
    local integer i
    local integer curamount = 0
    local integer newvalue
    local integer factor
    local integer templevel
    local integer array level
    
    if value != 0 then
    
        set i = 0
        loop
            set level[i] = GetUnitAbilityLevel(target, $ID$[i])
            exitwhen i == 9
            set i = i + 1
        endloop
    
        if level[0] == 0 then
            set i = 0
            loop
                call UnitAddAbility(target, $ID$[i])
                exitwhen i == 9
                set i = i + 1
            endloop
        endif
    
        // positive value check
        set i = 4
        set factor = 10000
        loop
            set curamount = curamount + (level[i] - 1) * factor
            exitwhen i == 0
            set factor = factor / 10
            set i = i - 1
        endloop
    
        //negative value check
        set i = 9
        set factor = 10000
        loop
            set curamount = curamount - (level[i] - 1) * factor
            exitwhen i == 5
            set factor = factor / 10
            set i = i - 1
        endloop
    
        set newvalue = curamount + value
        if newvalue == 0 then
            set i = 0
            loop
                call SetUnitAbilityLevel(target, $ID$[i], 1)
                exitwhen i == 9
                set i = i + 1
            endloop
        elseif newvalue > 0 then
            set i = 4
            set factor = 10000
            loop
                set templevel = newvalue / factor
                call SetUnitAbilityLevel(target, $ID$[i], templevel + 1)
                set newvalue = newvalue - (templevel * factor)
                exitwhen i == 0
                set factor = factor / 10
                set i = i - 1
            endloop
            
            set i = 5
            loop
                call SetUnitAbilityLevel(target, $ID$[i], 1)
                exitwhen i == 9
                set i = i + 1
            endloop
        else
            set newvalue = -newvalue
            set i = 9
            set factor = 10000
            loop
                set templevel = newvalue / factor
                call SetUnitAbilityLevel(target, $ID$[i], templevel + 1)
                set newvalue = newvalue - (templevel * factor)
                exitwhen i == 5
                set factor = factor / 10
                set i = i - 1
            endloop
            
            set i = 0
            loop
                call SetUnitAbilityLevel(target, $ID$[i], 1)
                exitwhen i == 4
                set i = i + 1
            endloop
        endif
    endif
endfunction

//! endtextmacro

//! textmacro CreateFunc2 takes FUNCNAME, ID, STATE

public function SetMax$FUNCNAME$ takes unit target, integer value, boolean change returns nothing
    local integer i
    local integer a
    local integer newvalue
    local integer factor
    local integer templevel
    local integer changeamount = value - R2I(GetUnitState(target, UNIT_STATE_MAX_$STATE$))
    local integer array level
    local real statevalue
    
    if change then
        set statevalue = GetUnitState(target, UNIT_STATE_$STATE$) + changeamount
        if statevalue <= 0 then
            set statevalue = 1
        endif
    else
        set statevalue = GetUnitState(target, UNIT_STATE_$STATE$)
    endif
    
    if value <= 0 then
        set newvalue = 1 - R2I(GetUnitState(target, UNIT_STATE_MAX_$STATE$))
    else
        set newvalue = changeamount
    endif
    if newvalue != 0 then
        set factor = 10000
        if newvalue > 0 then
            set i = 6
            loop
                set templevel = newvalue / factor
                
                set a = 0
                loop
                    exitwhen a == templevel
                    call UnitAddAbility(target, $ID$)
                    call SetUnitAbilityLevel(target, $ID$, i)
                    call UnitRemoveAbility(target, $ID$)
                    set a = a + 1
                endloop
                
                set newvalue = newvalue - (templevel * factor)
                exitwhen i == 1
                set factor = factor / 10
                set i = i - 1
            endloop
        else
            set newvalue = -newvalue
            set i = 11
            loop
                set templevel = newvalue / factor
                
                set a = 0
                loop
                    exitwhen a == templevel
                    call UnitAddAbility(target, $ID$)
                    call SetUnitAbilityLevel(target, $ID$, i)
                    call UnitRemoveAbility(target, $ID$)
                    set a = a + 1
                endloop
                
                set newvalue = newvalue - (templevel * factor)
                exitwhen i == 7
                set factor = factor / 10
                set i = i - 1
            endloop
        endif
        call SetUnitState(target, UNIT_STATE_$STATE$, statevalue)
    endif
endfunction

public function ChangeMax$FUNCNAME$ takes unit target, integer value, boolean change returns nothing
    call SetMax$FUNCNAME$(target, R2I(GetUnitState(target, UNIT_STATE_MAX_$STATE$)) + value, change)
endfunction

//! endtextmacro

//============================================================
//                         Armor
//============================================================
globals
    private integer array ARMORID
endglobals

//! runtextmacro CreateFunc("SetArmor", "ARMORID")

//============================================================
//                         Damage
//============================================================
globals
    private integer array DAMAGEID
endglobals

//! runtextmacro CreateFunc("SetDamage", "DAMAGEID")

//============================================================
//                         Health
//============================================================
globals
    private integer HEALTHID
endglobals

//! runtextmacro CreateFunc2("Health", "HEALTHID", "LIFE")

//============================================================
//                          Mana
//============================================================
globals
    private integer MANAID
endglobals

//! runtextmacro CreateFunc2("Mana", "MANAID", "MANA")

//============================================================
//                 Setup & Preload Objects
//============================================================
private function OnInit takes nothing returns nothing
    local integer i
    
    // create preloader
    local unit u = CreateUnit(Player(13), 'pUSS', 0, 0, 0)
    
    // setup armor
    set ARMORID[0] = 'dEF0'
    set ARMORID[1] = 'dEF1'
    set ARMORID[2] = 'dEF2'
    set ARMORID[3] = 'dEF3'
    set ARMORID[4] = 'dEF4'
    set ARMORID[5] = 'dEF5'
    set ARMORID[6] = 'dEF6'
    set ARMORID[7] = 'dEF7'
    set ARMORID[8] = 'dEF8'
    set ARMORID[9] = 'dEF9'
    
    // setup damage
    set DAMAGEID[0] = 'dMG0'
    set DAMAGEID[1] = 'dMG1'
    set DAMAGEID[2] = 'dMG2'
    set DAMAGEID[3] = 'dMG3'
    set DAMAGEID[4] = 'dMG4'
    set DAMAGEID[5] = 'dMG5'
    set DAMAGEID[6] = 'dMG6'
    set DAMAGEID[7] = 'dMG7'
    set DAMAGEID[8] = 'dMG8'
    set DAMAGEID[9] = 'dMG9'
    
    // setup health
    set HEALTHID = 'uHPB'
    
    // setup mana
    set MANAID = 'uMPB'
    
    //preload
    set i = 0
    loop
        static if ARMORP then
            call UnitAddAbility(u, ARMORID[i])
        endif
        
        static if DAMAGEP then
            call UnitAddAbility(u, DAMAGEID[i])
        endif
        
        exitwhen i == 9
        set i = i + 1
    endloop
    
    static if HEALTHP then
        call UnitAddAbility(u, HEALTHID)
    endif
        
    static if MANAP then
        call UnitAddAbility(u, MANAID)
    endif
    
    call RemoveUnit(u)
    set u = null
endfunction

endlibrary

Changelogs

- Initial relase


Keywords:
unit, state, system, uss, armor, damage, max, mana, health, modify
Contents

Unit State System v1.0 (Map)

Reviews
12th Dec 2015 IcemanBo: Too long time as NeedsFix. Rejected. 11:24, 9th Feb 2011 Bribe: This has a lot of competition because of the Status library on TheHelper.net, and it also is running on very unstable script generation. The "Object" trigger...

Moderator

M

Moderator

12th Dec 2015
IcemanBo: Too long time as NeedsFix. Rejected.

11:24, 9th Feb 2011
Bribe: This has a lot of competition because of the Status library on TheHelper.net, and it also is running on very unstable script generation. The "Object" trigger should be posted as well as the script itself, and really could stand being updated to run with Nestharus' safe LUA snippets.
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
public function $FUNCNAME$ takes unit target, integer value returns nothing

Public functions aren't good convention


This should be running on lua

This isn't acceptable
JASS:
    set ARMORID[0] = 'dEF0'
    set ARMORID[1] = 'dEF1'
    set ARMORID[2] = 'dEF2'
    set ARMORID[3] = 'dEF3'
    set ARMORID[4] = 'dEF4'
    set ARMORID[5] = 'dEF5'
    set ARMORID[6] = 'dEF6'
    set ARMORID[7] = 'dEF7'
    set ARMORID[8] = 'dEF8'
    set ARMORID[9] = 'dEF9'
    
    // setup damage
    set DAMAGEID[0] = 'dMG0'
    set DAMAGEID[1] = 'dMG1'
    set DAMAGEID[2] = 'dMG2'
    set DAMAGEID[3] = 'dMG3'
    set DAMAGEID[4] = 'dMG4'
    set DAMAGEID[5] = 'dMG5'
    set DAMAGEID[6] = 'dMG6'
    set DAMAGEID[7] = 'dMG7'
    set DAMAGEID[8] = 'dMG8'
    set DAMAGEID[9] = 'dMG9'

The arrays should all be one array as well.

If you're going to do a system like this, you might as well do all of the possible unit properties too.

I'll be doing a system just like this one, so if you don't want to learn all of the lua stuff you need, you might as well leave it up to me and I'll get to it when I get to it.

And yea, it should be something along the lines of function UnitSetState takes unit whichUnit, integer whichState, real newVal returns nothing


Use http://www.hiveworkshop.com/forums/submissions-414/snippet-lua_get_var_object-185944/. I don't suggest importing the variables as you should be writing the values to an array.

All of the lua scripts you need are in the JASS section (submissions/jass).

And finally this is almost never acceptable
library State initializer OnInit

module initializers only -.-


THW also already has 2 unit state systems that are certainly better than this one ; ). The only reason I'm making one is because of the new lua resources that will make the current 2 obsolete ; P.
 
Top