Snippet MissileDamage

Reaction score
91
I also noticed this in my last script:
JASS:

        set x2 = GetUnitX(d.targ)
        set y2 = GetUnitX(d.targ) // <----


Will look over the mistakes you pointed and going to edit if there are still some problems...
EDIT: Now the damage is instant - the snippet doesn't wait for the missile at all. I also noticed that the timer isn't paused in the end.
EDIT2: And only the first assigned instance of the struct causes damage (I mean the first unit that casts the spell will deal damage, other MDs after it do nothing). Shouldn't it be better if I used dynamic timers?

JASS:

library MD

//===========================================================================
// CONFIGURE IF NECESSARY
//===========================================================================
globals
// Period of the timer.
    private constant real PERIOD = 0.03125 
// Minimum damage required for the unit to be damaged.
// If it's less than 100 then the unit will be hurt,
// else the damaging will be delayed until the condition is met.
    private constant real LEAST_RANGE = 100.
endglobals
//===========================================================================

private keyword Data

globals
    private Data array D
    private integer I = 0
    private timer t = CreateTimer()
endglobals
    

private struct Data
    unit cast
    unit targ
    real dmg
    real x
    real y
    real s
    attacktype at
    damagetype dt
    
    static method create takes unit cast, unit targ, real dmg, real speed, attacktype at, damagetype dt returns Data
        local Data d = Data.allocate()
        set d.cast = cast
        set d.targ = targ
        set d.dmg = dmg
        set d.s = speed
        set d.at = at
        set d.dt = dt
        return d
    endmethod
endstruct

private function Callback takes nothing returns nothing
    local Data d 
    local integer i = 0
    local real x1
    local real y1
    local real x2     
    local real y2 
    local real xx
    local real yy 
    local real r 
    local real tr 
    loop
        exitwhen i > I
        set d = D<i>
        set x1 = d.x
        set y1 = d.y
        set x2 = GetUnitX(d.targ)
        set y2 = GetUnitY(d.targ)
        set xx = x1 - x2
        set yy = y1 - y2
        set r = SquareRoot(xx * xx + yy * yy)
        //set tr = r / d.s
        call BJDebugMsg(&quot;calculated range, ifs&quot;)
        if r &lt; LEAST_RANGE then
            if IsUnitVisible(d.targ, GetOwningPlayer(d.cast)) then
                if not IsUnitInvisible(d.targ, GetOwningPlayer(d.cast)) then
                    if GetWidgetLife(d.targ) &gt; 0.405 then
                        call UnitDamageTarget(d.cast, d.targ, d.dmg, true, true, d.at, d.dt, WEAPON_TYPE_WHOKNOWS)
                    endif
                endif
            endif
            call BJDebugMsg(&quot;d.destroy()&quot;)
            call d.destroy()
            set D<i> = D[ I ]
            set I = I - 1
        else
            call BJDebugMsg(&quot;d.x = x2; d.y = y2&quot;)
            set d.x = x2
            set d.y = y2
        endif
        if I == 0 then
            call BJDebugMsg(&quot;Paused timer&quot;)
            call PauseTimer(t)
        endif
        call BJDebugMsg(&quot;i = i + 1&quot;)
        set i = i + 1
    endloop
endfunction

public function Start takes unit cast, unit targ, real dmg, real speed, attacktype at, damagetype dt returns nothing
    local Data d = Data.create(cast, targ, dmg, speed, at, dt)
    local real x1 = GetUnitX(d.cast)
    local real y1 = GetUnitY(d.cast)
    local real x2 = GetUnitX(d.targ)
    local real y2 = GetUnitY(d.targ)
    local real xx = x1 - x2
    local real yy = y1 - y2
    local real r  = SquareRoot(xx * xx + yy * yy)
    local real tr = r / d.s    
    set d.x = x2
    set d.y = y2
    set D[ I ] = d
    set I = I + 1
    if I == 1 then
        call BJDebugMsg(&quot;Timer started&quot;)
        call TimerStart(t, PERIOD, false, function Callback)
    endif
endfunction

endlibrary
</i></i>
 

Flare

Stops copies me!
Reaction score
662
got it:
JASS:
 
//in function Start
    set I = I + 1
    set D<i> = d</i>

JASS:
 
//should be
    set D<i> = d
    set I = I + 1
</i>

That's not the problem - setting the global data array after incrementing the instance counter should work just fine

JASS:
            call BJDebugMsg(&quot;d.destroy()&quot;)
            call d.destroy()
            set D<i> = D[ I ]
            set I = I - 1</i>

