• 🏆 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!

[Snippet] Unit

This is an API used for managing units.

It can increase efficiency by storing constant unit data too.

I didn't provide "Get" functions in the API because that would actually decrease efficiency =o

JASS:
library Unit requires AIDS, AutoFly // v1.2.0.0 by Magtheridon96
    private module Init
        private static method onInit takes nothing returns nothing
            call AIDS_RegisterOnEnter(Condition(function thistype.run))
            call AIDS_RegisterOnDeallocate(Condition(function thistype.clear))
        endmethod
    endmodule
    
    struct Unit
        unit whichUnit
        string unitName
        integer handleId
        integer foodMade
        integer foodUsed
        race unitRace
        real defaultRange
        real defaultMoveSpeed
        real defaultZ
        real defaultTurnSpeed
        
        static integer array index
        static method operator [] takes unit u returns thistype
            return index[GetUnitId(u)]
        endmethod
        method operator hp= takes real new returns nothing
            call SetWidgetLife(this.whichUnit,new)
        endmethod
        method operator hpAdd= takes real new returns nothing
            call SetWidgetLife(this.whichUnit,GetWidgetLife(this.whichUnit)+new)
        endmethod
        method operator hpSub= takes real new returns nothing
            call SetWidgetLife(this.whichUnit,GetWidgetLife(this.whichUnit)-new)
        endmethod
        method operator mana= takes real new returns nothing
            call SetUnitState(this.whichUnit,UNIT_STATE_MANA,new)
        endmethod
        method operator manaAdd= takes real new returns nothing
            call SetUnitState(this.whichUnit,UNIT_STATE_MANA,GetUnitState(this.whichUnit,UNIT_STATE_MANA)+new)
        endmethod
        method operator manaSub= takes real new returns nothing
            call SetUnitState(this.whichUnit,UNIT_STATE_MANA,GetUnitState(this.whichUnit,UNIT_STATE_MANA)-new)
        endmethod
        method operator moveSpeed= takes real new returns nothing
            call SetUnitMoveSpeed(this.whichUnit,new)
        endmethod
        method operator moveSpeedAdd= takes real new returns nothing
            call SetUnitMoveSpeed(this.whichUnit,GetUnitMoveSpeed(this.whichUnit)+new)
        endmethod
        method operator moveSpeedSub= takes real new returns nothing
            call SetUnitMoveSpeed(this.whichUnit,GetUnitMoveSpeed(this.whichUnit)-new)
        endmethod
        method operator angle= takes real new returns nothing
            call SetUnitFacing(this.whichUnit,new)
        endmethod
        method operator x= takes real new returns nothing
            call SetUnitX(this.whichUnit,new)
        endmethod
        method operator y= takes real new returns nothing
            call SetUnitY(this.whichUnit,new)
        endmethod
        method operator z= takes real new returns nothing
            call SetUnitFlyHeight(this.whichUnit,new,0)
        endmethod
        method operator range= takes real new returns nothing
            call SetUnitAcquireRange(this.whichUnit,new)
        endmethod
        method operator rangeAdd= takes real new returns nothing
            call SetUnitAcquireRange(this.whichUnit,GetUnitAcquireRange(this.whichUnit)+new)
        endmethod
        method operator rangeSub= takes real new returns nothing
            call SetUnitAcquireRange(this.whichUnit,GetUnitAcquireRange(this.whichUnit)-new)
        endmethod
        method operator order= takes integer new returns nothing
            call IssueImmediateOrderById(this.whichUnit,new)
        endmethod
        method operator turnSpeed= takes real new returns nothing
            call SetUnitTurnSpeed(this.whichUnit,new)
        endmethod
        method operator turnSpeedAdd= takes real new returns nothing
            call SetUnitTurnSpeed(this.whichUnit,GetUnitTurnSpeed(this.whichUnit)+new)
        endmethod
        method operator turnSpeedSub= takes real new returns nothing
            call SetUnitTurnSpeed(this.whichUnit,GetUnitTurnSpeed(this.whichUnit)-new)
        endmethod
        method operator pathing= takes boolean new returns nothing
            call SetUnitPathing(this.whichUnit,new)
        endmethod
        method operator size= takes real new returns nothing
            call SetUnitScale(this.whichUnit,new,new,new)
        endmethod
        method operator invulnerable= takes boolean new returns nothing
            call SetUnitInvulnerable(this.whichUnit,new)
        endmethod
        method operator animation= takes string new returns nothing
            call SetUnitAnimation(this.whichUnit,new)
        endmethod
        method operator animationIndex= takes integer new returns nothing
            call SetUnitAnimationByIndex(this.whichUnit,new)
        endmethod
        method operator blendtime= takes real new returns nothing
            call SetUnitBlendTime(this.whichUnit,new)
        endmethod
        method operator creepGuard= takes boolean new returns nothing
            call SetUnitCreepGuard(this.whichUnit,new)
        endmethod
        method operator exploded= takes boolean new returns nothing
            call SetUnitExploded(this.whichUnit,new)
        endmethod
        method operator color= takes playercolor new returns nothing
            call SetUnitColor(this.whichUnit,new)
        endmethod
        method operator useFood= takes boolean new returns nothing
            call SetUnitUseFood(this.whichUnit,new)
        endmethod
        method operator owner= takes player new returns nothing
            call SetUnitOwner(this.whichUnit,new,true)
        endmethod
        method operator propWindow= takes real new returns nothing
            call SetUnitPropWindow(this.whichUnit,new)
        endmethod
        method operator typeSlots= takes integer new returns nothing
            call SetUnitTypeSlots(this.whichUnit,new)
        endmethod
        method operator timeScale= takes real new returns nothing
            call SetUnitTimeScale(this.whichUnit,new)
        endmethod
        method operator level= takes integer new returns nothing
            call SetHeroLevel(this.whichUnit,new,false)
        endmethod
        method operator levelAdd= takes integer new returns nothing
            call SetHeroLevel(this.whichUnit,GetUnitLevel(this.whichUnit)+new,false)
        endmethod
        method operator levelSub= takes integer new returns nothing
            call SetHeroLevel(this.whichUnit,GetUnitLevel(this.whichUnit)-new,false)
        endmethod
        method operator agility= takes integer new returns nothing
            call SetHeroAgi(this.whichUnit,new,true)
        endmethod
        method operator agilityAdd= takes integer new returns nothing
            call SetHeroAgi(this.whichUnit,GetHeroAgi(this.whichUnit,false)+new,true)
        endmethod
        method operator agilitySub= takes integer new returns nothing
            call SetHeroAgi(this.whichUnit,GetHeroAgi(this.whichUnit,false)-new,true)
        endmethod
        method operator intelligence= takes integer new returns nothing
            call SetHeroInt(this.whichUnit,new,true)
        endmethod
        method operator intelligenceAdd= takes integer new returns nothing
            call SetHeroInt(this.whichUnit,GetHeroInt(this.whichUnit,false)+new,true)
        endmethod
        method operator intelligenceSub= takes integer new returns nothing
            call SetHeroInt(this.whichUnit,GetHeroInt(this.whichUnit,false)-new,true)
        endmethod
        method operator strength= takes integer new returns nothing
            call SetHeroStr(this.whichUnit,new,true)
        endmethod
        method operator strengthAdd= takes integer new returns nothing
            call SetHeroStr(this.whichUnit,GetHeroStr(this.whichUnit,false)+new,true)
        endmethod
        method operator strengthSub= takes integer new returns nothing
            call SetHeroStr(this.whichUnit,GetHeroStr(this.whichUnit,false)-new,true)
        endmethod
        method operator xp= takes integer new returns nothing
            call SetHeroXP(this.whichUnit,new,false)
        endmethod
        method operator xpAdd= takes integer new returns nothing
            call SetHeroXP(this.whichUnit,GetHeroXP(this.whichUnit)+new,false)
        endmethod
        method operator xpSub= takes integer new returns nothing
            call SetHeroXP(this.whichUnit,GetHeroXP(this.whichUnit)-new,false)
        endmethod
        static method run takes nothing returns boolean
            local thistype this = thistype.allocate()
            set this.whichUnit = GetTriggerUnit()
            set this.handleId = GetHandleId(this.whichUnit)
            set this.unitName = GetUnitName(this.whichUnit)
            set this.foodMade = GetUnitFoodMade(this.whichUnit)
            set this.foodUsed = GetUnitFoodUsed(this.whichUnit)
            set this.unitRace = GetUnitRace(this.whichUnit)
            set this.defaultZ = GetUnitDefaultFlyHeight(this.whichUnit)
            set this.defaultRange = GetUnitDefaultAcquireRange(this.whichUnit)
            set this.defaultMoveSpeed = GetUnitDefaultMoveSpeed(this.whichUnit)
            set this.defaultTurnSpeed = GetUnitDefaultTurnSpeed(this.whichUnit)
            set thistype.index[GetUnitId(this.whichUnit)]=this
            return false
        endmethod
        method destroy takes nothing returns nothing
            set this.whichUnit = null
            set this.unitRace = null
            call this.deallocate()
        endmethod
        static method clear takes nothing returns boolean
            call Unit[GetTriggerUnit()].destroy()
            return false
        endmethod
        implement Init
    endstruct
