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