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

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

The client now uses the new getAckedMessages method

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