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

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

Player targeting logic has been fixed

  • Property mode set to 100644
File size: 41.5 KB
Line 
1#include "../../common/Compiler.h"
2
3#if defined WINDOWS
4 #include <winsock2.h>
5 #include <ws2tcpip.h>
6#elif defined LINUX
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>
13#endif
14
15#include <cstdio>
16#include <cstdlib>
17#include <cmath>
18#include <sys/types.h>
19#include <string>
20#include <iostream>
21#include <sstream>
22#include <fstream>
23#include <map>
24#include <vector>
25#include <stdexcept>
26
27#include <allegro5/allegro.h>
28#include <allegro5/allegro_font.h>
29#include <allegro5/allegro_ttf.h>
30#include <allegro5/allegro_primitives.h>
31
32#include "../../common/Common.h"
33#include "../../common/MessageContainer.h"
34#include "../../common/MessageProcessor.h"
35#include "../../common/WorldMap.h"
36#include "../../common/Player.h"
37#include "../../common/Projectile.h"
38#include "../../common/Game.h"
39#include "../../common/GameSummary.h"
40
41#include "Window.h"
42#include "TextLabel.h"
43#include "Button.h"
44#include "Textbox.h"
45#include "RadioButtonList.h"
46
47#include "GameRender.h"
48
49#include "chat.h"
50
51#ifdef WINDOWS
52 #pragma comment(lib, "ws2_32.lib")
53#endif
54
55using namespace std;
56
57void initWinSock();
58void shutdownWinSock();
59void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player*>& mapPlayers,
60 map<unsigned int, Projectile>& mapProjectiles, unsigned int& curPlayerId);
61int getRefreshRate(int width, int height);
62void drawMessageStatus(ALLEGRO_FONT* font);
63
64// Callback declarations
65void goToLoginScreen();
66void goToRegisterScreen();
67void registerAccount();
68void login();
69void logout();
70void quit();
71void sendChatMessage();
72void toggleDebugging();
73void joinGame();
74void createGame();
75void leaveGame();
76void closeGameSummary();
77
78const float FPS = 60;
79const int SCREEN_W = 1024;
80const int SCREEN_H = 768;
81
82enum STATE {
83 STATE_START,
84 STATE_LOBBY,
85 STATE_GAME
86};
87
88int state;
89
90bool doexit;
91
92vector<GuiComponent*> vctComponents;
93
94Window* wndLogin;
95Window* wndRegister;
96Window* wndLobby;
97Window* wndLobbyDebug;
98Window* wndGame;
99Window* wndGameSummary;
100Window* wndCurrent;
101
102// wndLogin
103Textbox* txtUsername;
104Textbox* txtPassword;
105TextLabel* lblLoginStatus;
106
107// wndRegister
108Textbox* txtUsernameRegister;
109Textbox* txtPasswordRegister;
110RadioButtonList* rblClasses;
111TextLabel* lblRegisterStatus;
112
113// wndLobby
114Textbox* txtJoinGame;
115Textbox* txtCreateGame;
116Textbox* txtChat;
117
118int sock;
119struct sockaddr_in server, from;
120struct hostent *hp;
121NETWORK_MSG msgTo, msgFrom;
122string username;
123chat chatConsole, debugConsole;
124bool debugging;
125map<string, int> mapGames;
126Game* game;
127GameSummary* gameSummary;
128
129MessageProcessor msgProcessor;
130
131int main(int argc, char **argv)
132{
133 ALLEGRO_DISPLAY *display = NULL;
134 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
135 ALLEGRO_TIMER *timer = NULL;
136 bool key[4] = { false, false, false, false };
137 map<unsigned int, Player*> mapPlayers;
138 map<unsigned int, Projectile> mapProjectiles;
139 unsigned int curPlayerId = -1;
140 ofstream outputLog;
141
142 doexit = false;
143 debugging = false;
144 bool redraw = true;
145 bool fullscreen = false;
146 game = NULL;
147 gameSummary = NULL;
148
149 state = STATE_START;
150
151 if(!al_init()) {
152 fprintf(stderr, "failed to initialize allegro!\n");
153 return -1;
154 }
155
156 outputLog.open("client.log", ios::app);
157 outputLog << "Started client on " << getCurrentDateTimeString() << endl;
158
159 if (al_init_primitives_addon())
160 cout << "Primitives initialized" << endl;
161 else
162 cout << "Primitives not initialized" << endl;
163
164 al_init_font_addon();
165 al_init_ttf_addon();
166
167 #if defined WINDOWS
168 ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0);
169 #elif defined LINUX
170 ALLEGRO_FONT *font = al_load_ttf_font("pirulen.ttf", 12, 0);
171 #endif
172
173 if (!font) {
174 fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
175 getchar();
176 return -1;
177 }
178
179 if(!al_install_keyboard()) {
180 fprintf(stderr, "failed to initialize the keyboard!\n");
181 return -1;
182 }
183
184 if(!al_install_mouse()) {
185 fprintf(stderr, "failed to initialize the mouse!\n");
186 return -1;
187 }
188
189 timer = al_create_timer(1.0 / FPS);
190 if(!timer) {
191 fprintf(stderr, "failed to create timer!\n");
192 return -1;
193 }
194
195 int refreshRate = getRefreshRate(SCREEN_W, SCREEN_H);
196 // if the computer doesn't support this resolution, just use windowed mode
197 if (refreshRate > 0 && fullscreen) {
198 al_set_new_display_flags(ALLEGRO_FULLSCREEN);
199 al_set_new_display_refresh_rate(refreshRate);
200 }
201 display = al_create_display(SCREEN_W, SCREEN_H);
202 if(!display) {
203 fprintf(stderr, "failed to create display!\n");
204 al_destroy_timer(timer);
205 return -1;
206 }
207
208 debugConsole.addLine("Debug console:");
209 debugConsole.addLine("");
210
211 wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
212 vctComponents.push_back(wndLogin->addComponent(new Textbox(516, 40, 100, 20, font)));
213 vctComponents.push_back(wndLogin->addComponent(new Textbox(516, 70, 100, 20, font)));
214 vctComponents.push_back(wndLogin->addComponent(new TextLabel(410, 40, 100, 20, font, "Username:", ALLEGRO_ALIGN_RIGHT)));
215 vctComponents.push_back(wndLogin->addComponent(new TextLabel(410, 70, 100, 20, font, "Password:", ALLEGRO_ALIGN_RIGHT)));
216 vctComponents.push_back(wndLogin->addComponent(new TextLabel((SCREEN_W-600)/2, 100, 600, 20, font, "", ALLEGRO_ALIGN_CENTRE)));
217 vctComponents.push_back(wndLogin->addComponent(new Button(SCREEN_W/2-100, 130, 90, 20, font, "Register", goToRegisterScreen)));
218 vctComponents.push_back(wndLogin->addComponent(new Button(SCREEN_W/2+10, 130, 90, 20, font, "Login", login)));
219 vctComponents.push_back(wndLogin->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit)));
220 vctComponents.push_back(wndLogin->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
221
222 txtUsername = (Textbox*)wndLogin->getComponent(0);
223 txtPassword = (Textbox*)wndLogin->getComponent(1);
224 lblLoginStatus = (TextLabel*)wndLogin->getComponent(4);
225
226 cout << "Created login screen" << endl;
227
228 wndRegister = new Window(0, 0, SCREEN_W, SCREEN_H);
229 vctComponents.push_back(wndRegister->addComponent(new Textbox(516, 40, 100, 20, font)));
230 vctComponents.push_back(wndRegister->addComponent(new Textbox(516, 70, 100, 20, font)));
231 vctComponents.push_back(wndRegister->addComponent(new TextLabel(410, 40, 100, 20, font, "Username:", ALLEGRO_ALIGN_RIGHT)));
232 vctComponents.push_back(wndRegister->addComponent(new TextLabel(410, 70, 100, 20, font, "Password:", ALLEGRO_ALIGN_RIGHT)));
233 vctComponents.push_back(wndRegister->addComponent(new RadioButtonList(432, 100, "Pick a class", font)));
234 vctComponents.push_back(wndRegister->addComponent(new TextLabel((SCREEN_W-600)/2, 190, 600, 20, font, "", ALLEGRO_ALIGN_CENTRE)));
235 vctComponents.push_back(wndRegister->addComponent(new Button(SCREEN_W/2-100, 220, 90, 20, font, "Back", goToLoginScreen)));
236 vctComponents.push_back(wndRegister->addComponent(new Button(SCREEN_W/2+10, 220, 90, 20, font, "Submit", registerAccount)));
237 vctComponents.push_back(wndRegister->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit)));
238 vctComponents.push_back(wndRegister->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
239
240 txtUsernameRegister = (Textbox*)wndRegister->getComponent(0);
241 txtPasswordRegister = (Textbox*)wndRegister->getComponent(1);
242
243 rblClasses = (RadioButtonList*)wndRegister->getComponent(4);
244 rblClasses->addRadioButton("Warrior");
245 rblClasses->addRadioButton("Ranger");
246
247 lblRegisterStatus = (TextLabel*)wndRegister->getComponent(5);
248
249 cout << "Created register screen" << endl;
250
251 txtJoinGame = new Textbox(SCREEN_W*1/2+15+4, 40, 100, 20, font);
252 txtCreateGame = new Textbox(SCREEN_W*3/4+4, 40, 100, 20, font);
253
254 vctComponents.push_back(txtJoinGame);
255 vctComponents.push_back(txtCreateGame);
256
257 wndLobby = new Window(0, 0, SCREEN_W, SCREEN_H);
258 vctComponents.push_back(wndLobby->addComponent(new Button(920, 10, 80, 20, font, "Logout", logout)));
259 vctComponents.push_back(wndLobby->addComponent(new TextLabel(SCREEN_W*1/2+15-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
260 wndLobby->addComponent(txtJoinGame);
261 vctComponents.push_back(wndLobby->addComponent(new Button(SCREEN_W*1/2+15-100, 80, 200, 20, font, "Join Existing Game", joinGame)));
262 vctComponents.push_back(wndLobby->addComponent(new TextLabel(SCREEN_W*3/4-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
263 wndLobby->addComponent(txtCreateGame);
264 vctComponents.push_back(wndLobby->addComponent(new Button(SCREEN_W*3/4-100, 80, 200, 20, font, "Create New Game", createGame)));
265 vctComponents.push_back(wndLobby->addComponent(new Textbox(95, 40, 300, 20, font)));
266 vctComponents.push_back(wndLobby->addComponent(new Button(95, 70, 60, 20, font, "Send", sendChatMessage)));
267 vctComponents.push_back(wndLobby->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
268
269
270 txtChat = (Textbox*)wndLobby->getComponent(7);
271
272 cout << "Created lobby screen" << endl;
273
274 wndLobbyDebug = new Window(0, 0, SCREEN_W, SCREEN_H);
275 vctComponents.push_back(wndLobbyDebug->addComponent(new Button(920, 10, 80, 20, font, "Logout", logout)));
276 vctComponents.push_back(wndLobbyDebug->addComponent(new TextLabel(SCREEN_W*1/2+15-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
277 wndLobbyDebug->addComponent(txtJoinGame);
278 vctComponents.push_back(wndLobbyDebug->addComponent(new Button(SCREEN_W*1/2+15-100, 80, 200, 20, font, "Join Existing Game", joinGame)));
279 vctComponents.push_back(wndLobbyDebug->addComponent(new TextLabel(SCREEN_W*3/4-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
280 wndLobbyDebug->addComponent(txtCreateGame);
281 vctComponents.push_back(wndLobbyDebug->addComponent(new Button(SCREEN_W*3/4-100, 80, 200, 20, font, "Create New Game", createGame)));
282 vctComponents.push_back(wndLobbyDebug->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
283
284 cout << "Created debug lobby screen" << endl;
285
286 wndGame = new Window(0, 0, SCREEN_W, SCREEN_H);
287 vctComponents.push_back(wndGame->addComponent(new Button(880, 10, 120, 20, font, "Leave Game", leaveGame)));
288
289 cout << "Created new game screen" << endl;
290
291 wndGameSummary = new Window(0, 0, SCREEN_W, SCREEN_H);
292 vctComponents.push_back(wndGameSummary->addComponent(new Button(840, 730, 160, 20, font, "Back to Lobby", closeGameSummary)));
293
294 cout << "Created game summary screen" << endl;
295
296 goToLoginScreen();
297
298 event_queue = al_create_event_queue();
299 if(!event_queue) {
300 fprintf(stderr, "failed to create event_queue!\n");
301 al_destroy_display(display);
302 al_destroy_timer(timer);
303 return -1;
304 }
305
306 al_set_target_bitmap(al_get_backbuffer(display));
307
308 al_register_event_source(event_queue, al_get_display_event_source(display));
309 al_register_event_source(event_queue, al_get_timer_event_source(timer));
310 al_register_event_source(event_queue, al_get_keyboard_event_source());
311 al_register_event_source(event_queue, al_get_mouse_event_source());
312
313 al_clear_to_color(al_map_rgb(0,0,0));
314
315 al_flip_display();
316
317 if (argc != 3) {
318 cout << "Usage: server port" << endl;
319 exit(1);
320 }
321
322 initWinSock();
323
324 sock = socket(AF_INET, SOCK_DGRAM, 0);
325 if (sock < 0)
326 error("socket");
327
328 set_nonblock(sock);
329
330 server.sin_family = AF_INET;
331 hp = gethostbyname(argv[1]);
332 if (hp == 0)
333 error("Unknown host");
334
335 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
336 server.sin_port = htons(atoi(argv[2]));
337
338 msgProcessor = MessageProcessor(sock, &outputLog);
339
340 al_start_timer(timer);
341
342 while(!doexit)
343 {
344 ALLEGRO_EVENT ev;
345
346 al_wait_for_event(event_queue, &ev);
347
348 if(wndCurrent->handleEvent(ev)) {
349 // do nothing
350 }
351 else if(ev.type == ALLEGRO_EVENT_TIMER) {
352 redraw = true; // seems like we should just call a draw function here instead
353 }
354 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
355 doexit = true;
356 }
357 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
358 }
359 else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
360 switch(ev.keyboard.keycode) {
361 case ALLEGRO_KEY_ESCAPE:
362 doexit = true;
363 break;
364 case ALLEGRO_KEY_S: // pickup an item next to you
365 if (state == STATE_GAME) {
366 msgTo.type = MSG_TYPE_PICKUP_FLAG;
367 memcpy(msgTo.buffer, &curPlayerId, 4);
368 msgProcessor.sendMessage(&msgTo, &server);
369 }
370 break;
371 case ALLEGRO_KEY_D: // drop the current item
372 if (state == STATE_GAME) {
373 Player* p = NULL;
374 try {
375 p = mapPlayers.at(curPlayerId);
376 } catch (const out_of_range& ex) {}
377
378 if (p != NULL) {
379 int flagType = WorldMap::OBJECT_NONE;
380
381 if (p->hasBlueFlag)
382 flagType = WorldMap::OBJECT_BLUE_FLAG;
383 else if (p->hasRedFlag)
384 flagType = WorldMap::OBJECT_RED_FLAG;
385
386 if (flagType != WorldMap::OBJECT_NONE) {
387 msgTo.type = MSG_TYPE_DROP_FLAG;
388 memcpy(msgTo.buffer, &curPlayerId, 4);
389 msgProcessor.sendMessage(&msgTo, &server);
390 }
391 }
392 }
393 break;
394 }
395 }
396 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
397 if(wndCurrent == wndGame) {
398 if (ev.mouse.button == 1) { // left click
399 msgTo.type = MSG_TYPE_PLAYER_MOVE;
400
401 POSITION pos;
402 pos.x = ev.mouse.x;
403 pos.y = ev.mouse.y;
404 pos = screenToMap(pos);
405
406 if (pos.x != -1)
407 {
408 memcpy(msgTo.buffer, &curPlayerId, 4);
409 memcpy(msgTo.buffer+4, &pos.x, 4);
410 memcpy(msgTo.buffer+8, &pos.y, 4);
411
412 msgProcessor.sendMessage(&msgTo, &server);
413 }
414 else
415 cout << "Invalid point: User did not click on the map" << endl;
416 }else if (ev.mouse.button == 2) { // right click
417 cout << "Detected a right-click" << endl;
418 map<unsigned int, Player*>::iterator it;
419
420 Player* curPlayer = mapPlayers[curPlayerId];;
421
422 cout << "Got current player" << endl;
423 cout << "current game: " << game << endl;
424
425 map<unsigned int, Player*> playersInGame = game->getPlayers();
426 Player* target;
427
428 for(it = playersInGame.begin(); it != playersInGame.end(); it++)
429 {
430 // need to check if the right-click was actually on this player
431 // right now, this code will target all players other than the current one
432 target = it->second;
433 cout << "set target" << endl;
434 if (target->team != curPlayer->team)
435 {
436 cout << "Found valid target" << endl;
437
438 POSITION cursorPos;
439 cursorPos.x = ev.mouse.x;
440 cursorPos.y = ev.mouse.y;
441 cursorPos = screenToMap(cursorPos);
442
443 float distance =posDistance(cursorPos.toFloat(), target->pos);
444
445 if (distance < 25) {
446 unsigned int targetId = target->getId();
447
448 msgTo.type = MSG_TYPE_ATTACK;
449 memcpy(msgTo.buffer, &curPlayerId, 4);
450 memcpy(msgTo.buffer+4, &targetId, 4);
451
452 msgProcessor.sendMessage(&msgTo, &server);
453 }
454 }
455 }
456 }
457 }
458 }
459
460 if (msgProcessor.receiveMessage(&msgFrom, &from) >= 0)
461 processMessage(msgFrom, state, chatConsole, mapPlayers, mapProjectiles, curPlayerId);
462
463 if (redraw)
464 {
465 redraw = false;
466
467 msgProcessor.resendUnackedMessages();
468
469 if (debugging && wndCurrent == wndLobby)
470 wndLobbyDebug->draw(display);
471 else
472 wndCurrent->draw(display);
473
474 if (wndCurrent == wndLobby) {
475 if (!debugging)
476 chatConsole.draw(font, al_map_rgb(255,255,255));
477
478 map<string, int>::iterator it;
479 int i=0;
480 ostringstream ossGame;
481 for (it = mapGames.begin(); it != mapGames.end(); it++) {
482 ossGame << it->first << " (" << it->second << " players)" << endl;
483 al_draw_text(font, al_map_rgb(0, 255, 0), SCREEN_W*1/2-100, 120+i*15, ALLEGRO_ALIGN_LEFT, ossGame.str().c_str());
484 ossGame.clear();
485 ossGame.str("");
486 i++;
487 }
488 }
489 else if (wndCurrent == wndGame)
490 {
491 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 4, ALLEGRO_ALIGN_LEFT, "Players");
492
493 map<unsigned int, Player*>& gamePlayers = game->getPlayers();
494 map<unsigned int, Player*>::iterator it;
495
496 if (!debugging) {
497 int playerCount = 0;
498 for (it = gamePlayers.begin(); it != gamePlayers.end(); it++)
499 {
500 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 19+(playerCount+1)*15, ALLEGRO_ALIGN_LEFT, it->second->name.c_str());
501 playerCount++;
502 }
503 }
504
505 ostringstream ossScoreBlue, ossScoreRed;
506
507 ossScoreBlue << "Blue: " << game->getBlueScore() << endl;
508 ossScoreRed << "Red: " << game->getRedScore() << endl;
509
510 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
511 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
512
513 // update players
514 for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
515 {
516 it->second->updateTarget(game->getPlayers());
517 }
518
519 for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
520 {
521 it->second->move(game->getMap()); // ignore return value
522 }
523
524 // update projectile positions
525 map<unsigned int, Projectile>::iterator it2;
526 for (it2 = game->getProjectiles().begin(); it2 != game->getProjectiles().end(); it2++)
527 {
528 it2->second.move(game->getPlayers());
529 }
530
531 GameRender::drawMap(game->getMap());
532 GameRender::drawPlayers(game->getPlayers(), font, curPlayerId);
533 GameRender::drawProjectiles(game->getProjectiles(), game->getPlayers());
534 }
535 else if (wndCurrent == wndGameSummary)
536 {
537 ostringstream ossBlueScore, ossRedScore;
538
539 ossBlueScore << "Blue Score: " << gameSummary->getBlueScore();
540 ossRedScore << "Red Score: " << gameSummary->getRedScore();
541
542 string strWinner;
543
544 if (gameSummary->getWinner() == 0)
545 strWinner = "Blue Team Wins";
546 else if (gameSummary->getWinner() == 1)
547 strWinner = "Red Team Wins";
548 else
549 strWinner = "winner set to wrong value";
550
551 al_draw_text(font, al_map_rgb(0, 255, 0), 512, 40, ALLEGRO_ALIGN_CENTRE, gameSummary->getName().c_str());
552 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossBlueScore.str().c_str());
553 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossRedScore.str().c_str());
554 al_draw_text(font, al_map_rgb(0, 255, 0), 512, 120, ALLEGRO_ALIGN_CENTRE, strWinner.c_str());
555 }
556
557 if (debugging) {
558 //debugConsole.draw(font, al_map_rgb(255,255,255));
559 drawMessageStatus(font);
560 }
561
562 al_flip_display();
563 }
564 }
565
566 #if defined WINDOWS
567 closesocket(sock);
568 #elif defined LINUX
569 close(sock);
570 #endif
571
572 shutdownWinSock();
573
574 // delete all components
575 for (unsigned int x=0; x<vctComponents.size(); x++)
576 delete vctComponents[x];
577
578 delete wndLogin;
579 delete wndRegister;
580 delete wndLobby;
581 delete wndLobbyDebug;
582 delete wndGame;
583 delete wndGameSummary;
584
585 // game should be deleted when the player leaves a gamw
586 if (game != NULL)
587 delete game;
588
589 if (gameSummary != NULL)
590 delete gameSummary;
591
592 map<unsigned int, Player*>::iterator it;
593
594 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
595 delete it->second;
596 }
597
598 al_destroy_event_queue(event_queue);
599 al_destroy_display(display);
600 al_destroy_timer(timer);
601
602 outputLog << "Stopped client on " << getCurrentDateTimeString() << endl;
603 outputLog.close();
604
605 return 0;
606}
607
608void initWinSock()
609{
610#if defined WINDOWS
611 WORD wVersionRequested;
612 WSADATA wsaData;
613 int wsaerr;
614
615 wVersionRequested = MAKEWORD(2, 2);
616 wsaerr = WSAStartup(wVersionRequested, &wsaData);
617
618 if (wsaerr != 0) {
619 cout << "The Winsock dll not found." << endl;
620 exit(1);
621 }else
622 cout << "The Winsock dll was found." << endl;
623#endif
624}
625
626void shutdownWinSock()
627{
628#if defined WINDOWS
629 WSACleanup();
630#endif
631}
632
633void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player*>& mapPlayers,
634 map<unsigned int, Projectile>& mapProjectiles, unsigned int& curPlayerId)
635{
636 // this is outdated since most messages now don't contain just a text string
637 string response = string(msg.buffer);
638
639 switch(state)
640 {
641 case STATE_START:
642 {
643 cout << "In STATE_START" << endl;
644
645 switch(msg.type)
646 {
647 case MSG_TYPE_REGISTER:
648 {
649 lblRegisterStatus->setText(response);
650 break;
651 }
652 default:
653 {
654 cout << "(STATE_REGISTER) Received invalid message of type " << msg.type << endl;
655 break;
656 }
657 }
658
659 break;
660 }
661 case STATE_LOBBY:
662 {
663 cout << "In STATE_LOBBY" << endl;
664 switch(msg.type)
665 {
666 case MSG_TYPE_LOGIN:
667 {
668 if (response.compare("Player has already logged in.") == 0)
669 {
670 goToLoginScreen();
671 state = STATE_START;
672
673 lblLoginStatus->setText(response);
674 }
675 else if (response.compare("Incorrect username or password") == 0)
676 {
677 goToLoginScreen();
678 state = STATE_START;
679
680 lblLoginStatus->setText(response);
681 }
682 else
683 {
684 wndCurrent = wndLobby;
685
686 // this message should only be sent when a player first logs in so they know their id
687
688 Player* p = new Player("", "");
689 p->deserialize(msg.buffer);
690
691 if (mapPlayers.find(p->getId()) != mapPlayers.end())
692 delete mapPlayers[p->getId()];
693 mapPlayers[p->getId()] = p;
694 curPlayerId = p->getId();
695
696 cout << "Got a valid login response with the player" << endl;
697 cout << "Player id: " << curPlayerId << endl;
698 cout << "Player health: " << p->health << endl;
699 cout << "player map size: " << mapPlayers.size() << endl;
700 }
701
702 break;
703 }
704 case MSG_TYPE_LOGOUT:
705 {
706 cout << "Got a logout message" << endl;
707
708 int playerId;
709
710 // Check if it's about you or another player
711 memcpy(&playerId, msg.buffer, 4);
712 response = string(msg.buffer+4);
713
714 if (playerId == curPlayerId)
715 {
716 if (response.compare("You have successfully logged out.") == 0)
717 {
718 cout << "Logged out" << endl;
719 state = STATE_START;
720 goToLoginScreen();
721 }
722
723 // if there was an error logging out, nothing happens
724 }
725 else
726 {
727 delete mapPlayers[playerId];
728 }
729
730 break;
731 }
732 case MSG_TYPE_PLAYER:
733 {
734 cout << "Received MSG_TYPE_PLAYER" << endl;
735
736 Player p("", "");
737 p.deserialize(msg.buffer);
738 p.timeLastUpdated = getCurrentMillis();
739 p.isChasing = false;
740 if (p.health <= 0)
741 p.isDead = true;
742 else
743 p.isDead = false;
744
745 if (mapPlayers.find(p.getId()) != mapPlayers.end())
746 *(mapPlayers[p.getId()]) = p;
747 else
748 mapPlayers[p.getId()] = new Player(p);
749
750 break;
751 }
752 case MSG_TYPE_PLAYER_MOVE:
753 {
754 unsigned int id;
755 int x, y;
756
757 memcpy(&id, msg.buffer, 4);
758 memcpy(&x, msg.buffer+4, 4);
759 memcpy(&y, msg.buffer+8, 4);
760
761 mapPlayers[id]->target.x = x;
762 mapPlayers[id]->target.y = y;
763
764 break;
765 }
766 case MSG_TYPE_CHAT:
767 {
768 chatConsole.addLine(response);
769
770 break;
771 }
772 case MSG_TYPE_ATTACK:
773 {
774 cout << "Received START_ATTACK message" << endl;
775
776 unsigned int id, targetID;
777 memcpy(&id, msg.buffer, 4);
778 memcpy(&targetID, msg.buffer+4, 4);
779
780 cout << "source id: " << id << endl;
781 cout << "target id: " << targetID << endl;
782
783 Player* source = mapPlayers[id];
784 source->setTargetPlayer(targetID);
785 source->isChasing = true;
786
787 break;
788 }
789 case MSG_TYPE_PROJECTILE:
790 {
791 cout << "Received a PROJECTILE message" << endl;
792
793 unsigned int id, x, y, targetId;
794
795 memcpy(&id, msg.buffer, 4);
796 memcpy(&x, msg.buffer+4, 4);
797 memcpy(&y, msg.buffer+8, 4);
798 memcpy(&targetId, msg.buffer+12, 4);
799
800 cout << "id: " << id << endl;
801 cout << "x: " << x << endl;
802 cout << "y: " << y << endl;
803 cout << "Target: " << targetId << endl;
804
805 Projectile proj(x, y, targetId, 0);
806 proj.setId(id);
807
808 mapProjectiles[id] = proj;
809
810 break;
811 }
812 case MSG_TYPE_REMOVE_PROJECTILE:
813 {
814 cout << "Received a REMOVE_PROJECTILE message" << endl;
815
816 int id;
817 memcpy(&id, msg.buffer, 4);
818
819 mapProjectiles.erase(id);
820
821 break;
822 }
823 case MSG_TYPE_GAME_INFO:
824 {
825 cout << "Received a GAME_INFO message" << endl;
826
827 string gameName(msg.buffer+4);
828 int numPlayers;
829
830 memcpy(&numPlayers, msg.buffer, 4);
831
832 cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;
833
834 if (numPlayers > 0)
835 mapGames[gameName] = numPlayers;
836 else
837 mapGames.erase(gameName);
838
839 break;
840 }
841 case MSG_TYPE_JOIN_GAME_SUCCESS:
842 {
843 cout << "Received a JOIN_GAME_SUCCESS message" << endl;
844
845 string gameName(msg.buffer);
846
847 #if defined WINDOWS
848 game = new Game(gameName, "../../data/map.txt", &msgProcessor);
849 #elif defined LINUX
850 game = new Game(gameName, "../data/map.txt", &msgProcessor);
851 #endif
852
853 cout << "Game name: " << gameName << endl;
854
855 state = STATE_GAME;
856 wndCurrent = wndGame;
857
858 msgTo.type = MSG_TYPE_JOIN_GAME_ACK;
859 strcpy(msgTo.buffer, gameName.c_str());
860
861 msgProcessor.sendMessage(&msgTo, &server);
862
863 break;
864 }
865 case MSG_TYPE_JOIN_GAME_FAILURE:
866 {
867 cout << "Received a JOIN_GAME_FAILURE message" << endl;
868
869 break;
870 }
871 default:
872 {
873 cout << "(STATE_LOBBY) Received invlaid message of type " << msg.type << endl;
874
875 break;
876 }
877 }
878
879 break;
880 }
881 case STATE_GAME:
882 {
883 cout << "(STATE_GAME) ";
884 switch(msg.type)
885 {
886 case MSG_TYPE_GAME_INFO:
887 {
888 cout << "Received a GAME_INFO message" << endl;
889
890 string gameName(msg.buffer+4);
891 int numPlayers;
892
893 memcpy(&numPlayers, msg.buffer, 4);
894
895 cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;
896
897 if (numPlayers > 0)
898 mapGames[gameName] = numPlayers;
899 else
900 mapGames.erase(gameName);
901
902 break;
903 }
904 case MSG_TYPE_SCORE:
905 {
906 cout << "Received SCORE message!" << endl;
907
908 int blueScore;
909 memcpy(&blueScore, msg.buffer, 4);
910 cout << "blue score: " << blueScore << endl;
911 game->setBlueScore(blueScore);
912
913 int redScore;
914 memcpy(&redScore, msg.buffer+4, 4);
915 cout << "red score: " << redScore << endl;
916 game->setRedScore(redScore);
917
918 cout << "Processed SCORE message!" << endl;
919
920 break;
921 }
922 case MSG_TYPE_FINISH_GAME:
923 {
924 cout << "Got a finish game message" << endl;
925 cout << "Should switch to STATE_LOBBY and show the final score" << endl;
926
927 unsigned int winner, blueScore, redScore;
928 memcpy(&winner, msg.buffer, 4);
929 memcpy(&blueScore, msg.buffer+4, 4);
930 memcpy(&redScore, msg.buffer+8, 4);
931
932 string gameName(msg.buffer+12);
933
934 cout << "winner: " << winner << endl;
935 cout << "blueScore: " << blueScore << endl;
936 cout << "redScore: " << redScore << endl;
937 cout << "gameName: " << gameName << endl;
938
939 gameSummary = new GameSummary(gameName, winner, blueScore, redScore);
940
941 delete game;
942 game = NULL;
943 state = STATE_LOBBY;
944 wndCurrent = wndGameSummary;
945
946
947 cout << "winner from obj: " << gameSummary->getWinner() << endl;
948 cout << "blueScore from obj: " << gameSummary->getBlueScore() << endl;
949 cout << "redScore from obj: " << gameSummary->getRedScore() << endl;
950 cout << "gameName from obj: " << gameSummary->getName() << endl;
951 break;
952 }
953 case MSG_TYPE_PLAYER:
954 {
955 cout << "Received MSG_TYPE_PLAYER" << endl;
956
957 Player p("", "");
958 p.deserialize(msg.buffer);
959 p.timeLastUpdated = getCurrentMillis();
960 p.isChasing = false;
961 if (p.health <= 0)
962 p.isDead = true;
963 else
964 p.isDead = false;
965
966 if (mapPlayers.find(p.getId()) != mapPlayers.end())
967 *(mapPlayers[p.getId()]) = p;
968 else
969 mapPlayers[p.getId()] = new Player(p);
970
971 break;
972 }
973 case MSG_TYPE_PLAYER_JOIN_GAME:
974 {
975 cout << "Received MSG_TYPE_PLAYER_JOIN_GAME" << endl;
976
977 Player p("", "");
978 p.deserialize(msg.buffer);
979 cout << "Deserialized player" << endl;
980 p.timeLastUpdated = getCurrentMillis();
981 p.isChasing = false;
982 if (p.health <= 0)
983 p.isDead = true;
984 else
985 p.isDead = false;
986
987 if (mapPlayers.find(p.getId()) != mapPlayers.end())
988 *(mapPlayers[p.getId()]) = p;
989 else
990 mapPlayers[p.getId()] = new Player(p);
991
992 game->addPlayer(mapPlayers[p.getId()]);
993
994 break;
995 }
996 case MSG_TYPE_LEAVE_GAME:
997 {
998 cout << "Received a LEAVE_GAME message" << endl;
999
1000 string gameName(msg.buffer+4);
1001 unsigned int playerId;
1002
1003 memcpy(&playerId, msg.buffer, 4);
1004
1005 game->removePlayer(playerId);
1006
1007 break;
1008 }
1009 case MSG_TYPE_PLAYER_MOVE:
1010 {
1011 cout << "Received PLAYER_MOVE message" << endl;
1012
1013 unsigned int id;
1014 int x, y;
1015
1016 memcpy(&id, msg.buffer, 4);
1017 memcpy(&x, msg.buffer+4, 4);
1018 memcpy(&y, msg.buffer+8, 4);
1019
1020 cout << "id: " << id << endl;
1021
1022 mapPlayers[id]->target.x = x;
1023 mapPlayers[id]->target.y = y;
1024
1025 break;
1026 }
1027 case MSG_TYPE_OBJECT:
1028 {
1029 cout << "Received object message in STATE_GAME" << endl;
1030
1031 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
1032 o.deserialize(msg.buffer);
1033 cout << "object id: " << o.id << endl;
1034 game->getMap()->updateObject(o.id, o.type, o.pos.x, o.pos.y);
1035
1036 break;
1037 }
1038 case MSG_TYPE_REMOVE_OBJECT:
1039 {
1040 cout << "Received REMOVE_OBJECT message!" << endl;
1041
1042 int id;
1043 memcpy(&id, msg.buffer, 4);
1044
1045 cout << "Removing object with id " << id << endl;
1046
1047 if (!game->getMap()->removeObject(id))
1048 cout << "Did not remove the object" << endl;
1049
1050 break;
1051 }
1052 case MSG_TYPE_ATTACK:
1053 {
1054 cout << "Received START_ATTACK message" << endl;
1055
1056 unsigned int id, targetId;
1057 memcpy(&id, msg.buffer, 4);
1058 memcpy(&targetId, msg.buffer+4, 4);
1059
1060 cout << "source id: " << id << endl;
1061 cout << "target id: " << targetId << endl;
1062
1063 // need to check the target exists in the current game
1064 Player* source = game->getPlayers()[id];
1065 source->setTargetPlayer(targetId);
1066 source->isChasing = true;
1067
1068 break;
1069 }
1070 case MSG_TYPE_PROJECTILE:
1071 {
1072 cout << "Received a PROJECTILE message" << endl;
1073
1074 unsigned int projId, x, y, targetId;
1075
1076 memcpy(&projId, msg.buffer, 4);
1077 memcpy(&x, msg.buffer+4, 4);
1078 memcpy(&y, msg.buffer+8, 4);
1079 memcpy(&targetId, msg.buffer+12, 4);
1080
1081 cout << "projId: " << projId << endl;
1082 cout << "x: " << x << endl;
1083 cout << "y: " << y << endl;
1084 cout << "Target: " << targetId << endl;
1085
1086 Projectile proj(x, y, targetId, 0);
1087 proj.setId(projId);
1088
1089 game->addProjectile(proj);
1090
1091 break;
1092 }
1093 case MSG_TYPE_REMOVE_PROJECTILE:
1094 {
1095 cout << "Received a REMOVE_PROJECTILE message" << endl;
1096
1097 unsigned int id;
1098 memcpy(&id, msg.buffer, 4);
1099
1100 game->removeProjectile(id);
1101
1102 break;
1103 }
1104 default:
1105 {
1106 cout << "Received invalid message of type " << msg.type << endl;
1107
1108 break;
1109 }
1110 }
1111
1112 break;
1113 }
1114 default:
1115 {
1116 cout << "The state has an invalid value: " << state << endl;
1117
1118 break;
1119 }
1120 }
1121}
1122
1123int getRefreshRate(int width, int height)
1124{
1125 int numRefreshRates = al_get_num_display_modes();
1126 ALLEGRO_DISPLAY_MODE displayMode;
1127
1128 for(int i=0; i<numRefreshRates; i++) {
1129 al_get_display_mode(i, &displayMode);
1130
1131 if (displayMode.width == width && displayMode.height == height)
1132 return displayMode.refresh_rate;
1133 }
1134
1135 return 0;
1136}
1137
1138void drawMessageStatus(ALLEGRO_FONT* font)
1139{
1140 int clientMsgOffset = 5;
1141 int serverMsgOffset = 950;
1142
1143 al_draw_text(font, al_map_rgb(0, 255, 255), 0+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
1144 al_draw_text(font, al_map_rgb(0, 255, 255), 20+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Type");
1145 al_draw_text(font, al_map_rgb(0, 255, 255), 240+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Acked?");
1146
1147 //al_draw_text(font, al_map_rgb(0, 255, 255), serverMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
1148
1149 map<unsigned int, map<unsigned long, MessageContainer> >& sentMessages = msgProcessor.getSentMessages();
1150 int id, type;
1151 bool acked;
1152 ostringstream ossId, ossAcked;
1153
1154 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
1155
1156 int msgCount = 0;
1157 for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
1158 map<unsigned long, MessageContainer> playerMessage = it->second;
1159 map<unsigned long, MessageContainer>::iterator it2;
1160 for (it2 = playerMessage.begin(); it2 != playerMessage.end(); it2++) {
1161
1162 id = it->first;
1163 ossId.str("");;
1164 ossId << id;
1165
1166 type = it2->second.getMessage()->type;
1167 string typeStr = MessageContainer::getMsgTypeString(type);
1168
1169 acked = it2->second.getAcked();
1170 ossAcked.str("");;
1171 ossAcked << boolalpha << acked;
1172
1173 al_draw_text(font, al_map_rgb(0, 255, 0), clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
1174 al_draw_text(font, al_map_rgb(0, 255, 0), 20+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, typeStr.c_str());
1175 al_draw_text(font, al_map_rgb(0, 255, 0), 240+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossAcked.str().c_str());
1176
1177 msgCount++;
1178 }
1179 }
1180
1181 if (msgProcessor.getAckedMessages().size() > 0) {
1182 map<unsigned int, unsigned long long> ackedMessages = msgProcessor.getAckedMessages()[0];
1183 map<unsigned int, unsigned long long>::iterator it3;
1184
1185 msgCount = 0;
1186 for (it3 = ackedMessages.begin(); it3 != ackedMessages.end(); it3++) {
1187 ossId.str("");;
1188 ossId << it3->first;
1189
1190 al_draw_text(font, al_map_rgb(255, 0, 0), 25+serverMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
1191
1192 msgCount++;
1193 }
1194 }
1195}
1196
1197// Callback definitions
1198
1199void goToRegisterScreen()
1200{
1201 txtUsernameRegister->clear();
1202 txtPasswordRegister->clear();
1203 lblRegisterStatus->setText("");
1204 rblClasses->setSelectedButton(-1);
1205
1206 wndCurrent = wndRegister;
1207}
1208
1209void goToLoginScreen()
1210{
1211 txtUsername->clear();
1212 txtPassword->clear();
1213 lblLoginStatus->setText("");
1214
1215 wndCurrent = wndLogin;
1216}
1217
1218// maybe need a goToGameScreen function as well and add state changes to these functions as well
1219
1220void registerAccount()
1221{
1222 string username = txtUsernameRegister->getStr();
1223 string password = txtPasswordRegister->getStr();
1224
1225 txtUsernameRegister->clear();
1226 txtPasswordRegister->clear();
1227 // maybe clear rblClasses as well (add a method to RadioButtonList to enable this)
1228
1229 Player::PlayerClass playerClass;
1230
1231 switch (rblClasses->getSelectedButton()) {
1232 case 0:
1233 playerClass = Player::CLASS_WARRIOR;
1234 break;
1235 case 1:
1236 playerClass = Player::CLASS_RANGER;
1237 break;
1238 default:
1239 cout << "Invalid class selection" << endl;
1240 playerClass = Player::CLASS_NONE;
1241 break;
1242 }
1243
1244 msgTo.type = MSG_TYPE_REGISTER;
1245
1246 strcpy(msgTo.buffer, username.c_str());
1247 strcpy(msgTo.buffer+username.size()+1, password.c_str());
1248 memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
1249
1250 msgProcessor.sendMessage(&msgTo, &server);
1251}
1252
1253void login()
1254{
1255 string strUsername = txtUsername->getStr();
1256 string strPassword = txtPassword->getStr();
1257 username = strUsername;
1258
1259 txtUsername->clear();
1260 txtPassword->clear();
1261
1262 msgTo.type = MSG_TYPE_LOGIN;
1263
1264 strcpy(msgTo.buffer, strUsername.c_str());
1265 strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
1266
1267 msgProcessor.sendMessage(&msgTo, &server);
1268
1269 state = STATE_LOBBY;
1270}
1271
1272void logout()
1273{
1274 switch(state) {
1275 case STATE_LOBBY:
1276 txtJoinGame->clear();
1277 txtCreateGame->clear();
1278 break;
1279 default:
1280 cout << "Logout called from invalid state: " << state << endl;
1281 break;
1282 }
1283
1284 msgTo.type = MSG_TYPE_LOGOUT;
1285
1286 strcpy(msgTo.buffer, username.c_str());
1287
1288 msgProcessor.sendMessage(&msgTo, &server);
1289}
1290
1291void quit()
1292{
1293 doexit = true;
1294}
1295
1296void sendChatMessage()
1297{
1298 string msg = txtChat->getStr();
1299 txtChat->clear();
1300
1301 msgTo.type = MSG_TYPE_CHAT;
1302 strcpy(msgTo.buffer, msg.c_str());
1303
1304 msgProcessor.sendMessage(&msgTo, &server);
1305}
1306
1307void toggleDebugging()
1308{
1309 debugging = !debugging;
1310}
1311
1312void joinGame()
1313{
1314 cout << "Joining game" << endl;
1315
1316 string msg = txtJoinGame->getStr();
1317 txtJoinGame->clear();
1318
1319 msgTo.type = MSG_TYPE_JOIN_GAME;
1320 strcpy(msgTo.buffer, msg.c_str());
1321
1322 msgProcessor.sendMessage(&msgTo, &server);
1323}
1324
1325void createGame()
1326{
1327 cout << "Creating game" << endl;
1328
1329 string msg = txtCreateGame->getStr();
1330 txtCreateGame->clear();
1331
1332 cout << "Sending message: " << msg.c_str() << endl;
1333
1334 msgTo.type = MSG_TYPE_CREATE_GAME;
1335 strcpy(msgTo.buffer, msg.c_str());
1336
1337 msgProcessor.sendMessage(&msgTo, &server);
1338}
1339
1340void leaveGame()
1341{
1342 cout << "Leaving game" << endl;
1343
1344 game = NULL;
1345
1346 state = STATE_LOBBY;
1347 wndCurrent = wndLobby;
1348
1349 msgTo.type = MSG_TYPE_LEAVE_GAME;
1350
1351 msgProcessor.sendMessage(&msgTo, &server);
1352}
1353
1354void closeGameSummary()
1355{
1356 delete gameSummary;
1357 gameSummary = NULL;
1358 wndCurrent = wndLobby;
1359 cout << "Processed button actions" << endl;
1360}
Note: See TracBrowser for help on using the repository browser.