Jass tags... bug reporting here

General Jass Discussion

14 years
Here we can test the jass tags and report any issue with the style of it.

let's start...
Code (jass) Select
1
library Lightning requires optional ARGB
2
3
//******************************************************************************
4
//* Author: Fledermaus
5
//* Version: 1.02
6
//* 
7
//* This library is intended to replace the native lightning type.
8
//* It uses a Lightning struct to provide better functionality (and a nicer
9
//* syntax if you're that way inclined).
10
//* 
11
//******************************************************************************
12
//*
13
//* Configuration constants that you can alter however you like:
14
//* 
15
//*   TIMEOUT is the timer speed for updating the position of lightning and
16
//*             the alpha for fading in/out.
17
//* 
18
//******************************************************************************
19
//* 
20
//* List of methods:
21
//* 
22
//*   static method create takes string codeName, boolean checkVisibility, real fadeTime returns Lightning
23
//* 
24
//* Creates a new Lightning for you to use.
25
//*   - codeName: Determines what the Lightning looks like. I'm even so nice as
26
//*     to include a constant list of them in the global constant section for
27
//*     you!
28
//*   - checkVisibility: As with normal lightning, this determines if a player
29
//*     needs to have vision of the Lightning (start or end point/unit) in
30
//*     order to see it. If set to true, the Lightning becomes invisible when
31
//*     players lose vision of it.
32
//*   - fadeTime: Makes the Lightning fade into existence. A value of 0 is an
33
//*     instant fade in.
34
//* Note that it wont initially be attached to anything, that's where the next
35
//* two methods come in:
36
//* 
37
//* 
38
//*   method attachToPoint takes boolean atStart, real x, real y, real z, boolean considerTerrain returns boolean
39
//*   method attachToUnit takes boolean atStart, unit u, real zOffset returns boolean
40
//* 
41
//* They should be pretty self-explanatory, they attach the Lightning to either 
42
//* the startPoint or startUnit, or the endPoint or endUnit.
43
//* The two constants LIGHTNING_START and LIGHTNING_END can be used here to
44
//* help make your code easier to read.
45
//* Lightning attached to a unit(s) will follow it around.
46
//* The boolean considerTerrain in attachToPoint determines if the z value is
47
//* measured from the terrain or an absolute value.
48
//* If you don't attach to both a start and end point/unit, the Lightning will
49
//* connect to 0, 0. So you should attach to both as soon as it's created.
50
//* 
51
//* 
52
//*   method recolor takes real r, real g, real b, real a returns boolean
53
//*   method ARGBrecolor takes ARGB color returns nothing
54
//*   method operator color= takes ARGB color returns nothing
55
//* 
56
//* Used to set the colour of Lightning. Should only use values between 0.00
57
//* and 1.00.
58
//* ARGBrecolor and color= will obviously only work if you have ARGB.
59
//* 
60
//* 
61
//*   method operator red takes nothing returns real
62
//*   method operator green takes nothing returns real
63
//*   method operator blue takes nothing returns real
64
//*   method operator alpha takes nothing returns real
65
//*   method operator color takes nothing returns ARGB
66
//* 
67
//* Method operators to get the colour components of the Lightning. Pretty
68
//* self explanatory.
69
//* Again, color will only work if you have ARGB.
70
//* 
71
//* 
72
//*   method destroy takes nothing returns nothing
73
//*   method release takes real fadeTime returns nothing
74
//*   method releaseDelayed takes real delay, real fadeTime returns nothing
75
//* 
76
//* Again, pretty self explanatory. destroy will dispose of a Lightning
77
//* instantly, while release will fade it out over time. releaseDelayed lets
78
//* you release it after a delay.
79
//* 
80
//******************************************************************************
81
//*
82
//* There is the inherent limit that you can only have 8191 Lightning at once.
83
//* However, if you ever get this high, your map will have worse problems than
84
//* this system working improperly.
85
//* 
86
//******************************************************************************
87
//*
88
//* Credits: Thanks to Vexorian for ARGB and vJass. Kueken for the work he
89
//*          previously did which helped me create this library. Anitarf for
90
//*          motivating me to update it and helping me with some of the
91
//*          ideas. Oh and Dusk for being sexy ;)
92
//* 
93
//* If you have any further questions regarding Lightning or how to use it,
94
//* feel free to visit [url=http://www.wc3c.net]www.wc3c.net[/url] and ask questions there. This library should
95
//* only ever be released at WC3C and at no other site. Please give credits if
96
//* this library finds its way into your maps, and otherwise thanks for reading!
97
//* 
98
99
globals
100
    private constant real TIMEOUT = 0.03125 //32 fps
