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 <sys/types.h>
|
---|
16 | #include <cstdio>
|
---|
17 | #include <cstdlib>
|
---|
18 | #include <string>
|
---|
19 | #include <iostream>
|
---|
20 | #include <sstream>
|
---|
21 |
|
---|
22 | #include <map>
|
---|
23 |
|
---|
24 | #include <map>
|
---|
25 |
|
---|
26 | #include <allegro5/allegro.h>
|
---|
27 | #include <allegro5/allegro_font.h>
|
---|
28 | #include <allegro5/allegro_ttf.h>
|
---|
29 | #include <allegro5/allegro_primitives.h>
|
---|
30 |
|
---|
31 | #include "../../common/Message.h"
|
---|
32 | #include "../../common/Common.h"
|
---|
33 | #include "../../common/WorldMap.h"
|
---|
34 | #include "../../common/Player.h"
|
---|
35 |
|
---|
36 | #include "Window.h"
|
---|
37 | #include "Textbox.h"
|
---|
38 | #include "Button.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, 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 |
|
---|
55 | // callbacks
|
---|
56 | void registerAccount();
|
---|
57 | void login();
|
---|
58 | void logout();
|
---|
59 | void quit();
|
---|
60 | void sendChatMessage();
|
---|
61 |
|
---|
62 | void error(const char *);
|
---|
63 |
|
---|
64 | const float FPS = 60;
|
---|
65 | const int SCREEN_W = 640;
|
---|
66 | const int SCREEN_H = 480;
|
---|
67 |
|
---|
68 | enum STATE {
|
---|
69 | STATE_START,
|
---|
70 | STATE_LOGIN // this means you're already logged in
|
---|
71 | };
|
---|
72 |
|
---|
73 | int state;
|
---|
74 |
|
---|
75 | bool doexit;
|
---|
76 |
|
---|
77 | Window* wndLogin;
|
---|
78 | Window* wndMain;
|
---|
79 | Window* wndCurrent;
|
---|
80 |
|
---|
81 | Textbox* txtUsername;
|
---|
82 | Textbox* txtPassword;
|
---|
83 | Textbox* txtChat;
|
---|
84 |
|
---|
85 | int sock;
|
---|
86 | struct sockaddr_in server, from;
|
---|
87 | struct hostent *hp;
|
---|
88 | NETWORK_MSG msgTo, msgFrom;
|
---|
89 | string username;
|
---|
90 | chat chatConsole;
|
---|
91 |
|
---|
92 | int main(int argc, char **argv)
|
---|
93 | {
|
---|
94 | ALLEGRO_DISPLAY *display = NULL;
|
---|
95 | ALLEGRO_EVENT_QUEUE *event_queue = NULL;
|
---|
96 | ALLEGRO_TIMER *timer = NULL;
|
---|
97 | bool key[4] = { false, false, false, false };
|
---|
98 | bool redraw = true;
|
---|
99 | doexit = false;
|
---|
100 | map<unsigned int, Player> mapPlayers;
|
---|
101 | unsigned int curPlayerId = -1;
|
---|
102 | int scoreBlue, scoreRed;
|
---|
103 |
|
---|
104 | scoreBlue = 0;
|
---|
105 | scoreRed = 0;
|
---|
106 |
|
---|
107 | state = STATE_START;
|
---|
108 |
|
---|
109 | if(!al_init()) {
|
---|
110 | fprintf(stderr, "failed to initialize allegro!\n");
|
---|
111 | return -1;
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (al_init_primitives_addon())
|
---|
115 | cout << "Primitives initialized" << endl;
|
---|
116 | else
|
---|
117 | cout << "Primitives not initialized" << endl;
|
---|
118 |
|
---|
119 | al_init_font_addon();
|
---|
120 | al_init_ttf_addon();
|
---|
121 |
|
---|
122 | #if defined WINDOWS
|
---|
123 | ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0);
|
---|
124 | #elif defined LINUX
|
---|
125 | ALLEGRO_FONT *font = al_load_ttf_font("pirulen.ttf", 12, 0);
|
---|
126 | #endif
|
---|
127 |
|
---|
128 | if (!font) {
|
---|
129 | fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
|
---|
130 | getchar();
|
---|
131 | return -1;
|
---|
132 | }
|
---|
133 |
|
---|
134 | if(!al_install_keyboard()) {
|
---|
135 | fprintf(stderr, "failed to initialize the keyboard!\n");
|
---|
136 | return -1;
|
---|
137 | }
|
---|
138 |
|
---|
139 | if(!al_install_mouse()) {
|
---|
140 | fprintf(stderr, "failed to initialize the mouse!\n");
|
---|
141 | return -1;
|
---|
142 | }
|
---|
143 |
|
---|
144 | timer = al_create_timer(1.0 / FPS);
|
---|
145 | if(!timer) {
|
---|
146 | fprintf(stderr, "failed to create timer!\n");
|
---|
147 | return -1;
|
---|
148 | }
|
---|
149 |
|
---|
150 | display = al_create_display(SCREEN_W, SCREEN_H);
|
---|
151 | if(!display) {
|
---|
152 | fprintf(stderr, "failed to create display!\n");
|
---|
153 | al_destroy_timer(timer);
|
---|
154 | return -1;
|
---|
155 | }
|
---|
156 |
|
---|
157 | WorldMap* gameMap = WorldMap::loadMapFromFile("../../data/map.txt");
|
---|
158 |
|
---|
159 | wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
160 | wndLogin->addComponent(new Textbox(104, 40, 100, 20, font));
|
---|
161 | wndLogin->addComponent(new Textbox(104, 70, 100, 20, font));
|
---|
162 | wndLogin->addComponent(new Button(22, 100, 90, 20, font, "Register", registerAccount));
|
---|
163 | wndLogin->addComponent(new Button(122, 100, 60, 20, font, "Login", login));
|
---|
164 | wndLogin->addComponent(new Button(540, 10, 80, 20, font, "Quit", quit));
|
---|
165 |
|
---|
166 | txtUsername = (Textbox*)wndLogin->getComponent(0);
|
---|
167 | txtPassword = (Textbox*)wndLogin->getComponent(1);
|
---|
168 |
|
---|
169 | wndMain = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
170 | wndMain->addComponent(new Textbox(95, 40, 525, 20, font));
|
---|
171 | wndMain->addComponent(new Button(95, 70, 160, 20, font, "Send Message", sendChatMessage));
|
---|
172 | wndMain->addComponent(new Button(540, 10, 80, 20, font, "Logout", logout));
|
---|
173 |
|
---|
174 | txtChat = (Textbox*)wndMain->getComponent(0);
|
---|
175 |
|
---|
176 | wndCurrent = wndLogin;
|
---|
177 |
|
---|
178 | event_queue = al_create_event_queue();
|
---|
179 | if(!event_queue) {
|
---|
180 | fprintf(stderr, "failed to create event_queue!\n");
|
---|
181 | al_destroy_display(display);
|
---|
182 | al_destroy_timer(timer);
|
---|
183 | return -1;
|
---|
184 | }
|
---|
185 |
|
---|
186 | al_set_target_bitmap(al_get_backbuffer(display));
|
---|
187 |
|
---|
188 | al_register_event_source(event_queue, al_get_display_event_source(display));
|
---|
189 | al_register_event_source(event_queue, al_get_timer_event_source(timer));
|
---|
190 | al_register_event_source(event_queue, al_get_keyboard_event_source());
|
---|
191 | al_register_event_source(event_queue, al_get_mouse_event_source());
|
---|
192 |
|
---|
193 | al_clear_to_color(al_map_rgb(0,0,0));
|
---|
194 |
|
---|
195 | al_flip_display();
|
---|
196 |
|
---|
197 | if (argc != 3) {
|
---|
198 | cout << "Usage: server port" << endl;
|
---|
199 | exit(1);
|
---|
200 | }
|
---|
201 |
|
---|
202 | initWinSock();
|
---|
203 |
|
---|
204 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
205 | if (sock < 0)
|
---|
206 | error("socket");
|
---|
207 |
|
---|
208 | set_nonblock(sock);
|
---|
209 |
|
---|
210 | server.sin_family = AF_INET;
|
---|
211 | hp = gethostbyname(argv[1]);
|
---|
212 | if (hp==0)
|
---|
213 | error("Unknown host");
|
---|
214 |
|
---|
215 | memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
|
---|
216 | server.sin_port = htons(atoi(argv[2]));
|
---|
217 |
|
---|
218 | al_start_timer(timer);
|
---|
219 |
|
---|
220 | while(!doexit)
|
---|
221 | {
|
---|
222 | ALLEGRO_EVENT ev;
|
---|
223 |
|
---|
224 | al_wait_for_event(event_queue, &ev);
|
---|
225 |
|
---|
226 | if(wndCurrent->handleEvent(ev)) {
|
---|
227 | // do nothing
|
---|
228 | }
|
---|
229 | else if(ev.type == ALLEGRO_EVENT_TIMER) {
|
---|
230 | redraw = true; // seems like we should just call a draw function here instead
|
---|
231 | }
|
---|
232 | else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
|
---|
233 | doexit = true;
|
---|
234 | }
|
---|
235 | else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
|
---|
236 | }
|
---|
237 | else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
|
---|
238 | switch(ev.keyboard.keycode) {
|
---|
239 | case ALLEGRO_KEY_ESCAPE:
|
---|
240 | doexit = true;
|
---|
241 | break;
|
---|
242 | case ALLEGRO_KEY_S: // pickup an item next to you
|
---|
243 | if (state == STATE_LOGIN) {
|
---|
244 | msgTo.type = MSG_TYPE_PICKUP_FLAG;
|
---|
245 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
246 | sendMessage(&msgTo, sock, &server);
|
---|
247 | }
|
---|
248 | break;
|
---|
249 | case ALLEGRO_KEY_D: // drop the current item
|
---|
250 | if (state == STATE_LOGIN) {
|
---|
251 | // find the current player in the player list
|
---|
252 | map<unsigned int, Player>::iterator it;
|
---|
253 | Player* p = NULL;
|
---|
254 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
255 | {
|
---|
256 | if (it->second.id == curPlayerId)
|
---|
257 | p = &it->second;
|
---|
258 | }
|
---|
259 |
|
---|
260 | if (p != NULL) {
|
---|
261 | int flagType = WorldMap::OBJECT_NONE;
|
---|
262 |
|
---|
263 | if (p->hasBlueFlag)
|
---|
264 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
265 | else if (p->hasRedFlag)
|
---|
266 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
267 |
|
---|
268 | if (flagType != WorldMap::OBJECT_NONE) {
|
---|
269 | msgTo.type = MSG_TYPE_DROP_FLAG;
|
---|
270 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
271 | sendMessage(&msgTo, sock, &server);
|
---|
272 | }
|
---|
273 | }
|
---|
274 | }
|
---|
275 | break;
|
---|
276 | }
|
---|
277 | }
|
---|
278 | else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
|
---|
279 | if(wndCurrent == wndMain) {
|
---|
280 | if (ev.mouse.button == 1) { // left click
|
---|
281 | msgTo.type = MSG_TYPE_PLAYER_MOVE;
|
---|
282 |
|
---|
283 | POSITION pos;
|
---|
284 | pos.x = ev.mouse.x;
|
---|
285 | pos.y = ev.mouse.y;
|
---|
286 | pos = screenToMap(pos);
|
---|
287 |
|
---|
288 | if (pos.x != -1)
|
---|
289 | {
|
---|
290 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
291 | memcpy(msgTo.buffer+4, &pos.x, 4);
|
---|
292 | memcpy(msgTo.buffer+8, &pos.y, 4);
|
---|
293 |
|
---|
294 | sendMessage(&msgTo, sock, &server);
|
---|
295 | }
|
---|
296 | else
|
---|
297 | cout << "Invalid point: User did not click on the map" << endl;
|
---|
298 | }else if (ev.mouse.button == 2) { // right click
|
---|
299 | map<unsigned int, Player>::iterator it;
|
---|
300 |
|
---|
301 | Player* curPlayer;
|
---|
302 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
303 | {
|
---|
304 | if (it->second.id == curPlayerId)
|
---|
305 | curPlayer = &it->second;
|
---|
306 | }
|
---|
307 |
|
---|
308 | Player* target;
|
---|
309 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
310 | {
|
---|
311 | target = &it->second;
|
---|
312 | if (target->id != curPlayerId && target->team != curPlayer->team) {
|
---|
313 | msgTo.type = MSG_TYPE_START_ATTACK;
|
---|
314 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
315 | memcpy(msgTo.buffer+4, &target->id, 4);
|
---|
316 |
|
---|
317 | sendMessage(&msgTo, sock, &server);
|
---|
318 | }
|
---|
319 | }
|
---|
320 | }
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | if (receiveMessage(&msgFrom, sock, &from) >= 0)
|
---|
325 | processMessage(msgFrom, state, chatConsole, gameMap, mapPlayers, curPlayerId, scoreBlue, scoreRed);
|
---|
326 |
|
---|
327 | if (redraw)
|
---|
328 | {
|
---|
329 | redraw = false;
|
---|
330 |
|
---|
331 | wndCurrent->draw(display);
|
---|
332 |
|
---|
333 | chatConsole.draw(font, al_map_rgb(255,255,255));
|
---|
334 |
|
---|
335 | if(wndCurrent == wndLogin) {
|
---|
336 | al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Username:");
|
---|
337 | al_draw_text(font, al_map_rgb(0, 255, 0), 1, 73, ALLEGRO_ALIGN_LEFT, "Password:");
|
---|
338 | }
|
---|
339 | else if(wndCurrent == wndMain) {
|
---|
340 | al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
|
---|
341 |
|
---|
342 | ostringstream ossScoreBlue, ossScoreRed;
|
---|
343 |
|
---|
344 | ossScoreBlue << "Blue: " << scoreBlue << endl;
|
---|
345 | ossScoreRed << "Red: " << scoreRed << endl;
|
---|
346 |
|
---|
347 | al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
|
---|
348 | al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
|
---|
349 |
|
---|
350 | // update player positions
|
---|
351 | map<unsigned int, Player>::iterator it;
|
---|
352 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
353 | {
|
---|
354 | it->second.move(gameMap); // ignore return value
|
---|
355 | }
|
---|
356 |
|
---|
357 | drawMap(gameMap);
|
---|
358 | drawPlayers(mapPlayers, font, curPlayerId);
|
---|
359 | }
|
---|
360 |
|
---|
361 | al_flip_display();
|
---|
362 | }
|
---|
363 | }
|
---|
364 |
|
---|
365 | #if defined WINDOWS
|
---|
366 | closesocket(sock);
|
---|
367 | #elif defined LINUX
|
---|
368 | close(sock);
|
---|
369 | #endif
|
---|
370 |
|
---|
371 | shutdownWinSock();
|
---|
372 |
|
---|
373 | delete wndLogin;
|
---|
374 | delete wndMain;
|
---|
375 |
|
---|
376 | delete gameMap;
|
---|
377 |
|
---|
378 | al_destroy_event_queue(event_queue);
|
---|
379 | al_destroy_display(display);
|
---|
380 | al_destroy_timer(timer);
|
---|
381 |
|
---|
382 | return 0;
|
---|
383 | }
|
---|
384 |
|
---|
385 | // need to make a function like this that works on windows
|
---|
386 | void error(const char *msg)
|
---|
387 | {
|
---|
388 | perror(msg);
|
---|
389 | shutdownWinSock();
|
---|
390 | exit(1);
|
---|
391 | }
|
---|
392 |
|
---|
393 | void initWinSock()
|
---|
394 | {
|
---|
395 | #if defined WINDOWS
|
---|
396 | WORD wVersionRequested;
|
---|
397 | WSADATA wsaData;
|
---|
398 | int wsaerr;
|
---|
399 |
|
---|
400 | wVersionRequested = MAKEWORD(2, 2);
|
---|
401 | wsaerr = WSAStartup(wVersionRequested, &wsaData);
|
---|
402 |
|
---|
403 | if (wsaerr != 0) {
|
---|
404 | cout << "The Winsock dll not found." << endl;
|
---|
405 | exit(1);
|
---|
406 | }else
|
---|
407 | cout << "The Winsock dll was found." << endl;
|
---|
408 | #endif
|
---|
409 | }
|
---|
410 |
|
---|
411 | void shutdownWinSock()
|
---|
412 | {
|
---|
413 | #if defined WINDOWS
|
---|
414 | WSACleanup();
|
---|
415 | #endif
|
---|
416 | }
|
---|
417 |
|
---|
418 | POSITION screenToMap(POSITION pos)
|
---|
419 | {
|
---|
420 | pos.x = pos.x-300;
|
---|
421 | pos.y = pos.y-100;
|
---|
422 |
|
---|
423 | if (pos.x < 0 || pos.y < 0)
|
---|
424 | {
|
---|
425 | pos.x = -1;
|
---|
426 | pos.y = -1;
|
---|
427 | }
|
---|
428 |
|
---|
429 | return pos;
|
---|
430 | }
|
---|
431 |
|
---|
432 | POSITION mapToScreen(POSITION pos)
|
---|
433 | {
|
---|
434 | pos.x = pos.x+300;
|
---|
435 | pos.y = pos.y+100;
|
---|
436 |
|
---|
437 | return pos;
|
---|
438 | }
|
---|
439 |
|
---|
440 | POSITION mapToScreen(FLOAT_POSITION pos)
|
---|
441 | {
|
---|
442 | POSITION p;
|
---|
443 | p.x = pos.x+300;
|
---|
444 | p.y = pos.y+100;
|
---|
445 |
|
---|
446 | return p;
|
---|
447 | }
|
---|
448 |
|
---|
449 | void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, WorldMap *gameMap, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId, int &scoreBlue, int &scoreRed)
|
---|
450 | {
|
---|
451 | string response = string(msg.buffer);
|
---|
452 |
|
---|
453 | cout << "Processing message" << endl;
|
---|
454 |
|
---|
455 | switch(state)
|
---|
456 | {
|
---|
457 | case STATE_START:
|
---|
458 | {
|
---|
459 | cout << "In STATE_START" << endl;
|
---|
460 |
|
---|
461 | switch(msg.type)
|
---|
462 | {
|
---|
463 | case MSG_TYPE_REGISTER:
|
---|
464 | {
|
---|
465 | break;
|
---|
466 | }
|
---|
467 | case MSG_TYPE_LOGIN:
|
---|
468 | {
|
---|
469 | if (response.compare("Player has already logged in.") == 0)
|
---|
470 | {
|
---|
471 | username.clear();
|
---|
472 | cout << "User login failed" << endl;
|
---|
473 | }
|
---|
474 | else if (response.compare("Incorrect username or password") == 0)
|
---|
475 | {
|
---|
476 | username.clear();
|
---|
477 | cout << "User login failed" << endl;
|
---|
478 | }
|
---|
479 | else
|
---|
480 | {
|
---|
481 | state = STATE_LOGIN;
|
---|
482 | wndCurrent = wndMain;
|
---|
483 |
|
---|
484 | Player p("", "");
|
---|
485 | p.deserialize(msg.buffer);
|
---|
486 | mapPlayers[p.id] = p;
|
---|
487 | curPlayerId = p.id;
|
---|
488 |
|
---|
489 | cout << "Got a valid login response with the player" << endl;
|
---|
490 | cout << "Player id: " << curPlayerId << endl;
|
---|
491 | cout << "map size: " << mapPlayers.size() << endl;
|
---|
492 | }
|
---|
493 |
|
---|
494 | break;
|
---|
495 | }
|
---|
496 | case MSG_TYPE_PLAYER: // kind of hacky to put this here
|
---|
497 | {
|
---|
498 | cout << "Got MSG_TYPE_PLAYER message in STATE_START" << endl;
|
---|
499 |
|
---|
500 | Player p("", "");
|
---|
501 | p.deserialize(msg.buffer);
|
---|
502 | p.timeLastUpdated = getCurrentMillis();
|
---|
503 | mapPlayers[p.id] = p;
|
---|
504 |
|
---|
505 | cout << "new player id: " << p.id << endl;
|
---|
506 | cout << "map size: " << mapPlayers.size() << endl;
|
---|
507 |
|
---|
508 | break;
|
---|
509 | }
|
---|
510 | case MSG_TYPE_OBJECT:
|
---|
511 | {
|
---|
512 | cout << "Received OBJECT message in STATE_START." << endl;
|
---|
513 |
|
---|
514 | WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
|
---|
515 | o.deserialize(msg.buffer);
|
---|
516 | cout << "object id: " << o.id << endl;
|
---|
517 | gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
|
---|
518 |
|
---|
519 | break;
|
---|
520 | }
|
---|
521 | }
|
---|
522 |
|
---|
523 | break;
|
---|
524 | }
|
---|
525 | case STATE_LOGIN:
|
---|
526 | {
|
---|
527 | switch(msg.type)
|
---|
528 | {
|
---|
529 | case MSG_TYPE_REGISTER:
|
---|
530 | {
|
---|
531 | break;
|
---|
532 | }
|
---|
533 | case MSG_TYPE_LOGIN:
|
---|
534 | {
|
---|
535 | cout << "Got a login message" << endl;
|
---|
536 |
|
---|
537 | chatConsole.addLine(response);
|
---|
538 | cout << "Added new line" << endl;
|
---|
539 |
|
---|
540 | break;
|
---|
541 | }
|
---|
542 | case MSG_TYPE_LOGOUT:
|
---|
543 | {
|
---|
544 | cout << "Got a logout message" << endl;
|
---|
545 |
|
---|
546 | if (response.compare("You have successfully logged out.") == 0)
|
---|
547 | {
|
---|
548 | cout << "Logged out" << endl;
|
---|
549 | state = STATE_START;
|
---|
550 | wndCurrent = wndLogin;
|
---|
551 | }
|
---|
552 |
|
---|
553 | break;
|
---|
554 | }
|
---|
555 | case MSG_TYPE_PLAYER:
|
---|
556 | {
|
---|
557 | cout << "Got MSG_TYPE_PLAYER message in STATE_LOGIN" << endl;
|
---|
558 |
|
---|
559 | Player p("", "");
|
---|
560 | p.deserialize(msg.buffer);
|
---|
561 | p.timeLastUpdated = getCurrentMillis();
|
---|
562 | mapPlayers[p.id] = p;
|
---|
563 |
|
---|
564 | break;
|
---|
565 | }
|
---|
566 | case MSG_TYPE_PLAYER_MOVE:
|
---|
567 | {
|
---|
568 | cout << "Got a player move message" << endl;
|
---|
569 |
|
---|
570 | unsigned int id;
|
---|
571 | int x, y;
|
---|
572 |
|
---|
573 | memcpy(&id, msg.buffer, 4);
|
---|
574 | memcpy(&x, msg.buffer+4, 4);
|
---|
575 | memcpy(&y, msg.buffer+8, 4);
|
---|
576 |
|
---|
577 | mapPlayers[id].target.x = x;
|
---|
578 | mapPlayers[id].target.y = y;
|
---|
579 |
|
---|
580 | break;
|
---|
581 | }
|
---|
582 | case MSG_TYPE_CHAT:
|
---|
583 | {
|
---|
584 | chatConsole.addLine(response);
|
---|
585 |
|
---|
586 | break;
|
---|
587 | }
|
---|
588 | case MSG_TYPE_OBJECT:
|
---|
589 | {
|
---|
590 | cout << "Received object message in STATE_LOGIN." << endl;
|
---|
591 |
|
---|
592 | WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
|
---|
593 | o.deserialize(msg.buffer);
|
---|
594 | gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
|
---|
595 |
|
---|
596 | break;
|
---|
597 | }
|
---|
598 | case MSG_TYPE_REMOVE_OBJECT:
|
---|
599 | {
|
---|
600 | cout << "Received REMOVE_OBJECT message!" << endl;
|
---|
601 |
|
---|
602 | int id;
|
---|
603 | memcpy(&id, msg.buffer, 4);
|
---|
604 |
|
---|
605 | cout << "Removing object with id " << id << endl;
|
---|
606 |
|
---|
607 | if (!gameMap->removeObject(id))
|
---|
608 | cout << "Did not remove the object" << endl;
|
---|
609 |
|
---|
610 | break;
|
---|
611 | }
|
---|
612 | case MSG_TYPE_SCORE:
|
---|
613 | {
|
---|
614 | memcpy(&scoreBlue, msg.buffer, 4);
|
---|
615 | memcpy(&scoreRed, msg.buffer+4, 4);
|
---|
616 |
|
---|
617 | break;
|
---|
618 | }
|
---|
619 | default:
|
---|
620 | {
|
---|
621 | cout << "Received an unexpected message type: " << msg.type << endl;
|
---|
622 | }
|
---|
623 | }
|
---|
624 |
|
---|
625 | break;
|
---|
626 | }
|
---|
627 | default:
|
---|
628 | {
|
---|
629 | cout << "The state has an invalid value: " << state << endl;
|
---|
630 |
|
---|
631 | break;
|
---|
632 | }
|
---|
633 | }
|
---|
634 | }
|
---|
635 |
|
---|
636 | // this should probably be in the WorldMap class
|
---|
637 | void drawMap(WorldMap* gameMap)
|
---|
638 | {
|
---|
639 | POSITION mapPos;
|
---|
640 | mapPos.x = 0;
|
---|
641 | mapPos.y = 0;
|
---|
642 | mapPos = mapToScreen(mapPos);
|
---|
643 |
|
---|
644 | for (int x=0; x<12; x++)
|
---|
645 | {
|
---|
646 | for (int y=0; y<12; y++)
|
---|
647 | {
|
---|
648 | WorldMap::TerrainType el = gameMap->getElement(x, y);
|
---|
649 | WorldMap::StructureType structure = gameMap->getStructure(x, y);
|
---|
650 |
|
---|
651 | if (el == WorldMap::TERRAIN_GRASS)
|
---|
652 | 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));
|
---|
653 | else if (el == WorldMap::TERRAIN_OCEAN)
|
---|
654 | 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));
|
---|
655 | else if (el == WorldMap::TERRAIN_ROCK)
|
---|
656 | 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));
|
---|
657 |
|
---|
658 | if (structure == WorldMap::STRUCTURE_BLUE_FLAG) {
|
---|
659 | al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
|
---|
660 | //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));
|
---|
661 | }else if (structure == WorldMap::STRUCTURE_RED_FLAG) {
|
---|
662 | al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
|
---|
663 | //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));
|
---|
664 | }
|
---|
665 | }
|
---|
666 | }
|
---|
667 |
|
---|
668 | for (int x=0; x<12; x++)
|
---|
669 | {
|
---|
670 | for (int y=0; y<12; y++)
|
---|
671 | {
|
---|
672 | vector<WorldMap::Object> vctObjects = gameMap->getObjects(x, y);
|
---|
673 |
|
---|
674 | vector<WorldMap::Object>::iterator it;
|
---|
675 | for(it = vctObjects.begin(); it != vctObjects.end(); it++) {
|
---|
676 | switch(it->type) {
|
---|
677 | case WorldMap::OBJECT_BLUE_FLAG:
|
---|
678 | 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));
|
---|
679 | break;
|
---|
680 | case WorldMap::OBJECT_RED_FLAG:
|
---|
681 | 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));
|
---|
682 | break;
|
---|
683 | }
|
---|
684 | }
|
---|
685 | }
|
---|
686 | }
|
---|
687 | }
|
---|
688 |
|
---|
689 | void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId)
|
---|
690 | {
|
---|
691 | map<unsigned int, Player>::iterator it;
|
---|
692 |
|
---|
693 | Player* p;
|
---|
694 | POSITION pos;
|
---|
695 | ALLEGRO_COLOR color;
|
---|
696 |
|
---|
697 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
698 | {
|
---|
699 | p = &it->second;
|
---|
700 | pos = mapToScreen(p->pos);
|
---|
701 |
|
---|
702 | if (p->id == curPlayerId)
|
---|
703 | al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));
|
---|
704 |
|
---|
705 | if (p->team == 0)
|
---|
706 | color = al_map_rgb(0, 0, 255);
|
---|
707 | else if (p->team == 1)
|
---|
708 | color = al_map_rgb(255, 0, 0);
|
---|
709 |
|
---|
710 | al_draw_filled_circle(pos.x, pos.y, 12, color);
|
---|
711 |
|
---|
712 | // draw player class
|
---|
713 | int fontHeight = al_get_font_line_height(font);
|
---|
714 |
|
---|
715 | string strClass;
|
---|
716 | switch (p->playerClass) {
|
---|
717 | case Player::CLASS_WARRIOR:
|
---|
718 | strClass = "W";
|
---|
719 | break;
|
---|
720 | case Player::CLASS_RANGER:
|
---|
721 | strClass = "R";
|
---|
722 | break;
|
---|
723 | case Player::CLASS_NONE:
|
---|
724 | strClass = "";
|
---|
725 | break;
|
---|
726 | default:
|
---|
727 | strClass = "";
|
---|
728 | break;
|
---|
729 | }
|
---|
730 | al_draw_text(font, al_map_rgb(0, 0, 0), pos.x, pos.y-fontHeight/2, ALLEGRO_ALIGN_CENTRE, strClass.c_str());
|
---|
731 |
|
---|
732 | // draw player health
|
---|
733 | al_draw_filled_rectangle(pos.x-12, pos.y-24, pos.x+12, pos.y-16, al_map_rgb(0, 0, 0));
|
---|
734 | if (it->second.maxHealth != 0)
|
---|
735 | 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));
|
---|
736 |
|
---|
737 | if (p->hasBlueFlag)
|
---|
738 | al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(0, 0, 255));
|
---|
739 | else if (p->hasRedFlag)
|
---|
740 | al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(255, 0, 0));
|
---|
741 | }
|
---|
742 | }
|
---|
743 |
|
---|
744 | void registerAccount()
|
---|
745 | {
|
---|
746 | string username = txtUsername->getStr();
|
---|
747 | string password = txtPassword->getStr();
|
---|
748 |
|
---|
749 | txtUsername->clear();
|
---|
750 | txtPassword->clear();
|
---|
751 |
|
---|
752 | msgTo.type = MSG_TYPE_REGISTER;
|
---|
753 |
|
---|
754 | strcpy(msgTo.buffer, username.c_str());
|
---|
755 | strcpy(msgTo.buffer+username.size()+1, password.c_str());
|
---|
756 |
|
---|
757 | sendMessage(&msgTo, sock, &server);
|
---|
758 | }
|
---|
759 |
|
---|
760 | void login()
|
---|
761 | {
|
---|
762 | string strUsername = txtUsername->getStr();
|
---|
763 | string strPassword = txtPassword->getStr();
|
---|
764 | username = strUsername;
|
---|
765 |
|
---|
766 | txtUsername->clear();
|
---|
767 | txtPassword->clear();
|
---|
768 |
|
---|
769 | msgTo.type = MSG_TYPE_LOGIN;
|
---|
770 |
|
---|
771 | strcpy(msgTo.buffer, strUsername.c_str());
|
---|
772 | strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
|
---|
773 |
|
---|
774 | sendMessage(&msgTo, sock, &server);
|
---|
775 | }
|
---|
776 |
|
---|
777 | void logout()
|
---|
778 | {
|
---|
779 | txtChat->clear();
|
---|
780 |
|
---|
781 | msgTo.type = MSG_TYPE_LOGOUT;
|
---|
782 |
|
---|
783 | strcpy(msgTo.buffer, username.c_str());
|
---|
784 |
|
---|
785 | sendMessage(&msgTo, sock, &server);
|
---|
786 | }
|
---|
787 |
|
---|
788 | void quit()
|
---|
789 | {
|
---|
790 | doexit = true;
|
---|
791 | }
|
---|
792 |
|
---|
793 | void sendChatMessage()
|
---|
794 | {
|
---|
795 | string msg = txtChat->getStr();
|
---|
796 |
|
---|
797 | txtChat->clear();
|
---|
798 |
|
---|
799 | msgTo.type = MSG_TYPE_CHAT;
|
---|
800 |
|
---|
801 | strcpy(msgTo.buffer, msg.c_str());
|
---|
802 |
|
---|
803 | sendMessage(&msgTo, sock, &server);
|
---|
804 | }
|
---|