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

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

Added a basic ingame debug console

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