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. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. The poll for Hive's 12th Concept Art Contest is up! Go cast your vote for your favourite genie!
    Dismiss Notice
  4. Travel to distant realms and encounter scenes unknown to the common folk. The Greatest of Adventures is upon us with the 8th Cinematic Contest. Join in on a fun ride.
    Dismiss Notice
  5. The 18th Icon Contest is ON! Choose any ingame unit and give him/her Hero abilities. Good luck to all.
    Dismiss Notice
  6. Contestants are to create a scene set in the Stone Age. Come and see what you can come up with. We wish you the best of luck!
    Dismiss Notice
  7. Colour outside the lines! Techtree Contest #13 is a go. The contest is optionally paired.
    Dismiss Notice
  8. Greetings cerebrates, our Swarm needs new spawners that will have numerous children. Join the HIVE's 31st Modeling Contest - Spawners and Spawned! The contest is optionally paired.
    Dismiss Notice
  9. 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.

[vJASS] Just a piece of code

Discussion in 'Triggers & Scripts' started by Krogoth, Oct 29, 2012.

  1. Krogoth

    Krogoth

    Joined:
    Apr 5, 2011
    Messages:
    247
    Resources:
    0
    Resources:
    0
    It's a piece of code that performs Magina's Mana Void. It works well but how does it look at all and do you see leaks I have not seen?

    Updated:
    Code (vJASS):
    library SpellManaVoid requires Assist

    //=== Settings ===
    globals
        private constant integer ABILCODE = 'X003'
        private constant string SFX = "Abilities\\Spells\\Human\\DispelMagic\\DispelMagicTarget.mdl"
    //----------------
        private constant real RADIUS = 150
    endglobals
        struct SpellManaVoid extends array
            private static real array DAMAGE_KOEF
            private static method onInit takes nothing returns nothing
                set DAMAGE_KOEF[1] = 0.6
                set DAMAGE_KOEF[2] = 0.85
                set DAMAGE_KOEF[3] = 1.1
            endmethod
    //----------------
            private static method dealDamage takes nothing returns boolean
                set Assist.workUnit = GetFilterUnit()
                if IsUnitEnemy(Assist.workUnit, Event.triggerPlayer) and not IsUnitType(Assist.workUnit, UNIT_TYPE_MECHANICAL) and not IsUnitType(Assist.workUnit, UNIT_TYPE_MAGIC_IMMUNE) then
                    call vGDC.dealDamage(Event.triggerUnit, Assist.workUnit, Assist.workReal, MAGIC)
                endif
                return false
            endmethod
       
            static method checkEffect takes nothing returns nothing
                local real x
                local real y
                if Event.abilcode == ABILCODE then
                    set x = GetWidgetX(Event.targetUnit)
                    set y = GetWidgetY(Event.targetUnit)
                    call DestroyEffect(AddSpecialEffect(SFX, x, y))
                    set Assist.workReal = (GetUnitState(Event.targetUnit, UNIT_STATE_MAX_MANA) - GetUnitState(Event.targetUnit, UNIT_STATE_MANA)) * DAMAGE_KOEF[GetUnitAbilityLevel(Event.triggerUnit, ABILCODE)]
                    call GroupEnumUnitsInRange(Assist.workGroup, x, y, RADIUS, Condition(function thistype.dealDamage))
                endif
            endmethod
        endstruct
    endlibrary

    Old:
    Code (vJASS):
    library SpellManaVoid

    //=== Settings ===
    globals
        private constant integer ABILCODE = 'X003'
        private constant real RADIUS = 150
        private constant string SFX = "Abilities\\Spells\\Human\\DispelMagic\\DispelMagicTarget.mdl"
    endglobals
        struct SpellManaVoid
            private static real array DAMAGE_KOEF
            private static method onInit takes nothing returns nothing
                set DAMAGE_KOEF[1] = 0.6
                set DAMAGE_KOEF[2] = 0.85
                set DAMAGE_KOEF[3] = 1.1
            endmethod
    //----------------
            private static real damage
            private static group g = CreateGroup()

            private static method dealDamage takes nothing returns boolean
                local unit u = GetFilterUnit()
                if IsUnitEnemy(u, p) and not IsUnitType(u, UNIT_TYPE_MECHANICAL) and not IsUnitType(u, UNIT_TYPE_MAGIC_IMMUNE) then
                    call vGDC.dealDamage(Event.triggerUnit, u, damage, MAGIC)
                endif
                set u = null
                return false
            endmethod
       
            static method checkEffect takes nothing returns nothing
                local real x
                local real y
                if Event.abilcode == ABILCODE then
                    set x = GetWidgetX(Event.targetUnit)
                    set y = GetWidgetY(Event.targetUnit)
                    call DestroyEffect(AddSpecialEffect(SFX, x, y))
                    set damage = (GetUnitState(Event.targetUnit, UNIT_STATE_MAX_MANA) - GetUnitState(Event.targetUnit, UNIT_STATE_MANA)) * DAMAGE_KOEF[GetUnitAbilityLevel(Event.triggerUnit, ABILCODE)]
                    call ClearGroup(g)
                    call GroupEnumUnitsInRange(g, x, y, RADIUS, Condition(function thistype.dealDamage))
                    call DestroyGroup(g)
                    set g = null
                endif
            endmethod
        endstruct
    endlibrary
     
    Last edited: Oct 29, 2012
  2. PurgeandFire

    PurgeandFire

    Code Moderator

    Joined:
    Nov 11, 2006
    Messages:
    7,427
    Resources:
    18
    Icons:
    1
    Spells:
    4
    Tutorials:
    9
    JASS:
    4
    Resources:
    18
    :)

    • You should just use one global group. See this tutorial for the reason why and how to fix it.
    • When you do
      set p = GetOwningPlayer(Event.triggerUnit)
      , you can actually just use
      set p = GetTriggerPlayer()
      (I assume the event is a unit starts the effect of an ability) It is a useful little tip.
    • Finally, since you aren't actually using the struct features of allocation/deallocation, you can make it extend array:
      Code (vJASS):
      struct SpellManaVoid extends array

      This is very useful when you know you won't use any allocation/deallocation, because then it will remove a bunch of extra, unnecessary code. :)

    Overall, good job. No real glaring errors.

    P.S. Usually we indent anything within a library block. (like your globals block) This really doesn't matter, because it depends on whether or not it is your style. Just mentioning it hehe.
     
  3. Krogoth

    Krogoth

    Joined:
    Apr 5, 2011
    Messages:
    247
    Resources:
    0
    Resources:
    0
    Thanks! =]
     
  4. PurplePoot

    PurplePoot

    Joined:
    Dec 14, 2005
    Messages:
    11,162
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    If you are never going to instantiate the struct and aren't extending it, why are you using a struct at all?
     
  5. Krogoth

    Krogoth

    Joined:
    Apr 5, 2011
    Messages:
    247
    Resources:
    0
    Resources:
    0
    To use "Event." reference, for example. =]
    Have made it extending array.