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
  • No one is chatting at the moment.
  • 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
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/
  • The Helper The Helper:
    I think we need to add something to the bottom of the front page that shows the Headline News forum that has a link to go to the News Forum Index so people can see there is more news. Do you guys see what I am saying, lets say you read all the articles on the front page and you get to the end and it just ends, no kind of link for MOAR!
  • The Helper The Helper:
    Happy Wednesday!
    +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