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