3 * Daniel Nelson - 10/22/0
5 * Copyright (C) 2000 Daniel Nelson
6 * Copyright (C) 2004 Andrew Sayman
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
26 * Handles the program's state and transfers between them.
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"
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
],
55 state
= MS_BOTH_KEY_WAIT
;
57 strncpy(player_name
, _player_name
, GC_PLAYER_NAME_LENGTH
);
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();
84 void MetaState::programEnd ( )
88 Communicator::cleanUp();
91 void MetaState::gameStart ( )
95 MessageManager::freeMessage();
97 if (!(mode
& CM_SOLO
))
98 Communicator::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();
117 void MetaState::gameWon ( )
120 WinRecord::gameWon();
125 void MetaState::gameLoss ( )
127 WinRecord::gameLoss();
131 void MetaState::gameFinish ( )
133 final_time_step
= Game::time_step
;
136 if (state
& MS_CONCESSION
)
137 WinRecord::matchConceded();
140 if (!(mode
& CM_SOLO
))
141 Communicator::gameFinish();
143 Displayer::gameFinish();
145 if (WinRecord::isMatchFinished()) {
146 if (!(mode
& CM_SOLO
))
147 Communicator::cleanUp();
148 state
= MS_GAME_OVER_KEY_WAIT
;
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
166 glutSpecialUpFunc(null
);
167 glutKeyboardUpFunc(null
); // workaround
171 if (!(mode & CM_SOLO) && !WinRecord::isMatchFinished())
172 Communicator::barrier();
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;
189 case MS_BOTH_KEY_WAIT
:
190 if (WinRecord::current_game
== -1)
191 MessageManager::freeMessage();
193 CelebrationManager::celebrationFinish();
194 MessageManager::readyMessage(MS_WAITING
);
195 if (!(mode
& CM_SOLO
))
196 state
= MS_REMOTE_KEY_WAIT
;
198 state
= MS_READY_GAME_START
;
203 case MS_LOCAL_KEY_WAIT
:
204 if (WinRecord::current_game
== -1)
205 MessageManager::freeMessage();
207 CelebrationManager::celebrationFinish();
208 MessageManager::readyMessage(MS_WAITING
);
209 state
= MS_READY_GAME_START
;
212 case MS_GAME_OVER_KEY_WAIT
:
219 void MetaState::remoteKeyPressed ( )
222 case MS_BOTH_KEY_WAIT
:
223 state
= MS_LOCAL_KEY_WAIT
;
226 case MS_REMOTE_KEY_WAIT
:
227 state
= MS_READY_GAME_START
;
232 void MetaState::remoteReady ( )
235 case MS_READY_GAME_START
:
239 case MS_REMOTE_KEY_WAIT
:
240 state
= MS_READY_GAME_START
;
243 case MS_BOTH_KEY_WAIT
:
244 state
= MS_LOCAL_KEY_WAIT
;