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