HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Multi-dimension array alternative

10-04-2007, 09:10 PM#1
divo
It would be really neat if I could create a multi-dimension array like this:
somearray[integer player_id][group units]
But since jass only allows single dimension arrays I need an alternative.

I was thinking that I could just define some key ranges within the groups could be assigned. Something like:
Unit groups for player 1 somearray[0-100]
Unit groups for player 2 somearray[101-200]
Unit groups for player 3 somearray[201-300]
...etc...

My biggest concern is how effective this solution is. I know that it would cap the number of unit groups for each player_id to 100 but that is should do it.


I also came up with another solution: Creating an array for every player (12 in total). I know this is possible but not very flexible when I want to retrieve or set the values. I would have to create a function which would go something like...

Code:
function set_unitgroup takes player_id, group unitgroup returns nothing
  if (player_id == 1) then
    set array_for_player_1[*note*] = unitgroup
  endif

  if (player_id == 2) then
  ...etc...
endfunction

*note* I would have to create an associative variable that held the current index number and then auto-increment it.


Now, that is the 2 best solutions I could come up with... I like the first one more even though it is capped. I could just increase the ranges to e.g. 600 per player, given that there is 12 players and the max array size is 8192 (600*12=7200).


Which method would be the most effective (computing-time-wise) and what would you recommend? Any other solutions are also much appreciated.
10-04-2007, 09:35 PM#2
Captain Griffen
Most effective computing wise is obviously in one array as you just times the player ID by 600 and add it to the index. If you have over 600 unit groups, you have issues.
10-04-2007, 10:13 PM#3
divo
Thanks for the reply. I'm creating a tetris mod, so I need a fair amount of unit groups to organize the individual blocks. Depending on the size of each players area to lay the bricks, I would gestimate that each player needed around 50 unit groups, maybe more towards endgame. That is 50*12 = 600 unit groups in total, so I would only need to allocate max. 100 per player in that one array.

How will the array size affect the response time when I try to set or retrieve data?
10-05-2007, 12:07 AM#4
Pyrogasm
I don't think it will affect it at all, or at least not noticeably unless you're doing some freaky math calculations to get the index... or if you're looping through every group to find the one you want, 'cause that would be bad.
10-05-2007, 12:52 AM#5
PipeDream
Got a set with a variable number of elements? Use a list!
10-05-2007, 10:08 AM#6
divo
Thanks for the replies.

Quote:
Originally Posted by PipeDream
Got a set with a variable number of elements? Use a list!
You'll have to link me or at least explain what that is supposed to mean :).


Another question regarding optimization.. Everytime a row is cleared, I would have to loop through every block-unitgroup for that player to see if they could "fill-float" the newly created hole. There is 50 groups with 4 units in each (e.g. 200 units) to loop through and check if they can be moved down. After I've looped through every block I would have to do another loop instance of all the units that didn't move to check if the movement by the first instance had created new holes for the rest of the blocks to fill. I would then have to keep looping until the instance where no blocks moved, aka. there is no more holes to fill.

Let's say that in the worst-case scenario I would have to run that 200-ilteration loop 6 times, making it a 1200-ilteration loop. I could imagine that this would create some minor lag-spikes, but lag-spikes ruins the gameplay no matter their size.


This is the only solution I've found to fill the gaps. I would be grateful if anyone could find a simpler algorithm to do the same more efficiently.
10-05-2007, 05:54 PM#7
PipeDream
Collapse JASS:
struct grouplist
    group g
    grouplist next
endstruct

globals
    grouplist array playergroups
endglobals

function AddUnitGroupToPlayer integer p, group g returns nothing
     local grouplist ng = grouplist.create()
     set ng.g = g
     set ng.next = playergroups[p]
     set playergroups[p] = ng
endfunction

function DoStuffForPlayerP takes integer p returns nothing
    local grouplist c = playergroups[p]
    loop
        exitwhen c == 0
        
        // .. do stuff to c.g here

        set c = c.next
    endloop
endfunction