3 * Daniel Nelson - 8/25/0
5 * Copyright (C) 2000 Daniel Nelson
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * Daniel Nelson - aluminumangel.org
25 * The core loop plus extras.
26 * Remove dying_count_2.
40 #include "BlockManager.h"
41 #include "CelebrationManager.h"
43 #include "Controller.h"
44 #include "ActionRecorder.h"
45 #include "Communicator.h"
46 #include "ComboManager.h"
47 #include "CountDownManager.h"
49 #include "GarbageGenerator.h"
50 #include "GarbageManager.h"
52 #include "LevelLights.h"
54 #include "MessageManager.h"
55 #include "MetaState.h"
58 #include "ScoreRecordManager.h"
59 #include "SignManager.h"
61 #include "SparkleManager.h"
64 #include "WinRecord.h"
66 #include "ComputerPlayer.h"
73 int Game::awaking_count
;
74 int Game::dying_count
;
75 int Game::dying_count_2
;
76 int Game::previous_time
;
77 int Game::remaining_time
;
78 bool Game::button_down_pause
;
81 double Game::lastframe
= 0.0;
83 void Game::initialize ( )
86 WinRecord::initialize();
87 LevelLights::initialize();
90 LoseBar::initialize();
91 ActionRecorder::initialize();
96 void Game::gameStart ( )
103 WinRecord::gameStart();
105 BlockManager::gameStart();
106 GarbageManager::gameStart();
107 ComboManager::gameStart();
108 GarbageGenerator::gameStart();
111 Controller::gameStart();
113 Swapper::gameStart();
115 LevelLights::gameStart();
116 CountDownManager::gameStart();
118 LoseBar::gameStart();
119 ComputerPlayer::gameStart();
121 button_down_pause
= false;
129 void Game::gameFinish ( )
135 void Game::cleanUp ( )
137 CountDownManager::cleanUp();
143 * Called when creep detects a game loss condition; opponent may have already
144 * lost, so me must wait for confirmation.
147 if (!(MetaState::mode
& CM_SOLO
)) {
148 if (!(state
& (GS_MAY_HAVE_LOST
| GS_WON
))) {
149 state
= GS_MAY_HAVE_LOST
;
150 Communicator::setLossTimeStep();
153 state
|= GS_END_PLAY
;
156 void Game::lossConfirmation ( )
158 * Called by Communicator when opponent confirms game loss. Now we must only
159 * wait for the level lights to end play.
162 state
= GS_LOST
| GS_END_PLAY
;
165 void Game::aiPlayerLoss ( )
167 if (!(state
& GS_WON
) && !(state
& GS_END_PLAY
))
168 state
= GS_WON
| GS_END_PLAY
;
173 * Called by Communicator when opponent signals a game loss that predates
174 * ours or signals a reverse of our game loss. Now we must wait to confirm
175 * our opponent's loss and for the level lights to end play.
178 if (!(state
& GS_WON
))
179 state
= GS_WON
| GS_MUST_CONFIRM_LOSS
;
182 void Game::concession ( )
184 * Called by the controller upon player concession.
187 if (CountDownManager::start_pause_alarm
!= 0) return;
190 MetaState::state
|= MS_CONCESSION
;
193 void Game::buttonPause ( )
194 // run time step during pause (for Communicator); then reset the clock to the
195 // value at the pause time
198 if ((state
& (GS_PAUSED
| GS_SYNC_WAIT
)) == GS_PAUSED
) {
201 MessageManager::freeMessage();
202 if (CountDownManager::state
!= -1)
203 MessageManager::readyMessage(CountDownManager::state
);
205 if (!(MetaState::mode
& CM_SOLO
)) {
206 Communicator::unpauseSyncCheck();
207 Communicator::signalUnpaused();
215 // to simplify things, you can't pause if you're about to lose
216 if (!(state
& GS_NORMAL
)) return;
220 if (CountDownManager::state
!= -1)
221 MessageManager::freeMessage();
222 MessageManager::readyMessage(MS_PAUSED
);
224 if (!(MetaState::mode
& CM_SOLO
))
225 Communicator::signalPaused();
232 void Game::netSignalPause ( )
234 if (!(state
& GS_NORMAL
)) return;
238 if (CountDownManager::state
!= -1)
239 MessageManager::freeMessage();
240 MessageManager::readyMessage(MS_PAUSED
);
243 void Game::netSignalUnpause ( )
245 if ((state
& (GS_PAUSED
| GS_SYNC_WAIT
)) != GS_PAUSED
) return;
249 MessageManager::freeMessage();
250 if (CountDownManager::state
!= -1)
251 MessageManager::readyMessage(CountDownManager::state
);
254 void Game::syncPause ( int delay
)
258 state
= GS_PAUSED
| GS_SYNC_WAIT
;
261 if (CountDownManager::state
!= -1)
262 MessageManager::freeMessage();
263 MessageManager::readyMessage(MS_WAITING
);
266 void Game::syncUnpause ( )
270 MessageManager::freeMessage();
271 if (CountDownManager::state
!= -1)
272 MessageManager::readyMessage(CountDownManager::state
);
275 void Game::idleMeta ( )
280 gettimeofday(&now
,NULL
);
281 #define FPSDIFF (1.0f/30.0f)
283 nowd
+= (double)now
.tv_usec
/ 1000000.0f
;
285 if ( (nowd
- lastframe
) > FPSDIFF
) {
292 int modified_and_complete
= false;
296 int time
= glutGet((GLenum
) GLUT_ELAPSED_TIME
);
297 remaining_time
+= time
- previous_time
;
298 previous_time
= time
;
301 if (remaining_time
< GC_TIME_STEP_PERIOD
) break;
303 remaining_time
-= GC_TIME_STEP_PERIOD
;
306 if (remaining_time
< GC_TIME_STEP_PERIOD
)
307 modified_and_complete
= true;
311 // communicate state and other stuff
312 if (!(MetaState::state
& MS_GAME_OVER_KEY_WAIT
) &&
313 !(MetaState::mode
& CM_SOLO
))
314 Communicator::timeStepMeta();
316 // advance celebration
317 CelebrationManager::timeStep();
319 // advance the score record
320 ScoreRecordManager::timeStep();
322 // update message pulse
323 MessageManager::timeStep();
325 // update the pretty stuff too
326 SparkleManager::timeStep();
327 SignManager::timeStep();
328 if (!(MetaState::mode
& CM_REALLY_LOW_GRAPHICS
))
329 WinRecord::timeStep();
330 LevelLights::timeStep();
332 // advance the clock through its final tick fade
333 Clock::timeStepMeta();
335 // advance the score through its final increment fade
336 Score::timeStepMeta();
340 if (modified_and_complete
) {
346 void Game::idlePlay ( )
351 gettimeofday(&now
,NULL
);
352 #define FPSDIFF (1.0f/30.0f)
354 nowd
+= (double)now
.tv_usec
/ 1000000.0f
;
356 if ( (nowd
- lastframe
) > FPSDIFF
) {
363 int modified_and_complete
= false;
367 int time
= glutGet((GLenum
) GLUT_ELAPSED_TIME
);
368 remaining_time
+= time
- previous_time
;
369 previous_time
= time
;
372 if (remaining_time
< GC_TIME_STEP_PERIOD
) break;
374 remaining_time
-= GC_TIME_STEP_PERIOD
;
376 if (remaining_time
< GC_TIME_STEP_PERIOD
)
377 modified_and_complete
= true;
381 if (!(state
& GS_PAUSED
)) {
386 // check for and continue swaps and moves
389 // update the level lights
390 LevelLights::timeStep();
392 // update message pulse
393 MessageManager::timeStep();
395 // move the win record stars
396 WinRecord::timeStep();
398 // update the starting count down
399 CountDownManager::timeStep();
400 if (CountDownManager::start_pause_alarm
!= 0) {
401 // some objects require a time step during start pause
403 if (!(MetaState::mode
& CM_SOLO
))
404 Communicator::timeStepPlay();
407 Clock::timeStepMeta();
412 if (MetaState::mode
& CM_AI
)
413 ComputerPlayer::timeStep();
415 // loop over the grid, bottom to top; garbage will advance x and y
416 for (int y
= 1; y
< GC_PLAY_HEIGHT
; y
++)
417 for (int x
= 0; x
< GC_PLAY_WIDTH
; x
++) {
418 if (Grid::residentTypeAt(x
, y
) & GR_EMPTY
) continue;
420 if (Grid::residentTypeAt(x
, y
) & GR_BLOCK
)
421 Grid::blockAt(x
, y
).timeStep();
423 Grid::garbageAt(x
, y
).timeStep(x
, y
);
426 // perhaps creep upward
429 // process elimination check requests and update top_occupied_row
432 // handle new and finished combos
433 ComboManager::timeStep();
436 // communicate garbage and other stuff
437 if (!(MetaState::mode
& CM_SOLO
))
438 Communicator::timeStepPlay();
442 // drop queued garbage
443 GarbageGenerator::timeStep();
445 // move the sparkles and signs
446 SparkleManager::timeStep();
447 SignManager::timeStep();
449 // update the impact spring
450 if (!(MetaState::mode
& CM_REALLY_LOW_GRAPHICS
))
454 Clock::timeStepPlay();
457 Score::timeStepPlay();
459 // update the losebar
462 // advance extreme effects
463 if (MetaState::mode
& CM_X
)
469 // if this is a sync pause, count down the waiting period
470 if (state
& GS_SYNC_WAIT
&& !--sync_wait
)
473 // communicate when paused
474 if (!(MetaState::mode
& CM_SOLO
))
475 Communicator::timeStepPlay();
477 // update message pulse
478 MessageManager::timeStep();
481 if (state
& (GS_MAY_HAVE_LOST
| GS_MUST_CONFIRM_LOSS
))
484 if (state
& GS_END_PLAY
) {
485 // step_play = false;
486 if (state
& GS_PAUSED
)
487 MessageManager::freeMessage();
489 if ((MetaState::mode
& CM_SOLO
) && !(MetaState::mode
& CM_AI
))
490 state
= Score::gameFinish();
492 if (MetaState::mode
& CM_AI
)
493 state
= ComputerPlayer::gameFinish();
495 ActionRecorder::gameFinish();
498 MetaState::gameLoss();
500 MetaState::gameWon();
507 if (!button_down_pause
) {
508 if (Controller::pauseCommand()) {
509 button_down_pause
= true;
513 if (!Controller::pauseCommand())
514 button_down_pause
= false;
516 if (modified_and_complete
) {