[Snippet] FlyHeight

Scripts

14 years
edited 8 years
The documentation has a brief explanation as to why I made this.

Code

Code (jass) Select
1
/*************************************
2
*
3
*   FlyHeight
4
*   v1.0.0.1
5
*   By Magtheridon96
6
*
7
*   - Warcraft III movement types are pretty
8
*     bugged. It would be much better to write
9
*     them from scratch. This system allows
10
*     you to create flying units with changeable
11
*     heights.
12
*
13
*     All units must have Movement Type Foot.
14
*
15
*   Requires:
16
*   ---------
17
*
18
*       - UnitIndexer by Nestharus
19
*           - https://wc3modding.info/4607/unitindexer/
20
*
21
*       Optional:
22
*       ---------
23
*
24
*           - Table by Bribe
25
*               - https://wc3modding.info/4611/snippet-new-table/
26
*
27
*   API:
28
*   ----
29
*
30
*       - struct FlyHeight extends array
31
*
32
*           - static method operator [] takes unit u returns thistype
33
*               - Returns an instance of the struct given a unit
34
*
35
*           - static method setDefault takes integer unitType, real flyHeight returns nothing
36
*               - When a unit of a certain type enters the map, his height will be set
37
*                 to a default value input by the user using this function.
38
*
39
*           - method operator height takes nothing returns real
40
*               - Returns the height of a unit
41
*
42
*           - method operator height= takes real newHeight returns nothing
43
*               - Sets the height of a unit
44
*
45
*************************************/
46
library FlyHeight requires UnitIndexer, optional Table
47
    
48
    struct FlyHeight extends array
49
        private static real array h
50
        
51
        static if LIBRARY_Table then
52
            private static key defaultK
53
            private static Table default = defaultK
54
        else
55
            private static hashtable default = InitHashtable()
56
        endif
57
        
58
        method operator height takes nothing returns real
59
            return h[this]
60
        endmethod
61
        
62
        static method setDefault takes integer unitType, real defaultHeight returns nothing
63
            static if LIBRARY_Table then
64
                set default.real[unitType] = defaultHeight
65
            else
66
                call SaveReal(default, unitType, 0, defaultHeight)
67
            endif
68
        endmethod
69
        
70
        private static method haveDefault takes integer unitType returns boolean
71
            static if LIBRARY_Table then
72
                return default.real.has(unitType)
73
            else
74
                return HaveSavedReal(default, unitType, 0)
75
            endif
76
        endmethod
77
        
78
        private static method index takes nothing returns nothing
79
            local integer unitType = GetUnitTypeId(GetIndexedUnit())
80
            
81
            if haveDefault(unitType) then
82
                static if not LIBRARY_AutoFly then
83
                    if UnitAddAbility(GetIndexedUnit(), 'Amrf') then
84
                        call UnitRemoveAbility(GetIndexedUnit(), 'Amrf')
85
                    endif
86
                endif
87
                static if LIBRARY_Table then
88
                    set h[GetIndexedUnitId()] = default.real[unitType]
89
                else
90
                    set h[GetIndexedUnitId()] = LoadReal(default, unitType, 0)
91
                endif
92
                call SetUnitFlyHeight(GetIndexedUnit(), h[GetIndexedUnitId()], 0)
93
            endif
94
        endmethod
95
        
96
        implement UnitIndexStruct
97
        
98
        method operator height= takes real newHeight returns nothing
99
            if h[this] != newHeight then
100
                set h[this] = newHeight
101
                call SetUnitFlyHeight(this.unit, newHeight, 0)
102
            endif
103
        endmethod
104
    endstruct
105
    
106
endlibrary


Demo

Code (jass) Select
1
struct Demo extends array
2
    private static method onInit takes nothing returns nothing
3
        local unit peasant
4
        local unit hawk
5
            
6
        call FlyHeight.setDefault('hpea', 200)
7
        call FlyHeight.setDefault('hdhw', 150)
8
            
9
        set peasant = CreateUnit(Player(0), 'hpea', -256, 0, 270)
10
        set hawk = CreateUnit(Player(0), 'hdhw', 256, 0, 270)
11
            
12
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, R2SW(FlyHeight[peasant].height, 5, 1))
13
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, R2SW(FlyHeight[hawk].height, 5, 1))
14
            
15
        call TriggerSleepAction(5)
16
            
17
        set FlyHeight[peasant].height = 900
18
        set FlyHeight[hawk].height = 2
19
            
20
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, R2SW(FlyHeight[peasant].height, 5, 1))
21
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, R2SW(FlyHeight[hawk].height, 5, 1))
22
            
23
        set peasant = null
24
        set hawk = null
25
    endmethod
26
endstruct


What do you think? :D
14 years
Hmmm, any possiblity of a test map??
14 years
Nahh i search for this!! + REPT!!
14 years
There we go, I attached a map ^_^

By the way, I am never going to approve my own code snippets because that just isn't right :D
14 years
Quote from: Magtheridon96 on August 15, 2012, 10:23:46 PM
There we go, I attached a map ^_^

By the way, I am never going to approve my own code snippets because that just isn't right :D

And who told you that I'm going to impose that?? That's the reason that I'm here heheeee!!

And approvezorded!!!
14 years
But there are any spell's here?? ;) ;)