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

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

Clients store the total number of players in each game

  • Property mode set to 100644
File size: 35.0 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;
[2ee386d]121map<string, Game> 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) {
[2ee386d]454 map<string, Game>::iterator it;
455 int i=0;
456 for (it = mapGames.begin(); it != mapGames.end(); it++) {
457 al_draw_text(font, al_map_rgb(0, 255, 0), SCREEN_W*1/4-100, 120+i*15, ALLEGRO_ALIGN_LEFT, it->first.c_str());
458 i++;
[50e6c7a]459 }
460 }
461 else if (wndCurrent == wndGame)
462 {
[b35b2b2]463 if (!debugging)
464 chatConsole.draw(font, al_map_rgb(255,255,255));
[bc70282]465
[87b3ee2]466 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
[62ee2ce]467
[15efb4e]468 ostringstream ossScoreBlue, ossScoreRed;
469
470 ossScoreBlue << "Blue: " << scoreBlue << endl;
471 ossScoreRed << "Red: " << scoreRed << endl;
472
473 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
474 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
475
[032e550]476 // update players
[a1a3bd5]477 map<unsigned int, Player>::iterator it;
[032e550]478 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
479 {
480 it->second.updateTarget(mapPlayers);
481 }
482
[a1a3bd5]483 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
484 {
[227baaa]485 it->second.move(gameMap); // ignore return value
[a1a3bd5]486 }
487
[8c74150]488 // update projectile positions
489 map<unsigned int, Projectile>::iterator it2;
490 for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
491 {
492 it2->second.move(mapPlayers);
493 }
494
[62ee2ce]495 drawMap(gameMap);
[d09fe76]496 drawPlayers(mapPlayers, font, curPlayerId);
[8c74150]497
498 // draw projectiles
499 for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
500 {
501 Projectile proj = it2->second;
502
503 FLOAT_POSITION target = mapPlayers[proj.target].pos;
504 float angle = atan2(target.y-proj.pos.toFloat().y, target.x-proj.pos.toFloat().x);
505
506 POSITION start, end;
507 start.x = cos(angle)*15+proj.pos.x;
508 start.y = sin(angle)*15+proj.pos.y;
509 end.x = proj.pos.x;
510 end.y = proj.pos.y;
511
512 start = mapToScreen(start);
513 end = mapToScreen(end);
514
515 al_draw_line(start.x, start.y, end.x, end.y, al_map_rgb(0, 0, 0), 4);
516 }
[87b3ee2]517 }
518
[b35b2b2]519 if (debugging) {
520 //debugConsole.draw(font, al_map_rgb(255,255,255));
521 drawMessageStatus(font);
522 }
523
[d352805]524 al_flip_display();
525 }
526 }
[9a3e6b1]527
528 #if defined WINDOWS
529 closesocket(sock);
530 #elif defined LINUX
531 close(sock);
532 #endif
533
534 shutdownWinSock();
[d352805]535
[87b3ee2]536 delete wndLogin;
[1785314]537 delete wndRegister;
538 delete wndLobby;
539 delete wndGame;
540 delete wndGameDebug;
[87b3ee2]541
[62ee2ce]542 delete gameMap;
543
[d352805]544 al_destroy_event_queue(event_queue);
545 al_destroy_display(display);
546 al_destroy_timer(timer);
[8271c78]547
548 outputLog << "Stopped client on " << getCurrentDateTimeString() << endl;
549 outputLog.close();
550
[d352805]551 return 0;
552}
553
[1f1eb58]554
555
[4da5aa3]556// need to make a function like this that works on windows
557void error(const char *msg)
558{
559 perror(msg);
560 shutdownWinSock();
561 exit(1);
562}
563
[0dde5da]564void initWinSock()
565{
566#if defined WINDOWS
567 WORD wVersionRequested;
568 WSADATA wsaData;
569 int wsaerr;
570
571 wVersionRequested = MAKEWORD(2, 2);
572 wsaerr = WSAStartup(wVersionRequested, &wsaData);
573
574 if (wsaerr != 0) {
575 cout << "The Winsock dll not found." << endl;
576 exit(1);
577 }else
578 cout << "The Winsock dll was found." << endl;
579#endif
580}
581
582void shutdownWinSock()
583{
584#if defined WINDOWS
585 WSACleanup();
586#endif
[1912323]587}
588
[62ee2ce]589POSITION screenToMap(POSITION pos)
590{
591 pos.x = pos.x-300;
592 pos.y = pos.y-100;
593
594 if (pos.x < 0 || pos.y < 0)
595 {
596 pos.x = -1;
597 pos.y = -1;
598 }
599
600 return pos;
601}
602
603POSITION mapToScreen(POSITION pos)
604{
605 pos.x = pos.x+300;
606 pos.y = pos.y+100;
607
608 return pos;
609}
610
[ca44f82]611POSITION mapToScreen(FLOAT_POSITION pos)
612{
613 POSITION p;
614 p.x = pos.x+300;
615 p.y = pos.y+100;
616
617 return p;
618}
619
[fbcfc35]620void 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]621{
[4da5aa3]622 string response = string(msg.buffer);
623
624 switch(state)
625 {
626 case STATE_START:
627 {
[e607c0f]628 cout << "In STATE_START" << endl;
629
[87b3ee2]630 switch(msg.type)
[4da5aa3]631 {
[87b3ee2]632 case MSG_TYPE_REGISTER:
633 {
[365e156]634 lblRegisterStatus->setText(response);
[87b3ee2]635 break;
636 }
[bc70282]637 default:
638 {
639 cout << "(STATE_REGISTER) Received invalid message of type " << msg.type << endl;
640 break;
641 }
642 }
643
644 break;
645 }
[1785314]646 case STATE_LOBBY:
647 case STATE_GAME:
[bc70282]648 {
649 switch(msg.type)
650 {
[87b3ee2]651 case MSG_TYPE_LOGIN:
652 {
653 if (response.compare("Player has already logged in.") == 0)
654 {
[bc70282]655 goToLoginScreen();
656 state = STATE_START;
657
[365e156]658 lblLoginStatus->setText(response);
[87b3ee2]659 }
660 else if (response.compare("Incorrect username or password") == 0)
661 {
[bc70282]662 goToLoginScreen();
663 state = STATE_START;
664
[365e156]665 lblLoginStatus->setText(response);
[87b3ee2]666 }
667 else
668 {
[1785314]669 wndCurrent = wndLobby;
[88cdae2]670
671 Player p("", "");
672 p.deserialize(msg.buffer);
673 mapPlayers[p.id] = p;
674 curPlayerId = p.id;
675
676 cout << "Got a valid login response with the player" << endl;
[1f1eb58]677 cout << "Player id: " << curPlayerId << endl;
678 cout << "Player health: " << p.health << endl;
[bc70282]679 cout << "player map size: " << mapPlayers.size() << endl;
[87b3ee2]680 }
[88cdae2]681
[a1a3bd5]682 break;
683 }
684 case MSG_TYPE_LOGOUT:
685 {
[054b50b]686 cout << "Got a logout message" << endl;
[a1a3bd5]687
[87b3ee2]688 if (response.compare("You have successfully logged out.") == 0)
689 {
690 cout << "Logged out" << endl;
691 state = STATE_START;
[49da01a]692 goToLoginScreen();
[87b3ee2]693 }
[054b50b]694
695 break;
696 }
[4c202e0]697 case MSG_TYPE_PLAYER:
698 {
[1f1eb58]699 cout << "Received MSG_TYPE_PLAYER" << endl;
700
[60776f2]701 Player p("", "");
702 p.deserialize(msg.buffer);
[054b50b]703 p.timeLastUpdated = getCurrentMillis();
[032e550]704 p.isChasing = false;
[5a5f131]705 if (p.health <= 0)
706 p.isDead = true;
707 else
708 p.isDead = false;
709
[3a79253]710 mapPlayers[p.id] = p;
[4c202e0]711
[eb8adb1]712 break;
713 }
[a1a3bd5]714 case MSG_TYPE_PLAYER_MOVE:
715 {
716 unsigned int id;
717 int x, y;
718
719 memcpy(&id, msg.buffer, 4);
720 memcpy(&x, msg.buffer+4, 4);
721 memcpy(&y, msg.buffer+8, 4);
722
723 mapPlayers[id].target.x = x;
724 mapPlayers[id].target.y = y;
725
726 break;
727 }
[eb8adb1]728 case MSG_TYPE_CHAT:
729 {
730 chatConsole.addLine(response);
[4c202e0]731
[87b3ee2]732 break;
733 }
[45b2750]734 case MSG_TYPE_OBJECT:
735 {
[1785314]736 cout << "Received object message in STATE_LOBBY." << endl;
[45b2750]737
738 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
739 o.deserialize(msg.buffer);
[bc70282]740 cout << "object id: " << o.id << endl;
[45b2750]741 gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
742
743 break;
744 }
[2df63d6]745 case MSG_TYPE_REMOVE_OBJECT:
746 {
[7511a2b]747 cout << "Received REMOVE_OBJECT message!" << endl;
748
[2df63d6]749 int id;
750 memcpy(&id, msg.buffer, 4);
[7511a2b]751
752 cout << "Removing object with id " << id << endl;
753
[2df63d6]754 if (!gameMap->removeObject(id))
755 cout << "Did not remove the object" << endl;
[7511a2b]756
757 break;
[2df63d6]758 }
[15efb4e]759 case MSG_TYPE_SCORE:
760 {
761 memcpy(&scoreBlue, msg.buffer, 4);
762 memcpy(&scoreRed, msg.buffer+4, 4);
763
764 break;
765 }
[b978503]766 case MSG_TYPE_ATTACK:
767 {
[032e550]768 cout << "Received ATTACK message" << endl;
769
770 break;
771 }
772 case MSG_TYPE_START_ATTACK:
773 {
774 cout << "Received START_ATTACK message" << endl;
775
776 unsigned int id, targetID;
777 memcpy(&id, msg.buffer, 4);
778 memcpy(&targetID, msg.buffer+4, 4);
779
780 cout << "source id: " << id << endl;
781 cout << "target id: " << targetID << endl;
782
783 Player* source = &mapPlayers[id];
784 source->targetPlayer = targetID;
785 source->isChasing = true;
786
[b978503]787 break;
788 }
789 case MSG_TYPE_PROJECTILE:
790 {
[8c74150]791 cout << "Received a PROJECTILE message" << endl;
[fbcfc35]792
[50e6c7a]793 unsigned int id, x, y, targetId;
[fbcfc35]794
795 memcpy(&id, msg.buffer, 4);
796 memcpy(&x, msg.buffer+4, 4);
797 memcpy(&y, msg.buffer+8, 4);
798 memcpy(&targetId, msg.buffer+12, 4);
799
[8c74150]800 cout << "id: " << id << endl;
801 cout << "x: " << x << endl;
802 cout << "y: " << y << endl;
803 cout << "Target: " << targetId << endl;
804
805 Projectile proj(x, y, targetId, 0);
806 proj.setId(id);
807
808 mapProjectiles[id] = proj;
[fbcfc35]809
[b978503]810 break;
811 }
812 case MSG_TYPE_REMOVE_PROJECTILE:
813 {
[8c74150]814 cout << "Received a REMOVE_PROJECTILE message" << endl;
815
816 int id;
817 memcpy(&id, msg.buffer, 4);
818
819 mapProjectiles.erase(id);
820
[b978503]821 break;
822 }
[50e6c7a]823 case MSG_TYPE_GAME_INFO:
824 {
825 cout << "Received a GAME_INFO message" << endl;
826
[2ee386d]827 string gameName(msg.buffer+4);
[50e6c7a]828 int numPlayers;
829
830 memcpy(&numPlayers, msg.buffer, 4);
831
[2ee386d]832 cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;
833
834 if (mapGames.find(gameName) == mapGames.end())
835 mapGames[gameName] = Game(gameName);
836 mapGames[gameName].setNumPlayers(numPlayers);
[50e6c7a]837
838 break;
839 }
[45b2750]840 default:
841 {
[1785314]842 cout << "(STATE_LOBBY) Received invlaid message of type " << msg.type << endl;
[50e6c7a]843
[365e156]844 break;
[45b2750]845 }
[4da5aa3]846 }
[eb8adb1]847
[4da5aa3]848 break;
849 }
850 default:
851 {
852 cout << "The state has an invalid value: " << state << endl;
853
854 break;
855 }
856 }
[0dde5da]857}
[87b3ee2]858
[d436ac4]859// this should probably be in the WorldMap class
[62ee2ce]860void drawMap(WorldMap* gameMap)
861{
862 POSITION mapPos;
863 mapPos.x = 0;
864 mapPos.y = 0;
865 mapPos = mapToScreen(mapPos);
[6e66ffd]866
[147f662]867 for (int x=0; x<gameMap->width; x++)
[62ee2ce]868 {
[147f662]869 for (int y=0; y<gameMap->height; y++)
[62ee2ce]870 {
871 WorldMap::TerrainType el = gameMap->getElement(x, y);
[cc1c6c1]872 WorldMap::StructureType structure = gameMap->getStructure(x, y);
[62ee2ce]873
874 if (el == WorldMap::TERRAIN_GRASS)
875 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));
876 else if (el == WorldMap::TERRAIN_OCEAN)
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, 0, 255));
878 else if (el == WorldMap::TERRAIN_ROCK)
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(100, 100, 0));
[a1a3bd5]880
[6e66ffd]881 if (structure == WorldMap::STRUCTURE_BLUE_FLAG) {
882 al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
883 //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));
884 }else if (structure == WorldMap::STRUCTURE_RED_FLAG) {
885 al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
886 //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));
887 }
888 }
889 }
890
[f3cf1a5]891 for (int x=0; x<gameMap->width; x++)
[6e66ffd]892 {
[f3cf1a5]893 for (int y=0; y<gameMap->height; y++)
[6e66ffd]894 {
895 vector<WorldMap::Object> vctObjects = gameMap->getObjects(x, y);
896
897 vector<WorldMap::Object>::iterator it;
898 for(it = vctObjects.begin(); it != vctObjects.end(); it++) {
899 switch(it->type) {
900 case WorldMap::OBJECT_BLUE_FLAG:
901 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));
902 break;
903 case WorldMap::OBJECT_RED_FLAG:
904 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));
905 break;
906 }
907 }
[62ee2ce]908 }
909 }
910}
911
[d09fe76]912void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId)
[88cdae2]913{
914 map<unsigned int, Player>::iterator it;
915
[62ee2ce]916 Player* p;
917 POSITION pos;
[d09fe76]918 ALLEGRO_COLOR color;
[62ee2ce]919
[88cdae2]920 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
921 {
[62ee2ce]922 p = &it->second;
[5a5f131]923
924 if (p->isDead)
925 continue;
926
[62ee2ce]927 pos = mapToScreen(p->pos);
[88cdae2]928
[7efed11]929 if (p->id == curPlayerId)
[a6066e8]930 al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));
931
932 if (p->team == 0)
[d09fe76]933 color = al_map_rgb(0, 0, 255);
[a6066e8]934 else if (p->team == 1)
[d09fe76]935 color = al_map_rgb(255, 0, 0);
936
937 al_draw_filled_circle(pos.x, pos.y, 12, color);
938
939 // draw player class
940 int fontHeight = al_get_font_line_height(font);
941
942 string strClass;
943 switch (p->playerClass) {
944 case Player::CLASS_WARRIOR:
945 strClass = "W";
946 break;
947 case Player::CLASS_RANGER:
948 strClass = "R";
949 break;
950 case Player::CLASS_NONE:
951 strClass = "";
952 break;
953 default:
954 strClass = "";
955 break;
956 }
957 al_draw_text(font, al_map_rgb(0, 0, 0), pos.x, pos.y-fontHeight/2, ALLEGRO_ALIGN_CENTRE, strClass.c_str());
958
959 // draw player health
960 al_draw_filled_rectangle(pos.x-12, pos.y-24, pos.x+12, pos.y-16, al_map_rgb(0, 0, 0));
961 if (it->second.maxHealth != 0)
[e1f78f5]962 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]963
[7d91bbe]964 if (p->hasBlueFlag)
[7efed11]965 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(0, 0, 255));
[a6066e8]966 else if (p->hasRedFlag)
[7efed11]967 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(255, 0, 0));
[88cdae2]968 }
969}
970
[929b4e0]971int getRefreshRate(int width, int height)
972{
973 int numRefreshRates = al_get_num_display_modes();
974 ALLEGRO_DISPLAY_MODE displayMode;
975
976 for(int i=0; i<numRefreshRates; i++) {
977 al_get_display_mode(i, &displayMode);
978
979 if (displayMode.width == width && displayMode.height == height)
980 return displayMode.refresh_rate;
981 }
982
983 return 0;
984}
985
986void drawMessageStatus(ALLEGRO_FONT* font)
987{
988 int clientMsgOffset = 5;
989 int serverMsgOffset = 950;
990
991 al_draw_text(font, al_map_rgb(0, 255, 255), 0+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
992 al_draw_text(font, al_map_rgb(0, 255, 255), 20+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Type");
993 al_draw_text(font, al_map_rgb(0, 255, 255), 240+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Acked?");
994
995 al_draw_text(font, al_map_rgb(0, 255, 255), serverMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
996
997 map<unsigned int, map<unsigned long, MessageContainer> >& sentMessages = msgProcessor.getSentMessages();
998 int id, type;
999 bool acked;
1000 ostringstream ossId, ossAcked;
1001
1002 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
1003
1004 int msgCount = 0;
1005 for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
1006 map<unsigned long, MessageContainer> playerMessage = it->second;
1007 map<unsigned long, MessageContainer>::iterator it2;
1008 for (it2 = playerMessage.begin(); it2 != playerMessage.end(); it2++) {
1009
1010 id = it->first;
1011 ossId.str("");;
1012 ossId << id;
1013
1014 type = it2->second.getMessage()->type;
1015 string typeStr = MessageContainer::getMsgTypeString(type);
1016
1017 acked = it2->second.getAcked();
1018 ossAcked.str("");;
1019 ossAcked << boolalpha << acked;
1020
1021 al_draw_text(font, al_map_rgb(0, 255, 0), clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
1022 al_draw_text(font, al_map_rgb(0, 255, 0), 20+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, typeStr.c_str());
1023 al_draw_text(font, al_map_rgb(0, 255, 0), 240+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossAcked.str().c_str());
1024
1025 msgCount++;
1026 }
1027 }
1028
1029 if (msgProcessor.getAckedMessages().size() > 0) {
1030 map<unsigned int, unsigned long long> ackedMessages = msgProcessor.getAckedMessages()[0];
1031 map<unsigned int, unsigned long long>::iterator it3;
1032
1033 msgCount = 0;
1034 for (it3 = ackedMessages.begin(); it3 != ackedMessages.end(); it3++) {
1035 ossId.str("");;
1036 ossId << it3->first;
1037
1038 al_draw_text(font, al_map_rgb(255, 0, 0), 25+serverMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
1039
1040 msgCount++;
1041 }
1042 }
1043}
1044
1045// Callback definitions
1046
[5c95436]1047void goToRegisterScreen()
1048{
1049 txtUsernameRegister->clear();
1050 txtPasswordRegister->clear();
[49da01a]1051 lblRegisterStatus->setText("");
1052 rblClasses->setSelectedButton(-1);
1053
1054 wndCurrent = wndRegister;
[5c95436]1055}
1056
1057void goToLoginScreen()
[87b3ee2]1058{
1059 txtUsername->clear();
1060 txtPassword->clear();
[49da01a]1061 lblLoginStatus->setText("");
1062
1063 wndCurrent = wndLogin;
[5c95436]1064}
1065
[bc70282]1066// maybe need a goToGameScreen function as well and add state changes to these functions as well
1067
[5c95436]1068void registerAccount()
1069{
1070 string username = txtUsernameRegister->getStr();
1071 string password = txtPasswordRegister->getStr();
1072
1073 txtUsernameRegister->clear();
1074 txtPasswordRegister->clear();
1075 // maybe clear rblClasses as well (add a method to RadioButtonList to enable this)
1076
1077 Player::PlayerClass playerClass;
1078
1079 switch (rblClasses->getSelectedButton()) {
1080 case 0:
1081 playerClass = Player::CLASS_WARRIOR;
1082 break;
1083 case 1:
1084 playerClass = Player::CLASS_RANGER;
1085 break;
1086 default:
1087 cout << "Invalid class selection" << endl;
1088 playerClass = Player::CLASS_NONE;
1089 break;
1090 }
[87b3ee2]1091
1092 msgTo.type = MSG_TYPE_REGISTER;
1093
1094 strcpy(msgTo.buffer, username.c_str());
1095 strcpy(msgTo.buffer+username.size()+1, password.c_str());
[5c95436]1096 memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
[87b3ee2]1097
[753fa8a]1098 msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
[87b3ee2]1099}
1100
1101void login()
1102{
1103 string strUsername = txtUsername->getStr();
1104 string strPassword = txtPassword->getStr();
1105 username = strUsername;
1106
1107 txtUsername->clear();
1108 txtPassword->clear();
1109
1110 msgTo.type = MSG_TYPE_LOGIN;
1111
1112 strcpy(msgTo.buffer, strUsername.c_str());
1113 strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
1114
[753fa8a]1115 msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
[bc70282]1116
[1785314]1117 state = STATE_LOBBY;
[87b3ee2]1118}
1119
1120void logout()
1121{
[929b4e0]1122 switch(state) {
1123 case STATE_LOBBY:
1124 txtJoinGame->clear();
1125 txtCreateGame->clear();
1126 break;
1127 case STATE_GAME:
1128 txtChat->clear();
1129 chatConsole.clear();
1130 break;
1131 default:
1132 cout << "Logout called from invalid state: " << state << endl;
1133 break;
1134 }
[87b3ee2]1135
1136 msgTo.type = MSG_TYPE_LOGOUT;
1137
1138 strcpy(msgTo.buffer, username.c_str());
1139
[753fa8a]1140 msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
[87b3ee2]1141}
1142
1143void quit()
1144{
1145 doexit = true;
1146}
1147
1148void sendChatMessage()
1149{
1150 string msg = txtChat->getStr();
1151 txtChat->clear();
1152
1153 msgTo.type = MSG_TYPE_CHAT;
1154 strcpy(msgTo.buffer, msg.c_str());
1155
[753fa8a]1156 msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
[87b3ee2]1157}
[1f1eb58]1158
[b35b2b2]1159void toggleDebugging()
1160{
1161 debugging = !debugging;
1162}
1163
[929b4e0]1164void joinGame()
[b35b2b2]1165{
[929b4e0]1166 cout << "Joining game" << endl;
[bbebe9c]1167
1168 string msg = txtJoinGame->getStr();
1169 txtJoinGame->clear();
1170
1171 msgTo.type = MSG_TYPE_JOIN_GAME;
1172 strcpy(msgTo.buffer, msg.c_str());
1173
1174 msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
[dee75cc]1175}
[b35b2b2]1176
[929b4e0]1177void createGame()
[b35b2b2]1178{
[929b4e0]1179 cout << "Creating game" << endl;
[bbebe9c]1180
1181 string msg = txtCreateGame->getStr();
1182 txtCreateGame->clear();
1183
1184 msgTo.type = MSG_TYPE_CREATE_GAME;
1185 strcpy(msgTo.buffer, msg.c_str());
1186
1187 msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
[b35b2b2]1188}
Note: See TracBrowser for help on using the repository browser.