Initial Import
[glAntsMech.git] / glants_mech / win32 / glAntsV05 / los.cpp
blob660995bcad6fa66848c9f05437d0d1891d1ab546
1 //
2 // los.cpp
3 //
4 // - line of sight algorithm for the fire ant AI
5 //
6 // it is simple, returns a value of 1 or 2
7 //
8 #include <windows.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <math.h>
13 #include <gl\gl.h> // Header File For The OpenGL32 Library
14 #include <gl\glu.h> // Header File For The GLu32 Library
15 #include <gl\glaux.h> // Header File For The Glaux Library
17 #include "globals.h"
18 #include "bot.h"
19 #include "gldrawlib.h"
20 #include "fireants.h"
22 #define UP 1
23 #define DOWN 2
24 #define DIRECTION(dir, val) dir = val
26 // Our line of sight is simple, it is composed of a triangle
27 // if the point is within that triangle, return a one
28 // otherwise return a zero
31 // Draw Line of Sight
32 // where y is actually the z
34 void DrawLineOfSight(float x1, float y1, float x2, float y2)
36 float height = 0.1f;
38 glDisable(GL_LIGHTING);
39 glDisable(GL_TEXTURE_2D);
41 glColor3f(1.0f, 1.0f, 1.0f);
42 glBegin(GL_LINE_LOOP);
44 // Front Face
45 glVertex3f(x1, height, y1); // left bottom
46 glVertex3f(x2, height, y2); // right bottom
48 // Back Face
51 glEnd();
53 glEnable(GL_LIGHTING);
55 } // end of the function
60 // The algo just checks the y=mx+b of the line
61 // and depending on the location of the vertex, etc
62 // it is in the triangle or not
63 // - no clipping
65 bool CheckSight(DriverBotPtr bot, DriverBotPtr nme)
67 float rad;
68 float tmp_heading;
69 float tmp_x2;
70 float tmp_y2;
71 float tmp_x3;
72 float tmp_y3;
73 float a1, b1, a2, b2, a3, b3, a4, b4;
74 float m1, m2, m3;
75 float bb1, bb2, bb3;
76 float center_x, center_y;
77 int direction;
78 bool AB_vert;
79 bool BC_vert;
80 bool CA_vert;
81 int inside = 0;
83 AB_vert = BC_vert = CA_vert = false;
85 // now the next point of the triangle
86 // shift by 45 degrees
87 tmp_heading = bot->heading + 125.0f;
88 if (tmp_heading > 360.0f)
89 tmp_heading -= 360.0f;
91 rad = tmp_heading / RAD_TO_DEG;
93 tmp_x2 = LINE_OF_SIGHT * (float)cos(rad);
94 tmp_y2 = LINE_OF_SIGHT * (float)sin(rad);
96 tmp_x2 = tmp_x2 + bot->x;
97 tmp_y2 = (-tmp_y2) + bot->y;
99 // Now the final point
100 tmp_heading = bot->heading + 55.0f;
101 if (tmp_heading > 360.0f)
102 tmp_heading -= 360.0f;
104 rad = tmp_heading / RAD_TO_DEG;
106 tmp_x3 = LINE_OF_SIGHT * (float)cos(rad);
107 tmp_y3 = LINE_OF_SIGHT * (float)sin(rad);
109 tmp_x3 = tmp_x3 + bot->x;
110 tmp_y3 = (-tmp_y3) + bot->y;
113 a1 = tmp_x2;
114 b1 = tmp_y2;
116 a2 = tmp_x3;
117 b2 = tmp_y3;
119 a3 = bot->x;
120 b3 = bot->y;
122 a4 = nme->x;
123 b4 = nme->y;
126 // find the slope of the different lines
127 // have to check for divide by zero also
128 if ((a2 - a1) != 0)
130 m1 = (b2 - b1) / (a2 - a1); // a->b
131 bb1 = (b1)-(m1 * a1);
132 } else if ((a2 - a1) == 0) {
133 AB_vert = true;
134 } // end of the if-else
136 // y = mx + b
137 if ((a3 - a2) != 0)
139 m2 = (b3 - b2) / (a3 - a2);
140 bb2 = (b2)-(m2 * a2);
141 } else if ((a3 - a2) == 0) {
142 BC_vert = true;
143 } // end of if-else
145 // y = mx + b
146 if ((a1 - a3) != 0)
148 m3 = (b1 - b3) / (a1 - a3);
149 bb3 = (b3) - (m3 * a3);
150 } else if ((a3 - a2) == 0) {
151 CA_vert = true;
152 } // end of the if-else
154 center_x = (a1 + a2 + a3) / 3.0f;
155 center_y = (b1 + b2 + b3) / 3.0f;
157 // a->b
158 if (((m1 * center_x) + bb1) >= center_y)
159 DIRECTION(direction, UP);
160 else
161 DIRECTION(direction, DOWN);
163 if (AB_vert == true) {
164 if ((a1 < a4) && (a1 < center_x))
165 inside++;
166 else if ((a1 > a4) && (a1 > center_x))
167 inside++;
169 } else {
170 if (direction == UP) {
171 if (b4 <= ((m1 * a4) + bb1))
172 inside++;
173 } else if (direction == DOWN) {
174 if (b4 >= ((m1 * a4) + bb1))
175 inside++;
176 } // end of if-else
178 } // end of the if - else
180 // b->c
181 if (((m2 * center_x)+bb2) >= center_y)
182 DIRECTION(direction,UP);
183 else
184 DIRECTION(direction, DOWN);
186 if (BC_vert == true) {
187 if (( a2 < a4) && (a2 < center_x))
188 inside++;
189 else if ((a2 > a4) && (a2 > center_x))
190 inside++;
192 } else {
193 if (direction == UP) {
194 if (b4 <= ((m2 * a4) + bb2))
195 inside++;
197 } else if (direction == DOWN) {
199 if (b4 >= ((m2 * a4) + bb2))
200 inside++;
202 } // end of the if-else
204 } // end of the if-else
206 // c->a
207 if (((m3 * center_x) + bb3) >= center_y)
208 DIRECTION(direction, UP);
209 else
210 DIRECTION(direction, DOWN);
212 if (CA_vert == true) {
213 if ((a3 < a4) && (a3 < center_x))
214 inside++;
215 else if ((a3 > a4) && (a3 > center_x))
216 inside++;
217 } else {
219 if (direction == UP) {
220 if (b4 <= ((m3 * a4) + bb3))
221 inside++;
223 } else if (direction == DOWN) {
224 if (b4 >= ((m3 * a4) + bb3))
225 inside++;
226 } // end of the if-else
228 } // end of the if-else
230 #if DRAW_LINE_SIGHT
231 //DrawLineOfSight(bot->x, bot->y, tmp_x, tmp_y);
232 DrawLineOfSight(tmp_x2, tmp_y2, tmp_x3, tmp_y3);
233 DrawLineOfSight(bot->x, bot->y, tmp_x2, tmp_y2);
234 DrawLineOfSight(bot->x, bot->y, tmp_x3, tmp_y3);
235 #endif
237 // check if point lies inside
238 if (inside == 3) {
240 return true;
242 } else {
243 return false;
244 } // end of if-else
246 } // end of the function