looping through struct crashes thread, need help

MasterOfRa

New Member
Reaction score
10
I'm trying to write a new mana system for a game I'm working on, where the environment has a amount of mana, and spells use up nearby mana to cast, i had it working with a massive amout of dummy units (3.2k) but decided that that was not the best option. So instead, i am using a struct. The problem is that looping through 3.2k times crashes the thread, on 710th loop in GroupManaInRange function. What can i do to fix this?

JASS:
library Mana uses PUI, Globals
globals
endglobals

struct ManaData
    static integer instances = 0
    real qty
    real x
    real y
    boolean isGrouped
    boolean isDepleted
    static method create takes real qty, real x, real y returns ManaData
        local ManaData data = ManaData.allocate()
        set data.qty = qty
        set data.x   = x
        set data.y   = y
        set data.isGrouped = false
        set data.isDepleted = false
        set .instances = .instances + 1
        return data
    endmethod
endstruct

function GroupManaInRange takes real x, real y, real range returns nothing
    local integer count = 0
    local ManaData data
    local real dx
    local real dy
    call BJDebugMsg("Grouping Mana")
    set range = range * range // Do this to make calculating the distance easier
    loop
        exitwhen count >= ManaData.instances
        set data = ManaData(count)
        set dx = data.x - x
        set dy = data.y - y
        if not data.isDepleted then
            if (dx*dx)+(dy*dy) <= range then
                set data.isGrouped = true
            else
                set data.isGrouped = false
            endif
        else
            set data.isGrouped = false
        endif
        set count = count + 1
        call BJDebugMsg("Mana Group Loop "+R2S(count))
        call DoNothing()
    endloop
    call BJDebugMsg("Mana Grouping Finished")
endfunction

function CountGroupedMana takes nothing returns integer
    local integer returnState = 0
    local integer count
    local ManaData data
    loop
        set data = ManaData(count)
        if data.isGrouped == true then
            set returnState = returnState + 1
        endif
        set count = count + 1
        exitwhen ManaData(count) == null
    endloop
    return returnState
endfunction

function GetAvailableMana takes real x, real y, real range returns real
    local real returnState = 0
    local integer count
    local ManaData data
    call BJDebugMsg("GetAvailableMana Started")
    call GroupManaInRange(x,y,range)
    call BJDebugMsg("Mana Grouped")
    loop
        exitwhen count >= ManaData.instances
        set data = ManaData(count)
        if data.isGrouped then
            set returnState = returnState + data.qty
        endif
        set count = count + 1
    endloop
    call BJDebugMsg("Avalible Mana Counted :"+R2S(returnState))
    return returnState
endfunction

function UseMana takes real x, real y, real range, real amount returns nothing
    local integer count
    local ManaData data
    local real amountPerNode
    local integer nodes
    call GroupManaInRange(x,y,range)
    set nodes = CountGroupedMana()
    loop
        set count = 0
        set amountPerNode = amount / nodes
        loop
            exitwhen count >= ManaData.instances
            set data = ManaData(count)
            if data.isGrouped then
                if data.qty > amountPerNode then
                    set data.qty = data.qty - amountPerNode
                    set amount = amount - amountPerNode
                else
                    set amount = amount - data.qty
                    set data.qty = 0
                    set data.isDepleted = true
                    set nodes = nodes - 1
                    set data.isGrouped = false
                    call DestroyEffect(AddSpecialEffect(FID_dispelmana,data.x+GetRandomReal(-64,64),data.y+GetRandomReal(-64,64)))
                endif
            endif
            set count = count + 1
        endloop
        exitwhen amount <= .1
    endloop
endfunction

endlibrary
 

MasterOfRa

New Member
Reaction score
10
Very funny...

I was using dummy units, and it lagged the map like hell...

i was trying to use structs to make it more efficient

is there a way to allow over 3.2k units on the field without increasing lagg dramatically?

yes, i know, no models, but that didnt help either
 

Frozenhelfir

set Gwypaas = Guhveepaws
Reaction score
56
Thappy was referring to this line:

JASS:
local ManaData data


You forgot to create it:

JASS:
local ManaData data = MineData.create()


Does this line even do anything? It doesn't even pass enough parameters to be a constructor because your create method takes three values:

JASS:
set data = ManaData(count)
 

MasterOfRa

New Member
Reaction score
10
that function does not create anything, it loops through the existing mana nodes and groups them. I use a separate trigger to create the nodes.

set data = ManaData(count) is supposed to set data to point at that particular struct element

How would i use .execute?

i could use call GroupManaInRange.execute(x,y,range) but it wont help as it still crashes, because the massive function calls in the trigger, if i could call a new function for each loop, that may work. but may be a bad idea because that probaly would lag anyway
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top