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