1 | #include "../../common/Compiler.h"
|
---|
2 |
|
---|
3 | #if defined WINDOWS
|
---|
4 | #include <winsock2.h>
|
---|
5 | #include <ws2tcpip.h>
|
---|
6 | #elif defined LINUX
|
---|
7 | #include <sys/types.h>
|
---|
8 | #include <unistd.h>
|
---|
9 | #include <sys/socket.h>
|
---|
10 | #include <netinet/in.h>
|
---|
11 | #include <netdb.h>
|
---|
12 | #include <cstring>
|
---|
13 | #elif defined MAC
|
---|
14 | #include <netdb.h>
|
---|
15 | #endif
|
---|
16 |
|
---|
17 | #include <cstdio>
|
---|
18 | #include <cstdlib>
|
---|
19 | //#include <cmath>
|
---|
20 | #include <sys/types.h>
|
---|
21 | #include <string>
|
---|
22 | #include <iostream>
|
---|
23 | #include <sstream>
|
---|
24 | #include <fstream>
|
---|
25 | #include <map>
|
---|
26 | #include <vector>
|
---|
27 | #include <stdexcept>
|
---|
28 |
|
---|
29 | #include <allegro5/allegro.h>
|
---|
30 | #include <allegro5/allegro_font.h>
|
---|
31 | #include <allegro5/allegro_ttf.h>
|
---|
32 | #include <allegro5/allegro_primitives.h>
|
---|
33 |
|
---|
34 | #include "../../common/Common.h"
|
---|
35 | #include "../../common/MessageContainer.h"
|
---|
36 | #include "../../common/MessageProcessor.h"
|
---|
37 | #include "../../common/WorldMap.h"
|
---|
38 | #include "../../common/Player.h"
|
---|
39 | #include "../../common/Projectile.h"
|
---|
40 | #include "../../common/Game.h"
|
---|
41 | #include "../../common/GameSummary.h"
|
---|
42 |
|
---|
43 | #include "Window.h"
|
---|
44 | #include "TextLabel.h"
|
---|
45 | #include "Button.h"
|
---|
46 | #include "Textbox.h"
|
---|
47 | #include "RadioButtonList.h"
|
---|
48 |
|
---|
49 | #include "GameRender.h"
|
---|
50 |
|
---|
51 | #include "chat.h"
|
---|
52 |
|
---|
53 | #ifdef WINDOWS
|
---|
54 | #pragma comment(lib, "ws2_32.lib")
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | using namespace std;
|
---|
58 |
|
---|
59 | void initWinSock();
|
---|
60 | void shutdownWinSock();
|
---|
61 | void createGui(ALLEGRO_FONT* font);
|
---|
62 |
|
---|
63 | void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames, unsigned int& curPlayerId);
|
---|
64 | void handleMsgPlayer(NETWORK_MSG &msg, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames);
|
---|
65 | void handleMsgGameInfo(NETWORK_MSG &msg, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames);
|
---|
66 |
|
---|
67 | int getRefreshRate(int width, int height);
|
---|
68 | void drawMessageStatus(ALLEGRO_FONT* font);
|
---|
69 |
|
---|
70 | // Callback declarations
|
---|
71 | void goToLoginScreen();
|
---|
72 | void goToRegisterScreen();
|
---|
73 | void registerAccount();
|
---|
74 | void login();
|
---|
75 | void logout();
|
---|
76 | void quit();
|
---|
77 | void sendChatMessage();
|
---|
78 | void toggleDebugging();
|
---|
79 | void goToProfileScreen();
|
---|
80 | void goToLobbyScreen();
|
---|
81 | void joinGame(); // for joining the game lobby
|
---|
82 | void createGame(); // for joining the game lobby
|
---|
83 | void joinWaitingArea();
|
---|
84 | void joinRedTeam();
|
---|
85 | void joinBlueTeam();
|
---|
86 | void startGame(); // for leaving game lobby and starting the actual game
|
---|
87 | void leaveGame();
|
---|
88 | void closeGameSummary();
|
---|
89 |
|
---|
90 | const float FPS = 60;
|
---|
91 | const int SCREEN_W = 1024;
|
---|
92 | const int SCREEN_H = 768;
|
---|
93 |
|
---|
94 | enum STATE {
|
---|
95 | STATE_START,
|
---|
96 | STATE_LOBBY,
|
---|
97 | STATE_GAME_LOBBY,
|
---|
98 | STATE_GAME
|
---|
99 | };
|
---|
100 |
|
---|
101 | int state;
|
---|
102 |
|
---|
103 | bool doexit;
|
---|
104 |
|
---|
105 | vector<GuiComponent*> vctComponents;
|
---|
106 |
|
---|
107 | Window* wndLogin;
|
---|
108 | Window* wndRegister;
|
---|
109 | Window* wndLobby;
|
---|
110 | Window* wndLobbyDebug;
|
---|
111 | Window* wndProfile;
|
---|
112 | Window* wndGameLobby;
|
---|
113 | Window* wndGame;
|
---|
114 | Window* wndGameSummary;
|
---|
115 | Window* wndCurrent;
|
---|
116 |
|
---|
117 | // wndLogin
|
---|
118 | Textbox* txtUsername;
|
---|
119 | Textbox* txtPassword;
|
---|
120 | TextLabel* lblLoginStatus;
|
---|
121 |
|
---|
122 | // wndRegister
|
---|
123 | Textbox* txtUsernameRegister;
|
---|
124 | Textbox* txtPasswordRegister;
|
---|
125 | RadioButtonList* rblClasses;
|
---|
126 | TextLabel* lblRegisterStatus;
|
---|
127 |
|
---|
128 | // wndLobby
|
---|
129 | Textbox* txtJoinGame;
|
---|
130 | Textbox* txtCreateGame;
|
---|
131 | Textbox* txtChat;
|
---|
132 |
|
---|
133 | int sock;
|
---|
134 | struct sockaddr_in server, from;
|
---|
135 | struct hostent *hp;
|
---|
136 | NETWORK_MSG msgTo, msgFrom;
|
---|
137 | string username;
|
---|
138 | chat chatConsole, debugConsole;
|
---|
139 | bool debugging;
|
---|
140 | Game* game;
|
---|
141 | GameSummary* gameSummary;
|
---|
142 | Player* currentPlayer;
|
---|
143 |
|
---|
144 | int honorPoints, wins, losses, numGames;
|
---|
145 | int** gameHistory;
|
---|
146 |
|
---|
147 | MessageProcessor msgProcessor;
|
---|
148 |
|
---|
149 | int main(int argc, char **argv)
|
---|
150 | {
|
---|
151 | ALLEGRO_DISPLAY *display = NULL;
|
---|
152 | ALLEGRO_EVENT_QUEUE *event_queue = NULL;
|
---|
153 | ALLEGRO_TIMER *timer = NULL;
|
---|
154 | map<unsigned int, Player*> mapPlayers;
|
---|
155 | map<string, int> mapGames;
|
---|
156 | unsigned int curPlayerId = -1;
|
---|
157 | ofstream outputLog;
|
---|
158 |
|
---|
159 | doexit = false;
|
---|
160 | debugging = false;
|
---|
161 | bool redraw = true;
|
---|
162 | bool fullscreen = false;
|
---|
163 | game = NULL;
|
---|
164 | gameSummary = NULL;
|
---|
165 |
|
---|
166 | honorPoints = 0;
|
---|
167 | wins = 0;
|
---|
168 | losses = 0;
|
---|
169 | numGames = 0;
|
---|
170 | gameHistory = NULL;
|
---|
171 |
|
---|
172 | state = STATE_START;
|
---|
173 |
|
---|
174 | if(!al_init()) {
|
---|
175 | fprintf(stderr, "failed to initialize allegro!\n");
|
---|
176 | return -1;
|
---|
177 | }
|
---|
178 |
|
---|
179 | outputLog.open("client.log", ios::app);
|
---|
180 | outputLog << "Started client on " << getCurrentDateTimeString() << endl;
|
---|
181 |
|
---|
182 | if (al_init_primitives_addon())
|
---|
183 | cout << "Primitives initialized" << endl;
|
---|
184 | else
|
---|
185 | cout << "Primitives not initialized" << endl;
|
---|
186 |
|
---|
187 | al_init_font_addon();
|
---|
188 | al_init_ttf_addon();
|
---|
189 |
|
---|
190 | ALLEGRO_FONT* font;
|
---|
191 | #if defined WINDOWS
|
---|
192 | font = al_load_ttf_font("../pirulen.ttf", 12, 0);
|
---|
193 | #elif defined LINUX
|
---|
194 | font = al_load_ttf_font("pirulen.ttf", 12, 0);
|
---|
195 | #elif defined MAC
|
---|
196 | font = al_load_ttf_font("pirulen.ttf", 12, 0);
|
---|
197 | #endif
|
---|
198 |
|
---|
199 | if (!font) {
|
---|
200 | fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
|
---|
201 | getchar();
|
---|
202 | return -1;
|
---|
203 | }
|
---|
204 |
|
---|
205 | if(!al_install_keyboard()) {
|
---|
206 | fprintf(stderr, "failed to initialize the keyboard!\n");
|
---|
207 | return -1;
|
---|
208 | }
|
---|
209 |
|
---|
210 | if(!al_install_mouse()) {
|
---|
211 | fprintf(stderr, "failed to initialize the mouse!\n");
|
---|
212 | return -1;
|
---|
213 | }
|
---|
214 |
|
---|
215 | timer = al_create_timer(1.0 / FPS);
|
---|
216 | if(!timer) {
|
---|
217 | fprintf(stderr, "failed to create timer!\n");
|
---|
218 | return -1;
|
---|
219 | }
|
---|
220 |
|
---|
221 | int refreshRate = getRefreshRate(SCREEN_W, SCREEN_H);
|
---|
222 | // if the computer doesn't support this resolution, just use windowed mode
|
---|
223 | if (refreshRate > 0 && fullscreen) {
|
---|
224 | al_set_new_display_flags(ALLEGRO_FULLSCREEN);
|
---|
225 | al_set_new_display_refresh_rate(refreshRate);
|
---|
226 | }
|
---|
227 | display = al_create_display(SCREEN_W, SCREEN_H);
|
---|
228 | if(!display) {
|
---|
229 | fprintf(stderr, "failed to create display!\n");
|
---|
230 | al_destroy_timer(timer);
|
---|
231 | return -1;
|
---|
232 | }
|
---|
233 |
|
---|
234 | debugConsole.addLine("Debug console:");
|
---|
235 | debugConsole.addLine("");
|
---|
236 |
|
---|
237 | createGui(font);
|
---|
238 |
|
---|
239 | goToLoginScreen();
|
---|
240 |
|
---|
241 | event_queue = al_create_event_queue();
|
---|
242 | if(!event_queue) {
|
---|
243 | fprintf(stderr, "failed to create event_queue!\n");
|
---|
244 | al_destroy_display(display);
|
---|
245 | al_destroy_timer(timer);
|
---|
246 | return -1;
|
---|
247 | }
|
---|
248 |
|
---|
249 | al_set_target_bitmap(al_get_backbuffer(display));
|
---|
250 |
|
---|
251 | al_register_event_source(event_queue, al_get_display_event_source(display));
|
---|
252 | al_register_event_source(event_queue, al_get_timer_event_source(timer));
|
---|
253 | al_register_event_source(event_queue, al_get_keyboard_event_source());
|
---|
254 | al_register_event_source(event_queue, al_get_mouse_event_source());
|
---|
255 |
|
---|
256 | al_clear_to_color(al_map_rgb(0,0,0));
|
---|
257 |
|
---|
258 | al_flip_display();
|
---|
259 |
|
---|
260 | if (argc != 3) {
|
---|
261 | cout << "Usage: server port" << endl;
|
---|
262 | exit(1);
|
---|
263 | }
|
---|
264 |
|
---|
265 | initWinSock();
|
---|
266 |
|
---|
267 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
268 | if (sock < 0)
|
---|
269 | error("socket");
|
---|
270 |
|
---|
271 | set_nonblock(sock);
|
---|
272 |
|
---|
273 | server.sin_family = AF_INET;
|
---|
274 | hp = gethostbyname(argv[1]);
|
---|
275 | if (hp == 0)
|
---|
276 | error("Unknown host");
|
---|
277 |
|
---|
278 | memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
|
---|
279 | server.sin_port = htons(atoi(argv[2]));
|
---|
280 |
|
---|
281 | msgProcessor = MessageProcessor(sock, &outputLog);
|
---|
282 |
|
---|
283 | al_start_timer(timer);
|
---|
284 |
|
---|
285 | while (!doexit)
|
---|
286 | {
|
---|
287 | ALLEGRO_EVENT ev;
|
---|
288 |
|
---|
289 | al_wait_for_event(event_queue, &ev);
|
---|
290 |
|
---|
291 | if(wndCurrent->handleEvent(ev)) {
|
---|
292 | // do nothing
|
---|
293 | }
|
---|
294 | else if(ev.type == ALLEGRO_EVENT_TIMER) {
|
---|
295 | redraw = true;
|
---|
296 |
|
---|
297 | // remove any other timer events in the queue
|
---|
298 | while (al_peek_next_event(event_queue, &ev) && ev.type == ALLEGRO_EVENT_TIMER) {
|
---|
299 | al_get_next_event(event_queue, &ev);
|
---|
300 | }
|
---|
301 | }
|
---|
302 | else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
|
---|
303 | doexit = true;
|
---|
304 | }
|
---|
305 | else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
|
---|
306 | }
|
---|
307 | else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
|
---|
308 | switch(ev.keyboard.keycode) {
|
---|
309 | case ALLEGRO_KEY_ESCAPE:
|
---|
310 | doexit = true;
|
---|
311 | break;
|
---|
312 | case ALLEGRO_KEY_S: // pickup an item next to you
|
---|
313 | if (state == STATE_GAME) {
|
---|
314 | msgTo.type = MSG_TYPE_PICKUP_FLAG;
|
---|
315 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
316 | msgProcessor.sendMessage(&msgTo, &server);
|
---|
317 | }
|
---|
318 | break;
|
---|
319 | case ALLEGRO_KEY_D: // drop the current item
|
---|
320 | if (state == STATE_GAME) {
|
---|
321 | try {
|
---|
322 | Player* p = mapPlayers.at(curPlayerId);
|
---|
323 | int flagType = OBJECT_NONE;
|
---|
324 |
|
---|
325 | if (p->hasBlueFlag)
|
---|
326 | flagType = OBJECT_BLUE_FLAG;
|
---|
327 | else if (p->hasRedFlag)
|
---|
328 | flagType = OBJECT_RED_FLAG;
|
---|
329 |
|
---|
330 | if (flagType != OBJECT_NONE) {
|
---|
331 | msgTo.type = MSG_TYPE_DROP_FLAG;
|
---|
332 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
333 | msgProcessor.sendMessage(&msgTo, &server);
|
---|
334 | }
|
---|
335 | } catch (const out_of_range& ex) {}
|
---|
336 | }
|
---|
337 | break;
|
---|
338 | }
|
---|
339 | }
|
---|
340 | else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
|
---|
341 | if (wndCurrent == wndGame) {
|
---|
342 | if (ev.mouse.button == 1) { // left click
|
---|
343 | msgTo.type = MSG_TYPE_PLAYER_MOVE;
|
---|
344 |
|
---|
345 | POSITION pos;
|
---|
346 | pos.x = ev.mouse.x;
|
---|
347 | pos.y = ev.mouse.y;
|
---|
348 | pos = screenToMap(pos);
|
---|
349 |
|
---|
350 | if (pos.x != -1)
|
---|
351 | {
|
---|
352 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
353 | memcpy(msgTo.buffer+4, &pos.x, 4);
|
---|
354 | memcpy(msgTo.buffer+8, &pos.y, 4);
|
---|
355 |
|
---|
356 | msgProcessor.sendMessage(&msgTo, &server);
|
---|
357 | }
|
---|
358 | else
|
---|
359 | cout << "Invalid point: User did not click on the map" << endl;
|
---|
360 | }else if (ev.mouse.button == 2) { // right click
|
---|
361 | cout << "Detected a right-click" << endl;
|
---|
362 | map<unsigned int, Player*>::iterator it;
|
---|
363 |
|
---|
364 | Player* curPlayer = mapPlayers[curPlayerId];;
|
---|
365 |
|
---|
366 | cout << "Got current player" << endl;
|
---|
367 | cout << "current game: " << game << endl;
|
---|
368 |
|
---|
369 | map<unsigned int, Player*> playersInGame = game->getPlayers();
|
---|
370 | Player* target;
|
---|
371 |
|
---|
372 | for (it = playersInGame.begin(); it != playersInGame.end(); it++)
|
---|
373 | {
|
---|
374 | target = it->second;
|
---|
375 | cout << "set target" << endl;
|
---|
376 | if (target->team != curPlayer->team)
|
---|
377 | {
|
---|
378 | cout << "Found valid target" << endl;
|
---|
379 |
|
---|
380 | POSITION cursorPos;
|
---|
381 | cursorPos.x = ev.mouse.x;
|
---|
382 | cursorPos.y = ev.mouse.y;
|
---|
383 | cursorPos = screenToMap(cursorPos);
|
---|
384 |
|
---|
385 | float distance =posDistance(cursorPos.toFloat(), target->pos);
|
---|
386 |
|
---|
387 | if (distance < 25) {
|
---|
388 | unsigned int targetId = target->getId();
|
---|
389 |
|
---|
390 | msgTo.type = MSG_TYPE_ATTACK;
|
---|
391 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
392 | memcpy(msgTo.buffer+4, &targetId, 4);
|
---|
393 |
|
---|
394 | msgProcessor.sendMessage(&msgTo, &server);
|
---|
395 | }
|
---|
396 | }
|
---|
397 | }
|
---|
398 | }
|
---|
399 | }
|
---|
400 | }
|
---|
401 |
|
---|
402 | if (msgProcessor.receiveMessage(&msgFrom, &from) >= 0)
|
---|
403 | processMessage(msgFrom, state, chatConsole, mapPlayers, mapGames, curPlayerId);
|
---|
404 |
|
---|
405 | if (redraw)
|
---|
406 | {
|
---|
407 | redraw = false;
|
---|
408 |
|
---|
409 | msgProcessor.resendUnackedMessages();
|
---|
410 |
|
---|
411 | if (debugging && wndCurrent == wndLobby)
|
---|
412 | wndLobbyDebug->draw(display);
|
---|
413 | else
|
---|
414 | wndCurrent->draw(display);
|
---|
415 |
|
---|
416 | if (wndCurrent == wndLobby) {
|
---|
417 | if (!debugging)
|
---|
418 | chatConsole.draw(font, al_map_rgb(255,255,255));
|
---|
419 |
|
---|
420 | al_draw_text(font, al_map_rgb(0, 255, 0), SCREEN_W*1/2-100, 120, ALLEGRO_ALIGN_LEFT, "Current Games");
|
---|
421 |
|
---|
422 | map<string, int>::iterator it;
|
---|
423 | int i=0;
|
---|
424 | ostringstream oss;
|
---|
425 | for (it = mapGames.begin(); it != mapGames.end(); it++) {
|
---|
426 | oss << it->first << " (" << it->second << " players)" << endl;
|
---|
427 | al_draw_text(font, al_map_rgb(0, 255, 0), SCREEN_W*1/2-100, 135+i*15, ALLEGRO_ALIGN_LEFT, oss.str().c_str());
|
---|
428 | oss.clear();
|
---|
429 | oss.str("");
|
---|
430 | i++;
|
---|
431 | }
|
---|
432 |
|
---|
433 | al_draw_text(font, al_map_rgb(0, 255, 0), SCREEN_W*3/4-100, 120, ALLEGRO_ALIGN_LEFT, "Online Players");
|
---|
434 |
|
---|
435 | map<unsigned int, Player*>::iterator itPlayers;
|
---|
436 | i=0;
|
---|
437 | for (itPlayers = mapPlayers.begin(); itPlayers != mapPlayers.end(); itPlayers++) {
|
---|
438 | oss << itPlayers->second->name << endl;
|
---|
439 | al_draw_text(font, al_map_rgb(0, 255, 0), SCREEN_W*3/4-100, 135+i*15, ALLEGRO_ALIGN_LEFT, oss.str().c_str());
|
---|
440 | oss.clear();
|
---|
441 | oss.str("");
|
---|
442 | i++;
|
---|
443 | }
|
---|
444 | }
|
---|
445 | else if (wndCurrent == wndProfile)
|
---|
446 | {
|
---|
447 | ostringstream oss;
|
---|
448 | oss << "Honor Points: " << honorPoints << endl;
|
---|
449 | al_draw_text(font, al_map_rgb(0, 255, 0), 65, 90, ALLEGRO_ALIGN_LEFT, oss.str().c_str());
|
---|
450 | oss.clear();
|
---|
451 | oss.str("");
|
---|
452 |
|
---|
453 | oss << "Wins: " << wins << endl;
|
---|
454 | al_draw_text(font, al_map_rgb(0, 255, 0), 65, 105, ALLEGRO_ALIGN_LEFT, oss.str().c_str());
|
---|
455 | oss.clear();
|
---|
456 | oss.str("");
|
---|
457 |
|
---|
458 | oss << "Losses: " << losses << endl;
|
---|
459 | al_draw_text(font, al_map_rgb(0, 255, 0), 65, 120, ALLEGRO_ALIGN_LEFT, oss.str().c_str());
|
---|
460 | oss.clear();
|
---|
461 | oss.str("");
|
---|
462 |
|
---|
463 | // display records of the last 10 games
|
---|
464 | for (int i=0; i<numGames; i++) {
|
---|
465 |
|
---|
466 | if (gameHistory[i][0] == 0)
|
---|
467 | oss << "DEFEAT" << endl;
|
---|
468 | else if (gameHistory[i][0] == 1)
|
---|
469 | oss << "VICTORY" << endl;
|
---|
470 |
|
---|
471 | al_draw_text(font, al_map_rgb(0, 255, 0), 142, 190+30*(i+1), ALLEGRO_ALIGN_CENTRE, oss.str().c_str());
|
---|
472 | oss.clear();
|
---|
473 | oss.str("");
|
---|
474 |
|
---|
475 | oss << gameHistory[i][2] << endl;
|
---|
476 | al_draw_text(font, al_map_rgb(0, 255, 0), 302, 190+30*(i+1), ALLEGRO_ALIGN_CENTRE, oss.str().c_str());
|
---|
477 | oss.clear();
|
---|
478 | oss.str("");
|
---|
479 |
|
---|
480 | oss << gameHistory[i][3] << endl;
|
---|
481 | al_draw_text(font, al_map_rgb(0, 255, 0), 462, 190+30*(i+1), ALLEGRO_ALIGN_CENTRE, oss.str().c_str());
|
---|
482 | oss.clear();
|
---|
483 | oss.str("");
|
---|
484 |
|
---|
485 | oss << "6/11/2014, 5:12 PM" << endl;
|
---|
486 | al_draw_text(font, al_map_rgb(0, 255, 0), 622, 190+30*(i+1), ALLEGRO_ALIGN_CENTRE, oss.str().c_str());
|
---|
487 | oss.clear();
|
---|
488 | oss.str("");
|
---|
489 |
|
---|
490 | }
|
---|
491 |
|
---|
492 | }
|
---|
493 | else if (wndCurrent == wndGameLobby)
|
---|
494 | {
|
---|
495 | al_draw_text(font, al_map_rgb(0, 255, 0), 200, 100, ALLEGRO_ALIGN_LEFT, "Waiting Area");
|
---|
496 | al_draw_text(font, al_map_rgb(0, 255, 0), 400, 100, ALLEGRO_ALIGN_LEFT, "Blue Team");
|
---|
497 | al_draw_text(font, al_map_rgb(0, 255, 0), 600, 100, ALLEGRO_ALIGN_LEFT, "Red Team");
|
---|
498 |
|
---|
499 | int drawPosition = 0;
|
---|
500 |
|
---|
501 | map<unsigned int, Player*> gamePlayers = game->getPlayers();
|
---|
502 | map<unsigned int, Player*>::iterator itPlayers;
|
---|
503 | ostringstream oss;
|
---|
504 | int i=0;
|
---|
505 | for (itPlayers = gamePlayers.begin(); itPlayers != gamePlayers.end(); itPlayers++) {
|
---|
506 | switch (itPlayers->second->team) {
|
---|
507 | case 0:
|
---|
508 | drawPosition = 200;
|
---|
509 | break;
|
---|
510 | case 1:
|
---|
511 | drawPosition = 400;
|
---|
512 | break;
|
---|
513 | case 2:
|
---|
514 | drawPosition = 600;
|
---|
515 | break;
|
---|
516 | }
|
---|
517 |
|
---|
518 | oss << itPlayers->second->name << endl;
|
---|
519 | al_draw_text(font, al_map_rgb(0, 255, 0), drawPosition, 135+i*15, ALLEGRO_ALIGN_LEFT, oss.str().c_str());
|
---|
520 | oss.clear();
|
---|
521 | oss.str("");
|
---|
522 | i++;
|
---|
523 | }
|
---|
524 | }
|
---|
525 | else if (wndCurrent == wndGame)
|
---|
526 | {
|
---|
527 | al_draw_text(font, al_map_rgb(0, 255, 0), 4, 4, ALLEGRO_ALIGN_LEFT, "Players");
|
---|
528 |
|
---|
529 | map<unsigned int, Player*>& gamePlayers = game->getPlayers();
|
---|
530 | map<unsigned int, Player*>::iterator it;
|
---|
531 |
|
---|
532 | if (!debugging) {
|
---|
533 | int playerCount = 0;
|
---|
534 | for (it = gamePlayers.begin(); it != gamePlayers.end(); it++)
|
---|
535 | {
|
---|
536 | al_draw_text(font, al_map_rgb(0, 255, 0), 4, 19+(playerCount+1)*15, ALLEGRO_ALIGN_LEFT, it->second->name.c_str());
|
---|
537 | playerCount++;
|
---|
538 | }
|
---|
539 | }
|
---|
540 |
|
---|
541 | ostringstream ossScoreBlue, ossScoreRed;
|
---|
542 |
|
---|
543 | ossScoreBlue << "Blue: " << game->getBlueScore() << endl;
|
---|
544 | ossScoreRed << "Red: " << game->getRedScore() << endl;
|
---|
545 |
|
---|
546 | al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
|
---|
547 | al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
|
---|
548 |
|
---|
549 | // update players
|
---|
550 | for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
|
---|
551 | {
|
---|
552 | it->second->updateTarget(game->getPlayers());
|
---|
553 | }
|
---|
554 |
|
---|
555 | for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
|
---|
556 | {
|
---|
557 | it->second->move(game->getMap()); // ignore return value
|
---|
558 | }
|
---|
559 |
|
---|
560 | // update projectile positions
|
---|
561 | map<unsigned int, Projectile>::iterator it2;
|
---|
562 | for (it2 = game->getProjectiles().begin(); it2 != game->getProjectiles().end(); it2++)
|
---|
563 | {
|
---|
564 | it2->second.move(game->getPlayers());
|
---|
565 | }
|
---|
566 |
|
---|
567 | GameRender::drawMap(game->getMap());
|
---|
568 | GameRender::drawPlayers(game->getPlayers(), font, curPlayerId);
|
---|
569 | GameRender::drawProjectiles(game->getProjectiles(), game->getPlayers());
|
---|
570 | }
|
---|
571 | else if (wndCurrent == wndGameSummary)
|
---|
572 | {
|
---|
573 | ostringstream ossBlueScore, ossRedScore;
|
---|
574 |
|
---|
575 | ossBlueScore << "Blue Score: " << gameSummary->getBlueScore();
|
---|
576 | ossRedScore << "Red Score: " << gameSummary->getRedScore();
|
---|
577 |
|
---|
578 | string strWinner;
|
---|
579 |
|
---|
580 | if (gameSummary->getWinner() == 0)
|
---|
581 | strWinner = "Blue Team Wins";
|
---|
582 | else if (gameSummary->getWinner() == 1)
|
---|
583 | strWinner = "Red Team Wins";
|
---|
584 | else
|
---|
585 | strWinner = "winner set to wrong value";
|
---|
586 |
|
---|
587 | al_draw_text(font, al_map_rgb(0, 255, 0), 512, 40, ALLEGRO_ALIGN_CENTRE, gameSummary->getName().c_str());
|
---|
588 | al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossBlueScore.str().c_str());
|
---|
589 | al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossRedScore.str().c_str());
|
---|
590 | al_draw_text(font, al_map_rgb(0, 255, 0), 512, 120, ALLEGRO_ALIGN_CENTRE, strWinner.c_str());
|
---|
591 | }
|
---|
592 |
|
---|
593 | if (debugging) {
|
---|
594 | drawMessageStatus(font);
|
---|
595 | }
|
---|
596 |
|
---|
597 | al_flip_display();
|
---|
598 | }
|
---|
599 | }
|
---|
600 |
|
---|
601 | #if defined WINDOWS
|
---|
602 | closesocket(sock);
|
---|
603 | #elif defined LINUX
|
---|
604 | close(sock);
|
---|
605 | #endif
|
---|
606 |
|
---|
607 | shutdownWinSock();
|
---|
608 |
|
---|
609 | // delete all components
|
---|
610 | for (unsigned int x=0; x<vctComponents.size(); x++)
|
---|
611 | delete vctComponents[x];
|
---|
612 |
|
---|
613 | delete wndLogin;
|
---|
614 | delete wndRegister;
|
---|
615 | delete wndLobby;
|
---|
616 | delete wndLobbyDebug;
|
---|
617 | delete wndGameLobby;
|
---|
618 | delete wndGame;
|
---|
619 | delete wndGameSummary;
|
---|
620 |
|
---|
621 | // game should be deleted when the player leaves a gamw
|
---|
622 | if (game != NULL)
|
---|
623 | delete game;
|
---|
624 |
|
---|
625 | if (gameSummary != NULL)
|
---|
626 | delete gameSummary;
|
---|
627 |
|
---|
628 | map<unsigned int, Player*>::iterator it;
|
---|
629 |
|
---|
630 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
|
---|
631 | delete it->second;
|
---|
632 | }
|
---|
633 |
|
---|
634 | if (gameHistory != NULL) {
|
---|
635 | for (int i=0; i<numGames; i++) {
|
---|
636 | free(gameHistory[i]);
|
---|
637 | }
|
---|
638 |
|
---|
639 | free(gameHistory);
|
---|
640 | }
|
---|
641 |
|
---|
642 | al_destroy_event_queue(event_queue);
|
---|
643 | al_destroy_display(display);
|
---|
644 | al_destroy_timer(timer);
|
---|
645 |
|
---|
646 | outputLog << "Stopped client on " << getCurrentDateTimeString() << endl;
|
---|
647 | outputLog.close();
|
---|
648 |
|
---|
649 | return 0;
|
---|
650 | }
|
---|
651 |
|
---|
652 | void initWinSock()
|
---|
653 | {
|
---|
654 | #if defined WINDOWS
|
---|
655 | WORD wVersionRequested;
|
---|
656 | WSADATA wsaData;
|
---|
657 | int wsaerr;
|
---|
658 |
|
---|
659 | wVersionRequested = MAKEWORD(2, 2);
|
---|
660 | wsaerr = WSAStartup(wVersionRequested, &wsaData);
|
---|
661 |
|
---|
662 | if (wsaerr != 0) {
|
---|
663 | cout << "The Winsock dll not found." << endl;
|
---|
664 | exit(1);
|
---|
665 | }else
|
---|
666 | cout << "The Winsock dll was found." << endl;
|
---|
667 | #endif
|
---|
668 | }
|
---|
669 |
|
---|
670 | void shutdownWinSock()
|
---|
671 | {
|
---|
672 | #if defined WINDOWS
|
---|
673 | WSACleanup();
|
---|
674 | #endif
|
---|
675 | }
|
---|
676 |
|
---|
677 | void createGui(ALLEGRO_FONT* font) {
|
---|
678 |
|
---|
679 | // wndLogin
|
---|
680 |
|
---|
681 | wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
682 | vctComponents.push_back(wndLogin->addComponent(new Textbox(516, 40, 100, 20, font)));
|
---|
683 | vctComponents.push_back(wndLogin->addComponent(new Textbox(516, 70, 100, 20, font)));
|
---|
684 | vctComponents.push_back(wndLogin->addComponent(new TextLabel(410, 40, 100, 20, font, "Username:", ALLEGRO_ALIGN_RIGHT)));
|
---|
685 | vctComponents.push_back(wndLogin->addComponent(new TextLabel(410, 70, 100, 20, font, "Password:", ALLEGRO_ALIGN_RIGHT)));
|
---|
686 | vctComponents.push_back(wndLogin->addComponent(new TextLabel((SCREEN_W-600)/2, 100, 600, 20, font, "", ALLEGRO_ALIGN_CENTRE)));
|
---|
687 | vctComponents.push_back(wndLogin->addComponent(new Button(SCREEN_W/2-100, 130, 90, 20, font, "Register", goToRegisterScreen)));
|
---|
688 | vctComponents.push_back(wndLogin->addComponent(new Button(SCREEN_W/2+10, 130, 90, 20, font, "Login", login)));
|
---|
689 | vctComponents.push_back(wndLogin->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit)));
|
---|
690 | vctComponents.push_back(wndLogin->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
|
---|
691 |
|
---|
692 | txtUsername = (Textbox*)wndLogin->getComponent(0);
|
---|
693 | txtPassword = (Textbox*)wndLogin->getComponent(1);
|
---|
694 | lblLoginStatus = (TextLabel*)wndLogin->getComponent(4);
|
---|
695 |
|
---|
696 | cout << "Created login screen" << endl;
|
---|
697 |
|
---|
698 |
|
---|
699 | // wndRegister
|
---|
700 |
|
---|
701 | wndRegister = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
702 | vctComponents.push_back(wndRegister->addComponent(new Textbox(516, 40, 100, 20, font)));
|
---|
703 | vctComponents.push_back(wndRegister->addComponent(new Textbox(516, 70, 100, 20, font)));
|
---|
704 | vctComponents.push_back(wndRegister->addComponent(new TextLabel(410, 40, 100, 20, font, "Username:", ALLEGRO_ALIGN_RIGHT)));
|
---|
705 | vctComponents.push_back(wndRegister->addComponent(new TextLabel(410, 70, 100, 20, font, "Password:", ALLEGRO_ALIGN_RIGHT)));
|
---|
706 | vctComponents.push_back(wndRegister->addComponent(new RadioButtonList(432, 100, "Pick a class", font)));
|
---|
707 | vctComponents.push_back(wndRegister->addComponent(new TextLabel((SCREEN_W-600)/2, 190, 600, 20, font, "", ALLEGRO_ALIGN_CENTRE)));
|
---|
708 | vctComponents.push_back(wndRegister->addComponent(new Button(SCREEN_W/2-100, 220, 90, 20, font, "Back", goToLoginScreen)));
|
---|
709 | vctComponents.push_back(wndRegister->addComponent(new Button(SCREEN_W/2+10, 220, 90, 20, font, "Submit", registerAccount)));
|
---|
710 | vctComponents.push_back(wndRegister->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit)));
|
---|
711 | vctComponents.push_back(wndRegister->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
|
---|
712 |
|
---|
713 | txtUsernameRegister = (Textbox*)wndRegister->getComponent(0);
|
---|
714 | txtPasswordRegister = (Textbox*)wndRegister->getComponent(1);
|
---|
715 |
|
---|
716 | rblClasses = (RadioButtonList*)wndRegister->getComponent(4);
|
---|
717 | rblClasses->addRadioButton("Warrior");
|
---|
718 | rblClasses->addRadioButton("Ranger");
|
---|
719 |
|
---|
720 | lblRegisterStatus = (TextLabel*)wndRegister->getComponent(5);
|
---|
721 |
|
---|
722 | cout << "Created register screen" << endl;
|
---|
723 |
|
---|
724 |
|
---|
725 | // wndLobby
|
---|
726 |
|
---|
727 | txtJoinGame = new Textbox(SCREEN_W*1/2+15+4, 40, 100, 20, font);
|
---|
728 | vctComponents.push_back(txtJoinGame);
|
---|
729 |
|
---|
730 | txtCreateGame = new Textbox(SCREEN_W*3/4+4, 40, 100, 20, font);
|
---|
731 | vctComponents.push_back(txtCreateGame);
|
---|
732 |
|
---|
733 | wndLobby = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
734 | vctComponents.push_back(wndLobby->addComponent(new Button(920, 10, 80, 20, font, "Profile", goToProfileScreen)));
|
---|
735 | vctComponents.push_back(wndLobby->addComponent(new Button(920, 738, 80, 20, font, "Logout", logout)));
|
---|
736 | vctComponents.push_back(wndLobby->addComponent(new TextLabel(SCREEN_W*1/2+15-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
|
---|
737 | wndLobby->addComponent(txtJoinGame);
|
---|
738 | vctComponents.push_back(wndLobby->addComponent(new Button(SCREEN_W*1/2+15-100, 80, 200, 20, font, "Join Existing Game", joinGame)));
|
---|
739 | vctComponents.push_back(wndLobby->addComponent(new TextLabel(SCREEN_W*3/4-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
|
---|
740 | wndLobby->addComponent(txtCreateGame);
|
---|
741 | vctComponents.push_back(wndLobby->addComponent(new Button(SCREEN_W*3/4-100, 80, 200, 20, font, "Create New Game", createGame)));
|
---|
742 | vctComponents.push_back(wndLobby->addComponent(new Textbox(95, 40, 300, 20, font)));
|
---|
743 | vctComponents.push_back(wndLobby->addComponent(new Button(95, 70, 60, 20, font, "Send", sendChatMessage)));
|
---|
744 | vctComponents.push_back(wndLobby->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
|
---|
745 |
|
---|
746 | txtChat = (Textbox*)wndLobby->getComponent(7);
|
---|
747 |
|
---|
748 | cout << "Created lobby screen" << endl;
|
---|
749 |
|
---|
750 |
|
---|
751 | // wndLobbyDebug
|
---|
752 |
|
---|
753 | wndLobbyDebug = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
754 | vctComponents.push_back(wndLobbyDebug->addComponent(new Button(920, 10, 80, 20, font, "Profile", goToProfileScreen)));
|
---|
755 | vctComponents.push_back(wndLobbyDebug->addComponent(new Button(920, 738, 80, 20, font, "Logout", logout)));
|
---|
756 | vctComponents.push_back(wndLobbyDebug->addComponent(new TextLabel(SCREEN_W*1/2+15-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
|
---|
757 | wndLobbyDebug->addComponent(txtJoinGame);
|
---|
758 | vctComponents.push_back(wndLobbyDebug->addComponent(new Button(SCREEN_W*1/2+15-100, 80, 200, 20, font, "Join Existing Game", joinGame)));
|
---|
759 | vctComponents.push_back(wndLobbyDebug->addComponent(new TextLabel(SCREEN_W*3/4-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
|
---|
760 | wndLobbyDebug->addComponent(txtCreateGame);
|
---|
761 | vctComponents.push_back(wndLobbyDebug->addComponent(new Button(SCREEN_W*3/4-100, 80, 200, 20, font, "Create New Game", createGame)));
|
---|
762 | vctComponents.push_back(wndLobbyDebug->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
|
---|
763 |
|
---|
764 | cout << "Created debug lobby screen" << endl;
|
---|
765 |
|
---|
766 |
|
---|
767 | // wndProfile
|
---|
768 |
|
---|
769 | wndProfile = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
770 | vctComponents.push_back(wndProfile->addComponent(new TextLabel(450, 40, 124, 20, font, "Profile", ALLEGRO_ALIGN_CENTRE)));
|
---|
771 | vctComponents.push_back(wndProfile->addComponent(new TextLabel(160, 150, 124, 20, font, "Game History", ALLEGRO_ALIGN_CENTRE)));
|
---|
772 | vctComponents.push_back(wndProfile->addComponent(new TextLabel(600, 190, 124, 20, font, "Time", ALLEGRO_ALIGN_CENTRE)));
|
---|
773 | vctComponents.push_back(wndProfile->addComponent(new TextLabel(80, 190, 124, 20, font, "Result", ALLEGRO_ALIGN_CENTRE)));
|
---|
774 | vctComponents.push_back(wndProfile->addComponent(new TextLabel(240, 190, 124, 20, font, "Blue Score", ALLEGRO_ALIGN_CENTRE)));
|
---|
775 | vctComponents.push_back(wndProfile->addComponent(new TextLabel(400, 190, 124, 20, font, "Red Score", ALLEGRO_ALIGN_CENTRE)));
|
---|
776 | vctComponents.push_back(wndProfile->addComponent(new TextLabel(560, 190, 124, 20, font, "Time", ALLEGRO_ALIGN_CENTRE)));
|
---|
777 | vctComponents.push_back(wndProfile->addComponent(new Button(920, 738, 80, 20, font, "Back", goToLobbyScreen)));
|
---|
778 |
|
---|
779 |
|
---|
780 | // wndGameLobby
|
---|
781 |
|
---|
782 | wndGameLobby = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
783 | vctComponents.push_back(wndGameLobby->addComponent(new Button(180, 120, 160, 300, font, "", joinWaitingArea)));
|
---|
784 | vctComponents.push_back(wndGameLobby->addComponent(new Button(380, 120, 160, 300, font, "", joinBlueTeam)));
|
---|
785 | vctComponents.push_back(wndGameLobby->addComponent(new Button(580, 120, 160, 300, font, "", joinRedTeam)));
|
---|
786 | vctComponents.push_back(wndGameLobby->addComponent(new Button(40, 600, 120, 20, font, "Leave Game", leaveGame)));
|
---|
787 | vctComponents.push_back(wndGameLobby->addComponent(new Button(800, 600, 120, 20, font, "Start Game", startGame)));
|
---|
788 |
|
---|
789 |
|
---|
790 | // wndGame
|
---|
791 |
|
---|
792 | wndGame = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
793 | vctComponents.push_back(wndGame->addComponent(new Button(880, 10, 120, 20, font, "Leave Game", leaveGame)));
|
---|
794 |
|
---|
795 | cout << "Created new game screen" << endl;
|
---|
796 |
|
---|
797 | wndGameSummary = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
798 | vctComponents.push_back(wndGameSummary->addComponent(new Button(840, 730, 160, 20, font, "Back to Lobby", closeGameSummary)));
|
---|
799 |
|
---|
800 | cout << "Created game summary screen" << endl;
|
---|
801 | }
|
---|
802 |
|
---|
803 | void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames, unsigned int& curPlayerId)
|
---|
804 | {
|
---|
805 | cout << "Total players in map: " << mapPlayers.size() << endl;
|
---|
806 |
|
---|
807 | // this is outdated since most messages now don't contain just a text string
|
---|
808 | string response = string(msg.buffer);
|
---|
809 |
|
---|
810 | switch(state)
|
---|
811 | {
|
---|
812 | case STATE_START:
|
---|
813 | {
|
---|
814 | cout << "In STATE_START" << endl;
|
---|
815 |
|
---|
816 | switch(msg.type)
|
---|
817 | {
|
---|
818 | case MSG_TYPE_REGISTER:
|
---|
819 | {
|
---|
820 | lblRegisterStatus->setText(response);
|
---|
821 | break;
|
---|
822 | }
|
---|
823 | default:
|
---|
824 | {
|
---|
825 | cout << "(STATE_REGISTER) Received invalid message of type " << msg.type << endl;
|
---|
826 | break;
|
---|
827 | }
|
---|
828 | }
|
---|
829 |
|
---|
830 | break;
|
---|
831 | }
|
---|
832 | case STATE_LOBBY:
|
---|
833 | {
|
---|
834 | cout << "In STATE_LOBBY" << endl;
|
---|
835 | switch(msg.type)
|
---|
836 | {
|
---|
837 | case MSG_TYPE_LOGIN:
|
---|
838 | {
|
---|
839 | if (response.compare("Player has already logged in.") == 0)
|
---|
840 | {
|
---|
841 | goToLoginScreen();
|
---|
842 | state = STATE_START;
|
---|
843 |
|
---|
844 | lblLoginStatus->setText(response);
|
---|
845 | }
|
---|
846 | else if (response.compare("Incorrect username or password") == 0)
|
---|
847 | {
|
---|
848 | goToLoginScreen();
|
---|
849 | state = STATE_START;
|
---|
850 |
|
---|
851 | lblLoginStatus->setText(response);
|
---|
852 | }
|
---|
853 | else
|
---|
854 | {
|
---|
855 | wndCurrent = wndLobby;
|
---|
856 |
|
---|
857 | // this message should only be sent when a player first logs in so they know their id
|
---|
858 |
|
---|
859 | Player* p = new Player("", "");
|
---|
860 | p->deserialize(msg.buffer);
|
---|
861 |
|
---|
862 | if (mapPlayers.find(p->getId()) != mapPlayers.end())
|
---|
863 | delete mapPlayers[p->getId()];
|
---|
864 | mapPlayers[p->getId()] = p;
|
---|
865 | curPlayerId = p->getId();
|
---|
866 | currentPlayer = mapPlayers[curPlayerId];
|
---|
867 |
|
---|
868 | cout << "Got a valid login response with the player" << endl;
|
---|
869 | cout << "Player id: " << curPlayerId << endl;
|
---|
870 | cout << "Player health: " << p->health << endl;
|
---|
871 | cout << "player map size: " << mapPlayers.size() << endl;
|
---|
872 | }
|
---|
873 |
|
---|
874 | break;
|
---|
875 | }
|
---|
876 | case MSG_TYPE_LOGOUT:
|
---|
877 | {
|
---|
878 | cout << "Got a logout message" << endl;
|
---|
879 |
|
---|
880 | unsigned int playerId;
|
---|
881 |
|
---|
882 | // Check if it's about you or another player
|
---|
883 | memcpy(&playerId, msg.buffer, 4);
|
---|
884 | response = string(msg.buffer+4);
|
---|
885 |
|
---|
886 | if (playerId == curPlayerId)
|
---|
887 | {
|
---|
888 | cout << "Got logout message for self" << endl;
|
---|
889 |
|
---|
890 | if (response.compare("You have successfully logged out.") == 0)
|
---|
891 | {
|
---|
892 | cout << "Logged out" << endl;
|
---|
893 | state = STATE_START;
|
---|
894 | goToLoginScreen();
|
---|
895 | }
|
---|
896 |
|
---|
897 | // if there was an error logging out, nothing happens
|
---|
898 | }
|
---|
899 | else
|
---|
900 | {
|
---|
901 | delete mapPlayers[playerId];
|
---|
902 | mapPlayers.erase(playerId);
|
---|
903 | }
|
---|
904 |
|
---|
905 | break;
|
---|
906 | }
|
---|
907 | case MSG_TYPE_CHAT:
|
---|
908 | {
|
---|
909 | chatConsole.addLine(response);
|
---|
910 |
|
---|
911 | break;
|
---|
912 | }
|
---|
913 | case MSG_TYPE_PROFILE:
|
---|
914 | {
|
---|
915 | memcpy(&honorPoints, msg.buffer, 4);
|
---|
916 | memcpy(&wins, msg.buffer+4, 4);
|
---|
917 | memcpy(&losses, msg.buffer+8, 4);
|
---|
918 | memcpy(&numGames, msg.buffer+12, 4);
|
---|
919 |
|
---|
920 | cout << "Got records for " << numGames << " games." << endl;
|
---|
921 | gameHistory = (int**)malloc(numGames*sizeof(int*));
|
---|
922 | for (int i=0; i<numGames; i++) {
|
---|
923 | gameHistory[i] = (int*)malloc(4*sizeof(int));
|
---|
924 | cout << endl << "game record " << (i+1) << endl;
|
---|
925 |
|
---|
926 | memcpy(&gameHistory[i][0], msg.buffer+16+i*16, 4);
|
---|
927 | memcpy(&gameHistory[i][1], msg.buffer+20+i*16, 4);
|
---|
928 | memcpy(&gameHistory[i][2], msg.buffer+24+i*16, 4);
|
---|
929 | memcpy(&gameHistory[i][3], msg.buffer+28+i*16, 4);
|
---|
930 |
|
---|
931 | cout << "result: " << gameHistory[i][0] << endl;
|
---|
932 | cout << "team: " << gameHistory[i][1] << endl;
|
---|
933 | cout << "blue score: " << gameHistory[i][2] << endl;
|
---|
934 | cout << "red score: " << gameHistory[i][3] << endl;
|
---|
935 | }
|
---|
936 |
|
---|
937 | wndCurrent = wndProfile;
|
---|
938 |
|
---|
939 | break;
|
---|
940 | }
|
---|
941 | case MSG_TYPE_JOIN_GAME_SUCCESS:
|
---|
942 | {
|
---|
943 | cout << "Received a JOIN_GAME_SUCCESS message" << endl;
|
---|
944 |
|
---|
945 | string gameName(msg.buffer);
|
---|
946 |
|
---|
947 | #if defined WINDOWS
|
---|
948 | game = new Game(gameName, "../../data/map.txt", &msgProcessor);
|
---|
949 | #elif defined LINUX
|
---|
950 | game = new Game(gameName, "../data/map.txt", &msgProcessor);
|
---|
951 | #elif defined MAC
|
---|
952 | game = new Game(gameName, "../data/map.txt", &msgProcessor);
|
---|
953 | #endif
|
---|
954 |
|
---|
955 | cout << "Game name: " << gameName << endl;
|
---|
956 |
|
---|
957 | state = STATE_GAME_LOBBY;
|
---|
958 | wndCurrent = wndGameLobby;
|
---|
959 |
|
---|
960 | msgTo.type = MSG_TYPE_JOIN_GAME_ACK;
|
---|
961 | strcpy(msgTo.buffer, gameName.c_str());
|
---|
962 |
|
---|
963 | msgProcessor.sendMessage(&msgTo, &server);
|
---|
964 |
|
---|
965 | break;
|
---|
966 | }
|
---|
967 | case MSG_TYPE_JOIN_GAME_FAILURE:
|
---|
968 | {
|
---|
969 | cout << "Received a JOIN_GAME_FAILURE message" << endl;
|
---|
970 |
|
---|
971 | break;
|
---|
972 | }
|
---|
973 | case MSG_TYPE_PLAYER:
|
---|
974 | {
|
---|
975 | handleMsgPlayer(msg, mapPlayers, mapGames);
|
---|
976 |
|
---|
977 | break;
|
---|
978 | }
|
---|
979 | case MSG_TYPE_GAME_INFO:
|
---|
980 | {
|
---|
981 | handleMsgGameInfo(msg, mapPlayers, mapGames);
|
---|
982 |
|
---|
983 | break;
|
---|
984 | }
|
---|
985 | default:
|
---|
986 | {
|
---|
987 | cout << "(STATE_LOBBY) Received invlaid message of type " << msg.type << endl;
|
---|
988 |
|
---|
989 | break;
|
---|
990 | }
|
---|
991 | }
|
---|
992 |
|
---|
993 | break;
|
---|
994 | }
|
---|
995 | case STATE_GAME_LOBBY:
|
---|
996 | {
|
---|
997 | cout << "(STATE_GAME_LOBBY) ";
|
---|
998 | switch(msg.type)
|
---|
999 | {
|
---|
1000 | case MSG_TYPE_START_GAME:
|
---|
1001 | {
|
---|
1002 | state = STATE_GAME;
|
---|
1003 | wndCurrent = wndGame;
|
---|
1004 |
|
---|
1005 | break;
|
---|
1006 | }
|
---|
1007 | default:
|
---|
1008 | {
|
---|
1009 | // keep these lines commented until until the correct messages are moved into the STATE_GAME_LOBBY section
|
---|
1010 | //cout << "Received invalid message of type " << msg.type << endl;
|
---|
1011 |
|
---|
1012 | //break;
|
---|
1013 | }
|
---|
1014 | }
|
---|
1015 | }
|
---|
1016 | case STATE_GAME:
|
---|
1017 | {
|
---|
1018 | cout << "(STATE_GAME) ";
|
---|
1019 | switch(msg.type)
|
---|
1020 | {
|
---|
1021 | case MSG_TYPE_SCORE:
|
---|
1022 | {
|
---|
1023 | cout << "Received SCORE message!" << endl;
|
---|
1024 |
|
---|
1025 | int blueScore;
|
---|
1026 | memcpy(&blueScore, msg.buffer, 4);
|
---|
1027 | cout << "blue score: " << blueScore << endl;
|
---|
1028 | game->setBlueScore(blueScore);
|
---|
1029 |
|
---|
1030 | int redScore;
|
---|
1031 | memcpy(&redScore, msg.buffer+4, 4);
|
---|
1032 | cout << "red score: " << redScore << endl;
|
---|
1033 | game->setRedScore(redScore);
|
---|
1034 |
|
---|
1035 | cout << "Processed SCORE message!" << endl;
|
---|
1036 |
|
---|
1037 | break;
|
---|
1038 | }
|
---|
1039 | case MSG_TYPE_FINISH_GAME:
|
---|
1040 | {
|
---|
1041 | cout << "Got a finish game message" << endl;
|
---|
1042 | cout << "Should switch to STATE_LOBBY and show the final score" << endl;
|
---|
1043 |
|
---|
1044 | unsigned int winner, blueScore, redScore;
|
---|
1045 | memcpy(&winner, msg.buffer, 4);
|
---|
1046 | memcpy(&blueScore, msg.buffer+4, 4);
|
---|
1047 | memcpy(&redScore, msg.buffer+8, 4);
|
---|
1048 |
|
---|
1049 | string gameName(msg.buffer+12);
|
---|
1050 |
|
---|
1051 | gameSummary = new GameSummary(gameName, winner, blueScore, redScore);
|
---|
1052 |
|
---|
1053 | delete game;
|
---|
1054 | game = NULL;
|
---|
1055 | state = STATE_LOBBY;
|
---|
1056 | wndCurrent = wndGameSummary;
|
---|
1057 |
|
---|
1058 | break;
|
---|
1059 | }
|
---|
1060 | case MSG_TYPE_LOGOUT:
|
---|
1061 | {
|
---|
1062 | cout << "Got a logout message" << endl;
|
---|
1063 |
|
---|
1064 | unsigned int playerId;
|
---|
1065 |
|
---|
1066 | // Check if it's about you or another player
|
---|
1067 | memcpy(&playerId, msg.buffer, 4);
|
---|
1068 | response = string(msg.buffer+4);
|
---|
1069 |
|
---|
1070 | if (playerId == curPlayerId)
|
---|
1071 | cout << "Received MSG_TYPE_LOGOUT for self in STATE_GAME. This shouldn't happen." << endl;
|
---|
1072 | else {
|
---|
1073 | delete mapPlayers[playerId];
|
---|
1074 | mapPlayers.erase(playerId);
|
---|
1075 | }
|
---|
1076 |
|
---|
1077 | break;
|
---|
1078 | }
|
---|
1079 | case MSG_TYPE_PLAYER_JOIN_GAME:
|
---|
1080 | {
|
---|
1081 | cout << "Received MSG_TYPE_PLAYER_JOIN_GAME" << endl;
|
---|
1082 |
|
---|
1083 | Player p("", "");
|
---|
1084 | p.deserialize(msg.buffer);
|
---|
1085 | cout << "Deserialized player" << endl;
|
---|
1086 | cout << "player team: " << p.team << endl;
|
---|
1087 | cout << "current player team: " << currentPlayer->team << endl;
|
---|
1088 | p.timeLastUpdated = getCurrentMillis();
|
---|
1089 | p.isChasing = false;
|
---|
1090 | if (p.health <= 0)
|
---|
1091 | p.isDead = true;
|
---|
1092 | else
|
---|
1093 | p.isDead = false;
|
---|
1094 |
|
---|
1095 | if (mapPlayers.find(p.getId()) != mapPlayers.end())
|
---|
1096 | *(mapPlayers[p.getId()]) = p;
|
---|
1097 | else
|
---|
1098 | mapPlayers[p.getId()] = new Player(p);
|
---|
1099 |
|
---|
1100 | game->addPlayer(mapPlayers[p.getId()]);
|
---|
1101 |
|
---|
1102 | break;
|
---|
1103 | }
|
---|
1104 | case MSG_TYPE_LEAVE_GAME:
|
---|
1105 | {
|
---|
1106 | cout << "Received a LEAVE_GAME message" << endl;
|
---|
1107 |
|
---|
1108 | string gameName(msg.buffer+4);
|
---|
1109 | unsigned int playerId;
|
---|
1110 |
|
---|
1111 | memcpy(&playerId, msg.buffer, 4);
|
---|
1112 |
|
---|
1113 | game->removePlayer(playerId);
|
---|
1114 |
|
---|
1115 | break;
|
---|
1116 | }
|
---|
1117 | case MSG_TYPE_PLAYER_MOVE:
|
---|
1118 | {
|
---|
1119 | cout << "Received PLAYER_MOVE message" << endl;
|
---|
1120 |
|
---|
1121 | unsigned int id;
|
---|
1122 | int x, y;
|
---|
1123 |
|
---|
1124 | memcpy(&id, msg.buffer, 4);
|
---|
1125 | memcpy(&x, msg.buffer+4, 4);
|
---|
1126 | memcpy(&y, msg.buffer+8, 4);
|
---|
1127 |
|
---|
1128 | cout << "id: " << id << endl;
|
---|
1129 |
|
---|
1130 | mapPlayers[id]->target.x = x;
|
---|
1131 | mapPlayers[id]->target.y = y;
|
---|
1132 |
|
---|
1133 | mapPlayers[id]->isChasing = false;
|
---|
1134 | mapPlayers[id]->setTargetPlayer(0);
|
---|
1135 |
|
---|
1136 | break;
|
---|
1137 | }
|
---|
1138 | case MSG_TYPE_OBJECT:
|
---|
1139 | {
|
---|
1140 | cout << "Received object message in STATE_GAME" << endl;
|
---|
1141 |
|
---|
1142 | WorldMap::Object o(0, OBJECT_NONE, 0, 0);
|
---|
1143 | o.deserialize(msg.buffer);
|
---|
1144 | cout << "object id: " << o.id << endl;
|
---|
1145 | game->getMap()->updateObject(o.id, o.type, o.pos.x, o.pos.y);
|
---|
1146 |
|
---|
1147 | break;
|
---|
1148 | }
|
---|
1149 | case MSG_TYPE_REMOVE_OBJECT:
|
---|
1150 | {
|
---|
1151 | cout << "Received REMOVE_OBJECT message!" << endl;
|
---|
1152 |
|
---|
1153 | int id;
|
---|
1154 | memcpy(&id, msg.buffer, 4);
|
---|
1155 |
|
---|
1156 | cout << "Removing object with id " << id << endl;
|
---|
1157 |
|
---|
1158 | if (!game->getMap()->removeObject(id))
|
---|
1159 | cout << "Did not remove the object" << endl;
|
---|
1160 |
|
---|
1161 | break;
|
---|
1162 | }
|
---|
1163 | case MSG_TYPE_ATTACK:
|
---|
1164 | {
|
---|
1165 | cout << "Received START_ATTACK message" << endl;
|
---|
1166 |
|
---|
1167 | unsigned int id, targetId;
|
---|
1168 | memcpy(&id, msg.buffer, 4);
|
---|
1169 | memcpy(&targetId, msg.buffer+4, 4);
|
---|
1170 |
|
---|
1171 | cout << "source id: " << id << endl;
|
---|
1172 | cout << "target id: " << targetId << endl;
|
---|
1173 |
|
---|
1174 | // need to check the target exists in the current game
|
---|
1175 | Player* source = game->getPlayers()[id];
|
---|
1176 | source->setTargetPlayer(targetId);
|
---|
1177 | source->isChasing = true;
|
---|
1178 |
|
---|
1179 | break;
|
---|
1180 | }
|
---|
1181 | case MSG_TYPE_PROJECTILE:
|
---|
1182 | {
|
---|
1183 | cout << "Received a PROJECTILE message" << endl;
|
---|
1184 |
|
---|
1185 | unsigned int projId, x, y, targetId;
|
---|
1186 |
|
---|
1187 | memcpy(&projId, msg.buffer, 4);
|
---|
1188 | memcpy(&x, msg.buffer+4, 4);
|
---|
1189 | memcpy(&y, msg.buffer+8, 4);
|
---|
1190 | memcpy(&targetId, msg.buffer+12, 4);
|
---|
1191 |
|
---|
1192 | cout << "projId: " << projId << endl;
|
---|
1193 | cout << "x: " << x << endl;
|
---|
1194 | cout << "y: " << y << endl;
|
---|
1195 | cout << "Target: " << targetId << endl;
|
---|
1196 |
|
---|
1197 | Projectile proj(x, y, targetId, 0);
|
---|
1198 | proj.setId(projId);
|
---|
1199 |
|
---|
1200 | game->addProjectile(proj);
|
---|
1201 |
|
---|
1202 | break;
|
---|
1203 | }
|
---|
1204 | case MSG_TYPE_REMOVE_PROJECTILE:
|
---|
1205 | {
|
---|
1206 | cout << "Received a REMOVE_PROJECTILE message" << endl;
|
---|
1207 |
|
---|
1208 | unsigned int id;
|
---|
1209 | memcpy(&id, msg.buffer, 4);
|
---|
1210 |
|
---|
1211 | game->removeProjectile(id);
|
---|
1212 |
|
---|
1213 | break;
|
---|
1214 | }
|
---|
1215 | case MSG_TYPE_PLAYER:
|
---|
1216 | {
|
---|
1217 | handleMsgPlayer(msg, mapPlayers, mapGames);
|
---|
1218 |
|
---|
1219 | break;
|
---|
1220 | }
|
---|
1221 | case MSG_TYPE_GAME_INFO:
|
---|
1222 | {
|
---|
1223 | handleMsgGameInfo(msg, mapPlayers, mapGames);
|
---|
1224 |
|
---|
1225 | break;
|
---|
1226 | }
|
---|
1227 | default:
|
---|
1228 | {
|
---|
1229 | cout << "Received invalid message of type " << msg.type << endl;
|
---|
1230 |
|
---|
1231 | break;
|
---|
1232 | }
|
---|
1233 | }
|
---|
1234 |
|
---|
1235 | break;
|
---|
1236 | }
|
---|
1237 | default:
|
---|
1238 | {
|
---|
1239 | cout << "The state has an invalid value: " << state << endl;
|
---|
1240 |
|
---|
1241 | break;
|
---|
1242 | }
|
---|
1243 | }
|
---|
1244 | }
|
---|
1245 |
|
---|
1246 | int getRefreshRate(int width, int height)
|
---|
1247 | {
|
---|
1248 | int numRefreshRates = al_get_num_display_modes();
|
---|
1249 | ALLEGRO_DISPLAY_MODE displayMode;
|
---|
1250 |
|
---|
1251 | for(int i=0; i<numRefreshRates; i++) {
|
---|
1252 | al_get_display_mode(i, &displayMode);
|
---|
1253 |
|
---|
1254 | if (displayMode.width == width && displayMode.height == height)
|
---|
1255 | return displayMode.refresh_rate;
|
---|
1256 | }
|
---|
1257 |
|
---|
1258 | return 0;
|
---|
1259 | }
|
---|
1260 |
|
---|
1261 | void drawMessageStatus(ALLEGRO_FONT* font)
|
---|
1262 | {
|
---|
1263 | int clientMsgOffset = 5;
|
---|
1264 | int serverMsgOffset = 950;
|
---|
1265 |
|
---|
1266 | al_draw_text(font, al_map_rgb(0, 255, 255), 0+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
|
---|
1267 | al_draw_text(font, al_map_rgb(0, 255, 255), 20+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Type");
|
---|
1268 | al_draw_text(font, al_map_rgb(0, 255, 255), 240+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Acked?");
|
---|
1269 |
|
---|
1270 | //al_draw_text(font, al_map_rgb(0, 255, 255), serverMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
|
---|
1271 |
|
---|
1272 | map<unsigned int, map<unsigned long, MessageContainer> >& sentMessages = msgProcessor.getSentMessages();
|
---|
1273 | int id, type;
|
---|
1274 | bool acked;
|
---|
1275 | ostringstream ossId, ossAcked;
|
---|
1276 |
|
---|
1277 | map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
|
---|
1278 |
|
---|
1279 | int msgCount = 0;
|
---|
1280 | for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
|
---|
1281 | map<unsigned long, MessageContainer> playerMessage = it->second;
|
---|
1282 | map<unsigned long, MessageContainer>::iterator it2;
|
---|
1283 | for (it2 = playerMessage.begin(); it2 != playerMessage.end(); it2++) {
|
---|
1284 |
|
---|
1285 | id = it->first;
|
---|
1286 | ossId.str("");;
|
---|
1287 | ossId << id;
|
---|
1288 |
|
---|
1289 | type = it2->second.getMessage()->type;
|
---|
1290 | string typeStr = MessageContainer::getMsgTypeString(type);
|
---|
1291 |
|
---|
1292 | acked = it2->second.getAcked();
|
---|
1293 | ossAcked.str("");;
|
---|
1294 | ossAcked << boolalpha << acked;
|
---|
1295 |
|
---|
1296 | al_draw_text(font, al_map_rgb(0, 255, 0), clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
|
---|
1297 | al_draw_text(font, al_map_rgb(0, 255, 0), 20+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, typeStr.c_str());
|
---|
1298 | al_draw_text(font, al_map_rgb(0, 255, 0), 240+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossAcked.str().c_str());
|
---|
1299 |
|
---|
1300 | msgCount++;
|
---|
1301 | }
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | if (msgProcessor.getAckedMessages().size() > 0) {
|
---|
1305 | map<unsigned int, unsigned long long> ackedMessages = msgProcessor.getAckedMessages()[0];
|
---|
1306 | map<unsigned int, unsigned long long>::iterator it3;
|
---|
1307 |
|
---|
1308 | msgCount = 0;
|
---|
1309 | for (it3 = ackedMessages.begin(); it3 != ackedMessages.end(); it3++) {
|
---|
1310 | ossId.str("");;
|
---|
1311 | ossId << it3->first;
|
---|
1312 |
|
---|
1313 | al_draw_text(font, al_map_rgb(255, 0, 0), 25+serverMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
|
---|
1314 |
|
---|
1315 | msgCount++;
|
---|
1316 | }
|
---|
1317 | }
|
---|
1318 | }
|
---|
1319 |
|
---|
1320 | // message handling functions
|
---|
1321 |
|
---|
1322 | void handleMsgPlayer(NETWORK_MSG &msg, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames) {
|
---|
1323 | cout << "Received MSG_TYPE_PLAYER" << endl;
|
---|
1324 |
|
---|
1325 | Player p("", "");
|
---|
1326 | p.deserialize(msg.buffer);
|
---|
1327 | p.timeLastUpdated = getCurrentMillis();
|
---|
1328 | p.isChasing = false;
|
---|
1329 | if (p.health <= 0)
|
---|
1330 | p.isDead = true;
|
---|
1331 | else
|
---|
1332 | p.isDead = false;
|
---|
1333 |
|
---|
1334 | if (mapPlayers.find(p.getId()) != mapPlayers.end())
|
---|
1335 | *(mapPlayers[p.getId()]) = p;
|
---|
1336 | else
|
---|
1337 | mapPlayers[p.getId()] = new Player(p);
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | void handleMsgGameInfo(NETWORK_MSG &msg, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames) {
|
---|
1341 | cout << "Received a GAME_INFO message" << endl;
|
---|
1342 |
|
---|
1343 | string gameName(msg.buffer+4);
|
---|
1344 | int numPlayers;
|
---|
1345 |
|
---|
1346 | memcpy(&numPlayers, msg.buffer, 4);
|
---|
1347 |
|
---|
1348 | cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;
|
---|
1349 |
|
---|
1350 | if (numPlayers > 0)
|
---|
1351 | mapGames[gameName] = numPlayers;
|
---|
1352 | else
|
---|
1353 | mapGames.erase(gameName);
|
---|
1354 | }
|
---|
1355 |
|
---|
1356 | // Callback definitions
|
---|
1357 |
|
---|
1358 | void goToRegisterScreen()
|
---|
1359 | {
|
---|
1360 | txtUsernameRegister->clear();
|
---|
1361 | txtPasswordRegister->clear();
|
---|
1362 | lblRegisterStatus->setText("");
|
---|
1363 | rblClasses->setSelectedButton(-1);
|
---|
1364 |
|
---|
1365 | wndCurrent = wndRegister;
|
---|
1366 | }
|
---|
1367 |
|
---|
1368 | void goToLoginScreen()
|
---|
1369 | {
|
---|
1370 | txtUsername->clear();
|
---|
1371 | txtPassword->clear();
|
---|
1372 | lblLoginStatus->setText("");
|
---|
1373 |
|
---|
1374 | wndCurrent = wndLogin;
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 | // maybe need a goToGameScreen function as well and add state changes to these functions as well
|
---|
1378 |
|
---|
1379 | void registerAccount()
|
---|
1380 | {
|
---|
1381 | string username = txtUsernameRegister->getStr();
|
---|
1382 | string password = txtPasswordRegister->getStr();
|
---|
1383 |
|
---|
1384 | txtUsernameRegister->clear();
|
---|
1385 | txtPasswordRegister->clear();
|
---|
1386 | // maybe clear rblClasses as well (add a method to RadioButtonList to enable this)
|
---|
1387 |
|
---|
1388 | Player::PlayerClass playerClass;
|
---|
1389 |
|
---|
1390 | switch (rblClasses->getSelectedButton()) {
|
---|
1391 | case 0:
|
---|
1392 | playerClass = Player::CLASS_WARRIOR;
|
---|
1393 | break;
|
---|
1394 | case 1:
|
---|
1395 | playerClass = Player::CLASS_RANGER;
|
---|
1396 | break;
|
---|
1397 | default:
|
---|
1398 | cout << "Invalid class selection" << endl;
|
---|
1399 | playerClass = Player::CLASS_NONE;
|
---|
1400 | break;
|
---|
1401 | }
|
---|
1402 |
|
---|
1403 | msgTo.type = MSG_TYPE_REGISTER;
|
---|
1404 |
|
---|
1405 | strcpy(msgTo.buffer, username.c_str());
|
---|
1406 | strcpy(msgTo.buffer+username.size()+1, password.c_str());
|
---|
1407 | memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
|
---|
1408 |
|
---|
1409 | msgProcessor.sendMessage(&msgTo, &server);
|
---|
1410 | }
|
---|
1411 |
|
---|
1412 | void login()
|
---|
1413 | {
|
---|
1414 | string strUsername = txtUsername->getStr();
|
---|
1415 | string strPassword = txtPassword->getStr();
|
---|
1416 | username = strUsername;
|
---|
1417 |
|
---|
1418 | txtUsername->clear();
|
---|
1419 | txtPassword->clear();
|
---|
1420 |
|
---|
1421 | msgTo.type = MSG_TYPE_LOGIN;
|
---|
1422 |
|
---|
1423 | strcpy(msgTo.buffer, strUsername.c_str());
|
---|
1424 | strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
|
---|
1425 |
|
---|
1426 | msgProcessor.sendMessage(&msgTo, &server);
|
---|
1427 |
|
---|
1428 | state = STATE_LOBBY;
|
---|
1429 | }
|
---|
1430 |
|
---|
1431 | void logout()
|
---|
1432 | {
|
---|
1433 | switch(state) {
|
---|
1434 | case STATE_LOBBY:
|
---|
1435 | txtJoinGame->clear();
|
---|
1436 | txtCreateGame->clear();
|
---|
1437 | break;
|
---|
1438 | default:
|
---|
1439 | cout << "Logout called from invalid state: " << state << endl;
|
---|
1440 | break;
|
---|
1441 | }
|
---|
1442 |
|
---|
1443 | msgTo.type = MSG_TYPE_LOGOUT;
|
---|
1444 |
|
---|
1445 | strcpy(msgTo.buffer, username.c_str());
|
---|
1446 |
|
---|
1447 | msgProcessor.sendMessage(&msgTo, &server);
|
---|
1448 | }
|
---|
1449 |
|
---|
1450 | void quit()
|
---|
1451 | {
|
---|
1452 | doexit = true;
|
---|
1453 | }
|
---|
1454 |
|
---|
1455 | void sendChatMessage()
|
---|
1456 | {
|
---|
1457 | string msg = txtChat->getStr();
|
---|
1458 | txtChat->clear();
|
---|
1459 |
|
---|
1460 | msgTo.type = MSG_TYPE_CHAT;
|
---|
1461 | strcpy(msgTo.buffer, msg.c_str());
|
---|
1462 |
|
---|
1463 | msgProcessor.sendMessage(&msgTo, &server);
|
---|
1464 | }
|
---|
1465 |
|
---|
1466 | void toggleDebugging()
|
---|
1467 | {
|
---|
1468 | debugging = !debugging;
|
---|
1469 | }
|
---|
1470 |
|
---|
1471 | void goToProfileScreen()
|
---|
1472 | {
|
---|
1473 | msgTo.type = MSG_TYPE_PROFILE;
|
---|
1474 |
|
---|
1475 | unsigned int playerId = currentPlayer->getId();
|
---|
1476 | memcpy(msgTo.buffer, &playerId, 4);
|
---|
1477 | msgProcessor.sendMessage(&msgTo, &server);
|
---|
1478 | }
|
---|
1479 |
|
---|
1480 | void goToLobbyScreen()
|
---|
1481 | {
|
---|
1482 | wndCurrent = wndLobby;
|
---|
1483 | }
|
---|
1484 |
|
---|
1485 | void joinGame()
|
---|
1486 | {
|
---|
1487 | cout << "Joining game" << endl;
|
---|
1488 |
|
---|
1489 | string msg = txtJoinGame->getStr();
|
---|
1490 | txtJoinGame->clear();
|
---|
1491 |
|
---|
1492 | msgTo.type = MSG_TYPE_JOIN_GAME;
|
---|
1493 | strcpy(msgTo.buffer, msg.c_str());
|
---|
1494 |
|
---|
1495 | msgProcessor.sendMessage(&msgTo, &server);
|
---|
1496 | }
|
---|
1497 |
|
---|
1498 | void createGame()
|
---|
1499 | {
|
---|
1500 | cout << "Creating game" << endl;
|
---|
1501 |
|
---|
1502 | string msg = txtCreateGame->getStr();
|
---|
1503 | txtCreateGame->clear();
|
---|
1504 |
|
---|
1505 | cout << "Sending message: " << msg.c_str() << endl;
|
---|
1506 |
|
---|
1507 | msgTo.type = MSG_TYPE_CREATE_GAME;
|
---|
1508 | strcpy(msgTo.buffer, msg.c_str());
|
---|
1509 |
|
---|
1510 | msgProcessor.sendMessage(&msgTo, &server);
|
---|
1511 | cout << "Sent CREATE_GAME message" << endl;
|
---|
1512 | }
|
---|
1513 |
|
---|
1514 | void joinWaitingArea() {
|
---|
1515 | cout << "joining waiting area" << endl;
|
---|
1516 | currentPlayer->team = 0;
|
---|
1517 |
|
---|
1518 | msgTo.type = MSG_TYPE_JOIN_TEAM;
|
---|
1519 | memcpy(msgTo.buffer, &(currentPlayer->team), 4);
|
---|
1520 |
|
---|
1521 | msgProcessor.sendMessage(&msgTo, &server);
|
---|
1522 | }
|
---|
1523 |
|
---|
1524 | void joinBlueTeam() {
|
---|
1525 | cout << "joining blue team" << endl;
|
---|
1526 | currentPlayer->team = 1;
|
---|
1527 |
|
---|
1528 | msgTo.type = MSG_TYPE_JOIN_TEAM;
|
---|
1529 | memcpy(msgTo.buffer, &(currentPlayer->team), 4);
|
---|
1530 |
|
---|
1531 | msgProcessor.sendMessage(&msgTo, &server);
|
---|
1532 | }
|
---|
1533 |
|
---|
1534 | void joinRedTeam() {
|
---|
1535 | cout << "joining red team" << endl;
|
---|
1536 | currentPlayer->team = 2;
|
---|
1537 |
|
---|
1538 | msgTo.type = MSG_TYPE_JOIN_TEAM;
|
---|
1539 | memcpy(msgTo.buffer, &(currentPlayer->team), 4);
|
---|
1540 |
|
---|
1541 | msgProcessor.sendMessage(&msgTo, &server);
|
---|
1542 | }
|
---|
1543 |
|
---|
1544 | void startGame() {
|
---|
1545 | msgTo.type = MSG_TYPE_START_GAME;
|
---|
1546 |
|
---|
1547 | msgProcessor.sendMessage(&msgTo, &server);
|
---|
1548 | }
|
---|
1549 |
|
---|
1550 | void leaveGame()
|
---|
1551 | {
|
---|
1552 | cout << "Leaving game" << endl;
|
---|
1553 |
|
---|
1554 | delete game;
|
---|
1555 | game = NULL;
|
---|
1556 |
|
---|
1557 | state = STATE_LOBBY;
|
---|
1558 | wndCurrent = wndLobby;
|
---|
1559 |
|
---|
1560 | msgTo.type = MSG_TYPE_LEAVE_GAME;
|
---|
1561 |
|
---|
1562 | msgProcessor.sendMessage(&msgTo, &server);
|
---|
1563 | }
|
---|
1564 |
|
---|
1565 | void closeGameSummary()
|
---|
1566 | {
|
---|
1567 | delete gameSummary;
|
---|
1568 | gameSummary = NULL;
|
---|
1569 | wndCurrent = wndLobby;
|
---|
1570 | cout << "Processed button actions" << endl;
|
---|
1571 | }
|
---|