[System] Track

Scripts

14 years
edited 8 years
This manages trackable objects in such a way that it actually improves functionality and has an interface to make even the toughest projects very readable.

It allows you to:
- Create trackables for specific players. (without desyncs)
- Easily register a trackable click and trackable hover.
- Retrieve which player clicked the trackable.
- Retrieve trackable information - the model, x, y, z,  and facing.

Trackable2 allows for similar functionality, but this one is improved in both speed and handle efficiency. There are some cases where trackable2 has unnecessary lag/freezes, but the Track library fixes it.

This is one of the only systems I haven't completely rewritten time and time again. :) 

This is the same thing as the hive version but I improved the documentation a bit for readability.

Code (jass) Select
1
library Track /* v3.0.1.0
2
*************************************************************************************
3
*
4
*   Manages trackable objects, allowing for easy event registrations, data retrieval,
5
*   and adds the capability of retrieving which player interacted with the trackable.    
6
*
7
*************************************************************************************
8
*
9
*   */uses/*
10
*   
11
*       */ Table /*       hiveworkshop.com/forums/jass-functions-413/snippet-new-table-188084/
12
*
13
************************************************************************************
14
*
15
*   SETTINGS
16
*/
17
globals
18
    private constant integer PLATFORM = 'Otip'
19
endglobals
20
/*
21
************************************************************************************
22
*
23
*    Functions
24
*
25
*        function CreateTrack takes string modelPath, real x, real y, real z, real facing returns Track
26
*            - Creates a trackable of "modelPath" at the coordinates ( x, y, z ) with 
27
*            - "facing" in degrees.
28
*        function CreateTrackForPlayer takes string modelPath, real x, real y, real z, real facing returns Track
29
*            - Same as the function above, except it will create it for only one player.
30
*
31
*        function RegisterAnyClickEvent takes code c returns nothing
32
*            - The code will be executed every time a trackable is clicked.
33
*        function RegisterAnyHoverEvent takes code c returns nothing
34
*            - The code will be executed every time a trackable is hovered.
35
*        function RegisterClickEvent takes Track obj, code c returns nothing
36
*            - The code will be executed every time a trackable of the instance
37
*            - "obj" is clicked.
38
*        function RegisterHoverEvent takes Track obj, code returns nothing
39
*            - The code will be executed every time a trackable of the instance
40
*            - "obj" is hovered.
41
*
42
*        function EnableTrackInstance takes Track obj, boolean flag returns nothing
43
*            - If an instance is enabled, it will enable its events to fire.
44
*            - All instances are enabled by default.
45
*        function IsTrackInstanceEnabled takes Track obj returns boolean
46
*            - Returns whether or not an instance has its events enabled.
47
*
48
*    Event Responses
49
*
50
*        function GetTriggerTrackInstance takes nothing returns Track
51
*            - Returns the Track instance that had a player interaction.
52
*        function GetTriggerTrackable takes nothing returns trackable
53
*            - Returns the trackable object that had a player interaction.
54
*        function GetTriggerTrackablePlayer takes nothing returns player
55
*            - Returns the player that had interacted with the trackable object.
56
*
57
*******************************************************************
58
*
59
*   struct Track
60
*
61
*        static Track instance
62
*           - The triggering instance of the event.
63
*        static trackable object
64
*           - The triggering trackable object of the event.
65
*        static player tracker
66
*           - The player who interacted with the trackable object of the event.
67
*
68
*        readonly real x
69
*        readonly real y
70
*        readonly real z
71
*           - The coordinates of the trackable object.
72
*        readonly real facing
73
*           - The facing angle of the trackable object.
74
*        readonly string model
75
*           - The string path of the trackable object.
76
*
77
*        method operator enabled takes nothing returns boolean
78
*        
79
*        static method create takes string modelPath, real x, real y, real z, real facing returns Track
80
*        static method createForPlayer takes string modelPath, real x, real y, real z, real facing, player p returns Track
81
*
82
*        static method registerAnyClick takes code c returns nothing
83
*        static method registerAnyHover takes code c returns nothing
84
*
85
*        method registerOnClick takes code c returns nothing
86
*        method registerOnHover takes code c returns nothing
87
*
88
*        method enable takes nothing returns nothing
89
*        method disable takes nothing returns nothing
90
*
91
*            - All of the above are the struct interface equivalents of the functions.
92
*
93
********************************************************************
94
*    
95
*    Credits
96
*       - Azlier for Trackable2
97
*       - Arhowk for finding a bug for me
98
*
99
********************************************************************/
100
    
