Each module now includes its own header first. Removed sstream.h and the definition...
[crack-attack.git] / src / MetaState.cxx
blobd96b40311f41372c7bc5c01cba3b84bfe87c2030
1 /*
2 * MetaState.cxx
3 * Daniel Nelson - 10/22/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
26 * Handles the program's state and transfers between them.
29 #include "MetaState.h"
31 #include <GL/glut.h>
32 #include <cstring>
34 #include "glext.h"
36 #include "Game.h"
37 #include "Displayer.h"
38 #include "CelebrationManager.h"
39 #include "Controller.h"
40 #include "Communicator.h"
41 #include "WinRecord.h"
42 #include "MessageManager.h"
44 int MetaState::state;
45 int MetaState::mode = 0;
46 int MetaState::final_time_step;
47 char MetaState::player_name[GC_PLAYER_NAME_LENGTH];
49 void MetaState::programStart ( int _mode,
50 char _player_name[GC_PLAYER_NAME_LENGTH],
51 int width,
52 int height)
54 state = MS_BOTH_KEY_WAIT;
55 mode |= _mode;
56 strncpy(player_name, _player_name, GC_PLAYER_NAME_LENGTH);
58 Game::initialize();
59 Displayer::initialize(width, height);
61 MessageManager::mode = MM_NORMAL;
62 MessageManager::readyMessage(MS_ANYKEY);
64 glutKeyboardFunc(Controller::keyboardMeta);
65 glutSpecialFunc(Controller::specialMeta);
66 glutKeyboardUpFunc(null);
67 glutSpecialUpFunc(null);
68 glutEntryFunc(Controller::entry);
69 glutDisplayFunc(Displayer::displayMeta);
70 glutReshapeFunc(Displayer::reshape);
71 glutIdleFunc(Game::idleMeta);
74 if (!(mode & CM_SOLO))
75 Communicator::barrier();
78 atexit(programEnd);
80 Game::go();
83 void MetaState::programEnd ( )
85 Game::cleanUp();
86 Displayer::cleanUp();
87 Communicator::cleanUp();
90 void MetaState::gameStart ( )
92 state = MS_GAME_PLAY;
94 MessageManager::freeMessage();
96 if (!(mode & CM_SOLO))
97 Communicator::gameStart();
98 Game::gameStart();
99 Displayer::gameStart();
101 glutKeyboardFunc(Controller::keyboardPlay);
102 glutSpecialFunc(Controller::specialPlay);
103 glutKeyboardUpFunc(Controller::keyboardUpPlay);
104 glutSpecialUpFunc(Controller::specialUpPlay);
105 glutDisplayFunc(Displayer::displayPlay);
106 glutIdleFunc(Game::idlePlay);
109 if (!(mode & CM_SOLO))
110 Communicator::barrier();
113 Game::go();
116 void MetaState::gameWon ( )
118 DOT(3);
119 WinRecord::gameWon();
120 DOT(3);
121 gameFinish();
124 void MetaState::gameLoss ( )
126 WinRecord::gameLoss();
127 gameFinish();
130 void MetaState::gameFinish ( )
132 final_time_step = Game::time_step;
134 DOT(3);
135 if (state & MS_CONCESSION)
136 WinRecord::matchConceded();
138 DOT(3);
139 if (!(mode & CM_SOLO))
140 Communicator::gameFinish();
141 Game::gameFinish();
142 Displayer::gameFinish();
144 if (WinRecord::isMatchFinished()) {
145 if (!(mode & CM_SOLO))
146 Communicator::cleanUp();
147 state = MS_GAME_OVER_KEY_WAIT;
149 } else
150 state = MS_CELEBRATION_WAIT | MS_BOTH_KEY_WAIT;
152 glutKeyboardFunc(Controller::keyboardMeta);
153 glutSpecialFunc(Controller::specialMeta);
154 glutDisplayFunc(Displayer::displayMeta);
155 glutIdleFunc(Game::idleMeta);
157 // GLUT 3.7 is seg faulting if glutKeyboardUpFunc is unset and
158 // glutSpecialUpFunc is set. This is the only use of
159 // Controller::keyboardUpMeta() .
160 // glutKeyboardUpFunc(null); // removed
161 if (mode & CM_SOLO || mode & CM_AI) {
162 glutSpecialUpFunc(Controller::specialUpMeta);
163 glutKeyboardUpFunc(Controller::keyboardUpMeta); // workaround
164 } else {
165 glutSpecialUpFunc(null);
166 glutKeyboardUpFunc(null); // workaround
170 if (!(mode & CM_SOLO) && !WinRecord::isMatchFinished())
171 Communicator::barrier();
174 Game::go();
177 void MetaState::celebrationComplete ( )
179 // allow the player to end the celebration
180 state &= ~MS_CELEBRATION_WAIT;
183 void MetaState::localKeyPressed ( bool esc )
185 if (state & MS_CELEBRATION_WAIT) return;
187 switch (state) {
188 case MS_BOTH_KEY_WAIT:
189 if (WinRecord::current_game == -1)
190 MessageManager::freeMessage();
191 else
192 CelebrationManager::celebrationFinish();
193 MessageManager::readyMessage(MS_WAITING);
194 if (!(mode & CM_SOLO))
195 state = MS_REMOTE_KEY_WAIT;
196 else {
197 state = MS_READY_GAME_START;
198 remoteReady();
200 return;
202 case MS_LOCAL_KEY_WAIT:
203 if (WinRecord::current_game == -1)
204 MessageManager::freeMessage();
205 else
206 CelebrationManager::celebrationFinish();
207 MessageManager::readyMessage(MS_WAITING);
208 state = MS_READY_GAME_START;
209 return;
211 case MS_GAME_OVER_KEY_WAIT:
212 if (!esc) return;
213 case MS_GAME_OVER_ANY_KEY_WAIT:
214 MESSAGE("Exiting the game");
215 exit(0);
216 return;
220 void MetaState::remoteKeyPressed ( )
222 switch (state) {
223 case MS_BOTH_KEY_WAIT:
224 state = MS_LOCAL_KEY_WAIT;
225 return;
227 case MS_REMOTE_KEY_WAIT:
228 state = MS_READY_GAME_START;
229 return;
233 void MetaState::remoteReady ( )
235 switch (state) {
236 case MS_READY_GAME_START:
237 gameStart();
238 return;
240 case MS_REMOTE_KEY_WAIT:
241 state = MS_READY_GAME_START;
242 return;
244 case MS_BOTH_KEY_WAIT:
245 state = MS_LOCAL_KEY_WAIT;
246 return;