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