-
Notifications
You must be signed in to change notification settings - Fork 0
/
Car.cpp
307 lines (244 loc) · 8.39 KB
/
Car.cpp
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
/*-----------------------------------------------------------------------------
Car.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
This creates the little two-triangle cars and moves them around the map.
-----------------------------------------------------------------------------*/
#define DEAD_ZONE 200
#define STUCK_TIME 230
#define UPDATE_INTERVAL 50 //milliseconds
#define MOVEMENT_SPEED 0.61f
#define CAR_SIZE 3.0f
#include <windows.h>
#include <math.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include "glTypes.h"
#include "building.h"
#include "car.h"
#include "camera.h"
#include "mesh.h"
#include "macro.h"
#include "math.h"
#include "random.h"
#include "render.h"
#include "texture.h"
#include "world.h"
#include "visible.h"
#include "win.h"
static GLvector direction[] =
{
0.0f, 0.0f, -1.0f,
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f,
-1.0f, 0.0f, 0.0f,
};
static int dangles[] = { 0, 90, 180, 270};
static GLvector2 angles[360];
static bool angles_done;
static unsigned char carmap[WORLD_SIZE][WORLD_SIZE];
static CCar* head;
static unsigned next_update;
static int count;
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
int CarCount ()
{
return count;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CarClear ()
{
CCar* c;
for (c = head; c; c = c->m_next)
c->Park ();
ZeroMemory (carmap, sizeof (carmap));
count = 0;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CarRender ()
{
CCar* c;
if (!angles_done) {
for (int i = 0 ;i < 360; i++) {
angles[i].x = cosf ((float)i * DEGREES_TO_RADIANS) * CAR_SIZE;
angles[i].y = sinf ((float)i * DEGREES_TO_RADIANS) * CAR_SIZE;
}
}
glDepthMask (false);
glEnable (GL_BLEND);
glDisable (GL_CULL_FACE);
glBlendFunc (GL_ONE, GL_ONE);
glBindTexture (GL_TEXTURE_2D, 0);
glBindTexture(GL_TEXTURE_2D, TextureId (TEXTURE_HEADLIGHT));
for (c = head; c; c = c->m_next)
c->Render ();
glDepthMask (true);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CarUpdate ()
{
CCar* c;
unsigned now;
if (!TextureReady () || !EntityReady ())
return;
now = GetTickCount ();
if (next_update > now)
return;
next_update = now + UPDATE_INTERVAL;
for (c = head; c; c = c->m_next)
c->Update ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CCar::CCar ()
{
m_ready = false;
m_next = head;
head = this;
count++;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
bool CCar::TestPosition (int row, int col)
{
//test the given position and see if it's already occupied
if (carmap[row][col])
return false;
//now make sure that the lane is going the right direction
if (WorldCell (row, col) != WorldCell (m_row, m_col))
return false;
return true;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CCar::Update (void)
{
int new_row, new_col;
GLvector old_pos;
GLvector camera;
//If the car isn't ready, place it on the map and get it moving
camera = CameraPosition ();
if (!m_ready) {
//if the car isn't ready, we need to place it somewhere on the map
m_row = DEAD_ZONE + RandomVal (WORLD_SIZE - DEAD_ZONE * 2);
m_col = DEAD_ZONE + RandomVal (WORLD_SIZE - DEAD_ZONE * 2);
//if there is already a car here, forget it.
if (carmap[m_row][m_col] > 0)
return;
//if this spot is not a road, forget it
if (!(WorldCell (m_row, m_col) & CLAIM_ROAD))
return;
if (!Visible (glVector ((float)m_row, 0.0f, (float)m_col)))
return;
//good spot. place the car
m_position = glVector ((float)m_row, 0.1f, (float)m_col);
m_drive_position = m_position;
m_ready = true;
if (WorldCell (m_row, m_col) & MAP_ROAD_NORTH)
m_direction = NORTH;
if (WorldCell (m_row, m_col) & MAP_ROAD_EAST)
m_direction = EAST;
if (WorldCell (m_row, m_col) & MAP_ROAD_SOUTH)
m_direction = SOUTH;
if (WorldCell (m_row, m_col) & MAP_ROAD_WEST)
m_direction = WEST;
m_drive_angle = dangles[m_direction];
m_max_speed = (float)(4 + RandomVal (6)) / 10.0f;
m_speed = 0.0f;
m_change = 3;
m_stuck = 0;
carmap[m_row][m_col]++;
}
//take the car off the map and move it
carmap[m_row][m_col]--;
old_pos = m_position;
m_speed += m_max_speed * 0.05f;
m_speed = MIN (m_speed, m_max_speed);
m_position += direction[m_direction] * MOVEMENT_SPEED * m_speed;
//If the car has moved out of view, there's no need to keep simulating it.
if (!Visible (glVector ((float)m_row, 0.0f, (float)m_col)))
m_ready = false;
//if the car is far away, remove it. We use manhattan units because buildings almost always
//block views of cars on the diagonal.
if (fabs (camera.x - m_position.x) + fabs (camera.z - m_position.z) > RenderFogDistance ())
m_ready = false;
//if the car gets too close to the edge of the map, take it out of play
if (m_position.x < DEAD_ZONE || m_position.x > (WORLD_SIZE - DEAD_ZONE))
m_ready = false;
if (m_position.z < DEAD_ZONE || m_position.z > (WORLD_SIZE - DEAD_ZONE))
m_ready = false;
if (m_stuck >= STUCK_TIME)
m_ready = false;
if (!m_ready)
return;
//Check the new position and make sure its not in another car
new_row = (int)m_position.x;
new_col = (int)m_position.z;
if (new_row != m_row || new_col != m_col) {
//see if the new position places us on top of another car
if (carmap[new_row][new_col]) {
m_position = old_pos;
m_speed = 0.0f;
m_stuck++;
} else {
//look at the new position and decide if we're heading towards or away from the camera
m_row = new_row;
m_col = new_col;
m_change--;
m_stuck = 0;
if (m_direction == NORTH)
m_front = camera.z < m_position.z;
else if (m_direction == SOUTH)
m_front = camera.z > m_position.z;
else if (m_direction == EAST)
m_front = camera.x > m_position.x;
else
m_front = camera.x < m_position.x;
}
}
m_drive_position = (m_drive_position + m_position) / 2.0f;
//place the car back on the map
carmap[m_row][m_col]++;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CCar::Render ()
{
GLvector pos;
int angle;
int turn;
float top;
if (!m_ready)
return;
if (!Visible (m_drive_position))
return;
if (m_front) {
glColor3f (1, 1, 0.8f);
top = CAR_SIZE;
} else {
glColor3f (0.5, 0.2f, 0);
top = 0.0f;
}
glBegin (GL_QUADS);
angle = dangles[m_direction];
pos = m_drive_position;//
angle = 360 - (int)MathAngle (m_position.x, m_position.z, pos.x, pos.z);
angle %= 360;
turn = (int)MathAngleDifference ((float)m_drive_angle, (float)angle);
m_drive_angle += SIGN (turn);
pos += glVector (0.5f, 0.0f, 0.5f);
glTexCoord2f (0, 0);
glVertex3f (pos.x + angles[angle].x, -CAR_SIZE, pos.z + angles[angle].y);
glTexCoord2f (1, 0);
glVertex3f (pos.x - angles[angle].x, -CAR_SIZE, pos.z - angles[angle].y);
glTexCoord2f (1, 1);
glVertex3f (pos.x - angles[angle].x, top, pos.z - angles[angle].y);
glTexCoord2f (0, 1);
glVertex3f (pos.x + angles[angle].x, top, pos.z + angles[angle].y);
glEnd ();
}