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
);
74 if (!(mode
& CM_SOLO
))
75 Communicator::barrier();
82 void MetaState::programEnd ( )
86 Communicator::cleanUp();
89 void MetaState::gameStart ( )
93 MessageManager::freeMessage();
95 if (!(mode
& CM_SOLO
))
96 Communicator::gameStart();
98 Displayer::gameStart();
100 glutKeyboardFunc(Controller::keyboardPlay
);
101 glutSpecialFunc(Controller::specialPlay
);
102 glutKeyboardUpFunc(Controller::keyboardUpPlay
);
103 glutSpecialUpFunc(Controller::specialUpPlay
);
104 glutDisplayFunc(Displayer::displayPlay
);
105 glutIdleFunc(Game::idlePlay
);
107 if (!(mode
& CM_SOLO
))
108 Communicator::barrier();
113 void MetaState::gameWon ( )
116 WinRecord::gameWon();
121 void MetaState::gameLoss ( )
123 WinRecord::gameLoss();
127 void MetaState::gameFinish ( )
129 final_time_step
= Game::time_step
;
132 if (state
& MS_CONCESSION
)
133 WinRecord::matchConceded();
136 if (!(mode
& CM_SOLO
))
137 Communicator::gameFinish();
139 Displayer::gameFinish();
141 if (WinRecord::isMatchFinished()) {
142 if (!(mode
& CM_SOLO
))
143 Communicator::cleanUp();
144 state
= MS_GAME_OVER_KEY_WAIT
;
147 state
= MS_CELEBRATION_WAIT
| MS_BOTH_KEY_WAIT
;
149 glutKeyboardFunc(Controller::keyboardMeta
);
150 glutSpecialFunc(Controller::specialMeta
);
151 glutDisplayFunc(Displayer::displayMeta
);
152 glutIdleFunc(Game::idleMeta
);
154 // GLUT 3.7 is seg faulting if glutKeyboardUpFunc is unset and
155 // glutSpecialUpFunc is set. This is the only use of
156 // Controller::keyboardUpMeta() .
157 // glutKeyboardUpFunc(null); // removed
158 if (mode
& CM_SOLO
|| mode
& CM_AI
) {
159 glutSpecialUpFunc(Controller::specialUpMeta
);
160 glutKeyboardUpFunc(Controller::keyboardUpMeta
); // workaround
162 glutSpecialUpFunc(null
);
163 glutKeyboardUpFunc(null
); // workaround
166 if (!(mode
& CM_SOLO
) && !WinRecord::isMatchFinished())
167 Communicator::barrier();
172 void MetaState::celebrationComplete ( )
174 // allow the player to end the celebration
175 state
&= ~MS_CELEBRATION_WAIT
;
178 void MetaState::localKeyPressed ( bool esc
)
180 if (state
& MS_CELEBRATION_WAIT
) return;
183 case MS_BOTH_KEY_WAIT
:
184 if (WinRecord::current_game
== -1)
185 MessageManager::freeMessage();
187 CelebrationManager::celebrationFinish();
188 MessageManager::readyMessage(MS_WAITING
);
189 if (!(mode
& CM_SOLO
))
190 state
= MS_REMOTE_KEY_WAIT
;
192 state
= MS_READY_GAME_START
;
197 case MS_LOCAL_KEY_WAIT
:
198 if (WinRecord::current_game
== -1)
199 MessageManager::freeMessage();
201 CelebrationManager::celebrationFinish();
202 MessageManager::readyMessage(MS_WAITING
);
203 state
= MS_READY_GAME_START
;
206 case MS_GAME_OVER_KEY_WAIT
:
213 void MetaState::remoteKeyPressed ( )
216 case MS_BOTH_KEY_WAIT
:
217 state
= MS_LOCAL_KEY_WAIT
;
220 case MS_REMOTE_KEY_WAIT
:
221 state
= MS_READY_GAME_START
;
226 void MetaState::remoteReady ( )
229 case MS_READY_GAME_START
:
233 case MS_REMOTE_KEY_WAIT
:
234 state
= MS_READY_GAME_START
;
237 case MS_BOTH_KEY_WAIT
:
238 state
= MS_LOCAL_KEY_WAIT
;