Development back on the trunk!
[crack-attack.git] / src / MetaState.cxx
blob133cc8766d76f7eb6fcac6745c2c36e43fc77d02
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 <GL/glut.h>
30 #include <cstring>
32 #include "glext.h"
34 using namespace std;
36 #include "Game.h"
37 #include "MetaState.h"
38 #include "Displayer.h"
39 #include "CelebrationManager.h"
40 #include "Controller.h"
41 #include "Communicator.h"
42 #include "WinRecord.h"
43 #include "MessageManager.h"
45 int MetaState::state;
46 int MetaState::mode = 0;
47 int MetaState::final_time_step;
48 char MetaState::player_name[GC_PLAYER_NAME_LENGTH];
50 void MetaState::programStart ( int _mode,
51 char _player_name[GC_PLAYER_NAME_LENGTH],
52 int width,
53 int height)
55 state = MS_BOTH_KEY_WAIT;
56 mode |= _mode;
57 strncpy(player_name, _player_name, GC_PLAYER_NAME_LENGTH);
59 Game::initialize();
60 Displayer::initialize(width, height);
62 MessageManager::mode = MM_NORMAL;
63 MessageManager::readyMessage(MS_ANYKEY);
65 glutKeyboardFunc(Controller::keyboardMeta);
66 glutSpecialFunc(Controller::specialMeta);
67 glutKeyboardUpFunc(null);
68 glutSpecialUpFunc(null);
69 glutEntryFunc(Controller::entry);
70 glutDisplayFunc(Displayer::displayMeta);
71 glutReshapeFunc(Displayer::reshape);
72 glutIdleFunc(Game::idleMeta);
75 if (!(mode & CM_SOLO))
76 Communicator::barrier();
79 atexit(programEnd);
81 Game::go();
84 void MetaState::programEnd ( )
86 Game::cleanUp();
87 Displayer::cleanUp();
88 Communicator::cleanUp();
91 void MetaState::gameStart ( )
93 state = MS_GAME_PLAY;
95 MessageManager::freeMessage();
97 if (!(mode & CM_SOLO))
98 Communicator::gameStart();
99 Game::gameStart();
100 Displayer::gameStart();
102 glutKeyboardFunc(Controller::keyboardPlay);
103 glutSpecialFunc(Controller::specialPlay);
104 glutKeyboardUpFunc(Controller::keyboardUpPlay);
105 glutSpecialUpFunc(Controller::specialUpPlay);
106 glutDisplayFunc(Displayer::displayPlay);
107 glutIdleFunc(Game::idlePlay);
110 if (!(mode & CM_SOLO))
111 Communicator::barrier();
114 Game::go();
117 void MetaState::gameWon ( )
119 DOT(3);
120 WinRecord::gameWon();
121 DOT(3);
122 gameFinish();
125 void MetaState::gameLoss ( )
127 WinRecord::gameLoss();
128 gameFinish();
131 void MetaState::gameFinish ( )
133 final_time_step = Game::time_step;
135 DOT(3);
136 if (state & MS_CONCESSION)
137 WinRecord::matchConceded();
139 DOT(3);
140 if (!(mode & CM_SOLO))
141 Communicator::gameFinish();
142 Game::gameFinish();
143 Displayer::gameFinish();
145 if (WinRecord::isMatchFinished()) {
146 if (!(mode & CM_SOLO))
147 Communicator::cleanUp();
148 state = MS_GAME_OVER_KEY_WAIT;
150 } else
151 state = MS_CELEBRATION_WAIT | MS_BOTH_KEY_WAIT;
153 glutKeyboardFunc(Controller::keyboardMeta);
154 glutSpecialFunc(Controller::specialMeta);
155 glutDisplayFunc(Displayer::displayMeta);
156 glutIdleFunc(Game::idleMeta);
158 // GLUT 3.7 is seg faulting if glutKeyboardUpFunc is unset and
159 // glutSpecialUpFunc is set. This is the only use of
160 // Controller::keyboardUpMeta() .
161 // glutKeyboardUpFunc(null); // removed
162 if (mode & CM_SOLO || mode & CM_AI) {
163 glutSpecialUpFunc(Controller::specialUpMeta);
164 glutKeyboardUpFunc(Controller::keyboardUpMeta); // workaround
165 } else {
166 glutSpecialUpFunc(null);
167 glutKeyboardUpFunc(null); // workaround
171 if (!(mode & CM_SOLO) && !WinRecord::isMatchFinished())
172 Communicator::barrier();
175 Game::go();
178 void MetaState::celebrationComplete ( )
180 // allow the player to end the celebration
181 state &= ~MS_CELEBRATION_WAIT;
184 void MetaState::localKeyPressed ( bool esc )
186 if (state & MS_CELEBRATION_WAIT) return;
188 switch (state) {
189 case MS_BOTH_KEY_WAIT:
190 if (WinRecord::current_game == -1)
191 MessageManager::freeMessage();
192 else
193 CelebrationManager::celebrationFinish();
194 MessageManager::readyMessage(MS_WAITING);
195 if (!(mode & CM_SOLO))
196 state = MS_REMOTE_KEY_WAIT;
197 else {
198 state = MS_READY_GAME_START;
199 remoteReady();
201 return;
203 case MS_LOCAL_KEY_WAIT:
204 if (WinRecord::current_game == -1)
205 MessageManager::freeMessage();
206 else
207 CelebrationManager::celebrationFinish();
208 MessageManager::readyMessage(MS_WAITING);
209 state = MS_READY_GAME_START;
210 return;
212 case MS_GAME_OVER_KEY_WAIT:
213 if (!esc) return;
214 case MS_GAME_OVER_ANY_KEY_WAIT:
215 MESSAGE("Exiting the game");
216 exit(0);
217 return;
221 void MetaState::remoteKeyPressed ( )
223 switch (state) {
224 case MS_BOTH_KEY_WAIT:
225 state = MS_LOCAL_KEY_WAIT;
226 return;
228 case MS_REMOTE_KEY_WAIT:
229 state = MS_READY_GAME_START;
230 return;
234 void MetaState::remoteReady ( )
236 switch (state) {
237 case MS_READY_GAME_START:
238 gameStart();
239 return;
241 case MS_REMOTE_KEY_WAIT:
242 state = MS_READY_GAME_START;
243 return;
245 case MS_BOTH_KEY_WAIT:
246 state = MS_LOCAL_KEY_WAIT;
247 return;