[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 |
|
---|
[d352805] | 15 | #include <sys/types.h>
|
---|
[88cdae2] | 16 | #include <cstdio>
|
---|
| 17 | #include <cstdlib>
|
---|
[a845faf] | 18 | #include <string>
|
---|
[1912323] | 19 | #include <iostream>
|
---|
[88cdae2] | 20 | #include <sstream>
|
---|
[1912323] | 21 |
|
---|
[eb8adb1] | 22 | #include <map>
|
---|
[1912323] | 23 |
|
---|
[3a79253] | 24 | #include <map>
|
---|
| 25 |
|
---|
[d352805] | 26 | #include <allegro5/allegro.h>
|
---|
| 27 | #include <allegro5/allegro_font.h>
|
---|
| 28 | #include <allegro5/allegro_ttf.h>
|
---|
[88cdae2] | 29 | #include <allegro5/allegro_primitives.h>
|
---|
[7d7df47] | 30 |
|
---|
[b53c6b3] | 31 | #include "../../common/Message.h"
|
---|
[e607c0f] | 32 | #include "../../common/Common.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"
|
---|
[6475138] | 40 | #include "chat.h"
|
---|
| 41 |
|
---|
[a845faf] | 42 | #ifdef WINDOWS
|
---|
[6475138] | 43 | #pragma comment(lib, "ws2_32.lib")
|
---|
[a845faf] | 44 | #endif
|
---|
[1912323] | 45 |
|
---|
| 46 | using namespace std;
|
---|
| 47 |
|
---|
[0dde5da] | 48 | void initWinSock();
|
---|
| 49 | void shutdownWinSock();
|
---|
[fbcfc35] | 50 | 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] | 51 | void drawMap(WorldMap* gameMap);
|
---|
[d09fe76] | 52 | void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId);
|
---|
[62ee2ce] | 53 | POSITION screenToMap(POSITION pos);
|
---|
| 54 | POSITION mapToScreen(POSITION pos);
|
---|
[87b3ee2] | 55 |
|
---|
| 56 | // callbacks
|
---|
| 57 | void registerAccount();
|
---|
| 58 | void login();
|
---|
| 59 | void logout();
|
---|
| 60 | void quit();
|
---|
| 61 | void sendChatMessage();
|
---|
[4da5aa3] | 62 |
|
---|
[1912323] | 63 | void error(const char *);
|
---|
[7d7df47] | 64 |
|
---|
[d352805] | 65 | const float FPS = 60;
|
---|
| 66 | const int SCREEN_W = 640;
|
---|
| 67 | const int SCREEN_H = 480;
|
---|
[0cc431d] | 68 |
|
---|
| 69 | enum STATE {
|
---|
| 70 | STATE_START,
|
---|
[87b3ee2] | 71 | STATE_LOGIN // this means you're already logged in
|
---|
[d352805] | 72 | };
|
---|
[87b3ee2] | 73 |
|
---|
| 74 | int state;
|
---|
| 75 |
|
---|
| 76 | bool doexit;
|
---|
| 77 |
|
---|
| 78 | Window* wndLogin;
|
---|
| 79 | Window* wndMain;
|
---|
| 80 | Window* wndCurrent;
|
---|
| 81 |
|
---|
| 82 | Textbox* txtUsername;
|
---|
| 83 | Textbox* txtPassword;
|
---|
| 84 | Textbox* txtChat;
|
---|
| 85 |
|
---|
| 86 | int sock;
|
---|
| 87 | struct sockaddr_in server, from;
|
---|
| 88 | struct hostent *hp;
|
---|
| 89 | NETWORK_MSG msgTo, msgFrom;
|
---|
| 90 | string username;
|
---|
| 91 | chat chatConsole;
|
---|
[d352805] | 92 |
|
---|
| 93 | int main(int argc, char **argv)
|
---|
| 94 | {
|
---|
| 95 | ALLEGRO_DISPLAY *display = NULL;
|
---|
| 96 | ALLEGRO_EVENT_QUEUE *event_queue = NULL;
|
---|
| 97 | ALLEGRO_TIMER *timer = NULL;
|
---|
| 98 | bool key[4] = { false, false, false, false };
|
---|
| 99 | bool redraw = true;
|
---|
[87b3ee2] | 100 | doexit = false;
|
---|
[eb8adb1] | 101 | map<unsigned int, Player> mapPlayers;
|
---|
[fbcfc35] | 102 | map<unsigned int, Projectile> mapProjectiles;
|
---|
[88cdae2] | 103 | unsigned int curPlayerId = -1;
|
---|
[15efb4e] | 104 | int scoreBlue, scoreRed;
|
---|
| 105 |
|
---|
| 106 | scoreBlue = 0;
|
---|
| 107 | scoreRed = 0;
|
---|
[9a3e6b1] | 108 |
|
---|
[87b3ee2] | 109 | state = STATE_START;
|
---|
[9a3e6b1] | 110 |
|
---|
[d352805] | 111 | if(!al_init()) {
|
---|
| 112 | fprintf(stderr, "failed to initialize allegro!\n");
|
---|
| 113 | return -1;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[88cdae2] | 116 | if (al_init_primitives_addon())
|
---|
| 117 | cout << "Primitives initialized" << endl;
|
---|
| 118 | else
|
---|
| 119 | cout << "Primitives not initialized" << endl;
|
---|
| 120 |
|
---|
[d352805] | 121 | al_init_font_addon();
|
---|
| 122 | al_init_ttf_addon();
|
---|
| 123 |
|
---|
[88cdae2] | 124 | #if defined WINDOWS
|
---|
| 125 | ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0);
|
---|
| 126 | #elif defined LINUX
|
---|
| 127 | ALLEGRO_FONT *font = al_load_ttf_font("pirulen.ttf", 12, 0);
|
---|
| 128 | #endif
|
---|
| 129 |
|
---|
[d352805] | 130 | if (!font) {
|
---|
| 131 | fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
|
---|
| 132 | getchar();
|
---|
| 133 | return -1;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | if(!al_install_keyboard()) {
|
---|
| 137 | fprintf(stderr, "failed to initialize the keyboard!\n");
|
---|
| 138 | return -1;
|
---|
| 139 | }
|
---|
[87b3ee2] | 140 |
|
---|
| 141 | if(!al_install_mouse()) {
|
---|
| 142 | fprintf(stderr, "failed to initialize the mouse!\n");
|
---|
| 143 | return -1;
|
---|
| 144 | }
|
---|
[d352805] | 145 |
|
---|
| 146 | timer = al_create_timer(1.0 / FPS);
|
---|
| 147 | if(!timer) {
|
---|
| 148 | fprintf(stderr, "failed to create timer!\n");
|
---|
| 149 | return -1;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | display = al_create_display(SCREEN_W, SCREEN_H);
|
---|
| 153 | if(!display) {
|
---|
| 154 | fprintf(stderr, "failed to create display!\n");
|
---|
| 155 | al_destroy_timer(timer);
|
---|
| 156 | return -1;
|
---|
| 157 | }
|
---|
[87b3ee2] | 158 |
|
---|
[384b7e0] | 159 | WorldMap* gameMap = WorldMap::loadMapFromFile("../../data/map.txt");
|
---|
[62ee2ce] | 160 |
|
---|
[87b3ee2] | 161 | wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
| 162 | wndLogin->addComponent(new Textbox(104, 40, 100, 20, font));
|
---|
| 163 | wndLogin->addComponent(new Textbox(104, 70, 100, 20, font));
|
---|
| 164 | wndLogin->addComponent(new Button(22, 100, 90, 20, font, "Register", registerAccount));
|
---|
| 165 | wndLogin->addComponent(new Button(122, 100, 60, 20, font, "Login", login));
|
---|
| 166 | wndLogin->addComponent(new Button(540, 10, 80, 20, font, "Quit", quit));
|
---|
| 167 |
|
---|
| 168 | txtUsername = (Textbox*)wndLogin->getComponent(0);
|
---|
| 169 | txtPassword = (Textbox*)wndLogin->getComponent(1);
|
---|
| 170 |
|
---|
| 171 | wndMain = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
| 172 | wndMain->addComponent(new Textbox(95, 40, 525, 20, font));
|
---|
| 173 | wndMain->addComponent(new Button(95, 70, 160, 20, font, "Send Message", sendChatMessage));
|
---|
| 174 | wndMain->addComponent(new Button(540, 10, 80, 20, font, "Logout", logout));
|
---|
| 175 |
|
---|
| 176 | txtChat = (Textbox*)wndMain->getComponent(0);
|
---|
| 177 |
|
---|
| 178 | wndCurrent = wndLogin;
|
---|
[d352805] | 179 |
|
---|
| 180 | event_queue = al_create_event_queue();
|
---|
| 181 | if(!event_queue) {
|
---|
| 182 | fprintf(stderr, "failed to create event_queue!\n");
|
---|
| 183 | al_destroy_display(display);
|
---|
| 184 | al_destroy_timer(timer);
|
---|
| 185 | return -1;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | al_set_target_bitmap(al_get_backbuffer(display));
|
---|
| 189 |
|
---|
| 190 | al_register_event_source(event_queue, al_get_display_event_source(display));
|
---|
| 191 | al_register_event_source(event_queue, al_get_timer_event_source(timer));
|
---|
| 192 | al_register_event_source(event_queue, al_get_keyboard_event_source());
|
---|
[87b3ee2] | 193 | al_register_event_source(event_queue, al_get_mouse_event_source());
|
---|
[d352805] | 194 |
|
---|
| 195 | al_clear_to_color(al_map_rgb(0,0,0));
|
---|
| 196 |
|
---|
| 197 | al_flip_display();
|
---|
[9a3e6b1] | 198 |
|
---|
| 199 | if (argc != 3) {
|
---|
| 200 | cout << "Usage: server port" << endl;
|
---|
| 201 | exit(1);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | initWinSock();
|
---|
| 205 |
|
---|
| 206 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
| 207 | if (sock < 0)
|
---|
| 208 | error("socket");
|
---|
| 209 |
|
---|
[e607c0f] | 210 | set_nonblock(sock);
|
---|
| 211 |
|
---|
[9a3e6b1] | 212 | server.sin_family = AF_INET;
|
---|
| 213 | hp = gethostbyname(argv[1]);
|
---|
| 214 | if (hp==0)
|
---|
| 215 | error("Unknown host");
|
---|
| 216 |
|
---|
| 217 | memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
|
---|
| 218 | server.sin_port = htons(atoi(argv[2]));
|
---|
| 219 |
|
---|
[d352805] | 220 | al_start_timer(timer);
|
---|
[e607c0f] | 221 |
|
---|
[d352805] | 222 | while(!doexit)
|
---|
| 223 | {
|
---|
| 224 | ALLEGRO_EVENT ev;
|
---|
[3d81c0d] | 225 |
|
---|
[d352805] | 226 | al_wait_for_event(event_queue, &ev);
|
---|
[87b3ee2] | 227 |
|
---|
| 228 | if(wndCurrent->handleEvent(ev)) {
|
---|
| 229 | // do nothing
|
---|
| 230 | }
|
---|
| 231 | else if(ev.type == ALLEGRO_EVENT_TIMER) {
|
---|
[a1a3bd5] | 232 | redraw = true; // seems like we should just call a draw function here instead
|
---|
[d352805] | 233 | }
|
---|
| 234 | else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
|
---|
[9a3e6b1] | 235 | doexit = true;
|
---|
[d352805] | 236 | }
|
---|
| 237 | else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
|
---|
| 238 | }
|
---|
| 239 | else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
|
---|
| 240 | switch(ev.keyboard.keycode) {
|
---|
| 241 | case ALLEGRO_KEY_ESCAPE:
|
---|
| 242 | doexit = true;
|
---|
| 243 | break;
|
---|
[4926168] | 244 | case ALLEGRO_KEY_S: // pickup an item next to you
|
---|
| 245 | if (state == STATE_LOGIN) {
|
---|
| 246 | msgTo.type = MSG_TYPE_PICKUP_FLAG;
|
---|
| 247 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
| 248 | sendMessage(&msgTo, sock, &server);
|
---|
| 249 | }
|
---|
| 250 | break;
|
---|
[626e5b0] | 251 | case ALLEGRO_KEY_D: // drop the current item
|
---|
| 252 | if (state == STATE_LOGIN) {
|
---|
[4926168] | 253 | // find the current player in the player list
|
---|
[626e5b0] | 254 | map<unsigned int, Player>::iterator it;
|
---|
| 255 | Player* p = NULL;
|
---|
| 256 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 257 | {
|
---|
| 258 | if (it->second.id == curPlayerId)
|
---|
| 259 | p = &it->second;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | if (p != NULL) {
|
---|
| 263 | int flagType = WorldMap::OBJECT_NONE;
|
---|
| 264 |
|
---|
| 265 | if (p->hasBlueFlag)
|
---|
| 266 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
| 267 | else if (p->hasRedFlag)
|
---|
| 268 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
| 269 |
|
---|
| 270 | if (flagType != WorldMap::OBJECT_NONE) {
|
---|
| 271 | msgTo.type = MSG_TYPE_DROP_FLAG;
|
---|
| 272 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
| 273 | sendMessage(&msgTo, sock, &server);
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 | break;
|
---|
[d352805] | 278 | }
|
---|
| 279 | }
|
---|
[88cdae2] | 280 | else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
|
---|
[ad5d122] | 281 | if(wndCurrent == wndMain) {
|
---|
[e1f78f5] | 282 | if (ev.mouse.button == 1) { // left click
|
---|
| 283 | msgTo.type = MSG_TYPE_PLAYER_MOVE;
|
---|
[88cdae2] | 284 |
|
---|
[e1f78f5] | 285 | POSITION pos;
|
---|
| 286 | pos.x = ev.mouse.x;
|
---|
| 287 | pos.y = ev.mouse.y;
|
---|
| 288 | pos = screenToMap(pos);
|
---|
[62ee2ce] | 289 |
|
---|
[e1f78f5] | 290 | if (pos.x != -1)
|
---|
| 291 | {
|
---|
| 292 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
| 293 | memcpy(msgTo.buffer+4, &pos.x, 4);
|
---|
| 294 | memcpy(msgTo.buffer+8, &pos.y, 4);
|
---|
| 295 |
|
---|
| 296 | sendMessage(&msgTo, sock, &server);
|
---|
| 297 | }
|
---|
| 298 | else
|
---|
| 299 | cout << "Invalid point: User did not click on the map" << endl;
|
---|
| 300 | }else if (ev.mouse.button == 2) { // right click
|
---|
| 301 | map<unsigned int, Player>::iterator it;
|
---|
| 302 |
|
---|
[fbcfc35] | 303 | cout << "Detected a right-click" << endl;
|
---|
| 304 |
|
---|
[e1f78f5] | 305 | Player* curPlayer;
|
---|
| 306 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 307 | {
|
---|
| 308 | if (it->second.id == curPlayerId)
|
---|
| 309 | curPlayer = &it->second;
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | Player* target;
|
---|
| 313 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 314 | {
|
---|
[fbcfc35] | 315 | // need to check if the right-click was actually on this player
|
---|
[e1f78f5] | 316 | target = &it->second;
|
---|
| 317 | if (target->id != curPlayerId && target->team != curPlayer->team) {
|
---|
| 318 | msgTo.type = MSG_TYPE_START_ATTACK;
|
---|
| 319 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
| 320 | memcpy(msgTo.buffer+4, &target->id, 4);
|
---|
[88cdae2] | 321 |
|
---|
[e1f78f5] | 322 | sendMessage(&msgTo, sock, &server);
|
---|
| 323 | }
|
---|
| 324 | }
|
---|
[62ee2ce] | 325 | }
|
---|
[ad5d122] | 326 | }
|
---|
[88cdae2] | 327 | }
|
---|
[e607c0f] | 328 |
|
---|
| 329 | if (receiveMessage(&msgFrom, sock, &from) >= 0)
|
---|
[fbcfc35] | 330 | processMessage(msgFrom, state, chatConsole, gameMap, mapPlayers, mapProjectiles, curPlayerId, scoreBlue, scoreRed);
|
---|
[054b50b] | 331 |
|
---|
[a1a3bd5] | 332 | if (redraw)
|
---|
[e607c0f] | 333 | {
|
---|
[d352805] | 334 | redraw = false;
|
---|
[88cdae2] | 335 |
|
---|
[87b3ee2] | 336 | wndCurrent->draw(display);
|
---|
[9a3e6b1] | 337 |
|
---|
[6475138] | 338 | chatConsole.draw(font, al_map_rgb(255,255,255));
|
---|
[9a3e6b1] | 339 |
|
---|
[87b3ee2] | 340 | if(wndCurrent == wndLogin) {
|
---|
| 341 | al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Username:");
|
---|
| 342 | al_draw_text(font, al_map_rgb(0, 255, 0), 1, 73, ALLEGRO_ALIGN_LEFT, "Password:");
|
---|
| 343 | }
|
---|
| 344 | else if(wndCurrent == wndMain) {
|
---|
| 345 | al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
|
---|
[62ee2ce] | 346 |
|
---|
[15efb4e] | 347 | ostringstream ossScoreBlue, ossScoreRed;
|
---|
| 348 |
|
---|
| 349 | ossScoreBlue << "Blue: " << scoreBlue << endl;
|
---|
| 350 | ossScoreRed << "Red: " << scoreRed << endl;
|
---|
| 351 |
|
---|
| 352 | al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
|
---|
| 353 | al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
|
---|
| 354 |
|
---|
[a1a3bd5] | 355 | // update player positions
|
---|
| 356 | map<unsigned int, Player>::iterator it;
|
---|
| 357 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 358 | {
|
---|
[227baaa] | 359 | it->second.move(gameMap); // ignore return value
|
---|
[a1a3bd5] | 360 | }
|
---|
| 361 |
|
---|
[62ee2ce] | 362 | drawMap(gameMap);
|
---|
[d09fe76] | 363 | drawPlayers(mapPlayers, font, curPlayerId);
|
---|
[87b3ee2] | 364 | }
|
---|
| 365 |
|
---|
[d352805] | 366 | al_flip_display();
|
---|
| 367 | }
|
---|
| 368 | }
|
---|
[9a3e6b1] | 369 |
|
---|
| 370 | #if defined WINDOWS
|
---|
| 371 | closesocket(sock);
|
---|
| 372 | #elif defined LINUX
|
---|
| 373 | close(sock);
|
---|
| 374 | #endif
|
---|
| 375 |
|
---|
| 376 | shutdownWinSock();
|
---|
[d352805] | 377 |
|
---|
[87b3ee2] | 378 | delete wndLogin;
|
---|
| 379 | delete wndMain;
|
---|
| 380 |
|
---|
[62ee2ce] | 381 | delete gameMap;
|
---|
| 382 |
|
---|
[d352805] | 383 | al_destroy_event_queue(event_queue);
|
---|
| 384 | al_destroy_display(display);
|
---|
| 385 | al_destroy_timer(timer);
|
---|
| 386 |
|
---|
| 387 | return 0;
|
---|
| 388 | }
|
---|
| 389 |
|
---|
[4da5aa3] | 390 | // need to make a function like this that works on windows
|
---|
| 391 | void error(const char *msg)
|
---|
| 392 | {
|
---|
| 393 | perror(msg);
|
---|
| 394 | shutdownWinSock();
|
---|
| 395 | exit(1);
|
---|
| 396 | }
|
---|
| 397 |
|
---|
[0dde5da] | 398 | void initWinSock()
|
---|
| 399 | {
|
---|
| 400 | #if defined WINDOWS
|
---|
| 401 | WORD wVersionRequested;
|
---|
| 402 | WSADATA wsaData;
|
---|
| 403 | int wsaerr;
|
---|
| 404 |
|
---|
| 405 | wVersionRequested = MAKEWORD(2, 2);
|
---|
| 406 | wsaerr = WSAStartup(wVersionRequested, &wsaData);
|
---|
| 407 |
|
---|
| 408 | if (wsaerr != 0) {
|
---|
| 409 | cout << "The Winsock dll not found." << endl;
|
---|
| 410 | exit(1);
|
---|
| 411 | }else
|
---|
| 412 | cout << "The Winsock dll was found." << endl;
|
---|
| 413 | #endif
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | void shutdownWinSock()
|
---|
| 417 | {
|
---|
| 418 | #if defined WINDOWS
|
---|
| 419 | WSACleanup();
|
---|
| 420 | #endif
|
---|
[1912323] | 421 | }
|
---|
| 422 |
|
---|
[62ee2ce] | 423 | POSITION screenToMap(POSITION pos)
|
---|
| 424 | {
|
---|
| 425 | pos.x = pos.x-300;
|
---|
| 426 | pos.y = pos.y-100;
|
---|
| 427 |
|
---|
| 428 | if (pos.x < 0 || pos.y < 0)
|
---|
| 429 | {
|
---|
| 430 | pos.x = -1;
|
---|
| 431 | pos.y = -1;
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 | return pos;
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | POSITION mapToScreen(POSITION pos)
|
---|
| 438 | {
|
---|
| 439 | pos.x = pos.x+300;
|
---|
| 440 | pos.y = pos.y+100;
|
---|
| 441 |
|
---|
| 442 | return pos;
|
---|
| 443 | }
|
---|
| 444 |
|
---|
[ca44f82] | 445 | POSITION mapToScreen(FLOAT_POSITION pos)
|
---|
| 446 | {
|
---|
| 447 | POSITION p;
|
---|
| 448 | p.x = pos.x+300;
|
---|
| 449 | p.y = pos.y+100;
|
---|
| 450 |
|
---|
| 451 | return p;
|
---|
| 452 | }
|
---|
| 453 |
|
---|
[fbcfc35] | 454 | 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] | 455 | {
|
---|
[4da5aa3] | 456 | string response = string(msg.buffer);
|
---|
| 457 |
|
---|
[a1a3bd5] | 458 | cout << "Processing message" << endl;
|
---|
| 459 |
|
---|
[4da5aa3] | 460 | switch(state)
|
---|
| 461 | {
|
---|
| 462 | case STATE_START:
|
---|
| 463 | {
|
---|
[e607c0f] | 464 | cout << "In STATE_START" << endl;
|
---|
| 465 |
|
---|
[87b3ee2] | 466 | switch(msg.type)
|
---|
[4da5aa3] | 467 | {
|
---|
[87b3ee2] | 468 | case MSG_TYPE_REGISTER:
|
---|
| 469 | {
|
---|
| 470 | break;
|
---|
| 471 | }
|
---|
| 472 | case MSG_TYPE_LOGIN:
|
---|
| 473 | {
|
---|
| 474 | if (response.compare("Player has already logged in.") == 0)
|
---|
| 475 | {
|
---|
| 476 | username.clear();
|
---|
| 477 | cout << "User login failed" << endl;
|
---|
| 478 | }
|
---|
| 479 | else if (response.compare("Incorrect username or password") == 0)
|
---|
| 480 | {
|
---|
| 481 | username.clear();
|
---|
| 482 | cout << "User login failed" << endl;
|
---|
| 483 | }
|
---|
| 484 | else
|
---|
| 485 | {
|
---|
| 486 | state = STATE_LOGIN;
|
---|
| 487 | wndCurrent = wndMain;
|
---|
[88cdae2] | 488 |
|
---|
| 489 | Player p("", "");
|
---|
| 490 | p.deserialize(msg.buffer);
|
---|
| 491 | mapPlayers[p.id] = p;
|
---|
| 492 | curPlayerId = p.id;
|
---|
| 493 |
|
---|
| 494 | cout << "Got a valid login response with the player" << endl;
|
---|
| 495 | cout << "Player id: " << curPlayerId << endl;
|
---|
[a1a3bd5] | 496 | cout << "map size: " << mapPlayers.size() << endl;
|
---|
[87b3ee2] | 497 | }
|
---|
[88cdae2] | 498 |
|
---|
[a1a3bd5] | 499 | break;
|
---|
| 500 | }
|
---|
| 501 | case MSG_TYPE_PLAYER: // kind of hacky to put this here
|
---|
| 502 | {
|
---|
[7511a2b] | 503 | cout << "Got MSG_TYPE_PLAYER message in STATE_START" << endl;
|
---|
[a1a3bd5] | 504 |
|
---|
| 505 | Player p("", "");
|
---|
| 506 | p.deserialize(msg.buffer);
|
---|
| 507 | p.timeLastUpdated = getCurrentMillis();
|
---|
| 508 | mapPlayers[p.id] = p;
|
---|
| 509 |
|
---|
| 510 | cout << "new player id: " << p.id << endl;
|
---|
| 511 | cout << "map size: " << mapPlayers.size() << endl;
|
---|
| 512 |
|
---|
[45b2750] | 513 | break;
|
---|
| 514 | }
|
---|
| 515 | case MSG_TYPE_OBJECT:
|
---|
| 516 | {
|
---|
[7511a2b] | 517 | cout << "Received OBJECT message in STATE_START." << endl;
|
---|
[45b2750] | 518 |
|
---|
| 519 | WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
|
---|
| 520 | o.deserialize(msg.buffer);
|
---|
[7511a2b] | 521 | cout << "object id: " << o.id << endl;
|
---|
[45b2750] | 522 | gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
|
---|
| 523 |
|
---|
[87b3ee2] | 524 | break;
|
---|
| 525 | }
|
---|
[4da5aa3] | 526 | }
|
---|
| 527 |
|
---|
| 528 | break;
|
---|
| 529 | }
|
---|
| 530 | case STATE_LOGIN:
|
---|
| 531 | {
|
---|
[eb8adb1] | 532 | switch(msg.type)
|
---|
[4da5aa3] | 533 | {
|
---|
[87b3ee2] | 534 | case MSG_TYPE_REGISTER:
|
---|
| 535 | {
|
---|
| 536 | break;
|
---|
| 537 | }
|
---|
| 538 | case MSG_TYPE_LOGIN:
|
---|
| 539 | {
|
---|
[a1a3bd5] | 540 | cout << "Got a login message" << endl;
|
---|
| 541 |
|
---|
[eb8adb1] | 542 | chatConsole.addLine(response);
|
---|
[a1a3bd5] | 543 | cout << "Added new line" << endl;
|
---|
[eb8adb1] | 544 |
|
---|
[a1a3bd5] | 545 | break;
|
---|
| 546 | }
|
---|
| 547 | case MSG_TYPE_LOGOUT:
|
---|
| 548 | {
|
---|
[054b50b] | 549 | cout << "Got a logout message" << endl;
|
---|
[a1a3bd5] | 550 |
|
---|
[87b3ee2] | 551 | if (response.compare("You have successfully logged out.") == 0)
|
---|
| 552 | {
|
---|
| 553 | cout << "Logged out" << endl;
|
---|
| 554 | state = STATE_START;
|
---|
| 555 | wndCurrent = wndLogin;
|
---|
| 556 | }
|
---|
[054b50b] | 557 |
|
---|
| 558 | break;
|
---|
| 559 | }
|
---|
[4c202e0] | 560 | case MSG_TYPE_PLAYER:
|
---|
| 561 | {
|
---|
[7511a2b] | 562 | cout << "Got MSG_TYPE_PLAYER message in STATE_LOGIN" << endl;
|
---|
[a1a3bd5] | 563 |
|
---|
[60776f2] | 564 | Player p("", "");
|
---|
| 565 | p.deserialize(msg.buffer);
|
---|
[054b50b] | 566 | p.timeLastUpdated = getCurrentMillis();
|
---|
[3a79253] | 567 | mapPlayers[p.id] = p;
|
---|
[4c202e0] | 568 |
|
---|
[eb8adb1] | 569 | break;
|
---|
| 570 | }
|
---|
[a1a3bd5] | 571 | case MSG_TYPE_PLAYER_MOVE:
|
---|
| 572 | {
|
---|
| 573 | cout << "Got a player move message" << endl;
|
---|
| 574 |
|
---|
| 575 | unsigned int id;
|
---|
| 576 | int x, y;
|
---|
| 577 |
|
---|
| 578 | memcpy(&id, msg.buffer, 4);
|
---|
| 579 | memcpy(&x, msg.buffer+4, 4);
|
---|
| 580 | memcpy(&y, msg.buffer+8, 4);
|
---|
| 581 |
|
---|
| 582 | mapPlayers[id].target.x = x;
|
---|
| 583 | mapPlayers[id].target.y = y;
|
---|
| 584 |
|
---|
| 585 | break;
|
---|
| 586 | }
|
---|
[eb8adb1] | 587 | case MSG_TYPE_CHAT:
|
---|
| 588 | {
|
---|
| 589 | chatConsole.addLine(response);
|
---|
[4c202e0] | 590 |
|
---|
[87b3ee2] | 591 | break;
|
---|
| 592 | }
|
---|
[45b2750] | 593 | case MSG_TYPE_OBJECT:
|
---|
| 594 | {
|
---|
[7511a2b] | 595 | cout << "Received object message in STATE_LOGIN." << endl;
|
---|
[45b2750] | 596 |
|
---|
| 597 | WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
|
---|
| 598 | o.deserialize(msg.buffer);
|
---|
| 599 | gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
|
---|
| 600 |
|
---|
| 601 | break;
|
---|
| 602 | }
|
---|
[2df63d6] | 603 | case MSG_TYPE_REMOVE_OBJECT:
|
---|
| 604 | {
|
---|
[7511a2b] | 605 | cout << "Received REMOVE_OBJECT message!" << endl;
|
---|
| 606 |
|
---|
[2df63d6] | 607 | int id;
|
---|
| 608 | memcpy(&id, msg.buffer, 4);
|
---|
[7511a2b] | 609 |
|
---|
| 610 | cout << "Removing object with id " << id << endl;
|
---|
| 611 |
|
---|
[2df63d6] | 612 | if (!gameMap->removeObject(id))
|
---|
| 613 | cout << "Did not remove the object" << endl;
|
---|
[7511a2b] | 614 |
|
---|
| 615 | break;
|
---|
[2df63d6] | 616 | }
|
---|
[15efb4e] | 617 | case MSG_TYPE_SCORE:
|
---|
| 618 | {
|
---|
| 619 | memcpy(&scoreBlue, msg.buffer, 4);
|
---|
| 620 | memcpy(&scoreRed, msg.buffer+4, 4);
|
---|
| 621 |
|
---|
| 622 | break;
|
---|
| 623 | }
|
---|
[b978503] | 624 | case MSG_TYPE_ATTACK:
|
---|
| 625 | {
|
---|
| 626 | break;
|
---|
| 627 | }
|
---|
| 628 | case MSG_TYPE_PROJECTILE:
|
---|
| 629 | {
|
---|
[fbcfc35] | 630 | cout << "Received a prjectile message" << endl;
|
---|
| 631 |
|
---|
| 632 | int id, x, y, targetId;
|
---|
| 633 |
|
---|
| 634 | memcpy(&id, msg.buffer, 4);
|
---|
| 635 | memcpy(&x, msg.buffer+4, 4);
|
---|
| 636 | memcpy(&y, msg.buffer+8, 4);
|
---|
| 637 | memcpy(&targetId, msg.buffer+12, 4);
|
---|
| 638 |
|
---|
| 639 | cout << "id" << id << endl;
|
---|
| 640 | cout << "x" << x << endl;
|
---|
| 641 | cout << "y" << y << endl;
|
---|
| 642 | cout << "Target" << targetId << endl;
|
---|
| 643 |
|
---|
[b978503] | 644 | break;
|
---|
| 645 | }
|
---|
| 646 | case MSG_TYPE_REMOVE_PROJECTILE:
|
---|
| 647 | {
|
---|
| 648 | break;
|
---|
| 649 | }
|
---|
[45b2750] | 650 | default:
|
---|
| 651 | {
|
---|
| 652 | cout << "Received an unexpected message type: " << msg.type << endl;
|
---|
| 653 | }
|
---|
[4da5aa3] | 654 | }
|
---|
[eb8adb1] | 655 |
|
---|
[4da5aa3] | 656 | break;
|
---|
| 657 | }
|
---|
| 658 | default:
|
---|
| 659 | {
|
---|
| 660 | cout << "The state has an invalid value: " << state << endl;
|
---|
| 661 |
|
---|
| 662 | break;
|
---|
| 663 | }
|
---|
| 664 | }
|
---|
[0dde5da] | 665 | }
|
---|
[87b3ee2] | 666 |
|
---|
[d436ac4] | 667 | // this should probably be in the WorldMap class
|
---|
[62ee2ce] | 668 | void drawMap(WorldMap* gameMap)
|
---|
| 669 | {
|
---|
| 670 | POSITION mapPos;
|
---|
| 671 | mapPos.x = 0;
|
---|
| 672 | mapPos.y = 0;
|
---|
| 673 | mapPos = mapToScreen(mapPos);
|
---|
[6e66ffd] | 674 |
|
---|
[62ee2ce] | 675 | for (int x=0; x<12; x++)
|
---|
| 676 | {
|
---|
| 677 | for (int y=0; y<12; y++)
|
---|
| 678 | {
|
---|
| 679 | WorldMap::TerrainType el = gameMap->getElement(x, y);
|
---|
[cc1c6c1] | 680 | WorldMap::StructureType structure = gameMap->getStructure(x, y);
|
---|
[62ee2ce] | 681 |
|
---|
| 682 | if (el == WorldMap::TERRAIN_GRASS)
|
---|
| 683 | 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));
|
---|
| 684 | else if (el == WorldMap::TERRAIN_OCEAN)
|
---|
| 685 | 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));
|
---|
| 686 | else if (el == WorldMap::TERRAIN_ROCK)
|
---|
| 687 | 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] | 688 |
|
---|
[6e66ffd] | 689 | if (structure == WorldMap::STRUCTURE_BLUE_FLAG) {
|
---|
| 690 | al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
|
---|
| 691 | //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));
|
---|
| 692 | }else if (structure == WorldMap::STRUCTURE_RED_FLAG) {
|
---|
| 693 | al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
|
---|
| 694 | //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));
|
---|
| 695 | }
|
---|
| 696 | }
|
---|
| 697 | }
|
---|
| 698 |
|
---|
| 699 | for (int x=0; x<12; x++)
|
---|
| 700 | {
|
---|
| 701 | for (int y=0; y<12; y++)
|
---|
| 702 | {
|
---|
| 703 | vector<WorldMap::Object> vctObjects = gameMap->getObjects(x, y);
|
---|
| 704 |
|
---|
| 705 | vector<WorldMap::Object>::iterator it;
|
---|
| 706 | for(it = vctObjects.begin(); it != vctObjects.end(); it++) {
|
---|
| 707 | switch(it->type) {
|
---|
| 708 | case WorldMap::OBJECT_BLUE_FLAG:
|
---|
| 709 | 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));
|
---|
| 710 | break;
|
---|
| 711 | case WorldMap::OBJECT_RED_FLAG:
|
---|
| 712 | 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));
|
---|
| 713 | break;
|
---|
| 714 | }
|
---|
| 715 | }
|
---|
[62ee2ce] | 716 | }
|
---|
| 717 | }
|
---|
| 718 | }
|
---|
| 719 |
|
---|
[d09fe76] | 720 | void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId)
|
---|
[88cdae2] | 721 | {
|
---|
| 722 | map<unsigned int, Player>::iterator it;
|
---|
| 723 |
|
---|
[62ee2ce] | 724 | Player* p;
|
---|
| 725 | POSITION pos;
|
---|
[d09fe76] | 726 | ALLEGRO_COLOR color;
|
---|
[62ee2ce] | 727 |
|
---|
[88cdae2] | 728 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 729 | {
|
---|
[62ee2ce] | 730 | p = &it->second;
|
---|
| 731 | pos = mapToScreen(p->pos);
|
---|
[88cdae2] | 732 |
|
---|
[7efed11] | 733 | if (p->id == curPlayerId)
|
---|
[a6066e8] | 734 | al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));
|
---|
| 735 |
|
---|
| 736 | if (p->team == 0)
|
---|
[d09fe76] | 737 | color = al_map_rgb(0, 0, 255);
|
---|
[a6066e8] | 738 | else if (p->team == 1)
|
---|
[d09fe76] | 739 | color = al_map_rgb(255, 0, 0);
|
---|
| 740 |
|
---|
| 741 | al_draw_filled_circle(pos.x, pos.y, 12, color);
|
---|
| 742 |
|
---|
| 743 | // draw player class
|
---|
| 744 | int fontHeight = al_get_font_line_height(font);
|
---|
| 745 |
|
---|
| 746 | string strClass;
|
---|
| 747 | switch (p->playerClass) {
|
---|
| 748 | case Player::CLASS_WARRIOR:
|
---|
| 749 | strClass = "W";
|
---|
| 750 | break;
|
---|
| 751 | case Player::CLASS_RANGER:
|
---|
| 752 | strClass = "R";
|
---|
| 753 | break;
|
---|
| 754 | case Player::CLASS_NONE:
|
---|
| 755 | strClass = "";
|
---|
| 756 | break;
|
---|
| 757 | default:
|
---|
| 758 | strClass = "";
|
---|
| 759 | break;
|
---|
| 760 | }
|
---|
| 761 | al_draw_text(font, al_map_rgb(0, 0, 0), pos.x, pos.y-fontHeight/2, ALLEGRO_ALIGN_CENTRE, strClass.c_str());
|
---|
| 762 |
|
---|
| 763 | // draw player health
|
---|
| 764 | al_draw_filled_rectangle(pos.x-12, pos.y-24, pos.x+12, pos.y-16, al_map_rgb(0, 0, 0));
|
---|
| 765 | if (it->second.maxHealth != 0)
|
---|
[e1f78f5] | 766 | 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] | 767 |
|
---|
[7d91bbe] | 768 | if (p->hasBlueFlag)
|
---|
[7efed11] | 769 | al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(0, 0, 255));
|
---|
[a6066e8] | 770 | else if (p->hasRedFlag)
|
---|
[7efed11] | 771 | al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(255, 0, 0));
|
---|
[88cdae2] | 772 | }
|
---|
| 773 | }
|
---|
| 774 |
|
---|
[87b3ee2] | 775 | void registerAccount()
|
---|
| 776 | {
|
---|
| 777 | string username = txtUsername->getStr();
|
---|
| 778 | string password = txtPassword->getStr();
|
---|
| 779 |
|
---|
| 780 | txtUsername->clear();
|
---|
| 781 | txtPassword->clear();
|
---|
| 782 |
|
---|
| 783 | msgTo.type = MSG_TYPE_REGISTER;
|
---|
| 784 |
|
---|
| 785 | strcpy(msgTo.buffer, username.c_str());
|
---|
| 786 | strcpy(msgTo.buffer+username.size()+1, password.c_str());
|
---|
| 787 |
|
---|
| 788 | sendMessage(&msgTo, sock, &server);
|
---|
| 789 | }
|
---|
| 790 |
|
---|
| 791 | void login()
|
---|
| 792 | {
|
---|
| 793 | string strUsername = txtUsername->getStr();
|
---|
| 794 | string strPassword = txtPassword->getStr();
|
---|
| 795 | username = strUsername;
|
---|
| 796 |
|
---|
| 797 | txtUsername->clear();
|
---|
| 798 | txtPassword->clear();
|
---|
| 799 |
|
---|
| 800 | msgTo.type = MSG_TYPE_LOGIN;
|
---|
| 801 |
|
---|
| 802 | strcpy(msgTo.buffer, strUsername.c_str());
|
---|
| 803 | strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
|
---|
| 804 |
|
---|
| 805 | sendMessage(&msgTo, sock, &server);
|
---|
| 806 | }
|
---|
| 807 |
|
---|
| 808 | void logout()
|
---|
| 809 | {
|
---|
| 810 | txtChat->clear();
|
---|
| 811 |
|
---|
| 812 | msgTo.type = MSG_TYPE_LOGOUT;
|
---|
| 813 |
|
---|
| 814 | strcpy(msgTo.buffer, username.c_str());
|
---|
| 815 |
|
---|
| 816 | sendMessage(&msgTo, sock, &server);
|
---|
| 817 | }
|
---|
| 818 |
|
---|
| 819 | void quit()
|
---|
| 820 | {
|
---|
| 821 | doexit = true;
|
---|
| 822 | }
|
---|
| 823 |
|
---|
| 824 | void sendChatMessage()
|
---|
| 825 | {
|
---|
| 826 | string msg = txtChat->getStr();
|
---|
| 827 |
|
---|
| 828 | txtChat->clear();
|
---|
| 829 |
|
---|
| 830 | msgTo.type = MSG_TYPE_CHAT;
|
---|
| 831 |
|
---|
| 832 | strcpy(msgTo.buffer, msg.c_str());
|
---|
| 833 |
|
---|
| 834 | sendMessage(&msgTo, sock, &server);
|
---|
| 835 | }
|
---|