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