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

Last change on this file since e2a0a27 was 45c9d0f, checked in by Dmitry Portnoy <dmp1488@…>, 10 years ago

Remove the response variable in the client-side processMessage

  • Property mode set to 100644
File size: 50.3 KB
RevLine 
[4c202e0]1#include "../../common/Compiler.h"
2
[e08572c]3#if defined WINDOWS
[0dde5da]4 #include <winsock2.h>
[6319311]5 #include <ws2tcpip.h>
[e08572c]6#elif defined LINUX
[0dde5da]7 #include <sys/types.h>
8 #include <unistd.h>
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <netdb.h>
12 #include <cstring>
[34bd549]13#elif defined MAC
14 #include <netdb.h>
[a845faf]15#endif
[1912323]16
[88cdae2]17#include <cstdio>
18#include <cstdlib>
[8c74150]19#include <sys/types.h>
[a845faf]20#include <string>
[1912323]21#include <iostream>
[ace001a]22#include <iomanip>
[88cdae2]23#include <sstream>
[8271c78]24#include <fstream>
[3a79253]25#include <map>
[f63aa57]26#include <vector>
[8aed9c0]27#include <stdexcept>
[3a79253]28
[d352805]29#include <allegro5/allegro.h>
30#include <allegro5/allegro_font.h>
31#include <allegro5/allegro_ttf.h>
[88cdae2]32#include <allegro5/allegro_primitives.h>
[7d7df47]33
[e607c0f]34#include "../../common/Common.h"
[b35b2b2]35#include "../../common/MessageContainer.h"
[10f6fc2]36#include "../../common/MessageProcessor.h"
[62ee2ce]37#include "../../common/WorldMap.h"
[4c202e0]38#include "../../common/Player.h"
[fbcfc35]39#include "../../common/Projectile.h"
[2ee386d]40#include "../../common/Game.h"
[3e44a59]41#include "../../common/GameSummary.h"
[7d7df47]42
[87b3ee2]43#include "Window.h"
[6319311]44#include "TextLabel.h"
[87b3ee2]45#include "Button.h"
[6319311]46#include "Textbox.h"
[5c95436]47#include "RadioButtonList.h"
[6319311]48
49#include "GameRender.h"
50
[6475138]51#include "chat.h"
52
[a845faf]53#ifdef WINDOWS
[6475138]54 #pragma comment(lib, "ws2_32.lib")
[a845faf]55#endif
[1912323]56
57using namespace std;
58
[0dde5da]59void initWinSock();
60void shutdownWinSock();
[b29ff6b]61void createGui(ALLEGRO_FONT* font);
[6f64166]62
[35d702d]63void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames, unsigned int& curPlayerId, string& alertMessage);
[6f64166]64void handleMsgPlayer(NETWORK_MSG &msg, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames);
65void handleMsgGameInfo(NETWORK_MSG &msg, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames);
66
[1f1eb58]67int getRefreshRate(int width, int height);
[929b4e0]68void drawMessageStatus(ALLEGRO_FONT* font);
[87b3ee2]69
[929b4e0]70// Callback declarations
[5c95436]71void goToLoginScreen();
72void goToRegisterScreen();
[87b3ee2]73void registerAccount();
74void login();
75void logout();
76void quit();
77void sendChatMessage();
[b35b2b2]78void toggleDebugging();
[fd9cdb5]79void goToProfileScreen();
80void goToLobbyScreen();
[a0ce8a3]81void joinGame(); // for joining the game lobby
82void createGame(); // for joining the game lobby
83void joinWaitingArea();
84void joinRedTeam();
85void joinBlueTeam();
86void startGame(); // for leaving game lobby and starting the actual game
[03ba5e3]87void leaveGame();
[3e44a59]88void closeGameSummary();
[4da5aa3]89
[d352805]90const float FPS = 60;
[9b1e12c]91const int SCREEN_W = 1024;
92const int SCREEN_H = 768;
[0cc431d]93
94enum STATE {
95 STATE_START,
[1785314]96 STATE_LOBBY,
[a0ce8a3]97 STATE_GAME_LOBBY,
[e0fd377]98 STATE_GAME
[d352805]99};
[87b3ee2]100
101int state;
102
103bool doexit;
104
[f63aa57]105vector<GuiComponent*> vctComponents;
106
[87b3ee2]107Window* wndLogin;
[5c95436]108Window* wndRegister;
[1785314]109Window* wndLobby;
[f63aa57]110Window* wndLobbyDebug;
[fd9cdb5]111Window* wndProfile;
[a0ce8a3]112Window* wndGameLobby;
[3ff2bd7]113Window* wndGame;
[3e44a59]114Window* wndGameSummary;
[87b3ee2]115Window* wndCurrent;
116
[5c95436]117// wndLogin
[87b3ee2]118Textbox* txtUsername;
119Textbox* txtPassword;
[365e156]120TextLabel* lblLoginStatus;
[5c95436]121
122// wndRegister
123Textbox* txtUsernameRegister;
124Textbox* txtPasswordRegister;
125RadioButtonList* rblClasses;
[365e156]126TextLabel* lblRegisterStatus;
[5c95436]127
[929b4e0]128// wndLobby
129Textbox* txtJoinGame;
130Textbox* txtCreateGame;
[87b3ee2]131Textbox* txtChat;
132
133int sock;
134struct sockaddr_in server, from;
135struct hostent *hp;
136NETWORK_MSG msgTo, msgFrom;
137string username;
[b35b2b2]138chat chatConsole, debugConsole;
139bool debugging;
[803566d]140Game* game;
[3e44a59]141GameSummary* gameSummary;
[a0ce8a3]142Player* currentPlayer;
[1f1eb58]143
[4c00935]144int honorPoints, wins, losses, numGames;
[b28e2bf]145int** gameHistory;
146
[10f6fc2]147MessageProcessor msgProcessor;
148
[35d702d]149string alertMessage;
150
[d352805]151int main(int argc, char **argv)
152{
153 ALLEGRO_DISPLAY *display = NULL;
154 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
155 ALLEGRO_TIMER *timer = NULL;
[e6c26b8]156 map<unsigned int, Player*> mapPlayers;
[6f64166]157 map<string, int> mapGames;
[88cdae2]158 unsigned int curPlayerId = -1;
[68d94de]159 ofstream outputLog;
160
[803566d]161 doexit = false;
[b35b2b2]162 debugging = false;
[803566d]163 bool redraw = true;
164 bool fullscreen = false;
165 game = NULL;
[3e44a59]166 gameSummary = NULL;
[15efb4e]167
[b28e2bf]168 honorPoints = 0;
[4c00935]169 wins = 0;
170 losses = 0;
[b28e2bf]171 numGames = 0;
172 gameHistory = NULL;
173
[35d702d]174 alertMessage = "";
175
[87b3ee2]176 state = STATE_START;
[9a3e6b1]177
[d352805]178 if(!al_init()) {
179 fprintf(stderr, "failed to initialize allegro!\n");
180 return -1;
181 }
182
[8271c78]183 outputLog.open("client.log", ios::app);
184 outputLog << "Started client on " << getCurrentDateTimeString() << endl;
185
[88cdae2]186 if (al_init_primitives_addon())
187 cout << "Primitives initialized" << endl;
188 else
189 cout << "Primitives not initialized" << endl;
190
[d352805]191 al_init_font_addon();
192 al_init_ttf_addon();
193
[b29ff6b]194 ALLEGRO_FONT* font;
[88cdae2]195 #if defined WINDOWS
[b29ff6b]196 font = al_load_ttf_font("../pirulen.ttf", 12, 0);
[88cdae2]197 #elif defined LINUX
[b29ff6b]198 font = al_load_ttf_font("pirulen.ttf", 12, 0);
[34bd549]199 #elif defined MAC
200 font = al_load_ttf_font("pirulen.ttf", 12, 0);
[88cdae2]201 #endif
202
[d352805]203 if (!font) {
204 fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
205 getchar();
[803566d]206 return -1;
[d352805]207 }
208
209 if(!al_install_keyboard()) {
210 fprintf(stderr, "failed to initialize the keyboard!\n");
211 return -1;
212 }
[87b3ee2]213
214 if(!al_install_mouse()) {
215 fprintf(stderr, "failed to initialize the mouse!\n");
216 return -1;
217 }
[d352805]218
219 timer = al_create_timer(1.0 / FPS);
220 if(!timer) {
221 fprintf(stderr, "failed to create timer!\n");
222 return -1;
223 }
224
[1f1eb58]225 int refreshRate = getRefreshRate(SCREEN_W, SCREEN_H);
226 // if the computer doesn't support this resolution, just use windowed mode
227 if (refreshRate > 0 && fullscreen) {
228 al_set_new_display_flags(ALLEGRO_FULLSCREEN);
229 al_set_new_display_refresh_rate(refreshRate);
230 }
231 display = al_create_display(SCREEN_W, SCREEN_H);
[d352805]232 if(!display) {
233 fprintf(stderr, "failed to create display!\n");
234 al_destroy_timer(timer);
235 return -1;
236 }
[87b3ee2]237
[b35b2b2]238 debugConsole.addLine("Debug console:");
239 debugConsole.addLine("");
240
[b29ff6b]241 createGui(font);
[3e44a59]242
[49da01a]243 goToLoginScreen();
[d352805]244
245 event_queue = al_create_event_queue();
246 if(!event_queue) {
247 fprintf(stderr, "failed to create event_queue!\n");
248 al_destroy_display(display);
249 al_destroy_timer(timer);
250 return -1;
251 }
252
253 al_set_target_bitmap(al_get_backbuffer(display));
254
255 al_register_event_source(event_queue, al_get_display_event_source(display));
256 al_register_event_source(event_queue, al_get_timer_event_source(timer));
257 al_register_event_source(event_queue, al_get_keyboard_event_source());
[87b3ee2]258 al_register_event_source(event_queue, al_get_mouse_event_source());
[d352805]259
260 al_clear_to_color(al_map_rgb(0,0,0));
261
262 al_flip_display();
[9a3e6b1]263
264 if (argc != 3) {
265 cout << "Usage: server port" << endl;
266 exit(1);
267 }
268
269 initWinSock();
[803566d]270
[9a3e6b1]271 sock = socket(AF_INET, SOCK_DGRAM, 0);
272 if (sock < 0)
273 error("socket");
274
[e607c0f]275 set_nonblock(sock);
276
[9a3e6b1]277 server.sin_family = AF_INET;
278 hp = gethostbyname(argv[1]);
[9c18cb7]279 if (hp == 0)
[9a3e6b1]280 error("Unknown host");
281
282 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
283 server.sin_port = htons(atoi(argv[2]));
284
[68d94de]285 msgProcessor = MessageProcessor(sock, &outputLog);
286
[d352805]287 al_start_timer(timer);
[e607c0f]288
[883bb5d]289 while (!doexit)
[d352805]290 {
291 ALLEGRO_EVENT ev;
[0b6f9ec]292
[d352805]293 al_wait_for_event(event_queue, &ev);
[87b3ee2]294
295 if(wndCurrent->handleEvent(ev)) {
296 // do nothing
297 }
298 else if(ev.type == ALLEGRO_EVENT_TIMER) {
[883bb5d]299 redraw = true;
300
301 // remove any other timer events in the queue
302 while (al_peek_next_event(event_queue, &ev) && ev.type == ALLEGRO_EVENT_TIMER) {
303 al_get_next_event(event_queue, &ev);
304 }
[d352805]305 }
306 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
[9a3e6b1]307 doexit = true;
[d352805]308 }
309 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
310 }
311 else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
312 switch(ev.keyboard.keycode) {
313 case ALLEGRO_KEY_ESCAPE:
314 doexit = true;
315 break;
[4926168]316 case ALLEGRO_KEY_S: // pickup an item next to you
[e0fd377]317 if (state == STATE_GAME) {
[4926168]318 msgTo.type = MSG_TYPE_PICKUP_FLAG;
319 memcpy(msgTo.buffer, &curPlayerId, 4);
[68d94de]320 msgProcessor.sendMessage(&msgTo, &server);
[4926168]321 }
322 break;
[626e5b0]323 case ALLEGRO_KEY_D: // drop the current item
[e0fd377]324 if (state == STATE_GAME) {
[6c9bcdd]325 try {
[5c7f28d]326 Player* p = mapPlayers.at(curPlayerId);
[f66d04f]327 int flagType = OBJECT_NONE;
[626e5b0]328
329 if (p->hasBlueFlag)
[f66d04f]330 flagType = OBJECT_BLUE_FLAG;
[626e5b0]331 else if (p->hasRedFlag)
[f66d04f]332 flagType = OBJECT_RED_FLAG;
[626e5b0]333
[f66d04f]334 if (flagType != OBJECT_NONE) {
[626e5b0]335 msgTo.type = MSG_TYPE_DROP_FLAG;
336 memcpy(msgTo.buffer, &curPlayerId, 4);
[68d94de]337 msgProcessor.sendMessage(&msgTo, &server);
[626e5b0]338 }
[5c7f28d]339 } catch (const out_of_range& ex) {}
[626e5b0]340 }
341 break;
[d352805]342 }
343 }
[88cdae2]344 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
[a0ce8a3]345 if (wndCurrent == wndGame) {
[e1f78f5]346 if (ev.mouse.button == 1) { // left click
347 msgTo.type = MSG_TYPE_PLAYER_MOVE;
[88cdae2]348
[e1f78f5]349 POSITION pos;
350 pos.x = ev.mouse.x;
351 pos.y = ev.mouse.y;
352 pos = screenToMap(pos);
[62ee2ce]353
[e1f78f5]354 if (pos.x != -1)
355 {
356 memcpy(msgTo.buffer, &curPlayerId, 4);
357 memcpy(msgTo.buffer+4, &pos.x, 4);
358 memcpy(msgTo.buffer+8, &pos.y, 4);
359
[68d94de]360 msgProcessor.sendMessage(&msgTo, &server);
[e1f78f5]361 }
362 else
363 cout << "Invalid point: User did not click on the map" << endl;
364 }else if (ev.mouse.button == 2) { // right click
[b8abc90]365 cout << "Detected a right-click" << endl;
366 map<unsigned int, Player*>::iterator it;
[e1f78f5]367
[cbc70eb]368 Player* curPlayer = mapPlayers[curPlayerId];;
[fbcfc35]369
[b8abc90]370 cout << "Got current player" << endl;
371 cout << "current game: " << game << endl;
[e1f78f5]372
[b8abc90]373 map<unsigned int, Player*> playersInGame = game->getPlayers();
374 Player* target;
375
[a0ce8a3]376 for (it = playersInGame.begin(); it != playersInGame.end(); it++)
[b8abc90]377 {
378 target = it->second;
379 cout << "set target" << endl;
[e70b66b]380 if (target->team != curPlayer->team)
[e1f78f5]381 {
[b8abc90]382 cout << "Found valid target" << endl;
[88cdae2]383
[e70b66b]384 POSITION cursorPos;
385 cursorPos.x = ev.mouse.x;
386 cursorPos.y = ev.mouse.y;
387 cursorPos = screenToMap(cursorPos);
[5b92307]388
[e70b66b]389 float distance =posDistance(cursorPos.toFloat(), target->pos);
[b8abc90]390
[e70b66b]391 if (distance < 25) {
392 unsigned int targetId = target->getId();
393
394 msgTo.type = MSG_TYPE_ATTACK;
395 memcpy(msgTo.buffer, &curPlayerId, 4);
396 memcpy(msgTo.buffer+4, &targetId, 4);
397
398 msgProcessor.sendMessage(&msgTo, &server);
399 }
[e1f78f5]400 }
[b8abc90]401 }
[62ee2ce]402 }
[ad5d122]403 }
[88cdae2]404 }
[e607c0f]405
[68d94de]406 if (msgProcessor.receiveMessage(&msgFrom, &from) >= 0)
[35d702d]407 processMessage(msgFrom, state, chatConsole, mapPlayers, mapGames, curPlayerId, alertMessage);
[054b50b]408
[a1a3bd5]409 if (redraw)
[e607c0f]410 {
[d352805]411 redraw = false;
[88cdae2]412
[68d94de]413 msgProcessor.resendUnackedMessages();
[10f6fc2]414
[f63aa57]415 if (debugging && wndCurrent == wndLobby)
416 wndLobbyDebug->draw(display);
[b35b2b2]417 else
418 wndCurrent->draw(display);
[9a3e6b1]419
[50e6c7a]420 if (wndCurrent == wndLobby) {
[f63aa57]421 if (!debugging)
422 chatConsole.draw(font, al_map_rgb(255,255,255));
[53d41ea]423
[11ad6fb]424 al_draw_text(font, al_map_rgb(0, 255, 0), SCREEN_W*1/2-100, 120, ALLEGRO_ALIGN_LEFT, "Current Games");
425
[321fbbc]426 map<string, int>::iterator it;
[2ee386d]427 int i=0;
[11ad6fb]428 ostringstream oss;
[2ee386d]429 for (it = mapGames.begin(); it != mapGames.end(); it++) {
[11ad6fb]430 oss << it->first << " (" << it->second << " players)" << endl;
431 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());
432 oss.clear();
433 oss.str("");
434 i++;
435 }
436
437 al_draw_text(font, al_map_rgb(0, 255, 0), SCREEN_W*3/4-100, 120, ALLEGRO_ALIGN_LEFT, "Online Players");
438
439 map<unsigned int, Player*>::iterator itPlayers;
440 i=0;
441 for (itPlayers = mapPlayers.begin(); itPlayers != mapPlayers.end(); itPlayers++) {
442 oss << itPlayers->second->name << endl;
443 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());
444 oss.clear();
445 oss.str("");
[2ee386d]446 i++;
[50e6c7a]447 }
[35d702d]448
449 al_draw_text(font, al_map_rgb(0, 255, 0), SCREEN_W/2, 15, ALLEGRO_ALIGN_CENTER, alertMessage.c_str());
[50e6c7a]450 }
[fd9cdb5]451 else if (wndCurrent == wndProfile)
452 {
[b28e2bf]453 ostringstream oss;
454 oss << "Honor Points: " << honorPoints << endl;
455 al_draw_text(font, al_map_rgb(0, 255, 0), 65, 90, ALLEGRO_ALIGN_LEFT, oss.str().c_str());
456 oss.clear();
457 oss.str("");
[fd9cdb5]458
[4c00935]459 oss << "Wins: " << wins << endl;
460 al_draw_text(font, al_map_rgb(0, 255, 0), 65, 105, ALLEGRO_ALIGN_LEFT, oss.str().c_str());
461 oss.clear();
462 oss.str("");
463
[81c4e8a]464 oss << "Losses: " << losses << endl;
[4c00935]465 al_draw_text(font, al_map_rgb(0, 255, 0), 65, 120, ALLEGRO_ALIGN_LEFT, oss.str().c_str());
466 oss.clear();
467 oss.str("");
468
[fd9cdb5]469 // display records of the last 10 games
[b28e2bf]470 for (int i=0; i<numGames; i++) {
471
472 if (gameHistory[i][0] == 0)
473 oss << "DEFEAT" << endl;
474 else if (gameHistory[i][0] == 1)
475 oss << "VICTORY" << endl;
476
[4c00935]477 al_draw_text(font, al_map_rgb(0, 255, 0), 142, 190+30*(i+1), ALLEGRO_ALIGN_CENTRE, oss.str().c_str());
[b28e2bf]478 oss.clear();
479 oss.str("");
480
481 oss << gameHistory[i][2] << endl;
[4c00935]482 al_draw_text(font, al_map_rgb(0, 255, 0), 302, 190+30*(i+1), ALLEGRO_ALIGN_CENTRE, oss.str().c_str());
[b28e2bf]483 oss.clear();
484 oss.str("");
485
486 oss << gameHistory[i][3] << endl;
[4c00935]487 al_draw_text(font, al_map_rgb(0, 255, 0), 462, 190+30*(i+1), ALLEGRO_ALIGN_CENTRE, oss.str().c_str());
[b28e2bf]488 oss.clear();
489 oss.str("");
490
[ace001a]491 time_t time_finished = gameHistory[i][4];
492 struct tm* now = localtime(&time_finished);
493
494 oss << (now->tm_mon + 1) << "/" << now->tm_mday << "/" << (now->tm_year + 1900) << " ";;
495
496 if (now->tm_hour == 0)
497 oss << "12";
498 else if (now->tm_hour <= 12)
499 oss << now->tm_hour;
500 else
501 oss << now->tm_hour-12;
502
503 oss << ":" << setfill('0') << setw(2) << now->tm_min << setfill(' ') << " ";
504
505 if (now->tm_hour < 12)
506 oss << "AM";
507 else
508 oss << "PM";
509
510 oss << endl;
511
[4c00935]512 al_draw_text(font, al_map_rgb(0, 255, 0), 622, 190+30*(i+1), ALLEGRO_ALIGN_CENTRE, oss.str().c_str());
[b28e2bf]513 oss.clear();
514 oss.str("");
515
[fd9cdb5]516 }
517
518 }
[a0ce8a3]519 else if (wndCurrent == wndGameLobby)
520 {
521 al_draw_text(font, al_map_rgb(0, 255, 0), 200, 100, ALLEGRO_ALIGN_LEFT, "Waiting Area");
522 al_draw_text(font, al_map_rgb(0, 255, 0), 400, 100, ALLEGRO_ALIGN_LEFT, "Blue Team");
523 al_draw_text(font, al_map_rgb(0, 255, 0), 600, 100, ALLEGRO_ALIGN_LEFT, "Red Team");
524
525 int drawPosition = 0;
526
527 map<unsigned int, Player*> gamePlayers = game->getPlayers();
528 map<unsigned int, Player*>::iterator itPlayers;
529 ostringstream oss;
530 int i=0;
531 for (itPlayers = gamePlayers.begin(); itPlayers != gamePlayers.end(); itPlayers++) {
[3476207]532 switch (itPlayers->second->team) {
[7fa452f]533 case 0:
[3476207]534 drawPosition = 200;
535 break;
[7fa452f]536 case 1:
[3476207]537 drawPosition = 400;
538 break;
[7fa452f]539 case 2:
[3476207]540 drawPosition = 600;
541 break;
542 }
543
[a0ce8a3]544 oss << itPlayers->second->name << endl;
545 al_draw_text(font, al_map_rgb(0, 255, 0), drawPosition, 135+i*15, ALLEGRO_ALIGN_LEFT, oss.str().c_str());
546 oss.clear();
547 oss.str("");
548 i++;
549 }
550 }
[3ff2bd7]551 else if (wndCurrent == wndGame)
[03ba5e3]552 {
[b4c5b6a]553 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 4, ALLEGRO_ALIGN_LEFT, "Players");
554
555 map<unsigned int, Player*>& gamePlayers = game->getPlayers();
556 map<unsigned int, Player*>::iterator it;
557
[3ff2bd7]558 if (!debugging) {
559 int playerCount = 0;
560 for (it = gamePlayers.begin(); it != gamePlayers.end(); it++)
561 {
562 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 19+(playerCount+1)*15, ALLEGRO_ALIGN_LEFT, it->second->name.c_str());
563 playerCount++;
564 }
[b4c5b6a]565 }
566
[03ba5e3]567 ostringstream ossScoreBlue, ossScoreRed;
568
569 ossScoreBlue << "Blue: " << game->getBlueScore() << endl;
570 ossScoreRed << "Red: " << game->getRedScore() << endl;
571
572 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
573 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
[0693e25]574
[fef7c69]575 // update players
576 for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
577 {
578 it->second->updateTarget(game->getPlayers());
579 }
580
581 for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
582 {
583 it->second->move(game->getMap()); // ignore return value
584 }
585
[58ca135]586 // update projectile positions
587 map<unsigned int, Projectile>::iterator it2;
588 for (it2 = game->getProjectiles().begin(); it2 != game->getProjectiles().end(); it2++)
589 {
590 it2->second.move(game->getPlayers());
591 }
592
[6319311]593 GameRender::drawMap(game->getMap());
594 GameRender::drawPlayers(game->getPlayers(), font, curPlayerId);
[e5697b1]595 GameRender::drawProjectiles(game->getProjectiles(), game->getPlayers());
[03ba5e3]596 }
[f63aa57]597 else if (wndCurrent == wndGameSummary)
[50e6c7a]598 {
[635ad9b]599 ostringstream ossBlueScore, ossRedScore;
600
601 ossBlueScore << "Blue Score: " << gameSummary->getBlueScore();
602 ossRedScore << "Red Score: " << gameSummary->getRedScore();
603
[3e44a59]604 string strWinner;
605
606 if (gameSummary->getWinner() == 0)
[635ad9b]607 strWinner = "Blue Team Wins";
[3e44a59]608 else if (gameSummary->getWinner() == 1)
[635ad9b]609 strWinner = "Red Team Wins";
610 else
611 strWinner = "winner set to wrong value";
612
[3e44a59]613 al_draw_text(font, al_map_rgb(0, 255, 0), 512, 40, ALLEGRO_ALIGN_CENTRE, gameSummary->getName().c_str());
[635ad9b]614 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossBlueScore.str().c_str());
615 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossRedScore.str().c_str());
[3e44a59]616 al_draw_text(font, al_map_rgb(0, 255, 0), 512, 120, ALLEGRO_ALIGN_CENTRE, strWinner.c_str());
[87b3ee2]617 }
618
[b35b2b2]619 if (debugging) {
620 drawMessageStatus(font);
621 }
622
[d352805]623 al_flip_display();
624 }
625 }
[9a3e6b1]626
627 #if defined WINDOWS
628 closesocket(sock);
629 #elif defined LINUX
630 close(sock);
631 #endif
632
633 shutdownWinSock();
[d352805]634
[f63aa57]635 // delete all components
636 for (unsigned int x=0; x<vctComponents.size(); x++)
637 delete vctComponents[x];
638
[87b3ee2]639 delete wndLogin;
[1785314]640 delete wndRegister;
641 delete wndLobby;
[f63aa57]642 delete wndLobbyDebug;
[a0ce8a3]643 delete wndGameLobby;
[3ff2bd7]644 delete wndGame;
[f63aa57]645 delete wndGameSummary;
[87b3ee2]646
[eb2ad4f]647 // game should be deleted when the player leaves a gamw
[d519032]648 if (game != NULL)
649 delete game;
[62ee2ce]650
[3e44a59]651 if (gameSummary != NULL)
652 delete gameSummary;
653
[e6c26b8]654 map<unsigned int, Player*>::iterator it;
655
656 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
657 delete it->second;
658 }
659
[b28e2bf]660 if (gameHistory != NULL) {
661 for (int i=0; i<numGames; i++) {
662 free(gameHistory[i]);
663 }
664
665 free(gameHistory);
666 }
667
[d352805]668 al_destroy_event_queue(event_queue);
669 al_destroy_display(display);
670 al_destroy_timer(timer);
[8271c78]671
672 outputLog << "Stopped client on " << getCurrentDateTimeString() << endl;
673 outputLog.close();
674
[d352805]675 return 0;
676}
677
[0dde5da]678void initWinSock()
679{
680#if defined WINDOWS
681 WORD wVersionRequested;
682 WSADATA wsaData;
683 int wsaerr;
684
685 wVersionRequested = MAKEWORD(2, 2);
686 wsaerr = WSAStartup(wVersionRequested, &wsaData);
[803566d]687
[0dde5da]688 if (wsaerr != 0) {
689 cout << "The Winsock dll not found." << endl;
690 exit(1);
691 }else
692 cout << "The Winsock dll was found." << endl;
693#endif
694}
695
696void shutdownWinSock()
697{
698#if defined WINDOWS
699 WSACleanup();
700#endif
[1912323]701}
702
[b29ff6b]703void createGui(ALLEGRO_FONT* font) {
[fd9cdb5]704
[b29ff6b]705 // wndLogin
706
707 wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
708 vctComponents.push_back(wndLogin->addComponent(new Textbox(516, 40, 100, 20, font)));
709 vctComponents.push_back(wndLogin->addComponent(new Textbox(516, 70, 100, 20, font)));
710 vctComponents.push_back(wndLogin->addComponent(new TextLabel(410, 40, 100, 20, font, "Username:", ALLEGRO_ALIGN_RIGHT)));
711 vctComponents.push_back(wndLogin->addComponent(new TextLabel(410, 70, 100, 20, font, "Password:", ALLEGRO_ALIGN_RIGHT)));
712 vctComponents.push_back(wndLogin->addComponent(new TextLabel((SCREEN_W-600)/2, 100, 600, 20, font, "", ALLEGRO_ALIGN_CENTRE)));
713 vctComponents.push_back(wndLogin->addComponent(new Button(SCREEN_W/2-100, 130, 90, 20, font, "Register", goToRegisterScreen)));
714 vctComponents.push_back(wndLogin->addComponent(new Button(SCREEN_W/2+10, 130, 90, 20, font, "Login", login)));
715 vctComponents.push_back(wndLogin->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit)));
716 vctComponents.push_back(wndLogin->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
717
718 txtUsername = (Textbox*)wndLogin->getComponent(0);
719 txtPassword = (Textbox*)wndLogin->getComponent(1);
720 lblLoginStatus = (TextLabel*)wndLogin->getComponent(4);
721
722 cout << "Created login screen" << endl;
723
724
725 // wndRegister
726
727 wndRegister = new Window(0, 0, SCREEN_W, SCREEN_H);
728 vctComponents.push_back(wndRegister->addComponent(new Textbox(516, 40, 100, 20, font)));
729 vctComponents.push_back(wndRegister->addComponent(new Textbox(516, 70, 100, 20, font)));
730 vctComponents.push_back(wndRegister->addComponent(new TextLabel(410, 40, 100, 20, font, "Username:", ALLEGRO_ALIGN_RIGHT)));
731 vctComponents.push_back(wndRegister->addComponent(new TextLabel(410, 70, 100, 20, font, "Password:", ALLEGRO_ALIGN_RIGHT)));
732 vctComponents.push_back(wndRegister->addComponent(new RadioButtonList(432, 100, "Pick a class", font)));
733 vctComponents.push_back(wndRegister->addComponent(new TextLabel((SCREEN_W-600)/2, 190, 600, 20, font, "", ALLEGRO_ALIGN_CENTRE)));
734 vctComponents.push_back(wndRegister->addComponent(new Button(SCREEN_W/2-100, 220, 90, 20, font, "Back", goToLoginScreen)));
735 vctComponents.push_back(wndRegister->addComponent(new Button(SCREEN_W/2+10, 220, 90, 20, font, "Submit", registerAccount)));
736 vctComponents.push_back(wndRegister->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit)));
737 vctComponents.push_back(wndRegister->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
738
739 txtUsernameRegister = (Textbox*)wndRegister->getComponent(0);
740 txtPasswordRegister = (Textbox*)wndRegister->getComponent(1);
741
742 rblClasses = (RadioButtonList*)wndRegister->getComponent(4);
743 rblClasses->addRadioButton("Warrior");
744 rblClasses->addRadioButton("Ranger");
745
746 lblRegisterStatus = (TextLabel*)wndRegister->getComponent(5);
747
748 cout << "Created register screen" << endl;
749
750
751 // wndLobby
752
753 txtJoinGame = new Textbox(SCREEN_W*1/2+15+4, 40, 100, 20, font);
754 vctComponents.push_back(txtJoinGame);
755
756 txtCreateGame = new Textbox(SCREEN_W*3/4+4, 40, 100, 20, font);
757 vctComponents.push_back(txtCreateGame);
758
759 wndLobby = new Window(0, 0, SCREEN_W, SCREEN_H);
[fd9cdb5]760 vctComponents.push_back(wndLobby->addComponent(new Button(920, 10, 80, 20, font, "Profile", goToProfileScreen)));
761 vctComponents.push_back(wndLobby->addComponent(new Button(920, 738, 80, 20, font, "Logout", logout)));
[b29ff6b]762 vctComponents.push_back(wndLobby->addComponent(new TextLabel(SCREEN_W*1/2+15-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
763 wndLobby->addComponent(txtJoinGame);
764 vctComponents.push_back(wndLobby->addComponent(new Button(SCREEN_W*1/2+15-100, 80, 200, 20, font, "Join Existing Game", joinGame)));
765 vctComponents.push_back(wndLobby->addComponent(new TextLabel(SCREEN_W*3/4-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
766 wndLobby->addComponent(txtCreateGame);
767 vctComponents.push_back(wndLobby->addComponent(new Button(SCREEN_W*3/4-100, 80, 200, 20, font, "Create New Game", createGame)));
768 vctComponents.push_back(wndLobby->addComponent(new Textbox(95, 40, 300, 20, font)));
769 vctComponents.push_back(wndLobby->addComponent(new Button(95, 70, 60, 20, font, "Send", sendChatMessage)));
770 vctComponents.push_back(wndLobby->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
771
772 txtChat = (Textbox*)wndLobby->getComponent(7);
773
774 cout << "Created lobby screen" << endl;
775
776
777 // wndLobbyDebug
778
779 wndLobbyDebug = new Window(0, 0, SCREEN_W, SCREEN_H);
[fd9cdb5]780 vctComponents.push_back(wndLobbyDebug->addComponent(new Button(920, 10, 80, 20, font, "Profile", goToProfileScreen)));
781 vctComponents.push_back(wndLobbyDebug->addComponent(new Button(920, 738, 80, 20, font, "Logout", logout)));
[b29ff6b]782 vctComponents.push_back(wndLobbyDebug->addComponent(new TextLabel(SCREEN_W*1/2+15-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
783 wndLobbyDebug->addComponent(txtJoinGame);
784 vctComponents.push_back(wndLobbyDebug->addComponent(new Button(SCREEN_W*1/2+15-100, 80, 200, 20, font, "Join Existing Game", joinGame)));
785 vctComponents.push_back(wndLobbyDebug->addComponent(new TextLabel(SCREEN_W*3/4-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT)));
786 wndLobbyDebug->addComponent(txtCreateGame);
787 vctComponents.push_back(wndLobbyDebug->addComponent(new Button(SCREEN_W*3/4-100, 80, 200, 20, font, "Create New Game", createGame)));
788 vctComponents.push_back(wndLobbyDebug->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging)));
789
790 cout << "Created debug lobby screen" << endl;
791
792
[fd9cdb5]793 // wndProfile
794
795 wndProfile = new Window(0, 0, SCREEN_W, SCREEN_H);
796 vctComponents.push_back(wndProfile->addComponent(new TextLabel(450, 40, 124, 20, font, "Profile", ALLEGRO_ALIGN_CENTRE)));
[4c00935]797 vctComponents.push_back(wndProfile->addComponent(new TextLabel(160, 150, 124, 20, font, "Game History", ALLEGRO_ALIGN_CENTRE)));
798 vctComponents.push_back(wndProfile->addComponent(new TextLabel(600, 190, 124, 20, font, "Time", ALLEGRO_ALIGN_CENTRE)));
799 vctComponents.push_back(wndProfile->addComponent(new TextLabel(80, 190, 124, 20, font, "Result", ALLEGRO_ALIGN_CENTRE)));
800 vctComponents.push_back(wndProfile->addComponent(new TextLabel(240, 190, 124, 20, font, "Blue Score", ALLEGRO_ALIGN_CENTRE)));
801 vctComponents.push_back(wndProfile->addComponent(new TextLabel(400, 190, 124, 20, font, "Red Score", ALLEGRO_ALIGN_CENTRE)));
802 vctComponents.push_back(wndProfile->addComponent(new TextLabel(560, 190, 124, 20, font, "Time", ALLEGRO_ALIGN_CENTRE)));
[fd9cdb5]803 vctComponents.push_back(wndProfile->addComponent(new Button(920, 738, 80, 20, font, "Back", goToLobbyScreen)));
804
805
[a0ce8a3]806 // wndGameLobby
807
808 wndGameLobby = new Window(0, 0, SCREEN_W, SCREEN_H);
809 vctComponents.push_back(wndGameLobby->addComponent(new Button(180, 120, 160, 300, font, "", joinWaitingArea)));
810 vctComponents.push_back(wndGameLobby->addComponent(new Button(380, 120, 160, 300, font, "", joinBlueTeam)));
811 vctComponents.push_back(wndGameLobby->addComponent(new Button(580, 120, 160, 300, font, "", joinRedTeam)));
812 vctComponents.push_back(wndGameLobby->addComponent(new Button(40, 600, 120, 20, font, "Leave Game", leaveGame)));
813 vctComponents.push_back(wndGameLobby->addComponent(new Button(800, 600, 120, 20, font, "Start Game", startGame)));
814
815
[b29ff6b]816 // wndGame
817
818 wndGame = new Window(0, 0, SCREEN_W, SCREEN_H);
819 vctComponents.push_back(wndGame->addComponent(new Button(880, 10, 120, 20, font, "Leave Game", leaveGame)));
820
821 cout << "Created new game screen" << endl;
822
823 wndGameSummary = new Window(0, 0, SCREEN_W, SCREEN_H);
824 vctComponents.push_back(wndGameSummary->addComponent(new Button(840, 730, 160, 20, font, "Back to Lobby", closeGameSummary)));
825
826 cout << "Created game summary screen" << endl;
827}
828
[35d702d]829void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames, unsigned int& curPlayerId, string& alertMessage)
[1912323]830{
[dfb9363]831 cout << "Total players in map: " << mapPlayers.size() << endl;
832
[4da5aa3]833 switch(state)
834 {
835 case STATE_START:
836 {
[e607c0f]837 cout << "In STATE_START" << endl;
838
[87b3ee2]839 switch(msg.type)
[4da5aa3]840 {
[87b3ee2]841 case MSG_TYPE_REGISTER:
842 {
[45c9d0f]843 lblRegisterStatus->setText(msg.buffer);
[87b3ee2]844 break;
845 }
[bc70282]846 default:
847 {
848 cout << "(STATE_REGISTER) Received invalid message of type " << msg.type << endl;
849 break;
850 }
851 }
852
853 break;
854 }
[1785314]855 case STATE_LOBBY:
[bc70282]856 {
[e0fd377]857 cout << "In STATE_LOBBY" << endl;
[bc70282]858 switch(msg.type)
859 {
[87b3ee2]860 case MSG_TYPE_LOGIN:
861 {
[45c9d0f]862 if (string(msg.buffer).compare("Player has already logged in.") == 0)
[87b3ee2]863 {
[bc70282]864 goToLoginScreen();
865 state = STATE_START;
866
[45c9d0f]867 lblLoginStatus->setText(msg.buffer);
[87b3ee2]868 }
[45c9d0f]869 else if (string(msg.buffer).compare("Incorrect username or password") == 0)
[87b3ee2]870 {
[bc70282]871 goToLoginScreen();
872 state = STATE_START;
873
[45c9d0f]874 lblLoginStatus->setText(msg.buffer);
[87b3ee2]875 }
876 else
877 {
[1785314]878 wndCurrent = wndLobby;
[95ffe57]879
880 // this message should only be sent when a player first logs in so they know their id
881
882 Player* p = new Player("", "");
883 p->deserialize(msg.buffer);
[e6c26b8]884
[5b92307]885 if (mapPlayers.find(p->getId()) != mapPlayers.end())
886 delete mapPlayers[p->getId()];
887 mapPlayers[p->getId()] = p;
888 curPlayerId = p->getId();
[a0ce8a3]889 currentPlayer = mapPlayers[curPlayerId];
[88cdae2]890
891 cout << "Got a valid login response with the player" << endl;
[1f1eb58]892 cout << "Player id: " << curPlayerId << endl;
[95ffe57]893 cout << "Player health: " << p->health << endl;
[bc70282]894 cout << "player map size: " << mapPlayers.size() << endl;
[87b3ee2]895 }
[88cdae2]896
[a1a3bd5]897 break;
898 }
899 case MSG_TYPE_LOGOUT:
900 {
[054b50b]901 cout << "Got a logout message" << endl;
[a1a3bd5]902
[1e250bf]903 unsigned int playerId;
[53ba300]904
905 // Check if it's about you or another player
906 memcpy(&playerId, msg.buffer, 4);
[45c9d0f]907 string response = string(msg.buffer+4);
[53ba300]908
909 if (playerId == curPlayerId)
[87b3ee2]910 {
[dfb9363]911 cout << "Got logout message for self" << endl;
912
[53ba300]913 if (response.compare("You have successfully logged out.") == 0)
914 {
915 cout << "Logged out" << endl;
916 state = STATE_START;
917 goToLoginScreen();
918 }
919
920 // if there was an error logging out, nothing happens
921 }
922 else
923 {
924 delete mapPlayers[playerId];
[dfb9363]925 mapPlayers.erase(playerId);
[87b3ee2]926 }
[054b50b]927
928 break;
929 }
[eb8adb1]930 case MSG_TYPE_CHAT:
931 {
[45c9d0f]932 chatConsole.addLine(msg.buffer);
[4c202e0]933
[87b3ee2]934 break;
935 }
[fd9cdb5]936 case MSG_TYPE_PROFILE:
937 {
[b28e2bf]938 memcpy(&honorPoints, msg.buffer, 4);
[4c00935]939 memcpy(&wins, msg.buffer+4, 4);
940 memcpy(&losses, msg.buffer+8, 4);
941 memcpy(&numGames, msg.buffer+12, 4);
[b28e2bf]942
943 cout << "Got records for " << numGames << " games." << endl;
944 gameHistory = (int**)malloc(numGames*sizeof(int*));
945 for (int i=0; i<numGames; i++) {
[ace001a]946 gameHistory[i] = (int*)malloc(5*sizeof(int));
[b28e2bf]947 cout << endl << "game record " << (i+1) << endl;
948
[ace001a]949 memcpy(&gameHistory[i][0], msg.buffer+16+i*20, 4);
950 memcpy(&gameHistory[i][1], msg.buffer+20+i*20, 4);
951 memcpy(&gameHistory[i][2], msg.buffer+24+i*20, 4);
952 memcpy(&gameHistory[i][3], msg.buffer+28+i*20, 4);
953 memcpy(&gameHistory[i][4], msg.buffer+32+i*20, 4);
[b28e2bf]954
955 cout << "result: " << gameHistory[i][0] << endl;
956 cout << "team: " << gameHistory[i][1] << endl;
957 cout << "blue score: " << gameHistory[i][2] << endl;
958 cout << "red score: " << gameHistory[i][3] << endl;
[ace001a]959
960 time_t time_finished = gameHistory[i][4];
961 struct tm* now = localtime(&time_finished);
962
963 cout << "time game finished: ";
964 cout << (now->tm_year + 1900) << '-'
965 << (now->tm_mon + 1) << '-'
966 << now->tm_mday << " "
967 << now->tm_hour << ":"
968 << now->tm_min
969 << endl;
[b28e2bf]970 }
971
[fd9cdb5]972 wndCurrent = wndProfile;
973
974 break;
975 }
[d519032]976 case MSG_TYPE_JOIN_GAME_SUCCESS:
977 {
978 cout << "Received a JOIN_GAME_SUCCESS message" << endl;
979
[8aed9c0]980 string gameName(msg.buffer);
981
982 #if defined WINDOWS
[1f6233e]983 game = new Game(gameName, "../../data/map.txt", &msgProcessor);
[8aed9c0]984 #elif defined LINUX
[1f6233e]985 game = new Game(gameName, "../data/map.txt", &msgProcessor);
[34bd549]986 #elif defined MAC
987 game = new Game(gameName, "../data/map.txt", &msgProcessor);
[8aed9c0]988 #endif
989
[03ba5e3]990 cout << "Game name: " << gameName << endl;
[d519032]991
[a0ce8a3]992 state = STATE_GAME_LOBBY;
993 wndCurrent = wndGameLobby;
[d519032]994
995 msgTo.type = MSG_TYPE_JOIN_GAME_ACK;
996 strcpy(msgTo.buffer, gameName.c_str());
997
[68d94de]998 msgProcessor.sendMessage(&msgTo, &server);
[d519032]999
1000 break;
1001 }
1002 case MSG_TYPE_JOIN_GAME_FAILURE:
1003 {
1004 cout << "Received a JOIN_GAME_FAILURE message" << endl;
1005
1006 break;
1007 }
[35d702d]1008 case MSG_TYPE_CREATE_GAME_FAILURE:
1009 {
1010 cout << "Received a CREATE_GAME_FAILURE message" << endl;
1011 alertMessage = "Game could not be created because one exists with that name";
1012
1013 break;
1014 }
[6f64166]1015 case MSG_TYPE_PLAYER:
1016 {
1017 handleMsgPlayer(msg, mapPlayers, mapGames);
1018
1019 break;
1020 }
1021 case MSG_TYPE_GAME_INFO:
1022 {
1023 handleMsgGameInfo(msg, mapPlayers, mapGames);
1024
1025 break;
1026 }
[45b2750]1027 default:
1028 {
[1785314]1029 cout << "(STATE_LOBBY) Received invlaid message of type " << msg.type << endl;
[50e6c7a]1030
[365e156]1031 break;
[45b2750]1032 }
[4da5aa3]1033 }
[eb8adb1]1034
[4da5aa3]1035 break;
1036 }
[a0ce8a3]1037 case STATE_GAME_LOBBY:
[cf05729]1038 {
[a0ce8a3]1039 cout << "(STATE_GAME_LOBBY) ";
[cf05729]1040 switch(msg.type)
1041 {
1042 case MSG_TYPE_START_GAME:
1043 {
1044 state = STATE_GAME;
1045 wndCurrent = wndGame;
1046
1047 break;
1048 }
1049 default:
1050 {
1051 // keep these lines commented until until the correct messages are moved into the STATE_GAME_LOBBY section
1052 //cout << "Received invalid message of type " << msg.type << endl;
1053
1054 //break;
1055 }
1056 }
1057 }
[e0fd377]1058 case STATE_GAME:
[803566d]1059 {
[e0fd377]1060 cout << "(STATE_GAME) ";
[803566d]1061 switch(msg.type)
1062 {
[d6b5f74]1063 case MSG_TYPE_SCORE:
1064 {
1065 cout << "Received SCORE message!" << endl;
1066
1067 int blueScore;
1068 memcpy(&blueScore, msg.buffer, 4);
1069 cout << "blue score: " << blueScore << endl;
1070 game->setBlueScore(blueScore);
1071
1072 int redScore;
1073 memcpy(&redScore, msg.buffer+4, 4);
1074 cout << "red score: " << redScore << endl;
1075 game->setRedScore(redScore);
1076
1077 cout << "Processed SCORE message!" << endl;
1078
1079 break;
1080 }
[3e44a59]1081 case MSG_TYPE_FINISH_GAME:
1082 {
1083 cout << "Got a finish game message" << endl;
1084 cout << "Should switch to STATE_LOBBY and show the final score" << endl;
1085
1086 unsigned int winner, blueScore, redScore;
[635ad9b]1087 memcpy(&winner, msg.buffer, 4);
1088 memcpy(&blueScore, msg.buffer+4, 4);
1089 memcpy(&redScore, msg.buffer+8, 4);
1090
1091 string gameName(msg.buffer+12);
1092
[3e44a59]1093 gameSummary = new GameSummary(gameName, winner, blueScore, redScore);
1094
1095 delete game;
1096 game = NULL;
1097 state = STATE_LOBBY;
1098 wndCurrent = wndGameSummary;
[35d702d]1099 alertMessage = "";
[3e44a59]1100
[31b347a]1101 break;
[6012178]1102 }
[dfb9363]1103 case MSG_TYPE_LOGOUT:
[5c7f28d]1104 {
1105 cout << "Got a logout message" << endl;
1106
[8df0c49]1107 unsigned int playerId;
[5c7f28d]1108
1109 // Check if it's about you or another player
1110 memcpy(&playerId, msg.buffer, 4);
1111
1112 if (playerId == curPlayerId)
1113 cout << "Received MSG_TYPE_LOGOUT for self in STATE_GAME. This shouldn't happen." << endl;
[dfb9363]1114 else {
[5c7f28d]1115 delete mapPlayers[playerId];
[dfb9363]1116 mapPlayers.erase(playerId);
1117 }
[5c7f28d]1118
1119 break;
1120 }
[6012178]1121 case MSG_TYPE_PLAYER_JOIN_GAME:
1122 {
1123 cout << "Received MSG_TYPE_PLAYER_JOIN_GAME" << endl;
1124
1125 Player p("", "");
1126 p.deserialize(msg.buffer);
[5b92307]1127 cout << "Deserialized player" << endl;
[3476207]1128 cout << "player team: " << p.team << endl;
1129 cout << "current player team: " << currentPlayer->team << endl;
[6012178]1130 p.timeLastUpdated = getCurrentMillis();
1131 p.isChasing = false;
1132 if (p.health <= 0)
1133 p.isDead = true;
1134 else
1135 p.isDead = false;
1136
[5b92307]1137 if (mapPlayers.find(p.getId()) != mapPlayers.end())
1138 *(mapPlayers[p.getId()]) = p;
[6012178]1139 else
[5b92307]1140 mapPlayers[p.getId()] = new Player(p);
[b4c5b6a]1141
[306758e]1142 game->addPlayer(mapPlayers[p.getId()]);
[b4c5b6a]1143
1144 break;
1145 }
[cd80d63]1146 case MSG_TYPE_LEAVE_GAME:
1147 {
1148 cout << "Received a LEAVE_GAME message" << endl;
1149
1150 string gameName(msg.buffer+4);
1151 unsigned int playerId;
1152
1153 memcpy(&playerId, msg.buffer, 4);
1154
1155 game->removePlayer(playerId);
1156
1157 break;
1158 }
[fef7c69]1159 case MSG_TYPE_PLAYER_MOVE:
1160 {
1161 cout << "Received PLAYER_MOVE message" << endl;
1162
1163 unsigned int id;
1164 int x, y;
1165
1166 memcpy(&id, msg.buffer, 4);
1167 memcpy(&x, msg.buffer+4, 4);
1168 memcpy(&y, msg.buffer+8, 4);
1169
1170 cout << "id: " << id << endl;
1171
1172 mapPlayers[id]->target.x = x;
1173 mapPlayers[id]->target.y = y;
1174
[1ee0ffa]1175 mapPlayers[id]->isChasing = false;
1176 mapPlayers[id]->setTargetPlayer(0);
1177
[fef7c69]1178 break;
1179 }
[803566d]1180 case MSG_TYPE_OBJECT:
1181 {
[e0fd377]1182 cout << "Received object message in STATE_GAME" << endl;
[803566d]1183
[f66d04f]1184 WorldMap::Object o(0, OBJECT_NONE, 0, 0);
[803566d]1185 o.deserialize(msg.buffer);
1186 cout << "object id: " << o.id << endl;
1187 game->getMap()->updateObject(o.id, o.type, o.pos.x, o.pos.y);
1188
1189 break;
1190 }
1191 case MSG_TYPE_REMOVE_OBJECT:
1192 {
[d519032]1193 cout << "Received REMOVE_OBJECT message!" << endl;
[803566d]1194
1195 int id;
1196 memcpy(&id, msg.buffer, 4);
1197
1198 cout << "Removing object with id " << id << endl;
1199
1200 if (!game->getMap()->removeObject(id))
1201 cout << "Did not remove the object" << endl;
1202
1203 break;
1204 }
[b8abc90]1205 case MSG_TYPE_ATTACK:
1206 {
1207 cout << "Received START_ATTACK message" << endl;
1208
1209 unsigned int id, targetId;
1210 memcpy(&id, msg.buffer, 4);
1211 memcpy(&targetId, msg.buffer+4, 4);
1212
1213 cout << "source id: " << id << endl;
1214 cout << "target id: " << targetId << endl;
1215
1216 // need to check the target exists in the current game
1217 Player* source = game->getPlayers()[id];
[5b92307]1218 source->setTargetPlayer(targetId);
[b8abc90]1219 source->isChasing = true;
1220
1221 break;
1222 }
[58ca135]1223 case MSG_TYPE_PROJECTILE:
1224 {
1225 cout << "Received a PROJECTILE message" << endl;
1226
1227 unsigned int projId, x, y, targetId;
1228
1229 memcpy(&projId, msg.buffer, 4);
1230 memcpy(&x, msg.buffer+4, 4);
1231 memcpy(&y, msg.buffer+8, 4);
1232 memcpy(&targetId, msg.buffer+12, 4);
1233
1234 cout << "projId: " << projId << endl;
1235 cout << "x: " << x << endl;
1236 cout << "y: " << y << endl;
1237 cout << "Target: " << targetId << endl;
1238
1239 Projectile proj(x, y, targetId, 0);
1240 proj.setId(projId);
1241
1242 game->addProjectile(proj);
1243
1244 break;
1245 }
1246 case MSG_TYPE_REMOVE_PROJECTILE:
1247 {
1248 cout << "Received a REMOVE_PROJECTILE message" << endl;
1249
1250 unsigned int id;
1251 memcpy(&id, msg.buffer, 4);
1252
1253 game->removeProjectile(id);
1254
1255 break;
1256 }
[6f64166]1257 case MSG_TYPE_PLAYER:
1258 {
1259 handleMsgPlayer(msg, mapPlayers, mapGames);
1260
1261 break;
1262 }
1263 case MSG_TYPE_GAME_INFO:
1264 {
1265 handleMsgGameInfo(msg, mapPlayers, mapGames);
1266
1267 break;
1268 }
[803566d]1269 default:
1270 {
[d519032]1271 cout << "Received invalid message of type " << msg.type << endl;
[803566d]1272
1273 break;
1274 }
1275 }
1276
1277 break;
1278 }
[4da5aa3]1279 default:
1280 {
1281 cout << "The state has an invalid value: " << state << endl;
1282
1283 break;
1284 }
1285 }
[0dde5da]1286}
[87b3ee2]1287
[929b4e0]1288int getRefreshRate(int width, int height)
1289{
1290 int numRefreshRates = al_get_num_display_modes();
1291 ALLEGRO_DISPLAY_MODE displayMode;
1292
1293 for(int i=0; i<numRefreshRates; i++) {
1294 al_get_display_mode(i, &displayMode);
1295
1296 if (displayMode.width == width && displayMode.height == height)
1297 return displayMode.refresh_rate;
1298 }
1299
1300 return 0;
1301}
1302
1303void drawMessageStatus(ALLEGRO_FONT* font)
1304{
1305 int clientMsgOffset = 5;
1306 int serverMsgOffset = 950;
1307
1308 al_draw_text(font, al_map_rgb(0, 255, 255), 0+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
1309 al_draw_text(font, al_map_rgb(0, 255, 255), 20+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Type");
1310 al_draw_text(font, al_map_rgb(0, 255, 255), 240+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Acked?");
1311
[3ff2bd7]1312 //al_draw_text(font, al_map_rgb(0, 255, 255), serverMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
[929b4e0]1313
1314 map<unsigned int, map<unsigned long, MessageContainer> >& sentMessages = msgProcessor.getSentMessages();
1315 int id, type;
1316 bool acked;
1317 ostringstream ossId, ossAcked;
1318
1319 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
1320
1321 int msgCount = 0;
1322 for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
1323 map<unsigned long, MessageContainer> playerMessage = it->second;
1324 map<unsigned long, MessageContainer>::iterator it2;
1325 for (it2 = playerMessage.begin(); it2 != playerMessage.end(); it2++) {
1326
1327 id = it->first;
1328 ossId.str("");;
1329 ossId << id;
1330
1331 type = it2->second.getMessage()->type;
1332 string typeStr = MessageContainer::getMsgTypeString(type);
1333
1334 acked = it2->second.getAcked();
1335 ossAcked.str("");;
1336 ossAcked << boolalpha << acked;
1337
1338 al_draw_text(font, al_map_rgb(0, 255, 0), clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
1339 al_draw_text(font, al_map_rgb(0, 255, 0), 20+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, typeStr.c_str());
1340 al_draw_text(font, al_map_rgb(0, 255, 0), 240+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossAcked.str().c_str());
1341
1342 msgCount++;
1343 }
1344 }
1345
1346 if (msgProcessor.getAckedMessages().size() > 0) {
1347 map<unsigned int, unsigned long long> ackedMessages = msgProcessor.getAckedMessages()[0];
1348 map<unsigned int, unsigned long long>::iterator it3;
1349
1350 msgCount = 0;
1351 for (it3 = ackedMessages.begin(); it3 != ackedMessages.end(); it3++) {
1352 ossId.str("");;
1353 ossId << it3->first;
1354
1355 al_draw_text(font, al_map_rgb(255, 0, 0), 25+serverMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
1356
1357 msgCount++;
1358 }
1359 }
1360}
1361
[6f64166]1362// message handling functions
1363
1364void handleMsgPlayer(NETWORK_MSG &msg, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames) {
1365 cout << "Received MSG_TYPE_PLAYER" << endl;
1366
1367 Player p("", "");
1368 p.deserialize(msg.buffer);
1369 p.timeLastUpdated = getCurrentMillis();
1370 p.isChasing = false;
1371 if (p.health <= 0)
1372 p.isDead = true;
1373 else
1374 p.isDead = false;
1375
1376 if (mapPlayers.find(p.getId()) != mapPlayers.end())
1377 *(mapPlayers[p.getId()]) = p;
1378 else
1379 mapPlayers[p.getId()] = new Player(p);
1380}
1381
1382void handleMsgGameInfo(NETWORK_MSG &msg, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames) {
1383 cout << "Received a GAME_INFO message" << endl;
1384
1385 string gameName(msg.buffer+4);
1386 int numPlayers;
1387
1388 memcpy(&numPlayers, msg.buffer, 4);
1389
1390 cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;
1391
1392 if (numPlayers > 0)
1393 mapGames[gameName] = numPlayers;
1394 else
1395 mapGames.erase(gameName);
1396}
1397
[929b4e0]1398// Callback definitions
1399
[5c95436]1400void goToRegisterScreen()
1401{
1402 txtUsernameRegister->clear();
1403 txtPasswordRegister->clear();
[49da01a]1404 lblRegisterStatus->setText("");
1405 rblClasses->setSelectedButton(-1);
1406
1407 wndCurrent = wndRegister;
[5c95436]1408}
1409
1410void goToLoginScreen()
[87b3ee2]1411{
1412 txtUsername->clear();
1413 txtPassword->clear();
[49da01a]1414 lblLoginStatus->setText("");
1415
1416 wndCurrent = wndLogin;
[5c95436]1417}
1418
[bc70282]1419// maybe need a goToGameScreen function as well and add state changes to these functions as well
1420
[5c95436]1421void registerAccount()
1422{
1423 string username = txtUsernameRegister->getStr();
1424 string password = txtPasswordRegister->getStr();
1425
1426 txtUsernameRegister->clear();
1427 txtPasswordRegister->clear();
1428 // maybe clear rblClasses as well (add a method to RadioButtonList to enable this)
1429
1430 Player::PlayerClass playerClass;
1431
1432 switch (rblClasses->getSelectedButton()) {
1433 case 0:
1434 playerClass = Player::CLASS_WARRIOR;
1435 break;
1436 case 1:
1437 playerClass = Player::CLASS_RANGER;
1438 break;
1439 default:
1440 cout << "Invalid class selection" << endl;
1441 playerClass = Player::CLASS_NONE;
1442 break;
1443 }
[87b3ee2]1444
1445 msgTo.type = MSG_TYPE_REGISTER;
1446
1447 strcpy(msgTo.buffer, username.c_str());
1448 strcpy(msgTo.buffer+username.size()+1, password.c_str());
[5c95436]1449 memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
[87b3ee2]1450
[68d94de]1451 msgProcessor.sendMessage(&msgTo, &server);
[87b3ee2]1452}
1453
1454void login()
1455{
1456 string strUsername = txtUsername->getStr();
1457 string strPassword = txtPassword->getStr();
1458 username = strUsername;
1459
1460 txtUsername->clear();
1461 txtPassword->clear();
1462
1463 msgTo.type = MSG_TYPE_LOGIN;
1464
1465 strcpy(msgTo.buffer, strUsername.c_str());
1466 strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
1467
[68d94de]1468 msgProcessor.sendMessage(&msgTo, &server);
[bc70282]1469
[1785314]1470 state = STATE_LOBBY;
[35d702d]1471 alertMessage = "";
[87b3ee2]1472}
1473
1474void logout()
1475{
[929b4e0]1476 switch(state) {
1477 case STATE_LOBBY:
1478 txtJoinGame->clear();
1479 txtCreateGame->clear();
1480 break;
1481 default:
1482 cout << "Logout called from invalid state: " << state << endl;
1483 break;
1484 }
[87b3ee2]1485
1486 msgTo.type = MSG_TYPE_LOGOUT;
1487
1488 strcpy(msgTo.buffer, username.c_str());
1489
[68d94de]1490 msgProcessor.sendMessage(&msgTo, &server);
[87b3ee2]1491}
1492
1493void quit()
1494{
1495 doexit = true;
1496}
1497
1498void sendChatMessage()
1499{
1500 string msg = txtChat->getStr();
1501 txtChat->clear();
1502
1503 msgTo.type = MSG_TYPE_CHAT;
1504 strcpy(msgTo.buffer, msg.c_str());
1505
[68d94de]1506 msgProcessor.sendMessage(&msgTo, &server);
[87b3ee2]1507}
[1f1eb58]1508
[b35b2b2]1509void toggleDebugging()
1510{
1511 debugging = !debugging;
1512}
1513
[fd9cdb5]1514void goToProfileScreen()
1515{
1516 msgTo.type = MSG_TYPE_PROFILE;
1517
[4c00935]1518 unsigned int playerId = currentPlayer->getId();
1519 memcpy(msgTo.buffer, &playerId, 4);
[fd9cdb5]1520 msgProcessor.sendMessage(&msgTo, &server);
1521}
1522
1523void goToLobbyScreen()
1524{
1525 wndCurrent = wndLobby;
1526}
1527
[929b4e0]1528void joinGame()
[b35b2b2]1529{
[929b4e0]1530 cout << "Joining game" << endl;
[bbebe9c]1531
1532 string msg = txtJoinGame->getStr();
1533 txtJoinGame->clear();
1534
1535 msgTo.type = MSG_TYPE_JOIN_GAME;
1536 strcpy(msgTo.buffer, msg.c_str());
1537
[68d94de]1538 msgProcessor.sendMessage(&msgTo, &server);
[dee75cc]1539}
[b35b2b2]1540
[929b4e0]1541void createGame()
[b35b2b2]1542{
[929b4e0]1543 cout << "Creating game" << endl;
[bbebe9c]1544
1545 string msg = txtCreateGame->getStr();
1546 txtCreateGame->clear();
1547
[d519032]1548 cout << "Sending message: " << msg.c_str() << endl;
1549
[bbebe9c]1550 msgTo.type = MSG_TYPE_CREATE_GAME;
1551 strcpy(msgTo.buffer, msg.c_str());
1552
[68d94de]1553 msgProcessor.sendMessage(&msgTo, &server);
[34bd549]1554 cout << "Sent CREATE_GAME message" << endl;
[03ba5e3]1555}
1556
[a0ce8a3]1557void joinWaitingArea() {
1558 cout << "joining waiting area" << endl;
[85da778]1559 currentPlayer->team = Player::TEAM_NONE;
[3476207]1560
1561 msgTo.type = MSG_TYPE_JOIN_TEAM;
1562 memcpy(msgTo.buffer, &(currentPlayer->team), 4);
1563
1564 msgProcessor.sendMessage(&msgTo, &server);
[a0ce8a3]1565}
1566
1567void joinBlueTeam() {
1568 cout << "joining blue team" << endl;
[85da778]1569 currentPlayer->team = Player::TEAM_BLUE;
[3476207]1570
1571 msgTo.type = MSG_TYPE_JOIN_TEAM;
1572 memcpy(msgTo.buffer, &(currentPlayer->team), 4);
1573
1574 msgProcessor.sendMessage(&msgTo, &server);
[a0ce8a3]1575}
1576
1577void joinRedTeam() {
1578 cout << "joining red team" << endl;
[85da778]1579 currentPlayer->team = Player::TEAM_RED;
[3476207]1580
1581 msgTo.type = MSG_TYPE_JOIN_TEAM;
1582 memcpy(msgTo.buffer, &(currentPlayer->team), 4);
1583
1584 msgProcessor.sendMessage(&msgTo, &server);
[a0ce8a3]1585}
1586
1587void startGame() {
[cf05729]1588 msgTo.type = MSG_TYPE_START_GAME;
[cb5a021]1589
1590 msgProcessor.sendMessage(&msgTo, &server);
[a0ce8a3]1591}
1592
[03ba5e3]1593void leaveGame()
1594{
1595 cout << "Leaving game" << endl;
1596
[8826eed]1597 delete game;
[03ba5e3]1598 game = NULL;
1599
1600 state = STATE_LOBBY;
1601 wndCurrent = wndLobby;
[35d702d]1602 alertMessage = "";
[03ba5e3]1603
1604 msgTo.type = MSG_TYPE_LEAVE_GAME;
1605
[68d94de]1606 msgProcessor.sendMessage(&msgTo, &server);
[95ffe57]1607}
[3e44a59]1608
1609void closeGameSummary()
1610{
1611 delete gameSummary;
[635ad9b]1612 gameSummary = NULL;
[3e44a59]1613 wndCurrent = wndLobby;
[635ad9b]1614 cout << "Processed button actions" << endl;
[8aed9c0]1615}
Note: See TracBrowser for help on using the repository browser.