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

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

A small bit of client code uses curPlayerId instead of searching through the list of players to get the current one

  • Property mode set to 100644
File size: 43.4 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
[cbc70eb]419 Player* curPlayer = mapPlayers[curPlayerId];;
[fbcfc35]420
[b8abc90]421 cout << "Got current player" << endl;
422 cout << "current game: " << game << endl;
[e1f78f5]423
[b8abc90]424 map<unsigned int, Player*> playersInGame = game->getPlayers();
425 Player* target;
426
427 for(it = playersInGame.begin(); it != playersInGame.end(); it++)
428 {
429 // need to check if the right-click was actually on this player
430 // right now, this code will target all players other than the current one
431 target = it->second;
432 cout << "set target" << endl;
433 if (target->id != curPlayerId && target->team != curPlayer->team)
[e1f78f5]434 {
[b8abc90]435 cout << "Found valid target" << endl;
[88cdae2]436
[b8abc90]437 msgTo.type = MSG_TYPE_START_ATTACK;
438 memcpy(msgTo.buffer, &curPlayerId, 4);
439 memcpy(msgTo.buffer+4, &target->id, 4);
440
[68d94de]441 msgProcessor.sendMessage(&msgTo, &server);
[e1f78f5]442 }
[b8abc90]443 }
[62ee2ce]444 }
[ad5d122]445 }
[88cdae2]446 }
[e607c0f]447
[68d94de]448 if (msgProcessor.receiveMessage(&msgFrom, &from) >= 0)
[e0fd377]449 processMessage(msgFrom, state, chatConsole, gameMap, mapPlayers, mapProjectiles, curPlayerId);
[054b50b]450
[a1a3bd5]451 if (redraw)
[e607c0f]452 {
[d352805]453 redraw = false;
[88cdae2]454
[68d94de]455 msgProcessor.resendUnackedMessages();
[10f6fc2]456
[1785314]457 if (debugging && wndCurrent == wndGame)
458 wndGameDebug->draw(display);
[b35b2b2]459 else
460 wndCurrent->draw(display);
[9a3e6b1]461
[50e6c7a]462 if (wndCurrent == wndLobby) {
[53d41ea]463 chatConsole.draw(font, al_map_rgb(255,255,255));
464
[321fbbc]465 map<string, int>::iterator it;
[2ee386d]466 int i=0;
[2992b1a]467 ostringstream ossGame;
[2ee386d]468 for (it = mapGames.begin(); it != mapGames.end(); it++) {
[321fbbc]469 ossGame << it->first << " (" << it->second << " players)" << endl;
[b4c5b6a]470 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]471 ossGame.clear();
472 ossGame.str("");
[2ee386d]473 i++;
[50e6c7a]474 }
475 }
[03ba5e3]476 else if (wndCurrent == wndNewGame)
477 {
[b4c5b6a]478 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 4, ALLEGRO_ALIGN_LEFT, "Players");
479
480 map<unsigned int, Player*>& gamePlayers = game->getPlayers();
481 map<unsigned int, Player*>::iterator it;
482
483 int playerCount = 0;
484 for (it = gamePlayers.begin(); it != gamePlayers.end(); it++)
485 {
486 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 19+(playerCount+1)*15, ALLEGRO_ALIGN_LEFT, it->second->name.c_str());
487 playerCount++;
488 }
489
[03ba5e3]490 ostringstream ossScoreBlue, ossScoreRed;
491
492 ossScoreBlue << "Blue: " << game->getBlueScore() << endl;
493 ossScoreRed << "Red: " << game->getRedScore() << endl;
494
495 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
496 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
[0693e25]497
[fef7c69]498 // update players
499 for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
500 {
501 it->second->updateTarget(game->getPlayers());
502 }
503
504 for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
505 {
506 it->second->move(game->getMap()); // ignore return value
507 }
508
[58ca135]509 // update projectile positions
510 map<unsigned int, Projectile>::iterator it2;
511 for (it2 = game->getProjectiles().begin(); it2 != game->getProjectiles().end(); it2++)
512 {
513 it2->second.move(game->getPlayers());
514 }
515
[6319311]516 GameRender::drawMap(game->getMap());
517 GameRender::drawPlayers(game->getPlayers(), font, curPlayerId);
[58ca135]518
519 // draw projectiles
520 for (it2 = game->getProjectiles().begin(); it2 != game->getProjectiles().end(); it2++)
521 {
522 Projectile proj = it2->second;
523
524 FLOAT_POSITION target = game->getPlayers()[proj.target]->pos;
525 float angle = atan2(target.y-proj.pos.toFloat().y, target.x-proj.pos.toFloat().x);
526
527 POSITION start, end;
528 start.x = cos(angle)*15+proj.pos.x;
529 start.y = sin(angle)*15+proj.pos.y;
530 end.x = proj.pos.x;
531 end.y = proj.pos.y;
532
533 start = mapToScreen(start);
534 end = mapToScreen(end);
535
536 al_draw_line(start.x, start.y, end.x, end.y, al_map_rgb(0, 0, 0), 4);
537 }
[03ba5e3]538 }
[50e6c7a]539 else if (wndCurrent == wndGame)
540 {
[b35b2b2]541 if (!debugging)
542 chatConsole.draw(font, al_map_rgb(255,255,255));
[bc70282]543
[87b3ee2]544 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
[62ee2ce]545
[15efb4e]546 ostringstream ossScoreBlue, ossScoreRed;
547
[e0fd377]548 ossScoreBlue << "Blue: " << game->getBlueScore() << endl;
549 ossScoreRed << "Red: " << game->getRedScore() << endl;
[15efb4e]550
551 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
552 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
553
[032e550]554 // update players
[e6c26b8]555 map<unsigned int, Player*>::iterator it;
[032e550]556 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
557 {
[e6c26b8]558 it->second->updateTarget(mapPlayers);
[032e550]559 }
560
[a1a3bd5]561 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
562 {
[e6c26b8]563 it->second->move(gameMap); // ignore return value
[a1a3bd5]564 }
565
[8c74150]566 // update projectile positions
567 map<unsigned int, Projectile>::iterator it2;
568 for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
569 {
570 it2->second.move(mapPlayers);
571 }
572
[6319311]573 GameRender::drawMap(gameMap);
574 GameRender::drawPlayers(mapPlayers, font, curPlayerId);
[8c74150]575
576 // draw projectiles
577 for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
578 {
579 Projectile proj = it2->second;
580
[e6c26b8]581 FLOAT_POSITION target = mapPlayers[proj.target]->pos;
[8c74150]582 float angle = atan2(target.y-proj.pos.toFloat().y, target.x-proj.pos.toFloat().x);
583
584 POSITION start, end;
585 start.x = cos(angle)*15+proj.pos.x;
586 start.y = sin(angle)*15+proj.pos.y;
587 end.x = proj.pos.x;
588 end.y = proj.pos.y;
589
590 start = mapToScreen(start);
591 end = mapToScreen(end);
592
593 al_draw_line(start.x, start.y, end.x, end.y, al_map_rgb(0, 0, 0), 4);
594 }
[3e44a59]595 }else if (wndCurrent == wndGameSummary) {
[635ad9b]596 ostringstream ossBlueScore, ossRedScore;
597
598 ossBlueScore << "Blue Score: " << gameSummary->getBlueScore();
599 ossRedScore << "Red Score: " << gameSummary->getRedScore();
600
[3e44a59]601 string strWinner;
602
603 if (gameSummary->getWinner() == 0)
[635ad9b]604 strWinner = "Blue Team Wins";
[3e44a59]605 else if (gameSummary->getWinner() == 1)
[635ad9b]606 strWinner = "Red Team Wins";
607 else
608 strWinner = "winner set to wrong value";
609
[3e44a59]610 al_draw_text(font, al_map_rgb(0, 255, 0), 512, 40, ALLEGRO_ALIGN_CENTRE, gameSummary->getName().c_str());
[635ad9b]611 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossBlueScore.str().c_str());
612 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossRedScore.str().c_str());
[3e44a59]613 al_draw_text(font, al_map_rgb(0, 255, 0), 512, 120, ALLEGRO_ALIGN_CENTRE, strWinner.c_str());
[87b3ee2]614 }
615
[b35b2b2]616 if (debugging) {
617 //debugConsole.draw(font, al_map_rgb(255,255,255));
618 drawMessageStatus(font);
619 }
620
[d352805]621 al_flip_display();
622 }
623 }
[9a3e6b1]624
625 #if defined WINDOWS
626 closesocket(sock);
627 #elif defined LINUX
628 close(sock);
629 #endif
630
631 shutdownWinSock();
[d352805]632
[87b3ee2]633 delete wndLogin;
[1785314]634 delete wndRegister;
635 delete wndLobby;
636 delete wndGame;
637 delete wndGameDebug;
[87b3ee2]638
[62ee2ce]639 delete gameMap;
[d519032]640
641 if (game != NULL)
642 delete game;
[62ee2ce]643
[3e44a59]644 if (gameSummary != NULL)
645 delete gameSummary;
646
[e6c26b8]647 map<unsigned int, Player*>::iterator it;
648
649 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
650 delete it->second;
651 }
652
[d352805]653 al_destroy_event_queue(event_queue);
654 al_destroy_display(display);
655 al_destroy_timer(timer);
[8271c78]656
657 outputLog << "Stopped client on " << getCurrentDateTimeString() << endl;
658 outputLog.close();
659
[d352805]660 return 0;
661}
662
[0dde5da]663void initWinSock()
664{
665#if defined WINDOWS
666 WORD wVersionRequested;
667 WSADATA wsaData;
668 int wsaerr;
669
670 wVersionRequested = MAKEWORD(2, 2);
671 wsaerr = WSAStartup(wVersionRequested, &wsaData);
[803566d]672
[0dde5da]673 if (wsaerr != 0) {
674 cout << "The Winsock dll not found." << endl;
675 exit(1);
676 }else
677 cout << "The Winsock dll was found." << endl;
678#endif
679}
680
681void shutdownWinSock()
682{
683#if defined WINDOWS
684 WSACleanup();
685#endif
[1912323]686}
687
[3e44a59]688void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, WorldMap *gameMap, map<unsigned int, Player*>& mapPlayers,
[e0fd377]689 map<unsigned int, Projectile>& mapProjectiles, unsigned int& curPlayerId)
[1912323]690{
[53ba300]691 // this is outdated since most messages now don't contain just a text string
[4da5aa3]692 string response = string(msg.buffer);
693
694 switch(state)
695 {
696 case STATE_START:
697 {
[e607c0f]698 cout << "In STATE_START" << endl;
699
[87b3ee2]700 switch(msg.type)
[4da5aa3]701 {
[87b3ee2]702 case MSG_TYPE_REGISTER:
703 {
[365e156]704 lblRegisterStatus->setText(response);
[87b3ee2]705 break;
706 }
[bc70282]707 default:
708 {
709 cout << "(STATE_REGISTER) Received invalid message of type " << msg.type << endl;
710 break;
711 }
712 }
713
714 break;
715 }
[1785314]716 case STATE_LOBBY:
[bc70282]717 {
[e0fd377]718 cout << "In STATE_LOBBY" << endl;
[bc70282]719 switch(msg.type)
720 {
[87b3ee2]721 case MSG_TYPE_LOGIN:
722 {
723 if (response.compare("Player has already logged in.") == 0)
724 {
[bc70282]725 goToLoginScreen();
726 state = STATE_START;
727
[365e156]728 lblLoginStatus->setText(response);
[87b3ee2]729 }
730 else if (response.compare("Incorrect username or password") == 0)
731 {
[bc70282]732 goToLoginScreen();
733 state = STATE_START;
734
[365e156]735 lblLoginStatus->setText(response);
[87b3ee2]736 }
737 else
738 {
[1785314]739 wndCurrent = wndLobby;
[95ffe57]740
741 // this message should only be sent when a player first logs in so they know their id
742
743 Player* p = new Player("", "");
744 p->deserialize(msg.buffer);
[e6c26b8]745
[95ffe57]746 if (mapPlayers.find(p->id) != mapPlayers.end())
747 delete mapPlayers[p->id];
748 mapPlayers[p->id] = p;
749 curPlayerId = p->id;
[88cdae2]750
751 cout << "Got a valid login response with the player" << endl;
[1f1eb58]752 cout << "Player id: " << curPlayerId << endl;
[95ffe57]753 cout << "Player health: " << p->health << endl;
[bc70282]754 cout << "player map size: " << mapPlayers.size() << endl;
[87b3ee2]755 }
[88cdae2]756
[a1a3bd5]757 break;
758 }
759 case MSG_TYPE_LOGOUT:
760 {
[054b50b]761 cout << "Got a logout message" << endl;
[a1a3bd5]762
[53ba300]763 int playerId;
764
765 // Check if it's about you or another player
766 memcpy(&playerId, msg.buffer, 4);
767 response = string(msg.buffer+4);
768
769 if (playerId == curPlayerId)
[87b3ee2]770 {
[53ba300]771 if (response.compare("You have successfully logged out.") == 0)
772 {
773 cout << "Logged out" << endl;
774 state = STATE_START;
775 goToLoginScreen();
776 }
777
778 // if there was an error logging out, nothing happens
779 }
780 else
781 {
782 delete mapPlayers[playerId];
[87b3ee2]783 }
[054b50b]784
785 break;
786 }
[4c202e0]787 case MSG_TYPE_PLAYER:
788 {
[1f1eb58]789 cout << "Received MSG_TYPE_PLAYER" << endl;
790
[31b347a]791 Player p("", "");
792 p.deserialize(msg.buffer);
793 p.timeLastUpdated = getCurrentMillis();
794 p.isChasing = false;
795 if (p.health <= 0)
796 p.isDead = true;
[5a5f131]797 else
[31b347a]798 p.isDead = false;
[5a5f131]799
[31b347a]800 if (mapPlayers.find(p.id) != mapPlayers.end())
801 *(mapPlayers[p.id]) = p;
802 else
803 mapPlayers[p.id] = new Player(p);
[4c202e0]804
[eb8adb1]805 break;
806 }
[a1a3bd5]807 case MSG_TYPE_PLAYER_MOVE:
808 {
809 unsigned int id;
810 int x, y;
811
812 memcpy(&id, msg.buffer, 4);
813 memcpy(&x, msg.buffer+4, 4);
814 memcpy(&y, msg.buffer+8, 4);
815
[e6c26b8]816 mapPlayers[id]->target.x = x;
817 mapPlayers[id]->target.y = y;
[a1a3bd5]818
819 break;
820 }
[eb8adb1]821 case MSG_TYPE_CHAT:
822 {
823 chatConsole.addLine(response);
[4c202e0]824
[87b3ee2]825 break;
826 }
[45b2750]827 case MSG_TYPE_OBJECT:
828 {
[d519032]829 cout << "Received OBJECT message" << endl;
[45b2750]830
831 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
832 o.deserialize(msg.buffer);
[bc70282]833 cout << "object id: " << o.id << endl;
[45b2750]834 gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
835
836 break;
837 }
[2df63d6]838 case MSG_TYPE_REMOVE_OBJECT:
839 {
[7511a2b]840 cout << "Received REMOVE_OBJECT message!" << endl;
841
[2df63d6]842 int id;
843 memcpy(&id, msg.buffer, 4);
[7511a2b]844
845 cout << "Removing object with id " << id << endl;
846
[2df63d6]847 if (!gameMap->removeObject(id))
848 cout << "Did not remove the object" << endl;
[7511a2b]849
850 break;
[2df63d6]851 }
[b978503]852 case MSG_TYPE_ATTACK:
853 {
[032e550]854 cout << "Received ATTACK message" << endl;
855
856 break;
857 }
858 case MSG_TYPE_START_ATTACK:
859 {
860 cout << "Received START_ATTACK message" << endl;
861
862 unsigned int id, targetID;
863 memcpy(&id, msg.buffer, 4);
864 memcpy(&targetID, msg.buffer+4, 4);
865
866 cout << "source id: " << id << endl;
867 cout << "target id: " << targetID << endl;
868
[e6c26b8]869 Player* source = mapPlayers[id];
[032e550]870 source->targetPlayer = targetID;
871 source->isChasing = true;
872
[b978503]873 break;
874 }
875 case MSG_TYPE_PROJECTILE:
876 {
[8c74150]877 cout << "Received a PROJECTILE message" << endl;
[fbcfc35]878
[50e6c7a]879 unsigned int id, x, y, targetId;
[fbcfc35]880
881 memcpy(&id, msg.buffer, 4);
882 memcpy(&x, msg.buffer+4, 4);
883 memcpy(&y, msg.buffer+8, 4);
884 memcpy(&targetId, msg.buffer+12, 4);
885
[8c74150]886 cout << "id: " << id << endl;
887 cout << "x: " << x << endl;
888 cout << "y: " << y << endl;
889 cout << "Target: " << targetId << endl;
890
891 Projectile proj(x, y, targetId, 0);
892 proj.setId(id);
893
894 mapProjectiles[id] = proj;
[fbcfc35]895
[b978503]896 break;
897 }
898 case MSG_TYPE_REMOVE_PROJECTILE:
899 {
[8c74150]900 cout << "Received a REMOVE_PROJECTILE message" << endl;
901
902 int id;
903 memcpy(&id, msg.buffer, 4);
904
905 mapProjectiles.erase(id);
906
[b978503]907 break;
908 }
[50e6c7a]909 case MSG_TYPE_GAME_INFO:
910 {
[803566d]911 cout << "Received a GAME_INFO message" << endl;
[50e6c7a]912
[2ee386d]913 string gameName(msg.buffer+4);
[50e6c7a]914 int numPlayers;
915
916 memcpy(&numPlayers, msg.buffer, 4);
917
[2ee386d]918 cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;
919
[e103b51]920 if (numPlayers > 0)
921 mapGames[gameName] = numPlayers;
922 else
923 mapGames.erase(gameName);
[50e6c7a]924
925 break;
926 }
[d519032]927 case MSG_TYPE_JOIN_GAME_SUCCESS:
928 {
929 cout << "Received a JOIN_GAME_SUCCESS message" << endl;
930
[03ba5e3]931 string gameName(msg.buffer);
[233e736]932 game = new Game(gameName, "../../data/map.txt");
[03ba5e3]933 cout << "Game name: " << gameName << endl;
[d519032]934
[e0fd377]935 state = STATE_GAME;
[03ba5e3]936 wndCurrent = wndNewGame;
[d519032]937
938 msgTo.type = MSG_TYPE_JOIN_GAME_ACK;
939 strcpy(msgTo.buffer, gameName.c_str());
940
[68d94de]941 msgProcessor.sendMessage(&msgTo, &server);
[d519032]942
943 break;
944 }
945 case MSG_TYPE_JOIN_GAME_FAILURE:
946 {
947 cout << "Received a JOIN_GAME_FAILURE message" << endl;
948
949 break;
950 }
[45b2750]951 default:
952 {
[1785314]953 cout << "(STATE_LOBBY) Received invlaid message of type " << msg.type << endl;
[50e6c7a]954
[365e156]955 break;
[45b2750]956 }
[4da5aa3]957 }
[eb8adb1]958
[4da5aa3]959 break;
960 }
[e0fd377]961 case STATE_GAME:
[803566d]962 {
[e0fd377]963 cout << "(STATE_GAME) ";
[803566d]964 switch(msg.type)
965 {
966 case MSG_TYPE_GAME_INFO:
967 {
[d519032]968 cout << "Received a GAME_INFO message" << endl;
[803566d]969
970 string gameName(msg.buffer+4);
971 int numPlayers;
972
973 memcpy(&numPlayers, msg.buffer, 4);
974
975 cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;
976
[e103b51]977 if (numPlayers > 0)
978 mapGames[gameName] = numPlayers;
979 else
980 mapGames.erase(gameName);
[803566d]981
982 break;
983 }
[3e44a59]984 case MSG_TYPE_SCORE:
985 {
986 cout << "Received SCORE message!" << endl;
987
988 int blueScore;
989 memcpy(&blueScore, msg.buffer, 4);
990 cout << "blue score: " << blueScore << endl;
991 game->setBlueScore(blueScore);
992
993 int redScore;
994 memcpy(&redScore, msg.buffer+4, 4);
995 cout << "red score: " << redScore << endl;
996 game->setRedScore(redScore);
997
998 cout << "Processed SCORE message!" << endl;
999
1000 break;
1001 }
1002 case MSG_TYPE_FINISH_GAME:
1003 {
1004 cout << "Got a finish game message" << endl;
1005 cout << "Should switch to STATE_LOBBY and show the final score" << endl;
1006
1007 unsigned int winner, blueScore, redScore;
[635ad9b]1008 memcpy(&winner, msg.buffer, 4);
1009 memcpy(&blueScore, msg.buffer+4, 4);
1010 memcpy(&redScore, msg.buffer+8, 4);
1011
1012 string gameName(msg.buffer+12);
1013
1014 cout << "winner: " << winner << endl;
1015 cout << "blueScore: " << blueScore << endl;
1016 cout << "redScore: " << redScore << endl;
1017 cout << "gameName: " << gameName << endl;
[3e44a59]1018
1019 gameSummary = new GameSummary(gameName, winner, blueScore, redScore);
1020
1021 delete game;
1022 game = NULL;
1023 state = STATE_LOBBY;
1024 wndCurrent = wndGameSummary;
1025
[635ad9b]1026
1027 cout << "winner from obj: " << gameSummary->getWinner() << endl;
1028 cout << "blueScore from obj: " << gameSummary->getBlueScore() << endl;
1029 cout << "redScore from obj: " << gameSummary->getRedScore() << endl;
1030 cout << "gameName from obj: " << gameSummary->getName() << endl;
[3e44a59]1031 break;
1032 }
[b4c5b6a]1033 case MSG_TYPE_PLAYER:
1034 {
1035 cout << "Received MSG_TYPE_PLAYER" << endl;
1036
[31b347a]1037 Player p("", "");
1038 p.deserialize(msg.buffer);
1039 p.timeLastUpdated = getCurrentMillis();
1040 p.isChasing = false;
1041 if (p.health <= 0)
1042 p.isDead = true;
1043 else
1044 p.isDead = false;
1045
1046 if (mapPlayers.find(p.id) != mapPlayers.end())
1047 *(mapPlayers[p.id]) = p;
[b4c5b6a]1048 else
[31b347a]1049 mapPlayers[p.id] = new Player(p);
[b4c5b6a]1050
[31b347a]1051 break;
[6012178]1052 }
1053 case MSG_TYPE_PLAYER_JOIN_GAME:
1054 {
1055 cout << "Received MSG_TYPE_PLAYER_JOIN_GAME" << endl;
1056
1057 Player p("", "");
1058 p.deserialize(msg.buffer);
1059 p.timeLastUpdated = getCurrentMillis();
1060 p.isChasing = false;
1061 if (p.health <= 0)
1062 p.isDead = true;
1063 else
1064 p.isDead = false;
1065
1066 if (mapPlayers.find(p.id) != mapPlayers.end())
1067 *(mapPlayers[p.id]) = p;
1068 else
1069 mapPlayers[p.id] = new Player(p);
[b4c5b6a]1070
[31b347a]1071 game->addPlayer(mapPlayers[p.id]);
[b4c5b6a]1072
1073 break;
1074 }
[cd80d63]1075 case MSG_TYPE_LEAVE_GAME:
1076 {
1077 cout << "Received a LEAVE_GAME message" << endl;
1078
1079 string gameName(msg.buffer+4);
1080 unsigned int playerId;
1081
1082 memcpy(&playerId, msg.buffer, 4);
1083
1084 game->removePlayer(playerId);
1085
1086 break;
1087 }
[fef7c69]1088 case MSG_TYPE_PLAYER_MOVE:
1089 {
1090 cout << "Received PLAYER_MOVE message" << endl;
1091
1092 unsigned int id;
1093 int x, y;
1094
1095 memcpy(&id, msg.buffer, 4);
1096 memcpy(&x, msg.buffer+4, 4);
1097 memcpy(&y, msg.buffer+8, 4);
1098
1099 cout << "id: " << id << endl;
1100
1101 mapPlayers[id]->target.x = x;
1102 mapPlayers[id]->target.y = y;
1103
1104 break;
1105 }
[803566d]1106 case MSG_TYPE_OBJECT:
1107 {
[e0fd377]1108 cout << "Received object message in STATE_GAME" << endl;
[803566d]1109
1110 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
1111 o.deserialize(msg.buffer);
1112 cout << "object id: " << o.id << endl;
1113 game->getMap()->updateObject(o.id, o.type, o.pos.x, o.pos.y);
1114
1115 break;
1116 }
1117 case MSG_TYPE_REMOVE_OBJECT:
1118 {
[d519032]1119 cout << "Received REMOVE_OBJECT message!" << endl;
[803566d]1120
1121 int id;
1122 memcpy(&id, msg.buffer, 4);
1123
1124 cout << "Removing object with id " << id << endl;
1125
1126 if (!game->getMap()->removeObject(id))
1127 cout << "Did not remove the object" << endl;
1128
1129 break;
1130 }
[b8abc90]1131 case MSG_TYPE_ATTACK:
1132 {
1133 cout << "Received ATTACK message" << endl;
1134
1135 break;
1136 }
1137 case MSG_TYPE_START_ATTACK:
1138 {
1139 cout << "Received START_ATTACK message" << endl;
1140
1141 unsigned int id, targetId;
1142 memcpy(&id, msg.buffer, 4);
1143 memcpy(&targetId, msg.buffer+4, 4);
1144
1145 cout << "source id: " << id << endl;
1146 cout << "target id: " << targetId << endl;
1147
1148 // need to check the target exists in the current game
1149 Player* source = game->getPlayers()[id];
1150 source->targetPlayer = targetId;
1151 source->isChasing = true;
1152
1153 break;
1154 }
[58ca135]1155 case MSG_TYPE_PROJECTILE:
1156 {
1157 cout << "Received a PROJECTILE message" << endl;
1158
1159 unsigned int projId, x, y, targetId;
1160
1161 memcpy(&projId, msg.buffer, 4);
1162 memcpy(&x, msg.buffer+4, 4);
1163 memcpy(&y, msg.buffer+8, 4);
1164 memcpy(&targetId, msg.buffer+12, 4);
1165
1166 cout << "projId: " << projId << endl;
1167 cout << "x: " << x << endl;
1168 cout << "y: " << y << endl;
1169 cout << "Target: " << targetId << endl;
1170
1171 Projectile proj(x, y, targetId, 0);
1172 proj.setId(projId);
1173
1174 game->addProjectile(proj);
1175
1176 break;
1177 }
1178 case MSG_TYPE_REMOVE_PROJECTILE:
1179 {
1180 cout << "Received a REMOVE_PROJECTILE message" << endl;
1181
1182 unsigned int id;
1183 memcpy(&id, msg.buffer, 4);
1184
1185 game->removeProjectile(id);
1186
1187 break;
1188 }
[803566d]1189 default:
1190 {
[d519032]1191 cout << "Received invalid message of type " << msg.type << endl;
[803566d]1192
1193 break;
1194 }
1195 }
1196
1197 break;
1198 }
[4da5aa3]1199 default:
1200 {
1201 cout << "The state has an invalid value: " << state << endl;
1202
1203 break;
1204 }
1205 }
[0dde5da]1206}
[87b3ee2]1207
[929b4e0]1208int getRefreshRate(int width, int height)
1209{
1210 int numRefreshRates = al_get_num_display_modes();
1211 ALLEGRO_DISPLAY_MODE displayMode;
1212
1213 for(int i=0; i<numRefreshRates; i++) {
1214 al_get_display_mode(i, &displayMode);
1215
1216 if (displayMode.width == width && displayMode.height == height)
1217 return displayMode.refresh_rate;
1218 }
1219
1220 return 0;
1221}
1222
1223void drawMessageStatus(ALLEGRO_FONT* font)
1224{
1225 int clientMsgOffset = 5;
1226 int serverMsgOffset = 950;
1227
1228 al_draw_text(font, al_map_rgb(0, 255, 255), 0+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
1229 al_draw_text(font, al_map_rgb(0, 255, 255), 20+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Type");
1230 al_draw_text(font, al_map_rgb(0, 255, 255), 240+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Acked?");
1231
1232 al_draw_text(font, al_map_rgb(0, 255, 255), serverMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
1233
1234 map<unsigned int, map<unsigned long, MessageContainer> >& sentMessages = msgProcessor.getSentMessages();
1235 int id, type;
1236 bool acked;
1237 ostringstream ossId, ossAcked;
1238
1239 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
1240
1241 int msgCount = 0;
1242 for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
1243 map<unsigned long, MessageContainer> playerMessage = it->second;
1244 map<unsigned long, MessageContainer>::iterator it2;
1245 for (it2 = playerMessage.begin(); it2 != playerMessage.end(); it2++) {
1246
1247 id = it->first;
1248 ossId.str("");;
1249 ossId << id;
1250
1251 type = it2->second.getMessage()->type;
1252 string typeStr = MessageContainer::getMsgTypeString(type);
1253
1254 acked = it2->second.getAcked();
1255 ossAcked.str("");;
1256 ossAcked << boolalpha << acked;
1257
1258 al_draw_text(font, al_map_rgb(0, 255, 0), clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
1259 al_draw_text(font, al_map_rgb(0, 255, 0), 20+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, typeStr.c_str());
1260 al_draw_text(font, al_map_rgb(0, 255, 0), 240+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossAcked.str().c_str());
1261
1262 msgCount++;
1263 }
1264 }
1265
1266 if (msgProcessor.getAckedMessages().size() > 0) {
1267 map<unsigned int, unsigned long long> ackedMessages = msgProcessor.getAckedMessages()[0];
1268 map<unsigned int, unsigned long long>::iterator it3;
1269
1270 msgCount = 0;
1271 for (it3 = ackedMessages.begin(); it3 != ackedMessages.end(); it3++) {
1272 ossId.str("");;
1273 ossId << it3->first;
1274
1275 al_draw_text(font, al_map_rgb(255, 0, 0), 25+serverMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
1276
1277 msgCount++;
1278 }
1279 }
1280}
1281
1282// Callback definitions
1283
[5c95436]1284void goToRegisterScreen()
1285{
1286 txtUsernameRegister->clear();
1287 txtPasswordRegister->clear();
[49da01a]1288 lblRegisterStatus->setText("");
1289 rblClasses->setSelectedButton(-1);
1290
1291 wndCurrent = wndRegister;
[5c95436]1292}
1293
1294void goToLoginScreen()
[87b3ee2]1295{
1296 txtUsername->clear();
1297 txtPassword->clear();
[49da01a]1298 lblLoginStatus->setText("");
1299
1300 wndCurrent = wndLogin;
[5c95436]1301}
1302
[bc70282]1303// maybe need a goToGameScreen function as well and add state changes to these functions as well
1304
[5c95436]1305void registerAccount()
1306{
1307 string username = txtUsernameRegister->getStr();
1308 string password = txtPasswordRegister->getStr();
1309
1310 txtUsernameRegister->clear();
1311 txtPasswordRegister->clear();
1312 // maybe clear rblClasses as well (add a method to RadioButtonList to enable this)
1313
1314 Player::PlayerClass playerClass;
1315
1316 switch (rblClasses->getSelectedButton()) {
1317 case 0:
1318 playerClass = Player::CLASS_WARRIOR;
1319 break;
1320 case 1:
1321 playerClass = Player::CLASS_RANGER;
1322 break;
1323 default:
1324 cout << "Invalid class selection" << endl;
1325 playerClass = Player::CLASS_NONE;
1326 break;
1327 }
[87b3ee2]1328
1329 msgTo.type = MSG_TYPE_REGISTER;
1330
1331 strcpy(msgTo.buffer, username.c_str());
1332 strcpy(msgTo.buffer+username.size()+1, password.c_str());
[5c95436]1333 memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
[87b3ee2]1334
[68d94de]1335 msgProcessor.sendMessage(&msgTo, &server);
[87b3ee2]1336}
1337
1338void login()
1339{
1340 string strUsername = txtUsername->getStr();
1341 string strPassword = txtPassword->getStr();
1342 username = strUsername;
1343
1344 txtUsername->clear();
1345 txtPassword->clear();
1346
1347 msgTo.type = MSG_TYPE_LOGIN;
1348
1349 strcpy(msgTo.buffer, strUsername.c_str());
1350 strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
1351
[68d94de]1352 msgProcessor.sendMessage(&msgTo, &server);
[bc70282]1353
[1785314]1354 state = STATE_LOBBY;
[87b3ee2]1355}
1356
1357void logout()
1358{
[929b4e0]1359 switch(state) {
1360 case STATE_LOBBY:
1361 txtJoinGame->clear();
1362 txtCreateGame->clear();
1363 break;
1364 default:
1365 cout << "Logout called from invalid state: " << state << endl;
1366 break;
1367 }
[87b3ee2]1368
1369 msgTo.type = MSG_TYPE_LOGOUT;
1370
1371 strcpy(msgTo.buffer, username.c_str());
1372
[68d94de]1373 msgProcessor.sendMessage(&msgTo, &server);
[87b3ee2]1374}
1375
1376void quit()
1377{
1378 doexit = true;
1379}
1380
1381void sendChatMessage()
1382{
1383 string msg = txtChat->getStr();
1384 txtChat->clear();
1385
1386 msgTo.type = MSG_TYPE_CHAT;
1387 strcpy(msgTo.buffer, msg.c_str());
1388
[68d94de]1389 msgProcessor.sendMessage(&msgTo, &server);
[87b3ee2]1390}
[1f1eb58]1391
[b35b2b2]1392void toggleDebugging()
1393{
1394 debugging = !debugging;
1395}
1396
[929b4e0]1397void joinGame()
[b35b2b2]1398{
[929b4e0]1399 cout << "Joining game" << endl;
[bbebe9c]1400
1401 string msg = txtJoinGame->getStr();
1402 txtJoinGame->clear();
1403
1404 msgTo.type = MSG_TYPE_JOIN_GAME;
1405 strcpy(msgTo.buffer, msg.c_str());
1406
[68d94de]1407 msgProcessor.sendMessage(&msgTo, &server);
[dee75cc]1408}
[b35b2b2]1409
[929b4e0]1410void createGame()
[b35b2b2]1411{
[929b4e0]1412 cout << "Creating game" << endl;
[bbebe9c]1413
1414 string msg = txtCreateGame->getStr();
1415 txtCreateGame->clear();
1416
[d519032]1417 cout << "Sending message: " << msg.c_str() << endl;
1418
[bbebe9c]1419 msgTo.type = MSG_TYPE_CREATE_GAME;
1420 strcpy(msgTo.buffer, msg.c_str());
1421
[68d94de]1422 msgProcessor.sendMessage(&msgTo, &server);
[03ba5e3]1423}
1424
1425void leaveGame()
1426{
1427 cout << "Leaving game" << endl;
1428
1429 game = NULL;
1430
1431 state = STATE_LOBBY;
1432 wndCurrent = wndLobby;
1433
1434 msgTo.type = MSG_TYPE_LEAVE_GAME;
1435
[68d94de]1436 msgProcessor.sendMessage(&msgTo, &server);
[95ffe57]1437}
[3e44a59]1438
1439void closeGameSummary()
1440{
1441 delete gameSummary;
[635ad9b]1442 gameSummary = NULL;
[3e44a59]1443 wndCurrent = wndLobby;
[635ad9b]1444 cout << "Processed button actions" << endl;
[3e44a59]1445}
Note: See TracBrowser for help on using the repository browser.