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

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

Client sends a START_ATTACK message when a player right-clicks on another player in an individual game and processes START_ATTACK messages in individual games

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