- Binary is now relocatable
[crack-attack.git] / src / Game.h
blobe187e82d8e466e1a61ddbeb591b15a6bb82acd1b
1 /*
2 * Game.h
3 * Daniel Nelson - 8/24/0
5 * Copyright (C) 2000 Daniel Nelson
6 * Copyright (C) 2004 Andrew Sayman
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * Daniel Nelson - aluminumangel.org
23 * 174 W. 18th Ave.
24 * Columbus, OH 43210
27 #ifndef GAME_H
28 #define GAME_H
30 #include <climits>
31 #include <cstdlib>
32 #include <cmath>
33 #include <iostream>
34 #include <vector>
35 #include <GL/glut.h>
37 #ifndef _WIN32
38 # include <config.h>
39 #else
40 # include <glext.h>
41 #endif
43 #include "Mode.h"
45 using namespace std;
47 // null
48 #define null (0)
50 // no IEEE super pi
51 #define PI (3.14159265358979323846f)
52 #define SQRT_3 (1.73205080756887729352f)
54 // the name
55 #define GC_NAME "Crack Attack!"
56 #define GC_BINARY "crack-attack"
57 #define GC_VERSION "1.1.12"
59 // startup message
60 #define GC_MESSAGE GC_NAME " v" GC_VERSION "\n"
62 // directory delimiter
63 #ifndef _WIN32
64 # define GC_DD "/"
65 #else
66 # define GC_DD "\\"
67 #endif
69 // file constants
70 #ifndef _WIN32
71 # ifndef DATA_DIRECTORY
72 # define GC_DATA_DIRECTORY(x) "." GC_DD "data/" x
73 # else
74 # define GC_DATA_DIRECTORY(x) DATA_DIRECTORY x
75 # endif
76 # define GC_LOCAL_DATA_DIRECTORY GC_DD "." GC_BINARY GC_DD
77 #else
78 # define GC_DATA_DIRECTORY(x) ".." GC_DD "data" GC_DD x
79 # define GC_LOCAL_DATA_DIRECTORY(x) ".." GC_DD "localdata" GC_DD x
80 #endif
81 #ifdef ENABLE_BINRELOC
82 # undef GC_DATA_DIRECTORY
83 # include "prefix.h"
84 # define GC_DATA_DIRECTORY_INTER br_strcat(DATADIR, GC_DD GC_BINARY GC_DD)
85 # define GC_DATA_DIRECTORY(x) br_strcat(GC_DATA_DIRECTORY_INTER, x)
86 #endif
87 #define GC_GARBAGE_TEX_FILE_NAME_BASE "garbage_flavor"
88 #define GC_GARBAGE_TEX_NUMBER_DIGITS (3)
89 #define GC_NUMBER_STANDARD_GARBAGE_TEX (6)
90 #define GC_GARBAGE_TEX_MAX_NUMBER (1000)
91 #define GC_GARBAGE_NET_TEX_FILE_NAME GC_GARBAGE_TEX_FILE_NAME_BASE \
92 "_net.tga"
93 #define GC_GARBAGE_MY_TEX_FILE_NAME GC_GARBAGE_TEX_FILE_NAME_BASE \
94 "_my.tga"
95 #define GC_LOGO_TEX_FILE_NAME GC_DATA_DIRECTORY("logo.tga")
96 #define GC_X_LOGO_TEX_FILE_NAME GC_DATA_DIRECTORY("logo_x.tga")
97 #define GC_GARBAGE_LOGO_TEX_FILE_NAME GC_DATA_DIRECTORY(\
98 GC_GARBAGE_TEX_FILE_NAME_BASE \
99 "_logo.tga")
100 #define GC_GARBAGE_X_LOGO_TEX_FILE_NAME GC_DATA_DIRECTORY(\
101 GC_GARBAGE_TEX_FILE_NAME_BASE \
102 "_logo_x.tga")
103 #define GC_COUNT_DOWN_GO_TEX_FILE_NAME GC_DATA_DIRECTORY("count_down_go.tga")
104 #define GC_COUNT_DOWN_1_TEX_FILE_NAME GC_DATA_DIRECTORY("count_down_1.tga")
105 #define GC_COUNT_DOWN_2_TEX_FILE_NAME GC_DATA_DIRECTORY("count_down_2.tga")
106 #define GC_COUNT_DOWN_3_TEX_FILE_NAME GC_DATA_DIRECTORY("count_down_3.tga")
107 #define GC_ANYKEY_TEX_FILE_NAME GC_DATA_DIRECTORY("message_anykey.tga")
108 #define GC_WAITING_TEX_FILE_NAME GC_DATA_DIRECTORY("message_waiting.tga")
109 #define GC_PAUSED_TEX_FILE_NAME GC_DATA_DIRECTORY("message_paused.tga")
110 #define GC_WINNER_TEX_FILE_NAME GC_DATA_DIRECTORY("message_winner.tga")
111 #define GC_LOSER_TEX_FILE_NAME GC_DATA_DIRECTORY("message_loser.tga")
112 #define GC_GAME_OVER_TEX_FILE_NAME GC_DATA_DIRECTORY(\
113 "message_game_over.tga")
114 #define GC_X_REC_FILE_NAME "score_record_x"
115 #define GC_REC_FILE_NAME "score_record"
116 #define GC_DEFAULT_REC_FILE_NAME GC_DATA_DIRECTORY("default_record")
118 // random angle tables' size; must be power of two
119 #define GC_SIZE_RANDOM_ANGLE_TABLE (256)
121 // games per match
122 #define GC_GAMES_PER_MATCH (3)
124 // the play area dimensions; GC_SAFE_HEIGHT - 1 is the board height
125 #define GC_PLAY_WIDTH (6)
126 #define GC_PLAY_HEIGHT (45)
127 #define GC_SAFE_HEIGHT (13)
128 #define GC_GRID_SIZE (GC_PLAY_WIDTH * GC_PLAY_HEIGHT)
130 // object stores
131 #define GC_BLOCK_STORE_SIZE (GC_GRID_SIZE)
132 #define GC_GARBAGE_STORE_SIZE (2 * GC_PLAY_HEIGHT)
133 #define GC_COMBO_TABULATOR_STORE_SIZE (8)
134 #define GC_GARBAGE_QUEUE_SIZE (8)
136 // time steps per second
137 #define GC_STEPS_PER_SECOND (50)
139 // milliseconds per time step
140 #define GC_TIME_STEP_PERIOD (1000 / GC_STEPS_PER_SECOND)
142 // number of subdivisions per grid location
143 #define GC_STEPS_PER_GRID (60)
145 // names
146 #define GC_PLAYER_NAME_LENGTH (256)
147 #define GC_DEFAULT_PLAYER_NAME "NamelessOne"
149 // velocity of falling; must be a factor of GC_STEPS_PER_GRID
150 #define GC_FALL_VELOCITY (20)
152 // velocity and time length of swapping
153 #define GC_SWAP_VELOCITY (10)
154 #define GC_SWAP_DELAY (GC_STEPS_PER_GRID / GC_SWAP_VELOCITY)
156 // velocity of creep
157 #define GC_CREEP_DELAY (1200)
158 #define GC_CREEP_ADVANCE_TIMER_STEP GC_CREEP_DELAY
159 #define GC_CREEP_TIMER_STEP_INCREMENT (20)
160 #define GC_CREEP_INITIAL_TIMER_STEP GC_CREEP_TIMER_STEP_INCREMENT
161 #define GC_CREEP_MAX_TIMER_STEP (2400)
162 #define GC_CREEP_ADVANCE_VELOCITY (3)
163 #define GC_CREEP_INCREMENT_DELAY (10 * GC_STEPS_PER_SECOND)
165 // delay between safe height violation and loss
166 #define GC_LOSS_DELAY (7 * GC_STEPS_PER_SECOND)
167 #define GC_LOSS_DELAY_ELIMINATION (1 * GC_STEPS_PER_SECOND)
169 // time length between moves
170 #define GC_MOVE_DELAY (6)
172 // time length of block dying
173 #define GC_DYING_DELAY (90)
175 // time length of hanging
176 #define GC_HANG_DELAY (3)
178 // time length until popping, between popping, and after popping
179 #define GC_INTERNAL_POP_DELAY (15)
180 #define GC_INITIAL_POP_DELAY (50 + GC_INTERNAL_POP_DELAY)
181 #define GC_FINAL_POP_DELAY (50)
183 // minimum length of a elimination pattern
184 #define GC_MIN_PATTERN_LENGTH (3)
186 // chance of a creep row having a special block
187 #define GC_NO_SPECIAL_BLOCK_CHANCE_IN (3)
188 #define GC_X_NO_SPECIAL_BLOCK_CHANCE_IN (10)
190 // chance of garbage shattering to garbage
191 #define GC_GARBAGE_TO_GARBAGE_SHATTER (2)
193 // time length before garbage falls
194 #define GC_AVERAGE_GARBAGE_DROP_DELAY (300)
195 #define GC_SPREAD_GARBAGE_DROP_DELAY (40)
197 // time length of the introduction pause; should be multiple of 3
198 #define GC_START_PAUSE_DELAY (150)
200 // maximum height of a garbage block
201 #define GC_MAX_GARBAGE_HEIGHT (11)
203 // score constants
204 #define GC_MIN_PATTERN_SCORE (2)
205 #define GC_GRAY_SCORE (3)
206 #define GC_NUMBER_DIGITS (7)
207 #define GC_MIN_NUMBER_DIGITS_DISPLAYED (4)
208 #define GC_MAX_SCORE_INCREMENT_DELAY (12)
209 #define GC_MIN_SCORE_INCREMENT_DELAY (1)
210 #define GC_SCORE_DELAY_SLOPE (2)
211 #define GC_SCORE_REC_LENGTH (30)
212 #define GC_SCORE_DEFAULT_TOP_SCORE (600)
213 #define GC_SCORE_REC_DEFAULT_NAME "-----"
215 // sine constants
216 #define GC_SINE_TABLE_LENGTH (100)
217 #define GC_SINE_TABLE_LOWER_BOUND (-PI / 2.0f)
218 #define GC_SINE_TABLE_STEP_WIDTH (PI / (GC_SINE_TABLE_LENGTH - 1))
220 // initial swapper location
221 #define GC_INITIAL_SWAPPER_LOCATION_X (GC_PLAY_WIDTH / 2 - 1)
222 #define GC_INITIAL_SWAPPER_LOCATION_Y (4)
224 // extreme constants
225 #define GC_INVISIBLE_MAX_ALPHA (330)
226 #define GC_INVISIBLE_MIN_ALPHA (-20)
227 #define GC_INVISIBLE_QUICK_DECAY_RATE (3)
228 #define GC_INVISIBLE_PULSE_CHANCE_IN (30)
229 #define GC_INVISIBLE_PULSE_STRENGTH (70)
230 #define GC_CRAZY_LONG_MODE_PERIOD (150)
231 #define GC_CRAZY_SHORT_MODE_PERIOD (50)
232 #define GC_MAX_WILD_NUMBER (3)
233 #define GC_WILD_PERIOD (180)
234 #define GC_WILD_POLYMORPH_PERIOD (60)
235 #define GC_MAX_SPECIAL_COLOR_NUMBER (6)
237 // control keys
238 #define GC_LEFT_KEY ('a')
239 #define GC_RIGHT_KEY ('d')
240 #define GC_UP_KEY ('w')
241 #define GC_DOWN_KEY ('s')
242 #define GC_SWAP_KEY ('k')
243 #define GC_ADVANCE_KEY ('l')
244 #define GC_PAUSE_KEY ('p')
246 // Constants for the game window height and width.
247 #define GC_RESOLUTION_0 (400)
248 #define GC_RESOLUTION_1 (570)
249 #define GC_RESOLUTION_2 (680)
250 #define GC_RESOLUTION_3 (970)
251 #define GC_RESOLUTION_4 (1170)
253 // other crap
254 #ifndef max
255 # define max(a, b) ((a) > (b) ? (a) : (b))
256 #endif
258 // insure that our communication int is the same length on all machines
259 #if (INT_MAX == 2147483647)
260 typedef unsigned int uint32;
261 #else
262 typedef unsigned short uint32;
263 #endif
265 // debug
266 #ifndef NDEBUG
267 # include <fstream>
268 # ifndef _WIN32
269 # define COLOR(n) "\33[1;" << (n) << "m"
270 # define NOCOLOR "\33[m"
271 # else
272 # define COLOR(n) ""
273 # define NOCOLOR ""
274 # endif
275 # define DOT(n) { cerr << COLOR(37 - n) \
276 << "." NOCOLOR << flush; }
277 # define DUMP(a) { cerr << COLOR(32) << "Dump: " \
278 __FILE__ ":" << __LINE__ << ": " \
279 NOCOLOR #a \
280 " = " << (a) << endl; }
281 # define PERIODIC_DUMP(a, b) { if (!(Game::time_step % a)) \
282 cerr << COLOR(32) << "Dump: " \
283 __FILE__ ":" << __LINE__ << ": " \
284 NOCOLOR #b \
285 " = " << (b) << endl; }
286 # define MESSAGE(a) { cerr << COLOR(33) << "Mesg: " \
287 __FILE__ ":" << __LINE__ << ": " \
288 NOCOLOR << a << endl; }
289 # ifndef _WIN32
290 # define MARK() { cerr << COLOR(35) << "Mark: " \
291 __FILE__ ":" << __LINE__ << ": " \
292 << __PRETTY_FUNCTION__ << NOCOLOR \
293 << endl; }
294 # else
295 # define MARK() { cerr << COLOR(35) << "Mark: " \
296 __FILE__ ":" << __LINE__ << ":" \
297 NOCOLOR << endl; }
298 # endif
299 # define ENDL() { cerr << COLOR(34) << "Endl: " \
300 __FILE__ ":" << __LINE__ << ":" \
301 NOCOLOR << endl; }
302 # define LOG(a) { ofstream log("log", ios::app); \
303 log << "Log: " __FILE__ ":" \
304 << __LINE__ << ": " << a \
305 << endl; }
306 #else
307 # define DOT(n) ((void) 0)
308 # define DUMP(a) ((void) 0)
309 # define PERIODIC_DUMP(a, b) ((void) 0)
310 # define MESSAGE(a) ((void) 0)
311 # define MARK() ((void) 0)
312 # define ENDL() ((void) 0)
313 # define LOG(a) ((void) 0)
314 #endif
316 // fix bad Visual C++ scoping
317 #ifdef _WIN32
318 # define for if (false) { } else for
319 #endif
321 // game states
323 // Playing.
324 #define GS_NORMAL (1 << 0)
325 // Normal playing, but we're paused.
326 #define GS_PAUSED (1 << 1)
327 // Signals a local unpausing to opponent; used only in communication signals.
328 #define GS_UNPAUSED (1 << 2)
329 // Normal playing, but we're paused because we've gotten ahead of our opponent;
330 // most likely due to a pause.
331 #define GS_SYNC_WAIT (1 << 3)
332 // We've lost, but we haven't heard confirmation from opponent; play continues.
333 #define GS_MAY_HAVE_LOST (1 << 4)
334 // Opponent lost; we've received the message and it predates our loss; play
335 // continues until level lights reach minimum.
336 #define GS_WON (1 << 5)
337 // We've lost and opponent confirms it; play continues until level lights reach
338 // minimum.
339 #define GS_LOST (1 << 6)
340 // We've won but we can't quite yet cause we must confirm our opponent's loss.
341 #define GS_MUST_CONFIRM_LOSS (1 << 7)
342 // We've confirmed our opponent's loss, but we have to wait a bit yet until
343 // he receives it.
344 #define GS_CONFIRMATION_HOLD (1 << 8)
345 // The level lights signaled it's time to end play. Now we must simply complete
346 // the current time step.
347 #define GS_END_PLAY (1 << 9)
348 // Used only for communication. The match has been conceded.
349 #define GS_CONCESSION (1 << 10)
351 // flavor of blocks; special color blocks must be last; wild and then gray must
352 // be directly after normal flavors; here to prevent header entanglements;
353 // the appearance chance of the various flavors can be found in
354 // BlockManager::newCreepBlock()
355 #define BF_NORMAL_1 ( 0)
356 #define BF_NORMAL_2 ( 1)
357 #define BF_NORMAL_3 ( 2)
358 #define BF_NORMAL_4 ( 3)
359 #define BF_NORMAL_5 ( 4)
360 #define BF_WILD ( 5)
361 #define BF_GRAY ( 6)
362 #define BF_BLACK ( 7)
363 #define BF_WHITE ( 8)
364 #define BF_SPECIAL_COLOR_1 ( 9)
365 #define BF_SPECIAL_COLOR_2 (10)
366 #define BF_SPECIAL_COLOR_3 (11)
367 #define BF_SPECIAL_COLOR_4 (12)
368 #define BF_SPECIAL_COLOR_5 (13)
369 #define BF_NUMBER_NORMAL (BF_NORMAL_5 + 1)
370 #define BF_NUMBER (BF_SPECIAL_COLOR_5 + 1)
371 #define BF_NUMBER_SPECIAL (BF_NUMBER - (BF_GRAY + 1))
372 #define BF_FINAL_GRAY_SPECIAL (BF_WHITE)
374 /* static */ class Game {
375 public:
376 static void initialize ( );
377 static void gameStart ( );
378 static void gameFinish ( );
379 static void cleanUp ( );
381 static void loss ( );
382 static void lossConfirmation ( );
383 static void aiPlayerLoss ( );
384 static void won ( );
385 static void netSignalPause ( );
386 static void netSignalUnpause ( );
387 static void syncPause ( int delay );
388 static void concession ( );
390 static void idlePlay ( );
391 static void idleMeta ( );
393 static inline void go ( )
395 previous_time = glutGet((GLenum) GLUT_ELAPSED_TIME);
398 static inline float sqrt ( float x )
400 * An OK approximation of sqrt() on [0, 1] which is correct at the boundaries.
401 * Since all I really want is the vague shape of sqrt() and perfection at 0
402 * and 1, this'll work perfectly.
404 * Generated by minimizing int_0^1{dx (sqrt{x} - a x + (1 - a) x)^2}.
407 return ((27.0f / 14.0f) - (13.0f / 14.0f) * x) * x;
410 static int state;
411 static int time_step;
412 static int awaking_count;
413 static int dying_count;
414 static int dying_count_2;
416 private:
417 static void buttonPause ( );
418 static void syncUnpause ( );
420 static int previous_time;
421 static int remaining_time;
422 static bool button_down_pause;
423 static bool step_play;
424 static int sync_wait;
425 static double lastframe;
428 #endif