-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit.cpp
More file actions
445 lines (397 loc) · 10.2 KB
/
unit.cpp
File metadata and controls
445 lines (397 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* $Id: unit.cpp,v 1.18 2007/02/22 01:50:51 bulitko Exp $
*
* Hierarchical Open Graph File
*
* Created by Nathan Sturtevant on 9/28/04.
* Copyright 2004 Nathan Sturtevant. All rights reserved.
*
* This file is part of HOG.
*
* HOG is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* HOG is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HOG; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "unit.h"
#include "glUtil.h"
#include "fpUtil.h"
#include <cstdlib>
GLuint unit::sphereDispList = 0;
int unit::unitID = 0;
/** Create a unit
*
* Create a unit starting at a given x/y location, with a potential target.
*/
unit::unit(int _x, int _y, unit *_target)
:x(_x), y(_y), target(_target), group(0)
{
unitType = kDisplayOnly;
speed = .5;
map_revision = -1;
// blocking = true;
// lastMoveSuccess = true;
// aMap = 0;
r = (GLfloat)(rand()%256)/256.0;
g = (GLfloat)(rand()%256)/256.0;
b = (GLfloat)(rand()%256)/256.0;
if (target)
{
if (target->getObjectType() == kDisplayOnly)
target->setColor(r, g, b);
else
setColor(target->r*.8, target->g*.8, target->b*.8);
}
id = unit::unitID++;
}
/** Create a unit
*
* Create a unit starting at a given x/y location, r/g/b (0.255), with a potential target.
*/
// Create a unit with specified color
unit::unit(int _x, int _y, int _r, int _g, int _b, unit *_target)
:x(_x), y(_y), target(_target), group(0)
{
unitType = kDisplayOnly;
speed = .5;
map_revision = -1;
// blocking = true;
// lastMoveSuccess = true;
// aMap = 0;
r = (GLfloat)(_r%256)/256.0;
g = (GLfloat)(_g%256)/256.0;
b = (GLfloat)(_b%256)/256.0;
if (target)
{
if (target->getObjectType() == kDisplayOnly)
target->setColor(r, g, b);
else
setColor(target->r*.8, target->g*.8, target->b*.8);
}
id = unit::unitID++;
}
/** Create a unit
*
* Create a unit starting at a given x/y location, r/g/b (0.255), with a potential target.
*/
// Create a unit with specified color
unit::unit(int _x, int _y, float _r, float _g, float _b, unit *_target)
:x(_x), y(_y), target(_target), group(0)
{
unitType = kDisplayOnly;
speed = .5;
map_revision = -1;
// blocking = true;
// lastMoveSuccess = true;
// aMap = 0;
r = _r;
g = _g;
b = _b;
if (target)
{
if (target->getObjectType() == kDisplayOnly)
target->setColor(r, g, b);
else
setColor(target->r*.8, target->g*.8, target->b*.8);
}
id = unit::unitID++;
}
unit::~unit()
{
}
void unit::setUnitGroup(unitGroup *_group)
{
// If we already set to the given group then do nothing
if (_group == group)
return;
unitGroup *tmp = group;
// Set the back pointer
group = _group;
// If we had a group before then move
if (tmp != 0)
{
tmp->removeUnit(this);
if (_group)
_group->addUnit(this);
}
}
// if (_group == group)
// return;
// if (group)
// group->removeUnit(this);
// group = _group;
// if (group)
// group->addUnit(this);
//void unit::madeMove(unit *, tDirection ) { }
/** Make a move in the environment
*
* This is the most important function for any unit. Given an abstract map of the
* world, the unit must decide how to act next, by returning a direction for the
* next step. Overload this function to define your own unit movement. The default
* action is to stay put.
*/
tDirection unit::makeMove(mapProvider *, reservationProvider *, simulationInfo *)
{
return kStay;
}
/** Log any stats the unit might have collected so far.
*
* No stats collected by default.
*/
void unit::logStats(statCollection *)
{
}
/** is map updated?
*
* returns true if the map has been udpated from the last time we checked.
*/
bool unit::mapUpdated(mapAbstraction *aMap)
{
Map *map = aMap->getMap();
if (map_revision != map->getRevision())
{
map_revision = map->getRevision();
//printf("%p map_revision updated to %d\n", this, map_revision);
return true;
}
return false;
}
/**
* getLocation of unit
*
* returns the current unit location by reference
*/
void unit::getLocation(int &_x, int &_y)
{
_x = x; _y = y;
}
/**
* Get OpenGL coordinates of unit.
*
* Returns the x/y/z open GL coordinates of a unit, as well as the radius of the unit
* in the world.
*/
void unit::getOpenGLLocation(Map *map, GLdouble &_x, GLdouble &_y, GLdouble &_z, GLdouble &radius)
{
map->getOpenGLCoord(x, y, _x, _y, _z, radius);
}
/**
* Draw the unit.
*
* Draw the unit using openGL. Overload this if you want a custom look for the unit.
*/
void unit::openGLDraw(mapProvider *mp, simulationInfo *)
{
Map *map = mp->getMap();
GLdouble xx, yy, zz, rad;
if ((x < 0) || (x >= map->getMapWidth()) || (y < 0) || (y >= map->getMapHeight()))
return;
map->getOpenGLCoord(x, y, xx, yy, zz, rad);
glColor3f(r, g, b);
if (getObjectType() == kDisplayOnly)
drawTriangle(xx, yy, zz, rad);
else
drawSphere(xx, yy, zz, rad);
}
/**
* Draw a triangle for the unit.
*
* Draw's a pyramid at the location specified. Used for drawing kDisplayOnly objects.
*/
void unit::drawTriangle(GLdouble _x, GLdouble _y, GLdouble _z, GLdouble tRadius)
{
drawPyramid(_x, _y, _z, tRadius, 0.75*tRadius);
}
/**
* Draw a dome for the unit.
*
* Draw a dome at the coordinates specified; used for all other units by default.
*/
void unit::drawSphere(GLdouble _x, GLdouble _y, GLdouble _z, GLdouble tRadius)
{
if (sphereDispList)
{
glTranslatef(_x, _y, _z);
glCallList(sphereDispList);
glTranslatef(-_x, -_y, -_z);
return;
}
sphereDispList = glGenLists(1);
glTranslatef(_x, _y, _z);
glNewList(sphereDispList, GL_COMPILE_AND_EXECUTE);
int i,j;
int n = 64; // precision
double theta1,theta2,theta3;
point3d e,p,c(0, 0, 0);
if (tRadius < 0) tRadius = -tRadius;
if (n < 0) n = -n;
if (n < 4 || tRadius <= 0)
{
glBegin(GL_POINTS);
glVertex3f(c.x,c.y,c.z);
glEnd();
}
else {
for (j=n/4;j<n/2;j++)
{
theta1 = j * TWOPI / n - PID2;
theta2 = (j + 1) * TWOPI / n - PID2;
glBegin(GL_QUAD_STRIP);
//glBegin(GL_POINTS);
//glBegin(GL_TRIANGLE_STRIP);
//glBegin(GL_LINE_STRIP);
for (i=0;i<=n;i++)
{
theta3 = i * TWOPI / n;
e.x = cos(theta2) * cos(theta3);
e.y = cos(theta2) * sin(theta3);
e.z = sin(theta2);
p.x = c.x + tRadius * e.x;
p.y = c.y + tRadius * e.y;
p.z = c.z - tRadius * e.z;
glNormal3f(-e.x,-e.y,e.z);
//glTexCoord2f(i/(double)n,2*(j+1)/(double)n);
glVertex3f(p.x,p.y,p.z);
e.x = cos(theta1) * cos(theta3);
e.y = cos(theta1) * sin(theta3);
e.z = sin(theta1);
p.x = c.x + tRadius * e.x;
p.y = c.y + tRadius * e.y;
p.z = c.z - tRadius * e.z;
glNormal3f(-e.x,-e.y,e.z);
//glTexCoord2f(i/(double)n,2*j/(double)n);
glVertex3f(p.x,p.y,p.z);
}
glEnd();
}
}
glEndList();
glTranslatef(-_x, -_y, -_z);
}
/**
* Make a random move.
*
* the random unit follows a certain direction for a random amount of time,
* and then picks a new direction. makeMove just returns the current direction.
*/
tDirection randomUnit::makeMove(mapProvider *, reservationProvider *, simulationInfo *)
{
tDirection where = possibleDir[lastIndex];
return where;
}
/**
* Set location after last move.
*
* After moving, the unit picks a new random direction if the move wasn't a success.
* If it was, it has a 5% chance of changing direction.
*/
void randomUnit::updateLocation(int _x, int _y, bool success, simulationInfo *)
{
if (success)
{ // I moved successfully
if (rand()%20 == 7)
lastIndex = rand()%9;
}
else {
lastIndex = rand()%9;
}
x = _x; y = _y;
}
/**
* Make a move.
*
* the billiard ball unit keeps following the same direction until either a collision
* or instability
*/
tDirection billiardBallUnit::makeMove(mapProvider *, reservationProvider *, simulationInfo *)
{
tDirection where = possibleDir[lastIndex];
return where;
}
/**
* Set location after last move.
*
* if the billiard ball unit collides, it will cool off for a period of time
* otherwise, it may changes its mind if it unstable
*/
void billiardBallUnit::updateLocation(int _x, int _y, bool success, simulationInfo *)
{
if (success)
{
// I moved successfully
// If I was staying put then I should start moving after the coolOffPeriod expires
if (lastIndex == kStayIndex && --collisionStatus <= 0)
lastIndex = rand()%9;
// If I was moving then I may change my mind with probability probDirChange
if (lastIndex != kStayIndex && fless(rand()%10000,probDirChange*10000.0))
lastIndex = rand()%9;
}
else {
// I was not successful in my move (i.e., I collided)
// start resting
collisionStatus = coolOffPeriod;
lastIndex = coolOffPeriod > 0 ? kStayIndex : rand()%9;
}
x = _x; y = _y;
}
/**
* Make the next move.
*
* Make the next move following the right hand rule.
*/
tDirection rhrUnit::makeMove(mapProvider *, reservationProvider *, simulationInfo *)
{
tDirection where = possibleDir[lastIndex];
return where;
}
/**
* set rhr unit location.
*
* To follow the right hand rule, we do the following:
* 1) if the last move was successful, turn 90 degrees to the right and keep going.
* 2) if the last move wasn't successful, turn 90 degrees to the left and keep going.
*/
void rhrUnit::updateLocation(int _x, int _y, bool success, simulationInfo *)
{
if (success)
{ // I moved successfully
lastIndex = (lastIndex+2)%8;
}
else {
lastIndex = (lastIndex+6)%8;
}
x = _x; y = _y;
}
/**
* make teleport move.
*
* the teleport unit stays put for some # moves, and then teleports to a new random
* location.
*/
tDirection teleportUnit::makeMove(mapProvider *mp, reservationProvider *, simulationInfo *)
{
mapAbstraction *aMap = mp->getMapAbstraction();
if (timer == 0)
{
timer = stayTime;
x = rand()%aMap->getMap()->getMapWidth();
y = rand()%aMap->getMap()->getMapHeight();
return kTeleport;
}
timer--;
return kStay;
}
void teleportUnit::updateLocation(int _x, int _y, bool, simulationInfo *)
{
x = _x; y = _y;
}