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

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

gameMap removed from client

  • Property mode set to 100644
File size: 41.6 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;
434 if (target->id != curPlayerId && target->team != curPlayer->team)
[e1f78f5]435 {
[b8abc90]436 cout << "Found valid target" << endl;
[88cdae2]437
[3ff2bd7]438 msgTo.type = MSG_TYPE_ATTACK;
[b8abc90]439 memcpy(msgTo.buffer, &curPlayerId, 4);
440 memcpy(msgTo.buffer+4, &target->id, 4);
441
[68d94de]442 msgProcessor.sendMessage(&msgTo, &server);
[e1f78f5]443 }
[b8abc90]444 }
[62ee2ce]445 }
[ad5d122]446 }
[88cdae2]447 }
[e607c0f]448
[68d94de]449 if (msgProcessor.receiveMessage(&msgFrom, &from) >= 0)
[eb2ad4f]450 processMessage(msgFrom, state, chatConsole, mapPlayers, mapProjectiles, curPlayerId);
[054b50b]451
[a1a3bd5]452 if (redraw)
[e607c0f]453 {
[d352805]454 redraw = false;
[88cdae2]455
[68d94de]456 msgProcessor.resendUnackedMessages();
[10f6fc2]457
[f63aa57]458 if (debugging && wndCurrent == wndLobby)
459 wndLobbyDebug->draw(display);
[b35b2b2]460 else
461 wndCurrent->draw(display);
[9a3e6b1]462
[50e6c7a]463 if (wndCurrent == wndLobby) {
[f63aa57]464 if (!debugging)
465 chatConsole.draw(font, al_map_rgb(255,255,255));
[53d41ea]466
[321fbbc]467 map<string, int>::iterator it;
[2ee386d]468 int i=0;
[2992b1a]469 ostringstream ossGame;
[2ee386d]470 for (it = mapGames.begin(); it != mapGames.end(); it++) {
[321fbbc]471 ossGame << it->first << " (" << it->second << " players)" << endl;
[b4c5b6a]472 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]473 ossGame.clear();
474 ossGame.str("");
[2ee386d]475 i++;
[50e6c7a]476 }
477 }
[3ff2bd7]478 else if (wndCurrent == wndGame)
[03ba5e3]479 {
[b4c5b6a]480 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 4, ALLEGRO_ALIGN_LEFT, "Players");
481
482 map<unsigned int, Player*>& gamePlayers = game->getPlayers();
483 map<unsigned int, Player*>::iterator it;
484
[3ff2bd7]485 if (!debugging) {
486 int playerCount = 0;
487 for (it = gamePlayers.begin(); it != gamePlayers.end(); it++)
488 {
489 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 19+(playerCount+1)*15, ALLEGRO_ALIGN_LEFT, it->second->name.c_str());
490 playerCount++;
491 }
[b4c5b6a]492 }
493
[03ba5e3]494 ostringstream ossScoreBlue, ossScoreRed;
495
496 ossScoreBlue << "Blue: " << game->getBlueScore() << endl;
497 ossScoreRed << "Red: " << game->getRedScore() << endl;
498
499 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
500 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
[0693e25]501
[fef7c69]502 // update players
503 for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
504 {
505 it->second->updateTarget(game->getPlayers());
506 }
507
508 for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
509 {
510 it->second->move(game->getMap()); // ignore return value
511 }
512
[58ca135]513 // update projectile positions
514 map<unsigned int, Projectile>::iterator it2;
515 for (it2 = game->getProjectiles().begin(); it2 != game->getProjectiles().end(); it2++)
516 {
517 it2->second.move(game->getPlayers());
518 }
519
[6319311]520 GameRender::drawMap(game->getMap());
521 GameRender::drawPlayers(game->getPlayers(), font, curPlayerId);
[58ca135]522
523 // draw projectiles
524 for (it2 = game->getProjectiles().begin(); it2 != game->getProjectiles().end(); it2++)
525 {
526 Projectile proj = it2->second;
527
528 FLOAT_POSITION target = game->getPlayers()[proj.target]->pos;
529 float angle = atan2(target.y-proj.pos.toFloat().y, target.x-proj.pos.toFloat().x);
530
531 POSITION start, end;
532 start.x = cos(angle)*15+proj.pos.x;
533 start.y = sin(angle)*15+proj.pos.y;
534 end.x = proj.pos.x;
535 end.y = proj.pos.y;
536
537 start = mapToScreen(start);
538 end = mapToScreen(end);
539
540 al_draw_line(start.x, start.y, end.x, end.y, al_map_rgb(0, 0, 0), 4);
541 }
[03ba5e3]542 }
[f63aa57]543 else if (wndCurrent == wndGameSummary)
[50e6c7a]544 {
[635ad9b]545 ostringstream ossBlueScore, ossRedScore;
546
547 ossBlueScore << "Blue Score: " << gameSummary->getBlueScore();
548 ossRedScore << "Red Score: " << gameSummary->getRedScore();
549
[3e44a59]550 string strWinner;
551
552 if (gameSummary->getWinner() == 0)
[635ad9b]553 strWinner = "Blue Team Wins";
[3e44a59]554 else if (gameSummary->getWinner() == 1)
[635ad9b]555 strWinner = "Red Team Wins";
556 else
557 strWinner = "winner set to wrong value";
558
[3e44a59]559 al_draw_text(font, al_map_rgb(0, 255, 0), 512, 40, ALLEGRO_ALIGN_CENTRE, gameSummary->getName().c_str());
[635ad9b]560 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossBlueScore.str().c_str());
561 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossRedScore.str().c_str());
[3e44a59]562 al_draw_text(font, al_map_rgb(0, 255, 0), 512, 120, ALLEGRO_ALIGN_CENTRE, strWinner.c_str());
[87b3ee2]563 }
564
[b35b2b2]565 if (debugging) {
566 //debugConsole.draw(font, al_map_rgb(255,255,255));
567 drawMessageStatus(font);
568 }
569
[d352805]570 al_flip_display();
571 }
572 }
[9a3e6b1]573
574 #if defined WINDOWS
575 closesocket(sock);
576 #elif defined LINUX
577 close(sock);
578 #endif
579
580 shutdownWinSock();
[d352805]581
[f63aa57]582 // delete all components
583 for (unsigned int x=0; x<vctComponents.size(); x++)
584 delete vctComponents[x];
585
[87b3ee2]586 delete wndLogin;
[1785314]587 delete wndRegister;
588 delete wndLobby;
[f63aa57]589 delete wndLobbyDebug;
[3ff2bd7]590 delete wndGame;
[f63aa57]591 delete wndGameSummary;
[87b3ee2]592
[eb2ad4f]593 // game should be deleted when the player leaves a gamw
[d519032]594 if (game != NULL)
595 delete game;
[62ee2ce]596
[3e44a59]597 if (gameSummary != NULL)
598 delete gameSummary;
599
[e6c26b8]600 map<unsigned int, Player*>::iterator it;
601
602 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
603 delete it->second;
604 }
605
[d352805]606 al_destroy_event_queue(event_queue);
607 al_destroy_display(display);
608 al_destroy_timer(timer);
[8271c78]609
610 outputLog << "Stopped client on " << getCurrentDateTimeString() << endl;
611 outputLog.close();
612
[d352805]613 return 0;
614}
615
[0dde5da]616void initWinSock()
617{
618#if defined WINDOWS
619 WORD wVersionRequested;
620 WSADATA wsaData;
621 int wsaerr;
622
623 wVersionRequested = MAKEWORD(2, 2);
624 wsaerr = WSAStartup(wVersionRequested, &wsaData);
[803566d]625
[0dde5da]626 if (wsaerr != 0) {
627 cout << "The Winsock dll not found." << endl;
628 exit(1);
629 }else
630 cout << "The Winsock dll was found." << endl;
631#endif
632}
633
634void shutdownWinSock()
635{
636#if defined WINDOWS
637 WSACleanup();
638#endif
[1912323]639}
640
[eb2ad4f]641void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player*>& mapPlayers,
[e0fd377]642 map<unsigned int, Projectile>& mapProjectiles, unsigned int& curPlayerId)
[1912323]643{
[53ba300]644 // this is outdated since most messages now don't contain just a text string
[4da5aa3]645 string response = string(msg.buffer);
646
647 switch(state)
648 {
649 case STATE_START:
650 {
[e607c0f]651 cout << "In STATE_START" << endl;
652
[87b3ee2]653 switch(msg.type)
[4da5aa3]654 {
[87b3ee2]655 case MSG_TYPE_REGISTER:
656 {
[365e156]657 lblRegisterStatus->setText(response);
[87b3ee2]658 break;
659 }
[bc70282]660 default:
661 {
662 cout << "(STATE_REGISTER) Received invalid message of type " << msg.type << endl;
663 break;
664 }
665 }
666
667 break;
668 }
[1785314]669 case STATE_LOBBY:
[bc70282]670 {
[e0fd377]671 cout << "In STATE_LOBBY" << endl;
[bc70282]672 switch(msg.type)
673 {
[87b3ee2]674 case MSG_TYPE_LOGIN:
675 {
676 if (response.compare("Player has already logged in.") == 0)
677 {
[bc70282]678 goToLoginScreen();
679 state = STATE_START;
680
[365e156]681 lblLoginStatus->setText(response);
[87b3ee2]682 }
683 else if (response.compare("Incorrect username or password") == 0)
684 {
[bc70282]685 goToLoginScreen();
686 state = STATE_START;
687
[365e156]688 lblLoginStatus->setText(response);
[87b3ee2]689 }
690 else
691 {
[1785314]692 wndCurrent = wndLobby;
[95ffe57]693
694 // this message should only be sent when a player first logs in so they know their id
695
696 Player* p = new Player("", "");
697 p->deserialize(msg.buffer);
[e6c26b8]698
[95ffe57]699 if (mapPlayers.find(p->id) != mapPlayers.end())
700 delete mapPlayers[p->id];
701 mapPlayers[p->id] = p;
702 curPlayerId = p->id;
[88cdae2]703
704 cout << "Got a valid login response with the player" << endl;
[1f1eb58]705 cout << "Player id: " << curPlayerId << endl;
[95ffe57]706 cout << "Player health: " << p->health << endl;
[bc70282]707 cout << "player map size: " << mapPlayers.size() << endl;
[87b3ee2]708 }
[88cdae2]709
[a1a3bd5]710 break;
711 }
712 case MSG_TYPE_LOGOUT:
713 {
[054b50b]714 cout << "Got a logout message" << endl;
[a1a3bd5]715
[53ba300]716 int playerId;
717
718 // Check if it's about you or another player
719 memcpy(&playerId, msg.buffer, 4);
720 response = string(msg.buffer+4);
721
722 if (playerId == curPlayerId)
[87b3ee2]723 {
[53ba300]724 if (response.compare("You have successfully logged out.") == 0)
725 {
726 cout << "Logged out" << endl;
727 state = STATE_START;
728 goToLoginScreen();
729 }
730
731 // if there was an error logging out, nothing happens
732 }
733 else
734 {
735 delete mapPlayers[playerId];
[87b3ee2]736 }
[054b50b]737
738 break;
739 }
[4c202e0]740 case MSG_TYPE_PLAYER:
741 {
[1f1eb58]742 cout << "Received MSG_TYPE_PLAYER" << endl;
743
[31b347a]744 Player p("", "");
745 p.deserialize(msg.buffer);
746 p.timeLastUpdated = getCurrentMillis();
747 p.isChasing = false;
748 if (p.health <= 0)
749 p.isDead = true;
[5a5f131]750 else
[31b347a]751 p.isDead = false;
[5a5f131]752
[31b347a]753 if (mapPlayers.find(p.id) != mapPlayers.end())
754 *(mapPlayers[p.id]) = p;
755 else
756 mapPlayers[p.id] = new Player(p);
[4c202e0]757
[eb8adb1]758 break;
759 }
[a1a3bd5]760 case MSG_TYPE_PLAYER_MOVE:
761 {
762 unsigned int id;
763 int x, y;
764
765 memcpy(&id, msg.buffer, 4);
766 memcpy(&x, msg.buffer+4, 4);
767 memcpy(&y, msg.buffer+8, 4);
768
[e6c26b8]769 mapPlayers[id]->target.x = x;
770 mapPlayers[id]->target.y = y;
[a1a3bd5]771
772 break;
773 }
[eb8adb1]774 case MSG_TYPE_CHAT:
775 {
776 chatConsole.addLine(response);
[4c202e0]777
[87b3ee2]778 break;
779 }
[b978503]780 case MSG_TYPE_ATTACK:
[032e550]781 {
782 cout << "Received START_ATTACK message" << endl;
783
784 unsigned int id, targetID;
785 memcpy(&id, msg.buffer, 4);
786 memcpy(&targetID, msg.buffer+4, 4);
787
788 cout << "source id: " << id << endl;
789 cout << "target id: " << targetID << endl;
790
[e6c26b8]791 Player* source = mapPlayers[id];
[032e550]792 source->targetPlayer = targetID;
793 source->isChasing = true;
794
[b978503]795 break;
796 }
797 case MSG_TYPE_PROJECTILE:
798 {
[8c74150]799 cout << "Received a PROJECTILE message" << endl;
[fbcfc35]800
[50e6c7a]801 unsigned int id, x, y, targetId;
[fbcfc35]802
803 memcpy(&id, msg.buffer, 4);
804 memcpy(&x, msg.buffer+4, 4);
805 memcpy(&y, msg.buffer+8, 4);
806 memcpy(&targetId, msg.buffer+12, 4);
807
[8c74150]808 cout << "id: " << id << endl;
809 cout << "x: " << x << endl;
810 cout << "y: " << y << endl;
811 cout << "Target: " << targetId << endl;
812
813 Projectile proj(x, y, targetId, 0);
814 proj.setId(id);
815
816 mapProjectiles[id] = proj;
[fbcfc35]817
[b978503]818 break;
819 }
820 case MSG_TYPE_REMOVE_PROJECTILE:
821 {
[8c74150]822 cout << "Received a REMOVE_PROJECTILE message" << endl;
823
824 int id;
825 memcpy(&id, msg.buffer, 4);
826
827 mapProjectiles.erase(id);
828
[b978503]829 break;
830 }
[50e6c7a]831 case MSG_TYPE_GAME_INFO:
832 {
[803566d]833 cout << "Received a GAME_INFO message" << endl;
[50e6c7a]834
[2ee386d]835 string gameName(msg.buffer+4);
[50e6c7a]836 int numPlayers;
837
838 memcpy(&numPlayers, msg.buffer, 4);
839
[2ee386d]840 cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;
841
[e103b51]842 if (numPlayers > 0)
843 mapGames[gameName] = numPlayers;
844 else
845 mapGames.erase(gameName);
[50e6c7a]846
847 break;
848 }
[d519032]849 case MSG_TYPE_JOIN_GAME_SUCCESS:
850 {
851 cout << "Received a JOIN_GAME_SUCCESS message" << endl;
852
[8aed9c0]853 string gameName(msg.buffer);
854
855 #if defined WINDOWS
856 game = new Game(gameName, "../../data/map.txt");
857 #elif defined LINUX
858 game = new Game(gameName, "../data/map.txt");
859 #endif
860
[03ba5e3]861 cout << "Game name: " << gameName << endl;
[d519032]862
[e0fd377]863 state = STATE_GAME;
[3ff2bd7]864 wndCurrent = wndGame;
[d519032]865
866 msgTo.type = MSG_TYPE_JOIN_GAME_ACK;
867 strcpy(msgTo.buffer, gameName.c_str());
868
[68d94de]869 msgProcessor.sendMessage(&msgTo, &server);
[d519032]870
871 break;
872 }
873 case MSG_TYPE_JOIN_GAME_FAILURE:
874 {
875 cout << "Received a JOIN_GAME_FAILURE message" << endl;
876
877 break;
878 }
[45b2750]879 default:
880 {
[1785314]881 cout << "(STATE_LOBBY) Received invlaid message of type " << msg.type << endl;
[50e6c7a]882
[365e156]883 break;
[45b2750]884 }
[4da5aa3]885 }
[eb8adb1]886
[4da5aa3]887 break;
888 }
[e0fd377]889 case STATE_GAME:
[803566d]890 {
[e0fd377]891 cout << "(STATE_GAME) ";
[803566d]892 switch(msg.type)
893 {
894 case MSG_TYPE_GAME_INFO:
895 {
[d519032]896 cout << "Received a GAME_INFO message" << endl;
[803566d]897
898 string gameName(msg.buffer+4);
899 int numPlayers;
900
901 memcpy(&numPlayers, msg.buffer, 4);
902
903 cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;
904
[e103b51]905 if (numPlayers > 0)
906 mapGames[gameName] = numPlayers;
907 else
908 mapGames.erase(gameName);
[803566d]909
910 break;
911 }
[3e44a59]912 case MSG_TYPE_SCORE:
913 {
914 cout << "Received SCORE message!" << endl;
915
916 int blueScore;
917 memcpy(&blueScore, msg.buffer, 4);
918 cout << "blue score: " << blueScore << endl;
919 game->setBlueScore(blueScore);
920
921 int redScore;
922 memcpy(&redScore, msg.buffer+4, 4);
923 cout << "red score: " << redScore << endl;
924 game->setRedScore(redScore);
925
926 cout << "Processed SCORE message!" << endl;
927
928 break;
929 }
930 case MSG_TYPE_FINISH_GAME:
931 {
932 cout << "Got a finish game message" << endl;
933 cout << "Should switch to STATE_LOBBY and show the final score" << endl;
934
935 unsigned int winner, blueScore, redScore;
[635ad9b]936 memcpy(&winner, msg.buffer, 4);
937 memcpy(&blueScore, msg.buffer+4, 4);
938 memcpy(&redScore, msg.buffer+8, 4);
939
940 string gameName(msg.buffer+12);
941
942 cout << "winner: " << winner << endl;
943 cout << "blueScore: " << blueScore << endl;
944 cout << "redScore: " << redScore << endl;
945 cout << "gameName: " << gameName << endl;
[3e44a59]946
947 gameSummary = new GameSummary(gameName, winner, blueScore, redScore);
948
949 delete game;
950 game = NULL;
951 state = STATE_LOBBY;
952 wndCurrent = wndGameSummary;
953
[635ad9b]954
955 cout << "winner from obj: " << gameSummary->getWinner() << endl;
956 cout << "blueScore from obj: " << gameSummary->getBlueScore() << endl;
957 cout << "redScore from obj: " << gameSummary->getRedScore() << endl;
958 cout << "gameName from obj: " << gameSummary->getName() << endl;
[3e44a59]959 break;
960 }
[b4c5b6a]961 case MSG_TYPE_PLAYER:
962 {
963 cout << "Received MSG_TYPE_PLAYER" << endl;
964
[31b347a]965 Player p("", "");
966 p.deserialize(msg.buffer);
967 p.timeLastUpdated = getCurrentMillis();
968 p.isChasing = false;
969 if (p.health <= 0)
970 p.isDead = true;
971 else
972 p.isDead = false;
973
974 if (mapPlayers.find(p.id) != mapPlayers.end())
975 *(mapPlayers[p.id]) = p;
[b4c5b6a]976 else
[31b347a]977 mapPlayers[p.id] = new Player(p);
[b4c5b6a]978
[31b347a]979 break;
[6012178]980 }
981 case MSG_TYPE_PLAYER_JOIN_GAME:
982 {
983 cout << "Received MSG_TYPE_PLAYER_JOIN_GAME" << endl;
984
985 Player p("", "");
986 p.deserialize(msg.buffer);
987 p.timeLastUpdated = getCurrentMillis();
988 p.isChasing = false;
989 if (p.health <= 0)
990 p.isDead = true;
991 else
992 p.isDead = false;
993
994 if (mapPlayers.find(p.id) != mapPlayers.end())
995 *(mapPlayers[p.id]) = p;
996 else
997 mapPlayers[p.id] = new Player(p);
[b4c5b6a]998
[31b347a]999 game->addPlayer(mapPlayers[p.id]);
[b4c5b6a]1000
1001 break;
1002 }
[cd80d63]1003 case MSG_TYPE_LEAVE_GAME:
1004 {
1005 cout << "Received a LEAVE_GAME message" << endl;
1006
1007 string gameName(msg.buffer+4);
1008 unsigned int playerId;
1009
1010 memcpy(&playerId, msg.buffer, 4);
1011
1012 game->removePlayer(playerId);
1013
1014 break;
1015 }
[fef7c69]1016 case MSG_TYPE_PLAYER_MOVE:
1017 {
1018 cout << "Received PLAYER_MOVE message" << endl;
1019
1020 unsigned int id;
1021 int x, y;
1022
1023 memcpy(&id, msg.buffer, 4);
1024 memcpy(&x, msg.buffer+4, 4);
1025 memcpy(&y, msg.buffer+8, 4);
1026
1027 cout << "id: " << id << endl;
1028
1029 mapPlayers[id]->target.x = x;
1030 mapPlayers[id]->target.y = y;
1031
1032 break;
1033 }
[803566d]1034 case MSG_TYPE_OBJECT:
1035 {
[e0fd377]1036 cout << "Received object message in STATE_GAME" << endl;
[803566d]1037
1038 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
1039 o.deserialize(msg.buffer);
1040 cout << "object id: " << o.id << endl;
1041 game->getMap()->updateObject(o.id, o.type, o.pos.x, o.pos.y);
1042
1043 break;
1044 }
1045 case MSG_TYPE_REMOVE_OBJECT:
1046 {
[d519032]1047 cout << "Received REMOVE_OBJECT message!" << endl;
[803566d]1048
1049 int id;
1050 memcpy(&id, msg.buffer, 4);
1051
1052 cout << "Removing object with id " << id << endl;
1053
1054 if (!game->getMap()->removeObject(id))
1055 cout << "Did not remove the object" << endl;
1056
1057 break;
1058 }
[b8abc90]1059 case MSG_TYPE_ATTACK:
1060 {
1061 cout << "Received START_ATTACK message" << endl;
1062
1063 unsigned int id, targetId;
1064 memcpy(&id, msg.buffer, 4);
1065 memcpy(&targetId, msg.buffer+4, 4);
1066
1067 cout << "source id: " << id << endl;
1068 cout << "target id: " << targetId << endl;
1069
1070 // need to check the target exists in the current game
1071 Player* source = game->getPlayers()[id];
1072 source->targetPlayer = targetId;
1073 source->isChasing = true;
1074
1075 break;
1076 }
[58ca135]1077 case MSG_TYPE_PROJECTILE:
1078 {
1079 cout << "Received a PROJECTILE message" << endl;
1080
1081 unsigned int projId, x, y, targetId;
1082
1083 memcpy(&projId, msg.buffer, 4);
1084 memcpy(&x, msg.buffer+4, 4);
1085 memcpy(&y, msg.buffer+8, 4);
1086 memcpy(&targetId, msg.buffer+12, 4);
1087
1088 cout << "projId: " << projId << endl;
1089 cout << "x: " << x << endl;
1090 cout << "y: " << y << endl;
1091 cout << "Target: " << targetId << endl;
1092
1093 Projectile proj(x, y, targetId, 0);
1094 proj.setId(projId);
1095
1096 game->addProjectile(proj);
1097
1098 break;
1099 }
1100 case MSG_TYPE_REMOVE_PROJECTILE:
1101 {
1102 cout << "Received a REMOVE_PROJECTILE message" << endl;
1103
1104 unsigned int id;
1105 memcpy(&id, msg.buffer, 4);
1106
1107 game->removeProjectile(id);
1108
1109 break;
1110 }
[803566d]1111 default:
1112 {
[d519032]1113 cout << "Received invalid message of type " << msg.type << endl;
[803566d]1114
1115 break;
1116 }
1117 }
1118
1119 break;
1120 }
[4da5aa3]1121 default:
1122 {
1123 cout << "The state has an invalid value: " << state << endl;
1124
1125 break;
1126 }
1127 }
[0dde5da]1128}
[87b3ee2]1129
[929b4e0]1130int getRefreshRate(int width, int height)
1131{
1132 int numRefreshRates = al_get_num_display_modes();
1133 ALLEGRO_DISPLAY_MODE displayMode;
1134
1135 for(int i=0; i<numRefreshRates; i++) {
1136 al_get_display_mode(i, &displayMode);
1137
1138 if (displayMode.width == width && displayMode.height == height)
1139 return displayMode.refresh_rate;
1140 }
1141
1142 return 0;
1143}
1144
1145void drawMessageStatus(ALLEGRO_FONT* font)
1146{
1147 int clientMsgOffset = 5;
1148 int serverMsgOffset = 950;
1149
1150 al_draw_text(font, al_map_rgb(0, 255, 255), 0+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
1151 al_draw_text(font, al_map_rgb(0, 255, 255), 20+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Type");
1152 al_draw_text(font, al_map_rgb(0, 255, 255), 240+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Acked?");
1153
[3ff2bd7]1154 //al_draw_text(font, al_map_rgb(0, 255, 255), serverMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
[929b4e0]1155
1156 map<unsigned int, map<unsigned long, MessageContainer> >& sentMessages = msgProcessor.getSentMessages();
1157 int id, type;
1158 bool acked;
1159 ostringstream ossId, ossAcked;
1160
1161 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
1162
1163 int msgCount = 0;
1164 for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
1165 map<unsigned long, MessageContainer> playerMessage = it->second;
1166 map<unsigned long, MessageContainer>::iterator it2;
1167 for (it2 = playerMessage.begin(); it2 != playerMessage.end(); it2++) {
1168
1169 id = it->first;
1170 ossId.str("");;
1171 ossId << id;
1172
1173 type = it2->second.getMessage()->type;
1174 string typeStr = MessageContainer::getMsgTypeString(type);
1175
1176 acked = it2->second.getAcked();
1177 ossAcked.str("");;
1178 ossAcked << boolalpha << acked;
1179
1180 al_draw_text(font, al_map_rgb(0, 255, 0), clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
1181 al_draw_text(font, al_map_rgb(0, 255, 0), 20+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, typeStr.c_str());
1182 al_draw_text(font, al_map_rgb(0, 255, 0), 240+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossAcked.str().c_str());
1183
1184 msgCount++;
1185 }
1186 }
1187
1188 if (msgProcessor.getAckedMessages().size() > 0) {
1189 map<unsigned int, unsigned long long> ackedMessages = msgProcessor.getAckedMessages()[0];
1190 map<unsigned int, unsigned long long>::iterator it3;
1191
1192 msgCount = 0;
1193 for (it3 = ackedMessages.begin(); it3 != ackedMessages.end(); it3++) {
1194 ossId.str("");;
1195 ossId << it3->first;
1196
1197 al_draw_text(font, al_map_rgb(255, 0, 0), 25+serverMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
1198
1199 msgCount++;
1200 }
1201 }
1202}
1203
1204// Callback definitions
1205
[5c95436]1206void goToRegisterScreen()
1207{
1208 txtUsernameRegister->clear();
1209 txtPasswordRegister->clear();
[49da01a]1210 lblRegisterStatus->setText("");
1211 rblClasses->setSelectedButton(-1);
1212
1213 wndCurrent = wndRegister;
[5c95436]1214}
1215
1216void goToLoginScreen()
[87b3ee2]1217{
1218 txtUsername->clear();
1219 txtPassword->clear();
[49da01a]1220 lblLoginStatus->setText("");
1221
1222 wndCurrent = wndLogin;
[5c95436]1223}
1224
[bc70282]1225// maybe need a goToGameScreen function as well and add state changes to these functions as well
1226
[5c95436]1227void registerAccount()
1228{
1229 string username = txtUsernameRegister->getStr();
1230 string password = txtPasswordRegister->getStr();
1231
1232 txtUsernameRegister->clear();
1233 txtPasswordRegister->clear();
1234 // maybe clear rblClasses as well (add a method to RadioButtonList to enable this)
1235
1236 Player::PlayerClass playerClass;
1237
1238 switch (rblClasses->getSelectedButton()) {
1239 case 0:
1240 playerClass = Player::CLASS_WARRIOR;
1241 break;
1242 case 1:
1243 playerClass = Player::CLASS_RANGER;
1244 break;
1245 default:
1246 cout << "Invalid class selection" << endl;
1247 playerClass = Player::CLASS_NONE;
1248 break;
1249 }
[87b3ee2]1250
1251 msgTo.type = MSG_TYPE_REGISTER;
1252
1253 strcpy(msgTo.buffer, username.c_str());
1254 strcpy(msgTo.buffer+username.size()+1, password.c_str());
[5c95436]1255 memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
[87b3ee2]1256
[68d94de]1257 msgProcessor.sendMessage(&msgTo, &server);
[87b3ee2]1258}
1259
1260void login()
1261{
1262 string strUsername = txtUsername->getStr();
1263 string strPassword = txtPassword->getStr();
1264 username = strUsername;
1265
1266 txtUsername->clear();
1267 txtPassword->clear();
1268
1269 msgTo.type = MSG_TYPE_LOGIN;
1270
1271 strcpy(msgTo.buffer, strUsername.c_str());
1272 strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
1273
[68d94de]1274 msgProcessor.sendMessage(&msgTo, &server);
[bc70282]1275
[1785314]1276 state = STATE_LOBBY;
[87b3ee2]1277}
1278
1279void logout()
1280{
[929b4e0]1281 switch(state) {
1282 case STATE_LOBBY:
1283 txtJoinGame->clear();
1284 txtCreateGame->clear();
1285 break;
1286 default:
1287 cout << "Logout called from invalid state: " << state << endl;
1288 break;
1289 }
[87b3ee2]1290
1291 msgTo.type = MSG_TYPE_LOGOUT;
1292
1293 strcpy(msgTo.buffer, username.c_str());
1294
[68d94de]1295 msgProcessor.sendMessage(&msgTo, &server);
[87b3ee2]1296}
1297
1298void quit()
1299{
1300 doexit = true;
1301}
1302
1303void sendChatMessage()
1304{
1305 string msg = txtChat->getStr();
1306 txtChat->clear();
1307
1308 msgTo.type = MSG_TYPE_CHAT;
1309 strcpy(msgTo.buffer, msg.c_str());
1310
[68d94de]1311 msgProcessor.sendMessage(&msgTo, &server);
[87b3ee2]1312}
[1f1eb58]1313
[b35b2b2]1314void toggleDebugging()
1315{
1316 debugging = !debugging;
1317}
1318
[929b4e0]1319void joinGame()
[b35b2b2]1320{
[929b4e0]1321 cout << "Joining game" << endl;
[bbebe9c]1322
1323 string msg = txtJoinGame->getStr();
1324 txtJoinGame->clear();
1325
1326 msgTo.type = MSG_TYPE_JOIN_GAME;
1327 strcpy(msgTo.buffer, msg.c_str());
1328
[68d94de]1329 msgProcessor.sendMessage(&msgTo, &server);
[dee75cc]1330}
[b35b2b2]1331
[929b4e0]1332void createGame()
[b35b2b2]1333{
[929b4e0]1334 cout << "Creating game" << endl;
[bbebe9c]1335
1336 string msg = txtCreateGame->getStr();
1337 txtCreateGame->clear();
1338
[d519032]1339 cout << "Sending message: " << msg.c_str() << endl;
1340
[bbebe9c]1341 msgTo.type = MSG_TYPE_CREATE_GAME;
1342 strcpy(msgTo.buffer, msg.c_str());
1343
[68d94de]1344 msgProcessor.sendMessage(&msgTo, &server);
[03ba5e3]1345}
1346
1347void leaveGame()
1348{
1349 cout << "Leaving game" << endl;
1350
1351 game = NULL;
1352
1353 state = STATE_LOBBY;
1354 wndCurrent = wndLobby;
1355
1356 msgTo.type = MSG_TYPE_LEAVE_GAME;
1357
[68d94de]1358 msgProcessor.sendMessage(&msgTo, &server);
[95ffe57]1359}
[3e44a59]1360
1361void closeGameSummary()
1362{
1363 delete gameSummary;
[635ad9b]1364 gameSummary = NULL;
[3e44a59]1365 wndCurrent = wndLobby;
[635ad9b]1366 cout << "Processed button actions" << endl;
[8aed9c0]1367}
Note: See TracBrowser for help on using the repository browser.