C++ map is a one dimensional data structure, just key->value pairs, so that's a stupid answer.
Each unit in an RTS with AI needs to be able to get a list of all other units in a certain radius of itself so that it can automatically detect enemies in its line of sight and move towards them to attack them, right?
So if I just use a map on X,Y then suppose a unit has a radius R of sight, to do its sight-check it would have to check on the order of O(R^2) squares around it. Multiply that by N, and we would have a shitty implementation.
So I'll repeat the original question. Does anyone know how this is ACTUALLY done in RTS games? Because the answers given so far are B.S.
Use a 2d array (array of arrays). How hard was that?
Name:
Anonymous2007-07-31 0:52 ID:EYgpmlx/
Try this line of thinking:
Use a linked list of Unit objects for each "team".
Each unit knows it's own X & Y position on the grid.
"Line of sight" could mean "Falls inside a box
around the unit doing the sight check".
The boundry box for some unit is (say): top left->(X-20,Y-20) bottom right->(X+20, Y+20). Baddie is in "line of sight"
if (baddie.X > X-21) AND (baddie.X < X+21) AND (baddie.Y > Y-21) AND (baddie.Y < Y-21) ...
Once you know a baddie is in "in the box" other calculations
become easy (ie. Is there an obstruction between the two
units, what path does a missile need to take etc...).
You don't need to to the sight check on units in the same
linked list as the one doing the sight check.
If you are looking to attack then you don't need to check
against units the checking unit is un-able to attack.
Hope this is helpful...
Name:
Anonymous2007-07-31 3:16 ID:mvKBN4dn
a quad tree is a structure you can use to check nearby units in much less time than O(R^2).
Name:
Anonymous2007-07-31 3:27 ID:mvKBN4dn
>>10 or just a grid, with squares that have sides of similar length to the maximum line of sight. Then when we have a specific unit we only need to check the squares around it.
Like what >>8 said, an array of arrays but each element would be a list of units contained in that square/cell.
Name:
Anonymous2007-07-31 3:28 ID:mvKBN4dn
A simple array of arrays of booleans should work fine for fog of war.
WIDTH = infinite implies an infinite length array, which to me sounds really dumb. Anon, you are a strange person to be. You are a strange friend to have.
>>24
Not really, it's a very valid concern, you want to always make sure that your arrays can have infinite, zero and negative WIDTH.
Name:
Anonymous2007-07-31 11:24 ID:1ECclkE4
You misunderstand. By using x+y*WIDTH you are essentially converting
|---|---|---|
|1,1|2,1|3,1|
|---|---|---|
|1,2|2,2|3,2|
|---|---|---|
|1,3|2,3|3,3|
|---|---|---|
into
|---|---|---|
| 1 | 2 | 3 |
|---|---|---|
| 4 | 5 | 6 |
|---|---|---|
| 7 | 8 | 9 |
|---|---|---|
which relies on one of the dimensions - the one whose range is represented by WIDTH - being finite. If both dimensions are infinite, this will fail to work.