101
    private module Init
102
        private static method onInit takes nothing returns nothing
103
            set thistype.TrackTable     = Table.create()
104
        endmethod
105
    endmodule
106
107
    struct Track extends array
108
        private static trigger anyClick = CreateTrigger()
109
        private static trigger anyHover = CreateTrigger()
110
        private static Table TrackTable = 0
111
        
112
        static thistype  instance = 0
113
        static trackable object   = null
114
        static player    tracker  = null
115
        
116
        private static integer ic = 0
117
        private static integer ir = 0
118
        private thistype rn
119
        
120
        readonly real    x
121
        readonly real    y
122
        readonly real    z
123
        readonly real    facing
124
        readonly string  model
125
        
126
        private boolean  flag
127
        private trigger  reg
128
        private trigger  onClick
129
        private trigger  onHover
130
        
131
        static method registerAnyClick takes code c returns nothing
132
            call TriggerAddCondition(.anyClick, Filter(c))
133
        endmethod
134
        static method registerAnyHover takes code c returns nothing
135
            call TriggerAddCondition(.anyHover, Filter(c))
136
        endmethod
137
        
138
        method registerClick takes code c returns nothing
139
            if .onClick == null then
140
                set .onClick = CreateTrigger()
141
            endif
142
            call TriggerAddCondition(.onClick, Filter(c))
143
        endmethod
144
        method registerHover takes code c returns nothing
145
            if .onHover == null then
146
                set .onHover = CreateTrigger()
147
            endif
148
            call TriggerAddCondition(.onHover, Filter(c))
149
        endmethod
150
        
151
        method destroy takes nothing returns nothing
152
            call TrackTable.remove(GetHandleId(.reg))
153
            call TrackTable.remove(GetHandleId(.object))
154
            call DestroyTrigger(.reg)
155
            call DestroyTrigger(.onClick)
156
            call DestroyTrigger(.onHover)
157
            set .rn = ir
158
            set ir  = this
159
        endmethod
160
        
161
        method enable takes nothing returns nothing
162
            set this.flag = true
163
        endmethod
164
        method disable takes nothing returns nothing
165
            set this.flag = false
166
        endmethod
167
        method operator enabled takes nothing returns boolean
168
            return this.flag
169
        endmethod
170
        
171
        private static method onInteract takes nothing returns boolean
172
            local thistype  temp = instance
173
            local trackable tr   = object
174
            local player    p    = tracker
175
            
176
            set instance = TrackTable[GetHandleId(GetTriggeringTrigger())]
177
            set object   = GetTriggeringTrackable()
178
            set tracker  = Player(TrackTable[GetHandleId(object)])
179
            
180
            if instance.flag then
181
                if GetTriggerEventId() == EVENT_GAME_TRACKABLE_TRACK then
182
                    call TriggerEvaluate(instance.onHover)
183
                    call TriggerEvaluate(anyHover)
184
                else
185
                    call TriggerEvaluate(instance.onClick)
186
                    call TriggerEvaluate(anyClick)
187
                endif
188
            endif
189
            
190
            set instance = temp
191
            set tracker  = p
192
            set object   = tr
193
            set tr = null
194
            set p  = null
195
            return false
196
        endmethod
197
        
198
        private static method createTrack takes string modelPath, real x, real y, real z, real facing, player j returns thistype
199
            local destructable dest = null
200
            local thistype     this = ir
201
            local integer      i    = 11
202
            local trackable tr 
203
            local player p
204
            local string s
205
            if this == 0 then
206
                set ic   = ic + 1
207
                set this = ic
208
            else
209
                set ir   = .rn
210
            endif
211
            if z != 0 then
212
                set dest = CreateDestructableZ(PLATFORM, x, y, z, 0, 1, 0)
213
            endif
214
            if j != null then
215
                set i    = GetPlayerId(j)
216
            endif
217
            set .x = x 
218
            set .y = y
219
            set .z = z
220
            set .flag    = true
221
            set .facing  = facing
222
            set .model   = modelPath
223
            set .reg     = CreateTrigger()
224
            set .onClick = null
225
            set .onHover = null
226
            set TrackTable[GetHandleId(.reg)] = this
227
            call TriggerAddCondition(.reg, Condition(function thistype.onInteract))
228
            loop
229
                set p = Player(i)
230
                if GetPlayerSlotState(p) == PLAYER_SLOT_STATE_PLAYING and GetPlayerController(p) == MAP_CONTROL_USER then
