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

Last change on this file since 8aed9c0 was 8aed9c0, checked in by Dmitry Portnoy <dmp1488@…>, 11 years ago

Client compiles under linux

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