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

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

Client handles PROJECTILE and REMOVE_PROJECTILE messages in individual games and updates projectile targets and move projectils in individual games as well

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