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