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

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

Remove the Message.h include from several files

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