101
    
102
    //* Do not edit below here
103
    
104
    //Lightning type constants
105
            constant string   LIGHTNING_AERIAL_SHACKLES           = "LEAS"
106
            constant string   LIGHTNING_CHAIN_LIGHTNING_PRIMARY   = "CLPB"
107
            constant string   LIGHTNING_CHAIN_LIGHTNING_SECONDARY = "CLSB"
108
            constant string   LIGHTNING_DRAIN_LIFE                = "DRAL"
109
            constant string   LIGHTNING_DRAIN_LIFE_AND_MANA       = "DRAB"
110
            constant string   LIGHTNING_DRAIN_MANA                = "DRAM"
111
            constant string   LIGHTNING_FINGER_OF_DEATH           = "AFOD"
112
            constant string   LIGHTNING_FORKED_LIGHTNING          = "FORK"
113
            constant string   LIGHTNING_HEALING_WAVE_PRIMARY      = "HWPB"
114
            constant string   LIGHTNING_HEALING_WAVE_SECONDARY    = "HWSB"
115
            constant string   LIGHTNING_LIGHTNING_ATTACK          = "CHIM"
116
            constant string   LIGHTNING_MANA_BURN                 = "MBUR"
117
            constant string   LIGHTNING_MANA_FLARE                = "MFPB"
118
            constant string   LIGHTNING_SPIRIT_LINK               = "SPLK"
119
            constant boolean  LIGHTNING_START                     = true
120
            constant boolean  LIGHTNING_END                       = false
121
    
122
    private location        Loc   = Location(0., 0.)
123
    private Lightning array Array
124
    private integer         Index = 0
125
    private timer           Timer = CreateTimer()
126
endglobals
127
128
//! textmacro LightningDebugTextmacro takes TYPE, RETURN
129
    if .released then
130
        debug call BJDebugMsg(SCOPE_PREFIX + " Error: Trying to $TYPE$ a lightning that is fading out of existance")
131
        return $RETURN$
132
    endif
133
//! endtextmacro
134
135
//! textmacro LightningRecolourDebugTextmacro takes VARIABLE, COLOUR
136
    if $VARIABLE$ < 0. then
137
        debug call BJDebugMsg(SCOPE_PREFIX + " Error: $COLOUR$ value passed to recolor is less than 0.")
138
        set $VARIABLE$ = 0.
139
    elseif $VARIABLE$ > 1. then
140
        debug call BJDebugMsg(SCOPE_PREFIX + " Error: $COLOUR$ value passed to recolor is greater than 1.")
141
        set $VARIABLE$ = 1.
142
    endif
143
//! endtextmacro
144
145
//******************************************************************************
146
147
struct Lightning
148
    private lightning lightning
149
    private boolean   checkVisibility
150
    private boolean   updatePosition
151
    private boolean   released
152
    private boolean   canSee
153
    private boolean   startSet
154
    private boolean   endSet
155
    private unit      startUnit
156
    private unit      endUnit
157
    private real      startX
158
    private real      startY
159
    private real      startZ
160
    private real      startZOffset
161
    private real      endX
162
    private real      endY
163
    private real      endZ
164
    private real      endZOffset
165
    private real      redColour
166
    private real      greenColour
167
    private real      blueColour
168
    private real      alphaColour
169
    private real      fadingAlpha
170
    private real      fadingAlphaInc
171
    private real      releaseDelay
172
    
173
    private static method Update takes nothing returns nothing
174
        local integer   i = 0
175
        local real      a
176
        local Lightning l
