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