14 years
Here we can test the jass tags and report any issue with the style of it.
let's start...
code source<<<<<. http://www.wc3c.net/showpost.php?p=1134326&postcount=1
let's start...
1 library Lightning requires optional ARGB
2 3 //******************************************************************************4 //* Author: Fledermaus5 //* Version: 1.026 //* 7 //* This library is intended to replace the native lightning type.8 //* It uses a Lightning struct to provide better functionality (and a nicer9 //* 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 and16 //* 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 Lightning23 //* 24 //* Creates a new Lightning for you to use.25 //* - codeName: Determines what the Lightning looks like. I'm even so nice as26 //* to include a constant list of them in the global constant section for27 //* you!28 //* - checkVisibility: As with normal lightning, this determines if a player29 //* needs to have vision of the Lightning (start or end point/unit) in30 //* order to see it. If set to true, the Lightning becomes invisible when31 //* players lose vision of it.32 //* - fadeTime: Makes the Lightning fade into existence. A value of 0 is an33 //* instant fade in.34 //* Note that it wont initially be attached to anything, that's where the next35 //* two methods come in:36 //* 37 //* 38 //* method attachToPoint takes boolean atStart, real x, real y, real z, boolean considerTerrain returns boolean39 //* method attachToUnit takes boolean atStart, unit u, real zOffset returns boolean40 //* 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 to44 //* 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 is47 //* measured from the terrain or an absolute value.48 //* If you don't attach to both a start and end point/unit, the Lightning will49 //* 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 boolean53 //* method ARGBrecolor takes ARGB color returns nothing54 //* method operator color= takes ARGB color returns nothing55 //* 56 //* Used to set the colour of Lightning. Should only use values between 0.0057 //* and 1.00.58 //* ARGBrecolor and color= will obviously only work if you have ARGB.59 //* 60 //* 61 //* method operator red takes nothing returns real62 //* method operator green takes nothing returns real63 //* method operator blue takes nothing returns real64 //* method operator alpha takes nothing returns real65 //* method operator color takes nothing returns ARGB66 //* 67 //* Method operators to get the colour components of the Lightning. Pretty68 //* self explanatory.69 //* Again, color will only work if you have ARGB.70 //* 71 //* 72 //* method destroy takes nothing returns nothing73 //* method release takes real fadeTime returns nothing74 //* method releaseDelayed takes real delay, real fadeTime returns nothing75 //* 76 //* Again, pretty self explanatory. destroy will dispose of a Lightning77 //* instantly, while release will fade it out over time. releaseDelayed lets78 //* 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 than84 //* this system working improperly.85 //* 86 //******************************************************************************87 //*88 //* Credits: Thanks to Vexorian for ARGB and vJass. Kueken for the work he89 //* previously did which helped me create this library. Anitarf for90 //* motivating me to update it and helping me with some of the91 //* 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 should95 //* only ever be released at WC3C and at no other site. Please give credits if96 //* this library finds its way into your maps, and otherwise thanks for reading!97 //* 98 99 globals100 private constant real TIMEOUT = 0.03125 //32 fps
101
102 //* Do not edit below here103
104 //Lightning type constants105 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 endglobals127 128 //! textmacro LightningDebugTextmacro takes TYPE, RETURN129 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 endif133 //! endtextmacro134 135 //! textmacro LightningRecolourDebugTextmacro takes VARIABLE, COLOUR136 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 endif143 //! endtextmacro144 145 //******************************************************************************146 147 struct Lightning148 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 l177
178 loop179 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 unit184 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 endif190 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 endif196
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.alpha201 //because this could have been the first time it's visiable again202 set a = l.alphaColour
203 else204 //Otherwise make the alpha 0 so it's invisiable205 set a = 0.
206 endif207
208 //Check if the lightning has been delay released209 if l.releaseDelay > 0. and not l.released then
210 //Count down the delay211 set l.releaseDelay = l.releaseDelay - TIMEOUT
212 if l.releaseDelay <= 0. then
213 //Time to release it214 set l.released = true
215 if l.fadingAlphaInc < 1. then
216 //Increase the fadingAlpha by fadingAlphaInc to account for this cycle217 set l.fadingAlpha = 1. + l.fadingAlphaInc
218 else219 //If the fade time from the delayed release was 0., release it instantly220 set l.fadingAlpha = 0.
221 endif222 endif223 endif224
225 if l.released then
226 if l.fadingAlpha > 0. then
227 //Lightning is still fading out228 set l.fadingAlpha = l.fadingAlpha - l.fadingAlphaInc
229 if l.fadingAlpha < 0. then
230 set l.fadingAlpha = 0.
231 endif232 set a = a * l.fadingAlpha
233 else234 //Lightning has finished fading out and should be destroyed235 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 else246 call PauseTimer(Timer)
247 endif248 endif249 elseif l.fadingAlpha < 1. then
250 //Lightning is fading in251 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 lightning255 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 else262 call PauseTimer(Timer)
263 endif264 endif265 endif266
267 set a = a * l.fadingAlpha
268 endif269
270 //If the lightning still exists, update it's alpha value271 if l.lightning != null then
272 call SetLightningColor(l.lightning, l.redColour, l.greenColour, l.blueColour, a)
273 endif274
275 set i = i + 1
276 else277 set Index = Index - 1
278 if Index > 0 then
279 set Array[i] = Array[Index]
280 else281 call PauseTimer(Timer)
282 endif283 endif284 endloop285 endmethod286
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 players295 //can see it (checkVisibitiy set to false and at (0, 0) (0, 0)) so that it will not be null for296 //players that cannot see it as it's created. It can then be moved to the correct location and have297 //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 else312 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 endif317
318 if .updatePosition then
319 if Index == 0 then
320 call TimerStart(Timer, TIMEOUT, true, function Lightning.Update)
321 endif322 set Array[Index] = this
323 set Index = Index + 1
324 endif325
326 return this
327 endmethod328
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 else340 set .startZ = z
341 endif342 set .startSet = true
343 else344 set .endUnit = null
345 set .endX = x
346 set .endY = y
347 if considerTerrain then
348 set .endZ = GetLocationZ(Loc) + z
349 else350 set .endZ = z
351 endif352 set .endSet = true
353 endif354
355 if .startSet and .endSet then
356 set .canSee = MoveLightningEx(.lightning, .checkVisibility, .startX, .startY, .startZ, .endX, .endY, .endZ)
357 endif358
359 if .updatePosition and not .checkVisibility and .fadingAlpha == 1. and .startUnit == null and .endUnit == null and .releaseDelay <= 0. then
360 set .updatePosition = false
361 endif362
363 return true
364 endmethod365
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 endif379 else380 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 endif389 endif390
391 if .startSet and .endSet then
392 set .canSee = MoveLightningEx(.lightning, .checkVisibility, .startX, .startY, .startZ, .endX, .endY, .endZ)
393 endif394
395 if .updatePosition == false then
396 if Index == 0 then
397 call TimerStart(Timer, TIMEOUT, true, function Lightning.Update)
398 endif399 set Array[Index] = this
400 set Index = Index + 1
401 set .updatePosition = true
402 endif403
404 return true
405 endmethod406
407 method operator red takes nothing returns real
408 return .redColour
409 endmethod410
411 method operator green takes nothing returns real
412 return .greenColour
413 endmethod414
415 method operator blue takes nothing returns real
416 return .blueColour
417 endmethod418
419 method operator alpha takes nothing returns real
420 return .alphaColour
421 endmethod422
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 endif438
439 return SetLightningColor(.lightning, r, g, b, a * .fadingAlpha)
440 endmethod441
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 else456 set a = col / 0x1000000
457 set col = col - a * 0x1000000
458 endif459
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 else473 set alpha = 0.
474 endif475
476 call SetLightningColor(.lightning, .redColour, .greenColour, .blueColour, alpha * .fadingAlpha)
477 endmethod478
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 endmethod482
483 method operator color= takes ARGB color returns nothing
484 call .ARGBrecolor(color)
485 endmethod486 endif487
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 endif494
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 there501 if not .updatePosition then
502 if Index == 0 then
503 call TimerStart(Timer, TIMEOUT, true, function Lightning.Update)
504 endif505 set Array[Index] = this
506 set Index = Index + 1
507 set .updatePosition = true
508 endif509 elseif .updatePosition then
510 //It's already updating so just make it invisible now and it'll get cleaned up in the next loop511 set .fadingAlpha = 0.
512 call SetLightningColor(.lightning, .redColour, .blueColour, .greenColour, 0.)
513 else514 //It's not updating so it can be cleaned up now515 call DestroyLightning(.lightning)
516 set .lightning = null
517 set .startUnit = null
518 set .endUnit = null
519 call .deallocate()
520 endif521 endmethod522
523 method destroy takes nothing returns nothing
524 call .release(0.)
525 endmethod526
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 endif533 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 endif537
538 set .releaseDelay = delay
539 if fadeTime > 0. then
540 set .fadingAlphaInc = TIMEOUT / fadeTime
541 else542 //Don't want to divide by 0! 543 set .fadingAlphaInc = 1.
544 endif545
546 //Add it to the Update Loop if it's not already in there547 if not .updatePosition then
548 if Index == 0 then
549 call TimerStart(Timer, TIMEOUT, true, function Lightning.Update)
550 endif551 set Array[Index] = this
552 set Index = Index + 1
553 set .updatePosition = true
554 endif555 endmethod556 endstruct557 558 endlibrary559 560
code source<<<<<. http://www.wc3c.net/showpost.php?p=1134326&postcount=1

