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