HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Unit Arrays (Spawning Method)

10-29-2014, 08:09 PM#1
Kino
I cant seem to figure out how to index my units via the following critertia.
Unit Type
Level


Eg.
Level 1 has 5 possible unit types
Level 2 has 3 possible unit types

A trigger randomly spawns a unit based on the level.
Im thinking I need a unit-type array(most obviously), beyond that, Im clueless as to how to approach this.

I don't need actual examples, just a list of things I need to do.


Im sorry I couldn't be clearer, my brain is literally not functioning atm.
10-30-2014, 09:23 AM#2
Anitarf
I'd do it with an array of linked lists, but a simple 2D array can do the job as well. Since you don't have the same number of unit types per each level, you'll need an additional array to store that number for each level (or the same array, since unit types are also integers).

We don't actually have 2D arrays in JASS, so you'll need to use the standard method of simulating them with a 1D array and the [X+Y*maxX] index formula.
10-30-2014, 10:56 AM#3
Fledermaus
vJass has 2D arrays though!
As ani said, use integers. I'd also use the 0th index of each level index as the count for how many unit types there are in that level.
Collapse JASS:
globals
    private constant integer MAX_LEVELS     = 20
    private constant integer MAX_UNIT_TYPES = 20 //Should be 1 more than the actual max, so you can use the 0th index for the count
    
    integer array UnitIds[MAX_LEVELS][MAX_UNIT_TYPES]
endglobals

function InitUnitTypes takes nothing returns nothing
    set UnitIds[1][0] = 5 //Level 1 has 5 unit types
    set UnitIds[1][1] = 'u000'
    set UnitIds[1][2] = 'u001'
    set UnitIds[1][3] = 'u002'
    set UnitIds[1][4] = 'u003'
    set UnitIds[1][5] = 'u004'
    
    set UnitIds[2][0] = 6 //Level 2 has 6 unit types
    set UnitIds[2][1] = 'u010'
    set UnitIds[2][2] = 'u011'
    set UnitIds[2][3] = 'u012'
    set UnitIds[2][4] = 'u013'
    set UnitIds[2][5] = 'u014'
    set UnitIds[2][6] = 'u015'
    
    set UnitIds[3][0] = 6 //Level 3 has 3 unit types
    set UnitIds[3][1] = 'u110'
    set UnitIds[3][2] = 'u111'
    set UnitIds[3][3] = 'u112'
    
    //You get the idea...
endfunction

function UseUnitTypes takes integer level returns nothing
    //Iterate over all the unit types for a level
    local integer i = 1 //We start at 1 since 0 is the UnitId count for each level
    
    loop
      exitwhen i > UnitIds[level][0]
        //Do something with UnitIds[level][i]
        set i = i + 1
    endloop
    
    //Or get a random UnitId from the level
    set i = GetRandomInt(1, UnitIds[0])
endfunction
10-30-2014, 12:40 PM#4
Anitarf
This approach has a weakness and your example demonstrates it quite well (although I'm sure you didn't intend it to :) ). It is possible to make data entry errors like this:
set UnitIds[3][0] = 6 //Level 3 has 3 unit types

To avoid this, I'd add a wrapper function:
Collapse JASS:
SpawnSystemAddUnitType takes integer level, integer ut returns boolean
    local integer i = UnitIds[level][0] + 1
    if i>=MAX_UNIT_TYPES then
        debug call BJDebugMsg("Spawn System error: MAX_UNIT_TYPES is set too low for level "+I2S(level)+".")
        return false
    endif
    set UnitIds[level][0] = i
    set UnitIds[level][i] = ut
    return true
endfunction

Also, I take back what I said about using linked lists, since Kino needs random access an array is more suitable. If you want some units to have a higher chance of spawning than others, you might want to use pools instead, or you could use the simpler but more limited solution of adding the same unit type to the list multiple times.
10-30-2014, 07:58 PM#5
Fledermaus
:blush:
10-31-2014, 12:28 PM#6
Kino
Ok, I think I didn't explain my self clearly enough.

Quote:
We don't actually have 2D arrays in JASS, so you'll need to use the standard method of simulating them with a 1D array and the [X+Y*maxX] index formula.

I am aware of this, however I don't understand the "math logic" behind the said formula.
Eg. what should X and Y represent when used in the context of what Im trying to achieve.
11-01-2014, 03:50 PM#7
Anitarf
X and Y are the two 2D array indexes. In your case, Y would be the level and X would be the unit-type-id's place in list for that level.
11-03-2014, 01:47 PM#8
Kino
Thanks.