HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Lightning script

05-21-2009, 05:44 PM#1
0zyx0
This is a script making the usage of lightning effects easier. It can connect a lightning effect to one or two units, and it supports fading. I am posting this here to gather feedback. There are some issues I am aware of: When changing the alpha value of the created lightning effect, the fading will not work properly, and the code looks like a mess in some parts. I would like to know how to solve those issues.

I do also have to mention, this is based on a script by moyack, which I have permission to edit.
Collapse JASS:
library LightningUtils requires TimedLoop

globals
    private location loc = Location(0., 0.)
endglobals

private function GetUnitZ takes unit u returns real
    call MoveLocation(loc, GetUnitX(u), GetUnitY(u))
    return GetLocationZ(loc) + GetUnitFlyHeight(u)
endfunction

//! textmacro Lightning__COORD takes n, name
    private unit u$n$ = null
    private real X$n$
    private real Y$n$
    private real Z$n$
        
    method set$name$Coords takes real x, real y, real z returns nothing
        set .X$n$ = x
        set .Y$n$ = y
        set .Z$n$ = z
    endmethod
        
    method set$name$Unit takes unit u returns nothing
        set .u$n$ = u
    endmethod
//! endtextmacro

//! textmacro Lightning__Move    
    if u1 == null then
        if u2 != null then
            call MoveLightningEx(.l, false, .X1, .Y1, .Z1, GetUnitX(u2), GetUnitY(u2), GetUnitZ(u2))
        endif
    else
        if u2 == null then
            call MoveLightningEx(.l, false, GetUnitX(u1), GetUnitY(u1), GetUnitZ(u1), .X2, .Y2, .Z2)
        else
            call MoveLightningEx(.l, false, GetUnitX(u1), GetUnitY(u1), GetUnitZ(u1), GetUnitX(u2), GetUnitY(u2), GetUnitZ(u2))
        endif
    endif
//! endtextmacro

struct Lightning
    private lightning l = null
    private integer iterations = 0
    private integer i = 0
    private integer fadestart = 0
    private real fadeChange
    private string codeName
    
    method onDestroy takes nothing returns nothing
        call DestroyLightning(.l)
    endmethod
    
    //! runtextmacro Lightning__COORD("1", "Start")
    //! runtextmacro Lightning__COORD("2", "End")
    
    private method onTimedLoop takes nothing returns boolean
     local integer i = .i
     local unit u1 = .u1
     local unit u2 = .u2
     local lightning l = .l
        set i = i + 1
        if i == .iterations then
            return false
        endif
        //! runtextmacro Lightning__Move()
        if i >= .fadestart and .fadestart != 0 then
            call SetLightningColor(l, GetLightningColorR(l), GetLightningColorG(l), GetLightningColorB(l), GetLightningColorA(l) - .fadeChange)
        endif
     set .i = i
     set l = null
     set u1 = null
     set u2 = null
     return true
    endmethod
    
    private method move takes nothing returns nothing
     local unit u1 = .u1
     local unit u2 = .u2
        //! runtextmacro Lightning__Move()
     set u1 = null
     set u2 = null
    endmethod
    
    implement TimedLoop
    
    //! textmacro Lightning__Starter takes name, args, pos1, pos2
    static method $name$ takes string codeName, real duration, real fadeTime, $args$ returns lightning
     local Lightning this = Lightning.allocate()
        set this.l = AddLightning(codeName, false, 0., 0., 0., 0.)
        set this.iterations = R2I(duration/TimedLoop_PERIOD)
        call this.setStart$pos1$
        call this.setEnd$pos2$
        if fadeTime != 0. then
            set this.fadestart = R2I((duration-fadeTime)/TimedLoop_PERIOD)
            set this.fadeChange = 100./(duration-fadeTime)
        endif
        call this.move()
        call this.startTimedLoop()
        return .l
    endmethod
    //! endtextmacro
    
    //! runtextmacro Lightning__Starter("unit2unit","unit u1,unit u2","Unit(u1)","Unit(u2)")
    //! runtextmacro Lightning__Starter("unit2coords","unit u,real x,real y,real z","Unit(u)","Coords(x,y,z)")
    //! runtextmacro Lightning__Starter("coords2unit","real x,real y,real z,unit u","Coords(x,y,z)","Unit(u)")
    //! runtextmacro Lightning__Starter("coords2coords","real x,real y,real z,real x1,real y1,real z1","Coords(x,y,z)","Coords(x1,y1,z1)")
    
endstruct

endlibrary

The syntax for using this could look like this:
Collapse JASS:
call Lightning.unit2coords("DRAL", 5., 0.5, someunit, x, y, z)
call Lightning.coords2coords("DRAL", 10., 2., GetUnitX(u), GetUnitY(u), 0., 128., 128., 0.)
05-21-2009, 05:47 PM#2
Toadcop
lightnings doesnt save into save file... and i you try to do something with not existing lightning = crash ^_^ (the same is for images)
05-21-2009, 06:15 PM#3
moyack
Actually this shitty code does that.

This script is too limited due the fact that most of the time you use lightnings to link one thing with another and those things usually are units (which normally they move) and you would need to use other timer to the one already controlling its expiration timer to get control of these features.

I was working with one script which gives to the user an easier way to setup stuff for this special kind of handle. If you want to use it, I give you permission to improve it and /or submitting here in your name. Several ideas in the code were suggested by Viikuna, a very nice guy with good ideas, so give credits to him if you decide to work with this.
05-29-2009, 08:08 PM#4
0zyx0
This is the script I originally posted, the new script is in the first post.

Expand JASS: