source: network-game/client/Client/main.cpp@ cf05729

Last change on this file since cf05729 was cf05729, checked in by dportnoy <dmp1488@…>, 10 years ago

Client processes START_GAME response

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