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