177
        
178
        loop
179
          exitwhen i >= Index
180
            set l = Array[i]
181
            
182
            if l.updatePosition then
183
                //Update the lightning's position if it's attached to a unit
184
                if l.startUnit != null and GetUnitTypeId(l.startUnit) != 0 then
185
                    set l.startX = GetUnitX(l.startUnit)
186
                    set l.startY = GetUnitY(l.startUnit)
187
                    call MoveLocation(Loc, l.startX, l.startY)
188
                    set l.startZ = GetLocationZ(Loc) + GetUnitFlyHeight(l.startUnit) + l.startZOffset
189
                endif
190
                if l.endUnit != null and GetUnitTypeId(l.endUnit) != 0 then
191
                    set l.endX = GetUnitX(l.endUnit)
192
                    set l.endY = GetUnitY(l.endUnit)
193
                    call MoveLocation(Loc, l.endX, l.endY)
194
                    set l.endZ = GetLocationZ(Loc) + GetUnitFlyHeight(l.endUnit) + l.endZOffset
195
                endif
196
                
197
                //Check if the lightning can be seen (will locally change the variable for each player)
198
                set l.canSee = MoveLightningEx(l.lightning, l.checkVisibility, l.startX, l.startY, l.startZ, l.endX, l.endY, l.endZ)
199
                if l.canSee then
200
                    //If it can be seen, make sure you change the alpha back to l.alpha
201
                    //because this could have been the first time it's visiable again
202
                    set a = l.alphaColour
203
                else
204
                    //Otherwise make the alpha 0 so it's invisiable
205
                    set a = 0.
206
                endif
207
                
208
                //Check if the lightning has been delay released
209
                if l.releaseDelay > 0. and not l.released then
210
                    //Count down the delay
211
                    set l.releaseDelay = l.releaseDelay - TIMEOUT
212
                    if l.releaseDelay <= 0. then
213
                        //Time to release it
214
                        set l.released = true
215
                        if l.fadingAlphaInc < 1. then
216
                            //Increase the fadingAlpha by fadingAlphaInc to account for this cycle
217
                            set l.fadingAlpha = 1. + l.fadingAlphaInc
218
                        else
219
                            //If the fade time from the delayed release was 0., release it instantly
220
                            set l.fadingAlpha = 0.
221
                        endif
222
                    endif
223
                endif
224
                
225
                if l.released then
226
                    if l.fadingAlpha > 0. then
227
                        //Lightning is still fading out
228
                        set l.fadingAlpha = l.fadingAlpha - l.fadingAlphaInc
229
                        if l.fadingAlpha < 0. then
230
                            set l.fadingAlpha = 0.
231
                        endif
232
                        set a = a * l.fadingAlpha
233
                    else
234
                        //Lightning has finished fading out and should be destroyed
235
                        call DestroyLightning(l.lightning)
236
                        set l.lightning = null
237
                        set l.startUnit = null
238
                        set l.endUnit   = null
239
                        call l.deallocate()
240
                        
241
                        set Index = Index - 1
242
                        if Index > 0 then
243
                            set Array[i] = Array[Index]
244
                            set i = i - 1
245
                        else
246
                            call PauseTimer(Timer)
247
                        endif
248
                    endif
249
                elseif l.fadingAlpha < 1. then
250
                    //Lightning is fading in
251
                    set l.fadingAlpha = l.fadingAlpha + l.fadingAlphaInc
252
                    if l.fadingAlpha >= 1. then
253
                        set l.fadingAlpha = 1.
254
                        //Check if we need to keep updating the lightning
255
                        set l.updatePosition = l.checkVisibility or l.startUnit != null or l.endUnit != null or l.releaseDelay > 0.
256
                        if not l.updatePosition then
257
                            set Index = Index - 1
258
                            if Index > 0 then
259
                                set Array[i] = Array[Index]
260
                                set i = i - 1
261
                            else
262
                                call PauseTimer(Timer)
263
                            endif
264
                        endif
265
                    endif
266
                    
267
                    set a = a * l.fadingAlpha
268
                endif
269
                
