-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cc
397 lines (362 loc) · 9.75 KB
/
game.cc
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
/*
* GTK Cervi
* vim:set sw=4 sta:
*
* Copyright (C) 2003 Tomas Janousek <tomi@nomi.cz>
* See file COPYRIGHT and COPYING
*/
#pragma implementation
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <gdk/gdkkeysyms.h>
#include "game.h"
#include "music.h"
/*
* TODO:
* Controls :)
* (Optional) After reaching wall, do not collide but appear on the other side
* or bounce.
*/
// from main.cc for signalling music thread
extern void signal_mt();
namespace std {
// colors of cerv's
unsigned int colors[] = {
0xff0000,
0x00ff00,
0x00ffff,
0xff00ff,
0xff8000,
0x0000ff,
0xffff00,
0xffffff
};
// controls of cerv's
static struct {
unsigned int left[3], right[3];
} controls[8] = {
{{'N',0,0}, {'M',0,0}},
{{'1','!',0}, {'2','@',0}},
{{'[','{',0}, {']','}',0}},
{{'S',0,0}, {'D',0,0}},
{{GDK_KP_1,0,0}, {GDK_KP_2,0,0}},
{{GDK_KP_Add,0,0}, {GDK_KP_Subtract,0,0}},
{{0,0,0}, {0,0,0}},
{{0,0,0}, {0,0,0}}
};
// create ncervi cerv's, place them
Game::Game(int ncervi, const GameParam& par) : field(par.width,par.height),
_ncollided(0), over(false)
{
n_cervi = ncervi;
for (int i=0; i<n_cervi; i++) {
if (par.placing == 0) {
double x,y,angle,r,speed;
bool bad = 1;
while (bad) {
// place cerv's randomly with some distances between them
// and looking at center
angle = (rand() % 1000) * 2 * M_PI / 1000;
r = (rand() % 500) + 1500;
x = (field.width/2) - (field.width*cos(angle)*r/6000);
y = (field.height/2) - (field.height*sin(angle)*r/6000);
// check for distances between other cerv's
bad = 0;
for (int j=i-1; j>=0; j--)
if (hypot(cervi[j]->_x - x, cervi[j]->_y - y) < 50 ||
fabs(cervi[j]->_angle - angle) < 0.4) {
bad = 1;
break;
}
}
speed = 70 - 5 * (n_cervi - 2);
if (par.speed == 0)
speed += rand() % 50;
else
speed += 25;
cervi[i] = new Cerv(this,x,y,angle,speed,10,colors[i],i);
} else if (par.placing == 1) {
double speed = 70 - 5 * (n_cervi - 2);
if (par.speed == 0)
speed += rand() % 50;
else
speed += 25;
cervi[i] = new Cerv(this,field.width / 2,field.height / 2,
-M_PI_2 + i*2*M_PI/n_cervi +//position cerv's regularly
2*M_PI*((rand()%20)-10)/n_cervi/100, // with variation
speed,10,colors[i],i);
cervi[i]->_cspeed = cervi[i]->_speed;
cervi[i]->update(75*(n_cervi+2)/2); // keep similar distance
// between cerv's for any number of cerv's
cervi[i]->_cspeed = 1;
#ifdef DEBUG
} else {
cerr << "Bad placing" << endl;
throw 0;
#endif /* DEBUG */
}
}
m.reset(); // reset music to initial state
m.speed = speed();
m.play = true;
signal_mt(); // signal it
}
// delete all cervi
Game::~Game()
{
for (int i=0; i<n_cervi; i++) {
delete cervi[i];
}
m.play = false;
signal_mt();
}
// darken color
inline int darkencolor(int color, unsigned char light)
{
return ((((color & 0xff0000)*light/255) & 0xff0000)) |
((((color & 0xff00)*light/255) & 0xff00)) |
((((color & 0xff)*light/255) & 0xff));
}
// count a to the power of 2
inline double to2(double a)
{
return a*a;
}
// count distance and lightness from it
inline unsigned char dist2light(int x, int y, const Cerv *cerv)
{
double distance =
4 - to2(hypot(cerv->_x - x, cerv->_y - y));
if (distance < 0)
return 0;
return (unsigned char)(distance/ /*3.2*/ 4*255);
}
// update & draw
void Game::update(GtkWidget* area, GdkPixmap* pixmap,
unsigned long long int ticks)
{
if (over) return;
GdkGC *gc = gdk_gc_new(((GtkWidget*)area)->window);
if (!gc) throw 0;
if (ticks > 50) // will slow down game, but this can be caused only by
ticks = 50; // slow CPU or high load, so in result, user will have
// control over his Cerv even in these situations
// (... 20 fps is minimum)
// update, check collision, draw ...
for (int i=0; i<n_cervi; i++) {
if (cervi[i]->_collision) continue;
int _x = (int)nearbyint(cervi[i]->_x),
_y = (int)nearbyint(cervi[i]->_y);
for (unsigned long long int j=0; j<ticks; j++) {
cervi[i]->update(1);
int x = (int)nearbyint(cervi[i]->_x),
y = (int)nearbyint(cervi[i]->_y);
if (!(x == _x && y == _y)) {
if (!cervi[i]->_collision) cervi[i]->collision();
// set bits in bitfield
field._b[field.pos(x,y)] = 1;
field._b[field.pos(x-1,y)] = 1;
field._b[field.pos(x+1,y)] = 1;
field._b[field.pos(x,y-1)] = 1;
field._b[field.pos(x,y+1)] = 1;
// draw it (with antialias)
for (int iy = -2; iy < 3; iy++)
for (int ix = -2; ix < 3; ix++) {
unsigned char light =
dist2light(x+ix,y+iy,cervi[i]);
if (field._l[field.pos(x+ix,y+iy)] < light) {
field._l[field.pos(x+ix,y+iy)] = light;
gdk_rgb_gc_set_foreground(gc,
darkencolor(cervi[i]->_color,light));
gdk_draw_point(pixmap,gc,x+ix,y+iy);
}
}
_x = x;
_y = y;
GdkRectangle update_rect = {x-2,y-2,5,5};
gtk_widget_draw(GTK_WIDGET(area), &update_rect);
}
if (cervi[i]->_collision) break;
}
}
// count collided cerv's
int ncollision = 0;
for (int i=0; i<n_cervi; i++) {
if (cervi[i]->_collision)
ncollision++;
}
// increase speed if someone collided
if (ncollision - _ncollided) {
for (int i=0; i<n_cervi; i++)
if (!cervi[i]->_collision) {
cervi[i]->_speed += (ncollision - _ncollided) * 20;
cervi[i]->_rotspeed -= (ncollision - _ncollided);
}
m.speed = speed();
}
// set place numbers
int maxplace = -1;
for (int i=0; i<n_cervi; i++) {
if (cervi[i]->_collision && cervi[i]->_place > maxplace) {
maxplace = cervi[i]->_place;
}
}
if (maxplace == -1) maxplace++;
maxplace++;
for (int i=0; i<n_cervi; i++) {
if (cervi[i]->_collision && cervi[i]->_place == -1) {
cervi[i]->_place = maxplace;
}
}
// is round over ??
if (ncollision == n_cervi)
over = true;
// if all except one are collided, collide him to end round
if (ncollision == n_cervi-1 && n_cervi != 1) {
for (int i=0; i<n_cervi; i++) {
if (!cervi[i]->_collision) {
cervi[i]->_collision = true;
m.play = false;
signal_mt();
}
}
}
_ncollided = ncollision;
gdk_gc_unref(gc);
}
// save place numbers to int pos[n_cervi]
void Game::result(int *pos)
{
int maxplace = -1;
for (int i=0; i<n_cervi; i++) {
if (cervi[i]->_collision && cervi[i]->_place > maxplace) {
maxplace = cervi[i]->_place;
}
}
maxplace++;
for (int i=0; i<n_cervi; i++) {
pos[i] = maxplace - cervi[i]->_place;
}
}
// get game speed (average speed of non-collided cerv's)
double Game::speed()
{
double s = 0;
int n = 0;
for (int i=0; i<n_cervi; i++) {
if (cervi[i]->_collision) continue;
s += cervi[i]->_cspeed;
n ++;
}
if (n) {
return s/n;
}
return 100;
}
// move and/or rotate cerv and check if collided to wall
void Cerv::update(unsigned long long ticks)
{
for (int i=0; i<3; i++)
if (_game->keymap[controls[_n].left[i]]) {
_angle -= ticks * _rotspeed * _cspeed / 150 / 1000;
break;
}
for (int i=0; i<3; i++)
if (_game->keymap[controls[_n].right[i]]) {
_angle += ticks * _rotspeed * _cspeed / 150 / 1000;
break;
}
while (_angle > 2*M_PI)
_angle -= 2*M_PI;
while (_angle < 0)
_angle = 2*M_PI + _angle;
_x += ticks * _cspeed * cos(_angle) / 1000;
_y += ticks * _cspeed * sin(_angle) / 1000;
if (_cspeed < _speed) {
_cspeed += _speed * ticks / 1000;
if (_cspeed > _speed)
_cspeed = _speed;
m.speed = _game->speed();
}
if (_x < 1) {
_x = 1;
_collision = true;
}
if (_y < 1) {
_y = 1;
_collision = true;
}
if (_x > (_game->field.width-2)) {
_x = _game->field.width-2;
_collision = true;
}
if (_y > (_game->field.height-2)) {
_y = _game->field.height-2;
_collision = true;
}
}
// check if collided to himself or another cerv
int Cerv::collision()
{
if (!_collision) {
if (_angle >= 0 && _angle < M_PI_2) {
if (_game->field._b[_game->field.pos((int)nearbyint(_x)+2,
(int)nearbyint(_y))]
|| _game->field._b[_game->field.pos((int)nearbyint(_x)+1,
(int)nearbyint(_y)+1)]
|| _game->field._b[_game->field.pos((int)nearbyint(_x),
(int)nearbyint(_y)+2)]) {
_collision = true;
// |
// -+-
// |/
}
} else if (_angle >= M_PI_2 && _angle < M_PI) {
if (_game->field._b[_game->field.pos((int)nearbyint(_x)-2,
(int)nearbyint(_y))]
|| _game->field._b[_game->field.pos((int)nearbyint(_x)-1,
(int)nearbyint(_y)+1)]
|| _game->field._b[_game->field.pos((int)nearbyint(_x),
(int)nearbyint(_y)+2)]) {
_collision = true;
// |
// -+-
// \|
}
} else if (_angle >= M_PI && _angle < M_PI+M_PI_2) {
if (_game->field._b[_game->field.pos((int)nearbyint(_x)-2,
(int)nearbyint(_y))]
|| _game->field._b[_game->field.pos((int)nearbyint(_x)-1,
(int)nearbyint(_y)-1)]
|| _game->field._b[_game->field.pos((int)nearbyint(_x),
(int)nearbyint(_y)-2)]) {
_collision = true;
// /|
// -+-
// |
}
} else if (_angle >= M_PI+M_PI_2 && _angle < 2*M_PI) {
if (_game->field._b[_game->field.pos((int)nearbyint(_x)+2,
(int)nearbyint(_y))]
|| _game->field._b[_game->field.pos((int)nearbyint(_x)+1,
(int)nearbyint(_y)-1)]
|| _game->field._b[_game->field.pos((int)nearbyint(_x),
(int)nearbyint(_y)-2)]) {
_collision = true;
/* |\ */
// -+-
// |
}
#ifdef DEBUG
} else {
// this cannot happen if EVERYTHING is right
cerr << "This cannot happen!!! " << __FILE__ << " - " <<
__LINE__ << endl;
throw 0;
#endif /* DEBUG */
}
}
return _collision;
}
}