Projectile Help

Rllulium

New Member
Reaction score
10
Hello Again.

I picked up JASS (very) recently, deciding that it wasn't nearly as complicated as I first had anticipated. While knowledge of GUI and some general programming gave me a head start, problems are gathering up as soon as I attempt anything more complicated. I'd appreciate it if someone could take a look at this projectile spell and tell me what is wrong:
JASS:
globals
trigger PjMoveTrigger
integer PjIndex = 0
integer PjUserData
integer array PjSpellLevel
real array PjRange
real array PjDistance
real array PjAngle
unit array PjCaster
location PjStart
location PjClick
location PjLocation
location PjMovement
group PjGroup = CreateGroup()
group PjTarget = CreateGroup()
group PjRandom = CreateGroup()
endglobals

function PjConditions takes nothing returns boolean
    return GetSpellAbilityId() == 'A004'
endfunction

function PjActions takes nothing returns nothing
    set PjIndex = (PjIndex + 1)
    set PjCaster[PjIndex] = GetSpellAbilityUnit()
    set PjSpellLevel[PjIndex] = GetUnitAbilityLevelSwapped('A004', PjCaster[PjIndex])
    set PjStart = GetUnitLoc(PjCaster[PjIndex])
    set PjClick = GetSpellTargetLoc()
    set PjAngle[PjIndex] = AngleBetweenPoints(PjStart, PjClick)
    call CreateUnitAtLocSaveLast(GetOwningPlayer(PjCaster[PjIndex]),'e000',PolarProjectionBJ(PjStart, 20., PjAngle[PjIndex]) , PjAngle[PjIndex])
    call SetUnitPathing( GetLastCreatedUnit(), false )
    call SetUnitUserData( GetLastCreatedUnit(), PjIndex)
    call GroupAddUnit(PjGroup, GetLastCreatedUnit())
    set PjRange[PjIndex] = 1000.
    set PjDistance[PjIndex] = 0.
    call RemoveLocation(PjStart)
    call RemoveLocation(PjClick)
    if(not( IsTriggerEnabled(PjMoveTrigger)))then
        call EnableTrigger(PjMoveTrigger)
    endif
endfunction

//===========================================================================
function InitTrig_Projectile_Setup takes nothing returns nothing
    local trigger t = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_SPELL_CAST )
    call TriggerAddCondition( t, Condition( function PjConditions ) )
    call TriggerAddAction( t, function PjActions )
endfunction

JASS:
function PjTargetCheck takes nothing returns boolean
    local boolean b = false
    if (IsUnitEnemy(GetFilterUnit(), GetOwningPlayer(GetEnumUnit())) == true and IsUnitAliveBJ(GetFilterUnit()) == true) then
        set b = true
    endif
    return b
endfunction

function PjDamage takes nothing returns nothing
    call Damage_Spell(PjCaster[PjIndex],GetEnumUnit(),100)
endfunction

function PjMove takes nothing returns nothing
    set PjUserData = GetUnitUserData(GetEnumUnit())
    set PjLocation = GetUnitLoc(GetEnumUnit())
    set PjMovement = Location(GetLocationX(PjLocation) + Cos(PjAngle[PjUserData])*20, GetLocationY(PjLocation) + Sin(PjAngle[PjUserData])*20)
    call SetUnitPositionLoc( GetEnumUnit(), PjMovement )
    set PjDistance[PjUserData] = ( PjDistance[PjUserData] + 20.00 )
    set PjTarget = GetUnitsInRangeOfLocMatching(100.00, PjMovement, Condition(function PjTargetCheck))
    set PjRandom = GetRandomSubGroup(1, PjTarget)
    call ForGroup( PjRandom, function PjDamage )
    if ( CountUnitsInGroup(PjTarget) > 0 ) then
        call KillUnit( GetEnumUnit() )
        call GroupRemoveUnit(PjGroup, GetEnumUnit())
    endif
    if( PjDistance[PjUserData]>= PjRange[PjUserData])then
        call KillUnit( GetEnumUnit() )
        call GroupRemoveUnit(PjGroup, GetEnumUnit())
    endif
    call RemoveLocation(PjLocation)
    call RemoveLocation(PjMovement)
    call DestroyGroup(PjTarget)
    call DestroyGroup(PjRandom)
endfunction

function PjMoveActions takes nothing returns nothing
    call ForGroup(PjGroup, function PjMove)
    if ( CountUnitsInGroup(PjGroup) == 0 ) then
        call DisableTrigger( GetTriggeringTrigger() )
    endif
endfunction

//===========================================================================
function InitTrig_Projectile_Movement takes nothing returns nothing
    set PjMoveTrigger = CreateTrigger(  )
    call DisableTrigger( PjMoveTrigger )
    call TriggerRegisterTimerEventPeriodic( PjMoveTrigger, 0.02 )
    call TriggerAddAction( PjMoveTrigger, function PjMove )
endfunction


Thanks in advance!
 

PrisonLove

Hard Realist
Reaction score
78
I know this doesn't really help but you can do this as a bit of optimization. (Sorry I don't have time to help in depth, I have to go to work):

