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

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

Client makefile is up-to-date and includes -Wall

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