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

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

Client sends flag pickup and drop messages in individual games

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