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

Last change on this file since b48ef09 was 803566d, checked in by Dmitry Portnoy <dportnoy@…>, 11 years ago

The client has a new state to handle separate game instances

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