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

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

The global projectile map and related code is gone from main.cpp, the client processes LOGOUT messages about other players from STATE_GAME, and the map drawing code in GameRender uses switches instead of ifs

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