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

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

Client updates related to the attack message change

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