/******************************************************************************
*
* TIMED LIGHTNINGS by Maker v1.0.1.2
*
* Allows the creation of lightnings with expiration timer.
* Supports:
* o Fading lightnings in and out
* o Attaching to units
* o Attaching to points
* o Linear movement in x-, y- and z-axes
*
*
* Methods
*
* YourStructInstance.remove(boolean fade)
* Destroys the lightning with or without fading immediately
*
* P2U
* From a static point attached to a unit
* static method P2U takes lightning l, unit t, real time, real x1, real y1, real z1, real z2, real startAlpha, real endAlpha returns nothing
*
* The lightning, target unit, duration, origin x, origin y, origin z, end z
*
*
* P2UEx
* From a moving point attached to a unit
* static method P2UEx takes lightning l, unit a, real t, real zu, real x1, real y1, real z1, real x2, real y2, real z2, real startAlpha, real endAlpha returns nothing
*
* The lightning, target unit, duration, target z, origin start x, origin start y, origin start z, origin end x, origin end y, origin end z
*
* U2P
* From attached to a unit to a static point
* static method U2P takes lightning l, unit s, real t, real x1, real y1, real x2, real y2, real z1, real z2, real startAlpha, real endAlpha returns nothing
*
* The lightning, source unit, duration, origin x, origin y, point x , point y, source z, point z
*
* U2PEx
* From attached to a unit to a moving point
* static method U2PEx takes lightning l, unit a, real t, real zu, real x1, real y1, real z1, real x2, real y2, real z2, real startAlpha, real endAlpha returns nothing
*
* The lightning, source unit, duration, source z, point start x, point start y, point start z, point end x, point end y, point end z
*
* U2U
* From attached to a unit to attached to a unit
* static method U2U takes lightning l, unit s, unit t, real time, real z1, real z2, real startAlpha, real endAlpha returns nothing
*
* The lightning, source unit, target unit, duration, source z, target z
*
* P2P
* From a static point to a static point
* static method P2P takes lightning l, real t, real startAlpha, real endAlpha returns nothing
*
* The lightning, duration
*
* P2PEx
* From a moving point to a moving point
* static method P2PEx takes lightning l, real t, real x1, real y1, real z1, real x2, real y2, real z2, real x3, real y3, real z3, real x4, real y4, real z4, real startAlpha, real endAlpha returns nothing
*
* The lightning, duration, origin start x, origin start y, origin start z, origin end x, origin end y, origin end z, target start x, target start y, target start z, target end x, target end y, target end z
*
*
* Alpha values are between 1 and 0. 1 is fully visible, 0 is transparent.
*
* The above methods, excluding remove, now return the struct instance.
* With access to struct instance, you can now read and write to source and target.
* In addition, you can now stop the lightning handle from moving or start it up again
* at any moment of the lightning's life time.
* You can also read the number of ticks left, current alpha and the ending alpha
*
*******************************************************************************/
library TimedLightnings
globals
private constant real TO = 0.03125000 // Update interval
private integer CT = 0 // Lightning count
private timer TMR = CreateTimer()
private location loc = Location(0,0)
endglobals
struct TimedL extends array
private lightning l
boolean moves
unit source // source
unit target // target
readonly integer time // how many ticks, time
readonly real alpha // alpha value
readonly real endAlpha // end alpha value
private real da // transparency change rate
private real x1
private real x2
private real y1
private real y2
private real z1
private real z2
private real dx1
private real dy1
private real dz1
private real dx2
private real dy2
private real dz2
private integer next // next node
private integer prev // previous node
private static integer rlast = 0 // previous created
private static thistype first // first node
private static integer ic = 0
private static integer ir = 0
private thistype rn
private static thistype dat
private static thistype dat2
private static thistype dat3
private static method destroyL takes nothing returns nothing
/*-Link previous node with next one-*/
set dat3 = dat2.prev
set dat3.next = dat2.next
/*-----Set new last created node----*/
if dat2 == rlast then
set rlast = dat3
endif
/*-Link next node with previous one-*/
set dat3 = dat2.next
set dat3.prev = dat2.prev
/*--------Set new first node--------*/
if dat2 == first then
set first = dat3
endif
call DestroyLightning(dat2.l)
set CT = CT - 1
if CT == 0 then
call PauseTimer(TMR)
endif
set dat2.rn=ir
set ir=dat2
endmethod
private static method looping takes nothing returns nothing
local real z1
local real z2
set dat = first
loop
set z1 = 0
set z2 = 0
set dat.time = dat.time - 1
if dat.da != 0 then
set dat.alpha = dat.alpha - dat.da
call SetLightningColor(dat.l, 1, 1, 1, dat.alpha)
endif
if dat.source == null then
if dat.dx1 != 0 then
set dat.x1 = dat.x1 + dat.dx1
endif
if dat.dy1 != 0 then
set dat.y1 = dat.y1 + dat.dy1
endif
if dat.dz1 != 0 then
set dat.z1 = dat.z1 + dat.dz1
endif
else
set dat.x1 = GetUnitX(dat.source)
set dat.y1 = GetUnitY(dat.source)
set z1 = GetUnitFlyHeight(dat.source)
endif
if dat.target == null then
if dat.dx2 != 0 then
set dat.x2 = dat.x2 + dat.dx2
endif
if dat.dy2 != 0 then
set dat.y2 = dat.y2 + dat.dy2
endif
if dat.dz2 != 0 then
set dat.z2 = dat.z2 + dat.dz2
endif
else
set dat.x2 = GetUnitX(dat.target)
set dat.y2 = GetUnitY(dat.target)
set z2 = GetUnitFlyHeight(dat.target)
endif
if dat.moves then
call MoveLocation(loc, dat.x1, dat.y1)
set z1 = GetLocationZ(loc) + dat.z1 + z1
call MoveLocation(loc, dat.x2, dat.y2)
set z2 = GetLocationZ(loc) + dat.z2 + z2
call MoveLightningEx(dat.l, true, dat.x1, dat.y1, z1, dat.x2, dat.y2, z2)
endif
if dat.time == 0 then
set dat2 = dat
set dat = dat.next
call destroyL()
else
set dat = dat.next
endif
exitwhen dat == 0
endloop
endmethod
private static method InitAdd takes nothing returns nothing
/* Add node to list, make this the last on list */
if rlast != 0 then
set dat2 = rlast
set dat2.next = dat
endif
/* Link this with previous node */
set dat.prev = rlast
/* Make this the last created node */
set rlast = dat
set CT = CT + 1
if CT == 1 then
/* Make this the first node */
set first = dat
call TimerStart(TMR, TO, true, function thistype.looping)
endif
endmethod
private static method Recycle takes nothing returns nothing
if 0==ir then
set ic=ic+1
set dat=ic
else
set dat=ir
set ir=dat.rn
endif
endmethod
method remove takes boolean flag returns nothing
if not flag then
set .time = 1
else
set .time = R2I(1/TO)
set da = (alpha-endAlpha)*TO/1
endif
endmethod
static method P2U takes lightning l, unit t, real time, real x1, real y1, real z1, real z2, real startAlpha, real endAlpha returns thistype
local thistype this
call Recycle()
set this = dat
set .x1 = x1
set .y1 = y1
set .z1 = z1
set .z2 = z2
set .source = null
set .target = t
set .next = 0 // Nodes are added to the end of the list, there is no next node
set .l = l
set .time = R2I(time/TO) // Calculates how many loops does the lightning lasts
set .alpha = startAlpha
set .endAlpha = endAlpha
set .da = (startAlpha-endAlpha)*TO/time // Transparency change speed
set .moves = true
call InitAdd()
return this
endmethod
static method U2P takes lightning l, unit s, real t, real x1, real y1, real x2, real y2, real z1, real z2, real startAlpha, real endAlpha returns thistype
local thistype this
call Recycle()
set this = dat
set .x1 = x1
set .y1 = y1
set .x2 = x2
set .y2 = y2
set .z1 = z1
set .z2 = z2
set .source = s
set .target = null
set .next = 0
set .l = l
set .time = R2I(t/TO)
set .alpha = startAlpha
set .endAlpha = endAlpha
set .da = (startAlpha-endAlpha)*TO/t
set .moves = true
call InitAdd()
return this
endmethod
static method U2U takes lightning l, unit s, unit t, real time, real z1, real z2, real startAlpha, real endAlpha returns thistype
local thistype this
call Recycle()
set this = dat
set .z1 = z1
set .z2 = z2
set .source = s
set .target = t
set .next = 0
set .l = l
set .time = R2I(time/TO)
set .alpha = startAlpha
set .endAlpha = endAlpha
set .da = (startAlpha-endAlpha)*TO/time
set .moves = true
call InitAdd()
return this
endmethod
static method P2P takes lightning l, real t, real startAlpha, real endAlpha returns thistype
local thistype this
call Recycle()
set this = dat
set .source = null
set .target = null
set .next = 0
set .l = l
set .time = R2I(t/TO)
set .alpha = startAlpha
set .endAlpha = endAlpha
set .da = (startAlpha-endAlpha)*TO/t
set .moves = false
call InitAdd()
return this
endmethod
static method P2UEx takes lightning l, unit a, real t, real zu, real x1, real y1, real z1, real x2, real y2, real z2, real startAlpha, real endAlpha returns thistype
local thistype this
local real n = TO/t
call Recycle()
set this = dat
set .x1 = x1
set dx1 = (x2-x1)*n
set .y1 = y1
set dy1 = (y2-y1)*n
set .z1 = z1
set dz1 = (z2-z1)*n
set .z2 = zu
set .source = null
set .target = a
set .next = 0
set .l = l
set .time = R2I(t/TO)
set .alpha = startAlpha
set .endAlpha = endAlpha
set .da = (startAlpha-endAlpha)*n
set .moves = true
call InitAdd()
return this
endmethod
static method U2PEx takes lightning l, unit a, real t, real zu, real x1, real y1, real z1, real x2, real y2, real z2, real startAlpha, real endAlpha returns thistype
local thistype this
local real n = TO/t
call Recycle()
set this = dat
set .x2 = x1
set .dx2 = (x2-x1)*n
set .y2 = y1
set .dy2 = (y2-y1)*n
set .z2 = z1
set .dz2 = (z2-z1)*n
set .z1 = zu
set .source = a
set .target = null
set .next = 0
set .l = l
set .time = R2I(t/TO)
set .alpha = startAlpha
set .endAlpha = endAlpha
set .da = (startAlpha-endAlpha)*n
set .moves = true
call thistype.InitAdd()
return this
endmethod
static method P2PEx takes lightning l, real t, real x1, real y1, real z1, real x2, real y2, real z2, real x3, real y3, real z3, real x4, real y4, real z4, real startAlpha, real endAlpha returns thistype
local thistype this
local real n = TO/t
call Recycle()
set this = dat
set .x1 = x1
set .x2 = x3
set .y1 = y1
set .y2 = y3
set .z1 = z1
set .z2 = z3
set .dx1 = (x2-x1)*n
set .dy1 = (y2-y1)*n
set .dz1 = (z2-z1)*n
set .dx2 = (x4-x3)*n
set .dy2 = (y4-y3)*n
set .dz2 = (z4-z3)*n
set .source = null
set .target = null
set .next = 0
set .l = l
set .time = R2I(t/TO)
set .alpha = startAlpha
set .endAlpha = endAlpha
set .da = (startAlpha-endAlpha)*n
set .moves = true
call InitAdd()
return this
endmethod
endstruct
endlibrary