- Joined
- Sep 8, 2007
- Messages
- 994
Short Version:
Is there an alternative to a code array, or rather is there a way to let a struct's attribute be of type code?
Long Version:
Ok here's the situation:
I have 6 different itemtypes and a hero that can pick up up to 3 of those, including multiple items of the same type.
That hero has an ability. Everytime he casts it, I want to run a different trigger depending on the items he's got. Taking NO_ITEM as an option, that makes 7 * 7 * 7 = 343 possibilities of carrying a set of items, which can easily be saved in a database like this, using a triple nested loop:
I tried to use a code-attribute in the corresponding FormulaData object to trigger further actions, but apparently I can't, because code arrays aren't allowed (struct attributes will internally be compiled to arrays).
Any idea?
Is there an alternative to a code array, or rather is there a way to let a struct's attribute be of type code?
JASS:
private struct Example
code c
public static method create takes code c returns thistype
local thistype data = thistype.allocate()
set data.c = c
return data
endmethod
endstruct
Long Version:
Ok here's the situation:
I have 6 different itemtypes and a hero that can pick up up to 3 of those, including multiple items of the same type.
That hero has an ability. Everytime he casts it, I want to run a different trigger depending on the items he's got. Taking NO_ITEM as an option, that makes 7 * 7 * 7 = 343 possibilities of carrying a set of items, which can easily be saved in a database like this, using a triple nested loop:
JASS:
private function Settings takes nothing returns nothing
local integer i = 0
local integer j = 0
local integer k = 0
local integer array i1
set i1[0] = 0
set i1[1] = FIRE_ID
set i1[2] = WATER_ID
set i1[3] = NATURE_ID
set i1[4] = LIGHT_ID
set i1[5] = DARK_ID
set i1[6] = HUMAN_ID
//adding ALL possible partitions, filtering internally for doubles
loop
exitwhen i > 6
loop
exitwhen j > 6
loop
exitwhen k > 6
call FormulaData.create(i1[i],i1[j],i1[k])
set k = k + 1
endloop
set j = j + 1
set k = 0
endloop
set i = i + 1
set j = 0
endloop
call FormulaData.stopAdd()
endfunction
private struct FormulaData
readonly integer array id[3]
readonly static integer cnt = 0
private static boolean add = true
private static thistype array dt[MAX_ARRAY_SIZE]
public static method stopAdd takes nothing returns nothing
set add = false
endmethod
public static method create takes integer id1, integer id2, integer id3 returns thistype
local thistype data = thistype.allocate()
set data.id[0] = id1
set data.id[1] = id2
set data.id[2] = id3
call data.sort()
//if not already saved into database, save it
if get(data.id[0], data.id[1], data.id[2]) == 0 and add then
set dt[cnt] = data
set cnt = cnt + 1
endif
return data
endmethod
private method sort takes nothing returns nothing
local integer temp
if (this.id[0] < this.id[1]) then
set temp = this.id[0]
set this.id[0] = this.id[1]
set this.id[1] = temp
endif
if (this.id[1] < this.id[2]) then
set temp = this.id[1]
set this.id[1] = this.id[2]
set this.id[2] = temp
endif
if (this.id[0] < this.id[1]) then
set temp = this.id[0]
set this.id[0]= this.id[1]
set this.id[1] = this.id[0]
endif
endmethod
public static method get takes integer id1, integer id2, integer id3 returns thistype
local integer i = 0
loop
exitwhen i > cnt
if dt[i].id[0] == id1 and dt[i].id[1] == id2 and dt[i].id[2] == id3 then
return dt[i]
endif
set i = i+1
endloop
return 0
endmethod
I tried to use a code-attribute in the corresponding FormulaData object to trigger further actions, but apparently I can't, because code arrays aren't allowed (struct attributes will internally be compiled to arrays).
Any idea?