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

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

Client clears acked messages

  • Property mode set to 100644
File size: 28.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/Message.h"
31#include "../../common/MessageProcessor.h"
32#include "../../common/WorldMap.h"
33#include "../../common/Player.h"
34#include "../../common/Projectile.h"
35
36#include "Window.h"
37#include "Textbox.h"
38#include "Button.h"
39#include "RadioButtonList.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;
89
90// wndRegister
91Textbox* txtUsernameRegister;
92Textbox* txtPasswordRegister;
93RadioButtonList* rblClasses;
94
95// wndMain
96Textbox* txtChat;
97
98int sock;
99struct sockaddr_in server, from;
100struct hostent *hp;
101NETWORK_MSG msgTo, msgFrom;
102string username;
103chat chatConsole;
104
105MessageProcessor msgProcessor;
106
107int 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;
114 doexit = false;
115 map<unsigned int, Player> mapPlayers;
116 map<unsigned int, Projectile> mapProjectiles;
117 unsigned int curPlayerId = -1;
118 int scoreBlue, scoreRed;
119 bool fullscreen = false;
120
121 scoreBlue = 0;
122 scoreRed = 0;
123
124 state = STATE_START;
125
126 if(!al_init()) {
127 fprintf(stderr, "failed to initialize allegro!\n");
128 return -1;
129 }
130
131 if (al_init_primitives_addon())
132 cout << "Primitives initialized" << endl;
133 else
134 cout << "Primitives not initialized" << endl;
135
136 al_init_font_addon();
137 al_init_ttf_addon();
138
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
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 }
155
156 if(!al_install_mouse()) {
157 fprintf(stderr, "failed to initialize the mouse!\n");
158 return -1;
159 }
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
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);
174 if(!display) {
175 fprintf(stderr, "failed to create display!\n");
176 al_destroy_timer(timer);
177 return -1;
178 }
179
180 WorldMap* gameMap = WorldMap::loadMapFromFile("../../data/map.txt");
181
182 wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
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));
188
189 txtUsername = (Textbox*)wndLogin->getComponent(0);
190 txtPassword = (Textbox*)wndLogin->getComponent(1);
191
192 wndRegister = new Window(0, 0, SCREEN_W, SCREEN_H);
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));
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
207 wndMain = new Window(0, 0, SCREEN_W, SCREEN_H);
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));
211
212 txtChat = (Textbox*)wndMain->getComponent(0);
213
214 wndCurrent = wndLogin;
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());
229 al_register_event_source(event_queue, al_get_mouse_event_source());
230
231 al_clear_to_color(al_map_rgb(0,0,0));
232
233 al_flip_display();
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
246 set_nonblock(sock);
247
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
256 al_start_timer(timer);
257
258 while(!doexit)
259 {
260 ALLEGRO_EVENT ev;
261
262 al_wait_for_event(event_queue, &ev);
263
264 if(wndCurrent->handleEvent(ev)) {
265 // do nothing
266 }
267 else if(ev.type == ALLEGRO_EVENT_TIMER) {
268 redraw = true; // seems like we should just call a draw function here instead
269 }
270 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
271 doexit = true;
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;
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);
284 msgProcessor.sendMessage(&msgTo, sock, &server);
285 }
286 break;
287 case ALLEGRO_KEY_D: // drop the current item
288 if (state == STATE_LOGIN) {
289 // find the current player in the player list
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);
309 msgProcessor.sendMessage(&msgTo, sock, &server);
310 }
311 }
312 }
313 break;
314 }
315 }
316 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
317 if(wndCurrent == wndMain) {
318 if (ev.mouse.button == 1) { // left click
319 msgTo.type = MSG_TYPE_PLAYER_MOVE;
320
321 POSITION pos;
322 pos.x = ev.mouse.x;
323 pos.y = ev.mouse.y;
324 pos = screenToMap(pos);
325
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
332 msgProcessor.sendMessage(&msgTo, sock, &server);
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
339 cout << "Detected a right-click" << endl;
340
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 {
351 // need to check if the right-click was actually on this player
352 // right now, this code will target all players other than the current one
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);
358
359 msgProcessor.sendMessage(&msgTo, sock, &server);
360 }
361 }
362 }
363 }
364 }
365
366 if (msgProcessor.receiveMessage(&msgFrom, sock, &from) >= 0)
367 processMessage(msgFrom, state, chatConsole, gameMap, mapPlayers, mapProjectiles, curPlayerId, scoreBlue, scoreRed);
368
369 if (redraw)
370 {
371 redraw = false;
372
373 msgProcessor.resendUnackedMessages(sock);
374 msgProcessor.cleanAckedMessages();
375
376 wndCurrent->draw(display);
377
378 chatConsole.draw(font, al_map_rgb(255,255,255));
379
380 // There should be label gui components that show these or each textbox should have a label
381 if(wndCurrent == wndLogin || wndCurrent == wndRegister) {
382 al_draw_text(font, al_map_rgb(0, 255, 0), 416, 43, ALLEGRO_ALIGN_LEFT, "Username:");
383 al_draw_text(font, al_map_rgb(0, 255, 0), 413, 73, ALLEGRO_ALIGN_LEFT, "Password:");
384 }
385 else if(wndCurrent == wndMain) {
386 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
387
388 ostringstream ossScoreBlue, ossScoreRed;
389
390 ossScoreBlue << "Blue: " << scoreBlue << endl;
391 ossScoreRed << "Red: " << scoreRed << endl;
392
393 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
394 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
395
396 // update players
397 map<unsigned int, Player>::iterator it;
398 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
399 {
400 it->second.updateTarget(mapPlayers);
401 }
402
403 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
404 {
405 it->second.move(gameMap); // ignore return value
406 }
407
408 // update projectile positions
409 map<unsigned int, Projectile>::iterator it2;
410 for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
411 {
412 it2->second.move(mapPlayers);
413 }
414
415 drawMap(gameMap);
416 drawPlayers(mapPlayers, font, curPlayerId);
417
418 // draw projectiles
419 for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
420 {
421 Projectile proj = it2->second;
422
423 FLOAT_POSITION target = mapPlayers[proj.target].pos;
424 float angle = atan2(target.y-proj.pos.toFloat().y, target.x-proj.pos.toFloat().x);
425
426 POSITION start, end;
427 start.x = cos(angle)*15+proj.pos.x;
428 start.y = sin(angle)*15+proj.pos.y;
429 end.x = proj.pos.x;
430 end.y = proj.pos.y;
431
432 start = mapToScreen(start);
433 end = mapToScreen(end);
434
435 al_draw_line(start.x, start.y, end.x, end.y, al_map_rgb(0, 0, 0), 4);
436 }
437 }
438
439 al_flip_display();
440 }
441 }
442
443 #if defined WINDOWS
444 closesocket(sock);
445 #elif defined LINUX
446 close(sock);
447 #endif
448
449 shutdownWinSock();
450
451 delete wndLogin;
452 delete wndMain;
453
454 delete gameMap;
455
456 al_destroy_event_queue(event_queue);
457 al_destroy_display(display);
458 al_destroy_timer(timer);
459
460 return 0;
461}
462
463
464
465// need to make a function like this that works on windows
466void error(const char *msg)
467{
468 perror(msg);
469 shutdownWinSock();
470 exit(1);
471}
472
473void initWinSock()
474{
475#if defined WINDOWS
476 WORD wVersionRequested;
477 WSADATA wsaData;
478 int wsaerr;
479
480 wVersionRequested = MAKEWORD(2, 2);
481 wsaerr = WSAStartup(wVersionRequested, &wsaData);
482
483 if (wsaerr != 0) {
484 cout << "The Winsock dll not found." << endl;
485 exit(1);
486 }else
487 cout << "The Winsock dll was found." << endl;
488#endif
489}
490
491void shutdownWinSock()
492{
493#if defined WINDOWS
494 WSACleanup();
495#endif
496}
497
498POSITION screenToMap(POSITION pos)
499{
500 pos.x = pos.x-300;
501 pos.y = pos.y-100;
502
503 if (pos.x < 0 || pos.y < 0)
504 {
505 pos.x = -1;
506 pos.y = -1;
507 }
508
509 return pos;
510}
511
512POSITION mapToScreen(POSITION pos)
513{
514 pos.x = pos.x+300;
515 pos.y = pos.y+100;
516
517 return pos;
518}
519
520POSITION mapToScreen(FLOAT_POSITION pos)
521{
522 POSITION p;
523 p.x = pos.x+300;
524 p.y = pos.y+100;
525
526 return p;
527}
528
529void 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)
530{
531 string response = string(msg.buffer);
532
533 switch(state)
534 {
535 case STATE_START:
536 {
537 cout << "In STATE_START" << endl;
538
539 switch(msg.type)
540 {
541 case MSG_TYPE_REGISTER:
542 {
543 break;
544 }
545 case MSG_TYPE_LOGIN:
546 {
547 if (response.compare("Player has already logged in.") == 0)
548 {
549 username.clear();
550 cout << "User login failed" << endl;
551 }
552 else if (response.compare("Incorrect username or password") == 0)
553 {
554 username.clear();
555 cout << "User login failed" << endl;
556 }
557 else
558 {
559 state = STATE_LOGIN;
560 wndCurrent = wndMain;
561
562 Player p("", "");
563 p.deserialize(msg.buffer);
564 mapPlayers[p.id] = p;
565 curPlayerId = p.id;
566
567 cout << "Got a valid login response with the player" << endl;
568 cout << "Player id: " << curPlayerId << endl;
569 cout << "Player health: " << p.health << endl;
570 cout << "map size: " << mapPlayers.size() << endl;
571 }
572
573 break;
574 }
575 case MSG_TYPE_PLAYER: // kind of hacky to put this here
576 {
577 Player p("", "");
578 p.deserialize(msg.buffer);
579 p.timeLastUpdated = getCurrentMillis();
580
581 mapPlayers[p.id] = p;
582
583 break;
584 }
585 case MSG_TYPE_OBJECT:
586 {
587 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
588 o.deserialize(msg.buffer);
589 cout << "object id: " << o.id << endl;
590 gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
591
592 break;
593 }
594 }
595
596 break;
597 }
598 case STATE_LOGIN:
599 {
600 switch(msg.type)
601 {
602 case MSG_TYPE_REGISTER:
603 {
604 break;
605 }
606 case MSG_TYPE_LOGIN:
607 {
608 cout << "Got a login message" << endl;
609
610 chatConsole.addLine(response);
611 cout << "Added new line" << endl;
612
613 break;
614 }
615 case MSG_TYPE_LOGOUT:
616 {
617 cout << "Got a logout message" << endl;
618
619 if (response.compare("You have successfully logged out.") == 0)
620 {
621 cout << "Logged out" << endl;
622 state = STATE_START;
623 wndCurrent = wndLogin;
624 }
625
626 break;
627 }
628 case MSG_TYPE_PLAYER:
629 {
630 cout << "Received MSG_TYPE_PLAYER" << endl;
631
632 Player p("", "");
633 p.deserialize(msg.buffer);
634 p.timeLastUpdated = getCurrentMillis();
635 p.isChasing = false;
636 if (p.health <= 0)
637 p.isDead = true;
638 else
639 p.isDead = false;
640
641 cout << mapPlayers[p.id].pos.x << ", " << mapPlayers[p.id].pos.y << endl;
642 cout << "health:" << mapPlayers[p.id].health << endl;
643 cout << "is dead?:" << mapPlayers[p.id].isDead << endl;
644 mapPlayers[p.id] = p;
645 cout << mapPlayers[p.id].pos.x << ", " << mapPlayers[p.id].pos.y << endl;
646 cout << "health:" << mapPlayers[p.id].health << endl;
647 cout << "is dead?:" << mapPlayers[p.id].isDead << endl;
648
649 break;
650 }
651 case MSG_TYPE_PLAYER_MOVE:
652 {
653 unsigned int id;
654 int x, y;
655
656 memcpy(&id, msg.buffer, 4);
657 memcpy(&x, msg.buffer+4, 4);
658 memcpy(&y, msg.buffer+8, 4);
659
660 mapPlayers[id].target.x = x;
661 mapPlayers[id].target.y = y;
662
663 break;
664 }
665 case MSG_TYPE_CHAT:
666 {
667 chatConsole.addLine(response);
668
669 break;
670 }
671 case MSG_TYPE_OBJECT:
672 {
673 cout << "Received object message in STATE_LOGIN." << endl;
674
675 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
676 o.deserialize(msg.buffer);
677 gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
678
679 break;
680 }
681 case MSG_TYPE_REMOVE_OBJECT:
682 {
683 cout << "Received REMOVE_OBJECT message!" << endl;
684
685 int id;
686 memcpy(&id, msg.buffer, 4);
687
688 cout << "Removing object with id " << id << endl;
689
690 if (!gameMap->removeObject(id))
691 cout << "Did not remove the object" << endl;
692
693 break;
694 }
695 case MSG_TYPE_SCORE:
696 {
697 memcpy(&scoreBlue, msg.buffer, 4);
698 memcpy(&scoreRed, msg.buffer+4, 4);
699
700 break;
701 }
702 case MSG_TYPE_ATTACK:
703 {
704 cout << "Received ATTACK message" << endl;
705
706 break;
707 }
708 case MSG_TYPE_START_ATTACK:
709 {
710 cout << "Received START_ATTACK message" << endl;
711
712 unsigned int id, targetID;
713 memcpy(&id, msg.buffer, 4);
714 memcpy(&targetID, msg.buffer+4, 4);
715
716 cout << "source id: " << id << endl;
717 cout << "target id: " << targetID << endl;
718
719 Player* source = &mapPlayers[id];
720 source->targetPlayer = targetID;
721 source->isChasing = true;
722
723 break;
724 }
725 case MSG_TYPE_PROJECTILE:
726 {
727 cout << "Received a PROJECTILE message" << endl;
728
729 int id, x, y, targetId;
730
731 memcpy(&id, msg.buffer, 4);
732 memcpy(&x, msg.buffer+4, 4);
733 memcpy(&y, msg.buffer+8, 4);
734 memcpy(&targetId, msg.buffer+12, 4);
735
736 cout << "id: " << id << endl;
737 cout << "x: " << x << endl;
738 cout << "y: " << y << endl;
739 cout << "Target: " << targetId << endl;
740
741 Projectile proj(x, y, targetId, 0);
742 proj.setId(id);
743
744 mapProjectiles[id] = proj;
745
746 break;
747 }
748 case MSG_TYPE_REMOVE_PROJECTILE:
749 {
750 cout << "Received a REMOVE_PROJECTILE message" << endl;
751
752 int id;
753
754 memcpy(&id, msg.buffer, 4);
755
756 mapProjectiles.erase(id);
757
758 break;
759 }
760 default:
761 {
762 cout << "Received an unexpected message type: " << msg.type << endl;
763 }
764 }
765
766 break;
767 }
768 default:
769 {
770 cout << "The state has an invalid value: " << state << endl;
771
772 break;
773 }
774 }
775}
776
777// this should probably be in the WorldMap class
778void drawMap(WorldMap* gameMap)
779{
780 POSITION mapPos;
781 mapPos.x = 0;
782 mapPos.y = 0;
783 mapPos = mapToScreen(mapPos);
784
785 for (int x=0; x<gameMap->width; x++)
786 {
787 for (int y=0; y<gameMap->height; y++)
788 {
789 WorldMap::TerrainType el = gameMap->getElement(x, y);
790 WorldMap::StructureType structure = gameMap->getStructure(x, y);
791
792 if (el == WorldMap::TERRAIN_GRASS)
793 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));
794 else if (el == WorldMap::TERRAIN_OCEAN)
795 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));
796 else if (el == WorldMap::TERRAIN_ROCK)
797 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));
798
799 if (structure == WorldMap::STRUCTURE_BLUE_FLAG) {
800 al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
801 //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));
802 }else if (structure == WorldMap::STRUCTURE_RED_FLAG) {
803 al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
804 //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));
805 }
806 }
807 }
808
809 for (int x=0; x<gameMap->width; x++)
810 {
811 for (int y=0; y<gameMap->height; y++)
812 {
813 vector<WorldMap::Object> vctObjects = gameMap->getObjects(x, y);
814
815 vector<WorldMap::Object>::iterator it;
816 for(it = vctObjects.begin(); it != vctObjects.end(); it++) {
817 switch(it->type) {
818 case WorldMap::OBJECT_BLUE_FLAG:
819 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));
820 break;
821 case WorldMap::OBJECT_RED_FLAG:
822 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));
823 break;
824 }
825 }
826 }
827 }
828}
829
830void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId)
831{
832 map<unsigned int, Player>::iterator it;
833
834 Player* p;
835 POSITION pos;
836 ALLEGRO_COLOR color;
837
838 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
839 {
840 p = &it->second;
841
842 if (p->isDead)
843 continue;
844
845 pos = mapToScreen(p->pos);
846
847 if (p->id == curPlayerId)
848 al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));
849
850 if (p->team == 0)
851 color = al_map_rgb(0, 0, 255);
852 else if (p->team == 1)
853 color = al_map_rgb(255, 0, 0);
854
855 al_draw_filled_circle(pos.x, pos.y, 12, color);
856
857 // draw player class
858 int fontHeight = al_get_font_line_height(font);
859
860 string strClass;
861 switch (p->playerClass) {
862 case Player::CLASS_WARRIOR:
863 strClass = "W";
864 break;
865 case Player::CLASS_RANGER:
866 strClass = "R";
867 break;
868 case Player::CLASS_NONE:
869 strClass = "";
870 break;
871 default:
872 strClass = "";
873 break;
874 }
875 al_draw_text(font, al_map_rgb(0, 0, 0), pos.x, pos.y-fontHeight/2, ALLEGRO_ALIGN_CENTRE, strClass.c_str());
876
877 // draw player health
878 al_draw_filled_rectangle(pos.x-12, pos.y-24, pos.x+12, pos.y-16, al_map_rgb(0, 0, 0));
879 if (it->second.maxHealth != 0)
880 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));
881
882 if (p->hasBlueFlag)
883 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(0, 0, 255));
884 else if (p->hasRedFlag)
885 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(255, 0, 0));
886 }
887}
888
889void goToRegisterScreen()
890{
891 wndCurrent = wndRegister;
892
893 txtUsernameRegister->clear();
894 txtPasswordRegister->clear();
895}
896
897void goToLoginScreen()
898{
899 wndCurrent = wndLogin;
900
901 txtUsername->clear();
902 txtPassword->clear();
903}
904
905void registerAccount()
906{
907 string username = txtUsernameRegister->getStr();
908 string password = txtPasswordRegister->getStr();
909
910 txtUsernameRegister->clear();
911 txtPasswordRegister->clear();
912 // maybe clear rblClasses as well (add a method to RadioButtonList to enable this)
913
914 Player::PlayerClass playerClass;
915
916 switch (rblClasses->getSelectedButton()) {
917 case 0:
918 playerClass = Player::CLASS_WARRIOR;
919 break;
920 case 1:
921 playerClass = Player::CLASS_RANGER;
922 break;
923 default:
924 cout << "Invalid class selection" << endl;
925 playerClass = Player::CLASS_NONE;
926 break;
927 }
928
929 msgTo.type = MSG_TYPE_REGISTER;
930
931 strcpy(msgTo.buffer, username.c_str());
932 strcpy(msgTo.buffer+username.size()+1, password.c_str());
933 memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
934
935 msgProcessor.sendMessage(&msgTo, sock, &server);
936}
937
938void login()
939{
940 string strUsername = txtUsername->getStr();
941 string strPassword = txtPassword->getStr();
942 username = strUsername;
943
944 txtUsername->clear();
945 txtPassword->clear();
946
947 msgTo.type = MSG_TYPE_LOGIN;
948
949 strcpy(msgTo.buffer, strUsername.c_str());
950 strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
951
952 msgProcessor.sendMessage(&msgTo, sock, &server);
953}
954
955void logout()
956{
957 txtChat->clear();
958
959 msgTo.type = MSG_TYPE_LOGOUT;
960
961 strcpy(msgTo.buffer, username.c_str());
962
963 msgProcessor.sendMessage(&msgTo, sock, &server);
964}
965
966void quit()
967{
968 doexit = true;
969}
970
971void sendChatMessage()
972{
973 string msg = txtChat->getStr();
974
975 txtChat->clear();
976
977 msgTo.type = MSG_TYPE_CHAT;
978
979 strcpy(msgTo.buffer, msg.c_str());
980
981 msgProcessor.sendMessage(&msgTo, sock, &server);
982}
983
984int getRefreshRate(int width, int height) {
985 int numRefreshRates = al_get_num_display_modes();
986 ALLEGRO_DISPLAY_MODE displayMode;
987
988 for(int i=0; i<numRefreshRates; i++) {
989 al_get_display_mode(i, &displayMode);
990
991 if (displayMode.width == width && displayMode.height == height)
992 return displayMode.refresh_rate;
993 }
994
995 return 0;
996}
Note: See TracBrowser for help on using the repository browser.