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