231
                    if GetLocalPlayer() == p then
232
                        set s = modelPath
233
                    else
234
                        set s = ""
235
                    endif 
236
                    set tr = CreateTrackable(s, .x, .y, .facing)
237
                    call TriggerRegisterTrackableHitEvent(.reg, tr)
238
                    call TriggerRegisterTrackableTrackEvent(.reg, tr)
239
                    set TrackTable[GetHandleId(tr)] = i
240
                    if j != null then
241
                        exitwhen true
242
                    endif
243
                endif
244
                exitwhen i == 0
245
                set i = i - 1
246
            endloop
247
            if dest != null then
248
                call RemoveDestructable(dest)
249
                set dest = null
250
            endif
251
            set p  = null
252
            set tr = null
253
            return this
254
        endmethod
255
        
256
        static method create takes string modelPath, real x, real y, real z, real facing returns thistype
257
            return thistype.createTrack(modelPath, x, y, z, facing, null)
258
        endmethod 
259
        
260
        static method createForPlayer takes string modelPath, real x, real y, real z, real facing, player p returns thistype
261
            if not (GetPlayerSlotState(p) == PLAYER_SLOT_STATE_PLAYING and GetPlayerController(p) == MAP_CONTROL_USER) then
262
                return 0
263
            endif
264
            return thistype.createTrack(modelPath, x, y, z, facing, p)
265
        endmethod
266
        
267
        implement Init
268
    endstruct
269
    
270
    function CreateTrack takes string modelPath, real x, real y, real z, real facing returns Track
271
        return Track.create(modelPath, x, y, z, facing)
272
    endfunction
273
    
274
    function CreateTrackForPlayer takes string modelPath, real x, real y, real z, real facing, player who returns Track 
275
        return Track.createForPlayer(modelPath, x, y, z, facing, who)
276
    endfunction
277
    
278
    function EnableTrackInstance takes Track instance, boolean flag returns nothing
279
        if flag then
280
            call instance.enable()
281
        else
282
            call instance.disable()
283
        endif
284
    endfunction
285
    
286
    function IsTrackInstanceEnabled takes Track instance returns boolean
287
        return instance.enabled
288
    endfunction
289
    
290
    function RegisterAnyClickEvent takes code c returns nothing
291
        call Track.registerAnyClick(c)
292
    endfunction
293
    
294
    function RegisterAnyHoverEvent takes code c returns nothing
295
        call Track.registerAnyHover(c)
296
    endfunction
297
    
298
    function RegisterClickEvent takes Track obj, code c returns nothing
299
        call obj.registerClick(c)
300
    endfunction
301
    
302
    function RegisterHoverEvent takes Track obj, code c returns nothing
303
        call obj.registerHover(c)
304
    endfunction
305
    
306
    function GetTriggerTrackInstance takes nothing returns Track
307
        return Track.instance
308
    endfunction
309
    
310
    function GetTriggerTrackable takes nothing returns trackable
311
        return Track.object
312
    endfunction
313
    
314
    function GetTriggerTrackablePlayer takes nothing returns player
315
        return Track.tracker
316
    endfunction
317
endlibrary


API List:
Spoiler
Code (jass) Select
1
function CreateTrack takes string modelPath, real x, real y, real z, real facing returns Track
2
3
function CreateTrackForPlayer takes string modelPath, real x, real y, real z, real facing returns Track
4
5
function RegisterAnyClickEvent takes code c returns nothing
6
7
function RegisterAnyHoverEvent takes code c returns nothing
8
9
function RegisterClickEvent takes Track obj, code c returns nothing
10
11
function RegisterHoverEvent takes Track obj, code c returns nothing
12
13
function EnableTrackInstance takes Track obj, boolean flag returns nothing
14
15
function IsTrackInstanceEnabled takes Track obj returns boolean
16
17
function GetTriggerTrackInstance takes nothing returns Track
18
19
function GetTriggerTrackable takes nothing returns trackable
20
21
function GetTriggerTrackablePlayer takes nothing returns player

If you need a sample, then check out the demo map below.
  • Track.w3x (35.97 KB)
14 years
The system has a very nice presentation, I like the implementation and the test map is lovely. Approvezord'ed
14 years
Haha.. Nice, why PnF inactive on Hive??
I think Hive are suck now ( BECAUSE TO ARROGANT!! ) :) :)
PS:
moyack, I added you to my credit list ( Every map i was made ) Without reason..
14 years
Because you made this helping site  :P  :P