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

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

The old Game window has been completely removed from the client and the client frees all gui components on its own, instead of relying on the Window destructor

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