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

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

Add status messages for login and registration and remove the Message class (MessageProcessor includes all that functionality now)

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