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