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.
40 #include "MetaState.h"
41 #include "Displayer.h"
42 #include "CelebrationManager.h"
43 #include "Controller.h"
44 #include "Communicator.h"
45 #include "WinRecord.h"
46 #include "MessageManager.h"
49 int MetaState::mode
= 0;
50 int MetaState::final_time_step
;
51 char MetaState::player_name
[GC_PLAYER_NAME_LENGTH
];
53 void MetaState::programStart ( int _mode
,
54 char _player_name
[GC_PLAYER_NAME_LENGTH
],
58 state
= MS_BOTH_KEY_WAIT
;
60 strncpy(player_name
, _player_name
, GC_PLAYER_NAME_LENGTH
);
63 Displayer::initialize(width
, height
);
65 MessageManager::mode
= MM_NORMAL
;
66 MessageManager::readyMessage(MS_ANYKEY
);
68 glutKeyboardFunc(Controller::keyboardMeta
);
69 glutSpecialFunc(Controller::specialMeta
);
70 glutKeyboardUpFunc(null
);
71 glutSpecialUpFunc(null
);
72 glutEntryFunc(Controller::entry
);
73 glutDisplayFunc(Displayer::displayMeta
);
74 glutReshapeFunc(Displayer::reshape
);
75 glutIdleFunc(Game::idleMeta
);
77 if (!(mode
& CM_SOLO
))
78 Communicator::barrier();
85 void MetaState::programEnd ( )
89 Communicator::cleanUp();
92 void MetaState::gameStart ( )
96 MessageManager::freeMessage();
98 if (!(mode
& CM_SOLO
))
99 Communicator::gameStart();
101 Displayer::gameStart();
103 glutKeyboardFunc(Controller::keyboardPlay
);
104 glutSpecialFunc(Controller::specialPlay
);
105 glutKeyboardUpFunc(Controller::keyboardUpPlay
);
106 glutSpecialUpFunc(Controller::specialUpPlay
);
107 glutDisplayFunc(Displayer::displayPlay
);
108 glutIdleFunc(Game::idlePlay
);
110 if (!(mode
& CM_SOLO
))
111 Communicator::barrier();
116 void MetaState::gameWon ( )
119 WinRecord::gameWon();
124 void MetaState::gameLoss ( )
126 WinRecord::gameLoss();
130 void MetaState::gameFinish ( )
132 final_time_step
= Game::time_step
;
135 if (state
& MS_CONCESSION
)
136 WinRecord::matchConceded();
139 if (!(mode
& CM_SOLO
))
140 Communicator::gameFinish();
142 Displayer::gameFinish();
144 if (WinRecord::isMatchFinished()) {
145 if (!(mode
& CM_SOLO
))
146 Communicator::cleanUp();
147 state
= MS_GAME_OVER_KEY_WAIT
;
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
165 glutSpecialUpFunc(null
);
166 glutKeyboardUpFunc(null
); // workaround
169 if (!(mode
& CM_SOLO
) && !WinRecord::isMatchFinished())
170 Communicator::barrier();
175 void MetaState::celebrationComplete ( )
177 // allow the player to end the celebration
178 state
&= ~MS_CELEBRATION_WAIT
;
181 void MetaState::localKeyPressed ( bool esc
)
183 if (state
& MS_CELEBRATION_WAIT
) return;
186 case MS_BOTH_KEY_WAIT
:
187 if (WinRecord::current_game
== -1)
188 MessageManager::freeMessage();
190 CelebrationManager::celebrationFinish();
191 MessageManager::readyMessage(MS_WAITING
);
192 if (!(mode
& CM_SOLO
))
193 state
= MS_REMOTE_KEY_WAIT
;
195 state
= MS_READY_GAME_START
;
200 case MS_LOCAL_KEY_WAIT
:
201 if (WinRecord::current_game
== -1)
202 MessageManager::freeMessage();
204 CelebrationManager::celebrationFinish();
205 MessageManager::readyMessage(MS_WAITING
);
206 state
= MS_READY_GAME_START
;
209 case MS_GAME_OVER_KEY_WAIT
:
216 void MetaState::remoteKeyPressed ( )
219 case MS_BOTH_KEY_WAIT
:
220 state
= MS_LOCAL_KEY_WAIT
;
223 case MS_REMOTE_KEY_WAIT
:
224 state
= MS_READY_GAME_START
;
229 void MetaState::remoteReady ( )
232 case MS_READY_GAME_START
:
236 case MS_REMOTE_KEY_WAIT
:
237 state
= MS_READY_GAME_START
;
240 case MS_BOTH_KEY_WAIT
:
241 state
= MS_LOCAL_KEY_WAIT
;