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

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

MessageProcessor now takes a socket and optional output log file as constructor arguments instead of accepting them as parameters in each method

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