(in Callback)

You should add
JASS:
set i = i -1

below the 'set D = D' line - let's say you have 20 instances

Instance 11 ends, instance 20 occupies the newly available slot (#20 --> #11)

I (instance counter) is decreased (I==19), and i (loop counter) is increased (i==12).

In the end, what was previously #20 (the new #11 instance) will be skipped by the loop. You just need to backtrack to previous array index (by setting i = i - 1 when ending an instance) and handle the data which was moved down the list.

Alternatively, loop from N to 0 i.e.
JASS:
local integer i = N //replace N with your instance counter var
loop
exitwhen i == 0 //or &lt; 0, depending on which way you&#039;ve arranged your instance increment (at top of this post)
...
set i = i - 1
endloop

That way, you handle the top index first, so even if #20 is moved down the list, it's already dealt with.
 
Reaction score
91
Thanks, Flare. Now it works fine except for a tiny little problem - the period somehow interferes with the damaging. It seems like it damages only when the timer ticks (e.g. every 1 second). Attached a test map if someone is interested...

JASS:

library MD

//===========================================================================
// CONFIGURE IF NECESSARY
//===========================================================================
globals
// Period of the timer.
    private constant real PERIOD = 1.
// Minimum damage required for the unit to be damaged.
// If it&#039;s less than 100 then the unit will be hurt,
// else the damaging will be delayed until the condition is met.
    private constant real LEAST_RANGE = 100.
endglobals
//===========================================================================

private keyword Data

globals
    private Data array D
    private integer N = 0
    private timer t = CreateTimer()
endglobals
    

private struct Data
    unit cast
    unit targ
    real dmg
    real x
    real y
    real s
    attacktype at
    damagetype dt
    
    static method create takes unit cast, unit targ, real dmg, real speed, attacktype at, damagetype dt returns Data
        local Data d = Data.allocate()
        set d.cast = cast
        set d.targ = targ
        set d.dmg = dmg
        set d.s = speed
        set d.at = at
        set d.dt = dt
        return d
    endmethod
endstruct

private function Callback takes nothing returns nothing
    local Data d 
    local integer i = N
    local real x1
    local real y1
    local real x2     
    local real y2 
    local real xx
    local real yy 
    local real r 
    local real tr 
    loop
        exitwhen i == 0
        set d = D<i>
        set x1 = d.x
        set y1 = d.y
        set x2 = GetUnitX(d.targ)
        set y2 = GetUnitY(d.targ)
        set xx = x1 - x2
        set yy = y1 - y2
        set r = SquareRoot(xx * xx + yy * yy)
        //set tr = r / d.s
        call BJDebugMsg(&quot;calculated range, ifs&quot;)
        if r &lt; LEAST_RANGE then
            if IsUnitVisible(d.targ, GetOwningPlayer(d.cast)) then
                if not IsUnitInvisible(d.targ, GetOwningPlayer(d.cast)) then
                    if GetWidgetLife(d.targ) &gt; 0.405 then
                        call UnitDamageTarget(d.cast, d.targ, d.dmg, true, true, d.at, d.dt, WEAPON_TYPE_WHOKNOWS)
                    endif
                endif
            endif
            call BJDebugMsg(&quot;d.destroy()&quot;)
            call d.destroy()
            set N = N - 1
            set D<i> = D[N]
        else
            call BJDebugMsg(&quot;d.x = x2; d.y = y2&quot;)
            set d.x = x2
            set d.y = y2
        endif
        if N &lt;= 0 then
            call BJDebugMsg(&quot;Paused timer&quot;)
            call PauseTimer(t)
        endif
        call BJDebugMsg(&quot;n = n + 1&quot;)
        set i = i - 1
    endloop
endfunction

public function Start takes unit cast, unit targ, real dmg, real speed, attacktype at, damagetype dt returns nothing
    local Data d = Data.create(cast, targ, dmg, speed, at, dt)
    local real x1 = GetUnitX(d.cast)
    local real y1 = GetUnitY(d.cast)
    local real x2 = GetUnitX(d.targ)
    local real y2 = GetUnitY(d.targ)
    local real xx = x1 - x2
    local real yy = y1 - y2
    local real r  = SquareRoot(xx * xx + yy * yy) 
    local real tr = r / d.s    
    set d.x = x2
    set d.y = y2
    set D[N] = d
    set N = N + 1
    if N == 1 then
        call BJDebugMsg(&quot;Timer started&quot;)
        call TimerStart(t, PERIOD, false, function Callback)
    endif
endfunction

endlibrary
</i></i>
 

Attachments

  • MissileDamage.w3x
    20.2 KB · Views: 272

Flare

Stops copies me!
Reaction score
662
Well, it's understandable that damage only occurs upon timer tick - the timer function is executed when the timer expires, so the damage can't be dealt anytime between timer expirations - just drop the timer interval a bit (0.25 - 0.5s should be fine, noone should notice). Anyway, it's configurable, if people want more accuracy, then can bring it down a bit. Highly unlikely that lag could result (unless alot of projectiles were in motion), but may as well stay on the safe side. When people can make out a noticeable difference between time of impact, and time of damage, then whoever is using the system can increase the frequency.

Assuming I've interpreted what you've said correctly
 
Reaction score
91
> Assuming I've interpreted what you've said correctly
You did, however I used 0.03125 period before and the damaging is almost instant after MD_Start() is ran (after 0.03125 second)...
 

Flare

Stops copies me!
Reaction score
662
Oh, it's damaging on the first timer execution...

This part is looking a bit suspicious
JASS:
//Here...
        set x2 = GetUnitX(d.targ)
        set y2 = GetUnitY(d.targ)
        set xx = x1 - x2
        set yy = y1 - y2
        set r = SquareRoot(xx * xx + yy * yy)
        //set tr = r / d.s
        call BJDebugMsg(&quot;calculated range, ifs&quot;)
        if r &lt; LEAST_RANGE then
            if IsUnitVisible(d.targ, GetOwningPlayer(d.cast)) then
                if not IsUnitInvisible(d.targ, GetOwningPlayer(d.cast)) then
                    if GetWidgetLife(d.targ) &gt; 0.405 then
                        call UnitDamageTarget(d.cast, d.targ, d.dmg, true, true, d.at, d.dt, WEAPON_TYPE_WHOKNOWS)
                    endif
                endif
            endif
            call BJDebugMsg(&quot;d.destroy()&quot;)
            call d.destroy()
            set N = N - 1
            set D<i> = D[N]
        else
// and down here ^_^
            call BJDebugMsg(&quot;d.x = x2; d.y = y2&quot;)
            set d.x = x2
            set d.y = y2
        endif
</i>


If the distance is greater than LEAST_RANGE, you're updating the struct's XY members to the target's XY at timer execution. When the timer executes again, .x and .y will, very likely, be VERY close to the target

So, let's say...
Code:
Source:
X1 = 0
Y1 = 0

Target:
X2 = 200
Y2 = 0
First timer execution, all's well and good. r is 200, so it's greater than LEAST_RANGE

If condition returned false, moving onto the else. Here comes a problem
Code:
Source:
X1 = 200
Y1 = 0

Target:
X2 = 200
Y2 = 0
Distance var set, it ends up 0 (since (200-200)^2 = 0). 0 is below LEAST_RANGE, if condition returns true, we do our damage.

Only solution I can think of would be to obtain the angle between .x, .y and x2, y2 and store the projected values as .x, .y e.g.
JASS:
set angle = Atan2 (dy, dx) //I think the parameters would be yy, xx for you
set d.x = d.x + Cos (angle) * TIMER_INTERVAL * d.s
set d.y = d.y + Sin (angle) * TIMER_INTERVAL * d.s

You will need a fairly high frequency timer timer for accurate projection
 
Reaction score
91
Argh, can we end this once and for all with dynamic timers? I've never tried using a single timer that would handle multiple structs and at the moment I'm quite "fail" and ashamed...

> If condition returned false, moving onto the else. Here comes a problem
That's not a problem, it was intended. If the distance between the caster and the target is less than 100 (assume the units are right next to each other) there is not need to run another loop just because the missile would reach almost instantly. I guess nobody would make a missile that would travel less than 100 range per second. :rolleyes:

EDIT:
> Only solution I can think of would be to obtain the angle between .x, .y and x2, y2
How would that help?
 
Reaction score
91
Nevermind, I'll stick to the original version with TT.
I did a few changes:
- Included a test map.
- Fixed inaccurate calculations because d.y received the value y1 instead of y2.
- Now uses TT_Once() instead of TT_StartEx().
- Added a global for better configuration.
 
Reaction score
91
Bump. Anyone has any comments?

I added support for some other systems (ABC, HSAS, TimerUtils and CSData) in case someone prefers them...
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top