source: network-game/client/Client/main.cpp@ cdb0e98

Last change on this file since cdb0e98 was b28e2bf, checked in by dportnoy <dmp1488@…>, 11 years ago

Client processes server PROFILE message and displays the player's honor points and game history on the profile page

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