Initial Import
[glAntsMech.git] / glants_mech / win32 / glAntsV05 / pheromone.cpp
blobb9ef5671cac447b5def2ae6d99577ed12ec1ac09
1 //
2 // pheromone.cpp
3 //
4 //
5 #include <windows.h>
6 #include <stdio.h>
7 #include <stdlib.h>
9 #include <gl\gl.h> // Header File For The OpenGL32 Library
10 #include <gl\glu.h> // Header File For The GLu32 Library
11 #include <gl\glaux.h> // Header File For The Glaux Library
13 #include "globals.h"
14 #include "gldrawlib.h"
15 #include "objects.h"
16 #include "bot.h"
17 #include "octree.h"
19 #undef CURRENT_OBJECT
20 #define CURRENT_OBJECT pheromone
22 #undef CURRENT_PTR
23 #define CURRENT_PTR StaticBotPtr
25 #undef CURRENT_OBJ
26 #define CURRENT_OBJ StaticBot
28 #undef CURRENT_BOT
29 #define CURRENT_BOT trail_set
32 //=====================================
33 static void init_pheromone(int list_id);
34 static void compile_pheromone(void);
35 static void draw_pheromone(void);
36 static void render_pheromone(void);
37 static void draw_pheromone(void);
40 static CURRENT_PTR CreatePheromone(int bot_id);
41 static void RenderPheromone(CURRENT_PTR boid);
42 static void DestroyPheromone(CURRENT_PTR b);
43 static void ProcessPheromone(CURRENT_PTR b);
46 static void Draw_Pheromones(void);
47 static void Generate_Pheromones(void);
48 static void Shutdown_Pheromones(void);
52 // simple objects library
53 // - make sure to change the number of objects
54 // in objects.h
56 DriverObjects CURRENT_OBJECT =
58 init_pheromone, // init, must be called first
59 compile_pheromone, // compile
60 draw_pheromone, // draw
61 render_pheromone, // render to scene
62 0 // loaded by INIT
66 // call 1. garden.generate
67 // call 2. garden.drawall
68 // call 3. garden.shutdown
69 DriverSentinel CURRENT_BOT =
72 CreatePheromone, // create
73 DestroyPheromone, // destroy
74 RenderPheromone, // render
75 ProcessPheromone, // process
77 Generate_Pheromones, // generate
78 Shutdown_Pheromones, // shutdown
79 Draw_Pheromones, // drawall
81 NULL, // ptr
82 0 // this value is faulty! MAX_PHEROMONES
86 // ActivatePheromone
88 // Note: this is a cpu hog, it will find an open
89 // slot searching to the max possibly
90 // implement a better method like some sort cache
92 void ActivatePheromone(float x, float y, float dir)
94 int i = 0;
95 for (i = 0; i < MAX_PHEROMONES; i++)
97 if (CURRENT_BOT.objects[i]->state == DEAD_STATE)
98 break;
99 } // end of the for
101 if (i >= MAX_PHEROMONES)
102 return;
104 // found a dead slot
105 CURRENT_BOT.objects[i]->position[0] =x;
106 CURRENT_BOT.objects[i]->position[2] =y;
107 CURRENT_BOT.objects[i]->rotation[1] = dir;
108 CURRENT_BOT.objects[i]->state = ALIVE_STATE;
109 CURRENT_BOT.objects[i]->food = PHEROMONE_LIFE;
111 CURRENT_BOT.objects[i]->delete_flag = false;
114 } // end of the function
117 // Generate Nests
119 static void Generate_Pheromones(void)
121 int index = 0;
124 // This has to be set here because
125 // of a config file bug
126 CURRENT_BOT.max_items = MAX_PHEROMONES;
128 // create the array of pointers
129 CURRENT_BOT.objects = (CURRENT_OBJ **)malloc(
130 sizeof(CURRENT_OBJ *)*CURRENT_BOT.max_items);
132 for (index = 0; index < CURRENT_BOT.max_items; index++)
136 // this bordering on insane
137 // allocate an array of bot pointers, duh for nest
138 CURRENT_BOT.objects[index] = CURRENT_BOT.create(index);
141 } // end of the for
144 } // end of the function
147 // Shutdown Nests
149 static void Shutdown_Pheromones(void)
151 int index = 0;
153 for (index = 0; index < CURRENT_BOT.max_items; index++)
155 CURRENT_BOT.destroy( CURRENT_BOT.objects[index]);
157 } // end of the for
159 // Shrug, free the ptr-to-ptrs
160 //free(CURRENT_BOT.objects);
161 RELEASE_OBJECT(CURRENT_BOT.objects);
164 } // end of the function
168 // Draw Nests
170 static void Draw_Pheromones(void)
172 int index = 0;
174 for (index = 0; index < CURRENT_BOT.max_items; index++)
176 CURRENT_BOT.process( CURRENT_BOT.objects[index]);
178 CURRENT_BOT.render( CURRENT_BOT.objects[index]);
180 } // end of the for
182 } // end of the function
186 // Process Events
188 static void ProcessPheromone(CURRENT_PTR b)
190 b->food--;
191 if (b->food < 0) {
192 b->food = 0;
193 b->state = DEAD_STATE; // pheromone is dead
195 } // end of the if
198 } // end of the function
202 // Create bot
204 static CURRENT_PTR CreatePheromone(int bot_id)
206 CURRENT_PTR bot;
208 bot = (CURRENT_PTR)malloc(sizeof(CURRENT_OBJ));
210 // I like to be extra careful
211 ZeroMemory((CURRENT_PTR)bot,
212 sizeof(CURRENT_OBJ));
214 bot->position[0] = 0;
215 bot->position[1] = 0;
216 bot->position[2] = 0;
218 bot->list_id = bot_id;
221 bot->rotation[0] = 0;
222 bot->rotation[1] = 0;
223 bot->rotation[2] = 0;
225 bot->size[0] = 1.0f;
226 bot->size[1] = 1.0f;
227 bot->size[2] = 1.0f;
229 bot->color[0] = 1.0f;
230 bot->color[1] = 1.0f;
231 bot->color[2] = 1.0f;
233 bot->state = DEAD_STATE;
235 bot->food = PHEROMONE_LIFE;
237 bot->delete_flag = false;
242 return bot;
244 } // end of the function
247 // DestroyBot
249 static void DestroyPheromone(CURRENT_PTR b)
251 free(b);
253 } // end of the functino
256 // RenderBot
258 static void RenderPheromone(CURRENT_PTR boid)
261 if (boid->state == DEAD_STATE)
262 return;
264 #if ENABLE_LIGHTS
265 glDisable(GL_LIGHTING);
266 #endif
268 BEGIN_BOT;
270 // Translate then rotate
271 glTranslatef(boid->position[0],boid->position[1],
272 boid->position[2]);
274 // rotate based on the ship struct
275 glRotatef(boid->rotation[1], 0.0f, 1.0f, 0.0f);
276 glRotatef(boid->rotation[0], 1.0f, 0.0f, 0.0f);
277 glRotatef(boid->rotation[2], 0.0f, 0.0f, 1.0f);
279 // Scale accordingly
281 glScalef(boid->size[0], boid->size[1], boid->size[2]);
284 // This may or may not change the color
285 glColor3f(boid->color[0], boid->color[1], boid->color[2]);
288 driver_objects[SQUARE_OBJECT]->render();
290 END_BOT;
292 #if ENABLE_LIGHTS
293 glEnable(GL_LIGHTING);
294 #endif
296 } // end of the function
299 //---------------------------------------------------------
301 //=========================================================
302 //=========================================================
303 static void draw_pheromone(void)
305 float h;
306 float width = 0.3f;
307 h = width / 2.0f;
309 glBegin(GL_LINE_LOOP);
311 // Front Face
312 glVertex3f(-h, 0.0f, h); // left bottom
313 glVertex3f( h, 0.0f, h); // right bottom
314 glVertex3f( h, 0.0f, -h); // top right
315 glVertex3f(-h, 0.0f,-h); // top left
317 glEnd();
319 } // end of the function
323 // init
324 // - load anything special about the
325 // one important function
327 static void init_pheromone(int list_id)
330 CURRENT_OBJECT.visible = 1;
332 // store the id through the function
333 // there is probably a better way to do this
334 CURRENT_OBJECT.call_id = list_id;
336 } // end of the functino
339 //=========================================================
340 // Now the function to actually draw it
341 //=========================================================
342 static void render_pheromone(void)
344 //glPushMatrix();
346 glCallList(CURRENT_OBJECT.call_id);
348 //glPopMatrix();
350 } // end of the function
352 //=========================================================
353 // compile
354 //=========================================================
355 static void compile_pheromone(void)
357 int id;
358 // setup a spot for display list for background
359 //object = getcurrentobject();
360 id = CURRENT_OBJECT.call_id;
362 // apply list
363 glNewList(id, GL_COMPILE);
365 // call drawing function
366 // but this may method make it a little better
367 CURRENT_OBJECT.draw();
369 glEndList();
371 } // end of the function