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