• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Solved] How to turn this to struct

Status
Not open for further replies.
Level 1
Joined
Dec 17, 2016
Messages
2
JASS:
scope Slash initializer init

globals
 private constant integer ID        = 'UNIS'
 private constant string EF         = "Abilities\\Spells\\Other\\Stampede\\StampedeMissileDeath.mdl"
 private constant string EFW        = "Abilities\\Weapons\\AvengerMissile\\AvengerMissile.mdl"
 private constant string EFB        = "mdl"
 private constant real AS           = 200.0
 private constant attacktype A_Type = ATTACK_TYPE_HERO
 private constant damagetype D_Type = DAMAGE_TYPE_MAGIC
 private constant weapontype W_Type = WEAPON_TYPE_METAL_HEAVY_SLICE
endglobals

private constant function Percent takes integer level returns integer
 return 14 + (1 * level)
endfunction

private function True takes nothing returns boolean
 return true
endfunction

private function Conditions takes nothing returns boolean
   return IsPlayerEnemy(GetOwningPlayer(GetTriggerUnit()), GetOwningPlayer(GetAttacker())) and GetRandomInt(0, 100) <= Percent(GetUnitAbilityLevel(GetAttacker(), ID)) and IsUnitType(GetTriggerUnit(), UNIT_TYPE_STRUCTURE) == false and GetUnitAbilityLevel(GetAttacker(), ID) > 0 and GetWidgetLife(GetTriggerUnit()) > 0.405
endfunction

private function Actions takes nothing returns nothing
 local unit u      = GetAttacker()
 local unit t      = GetTriggerUnit()
 local integer ii  = -1
 local effect Eff
 local effect Efe
 local real Damage = I2R(GetHeroAgi(u, true))
 local real Slash  =  GetRandomReal(2, 4) + ( 1 * GetUnitAbilityLevel(u, ID))
 local real x1     = GetUnitX(u)
 local real y1     = GetUnitY(u)
 local real x2     = GetUnitX(t)
 local real y2     = GetUnitY(t)
 set Eff = AddSpecialEffectTarget(EFW, u, "weapon")
  call PauseUnit(u, true)
  call SetUnitPathing(u, false)
  call SetUnitTimeScalePercent(u, AS)
  call SetUnitVertexColor(u, 150, 150, 150, 150)
  call SetUnitPosition(u, x2 + 70 * Cos(GetRandomReal(180, -180 * bj_PI)), y2 + 70 * Sin(GetRandomReal(-1, 1 * bj_PI)))
  call SetUnitFacingToFaceUnitTimed(u, t, .05)
  call SetUnitAnimation(u, "attack")
  call DestroyEffect(AddSpecialEffectTarget(EFB, u, "origin"))
  call UnitDamageTarget(u, t, Damage, true, false, A_Type, D_Type, W_Type)
  if UnitAddAbility(u, 'Amrf') and UnitRemoveAbility(u, 'Amrf') then
    endif
  call SetUnitFlyHeight(u, 70, 1000)
  call TriggerSleepAction(.05)
   loop
     set ii = ii + 1
      exitwhen ii > Slash
      if GetWidgetLife(u) > 0.405 then
      if GetWidgetLife(t) > 0.405 then
       call SetUnitPosition(u, x2 + 70 * Cos(GetRandomReal(-180, 180 * bj_PI)), y2 + 70 * Sin(GetRandomReal(-180, 180 * bj_PI)))
       call SetUnitFacingToFaceUnitTimed(u, t, 0.05)
       call SetUnitAnimation(u, "attack")
       call DestroyEffect(AddSpecialEffectTarget(EF, t, "chest"))
       call DestroyEffect(AddSpecialEffectTarget(EFB, u, "origin"))
       call UnitDamageTarget(u, t, Damage, true, false, A_Type, D_Type, null)
       set Slash = Slash - 1
      if GetWidgetLife(t) < 0.405 then
       call ModifyHeroStat(bj_HEROSTAT_AGI, u, bj_MODIFYMETHOD_ADD, R2I(.20))
      endif
       call TriggerSleepAction(.05)
      endif
      endif
    endloop
  call SetUnitFlyHeight(u, 0, 1000)
  call SetUnitFacingToFaceUnitTimed(u, t, .05)
  call PauseUnit(u, false)
  call SetUnitPathing(u, true)
  call SetUnitTimeScalePercent(u, 100)
  call SetUnitVertexColor(u, 250, 250, 250, 250)
  call DestroyEffect(Eff)
  call IssueTargetOrder(u, "attack", t)
  set Eff = null
  set u = null
  set t = null
endfunction

//===========================================================================
private function init takes nothing returns nothing
    local trigger tt = CreateTrigger()
    local integer i = 0
    loop
        exitwhen i > 15
            call TriggerRegisterPlayerUnitEvent(tt, Player(i), EVENT_PLAYER_UNIT_ATTACKED, Filter(function True))
            set i = i + 1
    endloop
    call TriggerAddCondition(tt, Condition(function Conditions))
    call TriggerAddAction(tt, function Actions)
    call Preload(EF)
endfunction

endscope
 
