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

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

Removed error function definition from main.cpp

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