270
                //If the lightning still exists, update it's alpha value
271
                if l.lightning != null then
272
                    call SetLightningColor(l.lightning, l.redColour, l.greenColour, l.blueColour, a)
273
                endif
274
                
275
                set i = i + 1
276
            else
277
                set Index = Index - 1
278
                if Index > 0 then
279
                    set Array[i] = Array[Index]
280
                else
281
                    call PauseTimer(Timer)
282
                endif
283
            endif
284
        endloop
285
    endmethod
286
    
287
    static method create takes string codeName, boolean checkVisibility, real fadeTime returns Lightning
288
        local Lightning this = Lightning.allocate()
289
        
290
        set .checkVisibility = checkVisibility
291
        set .updatePosition  = checkVisibility or fadeTime > 0.
292
        set .released        = false
293
        set .canSee          = true
294
        //The lightning needs to first be create so that noone can see it but without checking that players
295
        //can see it (checkVisibitiy set to false and at (0, 0) (0, 0)) so that it will not be null for
296
        //players that cannot see it as it's created. It can then be moved to the correct location and have
297
        //the proper visibility setting.
298
        set .lightning       = AddLightning(codeName, false, 0., 0., 0., 0.)
299
        set .redColour       = GetLightningColorR(.lightning)
300
        set .greenColour     = GetLightningColorG(.lightning)
301
        set .blueColour      = GetLightningColorB(.lightning)
302
        set .alphaColour     = GetLightningColorA(.lightning)
303
        set .startSet        = false
304
        set .endSet          = false
305
        set .releaseDelay    = 0.
306
        
307
        if fadeTime > 0. then
308
            set .fadingAlpha    = 0.
309
            set .fadingAlphaInc = TIMEOUT / fadeTime
310
            call SetLightningColor(.lightning, .redColour, .greenColour, .blueColour, 0.)
311
        else
312
            debug if fadeTime != 0. then
313
                debug call BJDebugMsg(SCOPE_PREFIX + " Error: Trying to create a lightning with a negative fade time")
314
            debug endif
315
            set .fadingAlpha = 1.
316
        endif
317
        
318
        if .updatePosition then
319
            if Index == 0 then
320
                call TimerStart(Timer, TIMEOUT, true, function Lightning.Update)
321
            endif
322
            set Array[Index] = this
323
            set Index = Index + 1
324
        endif
325
        
326
        return this
327
    endmethod
328
    
329
    method attachToPoint takes boolean atStart, real x, real y, real z, boolean considerTerrain returns boolean
330
        //! runtextmacro LightningDebugTextmacro("change the attached point of", "false")
331
        
332
        call MoveLocation(Loc, x, y)
333
        if atStart then
334
            set .startUnit = null
335
            set .startX    = x
336
            set .startY    = y
337
            if considerTerrain then
338
                set .startZ = GetLocationZ(Loc) + z
339
            else
340
                set .startZ = z
341
            endif
342
            set .startSet  = true
343
        else
344
            set .endUnit = null
345
            set .endX    = x
346
            set .endY    = y
347
            if considerTerrain then
348
                set .endZ = GetLocationZ(Loc) + z
349
            else
350
                set .endZ = z
351
            endif
352
            set .endSet  = true
353
        endif
354
        
355
        if .startSet and .endSet then
356
            set .canSee = MoveLightningEx(.lightning, .checkVisibility, .startX, .startY, .startZ, .endX, .endY, .endZ)
357
        endif
358
        
359
        if .updatePosition and not .checkVisibility and .fadingAlpha == 1. and .startUnit == null and .endUnit == null and .releaseDelay <= 0. then
360
            set .updatePosition = false
361
        endif
362
        
363
        return true
364
    endmethod
365
    
366
    method attachToUnit takes boolean atStart, unit u, real zOffset returns boolean
367
        //! runtextmacro LightningDebugTextmacro("change the attached unit of", "false")
368
        
369
        if atStart then
370
            set .startUnit    = u
371
            set .startZOffset = zOffset
372
            set .startSet     = true
373
            if u != null then
374
                set .startX = GetUnitX(u)
