Chain lightning randomly crashing in random places

Frozenhelfir

set Gwypaas = Guhveepaws
Reaction score
56
I'm making a spell for a contest with a theme of randomness, but the point of my spell isn't to randomly crash threads in random places! :mad: I've been working on this for quite some time, and every time I fix one crash, another one or two seem to crawl out of the floor.

The thread seems to crash either before the FirstOfGroup loop on the second or third time around (never the first), after:

JASS:
call BJDebugMsg("CL.dist2 == " + R2S(CL.dist2))


The second crash appears when the distance between units is calculated to take the closest unit from the last chained unit.

JASS:
call BJDebugMsg("Dist1 == " + R2S(CL.dist1))


Here's a quick summary of how this trigger works. When the dummy spell is used, a struct containing some basic data is created and attached to a timer of random duration. When it expires it calls one of three functions and passes the attached struct into them for use. I'm currently working on getting the chain lightning to work, which is the first effect. When called, another struct is created based around needed variables for a jumping spell.

The first loop runs until the max number of bounces have been reached. In this loop a group gets all units in range, and runs them through a FirstOfGroup loop. The target of the spell is used as the center of this, and the loop will find the closest unit, or randomly exit on a further one. Then the caster deals damage to the target unit, and Flare's Timed Lightning system creates the effect.

This isn't the WHOLE whole trigger, but the map is included if you want to test this or take a look at all of the code. The problem is easier seen when the ability level is higher, since higher levels have more bounces.

The function and all related parts are here:
JASS:
private function NotDummy takes nothing returns boolean
    return GetUnitTypeId(GetFilterUnit()) != DUMMY_ID and GetWidgetLife(GetFilterUnit()) > .405
endfunction

private function ChainLightning takes Data D returns nothing
local Chain CL = Chain.create(D.target)
    
    call BJDebugMsg("ChainLightning called")
    set D.i = GetRandomInt(2,3) * D.level
    call TL_Unit ("CLPB", D.caster,CL.u, 1, false, 1, 0)
    call UnitDamageTarget(D.caster,CL.u,CHAIN_BASE_DMG + CHAIN_INC_DMG * D.level +GetRandomInt(-CHAIN_RDM_DMG * D.level,CHAIN_RDM_DMG * D.level) ,false,false,ATTACK_TYPE_MAGIC, DAMAGE_TYPE_UNIVERSAL,null)
    
    loop
        call GroupEnumUnitsInRange(CL.g,GetUnitX(CL.u),GetUnitY(CL.u), 400, Condition(function NotDummy))
        call BJDebugMsg("Creating new group")
        call GroupAddUnit(CL.immune,CL.u)
        call BJDebugMsg("Group created")
        set CL.dist2 = CHAIN_JUMP_RADIUS
        call BJDebugMsg("CL.dist2 == " + R2S(CL.dist2))
        
        loop
            set CL.v = FirstOfGroup(CL.g)
            if IsUnitInGroup(CL.v,CL.immune) == false then
                //set CL.v = FirstOfGroup(CL.g)
                call BJDebugMsg("IsUnitInGroup(CL.immune) == false")
                call BJDebugMsg("FirstOfGroup == " + I2S(GetUnitTypeId(CL.v)))
                exitwhen CL.v == null
                
                
                call BJDebugMsg("Calculating distance")
                call BJDebugMsg("CL.u == " + I2S(GetUnitTypeId(CL.u)))
                set CL.dx = GetUnitX(CL.v) - GetUnitX(CL.u)
                set CL.dy = GetUnitY(CL.v) - GetUnitY(CL.u)
                set CL.dist1 = SquareRoot(CL.dx*CL.dx+CL.dy*CL.dy)
                call BJDebugMsg("Dist1 == " + R2S(CL.dist1))
                
                if CL.dist1 < CL.dist2 then
                    call BJDebugMsg(R2S(CL.dist1) + "<" + R2S(CL.dist2))
                    set CL.w = CL.v
                    set CL.dist2 = CL.dist1
                    if GetRandomInt(0,3) == 3 then
                        call BJDebugMsg("Random int == 3, exiting loop")
                        exitwhen true
                    endif
                endif
            //else
            call GroupRemoveUnit(CL.g,CL.v)
               // if FirstOfGroup(CL.g) == null then
                  //  exitwhen true
                //endif
            endif
        endloop
        
        call BJDebugMsg("Loop exited, dealing damage and creating lightning")
        
        call UnitDamageTarget(D.caster,CL.w,CHAIN_BASE_DMG + CHAIN_INC_DMG * D.level +GetRandomInt(-CHAIN_RDM_DMG * D.level,CHAIN_RDM_DMG * D.level) ,false,false,ATTACK_TYPE_MAGIC, DAMAGE_TYPE_UNIVERSAL,null)
        call TL_Unit ("CLPB", CL.u,CL.w, 1, false, 1, 0)
        set CL.u = CL.w
        call GroupClear(CL.g)
        exitwhen CL.i >= D.i
        set CL.i = CL.i+1
        
        //CALL TIMED LIGHTNING SYSTEM TO CREATE SOME LIGHTNING &gt;<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite1" alt=":)" title="Smile    :)" loading="lazy" data-shortname=":)" />
    endloop
    
    call D.destroy()
    call CL.destroy()
