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

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

Remove the Message.h include from several files

  • Property mode set to 100644
File size: 29.3 KB
Line 
1#include "../../common/Compiler.h"
2
3#if defined WINDOWS
4 #include <winsock2.h>
5 #include <WS2tcpip.h>
6#elif defined LINUX
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>
13#endif
14
15#include <cstdio>
16#include <cstdlib>
17#include <cmath>
18#include <sys/types.h>
19#include <string>
20#include <iostream>
21#include <sstream>
22#include <map>
23
24#include <allegro5/allegro.h>
25#include <allegro5/allegro_font.h>
26#include <allegro5/allegro_ttf.h>
27#include <allegro5/allegro_primitives.h>
28
29#include "../../common/Common.h"
30#include "../../common/MessageProcessor.h"
31#include "../../common/WorldMap.h"
32#include "../../common/Player.h"
33#include "../../common/Projectile.h"
34
35#include "Window.h"
36#include "Textbox.h"
37#include "Button.h"
38#include "RadioButtonList.h"
39#include "TextLabel.h"
40#include "chat.h"
41
42#ifdef WINDOWS
43 #pragma comment(lib, "ws2_32.lib")
44#endif
45
46using namespace std;
47
48void initWinSock();
49void shutdownWinSock();
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);
51void drawMap(WorldMap* gameMap);
52void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId);
53POSITION screenToMap(POSITION pos);
54POSITION mapToScreen(POSITION pos);
55int getRefreshRate(int width, int height);
56
57// callbacks
58void goToLoginScreen();
59void goToRegisterScreen();
60void registerAccount();
61void login();
62void logout();
63void quit();
64void sendChatMessage();
65
66void error(const char *);
67
68const float FPS = 60;
69const int SCREEN_W = 1024;
70const int SCREEN_H = 768;
71
72enum STATE {
73 STATE_START,
74 STATE_LOGIN // this means you're already logged in
75};
76
77int state;
78
79bool doexit;
80
81Window* wndLogin;
82Window* wndRegister;
83Window* wndMain;
84Window* wndCurrent;
85
86// wndLogin
87Textbox* txtUsername;
88Textbox* txtPassword;
89TextLabel* lblLoginStatus;
90
91// wndRegister
92Textbox* txtUsernameRegister;
93Textbox* txtPasswordRegister;
94RadioButtonList* rblClasses;
95TextLabel* lblRegisterStatus;
96
97// wndMain
98Textbox* txtChat;
99
100int sock;
101struct sockaddr_in server, from;
102struct hostent *hp;
103NETWORK_MSG msgTo, msgFrom;
104string username;
105chat chatConsole;
106
107MessageProcessor msgProcessor;
108
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;
116 doexit = false;
117 map<unsigned int, Player> mapPlayers;
118 map<unsigned int, Projectile> mapProjectiles;
119 unsigned int curPlayerId = -1;
120 int scoreBlue, scoreRed;
121 bool fullscreen = false;
122
123 scoreBlue = 0;
124 scoreRed = 0;
125
126 state = STATE_START;
127
128 if(!al_init()) {
129 fprintf(stderr, "failed to initialize allegro!\n");
130 return -1;
131 }
132
133 if (al_init_primitives_addon())
134 cout << "Primitives initialized" << endl;
135 else
136 cout << "Primitives not initialized" << endl;
137
138 al_init_font_addon();
139 al_init_ttf_addon();
140
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
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 }
157
158 if(!al_install_mouse()) {
159 fprintf(stderr, "failed to initialize the mouse!\n");
160 return -1;
161 }
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
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);
176 if(!display) {
177 fprintf(stderr, "failed to create display!\n");
178 al_destroy_timer(timer);
179 return -1;
180 }
181
182 WorldMap* gameMap = WorldMap::loadMapFromFile("../../data/map.txt");
183
184 cout << "Loaded map" << endl;
185
186 wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
187 wndLogin->addComponent(new Textbox(516, 40, 100, 20, font));
188 wndLogin->addComponent(new Textbox(516, 70, 100, 20, font));
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));
194 wndLogin->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit));
195
196 txtUsername = (Textbox*)wndLogin->getComponent(0);
197 txtPassword = (Textbox*)wndLogin->getComponent(1);
198 lblLoginStatus = (TextLabel*)wndLogin->getComponent(4);
199
200 cout << "Created login screen" << endl;
201
202 wndRegister = new Window(0, 0, SCREEN_W, SCREEN_H);
203 wndRegister->addComponent(new Textbox(516, 40, 100, 20, font));
204 wndRegister->addComponent(new Textbox(516, 70, 100, 20, font));
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));
211 wndRegister->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit));
212
213 txtUsernameRegister = (Textbox*)wndRegister->getComponent(0);
214 txtPasswordRegister = (Textbox*)wndRegister->getComponent(1);
215
216 rblClasses = (RadioButtonList*)wndRegister->getComponent(4);
217 rblClasses->addRadioButton("Warrior");
218 rblClasses->addRadioButton("Ranger");
219
220 lblRegisterStatus = (TextLabel*)wndRegister->getComponent(5);
221
222 cout << "Created register screen" << endl;
223
224 wndMain = new Window(0, 0, SCREEN_W, SCREEN_H);
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));
228
229 txtChat = (Textbox*)wndMain->getComponent(0);
230
231 cout << "Created main screen" << endl;
232
233 goToLoginScreen();
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());
248 al_register_event_source(event_queue, al_get_mouse_event_source());
249
250 al_clear_to_color(al_map_rgb(0,0,0));
251
252 al_flip_display();
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
265 set_nonblock(sock);
266
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
275 al_start_timer(timer);
276
277 while(!doexit)
278 {
279 ALLEGRO_EVENT ev;
280
281 al_wait_for_event(event_queue, &ev);
282
283 if(wndCurrent->handleEvent(ev)) {
284 // do nothing
285 }
286 else if(ev.type == ALLEGRO_EVENT_TIMER) {
287 redraw = true; // seems like we should just call a draw function here instead
288 }
289 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
290 doexit = true;
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;
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);
303 msgProcessor.sendMessage(&msgTo, sock, &server);
304 }
305 break;
306 case ALLEGRO_KEY_D: // drop the current item
307 if (state == STATE_LOGIN) {
308 // find the current player in the player list
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);
328 msgProcessor.sendMessage(&msgTo, sock, &server);
329 }
330 }
331 }
332 break;
333 }
334 }
335 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
336 if(wndCurrent == wndMain) {
337 if (ev.mouse.button == 1) { // left click
338 msgTo.type = MSG_TYPE_PLAYER_MOVE;
339
340 POSITION pos;
341 pos.x = ev.mouse.x;
342 pos.y = ev.mouse.y;
343 pos = screenToMap(pos);
344
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
351 msgProcessor.sendMessage(&msgTo, sock, &server);
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
358 cout << "Detected a right-click" << endl;
359
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 {
370 // need to check if the right-click was actually on this player
371 // right now, this code will target all players other than the current one
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);
377
378 msgProcessor.sendMessage(&msgTo, sock, &server);
379 }
380 }
381 }
382 }
383 }
384
385 if (msgProcessor.receiveMessage(&msgFrom, sock, &from) >= 0)
386 processMessage(msgFrom, state, chatConsole, gameMap, mapPlayers, mapProjectiles, curPlayerId, scoreBlue, scoreRed);
387
388 if (redraw)
389 {
390 redraw = false;
391
392 msgProcessor.resendUnackedMessages(sock);
393 msgProcessor.cleanAckedMessages();
394
395 wndCurrent->draw(display);
396
397 chatConsole.draw(font, al_map_rgb(255,255,255));
398
399 if(wndCurrent == wndMain) {
400 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
401
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
410 // update players
411 map<unsigned int, Player>::iterator it;
412 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
413 {
414 it->second.updateTarget(mapPlayers);
415 }
416
417 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
418 {
419 it->second.move(gameMap); // ignore return value
420 }
421
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
429 drawMap(gameMap);
430 drawPlayers(mapPlayers, font, curPlayerId);
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 }
451 }
452
453 al_flip_display();
454 }
455 }
456
457 #if defined WINDOWS
458 closesocket(sock);
459 #elif defined LINUX
460 close(sock);
461 #endif
462
463 shutdownWinSock();
464
465 delete wndLogin;
466 delete wndMain;
467
468 delete gameMap;
469
470 al_destroy_event_queue(event_queue);
471 al_destroy_display(display);
472 al_destroy_timer(timer);
473
474 return 0;
475}
476
477
478
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
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
510}
511
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
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
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)
544{
545 string response = string(msg.buffer);
546
547 switch(state)
548 {
549 case STATE_START:
550 {
551 cout << "In STATE_START" << endl;
552
553 switch(msg.type)
554 {
555 case MSG_TYPE_REGISTER:
556 {
557 lblRegisterStatus->setText(response);
558 break;
559 }
560 case MSG_TYPE_LOGIN:
561 {
562 if (response.compare("Player has already logged in.") == 0)
563 {
564 username.clear();
565 cout << "User login failed" << endl;
566 lblLoginStatus->setText(response);
567 }
568 else if (response.compare("Incorrect username or password") == 0)
569 {
570 username.clear();
571 cout << "User login failed" << endl;
572 lblLoginStatus->setText(response);
573 }
574 else
575 {
576 state = STATE_LOGIN;
577 wndCurrent = wndMain;
578
579 Player p("", "");
580 p.deserialize(msg.buffer);
581 mapPlayers[p.id] = p;
582 curPlayerId = p.id;
583
584 cout << "Got a valid login response with the player" << endl;
585 cout << "Player id: " << curPlayerId << endl;
586 cout << "Player health: " << p.health << endl;
587 cout << "map size: " << mapPlayers.size() << endl;
588 }
589
590 break;
591 }
592 case MSG_TYPE_PLAYER:
593 {
594 Player p("", "");
595 p.deserialize(msg.buffer);
596 p.timeLastUpdated = getCurrentMillis();
597
598 mapPlayers[p.id] = p;
599
600 break;
601 }
602 case MSG_TYPE_OBJECT:
603 {
604 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
605 o.deserialize(msg.buffer);
606 cout << "object id: " << o.id << endl;
607 gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
608
609 break;
610 }
611 default:
612 {
613 cout << "(STATE_REGISTER) Received invlaid message of type " << msg.type << endl;
614 break;
615 }
616 }
617
618 break;
619 }
620 case STATE_LOGIN:
621 {
622 switch(msg.type)
623 {
624 case MSG_TYPE_LOGIN:
625 {
626 cout << "Got a login message" << endl;
627
628 chatConsole.addLine(response);
629 cout << "Added new line" << endl;
630
631 break;
632 }
633 case MSG_TYPE_LOGOUT:
634 {
635 cout << "Got a logout message" << endl;
636
637 if (response.compare("You have successfully logged out.") == 0)
638 {
639 cout << "Logged out" << endl;
640 state = STATE_START;
641 goToLoginScreen();
642 }
643
644 break;
645 }
646 case MSG_TYPE_PLAYER:
647 {
648 cout << "Received MSG_TYPE_PLAYER" << endl;
649
650 Player p("", "");
651 p.deserialize(msg.buffer);
652 p.timeLastUpdated = getCurrentMillis();
653 p.isChasing = false;
654 if (p.health <= 0)
655 p.isDead = true;
656 else
657 p.isDead = false;
658
659 cout << mapPlayers[p.id].pos.x << ", " << mapPlayers[p.id].pos.y << endl;
660 cout << "health:" << mapPlayers[p.id].health << endl;
661 cout << "is dead?:" << mapPlayers[p.id].isDead << endl;
662 mapPlayers[p.id] = p;
663 cout << mapPlayers[p.id].pos.x << ", " << mapPlayers[p.id].pos.y << endl;
664 cout << "health:" << mapPlayers[p.id].health << endl;
665 cout << "is dead?:" << mapPlayers[p.id].isDead << endl;
666
667 break;
668 }
669 case MSG_TYPE_PLAYER_MOVE:
670 {
671 unsigned int id;
672 int x, y;
673
674 memcpy(&id, msg.buffer, 4);
675 memcpy(&x, msg.buffer+4, 4);
676 memcpy(&y, msg.buffer+8, 4);
677
678 mapPlayers[id].target.x = x;
679 mapPlayers[id].target.y = y;
680
681 break;
682 }
683 case MSG_TYPE_CHAT:
684 {
685 chatConsole.addLine(response);
686
687 break;
688 }
689 case MSG_TYPE_OBJECT:
690 {
691 cout << "Received object message in STATE_LOGIN." << endl;
692
693 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
694 o.deserialize(msg.buffer);
695 gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
696
697 break;
698 }
699 case MSG_TYPE_REMOVE_OBJECT:
700 {
701 cout << "Received REMOVE_OBJECT message!" << endl;
702
703 int id;
704 memcpy(&id, msg.buffer, 4);
705
706 cout << "Removing object with id " << id << endl;
707
708 if (!gameMap->removeObject(id))
709 cout << "Did not remove the object" << endl;
710
711 break;
712 }
713 case MSG_TYPE_SCORE:
714 {
715 memcpy(&scoreBlue, msg.buffer, 4);
716 memcpy(&scoreRed, msg.buffer+4, 4);
717
718 break;
719 }
720 case MSG_TYPE_ATTACK:
721 {
722 cout << "Received ATTACK message" << endl;
723
724 break;
725 }
726 case MSG_TYPE_START_ATTACK:
727 {
728 cout << "Received START_ATTACK message" << endl;
729
730 unsigned int id, targetID;
731 memcpy(&id, msg.buffer, 4);
732 memcpy(&targetID, msg.buffer+4, 4);
733
734 cout << "source id: " << id << endl;
735 cout << "target id: " << targetID << endl;
736
737 Player* source = &mapPlayers[id];
738 source->targetPlayer = targetID;
739 source->isChasing = true;
740
741 break;
742 }
743 case MSG_TYPE_PROJECTILE:
744 {
745 cout << "Received a PROJECTILE message" << endl;
746
747 int id, x, y, targetId;
748
749 memcpy(&id, msg.buffer, 4);
750 memcpy(&x, msg.buffer+4, 4);
751 memcpy(&y, msg.buffer+8, 4);
752 memcpy(&targetId, msg.buffer+12, 4);
753
754 cout << "id: " << id << endl;
755 cout << "x: " << x << endl;
756 cout << "y: " << y << endl;
757 cout << "Target: " << targetId << endl;
758
759 Projectile proj(x, y, targetId, 0);
760 proj.setId(id);
761
762 mapProjectiles[id] = proj;
763
764 break;
765 }
766 case MSG_TYPE_REMOVE_PROJECTILE:
767 {
768 cout << "Received a REMOVE_PROJECTILE message" << endl;
769
770 int id;
771
772 memcpy(&id, msg.buffer, 4);
773
774 mapProjectiles.erase(id);
775
776 break;
777 }
778 default:
779 {
780 cout << "(STATE_LOGIN) Received invlaid message of type " << msg.type << endl;
781 break;
782 }
783 }
784
785 break;
786 }
787 default:
788 {
789 cout << "The state has an invalid value: " << state << endl;
790
791 break;
792 }
793 }
794}
795
796// this should probably be in the WorldMap class
797void drawMap(WorldMap* gameMap)
798{
799 POSITION mapPos;
800 mapPos.x = 0;
801 mapPos.y = 0;
802 mapPos = mapToScreen(mapPos);
803
804 for (int x=0; x<gameMap->width; x++)
805 {
806 for (int y=0; y<gameMap->height; y++)
807 {
808 WorldMap::TerrainType el = gameMap->getElement(x, y);
809 WorldMap::StructureType structure = gameMap->getStructure(x, y);
810
811 if (el == WorldMap::TERRAIN_GRASS)
812 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));
813 else if (el == WorldMap::TERRAIN_OCEAN)
814 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));
815 else if (el == WorldMap::TERRAIN_ROCK)
816 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));
817
818 if (structure == WorldMap::STRUCTURE_BLUE_FLAG) {
819 al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
820 //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));
821 }else if (structure == WorldMap::STRUCTURE_RED_FLAG) {
822 al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
823 //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));
824 }
825 }
826 }
827
828 for (int x=0; x<gameMap->width; x++)
829 {
830 for (int y=0; y<gameMap->height; y++)
831 {
832 vector<WorldMap::Object> vctObjects = gameMap->getObjects(x, y);
833
834 vector<WorldMap::Object>::iterator it;
835 for(it = vctObjects.begin(); it != vctObjects.end(); it++) {
836 switch(it->type) {
837 case WorldMap::OBJECT_BLUE_FLAG:
838 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));
839 break;
840 case WorldMap::OBJECT_RED_FLAG:
841 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));
842 break;
843 }
844 }
845 }
846 }
847}
848
849void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId)
850{
851 map<unsigned int, Player>::iterator it;
852
853 Player* p;
854 POSITION pos;
855 ALLEGRO_COLOR color;
856
857 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
858 {
859 p = &it->second;
860
861 if (p->isDead)
862 continue;
863
864 pos = mapToScreen(p->pos);
865
866 if (p->id == curPlayerId)
867 al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));
868
869 if (p->team == 0)
870 color = al_map_rgb(0, 0, 255);
871 else if (p->team == 1)
872 color = al_map_rgb(255, 0, 0);
873
874 al_draw_filled_circle(pos.x, pos.y, 12, color);
875
876 // draw player class
877 int fontHeight = al_get_font_line_height(font);
878
879 string strClass;
880 switch (p->playerClass) {
881 case Player::CLASS_WARRIOR:
882 strClass = "W";
883 break;
884 case Player::CLASS_RANGER:
885 strClass = "R";
886 break;
887 case Player::CLASS_NONE:
888 strClass = "";
889 break;
890 default:
891 strClass = "";
892 break;
893 }
894 al_draw_text(font, al_map_rgb(0, 0, 0), pos.x, pos.y-fontHeight/2, ALLEGRO_ALIGN_CENTRE, strClass.c_str());
895
896 // draw player health
897 al_draw_filled_rectangle(pos.x-12, pos.y-24, pos.x+12, pos.y-16, al_map_rgb(0, 0, 0));
898 if (it->second.maxHealth != 0)
899 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));
900
901 if (p->hasBlueFlag)
902 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(0, 0, 255));
903 else if (p->hasRedFlag)
904 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(255, 0, 0));
905 }
906}
907
908void goToRegisterScreen()
909{
910 txtUsernameRegister->clear();
911 txtPasswordRegister->clear();
912 lblRegisterStatus->setText("");
913 rblClasses->setSelectedButton(-1);
914
915 wndCurrent = wndRegister;
916}
917
918void goToLoginScreen()
919{
920 txtUsername->clear();
921 txtPassword->clear();
922 lblLoginStatus->setText("");
923
924 wndCurrent = wndLogin;
925}
926
927void registerAccount()
928{
929 string username = txtUsernameRegister->getStr();
930 string password = txtPasswordRegister->getStr();
931
932 txtUsernameRegister->clear();
933 txtPasswordRegister->clear();
934 // maybe clear rblClasses as well (add a method to RadioButtonList to enable this)
935
936 Player::PlayerClass playerClass;
937
938 switch (rblClasses->getSelectedButton()) {
939 case 0:
940 playerClass = Player::CLASS_WARRIOR;
941 break;
942 case 1:
943 playerClass = Player::CLASS_RANGER;
944 break;
945 default:
946 cout << "Invalid class selection" << endl;
947 playerClass = Player::CLASS_NONE;
948 break;
949 }
950
951 msgTo.type = MSG_TYPE_REGISTER;
952
953 strcpy(msgTo.buffer, username.c_str());
954 strcpy(msgTo.buffer+username.size()+1, password.c_str());
955 memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
956
957 msgProcessor.sendMessage(&msgTo, sock, &server);
958}
959
960void login()
961{
962 string strUsername = txtUsername->getStr();
963 string strPassword = txtPassword->getStr();
964 username = strUsername;
965
966 txtUsername->clear();
967 txtPassword->clear();
968
969 msgTo.type = MSG_TYPE_LOGIN;
970
971 strcpy(msgTo.buffer, strUsername.c_str());
972 strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
973
974 msgProcessor.sendMessage(&msgTo, sock, &server);
975}
976
977void logout()
978{
979 txtChat->clear();
980
981 msgTo.type = MSG_TYPE_LOGOUT;
982
983 strcpy(msgTo.buffer, username.c_str());
984
985 msgProcessor.sendMessage(&msgTo, sock, &server);
986}
987
988void quit()
989{
990 doexit = true;
991}
992
993void sendChatMessage()
994{
995 string msg = txtChat->getStr();
996
997 txtChat->clear();
998
999 msgTo.type = MSG_TYPE_CHAT;
1000
1001 strcpy(msgTo.buffer, msg.c_str());
1002
1003 msgProcessor.sendMessage(&msgTo, sock, &server);
1004}
1005
1006int getRefreshRate(int width, int height) {
1007 int numRefreshRates = al_get_num_display_modes();
1008 ALLEGRO_DISPLAY_MODE displayMode;
1009
1010 for(int i=0; i<numRefreshRates; i++) {
1011 al_get_display_mode(i, &displayMode);
1012
1013 if (displayMode.width == width && displayMode.height == height)
1014 return displayMode.refresh_rate;
1015 }
1016
1017 return 0;
1018}
Note: See TracBrowser for help on using the repository browser.