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: 279

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.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +1
  • V-SNES V-SNES:
    Happy Friday!
    +1

      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