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

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

Reorganized the client-side game creation logic

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