endfunction


JASS:
private function Callback takes nothing returns nothing
local Data D = GetTimerData(GetExpiredTimer())
local integer rand = GetRandomInt(0,4)
    
    call BJDebugMsg(&quot;Callback called&quot;)
    set D.x = GetUnitX(D.target)
    set D.y = GetUnitY(D.target)

    if rand == 0 or rand == 3 then
        call BJDebugMsg(&quot;calling ChainLightning&quot;)
        call ChainLightning(D)
    elseif rand == 1 or rand == 4 then
        call BJDebugMsg(&quot;calling RandomBuffs&quot;)
        call RandomBuffs(D)
    elseif rand == 2 then
        call BJDebugMsg(&quot;calling Possession&quot;)
        call Possession(D)
    endif
endfunction

private function Act takes nothing returns nothing
local Data D = Data.create(GetTriggerUnit(),GetSpellTargetUnit(),GetRandomInt(0,6),GetUnitAbilityLevel(GetTriggerUnit(),SPELL_ID))

    call SetTimerData(D.t,D)
    call BJDebugMsg(&quot;TimerData attached&quot;)
    call TimerStart(D.t,D.time,false,function Callback)
    call BJDebugMsg(&quot;Timer started, will finish in&quot; + I2S(D.time))
endfunction


And here are the two structs used:
JASS:
private struct Data
    timer t
    unit target
    unit caster
    real x
    real y
    integer i
    integer time
    integer level
    
    static method create takes unit u, unit v, integer i, integer level returns Data
        local Data D = Data.allocate()
        set D.target = v
        set D.caster = u
        set D.t = NewTimer()
        set D.level = level
        set D.time = i
        return D
    endmethod
    
    private method onDestroy takes nothing returns nothing
        set .caster = null
        call ReleaseTimer(.t)
        //call DestroyTimer(.t)
        set .target = null
    endmethod
endstruct

private struct Chain
    real dx
    real dy
    real dist1
    real dist2
    unit u
    unit v
    unit w
    group g
    group immune
    integer i
    
    static method create takes unit u returns Chain
        local Chain CL = Chain.allocate()
        set CL.i = 1
        set CL.g = CreateGroup()
        set CL.immune = CreateGroup()
        set CL.u = u
        return CL
    endmethod
    
    private method onDestroy takes nothing returns nothing
        set .u = null
        set .v = null
        set .w = null
        call DestroyGroup(.g)
        call DestroyGroup(.immune)
        set .immune = null
        set .g = null
    endmethod
endstruct
 

Frozenhelfir

set Gwypaas = Guhveepaws
Reaction score
56
Probably, but it was happening with a group full of at least 20 things. Either way, I've already fixed it, but thanks for the input. Btw, are those noodles? :p
 
General chit-chat
Help Users

      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