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 <map>
|
---|
23 |
|
---|
24 | #include <allegro5/allegro.h>
|
---|
25 | #include <allegro5/allegro_font.h>
|
---|
26 | #include <allegro5/allegro_ttf.h>
|
---|
27 | #include <allegro5/allegro_primitives.h>
|
---|
28 |
|
---|
29 | #include "../../common/Common.h"
|
---|
30 | #include "../../common/Message.h"
|
---|
31 | #include "../../common/MessageProcessor.h"
|
---|
32 | #include "../../common/WorldMap.h"
|
---|
33 | #include "../../common/Player.h"
|
---|
34 | #include "../../common/Projectile.h"
|
---|
35 |
|
---|
36 | #include "Window.h"
|
---|
37 | #include "Textbox.h"
|
---|
38 | #include "Button.h"
|
---|
39 | #include "RadioButtonList.h"
|
---|
40 | #include "chat.h"
|
---|
41 |
|
---|
42 | #ifdef WINDOWS
|
---|
43 | #pragma comment(lib, "ws2_32.lib")
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | using namespace std;
|
---|
47 |
|
---|
48 | void initWinSock();
|
---|
49 | void shutdownWinSock();
|
---|
50 | void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, WorldMap *gameMap, map<unsigned int, Player>& mapPlayers, map<unsigned int, Projectile>& mapProjectiles, unsigned int& curPlayerId, int &scoreBlue, int &scoreRed);
|
---|
51 | void drawMap(WorldMap* gameMap);
|
---|
52 | void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId);
|
---|
53 | POSITION screenToMap(POSITION pos);
|
---|
54 | POSITION mapToScreen(POSITION pos);
|
---|
55 | int getRefreshRate(int width, int height);
|
---|
56 |
|
---|
57 | // callbacks
|
---|
58 | void goToLoginScreen();
|
---|
59 | void goToRegisterScreen();
|
---|
60 | void registerAccount();
|
---|
61 | void login();
|
---|
62 | void logout();
|
---|
63 | void quit();
|
---|
64 | void sendChatMessage();
|
---|
65 |
|
---|
66 | void error(const char *);
|
---|
67 |
|
---|
68 | const float FPS = 60;
|
---|
69 | const int SCREEN_W = 1024;
|
---|
70 | const int SCREEN_H = 768;
|
---|
71 |
|
---|
72 | enum STATE {
|
---|
73 | STATE_START,
|
---|
74 | STATE_LOGIN // this means you're already logged in
|
---|
75 | };
|
---|
76 |
|
---|
77 | int state;
|
---|
78 |
|
---|
79 | bool doexit;
|
---|
80 |
|
---|
81 | Window* wndLogin;
|
---|
82 | Window* wndRegister;
|
---|
83 | Window* wndMain;
|
---|
84 | Window* wndCurrent;
|
---|
85 |
|
---|
86 | // wndLogin
|
---|
87 | Textbox* txtUsername;
|
---|
88 | Textbox* txtPassword;
|
---|
89 |
|
---|
90 | // wndRegister
|
---|
91 | Textbox* txtUsernameRegister;
|
---|
92 | Textbox* txtPasswordRegister;
|
---|
93 | RadioButtonList* rblClasses;
|
---|
94 |
|
---|
95 | // wndMain
|
---|
96 | Textbox* txtChat;
|
---|
97 |
|
---|
98 | int sock;
|
---|
99 | struct sockaddr_in server, from;
|
---|
100 | struct hostent *hp;
|
---|
101 | NETWORK_MSG msgTo, msgFrom;
|
---|
102 | string username;
|
---|
103 | chat chatConsole;
|
---|
104 |
|
---|
105 | MessageProcessor msgProcessor;
|
---|
106 |
|
---|
107 | int main(int argc, char **argv)
|
---|
108 | {
|
---|
109 | ALLEGRO_DISPLAY *display = NULL;
|
---|
110 | ALLEGRO_EVENT_QUEUE *event_queue = NULL;
|
---|
111 | ALLEGRO_TIMER *timer = NULL;
|
---|
112 | bool key[4] = { false, false, false, false };
|
---|
113 | bool redraw = true;
|
---|
114 | doexit = false;
|
---|
115 | map<unsigned int, Player> mapPlayers;
|
---|
116 | map<unsigned int, Projectile> mapProjectiles;
|
---|
117 | unsigned int curPlayerId = -1;
|
---|
118 | int scoreBlue, scoreRed;
|
---|
119 | bool fullscreen = false;
|
---|
120 |
|
---|
121 | scoreBlue = 0;
|
---|
122 | scoreRed = 0;
|
---|
123 |
|
---|
124 | state = STATE_START;
|
---|
125 |
|
---|
126 | if(!al_init()) {
|
---|
127 | fprintf(stderr, "failed to initialize allegro!\n");
|
---|
128 | return -1;
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (al_init_primitives_addon())
|
---|
132 | cout << "Primitives initialized" << endl;
|
---|
133 | else
|
---|
134 | cout << "Primitives not initialized" << endl;
|
---|
135 |
|
---|
136 | al_init_font_addon();
|
---|
137 | al_init_ttf_addon();
|
---|
138 |
|
---|
139 | #if defined WINDOWS
|
---|
140 | ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0);
|
---|
141 | #elif defined LINUX
|
---|
142 | ALLEGRO_FONT *font = al_load_ttf_font("pirulen.ttf", 12, 0);
|
---|
143 | #endif
|
---|
144 |
|
---|
145 | if (!font) {
|
---|
146 | fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
|
---|
147 | getchar();
|
---|
148 | return -1;
|
---|
149 | }
|
---|
150 |
|
---|
151 | if(!al_install_keyboard()) {
|
---|
152 | fprintf(stderr, "failed to initialize the keyboard!\n");
|
---|
153 | return -1;
|
---|
154 | }
|
---|
155 |
|
---|
156 | if(!al_install_mouse()) {
|
---|
157 | fprintf(stderr, "failed to initialize the mouse!\n");
|
---|
158 | return -1;
|
---|
159 | }
|
---|
160 |
|
---|
161 | timer = al_create_timer(1.0 / FPS);
|
---|
162 | if(!timer) {
|
---|
163 | fprintf(stderr, "failed to create timer!\n");
|
---|
164 | return -1;
|
---|
165 | }
|
---|
166 |
|
---|
167 | int refreshRate = getRefreshRate(SCREEN_W, SCREEN_H);
|
---|
168 | // if the computer doesn't support this resolution, just use windowed mode
|
---|
169 | if (refreshRate > 0 && fullscreen) {
|
---|
170 | al_set_new_display_flags(ALLEGRO_FULLSCREEN);
|
---|
171 | al_set_new_display_refresh_rate(refreshRate);
|
---|
172 | }
|
---|
173 | display = al_create_display(SCREEN_W, SCREEN_H);
|
---|
174 | if(!display) {
|
---|
175 | fprintf(stderr, "failed to create display!\n");
|
---|
176 | al_destroy_timer(timer);
|
---|
177 | return -1;
|
---|
178 | }
|
---|
179 |
|
---|
180 | WorldMap* gameMap = WorldMap::loadMapFromFile("../../data/map.txt");
|
---|
181 |
|
---|
182 | wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
183 | wndLogin->addComponent(new Textbox(516, 40, 100, 20, font));
|
---|
184 | wndLogin->addComponent(new Textbox(516, 70, 100, 20, font));
|
---|
185 | wndLogin->addComponent(new Button(330, 100, 194, 20, font, "Create an Account", goToRegisterScreen));
|
---|
186 | wndLogin->addComponent(new Button(534, 100, 60, 20, font, "Login", login));
|
---|
187 | wndLogin->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit));
|
---|
188 |
|
---|
189 | txtUsername = (Textbox*)wndLogin->getComponent(0);
|
---|
190 | txtPassword = (Textbox*)wndLogin->getComponent(1);
|
---|
191 |
|
---|
192 | wndRegister = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
193 | wndRegister->addComponent(new Textbox(516, 40, 100, 20, font));
|
---|
194 | wndRegister->addComponent(new Textbox(516, 70, 100, 20, font));
|
---|
195 | wndRegister->addComponent(new Button(468, 100, 56, 20, font, "Back", goToLoginScreen));
|
---|
196 | wndRegister->addComponent(new Button(534, 100, 70, 20, font, "Submit", registerAccount));
|
---|
197 | wndRegister->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit));
|
---|
198 | wndRegister->addComponent(new RadioButtonList(432, 130, "Pick a class", font));
|
---|
199 |
|
---|
200 | txtUsernameRegister = (Textbox*)wndRegister->getComponent(0);
|
---|
201 | txtPasswordRegister = (Textbox*)wndRegister->getComponent(1);
|
---|
202 |
|
---|
203 | rblClasses = (RadioButtonList*)wndRegister->getComponent(5);
|
---|
204 | rblClasses->addRadioButton("Warrior");
|
---|
205 | rblClasses->addRadioButton("Ranger");
|
---|
206 |
|
---|
207 | wndMain = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
208 | wndMain->addComponent(new Textbox(95, 40, 300, 20, font));
|
---|
209 | wndMain->addComponent(new Button(95, 70, 60, 20, font, "Send", sendChatMessage));
|
---|
210 | wndMain->addComponent(new Button(920, 10, 80, 20, font, "Logout", logout));
|
---|
211 |
|
---|
212 | txtChat = (Textbox*)wndMain->getComponent(0);
|
---|
213 |
|
---|
214 | wndCurrent = wndLogin;
|
---|
215 |
|
---|
216 | event_queue = al_create_event_queue();
|
---|
217 | if(!event_queue) {
|
---|
218 | fprintf(stderr, "failed to create event_queue!\n");
|
---|
219 | al_destroy_display(display);
|
---|
220 | al_destroy_timer(timer);
|
---|
221 | return -1;
|
---|
222 | }
|
---|
223 |
|
---|
224 | al_set_target_bitmap(al_get_backbuffer(display));
|
---|
225 |
|
---|
226 | al_register_event_source(event_queue, al_get_display_event_source(display));
|
---|
227 | al_register_event_source(event_queue, al_get_timer_event_source(timer));
|
---|
228 | al_register_event_source(event_queue, al_get_keyboard_event_source());
|
---|
229 | al_register_event_source(event_queue, al_get_mouse_event_source());
|
---|
230 |
|
---|
231 | al_clear_to_color(al_map_rgb(0,0,0));
|
---|
232 |
|
---|
233 | al_flip_display();
|
---|
234 |
|
---|
235 | if (argc != 3) {
|
---|
236 | cout << "Usage: server port" << endl;
|
---|
237 | exit(1);
|
---|
238 | }
|
---|
239 |
|
---|
240 | initWinSock();
|
---|
241 |
|
---|
242 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
243 | if (sock < 0)
|
---|
244 | error("socket");
|
---|
245 |
|
---|
246 | set_nonblock(sock);
|
---|
247 |
|
---|
248 | server.sin_family = AF_INET;
|
---|
249 | hp = gethostbyname(argv[1]);
|
---|
250 | if (hp==0)
|
---|
251 | error("Unknown host");
|
---|
252 |
|
---|
253 | memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
|
---|
254 | server.sin_port = htons(atoi(argv[2]));
|
---|
255 |
|
---|
256 | al_start_timer(timer);
|
---|
257 |
|
---|
258 | while(!doexit)
|
---|
259 | {
|
---|
260 | ALLEGRO_EVENT ev;
|
---|
261 |
|
---|
262 | al_wait_for_event(event_queue, &ev);
|
---|
263 |
|
---|
264 | if(wndCurrent->handleEvent(ev)) {
|
---|
265 | // do nothing
|
---|
266 | }
|
---|
267 | else if(ev.type == ALLEGRO_EVENT_TIMER) {
|
---|
268 | redraw = true; // seems like we should just call a draw function here instead
|
---|
269 | }
|
---|
270 | else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
|
---|
271 | doexit = true;
|
---|
272 | }
|
---|
273 | else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
|
---|
274 | }
|
---|
275 | else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
|
---|
276 | switch(ev.keyboard.keycode) {
|
---|
277 | case ALLEGRO_KEY_ESCAPE:
|
---|
278 | doexit = true;
|
---|
279 | break;
|
---|
280 | case ALLEGRO_KEY_S: // pickup an item next to you
|
---|
281 | if (state == STATE_LOGIN) {
|
---|
282 | msgTo.type = MSG_TYPE_PICKUP_FLAG;
|
---|
283 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
284 | msgProcessor.sendMessage(&msgTo, sock, &server);
|
---|
285 | }
|
---|
286 | break;
|
---|
287 | case ALLEGRO_KEY_D: // drop the current item
|
---|
288 | if (state == STATE_LOGIN) {
|
---|
289 | // find the current player in the player list
|
---|
290 | map<unsigned int, Player>::iterator it;
|
---|
291 | Player* p = NULL;
|
---|
292 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
293 | {
|
---|
294 | if (it->second.id == curPlayerId)
|
---|
295 | p = &it->second;
|
---|
296 | }
|
---|
297 |
|
---|
298 | if (p != NULL) {
|
---|
299 | int flagType = WorldMap::OBJECT_NONE;
|
---|
300 |
|
---|
301 | if (p->hasBlueFlag)
|
---|
302 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
303 | else if (p->hasRedFlag)
|
---|
304 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
305 |
|
---|
306 | if (flagType != WorldMap::OBJECT_NONE) {
|
---|
307 | msgTo.type = MSG_TYPE_DROP_FLAG;
|
---|
308 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
309 | msgProcessor.sendMessage(&msgTo, sock, &server);
|
---|
310 | }
|
---|
311 | }
|
---|
312 | }
|
---|
313 | break;
|
---|
314 | }
|
---|
315 | }
|
---|
316 | else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
|
---|
317 | if(wndCurrent == wndMain) {
|
---|
318 | if (ev.mouse.button == 1) { // left click
|
---|
319 | msgTo.type = MSG_TYPE_PLAYER_MOVE;
|
---|
320 |
|
---|
321 | POSITION pos;
|
---|
322 | pos.x = ev.mouse.x;
|
---|
323 | pos.y = ev.mouse.y;
|
---|
324 | pos = screenToMap(pos);
|
---|
325 |
|
---|
326 | if (pos.x != -1)
|
---|
327 | {
|
---|
328 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
329 | memcpy(msgTo.buffer+4, &pos.x, 4);
|
---|
330 | memcpy(msgTo.buffer+8, &pos.y, 4);
|
---|
331 |
|
---|
332 | msgProcessor.sendMessage(&msgTo, sock, &server);
|
---|
333 | }
|
---|
334 | else
|
---|
335 | cout << "Invalid point: User did not click on the map" << endl;
|
---|
336 | }else if (ev.mouse.button == 2) { // right click
|
---|
337 | map<unsigned int, Player>::iterator it;
|
---|
338 |
|
---|
339 | cout << "Detected a right-click" << endl;
|
---|
340 |
|
---|
341 | Player* curPlayer;
|
---|
342 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
343 | {
|
---|
344 | if (it->second.id == curPlayerId)
|
---|
345 | curPlayer = &it->second;
|
---|
346 | }
|
---|
347 |
|
---|
348 | Player* target;
|
---|
349 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
350 | {
|
---|
351 | // need to check if the right-click was actually on this player
|
---|
352 | // right now, this code will target all players other than the current one
|
---|
353 | target = &it->second;
|
---|
354 | if (target->id != curPlayerId && target->team != curPlayer->team) {
|
---|
355 | msgTo.type = MSG_TYPE_START_ATTACK;
|
---|
356 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
357 | memcpy(msgTo.buffer+4, &target->id, 4);
|
---|
358 |
|
---|
359 | msgProcessor.sendMessage(&msgTo, sock, &server);
|
---|
360 | }
|
---|
361 | }
|
---|
362 | }
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | if (msgProcessor.receiveMessage(&msgFrom, sock, &from) >= 0)
|
---|
367 | processMessage(msgFrom, state, chatConsole, gameMap, mapPlayers, mapProjectiles, curPlayerId, scoreBlue, scoreRed);
|
---|
368 |
|
---|
369 | if (redraw)
|
---|
370 | {
|
---|
371 | redraw = false;
|
---|
372 |
|
---|
373 | msgProcessor.resendUnackedMessages(sock);
|
---|
374 |
|
---|
375 | wndCurrent->draw(display);
|
---|
376 |
|
---|
377 | chatConsole.draw(font, al_map_rgb(255,255,255));
|
---|
378 |
|
---|
379 | // There should be label gui components that show these or each textbox should have a label
|
---|
380 | if(wndCurrent == wndLogin || wndCurrent == wndRegister) {
|
---|
381 | al_draw_text(font, al_map_rgb(0, 255, 0), 416, 43, ALLEGRO_ALIGN_LEFT, "Username:");
|
---|
382 | al_draw_text(font, al_map_rgb(0, 255, 0), 413, 73, ALLEGRO_ALIGN_LEFT, "Password:");
|
---|
383 | }
|
---|
384 | else if(wndCurrent == wndMain) {
|
---|
385 | al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
|
---|
386 |
|
---|
387 | ostringstream ossScoreBlue, ossScoreRed;
|
---|
388 |
|
---|
389 | ossScoreBlue << "Blue: " << scoreBlue << endl;
|
---|
390 | ossScoreRed << "Red: " << scoreRed << endl;
|
---|
391 |
|
---|
392 | al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
|
---|
393 | al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
|
---|
394 |
|
---|
395 | // update players
|
---|
396 | map<unsigned int, Player>::iterator it;
|
---|
397 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
398 | {
|
---|
399 | it->second.updateTarget(mapPlayers);
|
---|
400 | }
|
---|
401 |
|
---|
402 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
403 | {
|
---|
404 | it->second.move(gameMap); // ignore return value
|
---|
405 | }
|
---|
406 |
|
---|
407 | // update projectile positions
|
---|
408 | map<unsigned int, Projectile>::iterator it2;
|
---|
409 | for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
|
---|
410 | {
|
---|
411 | it2->second.move(mapPlayers);
|
---|
412 | }
|
---|
413 |
|
---|
414 | drawMap(gameMap);
|
---|
415 | drawPlayers(mapPlayers, font, curPlayerId);
|
---|
416 |
|
---|
417 | // draw projectiles
|
---|
418 | for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
|
---|
419 | {
|
---|
420 | Projectile proj = it2->second;
|
---|
421 |
|
---|
422 | FLOAT_POSITION target = mapPlayers[proj.target].pos;
|
---|
423 | float angle = atan2(target.y-proj.pos.toFloat().y, target.x-proj.pos.toFloat().x);
|
---|
424 |
|
---|
425 | POSITION start, end;
|
---|
426 | start.x = cos(angle)*15+proj.pos.x;
|
---|
427 | start.y = sin(angle)*15+proj.pos.y;
|
---|
428 | end.x = proj.pos.x;
|
---|
429 | end.y = proj.pos.y;
|
---|
430 |
|
---|
431 | start = mapToScreen(start);
|
---|
432 | end = mapToScreen(end);
|
---|
433 |
|
---|
434 | al_draw_line(start.x, start.y, end.x, end.y, al_map_rgb(0, 0, 0), 4);
|
---|
435 | }
|
---|
436 | }
|
---|
437 |
|
---|
438 | al_flip_display();
|
---|
439 | }
|
---|
440 | }
|
---|
441 |
|
---|
442 | #if defined WINDOWS
|
---|
443 | closesocket(sock);
|
---|
444 | #elif defined LINUX
|
---|
445 | close(sock);
|
---|
446 | #endif
|
---|
447 |
|
---|
448 | shutdownWinSock();
|
---|
449 |
|
---|
450 | delete wndLogin;
|
---|
451 | delete wndMain;
|
---|
452 |
|
---|
453 | delete gameMap;
|
---|
454 |
|
---|
455 | al_destroy_event_queue(event_queue);
|
---|
456 | al_destroy_display(display);
|
---|
457 | al_destroy_timer(timer);
|
---|
458 |
|
---|
459 | return 0;
|
---|
460 | }
|
---|
461 |
|
---|
462 |
|
---|
463 |
|
---|
464 | // need to make a function like this that works on windows
|
---|
465 | void error(const char *msg)
|
---|
466 | {
|
---|
467 | perror(msg);
|
---|
468 | shutdownWinSock();
|
---|
469 | exit(1);
|
---|
470 | }
|
---|
471 |
|
---|
472 | void initWinSock()
|
---|
473 | {
|
---|
474 | #if defined WINDOWS
|
---|
475 | WORD wVersionRequested;
|
---|
476 | WSADATA wsaData;
|
---|
477 | int wsaerr;
|
---|
478 |
|
---|
479 | wVersionRequested = MAKEWORD(2, 2);
|
---|
480 | wsaerr = WSAStartup(wVersionRequested, &wsaData);
|
---|
481 |
|
---|
482 | if (wsaerr != 0) {
|
---|
483 | cout << "The Winsock dll not found." << endl;
|
---|
484 | exit(1);
|
---|
485 | }else
|
---|
486 | cout << "The Winsock dll was found." << endl;
|
---|
487 | #endif
|
---|
488 | }
|
---|
489 |
|
---|
490 | void shutdownWinSock()
|
---|
491 | {
|
---|
492 | #if defined WINDOWS
|
---|
493 | WSACleanup();
|
---|
494 | #endif
|
---|
495 | }
|
---|
496 |
|
---|
497 | POSITION screenToMap(POSITION pos)
|
---|
498 | {
|
---|
499 | pos.x = pos.x-300;
|
---|
500 | pos.y = pos.y-100;
|
---|
501 |
|
---|
502 | if (pos.x < 0 || pos.y < 0)
|
---|
503 | {
|
---|
504 | pos.x = -1;
|
---|
505 | pos.y = -1;
|
---|
506 | }
|
---|
507 |
|
---|
508 | return pos;
|
---|
509 | }
|
---|
510 |
|
---|
511 | POSITION mapToScreen(POSITION pos)
|
---|
512 | {
|
---|
513 | pos.x = pos.x+300;
|
---|
514 | pos.y = pos.y+100;
|
---|
515 |
|
---|
516 | return pos;
|
---|
517 | }
|
---|
518 |
|
---|
519 | POSITION mapToScreen(FLOAT_POSITION pos)
|
---|
520 | {
|
---|
521 | POSITION p;
|
---|
522 | p.x = pos.x+300;
|
---|
523 | p.y = pos.y+100;
|
---|
524 |
|
---|
525 | return p;
|
---|
526 | }
|
---|
527 |
|
---|
528 | void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, WorldMap *gameMap, map<unsigned int, Player>& mapPlayers, map<unsigned int, Projectile>& mapProjectiles, unsigned int& curPlayerId, int &scoreBlue, int &scoreRed)
|
---|
529 | {
|
---|
530 | string response = string(msg.buffer);
|
---|
531 |
|
---|
532 | switch(state)
|
---|
533 | {
|
---|
534 | case STATE_START:
|
---|
535 | {
|
---|
536 | cout << "In STATE_START" << endl;
|
---|
537 |
|
---|
538 | switch(msg.type)
|
---|
539 | {
|
---|
540 | case MSG_TYPE_REGISTER:
|
---|
541 | {
|
---|
542 | break;
|
---|
543 | }
|
---|
544 | case MSG_TYPE_LOGIN:
|
---|
545 | {
|
---|
546 | if (response.compare("Player has already logged in.") == 0)
|
---|
547 | {
|
---|
548 | username.clear();
|
---|
549 | cout << "User login failed" << endl;
|
---|
550 | }
|
---|
551 | else if (response.compare("Incorrect username or password") == 0)
|
---|
552 | {
|
---|
553 | username.clear();
|
---|
554 | cout << "User login failed" << endl;
|
---|
555 | }
|
---|
556 | else
|
---|
557 | {
|
---|
558 | state = STATE_LOGIN;
|
---|
559 | wndCurrent = wndMain;
|
---|
560 |
|
---|
561 | Player p("", "");
|
---|
562 | p.deserialize(msg.buffer);
|
---|
563 | mapPlayers[p.id] = p;
|
---|
564 | curPlayerId = p.id;
|
---|
565 |
|
---|
566 | cout << "Got a valid login response with the player" << endl;
|
---|
567 | cout << "Player id: " << curPlayerId << endl;
|
---|
568 | cout << "Player health: " << p.health << endl;
|
---|
569 | cout << "map size: " << mapPlayers.size() << endl;
|
---|
570 | }
|
---|
571 |
|
---|
572 | break;
|
---|
573 | }
|
---|
574 | case MSG_TYPE_PLAYER: // kind of hacky to put this here
|
---|
575 | {
|
---|
576 | Player p("", "");
|
---|
577 | p.deserialize(msg.buffer);
|
---|
578 | p.timeLastUpdated = getCurrentMillis();
|
---|
579 |
|
---|
580 | mapPlayers[p.id] = p;
|
---|
581 |
|
---|
582 | break;
|
---|
583 | }
|
---|
584 | case MSG_TYPE_OBJECT:
|
---|
585 | {
|
---|
586 | WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
|
---|
587 | o.deserialize(msg.buffer);
|
---|
588 | cout << "object id: " << o.id << endl;
|
---|
589 | gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
|
---|
590 |
|
---|
591 | break;
|
---|
592 | }
|
---|
593 | }
|
---|
594 |
|
---|
595 | break;
|
---|
596 | }
|
---|
597 | case STATE_LOGIN:
|
---|
598 | {
|
---|
599 | switch(msg.type)
|
---|
600 | {
|
---|
601 | case MSG_TYPE_REGISTER:
|
---|
602 | {
|
---|
603 | break;
|
---|
604 | }
|
---|
605 | case MSG_TYPE_LOGIN:
|
---|
606 | {
|
---|
607 | cout << "Got a login message" << endl;
|
---|
608 |
|
---|
609 | chatConsole.addLine(response);
|
---|
610 | cout << "Added new line" << endl;
|
---|
611 |
|
---|
612 | break;
|
---|
613 | }
|
---|
614 | case MSG_TYPE_LOGOUT:
|
---|
615 | {
|
---|
616 | cout << "Got a logout message" << endl;
|
---|
617 |
|
---|
618 | if (response.compare("You have successfully logged out.") == 0)
|
---|
619 | {
|
---|
620 | cout << "Logged out" << endl;
|
---|
621 | state = STATE_START;
|
---|
622 | wndCurrent = wndLogin;
|
---|
623 | }
|
---|
624 |
|
---|
625 | break;
|
---|
626 | }
|
---|
627 | case MSG_TYPE_PLAYER:
|
---|
628 | {
|
---|
629 | cout << "Received MSG_TYPE_PLAYER" << endl;
|
---|
630 |
|
---|
631 | Player p("", "");
|
---|
632 | p.deserialize(msg.buffer);
|
---|
633 | p.timeLastUpdated = getCurrentMillis();
|
---|
634 | p.isChasing = false;
|
---|
635 | if (p.health <= 0)
|
---|
636 | p.isDead = true;
|
---|
637 | else
|
---|
638 | p.isDead = false;
|
---|
639 |
|
---|
640 | cout << mapPlayers[p.id].pos.x << ", " << mapPlayers[p.id].pos.y << endl;
|
---|
641 | cout << "health:" << mapPlayers[p.id].health << endl;
|
---|
642 | cout << "is dead?:" << mapPlayers[p.id].isDead << endl;
|
---|
643 | mapPlayers[p.id] = p;
|
---|
644 | cout << mapPlayers[p.id].pos.x << ", " << mapPlayers[p.id].pos.y << endl;
|
---|
645 | cout << "health:" << mapPlayers[p.id].health << endl;
|
---|
646 | cout << "is dead?:" << mapPlayers[p.id].isDead << endl;
|
---|
647 |
|
---|
648 | break;
|
---|
649 | }
|
---|
650 | case MSG_TYPE_PLAYER_MOVE:
|
---|
651 | {
|
---|
652 | unsigned int id;
|
---|
653 | int x, y;
|
---|
654 |
|
---|
655 | memcpy(&id, msg.buffer, 4);
|
---|
656 | memcpy(&x, msg.buffer+4, 4);
|
---|
657 | memcpy(&y, msg.buffer+8, 4);
|
---|
658 |
|
---|
659 | mapPlayers[id].target.x = x;
|
---|
660 | mapPlayers[id].target.y = y;
|
---|
661 |
|
---|
662 | break;
|
---|
663 | }
|
---|
664 | case MSG_TYPE_CHAT:
|
---|
665 | {
|
---|
666 | chatConsole.addLine(response);
|
---|
667 |
|
---|
668 | break;
|
---|
669 | }
|
---|
670 | case MSG_TYPE_OBJECT:
|
---|
671 | {
|
---|
672 | cout << "Received object message in STATE_LOGIN." << endl;
|
---|
673 |
|
---|
674 | WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
|
---|
675 | o.deserialize(msg.buffer);
|
---|
676 | gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
|
---|
677 |
|
---|
678 | break;
|
---|
679 | }
|
---|
680 | case MSG_TYPE_REMOVE_OBJECT:
|
---|
681 | {
|
---|
682 | cout << "Received REMOVE_OBJECT message!" << endl;
|
---|
683 |
|
---|
684 | int id;
|
---|
685 | memcpy(&id, msg.buffer, 4);
|
---|
686 |
|
---|
687 | cout << "Removing object with id " << id << endl;
|
---|
688 |
|
---|
689 | if (!gameMap->removeObject(id))
|
---|
690 | cout << "Did not remove the object" << endl;
|
---|
691 |
|
---|
692 | break;
|
---|
693 | }
|
---|
694 | case MSG_TYPE_SCORE:
|
---|
695 | {
|
---|
696 | memcpy(&scoreBlue, msg.buffer, 4);
|
---|
697 | memcpy(&scoreRed, msg.buffer+4, 4);
|
---|
698 |
|
---|
699 | break;
|
---|
700 | }
|
---|
701 | case MSG_TYPE_ATTACK:
|
---|
702 | {
|
---|
703 | cout << "Received ATTACK message" << endl;
|
---|
704 |
|
---|
705 | break;
|
---|
706 | }
|
---|
707 | case MSG_TYPE_START_ATTACK:
|
---|
708 | {
|
---|
709 | cout << "Received START_ATTACK message" << endl;
|
---|
710 |
|
---|
711 | unsigned int id, targetID;
|
---|
712 | memcpy(&id, msg.buffer, 4);
|
---|
713 | memcpy(&targetID, msg.buffer+4, 4);
|
---|
714 |
|
---|
715 | cout << "source id: " << id << endl;
|
---|
716 | cout << "target id: " << targetID << endl;
|
---|
717 |
|
---|
718 | Player* source = &mapPlayers[id];
|
---|
719 | source->targetPlayer = targetID;
|
---|
720 | source->isChasing = true;
|
---|
721 |
|
---|
722 | break;
|
---|
723 | }
|
---|
724 | case MSG_TYPE_PROJECTILE:
|
---|
725 | {
|
---|
726 | cout << "Received a PROJECTILE message" << endl;
|
---|
727 |
|
---|
728 | int id, x, y, targetId;
|
---|
729 |
|
---|
730 | memcpy(&id, msg.buffer, 4);
|
---|
731 | memcpy(&x, msg.buffer+4, 4);
|
---|
732 | memcpy(&y, msg.buffer+8, 4);
|
---|
733 | memcpy(&targetId, msg.buffer+12, 4);
|
---|
734 |
|
---|
735 | cout << "id: " << id << endl;
|
---|
736 | cout << "x: " << x << endl;
|
---|
737 | cout << "y: " << y << endl;
|
---|
738 | cout << "Target: " << targetId << endl;
|
---|
739 |
|
---|
740 | Projectile proj(x, y, targetId, 0);
|
---|
741 | proj.setId(id);
|
---|
742 |
|
---|
743 | mapProjectiles[id] = proj;
|
---|
744 |
|
---|
745 | break;
|
---|
746 | }
|
---|
747 | case MSG_TYPE_REMOVE_PROJECTILE:
|
---|
748 | {
|
---|
749 | cout << "Received a REMOVE_PROJECTILE message" << endl;
|
---|
750 |
|
---|
751 | int id;
|
---|
752 |
|
---|
753 | memcpy(&id, msg.buffer, 4);
|
---|
754 |
|
---|
755 | mapProjectiles.erase(id);
|
---|
756 |
|
---|
757 | break;
|
---|
758 | }
|
---|
759 | default:
|
---|
760 | {
|
---|
761 | cout << "Received an unexpected message type: " << msg.type << endl;
|
---|
762 | }
|
---|
763 | }
|
---|
764 |
|
---|
765 | break;
|
---|
766 | }
|
---|
767 | default:
|
---|
768 | {
|
---|
769 | cout << "The state has an invalid value: " << state << endl;
|
---|
770 |
|
---|
771 | break;
|
---|
772 | }
|
---|
773 | }
|
---|
774 | }
|
---|
775 |
|
---|
776 | // this should probably be in the WorldMap class
|
---|
777 | void drawMap(WorldMap* gameMap)
|
---|
778 | {
|
---|
779 | POSITION mapPos;
|
---|
780 | mapPos.x = 0;
|
---|
781 | mapPos.y = 0;
|
---|
782 | mapPos = mapToScreen(mapPos);
|
---|
783 |
|
---|
784 | for (int x=0; x<gameMap->width; x++)
|
---|
785 | {
|
---|
786 | for (int y=0; y<gameMap->height; y++)
|
---|
787 | {
|
---|
788 | WorldMap::TerrainType el = gameMap->getElement(x, y);
|
---|
789 | WorldMap::StructureType structure = gameMap->getStructure(x, y);
|
---|
790 |
|
---|
791 | if (el == WorldMap::TERRAIN_GRASS)
|
---|
792 | al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(0, 255, 0));
|
---|
793 | else if (el == WorldMap::TERRAIN_OCEAN)
|
---|
794 | al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(0, 0, 255));
|
---|
795 | else if (el == WorldMap::TERRAIN_ROCK)
|
---|
796 | al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(100, 100, 0));
|
---|
797 |
|
---|
798 | if (structure == WorldMap::STRUCTURE_BLUE_FLAG) {
|
---|
799 | al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
|
---|
800 | //al_draw_filled_rectangle(x*25+5+mapPos.x, y*25+5+mapPos.y, x*25+20+mapPos.x, y*25+20+mapPos.y, al_map_rgb(0, 0, 255));
|
---|
801 | }else if (structure == WorldMap::STRUCTURE_RED_FLAG) {
|
---|
802 | al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
|
---|
803 | //al_draw_filled_rectangle(x*25+5+mapPos.x, y*25+5+mapPos.y, x*25+20+mapPos.x, y*25+20+mapPos.y, al_map_rgb(255, 0, 0));
|
---|
804 | }
|
---|
805 | }
|
---|
806 | }
|
---|
807 |
|
---|
808 | for (int x=0; x<gameMap->width; x++)
|
---|
809 | {
|
---|
810 | for (int y=0; y<gameMap->height; y++)
|
---|
811 | {
|
---|
812 | vector<WorldMap::Object> vctObjects = gameMap->getObjects(x, y);
|
---|
813 |
|
---|
814 | vector<WorldMap::Object>::iterator it;
|
---|
815 | for(it = vctObjects.begin(); it != vctObjects.end(); it++) {
|
---|
816 | switch(it->type) {
|
---|
817 | case WorldMap::OBJECT_BLUE_FLAG:
|
---|
818 | al_draw_filled_rectangle(it->pos.x-8+mapPos.x, it->pos.y-8+mapPos.y, it->pos.x+8+mapPos.x, it->pos.y+8+mapPos.y, al_map_rgb(0, 0, 255));
|
---|
819 | break;
|
---|
820 | case WorldMap::OBJECT_RED_FLAG:
|
---|
821 | al_draw_filled_rectangle(it->pos.x-8+mapPos.x, it->pos.y-8+mapPos.y, it->pos.x+8+mapPos.x, it->pos.y+8+mapPos.y, al_map_rgb(255, 0, 0));
|
---|
822 | break;
|
---|
823 | }
|
---|
824 | }
|
---|
825 | }
|
---|
826 | }
|
---|
827 | }
|
---|
828 |
|
---|
829 | void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId)
|
---|
830 | {
|
---|
831 | map<unsigned int, Player>::iterator it;
|
---|
832 |
|
---|
833 | Player* p;
|
---|
834 | POSITION pos;
|
---|
835 | ALLEGRO_COLOR color;
|
---|
836 |
|
---|
837 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
838 | {
|
---|
839 | p = &it->second;
|
---|
840 |
|
---|
841 | if (p->isDead)
|
---|
842 | continue;
|
---|
843 |
|
---|
844 | pos = mapToScreen(p->pos);
|
---|
845 |
|
---|
846 | if (p->id == curPlayerId)
|
---|
847 | al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));
|
---|
848 |
|
---|
849 | if (p->team == 0)
|
---|
850 | color = al_map_rgb(0, 0, 255);
|
---|
851 | else if (p->team == 1)
|
---|
852 | color = al_map_rgb(255, 0, 0);
|
---|
853 |
|
---|
854 | al_draw_filled_circle(pos.x, pos.y, 12, color);
|
---|
855 |
|
---|
856 | // draw player class
|
---|
857 | int fontHeight = al_get_font_line_height(font);
|
---|
858 |
|
---|
859 | string strClass;
|
---|
860 | switch (p->playerClass) {
|
---|
861 | case Player::CLASS_WARRIOR:
|
---|
862 | strClass = "W";
|
---|
863 | break;
|
---|
864 | case Player::CLASS_RANGER:
|
---|
865 | strClass = "R";
|
---|
866 | break;
|
---|
867 | case Player::CLASS_NONE:
|
---|
868 | strClass = "";
|
---|
869 | break;
|
---|
870 | default:
|
---|
871 | strClass = "";
|
---|
872 | break;
|
---|
873 | }
|
---|
874 | al_draw_text(font, al_map_rgb(0, 0, 0), pos.x, pos.y-fontHeight/2, ALLEGRO_ALIGN_CENTRE, strClass.c_str());
|
---|
875 |
|
---|
876 | // draw player health
|
---|
877 | al_draw_filled_rectangle(pos.x-12, pos.y-24, pos.x+12, pos.y-16, al_map_rgb(0, 0, 0));
|
---|
878 | if (it->second.maxHealth != 0)
|
---|
879 | al_draw_filled_rectangle(pos.x-11, pos.y-23, pos.x-11+(22*it->second.health)/it->second.maxHealth, pos.y-17, al_map_rgb(255, 0, 0));
|
---|
880 |
|
---|
881 | if (p->hasBlueFlag)
|
---|
882 | al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(0, 0, 255));
|
---|
883 | else if (p->hasRedFlag)
|
---|
884 | al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(255, 0, 0));
|
---|
885 | }
|
---|
886 | }
|
---|
887 |
|
---|
888 | void goToRegisterScreen()
|
---|
889 | {
|
---|
890 | wndCurrent = wndRegister;
|
---|
891 |
|
---|
892 | txtUsernameRegister->clear();
|
---|
893 | txtPasswordRegister->clear();
|
---|
894 | }
|
---|
895 |
|
---|
896 | void goToLoginScreen()
|
---|
897 | {
|
---|
898 | wndCurrent = wndLogin;
|
---|
899 |
|
---|
900 | txtUsername->clear();
|
---|
901 | txtPassword->clear();
|
---|
902 | }
|
---|
903 |
|
---|
904 | void registerAccount()
|
---|
905 | {
|
---|
906 | string username = txtUsernameRegister->getStr();
|
---|
907 | string password = txtPasswordRegister->getStr();
|
---|
908 |
|
---|
909 | txtUsernameRegister->clear();
|
---|
910 | txtPasswordRegister->clear();
|
---|
911 | // maybe clear rblClasses as well (add a method to RadioButtonList to enable this)
|
---|
912 |
|
---|
913 | Player::PlayerClass playerClass;
|
---|
914 |
|
---|
915 | switch (rblClasses->getSelectedButton()) {
|
---|
916 | case 0:
|
---|
917 | playerClass = Player::CLASS_WARRIOR;
|
---|
918 | break;
|
---|
919 | case 1:
|
---|
920 | playerClass = Player::CLASS_RANGER;
|
---|
921 | break;
|
---|
922 | default:
|
---|
923 | cout << "Invalid class selection" << endl;
|
---|
924 | playerClass = Player::CLASS_NONE;
|
---|
925 | break;
|
---|
926 | }
|
---|
927 |
|
---|
928 | msgTo.type = MSG_TYPE_REGISTER;
|
---|
929 |
|
---|
930 | strcpy(msgTo.buffer, username.c_str());
|
---|
931 | strcpy(msgTo.buffer+username.size()+1, password.c_str());
|
---|
932 | memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
|
---|
933 |
|
---|
934 | msgProcessor.sendMessage(&msgTo, sock, &server);
|
---|
935 | }
|
---|
936 |
|
---|
937 | void login()
|
---|
938 | {
|
---|
939 | string strUsername = txtUsername->getStr();
|
---|
940 | string strPassword = txtPassword->getStr();
|
---|
941 | username = strUsername;
|
---|
942 |
|
---|
943 | txtUsername->clear();
|
---|
944 | txtPassword->clear();
|
---|
945 |
|
---|
946 | msgTo.type = MSG_TYPE_LOGIN;
|
---|
947 |
|
---|
948 | strcpy(msgTo.buffer, strUsername.c_str());
|
---|
949 | strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
|
---|
950 |
|
---|
951 | msgProcessor.sendMessage(&msgTo, sock, &server);
|
---|
952 | }
|
---|
953 |
|
---|
954 | void logout()
|
---|
955 | {
|
---|
956 | txtChat->clear();
|
---|
957 |
|
---|
958 | msgTo.type = MSG_TYPE_LOGOUT;
|
---|
959 |
|
---|
960 | strcpy(msgTo.buffer, username.c_str());
|
---|
961 |
|
---|
962 | msgProcessor.sendMessage(&msgTo, sock, &server);
|
---|
963 | }
|
---|
964 |
|
---|
965 | void quit()
|
---|
966 | {
|
---|
967 | doexit = true;
|
---|
968 | }
|
---|
969 |
|
---|
970 | void sendChatMessage()
|
---|
971 | {
|
---|
972 | string msg = txtChat->getStr();
|
---|
973 |
|
---|
974 | txtChat->clear();
|
---|
975 |
|
---|
976 | msgTo.type = MSG_TYPE_CHAT;
|
---|
977 |
|
---|
978 | strcpy(msgTo.buffer, msg.c_str());
|
---|
979 |
|
---|
980 | msgProcessor.sendMessage(&msgTo, sock, &server);
|
---|
981 | }
|
---|
982 |
|
---|
983 | int getRefreshRate(int width, int height) {
|
---|
984 | int numRefreshRates = al_get_num_display_modes();
|
---|
985 | ALLEGRO_DISPLAY_MODE displayMode;
|
---|
986 |
|
---|
987 | for(int i=0; i<numRefreshRates; i++) {
|
---|
988 | al_get_display_mode(i, &displayMode);
|
---|
989 |
|
---|
990 | if (displayMode.width == width && displayMode.height == height)
|
---|
991 | return displayMode.refresh_rate;
|
---|
992 | }
|
---|
993 |
|
---|
994 | return 0;
|
---|
995 | }
|
---|