HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Help improving efficiency

07-30-2008, 11:12 PM#1
Feroc1ty
I was wondering if anyone could help me improve my codes efficiency for a map I'm working on, will greatly appreciate help.

Collapse JASS:
scope Shoot initializer Init
private struct spelldata
    real x = 0.0
    real y = 0.0
    real z = 0.0
    real distance = 0.0
    real angle = 0.0
    unit bullet = null
    real xvel = 0.0
    real yvel = 0.0
    private method onDestroy takes nothing returns nothing
        if .bullet != null then
            call RemoveUnit( .bullet )
            set .bullet = null
        endif
    endmethod
endstruct

private function collision takes nothing returns boolean
    local unit u = GetFilterUnit()
    local real uz = GetZ(GetUnitX(u), GetUnitY(u)) + GetUnitFlyHeight(u)
    local real tz = GetZ(GetUnitX(tempUnit), GetUnitY(tempUnit)) + GetUnitFlyHeight(tempUnit)
    if tempUnit != u and GetUnitTypeId( u ) == 'hpea' and uz > (tz-5) and uz < (tz+5) then
        call DestroyEffect( AddSpecialEffect( "Abilities\\Spells\\Items\\SpellShieldAmulet\\SpellShieldCaster.mdl", GetUnitX(u), GetUnitY(u) ) )
        call SetWidgetLife( u, 0.2 )
        set tempBoo = true
        set u = null
        return false
    elseif tempUnit != u and GetWidgetLife(u) > 0.405 and uz > (tz-15) and uz < (tz+15) then
        call IssueImmediateOrder( u, "innerfireoff" )
        set bj_lastCreatedUnit = CreateUnit( GetOwningPlayer( tempUnit ), 'dumy', -2040, -2040, 0)
        call UnitAddAbility( bj_lastCreatedUnit, 'bble')
        call IssueTargetOrderById( bj_lastCreatedUnit , OrderId("acidbomb"), u )
        call UnitApplyTimedLife( bj_lastCreatedUnit, 'BTLF', 0.5 )
        call UnitDamageTarget( tempUnit, u, GetRandomReal( 200, 1000 ), false, true, ATTACK_TYPE_PIERCE, DAMAGE_TYPE_FORCE, WEAPON_TYPE_WHOKNOWS)
        call DestroyEffect( AddSpecialEffect( "Objects\\Spawnmodels\\Critters\\Albatross\\CritterBloodAlbatross.mdl", GetUnitX(u), GetUnitY(u) ) )
        set tempBoo = true
    endif
    set u = null
    return false
endfunction

private function Loop takes nothing returns boolean
    local spelldata a = TT_GetData()
    local real zdiff = 0
    if GetWidgetLife(a.bullet) < 1 then
        call a.destroy()
        return true
    endif
    set a.distance = a.distance + (16)
    set zdiff = GetZ( a.x, a.y )
    set a.x = a.x + a.xvel
    set a.y = a.y + a.yvel
    set zdiff = zdiff - GetZ( a.x, a.y )
    set a.z = a.z + zdiff
    if a.z <= 0 then
        call a.destroy()
        return true
    endif
    if a.x > 2000 or a.x < -2000 then
        call a.destroy()
        return true
    endif
    if a.y > 2000 or a.y < -2000 then
        call a.destroy()
        return true
    endif
    call SetUnitFlyHeight( a.bullet, a.z , 9999 )
    call SetUnitX( a.bullet, a.x )
    call SetUnitY( a.bullet, a.y )
    set tempUnit = a.bullet
    call GroupClear( tempGroup )
    set tempBoo = false
    set tempBool = Condition(function collision)
    call GroupEnumUnitsInRange( tempGroup, a.x, a.y, 12, tempBool)
    if tempBoo == true then
        call a.destroy()
        return true
    endif
    if a.distance > 3000 then
        call a.destroy()
        return true
    endif
    return false
endfunction

private function Conditions takes nothing returns boolean
    return GetSpellAbilityId() == 'shot'
endfunction

private function Actions takes nothing returns nothing
    local spelldata a = spelldata.create()
    local unit u = GetTriggerUnit()
    local player p = GetOwningPlayer(u)
    set a.angle = (bj_RADTODEG * Atan2(GetLocationY(GetSpellTargetLoc()) - GetUnitY(u), GetLocationX(GetSpellTargetLoc()) - GetUnitX(u)))
    set a.bullet = CreateProjectile( p, 0, 0, a.angle, "Abilities\\Weapons\\MakuraMissile\\MakuraMissile.mdl" )
    set a.x = GetUnitX( u ) + ( 5 ) * Cos( a.angle * bj_DEGTORAD )
    set a.y = GetUnitY( u ) + ( 5 ) * Sin( a.angle * bj_DEGTORAD )
    set a.xvel = 16 * Cos( a.angle * bj_DEGTORAD )
    set a.yvel = 16 * Sin( a.angle * bj_DEGTORAD )
    set a.z = 20.0
    call TT_Start( function Loop, a )
endfunction

private function Init takes nothing returns nothing
    set gg_trg_Shoot = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Shoot, EVENT_PLAYER_UNIT_SPELL_CAST )
    call TriggerAddCondition( gg_trg_Shoot, Condition( function Conditions ) )
    call TriggerAddAction( gg_trg_Shoot, function Actions )
endfunction
endscope


Edit: Also, now I'm currently working on a grenade spell, similar to this, so I'm not going to post it, but I'm stumped on how to make realistic bounce off uneven terrain.

Edit2: Nevermind on the bounce off, my clan leader helped me.
07-31-2008, 09:18 AM#2
Jazradel
I only had a quick look, but I mostly found really minor things.

Collapse JASS:
    set a.angle = (bj_RADTODEG * Atan2(GetLocationY(GetSpellTargetLoc()) - GetUnitY(u), GetLocationX(GetSpellTargetLoc()) - GetUnitX(u)))
/ettc
    set a.x = GetUnitX( u ) + ( 5 ) * Cos( a.angle * bj_DEGTORAD )
//etc
See those two lines, and how you have bj_RADTODEG in one and bj_DEGTORAD in the other? They just cancel out, so you can remove them entirely.

Collapse JASS:
    if a.x > 2000 or a.x < -2000 then
        call a.destroy()
        return true
    endif
    if a.y > 2000 or a.y < -2000 then
        call a.destroy()
        return true
    endif
Combining these into a single if statement will be a slight help. Same with the two at the bottom.

I don't see the need for the tempBool variable.

You could store Cos/Sin( a.angle ) in a variable in Actions so you don't call it twice.

Collapse JASS:
       set u = null
        return false
In the if bracket in collision are redundant.
07-31-2008, 09:58 AM#3
Feroc1ty
Well, I don't want my script hitting two objects, after it hit one already, so it will just ignore rest of the code.