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

Last change on this file since f15d6a9 was 3476207, checked in by dportnoy <dmp1488@…>, 10 years ago

Client shows all players in the game lobby and sends messages to the server when the client player switches teams

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