source: network-game/client/Client/main.cpp@ 297682c

Last change on this file since 297682c was 297682c, checked in by dportnoy <dmp1488@…>, 11 years ago

Minor code refactoring

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