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