[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));
|
---|
| 257 | wndLobby->addComponent(new TextLabel(SCREEN_W*1/4-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT));
|
---|
| 258 | wndLobby->addComponent(new Textbox(SCREEN_W*1/4+4, 40, 100, 20, font));
|
---|
| 259 | wndLobby->addComponent(new Button(SCREEN_W*1/4-100, 80, 200, 20, font, "Join Existing Game", joinGame));
|
---|
| 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));
|
---|
| 263 |
|
---|
| 264 | txtJoinGame = (Textbox*)wndLobby->getComponent(2);
|
---|
| 265 | txtCreateGame = (Textbox*)wndLobby->getComponent(5);
|
---|
[87b3ee2] | 266 |
|
---|
[1785314] | 267 | cout << "Created lobby screen" << endl;
|
---|
[87b3ee2] | 268 |
|
---|
[1785314] | 269 | wndGame = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
| 270 | wndGame->addComponent(new Textbox(95, 40, 300, 20, font));
|
---|
| 271 | wndGame->addComponent(new Button(95, 70, 60, 20, font, "Send", sendChatMessage));
|
---|
| 272 | wndGame->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging));
|
---|
| 273 | wndGame->addComponent(new Button(920, 10, 80, 20, font, "Logout", logout));
|
---|
[b35b2b2] | 274 |
|
---|
[1785314] | 275 | txtChat = (Textbox*)wndGame->getComponent(0);
|
---|
| 276 |
|
---|
| 277 | wndGameDebug = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
| 278 | wndGameDebug->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging));
|
---|
| 279 | wndGameDebug->addComponent(new Button(920, 10, 80, 20, font, "Logout", logout));
|
---|
| 280 |
|
---|
| 281 | cout << "Created game screen" << endl;
|
---|
[365e156] | 282 |
|
---|
[03ba5e3] | 283 | wndNewGame = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
| 284 | wndNewGame->addComponent(new Button(880, 10, 120, 20, font, "Leave Game", leaveGame));
|
---|
| 285 |
|
---|
| 286 | cout << "Created new game screen" << endl;
|
---|
| 287 |
|
---|
[49da01a] | 288 | goToLoginScreen();
|
---|
[d352805] | 289 |
|
---|
| 290 | event_queue = al_create_event_queue();
|
---|
| 291 | if(!event_queue) {
|
---|
| 292 | fprintf(stderr, "failed to create event_queue!\n");
|
---|
| 293 | al_destroy_display(display);
|
---|
| 294 | al_destroy_timer(timer);
|
---|
| 295 | return -1;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | al_set_target_bitmap(al_get_backbuffer(display));
|
---|
| 299 |
|
---|
| 300 | al_register_event_source(event_queue, al_get_display_event_source(display));
|
---|
| 301 | al_register_event_source(event_queue, al_get_timer_event_source(timer));
|
---|
| 302 | al_register_event_source(event_queue, al_get_keyboard_event_source());
|
---|
[87b3ee2] | 303 | al_register_event_source(event_queue, al_get_mouse_event_source());
|
---|
[d352805] | 304 |
|
---|
| 305 | al_clear_to_color(al_map_rgb(0,0,0));
|
---|
| 306 |
|
---|
| 307 | al_flip_display();
|
---|
[9a3e6b1] | 308 |
|
---|
| 309 | if (argc != 3) {
|
---|
| 310 | cout << "Usage: server port" << endl;
|
---|
| 311 | exit(1);
|
---|
| 312 | }
|
---|
| 313 |
|
---|
| 314 | initWinSock();
|
---|
[803566d] | 315 |
|
---|
[9a3e6b1] | 316 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
| 317 | if (sock < 0)
|
---|
| 318 | error("socket");
|
---|
| 319 |
|
---|
[e607c0f] | 320 | set_nonblock(sock);
|
---|
| 321 |
|
---|
[9a3e6b1] | 322 | server.sin_family = AF_INET;
|
---|
| 323 | hp = gethostbyname(argv[1]);
|
---|
| 324 | if (hp==0)
|
---|
| 325 | error("Unknown host");
|
---|
| 326 |
|
---|
| 327 | memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
|
---|
| 328 | server.sin_port = htons(atoi(argv[2]));
|
---|
| 329 |
|
---|
[d352805] | 330 | al_start_timer(timer);
|
---|
[e607c0f] | 331 |
|
---|
[d352805] | 332 | while(!doexit)
|
---|
| 333 | {
|
---|
| 334 | ALLEGRO_EVENT ev;
|
---|
[3d81c0d] | 335 |
|
---|
[d352805] | 336 | al_wait_for_event(event_queue, &ev);
|
---|
[87b3ee2] | 337 |
|
---|
| 338 | if(wndCurrent->handleEvent(ev)) {
|
---|
| 339 | // do nothing
|
---|
| 340 | }
|
---|
| 341 | else if(ev.type == ALLEGRO_EVENT_TIMER) {
|
---|
[a1a3bd5] | 342 | redraw = true; // seems like we should just call a draw function here instead
|
---|
[d352805] | 343 | }
|
---|
| 344 | else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
|
---|
[9a3e6b1] | 345 | doexit = true;
|
---|
[d352805] | 346 | }
|
---|
| 347 | else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
|
---|
| 348 | }
|
---|
| 349 | else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
|
---|
| 350 | switch(ev.keyboard.keycode) {
|
---|
| 351 | case ALLEGRO_KEY_ESCAPE:
|
---|
| 352 | doexit = true;
|
---|
| 353 | break;
|
---|
[4926168] | 354 | case ALLEGRO_KEY_S: // pickup an item next to you
|
---|
[85bf1e2] | 355 | if (state == STATE_GAME) {
|
---|
[4926168] | 356 | msgTo.type = MSG_TYPE_PICKUP_FLAG;
|
---|
| 357 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
[753fa8a] | 358 | msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
|
---|
[4926168] | 359 | }
|
---|
| 360 | break;
|
---|
[626e5b0] | 361 | case ALLEGRO_KEY_D: // drop the current item
|
---|
[85bf1e2] | 362 | if (state == STATE_GAME) {
|
---|
[4926168] | 363 | // find the current player in the player list
|
---|
[e6c26b8] | 364 | map<unsigned int, Player*>::iterator it;
|
---|
[626e5b0] | 365 | Player* p = NULL;
|
---|
| 366 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 367 | {
|
---|
[e6c26b8] | 368 | if (it->second->id == curPlayerId)
|
---|
| 369 | p = it->second;
|
---|
[626e5b0] | 370 | }
|
---|
| 371 |
|
---|
| 372 | if (p != NULL) {
|
---|
| 373 | int flagType = WorldMap::OBJECT_NONE;
|
---|
| 374 |
|
---|
| 375 | if (p->hasBlueFlag)
|
---|
| 376 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
| 377 | else if (p->hasRedFlag)
|
---|
| 378 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
| 379 |
|
---|
| 380 | if (flagType != WorldMap::OBJECT_NONE) {
|
---|
| 381 | msgTo.type = MSG_TYPE_DROP_FLAG;
|
---|
| 382 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
[753fa8a] | 383 | msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
|
---|
[626e5b0] | 384 | }
|
---|
| 385 | }
|
---|
| 386 | }
|
---|
| 387 | break;
|
---|
[d352805] | 388 | }
|
---|
| 389 | }
|
---|
[88cdae2] | 390 | else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
|
---|
[1785314] | 391 | if(wndCurrent == wndLobby) {
|
---|
| 392 | if (ev.mouse.button == 1) { // left click
|
---|
[929b4e0] | 393 | txtJoinGame->clear();
|
---|
| 394 | txtCreateGame->clear();
|
---|
[1785314] | 395 | state = STATE_GAME;
|
---|
| 396 | wndCurrent = wndGame;
|
---|
| 397 | }
|
---|
| 398 | }else if(wndCurrent == wndGame) {
|
---|
[e1f78f5] | 399 | if (ev.mouse.button == 1) { // left click
|
---|
| 400 | msgTo.type = MSG_TYPE_PLAYER_MOVE;
|
---|
[88cdae2] | 401 |
|
---|
[e1f78f5] | 402 | POSITION pos;
|
---|
| 403 | pos.x = ev.mouse.x;
|
---|
| 404 | pos.y = ev.mouse.y;
|
---|
| 405 | pos = screenToMap(pos);
|
---|
[62ee2ce] | 406 |
|
---|
[e1f78f5] | 407 | if (pos.x != -1)
|
---|
| 408 | {
|
---|
| 409 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
| 410 | memcpy(msgTo.buffer+4, &pos.x, 4);
|
---|
| 411 | memcpy(msgTo.buffer+8, &pos.y, 4);
|
---|
| 412 |
|
---|
[753fa8a] | 413 | msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
|
---|
[e1f78f5] | 414 | }
|
---|
| 415 | else
|
---|
| 416 | cout << "Invalid point: User did not click on the map" << endl;
|
---|
| 417 | }else if (ev.mouse.button == 2) { // right click
|
---|
[e6c26b8] | 418 | map<unsigned int, Player*>::iterator it;
|
---|
[e1f78f5] | 419 |
|
---|
[fbcfc35] | 420 | cout << "Detected a right-click" << endl;
|
---|
| 421 |
|
---|
[e1f78f5] | 422 | Player* curPlayer;
|
---|
| 423 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 424 | {
|
---|
[e6c26b8] | 425 | if (it->second->id == curPlayerId)
|
---|
| 426 | curPlayer = it->second;
|
---|
[e1f78f5] | 427 | }
|
---|
| 428 |
|
---|
| 429 | Player* target;
|
---|
| 430 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 431 | {
|
---|
[fbcfc35] | 432 | // need to check if the right-click was actually on this player
|
---|
[f3cf1a5] | 433 | // right now, this code will target all players other than the current one
|
---|
[e6c26b8] | 434 | target = it->second;
|
---|
[50e6c7a] | 435 | if (target->id != curPlayerId && target->team != curPlayer->team)
|
---|
| 436 | {
|
---|
[e1f78f5] | 437 | msgTo.type = MSG_TYPE_START_ATTACK;
|
---|
| 438 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
| 439 | memcpy(msgTo.buffer+4, &target->id, 4);
|
---|
[88cdae2] | 440 |
|
---|
[753fa8a] | 441 | msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
|
---|
[e1f78f5] | 442 | }
|
---|
| 443 | }
|
---|
[62ee2ce] | 444 | }
|
---|
[ad5d122] | 445 | }
|
---|
[88cdae2] | 446 | }
|
---|
[e607c0f] | 447 |
|
---|
[753fa8a] | 448 | if (msgProcessor.receiveMessage(&msgFrom, sock, &from, &outputLog) >= 0)
|
---|
[fbcfc35] | 449 | processMessage(msgFrom, state, chatConsole, gameMap, mapPlayers, mapProjectiles, curPlayerId, scoreBlue, scoreRed);
|
---|
[054b50b] | 450 |
|
---|
[a1a3bd5] | 451 | if (redraw)
|
---|
[e607c0f] | 452 | {
|
---|
[d352805] | 453 | redraw = false;
|
---|
[88cdae2] | 454 |
|
---|
[753fa8a] | 455 | msgProcessor.resendUnackedMessages(sock, &outputLog);
|
---|
| 456 | //msgProcessor.cleanAckedMessages(&outputLog);
|
---|
[10f6fc2] | 457 |
|
---|
[1785314] | 458 | if (debugging && wndCurrent == wndGame)
|
---|
| 459 | wndGameDebug->draw(display);
|
---|
[b35b2b2] | 460 | else
|
---|
| 461 | wndCurrent->draw(display);
|
---|
[9a3e6b1] | 462 |
|
---|
[50e6c7a] | 463 | if (wndCurrent == wndLobby) {
|
---|
[321fbbc] | 464 | map<string, int>::iterator it;
|
---|
[2ee386d] | 465 | int i=0;
|
---|
[2992b1a] | 466 | ostringstream ossGame;
|
---|
[2ee386d] | 467 | for (it = mapGames.begin(); it != mapGames.end(); it++) {
|
---|
[321fbbc] | 468 | ossGame << it->first << " (" << it->second << " players)" << endl;
|
---|
[2992b1a] | 469 | al_draw_text(font, al_map_rgb(0, 255, 0), SCREEN_W*1/4-100, 120+i*15, ALLEGRO_ALIGN_LEFT, ossGame.str().c_str());
|
---|
| 470 | ossGame.clear();
|
---|
| 471 | ossGame.str("");
|
---|
[2ee386d] | 472 | i++;
|
---|
[50e6c7a] | 473 | }
|
---|
| 474 | }
|
---|
[03ba5e3] | 475 | else if (wndCurrent == wndNewGame)
|
---|
| 476 | {
|
---|
| 477 | ostringstream ossScoreBlue, ossScoreRed;
|
---|
| 478 |
|
---|
| 479 | ossScoreBlue << "Blue: " << game->getBlueScore() << endl;
|
---|
| 480 | ossScoreRed << "Red: " << game->getRedScore() << endl;
|
---|
| 481 |
|
---|
| 482 | al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
|
---|
| 483 | al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
|
---|
[0693e25] | 484 |
|
---|
[6319311] | 485 | GameRender::drawMap(game->getMap());
|
---|
| 486 | GameRender::drawPlayers(game->getPlayers(), font, curPlayerId);
|
---|
[03ba5e3] | 487 | }
|
---|
[50e6c7a] | 488 | else if (wndCurrent == wndGame)
|
---|
| 489 | {
|
---|
[b35b2b2] | 490 | if (!debugging)
|
---|
| 491 | chatConsole.draw(font, al_map_rgb(255,255,255));
|
---|
[bc70282] | 492 |
|
---|
[87b3ee2] | 493 | al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
|
---|
[62ee2ce] | 494 |
|
---|
[15efb4e] | 495 | ostringstream ossScoreBlue, ossScoreRed;
|
---|
| 496 |
|
---|
| 497 | ossScoreBlue << "Blue: " << scoreBlue << endl;
|
---|
| 498 | ossScoreRed << "Red: " << scoreRed << endl;
|
---|
| 499 |
|
---|
| 500 | al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
|
---|
| 501 | al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
|
---|
| 502 |
|
---|
[032e550] | 503 | // update players
|
---|
[e6c26b8] | 504 | map<unsigned int, Player*>::iterator it;
|
---|
[032e550] | 505 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 506 | {
|
---|
[e6c26b8] | 507 | it->second->updateTarget(mapPlayers);
|
---|
[032e550] | 508 | }
|
---|
| 509 |
|
---|
[a1a3bd5] | 510 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 511 | {
|
---|
[e6c26b8] | 512 | it->second->move(gameMap); // ignore return value
|
---|
[a1a3bd5] | 513 | }
|
---|
| 514 |
|
---|
[8c74150] | 515 | // update projectile positions
|
---|
| 516 | map<unsigned int, Projectile>::iterator it2;
|
---|
| 517 | for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
|
---|
| 518 | {
|
---|
| 519 | it2->second.move(mapPlayers);
|
---|
| 520 | }
|
---|
| 521 |
|
---|
[6319311] | 522 | GameRender::drawMap(gameMap);
|
---|
| 523 | GameRender::drawPlayers(mapPlayers, font, curPlayerId);
|
---|
[8c74150] | 524 |
|
---|
| 525 | // draw projectiles
|
---|
| 526 | for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
|
---|
| 527 | {
|
---|
| 528 | Projectile proj = it2->second;
|
---|
| 529 |
|
---|
[e6c26b8] | 530 | FLOAT_POSITION target = mapPlayers[proj.target]->pos;
|
---|
[8c74150] | 531 | float angle = atan2(target.y-proj.pos.toFloat().y, target.x-proj.pos.toFloat().x);
|
---|
| 532 |
|
---|
| 533 | POSITION start, end;
|
---|
| 534 | start.x = cos(angle)*15+proj.pos.x;
|
---|
| 535 | start.y = sin(angle)*15+proj.pos.y;
|
---|
| 536 | end.x = proj.pos.x;
|
---|
| 537 | end.y = proj.pos.y;
|
---|
| 538 |
|
---|
| 539 | start = mapToScreen(start);
|
---|
| 540 | end = mapToScreen(end);
|
---|
| 541 |
|
---|
| 542 | al_draw_line(start.x, start.y, end.x, end.y, al_map_rgb(0, 0, 0), 4);
|
---|
| 543 | }
|
---|
[87b3ee2] | 544 | }
|
---|
| 545 |
|
---|
[b35b2b2] | 546 | if (debugging) {
|
---|
| 547 | //debugConsole.draw(font, al_map_rgb(255,255,255));
|
---|
| 548 | drawMessageStatus(font);
|
---|
| 549 | }
|
---|
| 550 |
|
---|
[d352805] | 551 | al_flip_display();
|
---|
| 552 | }
|
---|
| 553 | }
|
---|
[9a3e6b1] | 554 |
|
---|
| 555 | #if defined WINDOWS
|
---|
| 556 | closesocket(sock);
|
---|
| 557 | #elif defined LINUX
|
---|
| 558 | close(sock);
|
---|
| 559 | #endif
|
---|
| 560 |
|
---|
| 561 | shutdownWinSock();
|
---|
[d352805] | 562 |
|
---|
[87b3ee2] | 563 | delete wndLogin;
|
---|
[1785314] | 564 | delete wndRegister;
|
---|
| 565 | delete wndLobby;
|
---|
| 566 | delete wndGame;
|
---|
| 567 | delete wndGameDebug;
|
---|
[87b3ee2] | 568 |
|
---|
[62ee2ce] | 569 | delete gameMap;
|
---|
[d519032] | 570 |
|
---|
| 571 | if (game != NULL)
|
---|
| 572 | delete game;
|
---|
[62ee2ce] | 573 |
|
---|
[e6c26b8] | 574 | map<unsigned int, Player*>::iterator it;
|
---|
| 575 |
|
---|
| 576 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
|
---|
| 577 | delete it->second;
|
---|
| 578 | }
|
---|
| 579 |
|
---|
[d352805] | 580 | al_destroy_event_queue(event_queue);
|
---|
| 581 | al_destroy_display(display);
|
---|
| 582 | al_destroy_timer(timer);
|
---|
[8271c78] | 583 |
|
---|
| 584 | outputLog << "Stopped client on " << getCurrentDateTimeString() << endl;
|
---|
| 585 | outputLog.close();
|
---|
| 586 |
|
---|
[d352805] | 587 | return 0;
|
---|
| 588 | }
|
---|
| 589 |
|
---|
[1f1eb58] | 590 |
|
---|
| 591 |
|
---|
[4da5aa3] | 592 | // need to make a function like this that works on windows
|
---|
| 593 | void error(const char *msg)
|
---|
| 594 | {
|
---|
| 595 | perror(msg);
|
---|
| 596 | shutdownWinSock();
|
---|
| 597 | exit(1);
|
---|
| 598 | }
|
---|
| 599 |
|
---|
[0dde5da] | 600 | void initWinSock()
|
---|
| 601 | {
|
---|
| 602 | #if defined WINDOWS
|
---|
| 603 | WORD wVersionRequested;
|
---|
| 604 | WSADATA wsaData;
|
---|
| 605 | int wsaerr;
|
---|
| 606 |
|
---|
| 607 | wVersionRequested = MAKEWORD(2, 2);
|
---|
| 608 | wsaerr = WSAStartup(wVersionRequested, &wsaData);
|
---|
[803566d] | 609 |
|
---|
[0dde5da] | 610 | if (wsaerr != 0) {
|
---|
| 611 | cout << "The Winsock dll not found." << endl;
|
---|
| 612 | exit(1);
|
---|
| 613 | }else
|
---|
| 614 | cout << "The Winsock dll was found." << endl;
|
---|
| 615 | #endif
|
---|
| 616 | }
|
---|
| 617 |
|
---|
| 618 | void shutdownWinSock()
|
---|
| 619 | {
|
---|
| 620 | #if defined WINDOWS
|
---|
| 621 | WSACleanup();
|
---|
| 622 | #endif
|
---|
[1912323] | 623 | }
|
---|
| 624 |
|
---|
[e6c26b8] | 625 | 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] | 626 | {
|
---|
[4da5aa3] | 627 | string response = string(msg.buffer);
|
---|
| 628 |
|
---|
| 629 | switch(state)
|
---|
| 630 | {
|
---|
| 631 | case STATE_START:
|
---|
| 632 | {
|
---|
[e607c0f] | 633 | cout << "In STATE_START" << endl;
|
---|
| 634 |
|
---|
[87b3ee2] | 635 | switch(msg.type)
|
---|
[4da5aa3] | 636 | {
|
---|
[87b3ee2] | 637 | case MSG_TYPE_REGISTER:
|
---|
| 638 | {
|
---|
[365e156] | 639 | lblRegisterStatus->setText(response);
|
---|
[87b3ee2] | 640 | break;
|
---|
| 641 | }
|
---|
[bc70282] | 642 | default:
|
---|
| 643 | {
|
---|
| 644 | cout << "(STATE_REGISTER) Received invalid message of type " << msg.type << endl;
|
---|
| 645 | break;
|
---|
| 646 | }
|
---|
| 647 | }
|
---|
| 648 |
|
---|
| 649 | break;
|
---|
| 650 | }
|
---|
[1785314] | 651 | case STATE_LOBBY:
|
---|
[d519032] | 652 | cout << "In STATE_LOBBY" << endl;
|
---|
[1785314] | 653 | case STATE_GAME:
|
---|
[bc70282] | 654 | {
|
---|
| 655 | switch(msg.type)
|
---|
| 656 | {
|
---|
[87b3ee2] | 657 | case MSG_TYPE_LOGIN:
|
---|
| 658 | {
|
---|
| 659 | if (response.compare("Player has already logged in.") == 0)
|
---|
| 660 | {
|
---|
[bc70282] | 661 | goToLoginScreen();
|
---|
| 662 | state = STATE_START;
|
---|
| 663 |
|
---|
[365e156] | 664 | lblLoginStatus->setText(response);
|
---|
[87b3ee2] | 665 | }
|
---|
| 666 | else if (response.compare("Incorrect username or password") == 0)
|
---|
| 667 | {
|
---|
[bc70282] | 668 | goToLoginScreen();
|
---|
| 669 | state = STATE_START;
|
---|
| 670 |
|
---|
[365e156] | 671 | lblLoginStatus->setText(response);
|
---|
[87b3ee2] | 672 | }
|
---|
| 673 | else
|
---|
| 674 | {
|
---|
[1785314] | 675 | wndCurrent = wndLobby;
|
---|
[95ffe57] | 676 |
|
---|
| 677 | // this message should only be sent when a player first logs in so they know their id
|
---|
| 678 |
|
---|
| 679 | Player* p = new Player("", "");
|
---|
| 680 | p->deserialize(msg.buffer);
|
---|
[e6c26b8] | 681 |
|
---|
[95ffe57] | 682 | if (mapPlayers.find(p->id) != mapPlayers.end())
|
---|
| 683 | delete mapPlayers[p->id];
|
---|
| 684 | mapPlayers[p->id] = p;
|
---|
| 685 | curPlayerId = p->id;
|
---|
[88cdae2] | 686 |
|
---|
| 687 | cout << "Got a valid login response with the player" << endl;
|
---|
[1f1eb58] | 688 | cout << "Player id: " << curPlayerId << endl;
|
---|
[95ffe57] | 689 | cout << "Player health: " << p->health << endl;
|
---|
[bc70282] | 690 | cout << "player map size: " << mapPlayers.size() << endl;
|
---|
[87b3ee2] | 691 | }
|
---|
[88cdae2] | 692 |
|
---|
[a1a3bd5] | 693 | break;
|
---|
| 694 | }
|
---|
| 695 | case MSG_TYPE_LOGOUT:
|
---|
| 696 | {
|
---|
[054b50b] | 697 | cout << "Got a logout message" << endl;
|
---|
[a1a3bd5] | 698 |
|
---|
[87b3ee2] | 699 | if (response.compare("You have successfully logged out.") == 0)
|
---|
| 700 | {
|
---|
| 701 | cout << "Logged out" << endl;
|
---|
| 702 | state = STATE_START;
|
---|
[49da01a] | 703 | goToLoginScreen();
|
---|
[87b3ee2] | 704 | }
|
---|
[054b50b] | 705 |
|
---|
| 706 | break;
|
---|
| 707 | }
|
---|
[4c202e0] | 708 | case MSG_TYPE_PLAYER:
|
---|
| 709 | {
|
---|
[1f1eb58] | 710 | cout << "Received MSG_TYPE_PLAYER" << endl;
|
---|
| 711 |
|
---|
[95ffe57] | 712 | Player* p = new Player("", "");
|
---|
| 713 | p->deserialize(msg.buffer);
|
---|
| 714 | p->timeLastUpdated = getCurrentMillis();
|
---|
| 715 | p->isChasing = false;
|
---|
| 716 | if (p->health <= 0)
|
---|
| 717 | p->isDead = true;
|
---|
[5a5f131] | 718 | else
|
---|
[95ffe57] | 719 | p->isDead = false;
|
---|
[5a5f131] | 720 |
|
---|
[95ffe57] | 721 | if (mapPlayers.find(p->id) != mapPlayers.end())
|
---|
| 722 | delete mapPlayers[p->id];
|
---|
| 723 | mapPlayers[p->id] = p;
|
---|
[4c202e0] | 724 |
|
---|
[eb8adb1] | 725 | break;
|
---|
| 726 | }
|
---|
[a1a3bd5] | 727 | case MSG_TYPE_PLAYER_MOVE:
|
---|
| 728 | {
|
---|
| 729 | unsigned int id;
|
---|
| 730 | int x, y;
|
---|
| 731 |
|
---|
| 732 | memcpy(&id, msg.buffer, 4);
|
---|
| 733 | memcpy(&x, msg.buffer+4, 4);
|
---|
| 734 | memcpy(&y, msg.buffer+8, 4);
|
---|
| 735 |
|
---|
[e6c26b8] | 736 | mapPlayers[id]->target.x = x;
|
---|
| 737 | mapPlayers[id]->target.y = y;
|
---|
[a1a3bd5] | 738 |
|
---|
| 739 | break;
|
---|
| 740 | }
|
---|
[eb8adb1] | 741 | case MSG_TYPE_CHAT:
|
---|
| 742 | {
|
---|
| 743 | chatConsole.addLine(response);
|
---|
[4c202e0] | 744 |
|
---|
[87b3ee2] | 745 | break;
|
---|
| 746 | }
|
---|
[45b2750] | 747 | case MSG_TYPE_OBJECT:
|
---|
| 748 | {
|
---|
[d519032] | 749 | cout << "Received OBJECT message" << endl;
|
---|
[45b2750] | 750 |
|
---|
| 751 | WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
|
---|
| 752 | o.deserialize(msg.buffer);
|
---|
[bc70282] | 753 | cout << "object id: " << o.id << endl;
|
---|
[45b2750] | 754 | gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
|
---|
| 755 |
|
---|
| 756 | break;
|
---|
| 757 | }
|
---|
[2df63d6] | 758 | case MSG_TYPE_REMOVE_OBJECT:
|
---|
| 759 | {
|
---|
[7511a2b] | 760 | cout << "Received REMOVE_OBJECT message!" << endl;
|
---|
| 761 |
|
---|
[2df63d6] | 762 | int id;
|
---|
| 763 | memcpy(&id, msg.buffer, 4);
|
---|
[7511a2b] | 764 |
|
---|
| 765 | cout << "Removing object with id " << id << endl;
|
---|
| 766 |
|
---|
[2df63d6] | 767 | if (!gameMap->removeObject(id))
|
---|
| 768 | cout << "Did not remove the object" << endl;
|
---|
[7511a2b] | 769 |
|
---|
| 770 | break;
|
---|
[2df63d6] | 771 | }
|
---|
[15efb4e] | 772 | case MSG_TYPE_SCORE:
|
---|
| 773 | {
|
---|
| 774 | memcpy(&scoreBlue, msg.buffer, 4);
|
---|
| 775 | memcpy(&scoreRed, msg.buffer+4, 4);
|
---|
| 776 |
|
---|
| 777 | break;
|
---|
| 778 | }
|
---|
[b978503] | 779 | case MSG_TYPE_ATTACK:
|
---|
| 780 | {
|
---|
[032e550] | 781 | cout << "Received ATTACK message" << endl;
|
---|
| 782 |
|
---|
| 783 | break;
|
---|
| 784 | }
|
---|
| 785 | case MSG_TYPE_START_ATTACK:
|
---|
| 786 | {
|
---|
| 787 | cout << "Received START_ATTACK message" << endl;
|
---|
| 788 |
|
---|
| 789 | unsigned int id, targetID;
|
---|
| 790 | memcpy(&id, msg.buffer, 4);
|
---|
| 791 | memcpy(&targetID, msg.buffer+4, 4);
|
---|
| 792 |
|
---|
| 793 | cout << "source id: " << id << endl;
|
---|
| 794 | cout << "target id: " << targetID << endl;
|
---|
| 795 |
|
---|
[e6c26b8] | 796 | Player* source = mapPlayers[id];
|
---|
[032e550] | 797 | source->targetPlayer = targetID;
|
---|
| 798 | source->isChasing = true;
|
---|
| 799 |
|
---|
[b978503] | 800 | break;
|
---|
| 801 | }
|
---|
| 802 | case MSG_TYPE_PROJECTILE:
|
---|
| 803 | {
|
---|
[8c74150] | 804 | cout << "Received a PROJECTILE message" << endl;
|
---|
[fbcfc35] | 805 |
|
---|
[50e6c7a] | 806 | unsigned int id, x, y, targetId;
|
---|
[fbcfc35] | 807 |
|
---|
| 808 | memcpy(&id, msg.buffer, 4);
|
---|
| 809 | memcpy(&x, msg.buffer+4, 4);
|
---|
| 810 | memcpy(&y, msg.buffer+8, 4);
|
---|
| 811 | memcpy(&targetId, msg.buffer+12, 4);
|
---|
| 812 |
|
---|
[8c74150] | 813 | cout << "id: " << id << endl;
|
---|
| 814 | cout << "x: " << x << endl;
|
---|
| 815 | cout << "y: " << y << endl;
|
---|
| 816 | cout << "Target: " << targetId << endl;
|
---|
| 817 |
|
---|
| 818 | Projectile proj(x, y, targetId, 0);
|
---|
| 819 | proj.setId(id);
|
---|
| 820 |
|
---|
| 821 | mapProjectiles[id] = proj;
|
---|
[fbcfc35] | 822 |
|
---|
[b978503] | 823 | break;
|
---|
| 824 | }
|
---|
| 825 | case MSG_TYPE_REMOVE_PROJECTILE:
|
---|
| 826 | {
|
---|
[8c74150] | 827 | cout << "Received a REMOVE_PROJECTILE message" << endl;
|
---|
| 828 |
|
---|
| 829 | int id;
|
---|
| 830 | memcpy(&id, msg.buffer, 4);
|
---|
| 831 |
|
---|
| 832 | mapProjectiles.erase(id);
|
---|
| 833 |
|
---|
[b978503] | 834 | break;
|
---|
| 835 | }
|
---|
[50e6c7a] | 836 | case MSG_TYPE_GAME_INFO:
|
---|
| 837 | {
|
---|
[803566d] | 838 | cout << "Received a GAME_INFO message" << endl;
|
---|
[50e6c7a] | 839 |
|
---|
[2ee386d] | 840 | string gameName(msg.buffer+4);
|
---|
[50e6c7a] | 841 | int numPlayers;
|
---|
| 842 |
|
---|
| 843 | memcpy(&numPlayers, msg.buffer, 4);
|
---|
| 844 |
|
---|
[2ee386d] | 845 | cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;
|
---|
| 846 |
|
---|
[321fbbc] | 847 | mapGames[gameName] = numPlayers;
|
---|
[50e6c7a] | 848 |
|
---|
| 849 | break;
|
---|
| 850 | }
|
---|
[d519032] | 851 | case MSG_TYPE_JOIN_GAME_SUCCESS:
|
---|
| 852 | {
|
---|
| 853 | cout << "Received a JOIN_GAME_SUCCESS message" << endl;
|
---|
| 854 |
|
---|
[03ba5e3] | 855 | string gameName(msg.buffer);
|
---|
[233e736] | 856 | game = new Game(gameName, "../../data/map.txt");
|
---|
[03ba5e3] | 857 | cout << "Game name: " << gameName << endl;
|
---|
[d519032] | 858 |
|
---|
| 859 | state = STATE_NEW_GAME;
|
---|
[03ba5e3] | 860 | wndCurrent = wndNewGame;
|
---|
[d519032] | 861 |
|
---|
| 862 | msgTo.type = MSG_TYPE_JOIN_GAME_ACK;
|
---|
| 863 | strcpy(msgTo.buffer, gameName.c_str());
|
---|
| 864 |
|
---|
| 865 | msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
|
---|
| 866 |
|
---|
| 867 | break;
|
---|
| 868 | }
|
---|
| 869 | case MSG_TYPE_JOIN_GAME_FAILURE:
|
---|
| 870 | {
|
---|
| 871 | cout << "Received a JOIN_GAME_FAILURE message" << endl;
|
---|
| 872 |
|
---|
| 873 | break;
|
---|
| 874 | }
|
---|
[45b2750] | 875 | default:
|
---|
| 876 | {
|
---|
[1785314] | 877 | cout << "(STATE_LOBBY) Received invlaid message of type " << msg.type << endl;
|
---|
[50e6c7a] | 878 |
|
---|
[365e156] | 879 | break;
|
---|
[45b2750] | 880 | }
|
---|
[4da5aa3] | 881 | }
|
---|
[eb8adb1] | 882 |
|
---|
[4da5aa3] | 883 | break;
|
---|
| 884 | }
|
---|
[803566d] | 885 | case STATE_NEW_GAME:
|
---|
| 886 | {
|
---|
[d519032] | 887 | cout << "(STATE_NEW_GAME) ";
|
---|
[803566d] | 888 | switch(msg.type)
|
---|
| 889 | {
|
---|
| 890 | case MSG_TYPE_GAME_INFO:
|
---|
| 891 | {
|
---|
[d519032] | 892 | cout << "Received a GAME_INFO message" << endl;
|
---|
[803566d] | 893 |
|
---|
| 894 | string gameName(msg.buffer+4);
|
---|
| 895 | int numPlayers;
|
---|
| 896 |
|
---|
| 897 | memcpy(&numPlayers, msg.buffer, 4);
|
---|
| 898 |
|
---|
| 899 | cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;
|
---|
| 900 |
|
---|
| 901 | mapGames[gameName] = numPlayers;
|
---|
| 902 |
|
---|
| 903 | break;
|
---|
| 904 | }
|
---|
| 905 | case MSG_TYPE_OBJECT:
|
---|
| 906 | {
|
---|
[d519032] | 907 | cout << "Received object message in STATE_NEW_GAME" << endl;
|
---|
[803566d] | 908 |
|
---|
| 909 | WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
|
---|
| 910 | o.deserialize(msg.buffer);
|
---|
| 911 | cout << "object id: " << o.id << endl;
|
---|
| 912 | game->getMap()->updateObject(o.id, o.type, o.pos.x, o.pos.y);
|
---|
| 913 |
|
---|
| 914 | break;
|
---|
| 915 | }
|
---|
| 916 | case MSG_TYPE_REMOVE_OBJECT:
|
---|
| 917 | {
|
---|
[d519032] | 918 | cout << "Received REMOVE_OBJECT message!" << endl;
|
---|
[803566d] | 919 |
|
---|
| 920 | int id;
|
---|
| 921 | memcpy(&id, msg.buffer, 4);
|
---|
| 922 |
|
---|
| 923 | cout << "Removing object with id " << id << endl;
|
---|
| 924 |
|
---|
| 925 | if (!game->getMap()->removeObject(id))
|
---|
| 926 | cout << "Did not remove the object" << endl;
|
---|
| 927 |
|
---|
| 928 | break;
|
---|
| 929 | }
|
---|
| 930 | case MSG_TYPE_SCORE:
|
---|
| 931 | {
|
---|
| 932 | cout << "Received SCORE message!" << endl;
|
---|
| 933 |
|
---|
| 934 | int blueScore;
|
---|
| 935 | memcpy(&blueScore, msg.buffer, 4);
|
---|
| 936 | cout << "blue score: " << blueScore << endl;
|
---|
| 937 | game->setBlueScore(blueScore);
|
---|
| 938 |
|
---|
| 939 | int redScore;
|
---|
| 940 | memcpy(&redScore, msg.buffer+4, 4);
|
---|
| 941 | cout << "red score: " << redScore << endl;
|
---|
| 942 | game->setRedScore(redScore);
|
---|
| 943 |
|
---|
| 944 | cout << "Processed SCORE message!" << endl;
|
---|
| 945 |
|
---|
| 946 | break;
|
---|
| 947 | }
|
---|
| 948 | default:
|
---|
| 949 | {
|
---|
[d519032] | 950 | cout << "Received invalid message of type " << msg.type << endl;
|
---|
[803566d] | 951 |
|
---|
| 952 | break;
|
---|
| 953 | }
|
---|
| 954 | }
|
---|
| 955 |
|
---|
| 956 | break;
|
---|
| 957 | }
|
---|
[4da5aa3] | 958 | default:
|
---|
| 959 | {
|
---|
| 960 | cout << "The state has an invalid value: " << state << endl;
|
---|
| 961 |
|
---|
| 962 | break;
|
---|
| 963 | }
|
---|
| 964 | }
|
---|
[0dde5da] | 965 | }
|
---|
[87b3ee2] | 966 |
|
---|
[929b4e0] | 967 | int getRefreshRate(int width, int height)
|
---|
| 968 | {
|
---|
| 969 | int numRefreshRates = al_get_num_display_modes();
|
---|
| 970 | ALLEGRO_DISPLAY_MODE displayMode;
|
---|
| 971 |
|
---|
| 972 | for(int i=0; i<numRefreshRates; i++) {
|
---|
| 973 | al_get_display_mode(i, &displayMode);
|
---|
| 974 |
|
---|
| 975 | if (displayMode.width == width && displayMode.height == height)
|
---|
| 976 | return displayMode.refresh_rate;
|
---|
| 977 | }
|
---|
| 978 |
|
---|
| 979 | return 0;
|
---|
| 980 | }
|
---|
| 981 |
|
---|
| 982 | void drawMessageStatus(ALLEGRO_FONT* font)
|
---|
| 983 | {
|
---|
| 984 | int clientMsgOffset = 5;
|
---|
| 985 | int serverMsgOffset = 950;
|
---|
| 986 |
|
---|
| 987 | al_draw_text(font, al_map_rgb(0, 255, 255), 0+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
|
---|
| 988 | al_draw_text(font, al_map_rgb(0, 255, 255), 20+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Type");
|
---|
| 989 | al_draw_text(font, al_map_rgb(0, 255, 255), 240+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Acked?");
|
---|
| 990 |
|
---|
| 991 | al_draw_text(font, al_map_rgb(0, 255, 255), serverMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
|
---|
| 992 |
|
---|
| 993 | map<unsigned int, map<unsigned long, MessageContainer> >& sentMessages = msgProcessor.getSentMessages();
|
---|
| 994 | int id, type;
|
---|
| 995 | bool acked;
|
---|
| 996 | ostringstream ossId, ossAcked;
|
---|
| 997 |
|
---|
| 998 | map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
|
---|
| 999 |
|
---|
| 1000 | int msgCount = 0;
|
---|
| 1001 | for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
|
---|
| 1002 | map<unsigned long, MessageContainer> playerMessage = it->second;
|
---|
| 1003 | map<unsigned long, MessageContainer>::iterator it2;
|
---|
| 1004 | for (it2 = playerMessage.begin(); it2 != playerMessage.end(); it2++) {
|
---|
| 1005 |
|
---|
| 1006 | id = it->first;
|
---|
| 1007 | ossId.str("");;
|
---|
| 1008 | ossId << id;
|
---|
| 1009 |
|
---|
| 1010 | type = it2->second.getMessage()->type;
|
---|
| 1011 | string typeStr = MessageContainer::getMsgTypeString(type);
|
---|
| 1012 |
|
---|
| 1013 | acked = it2->second.getAcked();
|
---|
| 1014 | ossAcked.str("");;
|
---|
| 1015 | ossAcked << boolalpha << acked;
|
---|
| 1016 |
|
---|
| 1017 | al_draw_text(font, al_map_rgb(0, 255, 0), clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
|
---|
| 1018 | al_draw_text(font, al_map_rgb(0, 255, 0), 20+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, typeStr.c_str());
|
---|
| 1019 | al_draw_text(font, al_map_rgb(0, 255, 0), 240+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossAcked.str().c_str());
|
---|
| 1020 |
|
---|
| 1021 | msgCount++;
|
---|
| 1022 | }
|
---|
| 1023 | }
|
---|
| 1024 |
|
---|
| 1025 | if (msgProcessor.getAckedMessages().size() > 0) {
|
---|
| 1026 | map<unsigned int, unsigned long long> ackedMessages = msgProcessor.getAckedMessages()[0];
|
---|
| 1027 | map<unsigned int, unsigned long long>::iterator it3;
|
---|
| 1028 |
|
---|
| 1029 | msgCount = 0;
|
---|
| 1030 | for (it3 = ackedMessages.begin(); it3 != ackedMessages.end(); it3++) {
|
---|
| 1031 | ossId.str("");;
|
---|
| 1032 | ossId << it3->first;
|
---|
| 1033 |
|
---|
| 1034 | al_draw_text(font, al_map_rgb(255, 0, 0), 25+serverMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
|
---|
| 1035 |
|
---|
| 1036 | msgCount++;
|
---|
| 1037 | }
|
---|
| 1038 | }
|
---|
| 1039 | }
|
---|
| 1040 |
|
---|
| 1041 | // Callback definitions
|
---|
| 1042 |
|
---|
[5c95436] | 1043 | void goToRegisterScreen()
|
---|
| 1044 | {
|
---|
| 1045 | txtUsernameRegister->clear();
|
---|
| 1046 | txtPasswordRegister->clear();
|
---|
[49da01a] | 1047 | lblRegisterStatus->setText("");
|
---|
| 1048 | rblClasses->setSelectedButton(-1);
|
---|
| 1049 |
|
---|
| 1050 | wndCurrent = wndRegister;
|
---|
[5c95436] | 1051 | }
|
---|
| 1052 |
|
---|
| 1053 | void goToLoginScreen()
|
---|
[87b3ee2] | 1054 | {
|
---|
| 1055 | txtUsername->clear();
|
---|
| 1056 | txtPassword->clear();
|
---|
[49da01a] | 1057 | lblLoginStatus->setText("");
|
---|
| 1058 |
|
---|
| 1059 | wndCurrent = wndLogin;
|
---|
[5c95436] | 1060 | }
|
---|
| 1061 |
|
---|
[bc70282] | 1062 | // maybe need a goToGameScreen function as well and add state changes to these functions as well
|
---|
| 1063 |
|
---|
[5c95436] | 1064 | void registerAccount()
|
---|
| 1065 | {
|
---|
| 1066 | string username = txtUsernameRegister->getStr();
|
---|
| 1067 | string password = txtPasswordRegister->getStr();
|
---|
| 1068 |
|
---|
| 1069 | txtUsernameRegister->clear();
|
---|
| 1070 | txtPasswordRegister->clear();
|
---|
| 1071 | // maybe clear rblClasses as well (add a method to RadioButtonList to enable this)
|
---|
| 1072 |
|
---|
| 1073 | Player::PlayerClass playerClass;
|
---|
| 1074 |
|
---|
| 1075 | switch (rblClasses->getSelectedButton()) {
|
---|
| 1076 | case 0:
|
---|
| 1077 | playerClass = Player::CLASS_WARRIOR;
|
---|
| 1078 | break;
|
---|
| 1079 | case 1:
|
---|
| 1080 | playerClass = Player::CLASS_RANGER;
|
---|
| 1081 | break;
|
---|
| 1082 | default:
|
---|
| 1083 | cout << "Invalid class selection" << endl;
|
---|
| 1084 | playerClass = Player::CLASS_NONE;
|
---|
| 1085 | break;
|
---|
| 1086 | }
|
---|
[87b3ee2] | 1087 |
|
---|
| 1088 | msgTo.type = MSG_TYPE_REGISTER;
|
---|
| 1089 |
|
---|
| 1090 | strcpy(msgTo.buffer, username.c_str());
|
---|
| 1091 | strcpy(msgTo.buffer+username.size()+1, password.c_str());
|
---|
[5c95436] | 1092 | memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
|
---|
[87b3ee2] | 1093 |
|
---|
[753fa8a] | 1094 | msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
|
---|
[87b3ee2] | 1095 | }
|
---|
| 1096 |
|
---|
| 1097 | void login()
|
---|
| 1098 | {
|
---|
| 1099 | string strUsername = txtUsername->getStr();
|
---|
| 1100 | string strPassword = txtPassword->getStr();
|
---|
| 1101 | username = strUsername;
|
---|
| 1102 |
|
---|
| 1103 | txtUsername->clear();
|
---|
| 1104 | txtPassword->clear();
|
---|
| 1105 |
|
---|
| 1106 | msgTo.type = MSG_TYPE_LOGIN;
|
---|
| 1107 |
|
---|
| 1108 | strcpy(msgTo.buffer, strUsername.c_str());
|
---|
| 1109 | strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
|
---|
| 1110 |
|
---|
[753fa8a] | 1111 | msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
|
---|
[bc70282] | 1112 |
|
---|
[1785314] | 1113 | state = STATE_LOBBY;
|
---|
[87b3ee2] | 1114 | }
|
---|
| 1115 |
|
---|
| 1116 | void logout()
|
---|
| 1117 | {
|
---|
[929b4e0] | 1118 | switch(state) {
|
---|
| 1119 | case STATE_LOBBY:
|
---|
| 1120 | txtJoinGame->clear();
|
---|
| 1121 | txtCreateGame->clear();
|
---|
| 1122 | break;
|
---|
| 1123 | case STATE_GAME:
|
---|
| 1124 | txtChat->clear();
|
---|
| 1125 | chatConsole.clear();
|
---|
| 1126 | break;
|
---|
| 1127 | default:
|
---|
| 1128 | cout << "Logout called from invalid state: " << state << endl;
|
---|
| 1129 | break;
|
---|
| 1130 | }
|
---|
[87b3ee2] | 1131 |
|
---|
| 1132 | msgTo.type = MSG_TYPE_LOGOUT;
|
---|
| 1133 |
|
---|
| 1134 | strcpy(msgTo.buffer, username.c_str());
|
---|
| 1135 |
|
---|
[753fa8a] | 1136 | msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
|
---|
[87b3ee2] | 1137 | }
|
---|
| 1138 |
|
---|
| 1139 | void quit()
|
---|
| 1140 | {
|
---|
| 1141 | doexit = true;
|
---|
| 1142 | }
|
---|
| 1143 |
|
---|
| 1144 | void sendChatMessage()
|
---|
| 1145 | {
|
---|
| 1146 | string msg = txtChat->getStr();
|
---|
| 1147 | txtChat->clear();
|
---|
| 1148 |
|
---|
| 1149 | msgTo.type = MSG_TYPE_CHAT;
|
---|
| 1150 | strcpy(msgTo.buffer, msg.c_str());
|
---|
| 1151 |
|
---|
[753fa8a] | 1152 | msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
|
---|
[87b3ee2] | 1153 | }
|
---|
[1f1eb58] | 1154 |
|
---|
[b35b2b2] | 1155 | void toggleDebugging()
|
---|
| 1156 | {
|
---|
| 1157 | debugging = !debugging;
|
---|
| 1158 | }
|
---|
| 1159 |
|
---|
[929b4e0] | 1160 | void joinGame()
|
---|
[b35b2b2] | 1161 | {
|
---|
[929b4e0] | 1162 | cout << "Joining game" << endl;
|
---|
[bbebe9c] | 1163 |
|
---|
| 1164 | string msg = txtJoinGame->getStr();
|
---|
| 1165 | txtJoinGame->clear();
|
---|
| 1166 |
|
---|
| 1167 | msgTo.type = MSG_TYPE_JOIN_GAME;
|
---|
| 1168 | strcpy(msgTo.buffer, msg.c_str());
|
---|
| 1169 |
|
---|
| 1170 | msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
|
---|
[dee75cc] | 1171 | }
|
---|
[b35b2b2] | 1172 |
|
---|
[929b4e0] | 1173 | void createGame()
|
---|
[b35b2b2] | 1174 | {
|
---|
[929b4e0] | 1175 | cout << "Creating game" << endl;
|
---|
[bbebe9c] | 1176 |
|
---|
| 1177 | string msg = txtCreateGame->getStr();
|
---|
| 1178 | txtCreateGame->clear();
|
---|
| 1179 |
|
---|
[d519032] | 1180 | cout << "Sending message: " << msg.c_str() << endl;
|
---|
| 1181 |
|
---|
[bbebe9c] | 1182 | msgTo.type = MSG_TYPE_CREATE_GAME;
|
---|
| 1183 | strcpy(msgTo.buffer, msg.c_str());
|
---|
| 1184 |
|
---|
[03ba5e3] | 1185 | msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
|
---|
| 1186 | }
|
---|
| 1187 |
|
---|
| 1188 | void leaveGame()
|
---|
| 1189 | {
|
---|
| 1190 | cout << "Leaving game" << endl;
|
---|
| 1191 |
|
---|
| 1192 | game = NULL;
|
---|
| 1193 |
|
---|
| 1194 | state = STATE_LOBBY;
|
---|
| 1195 | wndCurrent = wndLobby;
|
---|
| 1196 |
|
---|
| 1197 | msgTo.type = MSG_TYPE_LEAVE_GAME;
|
---|
| 1198 |
|
---|
[bbebe9c] | 1199 | msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
|
---|
[95ffe57] | 1200 | }
|
---|