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

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

Client clears acked messages

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