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

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

Client only stores the game name and number of players of each game

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