Didn't set the version as a string
[crack-attack.git] / src / Game.h
blobcc0d642430ba647ef2c2b2388da93ed4b0e57258
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 <GL/glut.h>
36 #ifndef _WIN32
37 # include <config.h>
38 #else
39 # include <glext.h>
40 #endif
42 #include "Mode.h"
44 using namespace std;
46 // null
47 #define null (0)
49 // no IEEE super pi
50 #define PI (3.14159265358979323846f)
51 #define SQRT_3 (1.73205080756887729352f)
53 // the name
54 #define GC_NAME "Crack Attack!"
55 #define GC_BINARY "crack-attack"
56 #define GC_VERSION "1.1.11"
58 // startup message
59 #define GC_MESSAGE GC_NAME " v" GC_VERSION "\n"
61 // directory delimiter
62 #ifndef _WIN32
63 # define GC_DD "/"
64 #else
65 # define GC_DD "\\"
66 #endif
68 // file constants
69 #ifndef _WIN32
70 # ifndef DATA_DIRECTORY
71 # define GC_DATA_DIRECTORY "." GC_DD "data/"
72 # else
73 # define GC_DATA_DIRECTORY DATA_DIRECTORY
74 # endif
75 # define GC_LOCAL_DATA_DIRECTORY GC_DD "." GC_BINARY GC_DD
76 #else
77 # define GC_DATA_DIRECTORY ".." GC_DD "data" GC_DD
78 # define GC_LOCAL_DATA_DIRECTORY ".." GC_DD "localdata" GC_DD
79 #endif
80 #define GC_GARBAGE_TEX_FILE_NAME_BASE "garbage_flavor"
81 #define GC_GARBAGE_TEX_NUMBER_DIGITS (3)
82 #define GC_NUMBER_STANDARD_GARBAGE_TEX (6)
83 #define GC_GARBAGE_TEX_MAX_NUMBER (1000)
84 #define GC_GARBAGE_NET_TEX_FILE_NAME GC_GARBAGE_TEX_FILE_NAME_BASE \
85 "_net.tga"
86 #define GC_GARBAGE_MY_TEX_FILE_NAME GC_GARBAGE_TEX_FILE_NAME_BASE \
87 "_my.tga"
88 #define GC_LOGO_TEX_FILE_NAME GC_DATA_DIRECTORY "logo.tga"
89 #define GC_X_LOGO_TEX_FILE_NAME GC_DATA_DIRECTORY "logo_x.tga"
90 #define GC_GARBAGE_LOGO_TEX_FILE_NAME GC_DATA_DIRECTORY \
91 GC_GARBAGE_TEX_FILE_NAME_BASE \
92 "_logo.tga"
93 #define GC_GARBAGE_X_LOGO_TEX_FILE_NAME GC_DATA_DIRECTORY \
94 GC_GARBAGE_TEX_FILE_NAME_BASE \
95 "_logo_x.tga"
96 #define GC_COUNT_DOWN_GO_TEX_FILE_NAME GC_DATA_DIRECTORY "count_down_go.tga"
97 #define GC_COUNT_DOWN_1_TEX_FILE_NAME GC_DATA_DIRECTORY "count_down_1.tga"
98 #define GC_COUNT_DOWN_2_TEX_FILE_NAME GC_DATA_DIRECTORY "count_down_2.tga"
99 #define GC_COUNT_DOWN_3_TEX_FILE_NAME GC_DATA_DIRECTORY "count_down_3.tga"
100 #define GC_ANYKEY_TEX_FILE_NAME GC_DATA_DIRECTORY "message_anykey.tga"
101 #define GC_WAITING_TEX_FILE_NAME GC_DATA_DIRECTORY "message_waiting.tga"
102 #define GC_PAUSED_TEX_FILE_NAME GC_DATA_DIRECTORY "message_paused.tga"
103 #define GC_WINNER_TEX_FILE_NAME GC_DATA_DIRECTORY "message_winner.tga"
104 #define GC_LOSER_TEX_FILE_NAME GC_DATA_DIRECTORY "message_loser.tga"
105 #define GC_GAME_OVER_TEX_FILE_NAME GC_DATA_DIRECTORY \
106 "message_game_over.tga"
107 #define GC_X_REC_FILE_NAME "score_record_x"
108 #define GC_REC_FILE_NAME "score_record"
109 #define GC_DEFAULT_REC_FILE_NAME GC_DATA_DIRECTORY "default_record"
111 // random angle tables' size; must be power of two
112 #define GC_SIZE_RANDOM_ANGLE_TABLE (256)
114 // games per match
115 #define GC_GAMES_PER_MATCH (3)
117 // the play area dimensions; GC_SAFE_HEIGHT - 1 is the board height
118 #define GC_PLAY_WIDTH (6)
119 #define GC_PLAY_HEIGHT (45)
120 #define GC_SAFE_HEIGHT (13)
121 #define GC_GRID_SIZE (GC_PLAY_WIDTH * GC_PLAY_HEIGHT)
123 // object stores
124 #define GC_BLOCK_STORE_SIZE (GC_GRID_SIZE)
125 #define GC_GARBAGE_STORE_SIZE (2 * GC_PLAY_HEIGHT)
126 #define GC_COMBO_TABULATOR_STORE_SIZE (8)
127 #define GC_GARBAGE_QUEUE_SIZE (8)
129 // time steps per second
130 #define GC_STEPS_PER_SECOND (50)
132 // milliseconds per time step
133 #define GC_TIME_STEP_PERIOD (1000 / GC_STEPS_PER_SECOND)
135 // number of subdivisions per grid location
136 #define GC_STEPS_PER_GRID (60)
138 // names
139 #define GC_PLAYER_NAME_LENGTH (256)
140 #define GC_DEFAULT_PLAYER_NAME "NamelessOne"
142 // velocity of falling; must be a factor of GC_STEPS_PER_GRID
143 #define GC_FALL_VELOCITY (20)
145 // velocity and time length of swapping
146 #define GC_SWAP_VELOCITY (10)
147 #define GC_SWAP_DELAY (GC_STEPS_PER_GRID / GC_SWAP_VELOCITY)
149 // velocity of creep
150 #define GC_CREEP_DELAY (1200)
151 #define GC_CREEP_ADVANCE_TIMER_STEP GC_CREEP_DELAY
152 #define GC_CREEP_TIMER_STEP_INCREMENT (20)
153 #define GC_CREEP_INITIAL_TIMER_STEP GC_CREEP_TIMER_STEP_INCREMENT
154 #define GC_CREEP_MAX_TIMER_STEP (2400)
155 #define GC_CREEP_ADVANCE_VELOCITY (3)
156 #define GC_CREEP_INCREMENT_DELAY (10 * GC_STEPS_PER_SECOND)
158 // delay between safe height violation and loss
159 #define GC_LOSS_DELAY (7 * GC_STEPS_PER_SECOND)
160 #define GC_LOSS_DELAY_ELIMINATION (1 * GC_STEPS_PER_SECOND)
162 // time length between moves
163 #define GC_MOVE_DELAY (6)
165 // time length of block dying
166 #define GC_DYING_DELAY (90)
168 // time length of hanging
169 #define GC_HANG_DELAY (3)
171 // time length until popping, between popping, and after popping
172 #define GC_INTERNAL_POP_DELAY (15)
173 #define GC_INITIAL_POP_DELAY (50 + GC_INTERNAL_POP_DELAY)
174 #define GC_FINAL_POP_DELAY (50)
176 // minimum length of a elimination pattern
177 #define GC_MIN_PATTERN_LENGTH (3)
179 // chance of a creep row having a special block
180 #define GC_NO_SPECIAL_BLOCK_CHANCE_IN (3)
181 #define GC_X_NO_SPECIAL_BLOCK_CHANCE_IN (10)
183 // chance of garbage shattering to garbage
184 #define GC_GARBAGE_TO_GARBAGE_SHATTER (2)
186 // time length before garbage falls
187 #define GC_AVERAGE_GARBAGE_DROP_DELAY (300)
188 #define GC_SPREAD_GARBAGE_DROP_DELAY (40)
190 // time length of the introduction pause; should be multiple of 3
191 #define GC_START_PAUSE_DELAY (150)
193 // maximum height of a garbage block
194 #define GC_MAX_GARBAGE_HEIGHT (11)
196 // score constants
197 #define GC_MIN_PATTERN_SCORE (2)
198 #define GC_GRAY_SCORE (3)
199 #define GC_NUMBER_DIGITS (7)
200 #define GC_MIN_NUMBER_DIGITS_DISPLAYED (4)
201 #define GC_MAX_SCORE_INCREMENT_DELAY (12)
202 #define GC_MIN_SCORE_INCREMENT_DELAY (1)
203 #define GC_SCORE_DELAY_SLOPE (2)
204 #define GC_SCORE_REC_LENGTH (30)
205 #define GC_SCORE_DEFAULT_TOP_SCORE (600)
206 #define GC_SCORE_REC_DEFAULT_NAME "-----"
208 // sine constants
209 #define GC_SINE_TABLE_LENGTH (100)
210 #define GC_SINE_TABLE_LOWER_BOUND (-PI / 2.0f)
211 #define GC_SINE_TABLE_STEP_WIDTH (PI / (GC_SINE_TABLE_LENGTH - 1))
213 // initial swapper location
214 #define GC_INITIAL_SWAPPER_LOCATION_X (GC_PLAY_WIDTH / 2 - 1)
215 #define GC_INITIAL_SWAPPER_LOCATION_Y (4)
217 // extreme constants
218 #define GC_INVISIBLE_MAX_ALPHA (330)
219 #define GC_INVISIBLE_MIN_ALPHA (-20)
220 #define GC_INVISIBLE_QUICK_DECAY_RATE (3)
221 #define GC_INVISIBLE_PULSE_CHANCE_IN (30)
222 #define GC_INVISIBLE_PULSE_STRENGTH (70)
223 #define GC_CRAZY_LONG_MODE_PERIOD (150)
224 #define GC_CRAZY_SHORT_MODE_PERIOD (50)
225 #define GC_MAX_WILD_NUMBER (3)
226 #define GC_WILD_PERIOD (180)
227 #define GC_WILD_POLYMORPH_PERIOD (60)
228 #define GC_MAX_SPECIAL_COLOR_NUMBER (6)
230 // control keys
231 #define GC_LEFT_KEY ('a')
232 #define GC_RIGHT_KEY ('d')
233 #define GC_UP_KEY ('w')
234 #define GC_DOWN_KEY ('s')
235 #define GC_SWAP_KEY ('k')
236 #define GC_ADVANCE_KEY ('l')
237 #define GC_PAUSE_KEY ('p')
239 // other crap
240 #ifndef max
241 # define max(a, b) ((a) > (b) ? (a) : (b))
242 #endif
244 // insure that our communication int is the same length on all machines
245 #if (INT_MAX == 2147483647)
246 typedef unsigned int uint32;
247 #else
248 typedef unsigned short uint32;
249 #endif
251 // debug
252 #ifndef NDEBUG
253 # include <fstream>
254 # ifndef _WIN32
255 # define COLOR(n) "\33[1;" << (n) << "m"
256 # define NOCOLOR "\33[m"
257 # else
258 # define COLOR(n) ""
259 # define NOCOLOR ""
260 # endif
261 # define DOT(n) { cerr << COLOR(37 - n) \
262 << "." NOCOLOR << flush; }
263 # define DUMP(a) { cerr << COLOR(32) << "Dump: " \
264 __FILE__ ":" << __LINE__ << ": " \
265 NOCOLOR #a \
266 " = " << (a) << endl; }
267 # define PERIODIC_DUMP(a, b) { if (!(Game::time_step % a)) \
268 cerr << COLOR(32) << "Dump: " \
269 __FILE__ ":" << __LINE__ << ": " \
270 NOCOLOR #b \
271 " = " << (b) << endl; }
272 # define MESSAGE(a) { cerr << COLOR(33) << "Mesg: " \
273 __FILE__ ":" << __LINE__ << ": " \
274 NOCOLOR << a << endl; }
275 # ifndef _WIN32
276 # define MARK() { cerr << COLOR(35) << "Mark: " \
277 __FILE__ ":" << __LINE__ << ": " \
278 << __PRETTY_FUNCTION__ << NOCOLOR \
279 << endl; }
280 # else
281 # define MARK() { cerr << COLOR(35) << "Mark: " \
282 __FILE__ ":" << __LINE__ << ":" \
283 NOCOLOR << endl; }
284 # endif
285 # define ENDL() { cerr << COLOR(34) << "Endl: " \
286 __FILE__ ":" << __LINE__ << ":" \
287 NOCOLOR << endl; }
288 # define LOG(a) { ofstream log("log", ios::app); \
289 log << "Log: " __FILE__ ":" \
290 << __LINE__ << ": " << a \
291 << endl; }
292 #else
293 # define DOT(n) ((void) 0)
294 # define DUMP(a) ((void) 0)
295 # define PERIODIC_DUMP(a, b) ((void) 0)
296 # define MESSAGE(a) ((void) 0)
297 # define MARK() ((void) 0)
298 # define ENDL() ((void) 0)
299 # define LOG(a) ((void) 0)
300 #endif
302 // fix bad Visual C++ scoping
303 #ifdef _WIN32
304 # define for if (false) { } else for
305 #endif
307 // game states
309 // Playing.
310 #define GS_NORMAL (1 << 0)
311 // Normal playing, but we're paused.
312 #define GS_PAUSED (1 << 1)
313 // Signals a local unpausing to opponent; used only in communication signals.
314 #define GS_UNPAUSED (1 << 2)
315 // Normal playing, but we're paused because we've gotten ahead of our opponent;
316 // most likely due to a pause.
317 #define GS_SYNC_WAIT (1 << 3)
318 // We've lost, but we haven't heard confirmation from opponent; play continues.
319 #define GS_MAY_HAVE_LOST (1 << 4)
320 // Opponent lost; we've received the message and it predates our loss; play
321 // continues until level lights reach minimum.
322 #define GS_WON (1 << 5)
323 // We've lost and opponent confirms it; play continues until level lights reach
324 // minimum.
325 #define GS_LOST (1 << 6)
326 // We've won but we can't quite yet cause we must confirm our opponent's loss.
327 #define GS_MUST_CONFIRM_LOSS (1 << 7)
328 // We've confirmed our opponent's loss, but we have to wait a bit yet until
329 // he receives it.
330 #define GS_CONFIRMATION_HOLD (1 << 8)
331 // The level lights signaled it's time to end play. Now we must simply complete
332 // the current time step.
333 #define GS_END_PLAY (1 << 9)
334 // Used only for communication. The match has been conceded.
335 #define GS_CONCESSION (1 << 10)
337 // flavor of blocks; special color blocks must be last; wild and then gray must
338 // be directly after normal flavors; here to prevent header entanglements;
339 // the appearance chance of the various flavors can be found in
340 // BlockManager::newCreepBlock()
341 #define BF_NORMAL_1 ( 0)
342 #define BF_NORMAL_2 ( 1)
343 #define BF_NORMAL_3 ( 2)
344 #define BF_NORMAL_4 ( 3)
345 #define BF_NORMAL_5 ( 4)
346 #define BF_WILD ( 5)
347 #define BF_GRAY ( 6)
348 #define BF_BLACK ( 7)
349 #define BF_WHITE ( 8)
350 #define BF_SPECIAL_COLOR_1 ( 9)
351 #define BF_SPECIAL_COLOR_2 (10)
352 #define BF_SPECIAL_COLOR_3 (11)
353 #define BF_SPECIAL_COLOR_4 (12)
354 #define BF_SPECIAL_COLOR_5 (13)
355 #define BF_NUMBER_NORMAL (BF_NORMAL_5 + 1)
356 #define BF_NUMBER (BF_SPECIAL_COLOR_5 + 1)
357 #define BF_NUMBER_SPECIAL (BF_NUMBER - (BF_GRAY + 1))
358 #define BF_FINAL_GRAY_SPECIAL (BF_WHITE)
360 /* static */ class Game {
361 public:
362 static void initialize ( );
363 static void gameStart ( );
364 static void gameFinish ( );
365 static void cleanUp ( );
367 static void loss ( );
368 static void lossConfirmation ( );
369 static void won ( );
370 static void netSignalPause ( );
371 static void netSignalUnpause ( );
372 static void syncPause ( int delay );
373 static void concession ( );
375 static void idlePlay ( );
376 static void idleMeta ( );
378 static inline void go ( )
380 previous_time = glutGet((GLenum) GLUT_ELAPSED_TIME);
383 static inline float sqrt ( float x )
385 * An OK approximation of sqrt() on [0, 1] which is correct at the boundaries.
386 * Since all I really want is the vague shape of sqrt() and perfection at 0
387 * and 1, this'll work perfectly.
389 * Generated by minimizing int_0^1{dx (sqrt{x} - a x + (1 - a) x)^2}.
392 return ((27.0f / 14.0f) - (13.0f / 14.0f) * x) * x;
395 static int state;
396 static int time_step;
397 static int awaking_count;
398 static int dying_count;
399 static int dying_count_2;
401 private:
402 static void buttonPause ( );
403 static void syncUnpause ( );
405 static int previous_time;
406 static int remaining_time;
407 static bool button_down_pause;
408 static bool step_play;
409 static int sync_wait;
412 #endif