This:
JASS:
function PjConditions takes nothing returns boolean
    local boolean b = false
    if (GetSpellAbilityId() == 'A004' ) then
        set b = true
    endif
    return b
endfunction


Can be turned into:
JASS:
function PjConditions takes nothing returns boolean
    return GetSpellAbilityId() == 'A004'
endfunction



I know that doesn't solve your problem, but just something I noticed.
 

Trollvottel

never aging title
Reaction score
262
JASS:
set PjIndex = GetUnitId(GetEnumUnit())


KK, quite high index for an array variable. the size of an array variable is 8190(+2).
Better use some hashing or GetUnitUserData or some Attachment system.
 

Rllulium

New Member
Reaction score
10
I appriciate the advice, thanks for that.

But... the trigger is still not doing anything beyond spawning the dummy at my feet.

EDIT: Updated the code in the original post to its current state.
 

saw792

Is known to say things. That is all.
Reaction score
280
Regardless of whether the spell works correctly or not, this is still coded exactly like a GUI spell. Basically what you're doing here is using a GUI MUI method in JASS, which is quite silly as we have much better ways of doing things in JASS.

Seeing as you are using a globals block I must assume you can use vJASS, so the first thing you should do is take a look at a basic vJASS tutorial, a timer and attachment tutorial and Jesus4Lyf's leakless groups tutorial. Then you can work on running the spell off a single trigger and a timer with a struct and global group, limiting the BJs you use and using x and y coordinates instead of locations.

Unfortunately not many people will be able to/bother to help you with the code in its current state as it is far too messy/overcomplicated/incorrectly implemented, which is why I made the suggestions above. Good luck :)
 

Rllulium

New Member
Reaction score
10
Regardless of whether the spell works correctly or not, this is still coded exactly like a GUI spell. Basically what you're doing here is using a GUI MUI method in JASS, which is quite silly as we have much better ways of doing things in JASS.

Seeing as you are using a globals block I must assume you can use vJASS, so the first thing you should do is take a look at a basic vJASS tutorial, a timer and attachment tutorial and Jesus4Lyf's leakless groups tutorial. Then you can work on running the spell off a single trigger and a timer with a struct and global group, limiting the BJs you use and using x and y coordinates instead of locations.

Unfortunately not many people will be able to/bother to help you with the code in its current state as it is far too messy/overcomplicated/incorrectly implemented, which is why I made the suggestions above. Good luck :)

Thanks. This is exactly the kind of criticism I was looking for. :D

So I remade the whole thing (heavily inspired by tutorials though :p), here it is; now working flawlessly according to 2 minutes of testing. Do please point at places where I might improve it.
JASS:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//      Projectile_Create(unit caster, real damage, real speed, real range, real angle)
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
library Projectile requires Damage

private struct Input
    unit Caster
    unit Proj
    real Damage
    real Speed
    real Range
    real Dist
    real Angle
    method Move takes nothing returns nothing
        call SetUnitX(.Proj,GetUnitX(.Proj)+20*Sin(.Angle))
        call SetUnitY(.Proj,GetUnitY(.Proj)+20*Cos(.Angle))
        set .Dist = .Dist + .Speed
    endmethod
endstruct

globals
    private integer Count = 0
    private integer Index
    private group target = CreateGroup()
    private Input array Pj
    private timer Timer = CreateTimer()
endglobals

private function Impact takes nothing returns boolean
if IsUnitEnemy(GetFilterUnit(),GetOwningPlayer(Pj[Index].Caster)) then
    call Damage_Spell(Pj[Index].Caster,GetFilterUnit(),Pj[Index].Damage)
    set Pj[Index].Dist = Pj[Index].Range + 20
endif
return false
endfunction

private function Action takes nothing returns nothing
    set Index = 0
    loop
        exitwhen Index >= Count
        call GroupEnumUnitsInRange(target, GetUnitX(Pj[Index].Proj),GetUnitY(Pj[Index].Proj),100,Filter(function Impact))
        if Pj[Index].Dist > Pj[Index].Range then
            call KillUnit(Pj[Index].Proj)
            call Pj[Index].destroy()
            set Count = Count-1
            if Count > 0 then
                set Pj[Index] = Pj[Count]
                set Index = Index-1
            else
                call PauseTimer(Timer)
            endif
        else
            call Pj[Index].Move()
        endif
        set Index = Index + 1
    endloop
endfunction

public function Create takes unit c, real d, real s, real r, real a returns nothing
    local Input Temp = Input.create()
    set Temp.Caster = c
    set Temp.Damage = d
    set Temp.Speed = s
    set Temp.Range = r
    set Temp.Angle = a
    set Temp.Dist = 0
    call CreateUnitAtLocSaveLast(GetOwningPlayer(Temp.Caster),'e000',Location(GetUnitX(Temp.Caster),GetUnitY(Temp.Caster)),Temp.Angle)
    set Temp.Proj = GetLastCreatedUnit()
    if Count == 0 then
        call TimerStart(Timer,0.02,true,function Action)
    endif
    set Pj[Count] = Temp
    set Count = Count + 1
endfunction

endlibrary
 
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