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