1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. Hey guys, we've posted the Results for the 30th Modeling Contest. Check them out!
    Dismiss Notice
  3. The 15th Mini-Mapping Contest came to an end. The Secrets of Warcraft 3 are soon to be revealed! Come and vote in the public poll for your favorite maps.
    Dismiss Notice
  4. The 12th incarnation of the Music Contest is LIVE! The theme is Synthwave. Knight Rider needs a song to listen to on his journey. You should definitely have some fun with this theme!
    Dismiss Notice
  5. Join other hivers in a friendly concept-art contest. The contestants have to create a genie coming out of its container. We wish you the best of luck!
    Dismiss Notice
  6. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[Wurst] Lightning

Discussion in 'Submissions' started by Chaosy, Jan 7, 2018.

  1. Chaosy

    Chaosy

    Joined:
    Jun 9, 2011
    Messages:
    10,574
    Resources:
    18
    Maps:
    1
    Spells:
    11
    Tutorials:
    6
    Resources:
    18
    A lightning library I am working on.

    Features
    • lightning . extensions
    • enum representing all lightning types
    • Lightning class with bonus functionality
      • Getting lightning source and target location in either x/y or vec2
      • attaching between location to unit
      • attaching between unit to unit
    wurstbin
    code

    Code (WurstScript):

    package Lightning

    import TimerUtils
    import Table
    import RegisterEvents

    Table hash

    enum LightningType
        CHAIN_LIGHTNING_PRIMARY
        CHAIN_LIGHTNING_SECONDARY
        DRAIN
        DRAIN_LIFE
        DRAIN_MANA
        FINGER_OF_DEATH
        FORKED_LIGHTNING
        HEALING_WAVE_PRIMARY
        HEALING_WAGVE_SECONDARY
        LIGHTNING_ATTACK
        MAGIC_LEASH
        MANA_BURN
        MANA_FLARE
        SPIRIT_LINK


    function getLightningType(LightningType lt) returns string
        switch(lt)
            case CHAIN_LIGHTNING_PRIMARY
                return "CLPB"
            case CHAIN_LIGHTNING_SECONDARY
                return "CLSB"
            case DRAIN
                return "DRAB"
            case DRAIN_LIFE
                return "DRAL"
            case DRAIN_MANA
                return "DRAM"
            case FINGER_OF_DEATH
                return "AFOD"
            case FORKED_LIGHTNING
                return "FORK"
            case HEALING_WAVE_PRIMARY
                return "HWPB"
            case HEALING_WAGVE_SECONDARY
                return "HWSB"
            case LIGHTNING_ATTACK
                return "CHIM"
            case MAGIC_LEASH
                return "LEAS"
            case MANA_BURN
                return "MBUR"
            case MANA_FLARE
                return "MFPB"
            case SPIRIT_LINK
                return "SPLK"
            default
                return ""

    function lightning.move(vec3 one, vec3 two)
        MoveLightningEx(this, true, one.x, one.y, one.z, two.x, two.y, two.z)

    function lightning.move(vec2 one, vec2 two)
        MoveLightning(this, true, one.x, one.y, two.x, two.y)

    function lightning.move(boolean vis, vec2 one, vec2 two)
        MoveLightning(this, vis, one.x, one.y, two.x, two.y)

    function lightning.move(boolean vis, real x1, real y1, real x2, real y2)
        MoveLightning(this, vis, x1, y1, x2, y2)

    function lightning.setColor(real r, real g, real b, real a)
        SetLightningColor(this, r, g, b, a)

    function lightning.getColorR() returns real
        return GetLightningColorR(this)

    function lightning.getColorG() returns real
        return GetLightningColorG(this)

    function lightning.getColorB() returns real
        return GetLightningColorB(this)

    function lightning.getColorA() returns real
        return GetLightningColorA(this)

    function lightning.getColor() returns color
        integer r = R2I(this.getColorR())
        integer g = R2I(this.getColorG())
        integer b = R2I(this.getColorB())

        return color(r, g, b)

    function lightning.Destroy()
        DestroyLightning(this)

    public class Lightning
        lightning l
        LightningType sort
        LightningType defaultType = LightningType.DRAIN_LIFE
        boolean vis = true
        boolean attached = false

        timer attachTimer
        unit attachTarget
        unit attachSource

        real sx
        real sy
        real sz
        real tx
        real ty
        real tz

        vec2 source
        vec2 target

        construct(vec2 v1, vec2 v2)
            sort = defaultType
            l = AddLightning(getLightningType(sort), vis, v1.x, v1.y, v2.x, v2.y)

        construct(real x1, real x2, real y1, real y2)
            sort = defaultType
            savePos(x1, x2, y1, y2)
            l = AddLightning(getLightningType(sort), vis, x1, y1, x2, y2)

        construct(boolean show, vec2 v1, vec2 v2)
            sort = defaultType
            vis = show
            saveVec(v1, v2)
            l = AddLightning(getLightningType(sort), vis, v1.x, v1.y, v2.x, v2.y)

        construct(boolean show, real x1, real x2, real y1, real y2)
            sort = defaultType
            vis = show
            savePos(x1, x2, y1, y2)
            l = AddLightning(getLightningType(sort), vis, x1, y1, x2, y2)

        construct(LightningType lt,vec2 v1, vec2 v2)
            sort = lt
            saveVec(v1, v2)
            l = AddLightning(getLightningType(sort), vis, v1.x, v1.y, v2.x, v2.y)

        construct(LightningType lt, real x1, real x2, real y1, real y2)
            sort = lt
            savePos(x1, x2, y1, y2)
            l = AddLightning(getLightningType(sort), vis, x1, y1, x2, y2)

        function savePos(real x1, real x2, real y1, real y2)
            sx = x1
            sy = y1
            tx = x2
            ty = y2

        function saveVec(vec2 s, vec2 t)
            source = s
            target = t

        function getLightning() returns lightning
            return l

        function getType() returns LightningType
            return sort

        function move(real x1, real y1, real x2, real y2)
            l.move(vis, x1, y1, x2, y2)

        function getColor() returns color
            return l.getColor()

        function setColor(real r, real g, real b, real a)
            l.setColor(r, g, b, a)

        function getSource() returns vec2
            return source

        function getTarget() returns vec2
            return target

        function getSourceX() returns real
            return sx

        function getSourceY() returns real
            return sy

        function getTargetX() returns real
            return tx

        function getTargetY() returns real
            return ty

        function isAttached() returns boolean
            return attached

        function onAttachInit()
            attached = true
            attachTimer = getTimer()
            attachTimer.setData(this castTo int)

        function isUnitValid(unit u) returns boolean
            return u != null and u.isAlive()

        function attach(unit u1)
            if(isUnitValid(u1) == false)
                print("The unit you're trying to attach is equal to null")
                return
            else if(isAttached() == false)
                integer h = GetHandleId(u1)
                attachTarget = u1
                hash.saveBoolean(h, true)
                hash.saveInt(h, this castTo int)
                onAttachInit()
                TimerStart(attachTimer, 0.03, true, () -> begin
                    Lightning instance = GetExpiredTimer().getData() castTo Lightning
                    instance.l.move(instance.source, instance.attachTarget.getPos())
                end)

        function attach(unit u1, unit u2)
            if(isUnitValid(u1) == false and isUnitValid(u2) == false)
                print("The unit you're trying to attach is equal to null")
                return
            else if(isAttached() == false)
                integer h = GetHandleId(u1)
                attachSource = u1
                attachTarget = u2
                hash.saveBoolean(GetHandleId(u1), true)
                hash.saveInt(h, this castTo int)
                h = GetHandleId(u2)
                hash.saveBoolean(GetHandleId(u2), true)
                hash.saveInt(h, this castTo int)
                onAttachInit()
                TimerStart(attachTimer, 0.03, true, () -> begin
                    Lightning instance = GetExpiredTimer().getData() castTo Lightning
                    instance.l.move(instance.attachSource.getPos(), instance.attachTarget.getPos())
                end)

        function flushUnit(unit u)
            integer h = GetHandleId(u)
            hash.removeInt(h)
            hash.removeBoolean(h)

        function deAttach()
            attachTimer.release()
            boolean isAttached = attachTarget != null ? true : false
            flushUnit(attachSource)
            if(isAttached)
                flushUnit(attachTarget)

        ondestroy
            if isAttached()
                deAttach()
            l.Destroy()

    function isUnitAttached(unit u) returns boolean
        return hash.loadBoolean(GetHandleId(u))

    function onDeath()
        unit u = GetTriggerUnit()
        integer h = GetHandleId(u)
        destroy hash.loadInt(h) castTo Lightning

    function onDeathFilter() returns boolean
        return isUnitAttached(GetTriggerUnit())

    init
        hash = new Table()
        CreateTrigger()
        ..registerAnyUnitEvent(EVENT_PLAYER_UNIT_DEATH)
        ..addCondition(Condition(function onDeathFilter))
        ..addAction(function onDeath)
       

    [/wurst][/hidden]


    Demo:
    Code (WurstScript):

    unit u
    unit u2

    init
        u = CreateUnit(players[0], 'Hpal', 99, 99, 0)
        u2 = CreateUnit(players[0], 'hfoo', 0, 0, 0)
        TimerStart(CreateTimer(), 1, false, () -> begin
            vec2 v1 = vec2(0, 0)
            vec2 v2 = vec2(99, 99)
            new Lightning(v1, v2)
            ..attach(u, u2)
        end)
     

    Attached Files:

    Last edited: Jan 7, 2018
  2. Chaosy

    Chaosy

    Joined:
    Jun 9, 2011
    Messages:
    10,574
    Resources:
    18
    Maps:
    1
    Spells:
    11
    Tutorials:
    6
    Resources:
    18
    TODO; cleanup when attached units die.
     
  3. Chaosy

    Chaosy

    Joined:
    Jun 9, 2011
    Messages:
    10,574
    Resources:
    18
    Maps:
    1
    Spells:
    11
    Tutorials:
    6
    Resources:
    18
    Updated. Note that I have only updated the code in the hidden tab so far, the link and map is not up to date right now.
     
  4. HappyTauren

    HappyTauren

    Joined:
    Nov 3, 2006
    Messages:
    8,414
    Resources:
    87
    Models:
    61
    Icons:
    23
    Packs:
    1
    Tutorials:
    2
    Resources:
    87
    Oh nice, a Wurst submission. I'll comment on this later when I have the time to review it.
     
  5. Trokkin

    Trokkin

    Joined:
    Jan 23, 2015
    Messages:
    97
    Resources:
    1
    Tutorials:
    1
    Resources:
    1
    Hey, most of this your package were added in recent stdlib2 update as a new handle wrapper package. The rest of your code - the struct - is useful though I feel it need some perfection.
    For example, I'd recommend you use HashMap instead of Table.
    Well, anyway, it's nice to see another man into wurst, so, welcome home :)
     
  6. Frotty

    Frotty

    Wurst Reviewer

    Joined:
    Jan 1, 2009
    Messages:
    1,397
    Resources:
    11
    Models:
    3
    Tools:
    1
    Maps:
    5
    Tutorials:
    1
    Wurst:
    1
    Resources:
    11
    Did you update this? The bin link is dead (sry, switched servers) but I think this could be useful.
     
  7. Chaosy

    Chaosy

    Joined:
    Jun 9, 2011
    Messages:
    10,574
    Resources:
    18
    Maps:
    1
    Spells:
    11
    Tutorials:
    6
    Resources:
    18
    I did not. I'll need to do a touchup on this I think.
     
  8. Frotty

    Frotty

    Wurst Reviewer

    Joined:
    Jan 1, 2009
    Messages:
    1,397
    Resources:
    11
    Models:
    3
    Tools:
    1
    Maps:
    5
    Tutorials:
    1
    Wurst:
    1
    Resources:
    11
    Just found and restored a backup, bin links are restored \o/. What about this @Chaosy ?
     
  9. Chaosy

    Chaosy

    Joined:
    Jun 9, 2011
    Messages:
    10,574
    Resources:
    18
    Maps:
    1
    Spells:
    11
    Tutorials:
    6
    Resources:
    18
    Not interested in updating this, while useful it is not fun to develop. Graveyard it for now.
     
  10. Cokemonkey11

    Cokemonkey11

    Wurst Reviewer

    Joined:
    May 9, 2006
    Messages:
    3,225
    Resources:
    18
    Tools:
    1
    Maps:
    5
    Spells:
    3
    Tutorials:
    2
    JASS:
    7
    Resources:
    18
    This looks pretty useful, maybe throw it in a PR for others to contribute to if you don't feel like finishing it up?

    My feedback:

    - move into standard library what is clearly meant for stdlib (helper fns, etc)
    - [optional] consider changing the unit-attach and on-death behaviours to consume closures, to make them more generic, e.g.

    ```
    let u = createUnit(...)
    let v = createUnit(...)

    new Lightning(
    vec2(0., 0.),
    vec2(512., 0.),
    (lgt) -> lgt.move(u.getPos), v.getPos()),
    () -> u.isAlive() and v.isAlive()
    )
    ```
     
  11. Chaosy

    Chaosy

    Joined:
    Jun 9, 2011
    Messages:
    10,574
    Resources:
    18
    Maps:
    1
    Spells:
    11
    Tutorials:
    6
    Resources:
    18
    Nah, as far as I am concerned anyone is free continue if they feel inspired.
    A name in the credits would be nice, but I do not really mind if someone just claims it.
     
  12. Cokemonkey11

    Cokemonkey11

    Wurst Reviewer

    Joined:
    May 9, 2006
    Messages:
    3,225
    Resources:
    18
    Tools:
    1
    Maps:
    5
    Spells:
    3
    Tutorials:
    2
    JASS:
    7
    Resources:
    18
    The suggestion is that you open the PR