375
                set .startY = GetUnitY(u)
376
                call MoveLocation(Loc, .startX, .startY)
377
                set .startZ = GetLocationZ(Loc) + GetUnitFlyHeight(u) + zOffset
378
            endif
379
        else
380
            set .endUnit    = u
381
            set .endZOffset = zOffset
382
            set .endSet     = true
383
            if u != null then
384
                set .endX = GetUnitX(u)
385
                set .endY = GetUnitY(u)
386
                call MoveLocation(Loc, .endX, .endY)
387
                set .endZ = GetLocationZ(Loc) + GetUnitFlyHeight(.endUnit) + zOffset
388
            endif
389
        endif
390
        
391
        if .startSet and .endSet then
392
            set .canSee = MoveLightningEx(.lightning, .checkVisibility, .startX, .startY, .startZ, .endX, .endY, .endZ)
393
        endif
394
        
395
        if .updatePosition == false then
396
            if Index == 0 then
397
                call TimerStart(Timer, TIMEOUT, true, function Lightning.Update)
398
            endif
399
            set Array[Index] = this
400
            set Index = Index + 1
401
            set .updatePosition = true
402
        endif
403
        
404
        return true
405
    endmethod
406
    
407
    method operator red takes nothing returns real
408
        return .redColour
409
    endmethod
410
    
411
    method operator green takes nothing returns real
412
        return .greenColour
413
    endmethod
414
    
415
    method operator blue takes nothing returns real
416
        return .blueColour
417
    endmethod
418
    
419
    method operator alpha takes nothing returns real
420
        return .alphaColour
421
    endmethod
422
    
423
    method recolor takes real r, real g, real b, real a returns boolean
424
        //! runtextmacro LightningDebugTextmacro("recolor", "false")
425
        //! runtextmacro LightningRecolourDebugTextmacro("r", "red")
426
        //! runtextmacro LightningRecolourDebugTextmacro("g", "green")
427
        //! runtextmacro LightningRecolourDebugTextmacro("b", "blue")
428
        //! runtextmacro LightningRecolourDebugTextmacro("a", "alpha")
429
    
430
        set .redColour   = r
431
        set .greenColour = g
432
        set .blueColour  = b
433
        set .alphaColour = a
434
        
435
        if .canSee == false then
436
            set a = 0.
437
        endif
438
        
439
        return SetLightningColor(.lightning, r, g, b, a * .fadingAlpha)
440
    endmethod
441
    
442
    static if LIBRARY_ARGB then    
443
    method ARGBrecolor takes ARGB color returns nothing
444
        local integer col   = integer(color)
445
        local integer a
446
        local integer r
447
        local integer g
448
        local integer b
449
        local real    alpha
450
        
451
        if col < 0 then
452
            set col = -(-col + 0x80000000)
453
            set a   = 0x80 + col / 0x1000000
454
            set col = col - (a - 0x80) * 0x1000000
455
        else
456
            set a   = col / 0x1000000
457
            set col = col - a * 0x1000000
458
        endif
459
        
460
        set r   = col / 0x10000
461
        set col = col - r * 0x10000
462
        set g   = col / 0x100
463
        set b   = col - g * 0x100
464
        
465
        set .redColour   = I2R(r) / 255.
466
        set .greenColour = I2R(g) / 255.
467
        set .blueColour  = I2R(b) / 255.
468
        set .alphaColour = I2R(a) / 255.
469
        
470
        if .canSee then
471
            set alpha = .alphaColour
472
        else
473
            set alpha = 0.
474
        endif
475
        
476
        call SetLightningColor(.lightning, .redColour, .greenColour, .blueColour, alpha * .fadingAlpha)
477
    endmethod
478
    
479
    method operator color takes nothing returns ARGB
480
        return ARGB.create(R2I(.alphaColour * 255.), R2I(.redColour * 255.), R2I(.blueColour * 255.), R2I(.greenColour * 255.))
481
    endmethod
482
    
483
    method operator color= takes ARGB color returns nothing
484
        call .ARGBrecolor(color)
485
    endmethod
486
    endif
487
    
488
    method release takes real fadeTime returns nothing
489
        //! runtextmacro LightningDebugTextmacro("release", "")
490
        if fadeTime < 0. then
491
            debug call BJDebugMsg(SCOPE_PREFIX + " Error: Trying to release a lightning with a negative fade time")
492
            set fadeTime = 0.
493
        endif
494
        
495
        set .released = true
496
        if fadeTime > 0. then
497
            set .fadingAlpha    = 1.
498
            set .fadingAlphaInc = TIMEOUT / fadeTime
499
            
500
            //Add it to the Update Loop if it's not already in there
501
            if not .updatePosition then
502
                if Index == 0 then
503
                    call TimerStart(Timer, TIMEOUT, true, function Lightning.Update)
504
                endif
505
                set Array[Index] = this
506
                set Index = Index + 1
507
                set .updatePosition = true
508
            endif
509
        elseif .updatePosition then
510
            //It's already updating so just make it invisible now and it'll get cleaned up in the next loop
511
            set .fadingAlpha = 0.
512
            call SetLightningColor(.lightning, .redColour, .blueColour, .greenColour, 0.)
513
        else
514
            //It's not updating so it can be cleaned up now
515
            call DestroyLightning(.lightning)
516
            set .lightning = null
517
            set .startUnit = null
518
            set .endUnit   = null
519
            call .deallocate()
520
        endif
521
    endmethod
522
    
523
    method destroy takes nothing returns nothing
524
        call .release(0.)
525
    endmethod
526
    
527
    method releaseDelayed takes real delay, real fadeTime returns nothing
528
        //! runtextmacro LightningDebugTextmacro("delayed release", "")
529
        if delay <= 0. then
530
            debug call BJDebugMsg(SCOPE_PREFIX + " Error: Trying to delayed release a lightning with a delay not greater than 0.")
531
            set delay = 0.01
532
        endif
533
        if fadeTime < 0. then
534
            debug call BJDebugMsg(SCOPE_PREFIX + " Error: Trying to delayed release a lightning with a negative fade time")
535
            set fadeTime = 0.
536
        endif
537
        
538
        set .releaseDelay = delay
539
        if fadeTime > 0. then
540
            set .fadingAlphaInc = TIMEOUT / fadeTime
541
        else
542
            //Don't want to divide by 0! 
543
            set .fadingAlphaInc = 1.
544
        endif
545
        
546
        //Add it to the Update Loop if it's not already in there
547
        if not .updatePosition then
548
            if Index == 0 then
549
                call TimerStart(Timer, TIMEOUT, true, function Lightning.Update)
550
            endif
551
            set Array[Index] = this
552
            set Index = Index + 1
553
            set .updatePosition = true
554
        endif
555
    endmethod
556
endstruct
557
558
endlibrary
559
560


code source<<<<<. http://www.wc3c.net/showpost.php?p=1134326&postcount=1
14 years
You should make the script check if "//" is proceeded by a '!' to detect textmacros and color them grey.
Actually, what kind of highlighter are you using? A general syntax highlighter that uses tokens and shit? Or a syntax highlighter coded from scratch?
14 years
Quote from: Magtheridon96 on July 09, 2012, 03:48:06 PM
You should make the script check if "//" is proceeded by a '!' to detect textmacros and color them grey.
Actually, what kind of highlighter are you using? A general syntax highlighter that uses tokens and shit? Or a syntax highlighter coded from scratch?
I use Geshi, because it offer a reasonable way to add features in a... safe way :P

I'll chek how to add this feature, actually I've tried but with no good results :(

14 years
edited 14 years
Then what you need is a custom script highlighter :p
I /might/ make one. Just /might/. (If I learn JavaScript syntax in the next hour, which is very possible, since I learned C# and wrote 2 applications in 5 hours :P)

edit
On second thought, no :P
It would be much simpler to just inject the code on the site ^.^
When you encounter
Code (jass) Select
1
, filter all spaces, and search each line for "//!" as the first 3 characters.
2
When you encounter 
, stop parsing if you encounter that text OUTSIDE of a string :3