endlibrary

It requires AIDS and AutoFly

If you want, I'll create a UnitIndexer-based version of this.... On second thought, nevermind 0.o

The final product would be:

JASS:
function bla takes nothing returns nothing
    local unit u = GetTriggerUnit()
    set Unit[u].hp = 2423
    set Unit[u].maxMana = 2325
    set Unit[u].angle = 90
    set Unit[u].z = 100
    set Unit[u].x = 34
    set Unit[u].y = 24
    set Unit[u].hpAdd = 23 // Adds 23 hp
    set Unit[u].manaAdd = 24 // Adds 24 mana
    set Unit[u].moveSpeedSub = 4 // Decreases by 4
endfunction

Give credits if used :)
 
Last edited:
w00t? 0_0
How come I've never heard of it D:

edit:
I found it..
I wonder why it was GYed..

edit:
UnitData has the potential to slow down maps. Constants aren't stored in variables (That's how I remember it from 45 minutes ago ... then again, my memory sux), get functions are provided when you don't really need
them (inlined, and repeating...)

edit:
Maybe I should change it to "Widget" :p
 
Last edited:
Level 31
Joined
Jul 10, 2007
Messages
6,306
/sigh

hook RemoveUnit recycle

why??? ... you are using a unit indexer, you can use that to recycle >.<. Hell, you should get rid of the need for a UnitIndexer as well as all of the values...

