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