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