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

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

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

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