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

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

Client no longer processes messages in STATE_LOBBY that should only be sent when the client is in STATE_GAME. Processing for messages handled in multiple states has been moved to separate functions)

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