• 🏆 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!

[JASS] im a failure :< cant solve this shit

Status
Not open for further replies.
Level 11
Joined
Apr 6, 2008
Messages
760
been working on this shit spell for a long time now and i can't get it work properly :/. the problem is that the spell will move the wrong way.

(ignore the codes crappyness)

JASS:
scope meteor initializer init

private struct Data
unit caster
unit dummy

real PosX
real PosY
real PosZ

real startX
real startY
real startZ
real speedX
real speedY
real speedZ
real endtime
real time
real cos
real sin


static integer array Ar
static integer Total = 0
static timer Time = CreateTimer()
    
    static method create takes unit u,location Tloc returns Data
        local Data Dat = Data.allocate()
        local real StartX = GetUnitX(u)
        local real StartY = GetUnitY(u)
        local real StartZ = 500
        local real endX = GetLocationX(Tloc)
        local real endY = GetLocationY(Tloc)
        local real endZ = 0
        local real distX = endX - StartX
        local real distY = endY - StartY
        local real distZ = endZ - StartZ
        local real angle = Atan2(distY,distX)
        local real dist = SquareRoot((distX*distX)+(distY*distY)+(distZ*distZ))

        set Dat.caster = u
        set Dat.startX = StartX
        set Dat.startY = StartY
        set Dat.startZ = 500.
        set Dat.endtime = dist/300
        set Dat.speedX = distX / Dat.endtime
        set Dat.speedY = distY / Dat.endtime
        set Dat.speedZ = distZ / Dat.endtime
        set Dat.time = 0.
        set Dat.cos = Cos(angle)
        set Dat.sin = Sin(angle)
        
        set Dat.dummy = CreateUnit(GetOwningPlayer(Dat.caster),'hfoo',StartX,StartY,angle*bj_RADTODEG)
        
        call UnitAddAbility(Dat.dummy,'Amrf')
        call SetUnitFlyHeight(Dat.dummy,500,0.)
        call UnitRemoveAbility(Dat.dummy,'Amrf')
        
        
        if Dat.Total == 0 then
            call TimerStart(Dat.Time,.03,true,function Data.Loop)
        endif
                
        set Dat.Ar[Dat.Total] = Dat
        set Dat.Total = Dat.Total + 1
        
        call RemoveLocation(Tloc)
        
        set Tloc = null
        set u = null
        
        return Dat
    endmethod
    
    static method Loop takes nothing returns nothing
        local Data Dat
        local integer i = 0
        local real x
        local real y
        local real z
        
        loop
            exitwhen i >= Dat.Total
            set Dat = Dat.Ar[i]
            
            set Dat.time = Dat.time + .03
            
            if Dat.time < Dat.endtime then
                set x = Dat.startX + Dat.speedX * Dat.time
                set y = Dat.startY + Dat.speedY * Dat.time
                set z = Dat.startZ + Dat.speedZ * Dat.time
                
                
                call SetUnitX(Dat.dummy,x * Dat.cos)
                call SetUnitY(Dat.dummy,y * Dat.sin)
                call SetUnitFlyHeight(Dat.dummy,z,0.)
            else
                set Dat.Total = Dat.Total - 1
                set Dat.Ar[i] = Dat.Ar[Dat.Total]
                set i = i - 1
                call Dat.destroy()
            endif
            
            set i = i + 1
        endloop
            
        if Dat.Total == 0 then
            call PauseTimer(Dat.Time)
        endif
    endmethod
    
    method onDestroy takes nothing returns nothing
        set .caster = null
        set .dummy = null
    endmethod
endstruct

private function OnCast takes nothing returns boolean
    local unit u
    local location Tloc
    
    if GetSpellAbilityId()=='A04Q' then
        set u = GetTriggerUnit()
        set Tloc = GetSpellTargetLoc()
        call Data.create(u,Tloc)
        call RemoveLocation(Tloc)
        set u = null
        set Tloc = null
    endif

    return false
endfunction

private function init takes nothing returns nothing
    local trigger Trig = CreateTrigger()
    
    call TriggerRegisterAnyUnitEventBJ(Trig,EVENT_PLAYER_UNIT_SPELL_EFFECT)
    call TriggerAddCondition(Trig,Filter(function OnCast))
    
    set Trig = null
endfunction

endscope
 
Last edited:
Level 6
Joined
Mar 20, 2008
Messages
208
JASS:
                call SetUnitX(Dat.dummy,x)
                call SetUnitY(Dat.dummy,y)

try this.

It looks like he has that under his if clause.

Anyway, I suggest to throw in some debugging code, use the gamedisplay message to make sure you are hitting that if-then clause.
 
Level 4
Joined
Nov 23, 2007
Messages
113
"im a failure :< cant solve this shit"
Every programmer has bugs in their code now and again. Relax and don't be so hard on yourself as that only makes it harder to find the bugs ;)
 
Level 23
Joined
Nov 29, 2006
Messages
2,482
I think it is this part which is wrong:
JASS:
            if Dat.time < Dat.endtime then
                set x = Dat.startX + Dat.speedX * Dat.time
                set y = Dat.startY + Dat.speedY * Dat.time
                set z = Dat.startZ + Dat.speedZ * Dat.time


                call SetUnitX(Dat.dummy,x * Dat.cos)
                call SetUnitY(Dat.dummy,y * Dat.sin)
                call SetUnitFlyHeight(Dat.dummy,z,0.)
            else

What you have done is to reduce the full coordinates of the x, y with cos and sin. I think it should would by simply moving the cos and sin:

JASS:
                set x = Dat.startX + Dat.speedX * Dat.time * Dat.cos
                set y = Dat.startY + Dat.speedY * Dat.time * Dat.sin
                set z = Dat.startZ + Dat.speedZ * Dat.time


                call SetUnitX(Dat.dummy,x)
                call SetUnitY(Dat.dummy,y)
                call SetUnitFlyHeight(Dat.dummy,z,0.)
 
Level 29
Joined
Jul 29, 2007
Messages
5,174

Equivalent results.
The same way 2*2*2 and 2*2 and the result later on multiplied by 2 will both be 8.

As to the thread

JASS:
Dat.endtime
isn't initialized in context
JASS:
set Dat.speedX = distX / Dat.endtime
set Dat.speedY = distY / Dat.endtime
set Dat.speedZ = distZ / Dat.endtime

And C++ just gives some random (they are probably not really random, but I have no idea what's their meaning) values to uninitialized variables.
 
Level 23
Joined
Nov 29, 2006
Messages
2,482
No?

(Dat.startX + Dat.speedX * Dat.time) * Dat.cos

and
Dat.startX + Dat.speedX * Dat.time * Dat.cos


I rest my case...

But I didn't spot the initialized variable. That is probably the issue too.
 
Level 4
Joined
Nov 23, 2007
Messages
113
And C++ just gives some random (they are probably not really random, but I have no idea what's their meaning) values to uninitialized variables.

Your assumption is correct, they are not randomly "generated". The default value is whatever was in that memory space prior to dynamic allocation (or on the local heap if it is a local variable). ie., the contents are unpredictable.
 
Level 14
Joined
Nov 18, 2007
Messages
816
@Eccho: removing cos and sin completely would be correct, not moving them around, since that doesnt fix anything.

@GhostWorlf: Dat.endtime is initialized.

@Ciebron: Replace "Dat.Total" with "Data.Total", "Dat.Ar" with "Data.Ar" and "Dat.Time" with "Data.Time"
 
Status
Not open for further replies.
Top