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 | delete game;
|
---|
552 |
|
---|
553 | al_destroy_event_queue(event_queue);
|
---|
554 | al_destroy_display(display);
|
---|
555 | al_destroy_timer(timer);
|
---|
556 |
|
---|
557 | outputLog << "Stopped client on " << getCurrentDateTimeString() << endl;
|
---|
558 | outputLog.close();
|
---|
559 |
|
---|
560 | return 0;
|
---|
561 | }
|
---|
562 |
|
---|
563 |
|
---|
564 |
|
---|
565 | // need to make a function like this that works on windows
|
---|
566 | void error(const char *msg)
|
---|
567 | {
|
---|
568 | perror(msg);
|
---|
569 | shutdownWinSock();
|
---|
570 | exit(1);
|
---|
571 | }
|
---|
572 |
|
---|
573 | void initWinSock()
|
---|
574 | {
|
---|
575 | #if defined WINDOWS
|
---|
576 | WORD wVersionRequested;
|
---|
577 | WSADATA wsaData;
|
---|
578 | int wsaerr;
|
---|
579 |
|
---|
580 | wVersionRequested = MAKEWORD(2, 2);
|
---|
581 | wsaerr = WSAStartup(wVersionRequested, &wsaData);
|
---|
582 |
|
---|
583 | if (wsaerr != 0) {
|
---|
584 | cout << "The Winsock dll not found." << endl;
|
---|
585 | exit(1);
|
---|
586 | }else
|
---|
587 | cout << "The Winsock dll was found." << endl;
|
---|
588 | #endif
|
---|
589 | }
|
---|
590 |
|
---|
591 | void shutdownWinSock()
|
---|
592 | {
|
---|
593 | #if defined WINDOWS
|
---|
594 | WSACleanup();
|
---|
595 | #endif
|
---|
596 | }
|
---|
597 |
|
---|
598 | POSITION screenToMap(POSITION pos)
|
---|
599 | {
|
---|
600 | pos.x = pos.x-300;
|
---|
601 | pos.y = pos.y-100;
|
---|
602 |
|
---|
603 | if (pos.x < 0 || pos.y < 0)
|
---|
604 | {
|
---|
605 | pos.x = -1;
|
---|
606 | pos.y = -1;
|
---|
607 | }
|
---|
608 |
|
---|
609 | return pos;
|
---|
610 | }
|
---|
611 |
|
---|
612 | POSITION mapToScreen(POSITION pos)
|
---|
613 | {
|
---|
614 | pos.x = pos.x+300;
|
---|
615 | pos.y = pos.y+100;
|
---|
616 |
|
---|
617 | return pos;
|
---|
618 | }
|
---|
619 |
|
---|
620 | POSITION mapToScreen(FLOAT_POSITION pos)
|
---|
621 | {
|
---|
622 | POSITION p;
|
---|
623 | p.x = pos.x+300;
|
---|
624 | p.y = pos.y+100;
|
---|
625 |
|
---|
626 | return p;
|
---|
627 | }
|
---|
628 |
|
---|
629 | 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)
|
---|
630 | {
|
---|
631 | string response = string(msg.buffer);
|
---|
632 |
|
---|
633 | switch(state)
|
---|
634 | {
|
---|
635 | case STATE_START:
|
---|
636 | {
|
---|
637 | cout << "In STATE_START" << endl;
|
---|
638 |
|
---|
639 | switch(msg.type)
|
---|
640 | {
|
---|
641 | case MSG_TYPE_REGISTER:
|
---|
642 | {
|
---|
643 | lblRegisterStatus->setText(response);
|
---|
644 | break;
|
---|
645 | }
|
---|
646 | default:
|
---|
647 | {
|
---|
648 | cout << "(STATE_REGISTER) Received invalid message of type " << msg.type << endl;
|
---|
649 | break;
|
---|
650 | }
|
---|
651 | }
|
---|
652 |
|
---|
653 | break;
|
---|
654 | }
|
---|
655 | case STATE_LOBBY:
|
---|
656 | case STATE_GAME:
|
---|
657 | {
|
---|
658 | switch(msg.type)
|
---|
659 | {
|
---|
660 | case MSG_TYPE_LOGIN:
|
---|
661 | {
|
---|
662 | if (response.compare("Player has already logged in.") == 0)
|
---|
663 | {
|
---|
664 | goToLoginScreen();
|
---|
665 | state = STATE_START;
|
---|
666 |
|
---|
667 | lblLoginStatus->setText(response);
|
---|
668 | }
|
---|
669 | else if (response.compare("Incorrect username or password") == 0)
|
---|
670 | {
|
---|
671 | goToLoginScreen();
|
---|
672 | state = STATE_START;
|
---|
673 |
|
---|
674 | lblLoginStatus->setText(response);
|
---|
675 | }
|
---|
676 | else
|
---|
677 | {
|
---|
678 | wndCurrent = wndLobby;
|
---|
679 |
|
---|
680 | Player p("", "");
|
---|
681 | p.deserialize(msg.buffer);
|
---|
682 | mapPlayers[p.id] = p;
|
---|
683 | curPlayerId = p.id;
|
---|
684 |
|
---|
685 | cout << "Got a valid login response with the player" << endl;
|
---|
686 | cout << "Player id: " << curPlayerId << endl;
|
---|
687 | cout << "Player health: " << p.health << endl;
|
---|
688 | cout << "player map size: " << mapPlayers.size() << endl;
|
---|
689 | }
|
---|
690 |
|
---|
691 | break;
|
---|
692 | }
|
---|
693 | case MSG_TYPE_LOGOUT:
|
---|
694 | {
|
---|
695 | cout << "Got a logout message" << endl;
|
---|
696 |
|
---|
697 | if (response.compare("You have successfully logged out.") == 0)
|
---|
698 | {
|
---|
699 | cout << "Logged out" << endl;
|
---|
700 | state = STATE_START;
|
---|
701 | goToLoginScreen();
|
---|
702 | }
|
---|
703 |
|
---|
704 | break;
|
---|
705 | }
|
---|
706 | case MSG_TYPE_PLAYER:
|
---|
707 | {
|
---|
708 | cout << "Received MSG_TYPE_PLAYER" << endl;
|
---|
709 |
|
---|
710 | Player p("", "");
|
---|
711 | p.deserialize(msg.buffer);
|
---|
712 | p.timeLastUpdated = getCurrentMillis();
|
---|
713 | p.isChasing = false;
|
---|
714 | if (p.health <= 0)
|
---|
715 | p.isDead = true;
|
---|
716 | else
|
---|
717 | p.isDead = false;
|
---|
718 |
|
---|
719 | mapPlayers[p.id] = p;
|
---|
720 |
|
---|
721 | break;
|
---|
722 | }
|
---|
723 | case MSG_TYPE_PLAYER_MOVE:
|
---|
724 | {
|
---|
725 | unsigned int id;
|
---|
726 | int x, y;
|
---|
727 |
|
---|
728 | memcpy(&id, msg.buffer, 4);
|
---|
729 | memcpy(&x, msg.buffer+4, 4);
|
---|
730 | memcpy(&y, msg.buffer+8, 4);
|
---|
731 |
|
---|
732 | mapPlayers[id].target.x = x;
|
---|
733 | mapPlayers[id].target.y = y;
|
---|
734 |
|
---|
735 | break;
|
---|
736 | }
|
---|
737 | case MSG_TYPE_CHAT:
|
---|
738 | {
|
---|
739 | chatConsole.addLine(response);
|
---|
740 |
|
---|
741 | break;
|
---|
742 | }
|
---|
743 | case MSG_TYPE_OBJECT:
|
---|
744 | {
|
---|
745 | cout << "Received object message in STATE_LOBBY." << endl;
|
---|
746 |
|
---|
747 | WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
|
---|
748 | o.deserialize(msg.buffer);
|
---|
749 | cout << "object id: " << o.id << endl;
|
---|
750 | gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
|
---|
751 |
|
---|
752 | break;
|
---|
753 | }
|
---|
754 | case MSG_TYPE_REMOVE_OBJECT:
|
---|
755 | {
|
---|
756 | cout << "Received REMOVE_OBJECT message!" << endl;
|
---|
757 |
|
---|
758 | int id;
|
---|
759 | memcpy(&id, msg.buffer, 4);
|
---|
760 |
|
---|
761 | cout << "Removing object with id " << id << endl;
|
---|
762 |
|
---|
763 | if (!gameMap->removeObject(id))
|
---|
764 | cout << "Did not remove the object" << endl;
|
---|
765 |
|
---|
766 | break;
|
---|
767 | }
|
---|
768 | case MSG_TYPE_SCORE:
|
---|
769 | {
|
---|
770 | memcpy(&scoreBlue, msg.buffer, 4);
|
---|
771 | memcpy(&scoreRed, msg.buffer+4, 4);
|
---|
772 |
|
---|
773 | break;
|
---|
774 | }
|
---|
775 | case MSG_TYPE_ATTACK:
|
---|
776 | {
|
---|
777 | cout << "Received ATTACK message" << endl;
|
---|
778 |
|
---|
779 | break;
|
---|
780 | }
|
---|
781 | case MSG_TYPE_START_ATTACK:
|
---|
782 | {
|
---|
783 | cout << "Received START_ATTACK message" << endl;
|
---|
784 |
|
---|
785 | unsigned int id, targetID;
|
---|
786 | memcpy(&id, msg.buffer, 4);
|
---|
787 | memcpy(&targetID, msg.buffer+4, 4);
|
---|
788 |
|
---|
789 | cout << "source id: " << id << endl;
|
---|
790 | cout << "target id: " << targetID << endl;
|
---|
791 |
|
---|
792 | Player* source = &mapPlayers[id];
|
---|
793 | source->targetPlayer = targetID;
|
---|
794 | source->isChasing = true;
|
---|
795 |
|
---|
796 | break;
|
---|
797 | }
|
---|
798 | case MSG_TYPE_PROJECTILE:
|
---|
799 | {
|
---|
800 | cout << "Received a PROJECTILE message" << endl;
|
---|
801 |
|
---|
802 | unsigned int id, x, y, targetId;
|
---|
803 |
|
---|
804 | memcpy(&id, msg.buffer, 4);
|
---|
805 | memcpy(&x, msg.buffer+4, 4);
|
---|
806 | memcpy(&y, msg.buffer+8, 4);
|
---|
807 | memcpy(&targetId, msg.buffer+12, 4);
|
---|
808 |
|
---|
809 | cout << "id: " << id << endl;
|
---|
810 | cout << "x: " << x << endl;
|
---|
811 | cout << "y: " << y << endl;
|
---|
812 | cout << "Target: " << targetId << endl;
|
---|
813 |
|
---|
814 | Projectile proj(x, y, targetId, 0);
|
---|
815 | proj.setId(id);
|
---|
816 |
|
---|
817 | mapProjectiles[id] = proj;
|
---|
818 |
|
---|
819 | break;
|
---|
820 | }
|
---|
821 | case MSG_TYPE_REMOVE_PROJECTILE:
|
---|
822 | {
|
---|
823 | cout << "Received a REMOVE_PROJECTILE message" << endl;
|
---|
824 |
|
---|
825 | int id;
|
---|
826 | memcpy(&id, msg.buffer, 4);
|
---|
827 |
|
---|
828 | mapProjectiles.erase(id);
|
---|
829 |
|
---|
830 | break;
|
---|
831 | }
|
---|
832 | case MSG_TYPE_GAME_INFO:
|
---|
833 | {
|
---|
834 | cout << "Received a GAME_INFO message" << endl;
|
---|
835 |
|
---|
836 | string gameName(msg.buffer+4);
|
---|
837 | int numPlayers;
|
---|
838 |
|
---|
839 | memcpy(&numPlayers, msg.buffer, 4);
|
---|
840 |
|
---|
841 | cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;
|
---|
842 |
|
---|
843 | mapGames[gameName] = numPlayers;
|
---|
844 |
|
---|
845 | break;
|
---|
846 | }
|
---|
847 | default:
|
---|
848 | {
|
---|
849 | cout << "(STATE_LOBBY) Received invlaid message of type " << msg.type << endl;
|
---|
850 |
|
---|
851 | break;
|
---|
852 | }
|
---|
853 | }
|
---|
854 |
|
---|
855 | break;
|
---|
856 | }
|
---|
857 | case STATE_NEW_GAME:
|
---|
858 | {
|
---|
859 | switch(msg.type)
|
---|
860 | {
|
---|
861 | case MSG_TYPE_GAME_INFO:
|
---|
862 | {
|
---|
863 | cout << "(STATE_NEW_GAME) Received a GAME_INFO message" << endl;
|
---|
864 |
|
---|
865 | string gameName(msg.buffer+4);
|
---|
866 | int numPlayers;
|
---|
867 |
|
---|
868 | //game = new Game(gameName);
|
---|
869 |
|
---|
870 | memcpy(&numPlayers, msg.buffer, 4);
|
---|
871 |
|
---|
872 | cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;
|
---|
873 |
|
---|
874 | mapGames[gameName] = numPlayers;
|
---|
875 |
|
---|
876 | break;
|
---|
877 | }
|
---|
878 | case MSG_TYPE_OBJECT:
|
---|
879 | {
|
---|
880 | cout << "(STATE_NEW_GAME) Received object message in STATE_LOBBY." << endl;
|
---|
881 |
|
---|
882 | WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
|
---|
883 | o.deserialize(msg.buffer);
|
---|
884 | cout << "object id: " << o.id << endl;
|
---|
885 | game->getMap()->updateObject(o.id, o.type, o.pos.x, o.pos.y);
|
---|
886 |
|
---|
887 | break;
|
---|
888 | }
|
---|
889 | case MSG_TYPE_REMOVE_OBJECT:
|
---|
890 | {
|
---|
891 | cout << "(STATE_NEW_GAME) Received REMOVE_OBJECT message!" << endl;
|
---|
892 |
|
---|
893 | int id;
|
---|
894 | memcpy(&id, msg.buffer, 4);
|
---|
895 |
|
---|
896 | cout << "Removing object with id " << id << endl;
|
---|
897 |
|
---|
898 | if (!game->getMap()->removeObject(id))
|
---|
899 | cout << "Did not remove the object" << endl;
|
---|
900 |
|
---|
901 | break;
|
---|
902 | }
|
---|
903 | case MSG_TYPE_SCORE:
|
---|
904 | {
|
---|
905 | cout << "Received SCORE message!" << endl;
|
---|
906 |
|
---|
907 | int blueScore;
|
---|
908 | memcpy(&blueScore, msg.buffer, 4);
|
---|
909 | cout << "blue score: " << blueScore << endl;
|
---|
910 | game->setBlueScore(blueScore);
|
---|
911 |
|
---|
912 | int redScore;
|
---|
913 | memcpy(&redScore, msg.buffer+4, 4);
|
---|
914 | cout << "red score: " << redScore << endl;
|
---|
915 | game->setRedScore(redScore);
|
---|
916 |
|
---|
917 | cout << "Processed SCORE message!" << endl;
|
---|
918 |
|
---|
919 | break;
|
---|
920 | }
|
---|
921 | default:
|
---|
922 | {
|
---|
923 | cout << "(STATE_NEW_GAME) Received invalid message of type " << msg.type << endl;
|
---|
924 |
|
---|
925 | break;
|
---|
926 | }
|
---|
927 | }
|
---|
928 |
|
---|
929 | break;
|
---|
930 | }
|
---|
931 | default:
|
---|
932 | {
|
---|
933 | cout << "The state has an invalid value: " << state << endl;
|
---|
934 |
|
---|
935 | break;
|
---|
936 | }
|
---|
937 | }
|
---|
938 | }
|
---|
939 |
|
---|
940 | // this should probably be in the WorldMap class
|
---|
941 | void drawMap(WorldMap* gameMap)
|
---|
942 | {
|
---|
943 | POSITION mapPos;
|
---|
944 | mapPos.x = 0;
|
---|
945 | mapPos.y = 0;
|
---|
946 | mapPos = mapToScreen(mapPos);
|
---|
947 |
|
---|
948 | for (int x=0; x<gameMap->width; x++)
|
---|
949 | {
|
---|
950 | for (int y=0; y<gameMap->height; y++)
|
---|
951 | {
|
---|
952 | WorldMap::TerrainType el = gameMap->getElement(x, y);
|
---|
953 | WorldMap::StructureType structure = gameMap->getStructure(x, y);
|
---|
954 |
|
---|
955 | if (el == WorldMap::TERRAIN_GRASS)
|
---|
956 | 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));
|
---|
957 | else if (el == WorldMap::TERRAIN_OCEAN)
|
---|
958 | 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));
|
---|
959 | else if (el == WorldMap::TERRAIN_ROCK)
|
---|
960 | 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));
|
---|
961 |
|
---|
962 | if (structure == WorldMap::STRUCTURE_BLUE_FLAG) {
|
---|
963 | al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
|
---|
964 | //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));
|
---|
965 | }else if (structure == WorldMap::STRUCTURE_RED_FLAG) {
|
---|
966 | al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
|
---|
967 | //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));
|
---|
968 | }
|
---|
969 | }
|
---|
970 | }
|
---|
971 |
|
---|
972 | for (int x=0; x<gameMap->width; x++)
|
---|
973 | {
|
---|
974 | for (int y=0; y<gameMap->height; y++)
|
---|
975 | {
|
---|
976 | vector<WorldMap::Object> vctObjects = gameMap->getObjects(x, y);
|
---|
977 |
|
---|
978 | vector<WorldMap::Object>::iterator it;
|
---|
979 | for(it = vctObjects.begin(); it != vctObjects.end(); it++) {
|
---|
980 | switch(it->type) {
|
---|
981 | case WorldMap::OBJECT_BLUE_FLAG:
|
---|
982 | 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));
|
---|
983 | break;
|
---|
984 | case WorldMap::OBJECT_RED_FLAG:
|
---|
985 | 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));
|
---|
986 | break;
|
---|
987 | }
|
---|
988 | }
|
---|
989 | }
|
---|
990 | }
|
---|
991 | }
|
---|
992 |
|
---|
993 | void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId)
|
---|
994 | {
|
---|
995 | map<unsigned int, Player>::iterator it;
|
---|
996 |
|
---|
997 | Player* p;
|
---|
998 | POSITION pos;
|
---|
999 | ALLEGRO_COLOR color;
|
---|
1000 |
|
---|
1001 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
1002 | {
|
---|
1003 | p = &it->second;
|
---|
1004 |
|
---|
1005 | if (p->isDead)
|
---|
1006 | continue;
|
---|
1007 |
|
---|
1008 | pos = mapToScreen(p->pos);
|
---|
1009 |
|
---|
1010 | if (p->id == curPlayerId)
|
---|
1011 | al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));
|
---|
1012 |
|
---|
1013 | if (p->team == 0)
|
---|
1014 | color = al_map_rgb(0, 0, 255);
|
---|
1015 | else if (p->team == 1)
|
---|
1016 | color = al_map_rgb(255, 0, 0);
|
---|
1017 |
|
---|
1018 | al_draw_filled_circle(pos.x, pos.y, 12, color);
|
---|
1019 |
|
---|
1020 | // draw player class
|
---|
1021 | int fontHeight = al_get_font_line_height(font);
|
---|
1022 |
|
---|
1023 | string strClass;
|
---|
1024 | switch (p->playerClass) {
|
---|
1025 | case Player::CLASS_WARRIOR:
|
---|
1026 | strClass = "W";
|
---|
1027 | break;
|
---|
1028 | case Player::CLASS_RANGER:
|
---|
1029 | strClass = "R";
|
---|
1030 | break;
|
---|
1031 | case Player::CLASS_NONE:
|
---|
1032 | strClass = "";
|
---|
1033 | break;
|
---|
1034 | default:
|
---|
1035 | strClass = "";
|
---|
1036 | break;
|
---|
1037 | }
|
---|
1038 | al_draw_text(font, al_map_rgb(0, 0, 0), pos.x, pos.y-fontHeight/2, ALLEGRO_ALIGN_CENTRE, strClass.c_str());
|
---|
1039 |
|
---|
1040 | // draw player health
|
---|
1041 | al_draw_filled_rectangle(pos.x-12, pos.y-24, pos.x+12, pos.y-16, al_map_rgb(0, 0, 0));
|
---|
1042 | if (it->second.maxHealth != 0)
|
---|
1043 | 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));
|
---|
1044 |
|
---|
1045 | if (p->hasBlueFlag)
|
---|
1046 | al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(0, 0, 255));
|
---|
1047 | else if (p->hasRedFlag)
|
---|
1048 | al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(255, 0, 0));
|
---|
1049 | }
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | int getRefreshRate(int width, int height)
|
---|
1053 | {
|
---|
1054 | int numRefreshRates = al_get_num_display_modes();
|
---|
1055 | ALLEGRO_DISPLAY_MODE displayMode;
|
---|
1056 |
|
---|
1057 | for(int i=0; i<numRefreshRates; i++) {
|
---|
1058 | al_get_display_mode(i, &displayMode);
|
---|
1059 |
|
---|
1060 | if (displayMode.width == width && displayMode.height == height)
|
---|
1061 | return displayMode.refresh_rate;
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | return 0;
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 | void drawMessageStatus(ALLEGRO_FONT* font)
|
---|
1068 | {
|
---|
1069 | int clientMsgOffset = 5;
|
---|
1070 | int serverMsgOffset = 950;
|
---|
1071 |
|
---|
1072 | al_draw_text(font, al_map_rgb(0, 255, 255), 0+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
|
---|
1073 | al_draw_text(font, al_map_rgb(0, 255, 255), 20+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Type");
|
---|
1074 | al_draw_text(font, al_map_rgb(0, 255, 255), 240+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Acked?");
|
---|
1075 |
|
---|
1076 | al_draw_text(font, al_map_rgb(0, 255, 255), serverMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
|
---|
1077 |
|
---|
1078 | map<unsigned int, map<unsigned long, MessageContainer> >& sentMessages = msgProcessor.getSentMessages();
|
---|
1079 | int id, type;
|
---|
1080 | bool acked;
|
---|
1081 | ostringstream ossId, ossAcked;
|
---|
1082 |
|
---|
1083 | map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
|
---|
1084 |
|
---|
1085 | int msgCount = 0;
|
---|
1086 | for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
|
---|
1087 | map<unsigned long, MessageContainer> playerMessage = it->second;
|
---|
1088 | map<unsigned long, MessageContainer>::iterator it2;
|
---|
1089 | for (it2 = playerMessage.begin(); it2 != playerMessage.end(); it2++) {
|
---|
1090 |
|
---|
1091 | id = it->first;
|
---|
1092 | ossId.str("");;
|
---|
1093 | ossId << id;
|
---|
1094 |
|
---|
1095 | type = it2->second.getMessage()->type;
|
---|
1096 | string typeStr = MessageContainer::getMsgTypeString(type);
|
---|
1097 |
|
---|
1098 | acked = it2->second.getAcked();
|
---|
1099 | ossAcked.str("");;
|
---|
1100 | ossAcked << boolalpha << acked;
|
---|
1101 |
|
---|
1102 | al_draw_text(font, al_map_rgb(0, 255, 0), clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
|
---|
1103 | al_draw_text(font, al_map_rgb(0, 255, 0), 20+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, typeStr.c_str());
|
---|
1104 | al_draw_text(font, al_map_rgb(0, 255, 0), 240+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossAcked.str().c_str());
|
---|
1105 |
|
---|
1106 | msgCount++;
|
---|
1107 | }
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 | if (msgProcessor.getAckedMessages().size() > 0) {
|
---|
1111 | map<unsigned int, unsigned long long> ackedMessages = msgProcessor.getAckedMessages()[0];
|
---|
1112 | map<unsigned int, unsigned long long>::iterator it3;
|
---|
1113 |
|
---|
1114 | msgCount = 0;
|
---|
1115 | for (it3 = ackedMessages.begin(); it3 != ackedMessages.end(); it3++) {
|
---|
1116 | ossId.str("");;
|
---|
1117 | ossId << it3->first;
|
---|
1118 |
|
---|
1119 | al_draw_text(font, al_map_rgb(255, 0, 0), 25+serverMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
|
---|
1120 |
|
---|
1121 | msgCount++;
|
---|
1122 | }
|
---|
1123 | }
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 | // Callback definitions
|
---|
1127 |
|
---|
1128 | void goToRegisterScreen()
|
---|
1129 | {
|
---|
1130 | txtUsernameRegister->clear();
|
---|
1131 | txtPasswordRegister->clear();
|
---|
1132 | lblRegisterStatus->setText("");
|
---|
1133 | rblClasses->setSelectedButton(-1);
|
---|
1134 |
|
---|
1135 | wndCurrent = wndRegister;
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 | void goToLoginScreen()
|
---|
1139 | {
|
---|
1140 | txtUsername->clear();
|
---|
1141 | txtPassword->clear();
|
---|
1142 | lblLoginStatus->setText("");
|
---|
1143 |
|
---|
1144 | wndCurrent = wndLogin;
|
---|
1145 | }
|
---|
1146 |
|
---|
1147 | // maybe need a goToGameScreen function as well and add state changes to these functions as well
|
---|
1148 |
|
---|
1149 | void registerAccount()
|
---|
1150 | {
|
---|
1151 | string username = txtUsernameRegister->getStr();
|
---|
1152 | string password = txtPasswordRegister->getStr();
|
---|
1153 |
|
---|
1154 | txtUsernameRegister->clear();
|
---|
1155 | txtPasswordRegister->clear();
|
---|
1156 | // maybe clear rblClasses as well (add a method to RadioButtonList to enable this)
|
---|
1157 |
|
---|
1158 | Player::PlayerClass playerClass;
|
---|
1159 |
|
---|
1160 | switch (rblClasses->getSelectedButton()) {
|
---|
1161 | case 0:
|
---|
1162 | playerClass = Player::CLASS_WARRIOR;
|
---|
1163 | break;
|
---|
1164 | case 1:
|
---|
1165 | playerClass = Player::CLASS_RANGER;
|
---|
1166 | break;
|
---|
1167 | default:
|
---|
1168 | cout << "Invalid class selection" << endl;
|
---|
1169 | playerClass = Player::CLASS_NONE;
|
---|
1170 | break;
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | msgTo.type = MSG_TYPE_REGISTER;
|
---|
1174 |
|
---|
1175 | strcpy(msgTo.buffer, username.c_str());
|
---|
1176 | strcpy(msgTo.buffer+username.size()+1, password.c_str());
|
---|
1177 | memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
|
---|
1178 |
|
---|
1179 | msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 | void login()
|
---|
1183 | {
|
---|
1184 | string strUsername = txtUsername->getStr();
|
---|
1185 | string strPassword = txtPassword->getStr();
|
---|
1186 | username = strUsername;
|
---|
1187 |
|
---|
1188 | txtUsername->clear();
|
---|
1189 | txtPassword->clear();
|
---|
1190 |
|
---|
1191 | msgTo.type = MSG_TYPE_LOGIN;
|
---|
1192 |
|
---|
1193 | strcpy(msgTo.buffer, strUsername.c_str());
|
---|
1194 | strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
|
---|
1195 |
|
---|
1196 | msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
|
---|
1197 |
|
---|
1198 | state = STATE_LOBBY;
|
---|
1199 | }
|
---|
1200 |
|
---|
1201 | void logout()
|
---|
1202 | {
|
---|
1203 | switch(state) {
|
---|
1204 | case STATE_LOBBY:
|
---|
1205 | txtJoinGame->clear();
|
---|
1206 | txtCreateGame->clear();
|
---|
1207 | break;
|
---|
1208 | case STATE_GAME:
|
---|
1209 | txtChat->clear();
|
---|
1210 | chatConsole.clear();
|
---|
1211 | break;
|
---|
1212 | default:
|
---|
1213 | cout << "Logout called from invalid state: " << state << endl;
|
---|
1214 | break;
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | msgTo.type = MSG_TYPE_LOGOUT;
|
---|
1218 |
|
---|
1219 | strcpy(msgTo.buffer, username.c_str());
|
---|
1220 |
|
---|
1221 | msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
|
---|
1222 | }
|
---|
1223 |
|
---|
1224 | void quit()
|
---|
1225 | {
|
---|
1226 | doexit = true;
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | void sendChatMessage()
|
---|
1230 | {
|
---|
1231 | string msg = txtChat->getStr();
|
---|
1232 | txtChat->clear();
|
---|
1233 |
|
---|
1234 | msgTo.type = MSG_TYPE_CHAT;
|
---|
1235 | strcpy(msgTo.buffer, msg.c_str());
|
---|
1236 |
|
---|
1237 | msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
|
---|
1238 | }
|
---|
1239 |
|
---|
1240 | void toggleDebugging()
|
---|
1241 | {
|
---|
1242 | debugging = !debugging;
|
---|
1243 | }
|
---|
1244 |
|
---|
1245 | void joinGame()
|
---|
1246 | {
|
---|
1247 | cout << "Joining game" << endl;
|
---|
1248 |
|
---|
1249 | string msg = txtJoinGame->getStr();
|
---|
1250 | txtJoinGame->clear();
|
---|
1251 |
|
---|
1252 | msgTo.type = MSG_TYPE_JOIN_GAME;
|
---|
1253 | strcpy(msgTo.buffer, msg.c_str());
|
---|
1254 |
|
---|
1255 | msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
|
---|
1256 | }
|
---|
1257 |
|
---|
1258 | void createGame()
|
---|
1259 | {
|
---|
1260 | cout << "Creating game" << endl;
|
---|
1261 |
|
---|
1262 | // hack to help transitions to multiple games
|
---|
1263 | state = STATE_NEW_GAME;
|
---|
1264 | game = new Game( txtCreateGame->getStr());
|
---|
1265 |
|
---|
1266 | string msg = txtCreateGame->getStr();
|
---|
1267 | txtCreateGame->clear();
|
---|
1268 |
|
---|
1269 | msgTo.type = MSG_TYPE_CREATE_GAME;
|
---|
1270 | strcpy(msgTo.buffer, msg.c_str());
|
---|
1271 |
|
---|
1272 | msgProcessor.sendMessage(&msgTo, sock, &server, &outputLog);
|
---|
1273 | }
|
---|