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

Last change on this file since 306758e was 306758e, checked in by Dmitry Portnoy <dmp1488@…>, 10 years ago

Remove the serverSide argument from Game::addPlayer() since it's no longer useful

  • Property mode set to 100644
File size: 48.7 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
[4c00935]144int honorPoints, wins, losses, numGames;
[b28e2bf]145int** gameHistory;
146
[10f6fc2]147MessageProcessor msgProcessor;
148
[d352805]149int main(int argc, char **argv)
150{
151 ALLEGRO_DISPLAY *display = NULL;
152 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
153 ALLEGRO_TIMER *timer = NULL;
[e6c26b8]154 map<unsigned int, Player*> mapPlayers;
[6f64166]155 map<string, int> mapGames;
[88cdae2]156 unsigned int curPlayerId = -1;
[68d94de]157 ofstream outputLog;
158
[803566d]159 doexit = false;
[b35b2b2]160 debugging = false;
[803566d]161 bool redraw = true;
162 bool fullscreen = false;
163 game = NULL;
[3e44a59]164 gameSummary = NULL;
[15efb4e]165
[b28e2bf]166 honorPoints = 0;
[4c00935]167 wins = 0;
168 losses = 0;
[b28e2bf]169 numGames = 0;
170 gameHistory = NULL;
171
[87b3ee2]172 state = STATE_START;
[9a3e6b1]173
[d352805]174 if(!al_init()) {
175 fprintf(stderr, "failed to initialize allegro!\n");
176 return -1;
177 }
178
[8271c78]179 outputLog.open("client.log", ios::app);
180 outputLog << "Started client on " << getCurrentDateTimeString() << endl;
181
[88cdae2]182 if (al_init_primitives_addon())
183 cout << "Primitives initialized" << endl;
184 else
185 cout << "Primitives not initialized" << endl;
186
[d352805]187 al_init_font_addon();
188 al_init_ttf_addon();
189
[b29ff6b]190 ALLEGRO_FONT* font;
[88cdae2]191 #if defined WINDOWS
[b29ff6b]192 font = al_load_ttf_font("../pirulen.ttf", 12, 0);
[88cdae2]193 #elif defined LINUX
[b29ff6b]194 font = al_load_ttf_font("pirulen.ttf", 12, 0);
[34bd549]195 #elif defined MAC
196 font = al_load_ttf_font("pirulen.ttf", 12, 0);
[88cdae2]197 #endif
198
[d352805]199 if (!font) {
200 fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
201 getchar();
[803566d]202 return -1;
[d352805]203 }
204
205 if(!al_install_keyboard()) {
206 fprintf(stderr, "failed to initialize the keyboard!\n");
207 return -1;
208 }
[87b3ee2]209
210 if(!al_install_mouse()) {
211 fprintf(stderr, "failed to initialize the mouse!\n");
212 return -1;
213 }
[d352805]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
[1f1eb58]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);
[d352805]228 if(!display) {
229 fprintf(stderr, "failed to create display!\n");
230 al_destroy_timer(timer);
231 return -1;
232 }
[87b3ee2]233
[b35b2b2]234 debugConsole.addLine("Debug console:");
235 debugConsole.addLine("");
236
[b29ff6b]237 createGui(font);
[3e44a59]238
[49da01a]239 goToLoginScreen();
[d352805]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());
[87b3ee2]254 al_register_event_source(event_queue, al_get_mouse_event_source());
[d352805]255
256 al_clear_to_color(al_map_rgb(0,0,0));
257
258 al_flip_display();
[9a3e6b1]259
260 if (argc != 3) {
261 cout << "Usage: server port" << endl;
262 exit(1);
263 }
264
265 initWinSock();
[803566d]266
[9a3e6b1]267 sock = socket(AF_INET, SOCK_DGRAM, 0);
268 if (sock < 0)
269 error("socket");
270
[e607c0f]271 set_nonblock(sock);
272
[9a3e6b1]273 server.sin_family = AF_INET;
274 hp = gethostbyname(argv[1]);
[9c18cb7]275 if (hp == 0)
[9a3e6b1]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
[68d94de]281 msgProcessor = MessageProcessor(sock, &outputLog);
282
[d352805]283 al_start_timer(timer);
[e607c0f]284
[883bb5d]285 while (!doexit)
[d352805]286 {
287 ALLEGRO_EVENT ev;
[0b6f9ec]288
[d352805]289 al_wait_for_event(event_queue, &ev);
[87b3ee2]290
291 if(wndCurrent->handleEvent(ev)) {
292 // do nothing
293 }
294 else if(ev.type == ALLEGRO_EVENT_TIMER) {
[883bb5d]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 }
[d352805]301 }
302 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
[9a3e6b1]303 doexit = true;
[d352805]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;
[4926168]312 case ALLEGRO_KEY_S: // pickup an item next to you
[e0fd377]313 if (state == STATE_GAME) {
[4926168]314 msgTo.type = MSG_TYPE_PICKUP_FLAG;
315 memcpy(msgTo.buffer, &curPlayerId, 4);
[68d94de]316 msgProcessor.sendMessage(&msgTo, &server);
[4926168]317 }
318 break;
[626e5b0]319 case ALLEGRO_KEY_D: // drop the current item
[e0fd377]320 if (state == STATE_GAME) {
[6c9bcdd]321 try {
[5c7f28d]322 Player* p = mapPlayers.at(curPlayerId);
[f66d04f]323 int flagType = OBJECT_NONE;
[626e5b0]324
325 if (p->hasBlueFlag)
[f66d04f]326 flagType = OBJECT_BLUE_FLAG;
[626e5b0]327 else if (p->hasRedFlag)
[f66d04f]328 flagType = OBJECT_RED_FLAG;
[626e5b0]329
[f66d04f]330 if (flagType != OBJECT_NONE) {
[626e5b0]331 msgTo.type = MSG_TYPE_DROP_FLAG;
332 memcpy(msgTo.buffer, &curPlayerId, 4);
[68d94de]333 msgProcessor.sendMessage(&msgTo, &server);
[626e5b0]334 }
[5c7f28d]335 } catch (const out_of_range& ex) {}
[626e5b0]336 }
337 break;
[d352805]338 }
339 }
[88cdae2]340 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
[a0ce8a3]341 if (wndCurrent == wndGame) {
[e1f78f5]342 if (ev.mouse.button == 1) { // left click
343 msgTo.type = MSG_TYPE_PLAYER_MOVE;
[88cdae2]344
[e1f78f5]345 POSITION pos;
346 pos.x = ev.mouse.x;
347 pos.y = ev.mouse.y;
348 pos = screenToMap(pos);
[62ee2ce]349
[e1f78f5]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
[68d94de]356 msgProcessor.sendMessage(&msgTo, &server);
[e1f78f5]357 }
358 else
359 cout << "Invalid point: User did not click on the map" << endl;
360 }else if (ev.mouse.button == 2) { // right click
[b8abc90]361 cout << "Detected a right-click" << endl;
362 map<unsigned int, Player*>::iterator it;
[e1f78f5]363
[cbc70eb]364 Player* curPlayer = mapPlayers[curPlayerId];;
[fbcfc35]365
[b8abc90]366 cout << "Got current player" << endl;
367 cout << "current game: " << game << endl;
[e1f78f5]368
[b8abc90]369 map<unsigned int, Player*> playersInGame = game->getPlayers();
370 Player* target;
371
[a0ce8a3]372 for (it = playersInGame.begin(); it != playersInGame.end(); it++)
[b8abc90]373 {
374 target = it->second;
375 cout << "set target" << endl;
[e70b66b]376 if (target->team != curPlayer->team)
[e1f78f5]377 {
[b8abc90]378 cout << "Found valid target" << endl;
[88cdae2]379
[e70b66b]380 POSITION cursorPos;
381 cursorPos.x = ev.mouse.x;
382 cursorPos.y = ev.mouse.y;
383 cursorPos = screenToMap(cursorPos);
[5b92307]384
[e70b66b]385 float distance =posDistance(cursorPos.toFloat(), target->pos);
[b8abc90]386
[e70b66b]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 }
[e1f78f5]396 }
[b8abc90]397 }
[62ee2ce]398 }
[ad5d122]399 }
[88cdae2]400 }
[e607c0f]401
[68d94de]402 if (msgProcessor.receiveMessage(&msgFrom, &from) >= 0)
[6f64166]403 processMessage(msgFrom, state, chatConsole, mapPlayers, mapGames, curPlayerId);
[054b50b]404
[a1a3bd5]405 if (redraw)
[e607c0f]406 {
[d352805]407 redraw = false;
[88cdae2]408
[68d94de]409 msgProcessor.resendUnackedMessages();
[10f6fc2]410
[f63aa57]411 if (debugging && wndCurrent == wndLobby)
412 wndLobbyDebug->draw(display);
[b35b2b2]413 else
414 wndCurrent->draw(display);
[9a3e6b1]415
[50e6c7a]416 if (wndCurrent == wndLobby) {
[f63aa57]417 if (!debugging)
418 chatConsole.draw(font, al_map_rgb(255,255,255));
[53d41ea]419
[11ad6fb]420 al_draw_text(font, al_map_rgb(0, 255, 0), SCREEN_W*1/2-100, 120, ALLEGRO_ALIGN_LEFT, "Current Games");
421
[321fbbc]422 map<string, int>::iterator it;
[2ee386d]423 int i=0;
[11ad6fb]424 ostringstream oss;
[2ee386d]425 for (it = mapGames.begin(); it != mapGames.end(); it++) {
[11ad6fb]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("");
[2ee386d]442 i++;
[50e6c7a]443 }
444 }
[fd9cdb5]445 else if (wndCurrent == wndProfile)
446 {
[b28e2bf]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("");
[fd9cdb5]452
[4c00935]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
[81c4e8a]458 oss << "Losses: " << losses << endl;
[4c00935]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
[fd9cdb5]463 // display records of the last 10 games
[b28e2bf]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
[4c00935]471 al_draw_text(font, al_map_rgb(0, 255, 0), 142, 190+30*(i+1), ALLEGRO_ALIGN_CENTRE, oss.str().c_str());
[b28e2bf]472 oss.clear();
473 oss.str("");
474
475 oss << gameHistory[i][2] << endl;
[4c00935]476 al_draw_text(font, al_map_rgb(0, 255, 0), 302, 190+30*(i+1), ALLEGRO_ALIGN_CENTRE, oss.str().c_str());
[b28e2bf]477 oss.clear();
478 oss.str("");
479
480 oss << gameHistory[i][3] << endl;
[4c00935]481 al_draw_text(font, al_map_rgb(0, 255, 0), 462, 190+30*(i+1), ALLEGRO_ALIGN_CENTRE, oss.str().c_str());
[b28e2bf]482 oss.clear();
483 oss.str("");
484
485 oss << "6/11/2014, 5:12 PM" << endl;
[4c00935]486 al_draw_text(font, al_map_rgb(0, 255, 0), 622, 190+30*(i+1), ALLEGRO_ALIGN_CENTRE, oss.str().c_str());
[b28e2bf]487 oss.clear();
488 oss.str("");
489
[fd9cdb5]490 }
491
492 }
[a0ce8a3]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++) {
[3476207]506 switch (itPlayers->second->team) {
507 case -1:
508 drawPosition = 200;
509 break;
510 case 0:
511 drawPosition = 400;
512 break;
513 case 1:
514 drawPosition = 600;
515 break;
516 }
517
[a0ce8a3]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 }
[3ff2bd7]525 else if (wndCurrent == wndGame)
[03ba5e3]526 {
[b4c5b6a]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
[3ff2bd7]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 }
[b4c5b6a]539 }
540
[03ba5e3]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());
[0693e25]548
[fef7c69]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
[58ca135]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
[6319311]567 GameRender::drawMap(game->getMap());
568 GameRender::drawPlayers(game->getPlayers(), font, curPlayerId);
[e5697b1]569 GameRender::drawProjectiles(game->getProjectiles(), game->getPlayers());
[03ba5e3]570 }
[f63aa57]571 else if (wndCurrent == wndGameSummary)
[50e6c7a]572 {
[635ad9b]573 ostringstream ossBlueScore, ossRedScore;
574
575 ossBlueScore << "Blue Score: " << gameSummary->getBlueScore();
576 ossRedScore << "Red Score: " << gameSummary->getRedScore();
577
[3e44a59]578 string strWinner;
579
580 if (gameSummary->getWinner() == 0)
[635ad9b]581 strWinner = "Blue Team Wins";
[3e44a59]582 else if (gameSummary->getWinner() == 1)
[635ad9b]583 strWinner = "Red Team Wins";
584 else
585 strWinner = "winner set to wrong value";
586
[3e44a59]587 al_draw_text(font, al_map_rgb(0, 255, 0), 512, 40, ALLEGRO_ALIGN_CENTRE, gameSummary->getName().c_str());
[635ad9b]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());
[3e44a59]590 al_draw_text(font, al_map_rgb(0, 255, 0), 512, 120, ALLEGRO_ALIGN_CENTRE, strWinner.c_str());
[87b3ee2]591 }
592
[b35b2b2]593 if (debugging) {
594 drawMessageStatus(font);
595 }
596
[d352805]597 al_flip_display();
598 }
599 }
[9a3e6b1]600
601 #if defined WINDOWS
602 closesocket(sock);
603 #elif defined LINUX
604 close(sock);
605 #endif
606
607 shutdownWinSock();
[d352805]608
[f63aa57]609 // delete all components
610 for (unsigned int x=0; x<vctComponents.size(); x++)
611 delete vctComponents[x];
612
[87b3ee2]613 delete wndLogin;
[1785314]614 delete wndRegister;
615 delete wndLobby;
[f63aa57]616 delete wndLobbyDebug;
[a0ce8a3]617 delete wndGameLobby;
[3ff2bd7]618 delete wndGame;
[f63aa57]619 delete wndGameSummary;
[87b3ee2]620
[eb2ad4f]621 // game should be deleted when the player leaves a gamw
[d519032]622 if (game != NULL)
623 delete game;
[62ee2ce]624
[3e44a59]625 if (gameSummary != NULL)
626 delete gameSummary;
627
[e6c26b8]628 map<unsigned int, Player*>::iterator it;
629
630 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
631 delete it->second;
632 }
633
[b28e2bf]634 if (gameHistory != NULL) {
635 for (int i=0; i<numGames; i++) {
636 free(gameHistory[i]);
637 }
638
639 free(gameHistory);
640 }
641
[d352805]642 al_destroy_event_queue(event_queue);
643 al_destroy_display(display);
644 al_destroy_timer(timer);
[8271c78]645
646 outputLog << "Stopped client on " << getCurrentDateTimeString() << endl;
647 outputLog.close();
648
[d352805]649 return 0;
650}
651
[0dde5da]652void 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);
[803566d]661
[0dde5da]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
670void shutdownWinSock()
671{
672#if defined WINDOWS
673 WSACleanup();
674#endif
[1912323]675}
676
[b29ff6b]677void createGui(ALLEGRO_FONT* font) {
[fd9cdb5]678
[b29ff6b]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);
[fd9cdb5]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)));
[b29ff6b]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);
[fd9cdb5]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)));
[b29ff6b]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
[fd9cdb5]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)));
[4c00935]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)));
[fd9cdb5]777 vctComponents.push_back(wndProfile->addComponent(new Button(920, 738, 80, 20, font, "Back", goToLobbyScreen)));
778
779
[a0ce8a3]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
[b29ff6b]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
[6f64166]803void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames, unsigned int& curPlayerId)
[1912323]804{
[dfb9363]805 cout << "Total players in map: " << mapPlayers.size() << endl;
806
[53ba300]807 // this is outdated since most messages now don't contain just a text string
[4da5aa3]808 string response = string(msg.buffer);
809
810 switch(state)
811 {
812 case STATE_START:
813 {
[e607c0f]814 cout << "In STATE_START" << endl;
815
[87b3ee2]816 switch(msg.type)
[4da5aa3]817 {
[87b3ee2]818 case MSG_TYPE_REGISTER:
819 {
[365e156]820 lblRegisterStatus->setText(response);
[87b3ee2]821 break;
822 }
[bc70282]823 default:
824 {
825 cout << "(STATE_REGISTER) Received invalid message of type " << msg.type << endl;
826 break;
827 }
828 }
829
830 break;
831 }
[1785314]832 case STATE_LOBBY:
[bc70282]833 {
[e0fd377]834 cout << "In STATE_LOBBY" << endl;
[bc70282]835 switch(msg.type)
836 {
[87b3ee2]837 case MSG_TYPE_LOGIN:
838 {
839 if (response.compare("Player has already logged in.") == 0)
840 {
[bc70282]841 goToLoginScreen();
842 state = STATE_START;
843
[365e156]844 lblLoginStatus->setText(response);
[87b3ee2]845 }
846 else if (response.compare("Incorrect username or password") == 0)
847 {
[bc70282]848 goToLoginScreen();
849 state = STATE_START;
850
[365e156]851 lblLoginStatus->setText(response);
[87b3ee2]852 }
853 else
854 {
[1785314]855 wndCurrent = wndLobby;
[95ffe57]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);
[e6c26b8]861
[5b92307]862 if (mapPlayers.find(p->getId()) != mapPlayers.end())
863 delete mapPlayers[p->getId()];
864 mapPlayers[p->getId()] = p;
865 curPlayerId = p->getId();
[a0ce8a3]866 currentPlayer = mapPlayers[curPlayerId];
[88cdae2]867
868 cout << "Got a valid login response with the player" << endl;
[1f1eb58]869 cout << "Player id: " << curPlayerId << endl;
[95ffe57]870 cout << "Player health: " << p->health << endl;
[bc70282]871 cout << "player map size: " << mapPlayers.size() << endl;
[87b3ee2]872 }
[88cdae2]873
[a1a3bd5]874 break;
875 }
876 case MSG_TYPE_LOGOUT:
877 {
[054b50b]878 cout << "Got a logout message" << endl;
[a1a3bd5]879
[1e250bf]880 unsigned int playerId;
[53ba300]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)
[87b3ee2]887 {
[dfb9363]888 cout << "Got logout message for self" << endl;
889
[53ba300]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];
[dfb9363]902 mapPlayers.erase(playerId);
[87b3ee2]903 }
[054b50b]904
905 break;
906 }
[eb8adb1]907 case MSG_TYPE_CHAT:
908 {
909 chatConsole.addLine(response);
[4c202e0]910
[87b3ee2]911 break;
912 }
[fd9cdb5]913 case MSG_TYPE_PROFILE:
914 {
[b28e2bf]915 memcpy(&honorPoints, msg.buffer, 4);
[4c00935]916 memcpy(&wins, msg.buffer+4, 4);
917 memcpy(&losses, msg.buffer+8, 4);
918 memcpy(&numGames, msg.buffer+12, 4);
[b28e2bf]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
[4c00935]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);
[b28e2bf]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
[fd9cdb5]937 wndCurrent = wndProfile;
938
939 break;
940 }
[d519032]941 case MSG_TYPE_JOIN_GAME_SUCCESS:
942 {
943 cout << "Received a JOIN_GAME_SUCCESS message" << endl;
944
[8aed9c0]945 string gameName(msg.buffer);
946
947 #if defined WINDOWS
[1f6233e]948 game = new Game(gameName, "../../data/map.txt", &msgProcessor);
[8aed9c0]949 #elif defined LINUX
[1f6233e]950 game = new Game(gameName, "../data/map.txt", &msgProcessor);
[34bd549]951 #elif defined MAC
952 game = new Game(gameName, "../data/map.txt", &msgProcessor);
[8aed9c0]953 #endif
954
[03ba5e3]955 cout << "Game name: " << gameName << endl;
[d519032]956
[a0ce8a3]957 state = STATE_GAME_LOBBY;
958 wndCurrent = wndGameLobby;
[d519032]959
960 msgTo.type = MSG_TYPE_JOIN_GAME_ACK;
961 strcpy(msgTo.buffer, gameName.c_str());
962
[68d94de]963 msgProcessor.sendMessage(&msgTo, &server);
[d519032]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 }
[6f64166]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 }
[45b2750]985 default:
986 {
[1785314]987 cout << "(STATE_LOBBY) Received invlaid message of type " << msg.type << endl;
[50e6c7a]988
[365e156]989 break;
[45b2750]990 }
[4da5aa3]991 }
[eb8adb1]992
[4da5aa3]993 break;
994 }
[a0ce8a3]995 case STATE_GAME_LOBBY:
[cf05729]996 {
[a0ce8a3]997 cout << "(STATE_GAME_LOBBY) ";
[cf05729]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 }
[e0fd377]1016 case STATE_GAME:
[803566d]1017 {
[e0fd377]1018 cout << "(STATE_GAME) ";
[803566d]1019 switch(msg.type)
1020 {
[d6b5f74]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 }
[3e44a59]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;
[635ad9b]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
[3e44a59]1051 gameSummary = new GameSummary(gameName, winner, blueScore, redScore);
1052
1053 delete game;
1054 game = NULL;
1055 state = STATE_LOBBY;
1056 wndCurrent = wndGameSummary;
1057
[31b347a]1058 break;
[6012178]1059 }
[dfb9363]1060 case MSG_TYPE_LOGOUT:
[5c7f28d]1061 {
1062 cout << "Got a logout message" << endl;
1063
[8df0c49]1064 unsigned int playerId;
[5c7f28d]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;
[dfb9363]1072 else {
[5c7f28d]1073 delete mapPlayers[playerId];
[dfb9363]1074 mapPlayers.erase(playerId);
1075 }
[5c7f28d]1076
1077 break;
1078 }
[6012178]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);
[5b92307]1085 cout << "Deserialized player" << endl;
[3476207]1086 cout << "player team: " << p.team << endl;
1087 cout << "current player team: " << currentPlayer->team << endl;
[6012178]1088 p.timeLastUpdated = getCurrentMillis();
1089 p.isChasing = false;
1090 if (p.health <= 0)
1091 p.isDead = true;
1092 else
1093 p.isDead = false;
1094
[5b92307]1095 if (mapPlayers.find(p.getId()) != mapPlayers.end())
1096 *(mapPlayers[p.getId()]) = p;
[6012178]1097 else
[5b92307]1098 mapPlayers[p.getId()] = new Player(p);
[b4c5b6a]1099
[306758e]1100 game->addPlayer(mapPlayers[p.getId()]);
[b4c5b6a]1101
1102 break;
1103 }
[cd80d63]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 }
[fef7c69]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
[1ee0ffa]1133 mapPlayers[id]->isChasing = false;
1134 mapPlayers[id]->setTargetPlayer(0);
1135
[fef7c69]1136 break;
1137 }
[803566d]1138 case MSG_TYPE_OBJECT:
1139 {
[e0fd377]1140 cout << "Received object message in STATE_GAME" << endl;
[803566d]1141
[f66d04f]1142 WorldMap::Object o(0, OBJECT_NONE, 0, 0);
[803566d]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 {
[d519032]1151 cout << "Received REMOVE_OBJECT message!" << endl;
[803566d]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 }
[b8abc90]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];
[5b92307]1176 source->setTargetPlayer(targetId);
[b8abc90]1177 source->isChasing = true;
1178
1179 break;
1180 }
[58ca135]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 }
[6f64166]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 }
[803566d]1227 default:
1228 {
[d519032]1229 cout << "Received invalid message of type " << msg.type << endl;
[803566d]1230
1231 break;
1232 }
1233 }
1234
1235 break;
1236 }
[4da5aa3]1237 default:
1238 {
1239 cout << "The state has an invalid value: " << state << endl;
1240
1241 break;
1242 }
1243 }
[0dde5da]1244}
[87b3ee2]1245
[929b4e0]1246int 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
1261void 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
[3ff2bd7]1270 //al_draw_text(font, al_map_rgb(0, 255, 255), serverMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
[929b4e0]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
[6f64166]1320// message handling functions
1321
1322void 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
1340void 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
[929b4e0]1356// Callback definitions
1357
[5c95436]1358void goToRegisterScreen()
1359{
1360 txtUsernameRegister->clear();
1361 txtPasswordRegister->clear();
[49da01a]1362 lblRegisterStatus->setText("");
1363 rblClasses->setSelectedButton(-1);
1364
1365 wndCurrent = wndRegister;
[5c95436]1366}
1367
1368void goToLoginScreen()
[87b3ee2]1369{
1370 txtUsername->clear();
1371 txtPassword->clear();
[49da01a]1372 lblLoginStatus->setText("");
1373
1374 wndCurrent = wndLogin;
[5c95436]1375}
1376
[bc70282]1377// maybe need a goToGameScreen function as well and add state changes to these functions as well
1378
[5c95436]1379void 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 }
[87b3ee2]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());
[5c95436]1407 memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
[87b3ee2]1408
[68d94de]1409 msgProcessor.sendMessage(&msgTo, &server);
[87b3ee2]1410}
1411
1412void 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
[68d94de]1426 msgProcessor.sendMessage(&msgTo, &server);
[bc70282]1427
[1785314]1428 state = STATE_LOBBY;
[87b3ee2]1429}
1430
1431void logout()
1432{
[929b4e0]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 }
[87b3ee2]1442
1443 msgTo.type = MSG_TYPE_LOGOUT;
1444
1445 strcpy(msgTo.buffer, username.c_str());
1446
[68d94de]1447 msgProcessor.sendMessage(&msgTo, &server);
[87b3ee2]1448}
1449
1450void quit()
1451{
1452 doexit = true;
1453}
1454
1455void 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
[68d94de]1463 msgProcessor.sendMessage(&msgTo, &server);
[87b3ee2]1464}
[1f1eb58]1465
[b35b2b2]1466void toggleDebugging()
1467{
1468 debugging = !debugging;
1469}
1470
[fd9cdb5]1471void goToProfileScreen()
1472{
1473 msgTo.type = MSG_TYPE_PROFILE;
1474
[4c00935]1475 unsigned int playerId = currentPlayer->getId();
1476 memcpy(msgTo.buffer, &playerId, 4);
[fd9cdb5]1477 msgProcessor.sendMessage(&msgTo, &server);
1478}
1479
1480void goToLobbyScreen()
1481{
1482 wndCurrent = wndLobby;
1483}
1484
[929b4e0]1485void joinGame()
[b35b2b2]1486{
[929b4e0]1487 cout << "Joining game" << endl;
[bbebe9c]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
[68d94de]1495 msgProcessor.sendMessage(&msgTo, &server);
[dee75cc]1496}
[b35b2b2]1497
[929b4e0]1498void createGame()
[b35b2b2]1499{
[929b4e0]1500 cout << "Creating game" << endl;
[bbebe9c]1501
1502 string msg = txtCreateGame->getStr();
1503 txtCreateGame->clear();
1504
[d519032]1505 cout << "Sending message: " << msg.c_str() << endl;
1506
[bbebe9c]1507 msgTo.type = MSG_TYPE_CREATE_GAME;
1508 strcpy(msgTo.buffer, msg.c_str());
1509
[68d94de]1510 msgProcessor.sendMessage(&msgTo, &server);
[34bd549]1511 cout << "Sent CREATE_GAME message" << endl;
[03ba5e3]1512}
1513
[a0ce8a3]1514void joinWaitingArea() {
1515 cout << "joining waiting area" << endl;
1516 currentPlayer->team = -1;
[3476207]1517
1518 msgTo.type = MSG_TYPE_JOIN_TEAM;
1519 memcpy(msgTo.buffer, &(currentPlayer->team), 4);
1520
1521 msgProcessor.sendMessage(&msgTo, &server);
[a0ce8a3]1522}
1523
1524void joinBlueTeam() {
1525 cout << "joining blue team" << endl;
1526 currentPlayer->team = 0;
[3476207]1527
1528 msgTo.type = MSG_TYPE_JOIN_TEAM;
1529 memcpy(msgTo.buffer, &(currentPlayer->team), 4);
1530
1531 msgProcessor.sendMessage(&msgTo, &server);
[a0ce8a3]1532}
1533
1534void joinRedTeam() {
1535 cout << "joining red team" << endl;
1536 currentPlayer->team = 1;
[3476207]1537
1538 msgTo.type = MSG_TYPE_JOIN_TEAM;
1539 memcpy(msgTo.buffer, &(currentPlayer->team), 4);
1540
1541 msgProcessor.sendMessage(&msgTo, &server);
[a0ce8a3]1542}
1543
1544void startGame() {
[cf05729]1545 msgTo.type = MSG_TYPE_START_GAME;
[cb5a021]1546
1547 msgProcessor.sendMessage(&msgTo, &server);
[a0ce8a3]1548}
1549
[03ba5e3]1550void leaveGame()
1551{
1552 cout << "Leaving game" << endl;
1553
[8826eed]1554 delete game;
[03ba5e3]1555 game = NULL;
1556
1557 state = STATE_LOBBY;
1558 wndCurrent = wndLobby;
1559
1560 msgTo.type = MSG_TYPE_LEAVE_GAME;
1561
[68d94de]1562 msgProcessor.sendMessage(&msgTo, &server);
[95ffe57]1563}
[3e44a59]1564
1565void closeGameSummary()
1566{
1567 delete gameSummary;
[635ad9b]1568 gameSummary = NULL;
[3e44a59]1569 wndCurrent = wndLobby;
[635ad9b]1570 cout << "Processed button actions" << endl;
[8aed9c0]1571}
Note: See TracBrowser for help on using the repository browser.