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