And get rid of recycle and run too... just return the natives >.>.

also, it'd be smarter to have a Hero struct and a Unit struct and to have them as two separate resources. There should also be a widget struct.

The reason UnitData was gy'd I think was because it wasn't updated to use latest UnitIndexer API while still trying to support it =P.
 
why??? ... you are using a unit indexer, you can use that to recycle >.<. Hell, you should get rid of the need for a UnitIndexer as well as all of the values...

And get rid of recycle and run too... just return the natives >.>.

also, it'd be smarter to have a Hero struct and a Unit struct and to have them as two separate resources. There should also be a widget struct.

The reason UnitData was gy'd I think was because it wasn't updated to use latest UnitIndexer API while still trying to support it =P.

I can sense you're REALLY angry now =O =P
I'll fix it...

edit
The values are needed (since they're constant)
Running the same native over and over again for the same value is .. stupid >:D (Some values NEVER change)

edit
A unit indexer is needed here because this: static method operator [] takes unit u returns thistype depends on GetUnitId from AIDS

updated
I only changed what I thought was a must...
 
Last edited:
Level 31
Joined
Jul 10, 2007
Messages
6,306
Using fields for this is a really stupid idea. If you need to read a value over and over again, then store it in a local the first time you read it like you would any other time.


TriggerHappy once made a library similar to this and I can tell you that it was gy'd fairly quickly for the same reasons, plus the fact that he had a timer to update his fields.

And no, I don't get angry =).


I'm just letting you know that your current design is very bad.
 
Top