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