HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

RocketFunctions are causing lag after multiple casts

08-03-2007, 05:31 PM#1
Lordy
Hi,
I've written a few function to make a rocket go woosh :D
But retardedly, my functions are also the cause of lagz : /
I can cast the rocket spell quite a lot of times (10times) before it starts to lag, but then the lag is there and it gets bigger with every time I cast the spell.
I've already replaced a lot of handle vars with structs, but the lag is still alive.
I don't know where this lag is coming from!!
I appreciate it if you could look at the function and try to find the source of my lagz. If you want to test my rocket spell then you can. The map with it is attached in this post. You need to use the Tinker hero. First cast his ultimate on one of the rifles, then press "e" to cast Hell Fires and the rocket will woosh.
Thanks,
Remy.


Collapse JASS:
function SmartRocketFilter takes nothing returns boolean
    return TargetTypeCondition(GetFilterUnit(),udg_tempunit2,udg_tempint)
endfunction

struct RocketUpdateTimer
    unit Missile
    unit Target
    real HitHeight
    real Speed
    real oldAoA
    real oldFacing
    real mindHeight
    real SteerSpeed
    integer filterindex
    integer ticks
    string EndFunc
endstruct

function SmartRocketUpdate takes nothing returns nothing
    local timer Timer = GetExpiredTimer()
    local string TimerString = I2S(H2I(Timer))
    local RocketUpdateTimer timerstruct = GetHandleInt(Timer,"struct")
    
    local unit Missile = timerstruct.Missile
    local unit Target = timerstruct.Target
    local real HitHeight = timerstruct.HitHeight
    local real Speed = timerstruct.Speed
    local real oldAoA = timerstruct.oldAoA
    local real oldFacing = timerstruct.oldFacing
    local real mindHeight = timerstruct.mindHeight
    local real SteerSpeed = timerstruct.SteerSpeed
    local integer filterindex = timerstruct.filterindex
    local integer ticks = ModuloInteger(timerstruct.ticks,2)
    
    local real MX = GetWidgetX(Missile)
    local real MY = GetWidgetY(Missile)
    local real TX = GetWidgetX(Target)
    local real TY = GetWidgetY(Target)
    local real Dist = Distance(MX,MY,TX,TY)
    local real oldMHeight = GetUnitFlyHeight(Missile)
    local real THeight = GetUnitFlyHeight(Target)
    
    local real TAoA = Angle(0,oldMHeight,Dist,THeight+HitHeight)
    local real TFacing = Angle(MX,MY,TX,TY)
    local real TdAoA = TAoA - oldAoA
    
    local real TdFacing = TakeSmallestAngle(oldFacing,TFacing)
    local real SteerAngle = Atan2(TdAoA,-TdFacing)   //negative facing because left for the rocket is counterclockwise is a negative angle.
    local real dAoA = SteerSpeed*Sin(SteerAngle)
    local real dFacing = SteerSpeed*-Cos(SteerAngle) //same reason for the negative
    local real newAoA
    local real newFacing
    local real dHeight
    local real newMHeight
    local real HoriSpeed
    local real dX
    local real dY
    local group collisiongroup = CreateGroup()
    local real radius = 40
    local string endfunc
    local unit collider
    
    set timerstruct.ticks = ticks+1
    
    if dFacing > 0 then
        if dFacing > TdFacing then
            set dFacing = TdFacing
        endif
    else
        if dFacing < TdFacing then
            set dFacing = TdFacing
        endif
    endif
    
    if dAoA > 0 then
        if dAoA > TdAoA then
            set dAoA = TdAoA
        endif
    else
        if dAoA < TdAoA then
            set dAoA = TdAoA
        endif
    endif
    
    set newAoA = oldAoA+dAoA
    set newFacing = oldFacing+dFacing
    set dHeight = Speed*FlyingUpdateInterval()*Sin(newAoA)
    set newMHeight = oldMHeight + dHeight
    set HoriSpeed = Speed*Cos(newAoA)
    set dX = FlyingUpdateInterval() * HoriSpeed * Cos(newFacing)
    set dY = FlyingUpdateInterval() * HoriSpeed * Sin(newFacing)
    
    if newFacing > bj_PI then  //Is this nessecary?
        set newFacing = newFacing-2*bj_PI
        call Msg("rightythere")
    else
        if newFacing < -bj_PI then
            set newFacing = newFacing+2*bj_PI
            call Msg("rightythere2")
        endif
    endif
    
    call Msg("SmartRocketUpdate")
    call Msg("oldAoA: "+R2S(oldAoA))
    call Msg("oldFacing: "+R2S(oldFacing))
    call Msg("TAoA: "+R2S(TAoA))
    call Msg("TFacing: "+R2S(TFacing))
    call Msg("TdAoA: "+R2S(TdAoA))
    call Msg("TdFacing: "+R2S(TdFacing))
    call Msg("SteerAngle: "+R2S(SteerAngle))
    call Msg("SteerSpeed: "+R2S(SteerSpeed))
    call Msg("dAoA: "+R2S(dAoA))
    call Msg("dFacing: "+R2S(dFacing))
    call Msg("newAoA: "+R2S(newAoA))
    call Msg("newFacing: "+R2S(newFacing))
    call Msg("dHeight: "+R2S(dHeight))

    call SetUnitFacing(Missile,newFacing*bj_RADTODEG)
    if ticks == 0 then
        //call SetUnitLookAt(Missile,"Bone_Chest",Missile,1000*dX,1000*dY,1000*dHeight)
        call SetUnitLookAt(Missile,"Bone_Chest",Missile,1000*dX,0,1000*dHeight)
    endif
    
    set udg_tempunit2 = Missile
    set udg_tempint = filterindex
    call GroupEnumUnitsInRange(collisiongroup,MX,MY,radius,Condition(function SmartRocketFilter))

    loop
        set collider = FirstOfGroup(collisiongroup)
        if collider == null then
            exitwhen true
        endif
        set THeight = GetUnitFlyHeight(collider)
        if RAbsBJ(THeight+HitHeight-newMHeight) < mindHeight then
            exitwhen true
        else    
            call GroupRemoveUnit(collisiongroup,collider)
        endif
    endloop
    
    if (collider != null) or (not IsPointInRegion(udg_playground,MX+dX,MY+dY)) then
        call Msg("HomingRocketEnd")
        set newMHeight = THeight+HitHeight
        set TX = GetWidgetX(collider)
        set TY = GetWidgetY(collider)
        call SetUnitCor(Missile,TX,TY)
        call SetUnitFlyHeight(Missile, newMHeight, 0)
        set udg_tempunit = collider
        set udg_tempstring = TimerString
        set endfunc = timerstruct.EndFunc
        call timerstruct.Destroy
        call ExecuteFunc(endfunc)
        call PauseTimer(Timer)
        call DestroyTimer(Timer)
        call FlushStoredMission(udg_gamecache,TimerString)
    else
        set timerstruct.oldAoA = newAoA
        set timerstruct.oldFacing = newFacing
        call SetUnitCor(Missile, MX+dX, MY+dY)
        call SetUnitFlyHeight(Missile, newMHeight, 0)
    endif
    
    call DestroyGroup(collisiongroup)
    set collisiongroup = null
    set Missile = null
    set Target = null
    set Timer = null
