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

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

Client updates related to the attack message change

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