Basic AI Functions
One way of dicing things down is to subdivide the map into chunks. Let''s say your map is 100 x 100 tiles and the max radar range is 10. If you were to dice the map into 10x10 grids, you know for a fact that a unit could only possibly detect units in it''s own grid or the 8 grids surrounding it. Therefore, you create a way of calculating and storing what grid each object is in, and for each item, you just pull an active list of the objects that are in the surrounding grids. You only need to create this list once per sector, so it further optimizes the search if you process the items on a per sector basis... that is, all the items in (1,1), then (1,2), etc.
Dave Mark - President and Lead Designer
Intrinsic Algorithm - "Reducing the world to mathematical equations!"
Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play
"Reducing the world to mathematical equations!"
oblist myobs;
oblist tempobs;
// fill in yer oblist
tempobs = myobs;
oblist::iterator T = tempobs.begin();
bool cont = true;
for(oblist::iterator I = myobs.begin(); I != myobs.end(); ++I)
{
while(cont)
{
if((I != T) && ((*I)->coords - (*T)->coords).Magnitude() <= (*T)->QueryAggroRange()) )
{
(*T)->SetAggro(true);
tempobs.pop_front();
T = tempobs.begin();
cont = false;
}
else
{
++T;
}
}
}
What this does is sets up 2 lists to compare... myobs is the list of your objects and tempobs is a list we make that we can modify... the loop will start with the first ob in myobs(I) and then go through comparing that with each ob in tempobs(T). When we find a guy close enough to make T aggro, we set his aggro flag on then remove him from the list. We want him to stay in myobs cuz he can still turn others aggro if he is close enough to him. I don''t know if this is fastest, but will stop from doing redundant checks against people already turned aggro...
Fox''s suggestion will save time, but you can come into weird edge cases when breaking things into sectors like that... 2 guys can be right on the edge of a chunk, one on one side, the other on the other side... their actual distance between could be 0.001 but since they are in different chunks, they won''t be compared... just keep that in mind if you decide to do the division method, cuz you might have to compare a chunk with all chunks around it also...
I agree with the method where the terrain is splitted into multipule sectors. It is how I have my map setup so that I can do very fast frustrum culling... In your case just make each sector as big as the biggest argo distance... then check all objects in each sector to the other objects in the same sector as long with the oned in the adjecent ones.
This should dramatically reduce the number of comparisons bieng made.
Dwiel
To answer the question, yes, the objects have varying radar ranges.
Other than the sector/grid idea, what other methods are there?
-Enoch
Dave Mark - President and Lead Designer
Intrinsic Algorithm - "Reducing the world to mathematical equations!"
Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play
"Reducing the world to mathematical equations!"
There are numerous algorithms to chose from: cell-space partitioning (the grid idea mentioned by Dave), quad-trees, circle trees, sorted arrays etc.
My Website: ai-junkie.com | My Book: AI Techniques for Game Programming