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

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

Added a TextLabel gui element and updated the client to show status messages when registering or logging in

  • Property mode set to 100644
File size: 29.6 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 wndCurrent = wndLogin;
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 // There should be label gui components that show these or each textbox should have a label
401 if(wndCurrent == wndLogin || wndCurrent == wndRegister) {
402 //al_draw_text(font, al_map_rgb(0, 255, 0), 416, 43, ALLEGRO_ALIGN_LEFT, "Username:");
403 //al_draw_text(font, al_map_rgb(0, 255, 0), 413, 73, ALLEGRO_ALIGN_LEFT, "Password:");
404 }
405 else if(wndCurrent == wndMain) {
406 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
407
408 ostringstream ossScoreBlue, ossScoreRed;
409
410 ossScoreBlue << "Blue: " << scoreBlue << endl;
411 ossScoreRed << "Red: " << scoreRed << endl;
412
413 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
414 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
415
416 // update players
417 map<unsigned int, Player>::iterator it;
418 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
419 {
420 it->second.updateTarget(mapPlayers);
421 }
422
423 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
424 {
425 it->second.move(gameMap); // ignore return value
426 }
427
428 // update projectile positions
429 map<unsigned int, Projectile>::iterator it2;
430 for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
431 {
432 it2->second.move(mapPlayers);
433 }
434
435 drawMap(gameMap);
436 drawPlayers(mapPlayers, font, curPlayerId);
437
438 // draw projectiles
439 for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
440 {
441 Projectile proj = it2->second;
442
443 FLOAT_POSITION target = mapPlayers[proj.target].pos;
444 float angle = atan2(target.y-proj.pos.toFloat().y, target.x-proj.pos.toFloat().x);
445
446 POSITION start, end;
447 start.x = cos(angle)*15+proj.pos.x;
448 start.y = sin(angle)*15+proj.pos.y;
449 end.x = proj.pos.x;
450 end.y = proj.pos.y;
451
452 start = mapToScreen(start);
453 end = mapToScreen(end);
454
455 al_draw_line(start.x, start.y, end.x, end.y, al_map_rgb(0, 0, 0), 4);
456 }
457 }
458
459 al_flip_display();
460 }
461 }
462
463 #if defined WINDOWS
464 closesocket(sock);
465 #elif defined LINUX
466 close(sock);
467 #endif
468
469 shutdownWinSock();
470
471 delete wndLogin;
472 delete wndMain;
473
474 delete gameMap;
475
476 al_destroy_event_queue(event_queue);
477 al_destroy_display(display);
478 al_destroy_timer(timer);
479
480 return 0;
481}
482
483
484
485// need to make a function like this that works on windows
486void error(const char *msg)
487{
488 perror(msg);
489 shutdownWinSock();
490 exit(1);
491}
492
493void initWinSock()
494{
495#if defined WINDOWS
496 WORD wVersionRequested;
497 WSADATA wsaData;
498 int wsaerr;
499
500 wVersionRequested = MAKEWORD(2, 2);
501 wsaerr = WSAStartup(wVersionRequested, &wsaData);
502
503 if (wsaerr != 0) {
504 cout << "The Winsock dll not found." << endl;
505 exit(1);
506 }else
507 cout << "The Winsock dll was found." << endl;
508#endif
509}
510
511void shutdownWinSock()
512{
513#if defined WINDOWS
514 WSACleanup();
515#endif
516}
517
518POSITION screenToMap(POSITION pos)
519{
520 pos.x = pos.x-300;
521 pos.y = pos.y-100;
522
523 if (pos.x < 0 || pos.y < 0)
524 {
525 pos.x = -1;
526 pos.y = -1;
527 }
528
529 return pos;
530}
531
532POSITION mapToScreen(POSITION pos)
533{
534 pos.x = pos.x+300;
535 pos.y = pos.y+100;
536
537 return pos;
538}
539
540POSITION mapToScreen(FLOAT_POSITION pos)
541{
542 POSITION p;
543 p.x = pos.x+300;
544 p.y = pos.y+100;
545
546 return p;
547}
548
549void 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)
550{
551 string response = string(msg.buffer);
552
553 switch(state)
554 {
555 case STATE_START:
556 {
557 cout << "In STATE_START" << endl;
558
559 switch(msg.type)
560 {
561 case MSG_TYPE_REGISTER:
562 {
563 lblRegisterStatus->setText(response);
564 break;
565 }
566 case MSG_TYPE_LOGIN:
567 {
568 if (response.compare("Player has already logged in.") == 0)
569 {
570 username.clear();
571 cout << "User login failed" << endl;
572 lblLoginStatus->setText(response);
573 }
574 else if (response.compare("Incorrect username or password") == 0)
575 {
576 username.clear();
577 cout << "User login failed" << endl;
578 lblLoginStatus->setText(response);
579 }
580 else
581 {
582 state = STATE_LOGIN;
583 wndCurrent = wndMain;
584
585 Player p("", "");
586 p.deserialize(msg.buffer);
587 mapPlayers[p.id] = p;
588 curPlayerId = p.id;
589
590 cout << "Got a valid login response with the player" << endl;
591 cout << "Player id: " << curPlayerId << endl;
592 cout << "Player health: " << p.health << endl;
593 cout << "map size: " << mapPlayers.size() << endl;
594 }
595
596 break;
597 }
598 case MSG_TYPE_PLAYER:
599 {
600 Player p("", "");
601 p.deserialize(msg.buffer);
602 p.timeLastUpdated = getCurrentMillis();
603
604 mapPlayers[p.id] = p;
605
606 break;
607 }
608 case MSG_TYPE_OBJECT:
609 {
610 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
611 o.deserialize(msg.buffer);
612 cout << "object id: " << o.id << endl;
613 gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
614
615 break;
616 }
617 default:
618 {
619 cout << "(STATE_REGISTER) Received invlaid message of type " << msg.type << endl;
620 break;
621 }
622 }
623
624 break;
625 }
626 case STATE_LOGIN:
627 {
628 switch(msg.type)
629 {
630 case MSG_TYPE_LOGIN:
631 {
632 cout << "Got a login message" << endl;
633
634 chatConsole.addLine(response);
635 cout << "Added new line" << endl;
636
637 break;
638 }
639 case MSG_TYPE_LOGOUT:
640 {
641 cout << "Got a logout message" << endl;
642
643 if (response.compare("You have successfully logged out.") == 0)
644 {
645 cout << "Logged out" << endl;
646 state = STATE_START;
647 wndCurrent = wndLogin;
648 }
649
650 break;
651 }
652 case MSG_TYPE_PLAYER:
653 {
654 cout << "Received MSG_TYPE_PLAYER" << endl;
655
656 Player p("", "");
657 p.deserialize(msg.buffer);
658 p.timeLastUpdated = getCurrentMillis();
659 p.isChasing = false;
660 if (p.health <= 0)
661 p.isDead = true;
662 else
663 p.isDead = false;
664
665 cout << mapPlayers[p.id].pos.x << ", " << mapPlayers[p.id].pos.y << endl;
666 cout << "health:" << mapPlayers[p.id].health << endl;
667 cout << "is dead?:" << mapPlayers[p.id].isDead << endl;
668 mapPlayers[p.id] = p;
669 cout << mapPlayers[p.id].pos.x << ", " << mapPlayers[p.id].pos.y << endl;
670 cout << "health:" << mapPlayers[p.id].health << endl;
671 cout << "is dead?:" << mapPlayers[p.id].isDead << endl;
672
673 break;
674 }
675 case MSG_TYPE_PLAYER_MOVE:
676 {
677 unsigned int id;
678 int x, y;
679
680 memcpy(&id, msg.buffer, 4);
681 memcpy(&x, msg.buffer+4, 4);
682 memcpy(&y, msg.buffer+8, 4);
683
684 mapPlayers[id].target.x = x;
685 mapPlayers[id].target.y = y;
686
687 break;
688 }
689 case MSG_TYPE_CHAT:
690 {
691 chatConsole.addLine(response);
692
693 break;
694 }
695 case MSG_TYPE_OBJECT:
696 {
697 cout << "Received object message in STATE_LOGIN." << endl;
698
699 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
700 o.deserialize(msg.buffer);
701 gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
702
703 break;
704 }
705 case MSG_TYPE_REMOVE_OBJECT:
706 {
707 cout << "Received REMOVE_OBJECT message!" << endl;
708
709 int id;
710 memcpy(&id, msg.buffer, 4);
711
712 cout << "Removing object with id " << id << endl;
713
714 if (!gameMap->removeObject(id))
715 cout << "Did not remove the object" << endl;
716
717 break;
718 }
719 case MSG_TYPE_SCORE:
720 {
721 memcpy(&scoreBlue, msg.buffer, 4);
722 memcpy(&scoreRed, msg.buffer+4, 4);
723
724 break;
725 }
726 case MSG_TYPE_ATTACK:
727 {
728 cout << "Received ATTACK message" << endl;
729
730 break;
731 }
732 case MSG_TYPE_START_ATTACK:
733 {
734 cout << "Received START_ATTACK message" << endl;
735
736 unsigned int id, targetID;
737 memcpy(&id, msg.buffer, 4);
738 memcpy(&targetID, msg.buffer+4, 4);
739
740 cout << "source id: " << id << endl;
741 cout << "target id: " << targetID << endl;
742
743 Player* source = &mapPlayers[id];
744 source->targetPlayer = targetID;
745 source->isChasing = true;
746
747 break;
748 }
749 case MSG_TYPE_PROJECTILE:
750 {
751 cout << "Received a PROJECTILE message" << endl;
752
753 int id, x, y, targetId;
754
755 memcpy(&id, msg.buffer, 4);
756 memcpy(&x, msg.buffer+4, 4);
757 memcpy(&y, msg.buffer+8, 4);
758 memcpy(&targetId, msg.buffer+12, 4);
759
760 cout << "id: " << id << endl;
761 cout << "x: " << x << endl;
762 cout << "y: " << y << endl;
763 cout << "Target: " << targetId << endl;
764
765 Projectile proj(x, y, targetId, 0);
766 proj.setId(id);
767
768 mapProjectiles[id] = proj;
769
770 break;
771 }
772 case MSG_TYPE_REMOVE_PROJECTILE:
773 {
774 cout << "Received a REMOVE_PROJECTILE message" << endl;
775
776 int id;
777
778 memcpy(&id, msg.buffer, 4);
779
780 mapProjectiles.erase(id);
781
782 break;
783 }
784 default:
785 {
786 cout << "(STATE_LOGIN) Received invlaid message of type " << msg.type << endl;
787 break;
788 }
789 }
790
791 break;
792 }
793 default:
794 {
795 cout << "The state has an invalid value: " << state << endl;
796
797 break;
798 }
799 }
800}
801
802// this should probably be in the WorldMap class
803void drawMap(WorldMap* gameMap)
804{
805 POSITION mapPos;
806 mapPos.x = 0;
807 mapPos.y = 0;
808 mapPos = mapToScreen(mapPos);
809
810 for (int x=0; x<gameMap->width; x++)
811 {
812 for (int y=0; y<gameMap->height; y++)
813 {
814 WorldMap::TerrainType el = gameMap->getElement(x, y);
815 WorldMap::StructureType structure = gameMap->getStructure(x, y);
816
817 if (el == WorldMap::TERRAIN_GRASS)
818 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));
819 else if (el == WorldMap::TERRAIN_OCEAN)
820 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));
821 else if (el == WorldMap::TERRAIN_ROCK)
822 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));
823
824 if (structure == WorldMap::STRUCTURE_BLUE_FLAG) {
825 al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
826 //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));
827 }else if (structure == WorldMap::STRUCTURE_RED_FLAG) {
828 al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
829 //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));
830 }
831 }
832 }
833
834 for (int x=0; x<gameMap->width; x++)
835 {
836 for (int y=0; y<gameMap->height; y++)
837 {
838 vector<WorldMap::Object> vctObjects = gameMap->getObjects(x, y);
839
840 vector<WorldMap::Object>::iterator it;
841 for(it = vctObjects.begin(); it != vctObjects.end(); it++) {
842 switch(it->type) {
843 case WorldMap::OBJECT_BLUE_FLAG:
844 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));
845 break;
846 case WorldMap::OBJECT_RED_FLAG:
847 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));
848 break;
849 }
850 }
851 }
852 }
853}
854
855void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId)
856{
857 map<unsigned int, Player>::iterator it;
858
859 Player* p;
860 POSITION pos;
861 ALLEGRO_COLOR color;
862
863 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
864 {
865 p = &it->second;
866
867 if (p->isDead)
868 continue;
869
870 pos = mapToScreen(p->pos);
871
872 if (p->id == curPlayerId)
873 al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));
874
875 if (p->team == 0)
876 color = al_map_rgb(0, 0, 255);
877 else if (p->team == 1)
878 color = al_map_rgb(255, 0, 0);
879
880 al_draw_filled_circle(pos.x, pos.y, 12, color);
881
882 // draw player class
883 int fontHeight = al_get_font_line_height(font);
884
885 string strClass;
886 switch (p->playerClass) {
887 case Player::CLASS_WARRIOR:
888 strClass = "W";
889 break;
890 case Player::CLASS_RANGER:
891 strClass = "R";
892 break;
893 case Player::CLASS_NONE:
894 strClass = "";
895 break;
896 default:
897 strClass = "";
898 break;
899 }
900 al_draw_text(font, al_map_rgb(0, 0, 0), pos.x, pos.y-fontHeight/2, ALLEGRO_ALIGN_CENTRE, strClass.c_str());
901
902 // draw player health
903 al_draw_filled_rectangle(pos.x-12, pos.y-24, pos.x+12, pos.y-16, al_map_rgb(0, 0, 0));
904 if (it->second.maxHealth != 0)
905 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));
906
907 if (p->hasBlueFlag)
908 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(0, 0, 255));
909 else if (p->hasRedFlag)
910 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(255, 0, 0));
911 }
912}
913
914void goToRegisterScreen()
915{
916 wndCurrent = wndRegister;
917
918 txtUsernameRegister->clear();
919 txtPasswordRegister->clear();
920}
921
922void goToLoginScreen()
923{
924 wndCurrent = wndLogin;
925
926 txtUsername->clear();
927 txtPassword->clear();
928}
929
930void registerAccount()
931{
932 string username = txtUsernameRegister->getStr();
933 string password = txtPasswordRegister->getStr();
934
935 txtUsernameRegister->clear();
936 txtPasswordRegister->clear();
937 // maybe clear rblClasses as well (add a method to RadioButtonList to enable this)
938
939 Player::PlayerClass playerClass;
940
941 switch (rblClasses->getSelectedButton()) {
942 case 0:
943 playerClass = Player::CLASS_WARRIOR;
944 break;
945 case 1:
946 playerClass = Player::CLASS_RANGER;
947 break;
948 default:
949 cout << "Invalid class selection" << endl;
950 playerClass = Player::CLASS_NONE;
951 break;
952 }
953
954 msgTo.type = MSG_TYPE_REGISTER;
955
956 strcpy(msgTo.buffer, username.c_str());
957 strcpy(msgTo.buffer+username.size()+1, password.c_str());
958 memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
959
960 msgProcessor.sendMessage(&msgTo, sock, &server);
961}
962
963void login()
964{
965 string strUsername = txtUsername->getStr();
966 string strPassword = txtPassword->getStr();
967 username = strUsername;
968
969 txtUsername->clear();
970 txtPassword->clear();
971
972 msgTo.type = MSG_TYPE_LOGIN;
973
974 strcpy(msgTo.buffer, strUsername.c_str());
975 strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
976
977 msgProcessor.sendMessage(&msgTo, sock, &server);
978}
979
980void logout()
981{
982 txtChat->clear();
983
984 msgTo.type = MSG_TYPE_LOGOUT;
985
986 strcpy(msgTo.buffer, username.c_str());
987
988 msgProcessor.sendMessage(&msgTo, sock, &server);
989}
990
991void quit()
992{
993 doexit = true;
994}
995
996void sendChatMessage()
997{
998 string msg = txtChat->getStr();
999
1000 txtChat->clear();
1001
1002 msgTo.type = MSG_TYPE_CHAT;
1003
1004 strcpy(msgTo.buffer, msg.c_str());
1005
1006 msgProcessor.sendMessage(&msgTo, sock, &server);
1007}
1008
1009int getRefreshRate(int width, int height) {
1010 int numRefreshRates = al_get_num_display_modes();
1011 ALLEGRO_DISPLAY_MODE displayMode;
1012
1013 for(int i=0; i<numRefreshRates; i++) {
1014 al_get_display_mode(i, &displayMode);
1015
1016 if (displayMode.width == width && displayMode.height == height)
1017 return displayMode.refresh_rate;
1018 }
1019
1020 return 0;
1021}
Note: See TracBrowser for help on using the repository browser.