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

Last change on this file since 7f9b01c was 0065962, checked in by dportnoy15 <dmitry.portnoy@…>, 6 years ago

Update the readme with instructions for installing the client on OSX

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