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

Last change on this file since 29fdf12 was 257de75, checked in by dportnoy <dmp1488@…>, 11 years ago

Removed some debug messages from the client

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