14 years
edited 8 years
The documentation has a brief explanation as to why I made this.
Code
Demo
What do you think?
Code
1 /*************************************2 *3 * FlyHeight4 * v1.0.0.15 * By Magtheridon966 *7 * - Warcraft III movement types are pretty8 * bugged. It would be much better to write9 * them from scratch. This system allows10 * you to create flying units with changeable11 * heights.12 *13 * All units must have Movement Type Foot.14 *15 * Requires:16 * ---------17 *18 * - UnitIndexer by Nestharus19 * - https://wc3modding.info/4607/unitindexer/20 *21 * Optional:22 * ---------23 *24 * - Table by Bribe25 * - https://wc3modding.info/4611/snippet-new-table/26 *27 * API:28 * ----29 *30 * - struct FlyHeight extends array31 *32 * - static method operator [] takes unit u returns thistype33 * - Returns an instance of the struct given a unit34 *35 * - static method setDefault takes integer unitType, real flyHeight returns nothing36 * - When a unit of a certain type enters the map, his height will be set37 * to a default value input by the user using this function.38 *39 * - method operator height takes nothing returns real40 * - Returns the height of a unit41 *42 * - method operator height= takes real newHeight returns nothing43 * - Sets the height of a unit44 *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 else55 private static hashtable default = InitHashtable()
56 endif57
58 method operator height takes nothing returns real
59 return h[this]
60 endmethod61
62 static method setDefault takes integer unitType, real defaultHeight returns nothing
63 static if LIBRARY_Table then
64 set default.real[unitType] = defaultHeight
65 else66 call SaveReal(default, unitType, 0, defaultHeight)
67 endif68 endmethod69
70 private static method haveDefault takes integer unitType returns boolean
71 static if LIBRARY_Table then
72 return default.real.has(unitType)
73 else74 return HaveSavedReal(default, unitType, 0)
75 endif76 endmethod77
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 endif86 endif87 static if LIBRARY_Table then
88 set h[GetIndexedUnitId()] = default.real[unitType]
89 else90 set h[GetIndexedUnitId()] = LoadReal(default, unitType, 0)
91 endif92 call SetUnitFlyHeight(GetIndexedUnit(), h[GetIndexedUnitId()], 0)
93 endif94 endmethod95
96 implement UnitIndexStruct97
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 endif103 endmethod104 endstruct105
106 endlibrary
Demo
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 endmethod26 endstruct
What do you think?