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

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

Added a client release build that uses the release version of allegro

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