JASS:
struct Slash extends array
    //! novjass
 
        struct Slash extends array
      
            //why must this extend an array? Because in your code, you are not using the following methods:
                - call Slash.allocate()
                - call Slash(randomInt).deallocate()
          
            //which means that these functions end up getting generated anyways without ever being used.
      
            //By now, you must have noticed the keyword
                - static
          
            //That means that the element is singular, that is
                - private static sometype element
          
            //Which is the equivalent to
                globals
                    sometype s__Slash_element
                endglobals
 
    //===========================================================================================================//
    //! endnovjass
    private static constant integer ID        = 'UNIS'
    private static constant string EF         = "Abilities\\Spells\\Other\\Stampede\\StampedeMissileDeath.mdl"
    private static constant string EFW        = "Abilities\\Weapons\\AvengerMissile\\AvengerMissile.mdl"
    private static constant string EFB        = "mdl"
    private static constant real AS           = 200.0
    private static constant attacktype A_Type = ATTACK_TYPE_HERO
    private static constant damagetype D_Type = DAMAGE_TYPE_MAGIC
    private static constant weapontype W_Type = WEAPON_TYPE_METAL_HEAVY_SLICE
 
    private static constant method Percent takes integer level returns integer
        return 14 + (1 * level)
    endmethod

    // dunno why you wrote this?
    // with jassHelper, this should inline...
    private static constant method True takes nothing returns boolean
        return true
    endmethod

    private static method Conditions takes nothing returns boolean
        return IsPlayerEnemy(GetTriggerPlayer(), GetOwningPlayer(GetAttacker())) and GetRandomInt(0, 100) <= Percent(GetUnitAbilityLevel(GetAttacker(), ID)) and IsUnitType(GetTriggerUnit(), UNIT_TYPE_STRUCTURE) == false and GetUnitAbilityLevel(GetAttacker(), ID) > 0 and GetWidgetLife(GetTriggerUnit()) > 0.405
    endmethod

    private static method Actions takes nothing returns nothing
        local unit u      = GetAttacker()
        local unit t      = GetTriggerUnit()
        local integer ii  = -1
        local effect Eff
        local effect Efe
        local real Damage = I2R(GetHeroAgi(u, true))
        local real Slash  =  GetRandomReal(2, 4) + ( 1 * GetUnitAbilityLevel(u, ID))
        local real x1     = GetUnitX(u)
        local real y1     = GetUnitY(u)
        local real x2     = GetUnitX(t)
        local real y2     = GetUnitY(t)
        set Eff = AddSpecialEffectTarget(EFW, u, "weapon")
        call PauseUnit(u, true)
        call SetUnitPathing(u, false)
        call SetUnitTimeScale(u, AS / 100.)
        call SetUnitVertexColor(u, 150, 150, 150, 150)
        call SetUnitPosition(u, x2 + 70 * Cos(GetRandomReal(180, -180 * bj_PI)), y2 + 70 * Sin(GetRandomReal(-1, 1 * bj_PI)))
        set x1 = GetUnitX(u)
        set y1 = GetUnitY(u)      
        call SetUnitFacingTimed(u,  bj_RADTODEG * Atan2(y2 - y1, x2 - x1), .05)
        call SetUnitAnimation(u, "attack")
        call DestroyEffect(AddSpecialEffectTarget(EFB, u, "origin"))
        call UnitDamageTarget(u, t, Damage, true, false, A_Type, D_Type, W_Type)
        if UnitAddAbility(u, 'Amrf') and UnitRemoveAbility(u, 'Amrf') then
        endif
        call SetUnitFlyHeight(u, 70, 1000)
        call TriggerSleepAction(.05)
        loop
            set ii = ii + 1
            exitwhen ii > Slash or GetWidgetLife(u) < 0.405
            if GetWidgetLife(t) > 0.405 then
                set x2 = GetUnitX(t)
                set y2 = GetUnitY(t)
                call SetUnitPosition(u, x2 + 70 * Cos(GetRandomReal(-180, 180 * bj_PI)), y2 + 70 * Sin(GetRandomReal(-180, 180 * bj_PI)))
                set x1 = GetUnitX(u)
                set y1 = GetUnitY(u)
                call SetUnitFacingTimed(u, bj_RADTODEG * Atan2(y2 - y1, x2 - x1), .05)
                call SetUnitAnimation(u, "attack")
                call DestroyEffect(AddSpecialEffectTarget(EF, t, "chest"))
                call DestroyEffect(AddSpecialEffectTarget(EFB, u, "origin"))
                call UnitDamageTarget(u, t, Damage, true, false, A_Type, D_Type, null)
                //set Slash = Slash - 1
            elseif GetWidgetLife(t) < 0.405 then
                call SetHeroAgi(u, GetHeroAgi(u, false) + R2I(.20), true)
            endif
            call TriggerSleepAction(.05)
        endloop
        call SetUnitFlyHeight(u, 0, 1000)
        set x2 = GetUnitX(t)
        set y2 = GetUnitY(t)
        set x1 = GetUnitX(u)
        set y1 = GetUnitY(u)      
        call SetUnitFacingTimed(u, bj_RADTODEG * Atan2(y2 - y1, x2 - x1), .05)
        call PauseUnit(u, false)
        call SetUnitPathing(u, true)
        call SetUnitTimeScale(u, 1.)
        call SetUnitVertexColor(u, 250, 250, 250, 250)
        call DestroyEffect(Eff)
        call IssueTargetOrder(u, "attack", t)
        set Eff = null
        set u = null
        set t = null
    endmethod

    private static method onInit takes nothing returns nothing
        local trigger tt = CreateTrigger()
        local integer i = 0
        loop
            exitwhen i > 15
            call TriggerRegisterPlayerUnitEvent(tt, Player(i), EVENT_PLAYER_UNIT_ATTACKED, Filter(function thistype.True))
            set i = i + 1
        endloop
        call TriggerAddCondition(tt, Condition(function Slash.Conditions))
        call TriggerAddAction(tt, function Slash.Actions)
        call Preload(EF)
    endmethod

endstruct

Here is the struct version. I did a great deal of converting red-lined functions into native ones.

Come to think of it, you don't really need a filter function in the line call TriggerRegisterPlayerUnitEvent(tt, Player(i), EVENT_PLAYER_UNIT_ATTACKED, Filter(function thistype.True)). You can just replace the filter function with null.
 
Status
Not open for further replies.
Top