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