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