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

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

id and targetPlayer are now both private members of the Player class and have getters and setters to access them

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