3 // This may border on octree but they are similar
5 // the goal is to use this code with the pheremones
6 // when they are dropped by the ants
7 // if an ant drops 100 pheremones on the way to the nest
8 // 100 * 500, it would be silly to have to test all of these
9 // so some algorithm is needed to save cpu time
11 // - the octree is an array of pointers to seperate lists
12 // - the lists contains the position of the pheremone
13 // - whenever a pheremone is created, it is added to the list
14 // - whenever a pheremone is deleted, is is removed from the list
15 // - whenever the bot moves, he searches the list
16 // - better than brute force, of course
17 // - since we are using a hash function, possibly O(1)
23 #include <GL/gl.h> // Header File For The OpenGL32 Library
24 #include <GL/glu.h> // Header File For The GLu32 Library
30 #include "gldrawlib.h"
34 Octree
**pheromone_tree
;
35 Octree
**GenerateOctree(void);
40 Octree
*CreateOctree(void)
43 ptr
= (Octree
*)malloc(sizeof(Octree
));
47 } // end of the function
52 void DeleteOctree(Octree
**tree_ptr
)
54 // delete the lists on the tree
57 max
= tree_ptr
[0]->max_elements
;
59 for (i
= 0; i
< max
; i
++)
61 DestroyPtrList(tree_ptr
[i
]->list
);
65 // have to delete every node individually
66 for (i
= 0; i
< max
; i
++)
73 } // end of the function
77 // - struct has xmin,xmax,ymin, ymax
78 // and a pointer to a list
80 Octree
**GenerateOctree(void)
85 float height
= width
; // square grid
86 float x_min
,x_max
, y_min
, y_max
;
93 for (i
= -30.0f
; i
< 30.0f
; i
+= height
)
94 for (j
= -30.0f
; j
< 30.0f
; j
+= width
)
99 // First we have to create an array of pointers
100 tree_ptr
= (Octree
**)malloc(count
* sizeof(Octree
*));
103 for (index
= 0; index
< count
; index
++)
104 tree_ptr
[index
] = CreateOctree();
107 z
= 0; // reset counter
109 // populate the tree with data
110 for (i
= -30.0f
; i
< 30.0f
; i
+= height
)
112 for (j
= -30.0f
; j
< 30.0f
; j
+= width
)
119 tree_ptr
[z
]->list
= CreatePtrList();
120 tree_ptr
[z
]->x_max
= x_max
;
121 tree_ptr
[z
]->x_min
= x_min
;
122 tree_ptr
[z
]->y_max
= y_max
;
123 tree_ptr
[z
]->y_min
= y_min
;
124 tree_ptr
[z
]->max_elements
= count
;
133 } // end of the function
138 // - insert a pheremone into the tree
140 void InsertOctree(Octree
**tree_ptr
, StaticBotPtr bot
)
143 float x_min
,y_min
, x_max
, y_max
;
144 int max
= tree_ptr
[0]->max_elements
;
146 for (i
= 0; i
< max
; i
++)
148 x_min
= tree_ptr
[i
]->x_min
;
149 x_max
= tree_ptr
[i
]->x_max
;
150 y_min
= tree_ptr
[i
]->y_min
;
151 y_max
= tree_ptr
[i
]->y_max
;
153 if ((bot
->position
[0] > x_min
) &&
154 (bot
->position
[0] < x_max
) &&
155 (bot
->position
[2] > y_min
) &&
156 (bot
->position
[2] < y_max
))
158 // in the area add to list
159 InsertFront(tree_ptr
[i
]->list
, (StaticBotPtr
)bot
);
165 } // end of the function
170 // - search the list for a static bot
172 StaticBotPtr
SearchListBot(PtrList
*list
, DriverBotPtr bot
)
174 PtrNode
*current_ptr
;
176 float x_min
, x_max
, y_min
, y_max
;
180 if (list
->head
== NULL
)
183 current_ptr
= list
->head
;
185 while(current_ptr
!= NULL
)
188 x
= (StaticBotPtr
)current_ptr
->ptr
;
190 x_min
= x
->position
[0] - (x
->size
[0]/2.0f
);
191 x_max
= x
->position
[0] + (x
->size
[0]/2.0f
);
192 y_min
= x
->position
[2] - (x
->size
[0]/2.0f
);
193 y_max
= x
->position
[2] + (x
->size
[0]/2.0f
);
195 if ((bot
->x
> x_min
) && (bot
->x
< x_max
) &&
196 (bot
->y
> y_min
) && (bot
->y
< y_max
))
201 current_ptr
= current_ptr
->next
;
207 } // end of the function
211 // - check if we are in the region
213 StaticBotPtr
SearchOctree(Octree
**tree_ptr
, DriverBotPtr bot
)
215 // find out which bin to search
217 float x_min
,y_min
, x_max
, y_max
;
218 int max
= tree_ptr
[0]->max_elements
;
220 StaticBotPtr res
= NULL
;
222 for (i
= 0; i
< max
; i
++)
224 x_min
= tree_ptr
[i
]->x_min
;
225 x_max
= tree_ptr
[i
]->x_max
;
226 y_min
= tree_ptr
[i
]->y_min
;
227 y_max
= tree_ptr
[i
]->y_max
;
229 if ((bot
->x
> x_min
) &&
234 // in the area add to list
235 res
= SearchListBot(tree_ptr
[i
]->list
, bot
);
237 // Search the list in this region
246 } // end of the function
250 // - delete a node from the tree
252 void DeleteOctreeNode(Octree
**tree_ptr
, StaticBotPtr bot
)
254 // find out which bin to search
256 float x_min
,y_min
, x_max
, y_max
;
257 int max
= tree_ptr
[0]->max_elements
;
259 for (i
= 0; i
< max
; i
++)
261 x_min
= tree_ptr
[i
]->x_min
;
262 x_max
= tree_ptr
[i
]->x_max
;
263 y_min
= tree_ptr
[i
]->y_min
;
264 y_max
= tree_ptr
[i
]->y_max
;
266 if ((bot
->position
[0] > x_min
) &&
267 (bot
->position
[0] < x_max
) &&
268 (bot
->position
[2] > y_min
) &&
269 (bot
->position
[2] < y_max
))
273 DeletePtrNode(tree_ptr
[i
]->list
, bot
);
280 } // end of the function
285 // - you should probably only have to deal
286 // with these functions unless you want more
288 void pheromoneBuild(void)
290 pheromone_tree
= GenerateOctree();
291 } // end of the function
295 // - make sure this called
297 void pheromoneDestroy(void)
299 DeleteOctree(pheromone_tree
);
300 } // end of the function
303 // pheromoneInsert(StaticBotPtr bot)
305 void pheromoneInsert(StaticBotPtr bot
)
307 InsertOctree(pheromone_tree
, bot
);
308 }// end of the function
313 StaticBotPtr
pheromoneSearch(DriverBotPtr bot
)
316 ptr
= SearchOctree(pheromone_tree
, bot
);
319 } // end of the function
323 // - delete a pheromone
325 void pheromoneDelete(StaticBotPtr bot
)
328 DeleteOctreeNode(pheromone_tree
, bot
);
330 } // end of the function