endfunction

function StartRocket takes unit missile, unit target, real hitheight, real aoa, real h0, real speed, string endfunc, real steerspeed, real facing, real mindHeight, integer filterindex returns timer
    local timer t = CreateTimer()
    local RocketUpdateTimer timerstruct = RocketUpdateTimer.create()
    
    set timerstruct.Missile = missile
    set timerstruct.Target = target
    set timerstruct.HitHeight = hitheight
    set timerstruct.Speed = speed
    set timerstruct.oldAoA = aoa
    set timerstruct.oldFacing = facing
    set timerstruct.mindHeight = mindHeight
    set timerstruct.SteerSpeed = steerspeed
    set timerstruct.filterindex = filterindex
    set timerstruct.EndFunc = endfunc
     
    call Msg("setting aoa to: "+R2S(aoa))
    call Msg("setting steerspeed to: "+R2S(steerspeed))
    call Msg("setting facing to: "+R2S(facing*bj_DEGTORAD))
    
    call SetHandleInt(t,"struct",timerstruct)
    call SetUnitFlyHeight(missile,h0,0)
    call TimerStart(t,FlyingUpdateInterval(),true,function SmartRocketUpdate)
    return t
endfunction
Attached Files
File type: w3xherotestmapremy2.2.w3x (693.8 KB)
08-03-2007, 07:19 PM#2
blu_da_noob
What timer interval are you using? And you should really be running them all off one timer.
08-03-2007, 07:40 PM#3
Lordy
The interval is 0.02, so pretty low.
I have a high-end machine though. The rockets do not lag at all when I first cast the skill. The lag only comes after multiple casts. Why does the lag increase? That's what I need to know.

About putting all the rockets in 1 timer: yeah I could do that. The different rockets don't have anything in common though. They are no shared calculations/variables.
08-04-2007, 01:15 AM#4
grim001
have you tried getting rid of the debug messages in the callback? game messages lag incredibly badly when you spam them
08-04-2007, 01:39 AM#5
Lordy
grim001: I assumed it wasn't the messages because the lag _increases_ each time I cast the ability.
Now, after removing the messages and changes some other tiny things (I can't remember)... The lag is gone! I don't believe it, but it is =) *Happiez*
I started using vJass today and now I'm happiez.