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

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

Client shows all players in the game lobby and sends messages to the server when the client player switches teams

  • Property mode set to 100644
File size: 43.3 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 cout << "(STATE_GAME_LOBBY) ";
885 case STATE_GAME:
886 {
887 cout << "(STATE_GAME) ";
888 switch(msg.type)
889 {
890 case MSG_TYPE_SCORE:
891 {
892 cout << "Received SCORE message!" << endl;
893
894 int blueScore;
895 memcpy(&blueScore, msg.buffer, 4);
896 cout << "blue score: " << blueScore << endl;
897 game->setBlueScore(blueScore);
898
899 int redScore;
900 memcpy(&redScore, msg.buffer+4, 4);
901 cout << "red score: " << redScore << endl;
902 game->setRedScore(redScore);
903
904 cout << "Processed SCORE message!" << endl;
905
906 break;
907 }
908 case MSG_TYPE_FINISH_GAME:
909 {
910 cout << "Got a finish game message" << endl;
911 cout << "Should switch to STATE_LOBBY and show the final score" << endl;
912
913 unsigned int winner, blueScore, redScore;
914 memcpy(&winner, msg.buffer, 4);
915 memcpy(&blueScore, msg.buffer+4, 4);
916 memcpy(&redScore, msg.buffer+8, 4);
917
918 string gameName(msg.buffer+12);
919
920 gameSummary = new GameSummary(gameName, winner, blueScore, redScore);
921
922 delete game;
923 game = NULL;
924 state = STATE_LOBBY;
925 wndCurrent = wndGameSummary;
926
927 break;
928 }
929 case MSG_TYPE_LOGOUT:
930 {
931 cout << "Got a logout message" << endl;
932
933 unsigned int playerId;
934
935 // Check if it's about you or another player
936 memcpy(&playerId, msg.buffer, 4);
937 response = string(msg.buffer+4);
938
939 if (playerId == curPlayerId)
940 cout << "Received MSG_TYPE_LOGOUT for self in STATE_GAME. This shouldn't happen." << endl;
941 else {
942 delete mapPlayers[playerId];
943 mapPlayers.erase(playerId);
944 }
945
946 break;
947 }
948 case MSG_TYPE_PLAYER_JOIN_GAME:
949 {
950 cout << "Received MSG_TYPE_PLAYER_JOIN_GAME" << endl;
951
952 Player p("", "");
953 p.deserialize(msg.buffer);
954 cout << "Deserialized player" << endl;
955 cout << "player team: " << p.team << endl;
956 cout << "current player team: " << currentPlayer->team << endl;
957 p.timeLastUpdated = getCurrentMillis();
958 p.isChasing = false;
959 if (p.health <= 0)
960 p.isDead = true;
961 else
962 p.isDead = false;
963
964 if (mapPlayers.find(p.getId()) != mapPlayers.end())
965 *(mapPlayers[p.getId()]) = p;
966 else
967 mapPlayers[p.getId()] = new Player(p);
968
969 game->addPlayer(mapPlayers[p.getId()], false);
970
971 break;
972 }
973 case MSG_TYPE_LEAVE_GAME:
974 {
975 cout << "Received a LEAVE_GAME message" << endl;
976
977 string gameName(msg.buffer+4);
978 unsigned int playerId;
979
980 memcpy(&playerId, msg.buffer, 4);
981
982 game->removePlayer(playerId);
983
984 break;
985 }
986 case MSG_TYPE_PLAYER_MOVE:
987 {
988 cout << "Received PLAYER_MOVE message" << endl;
989
990 unsigned int id;
991 int x, y;
992
993 memcpy(&id, msg.buffer, 4);
994 memcpy(&x, msg.buffer+4, 4);
995 memcpy(&y, msg.buffer+8, 4);
996
997 cout << "id: " << id << endl;
998
999 mapPlayers[id]->target.x = x;
1000 mapPlayers[id]->target.y = y;
1001
1002 mapPlayers[id]->isChasing = false;
1003 mapPlayers[id]->setTargetPlayer(0);
1004
1005 break;
1006 }
1007 case MSG_TYPE_OBJECT:
1008 {
1009 cout << "Received object message in STATE_GAME" << endl;
1010
1011 WorldMap::Object o(0, OBJECT_NONE, 0, 0);
1012 o.deserialize(msg.buffer);
1013 cout << "object id: " << o.id << endl;
1014 game->getMap()->updateObject(o.id, o.type, o.pos.x, o.pos.y);
1015
1016 break;
1017 }
1018 case MSG_TYPE_REMOVE_OBJECT:
1019 {
1020 cout << "Received REMOVE_OBJECT message!" << endl;
1021
1022 int id;
1023 memcpy(&id, msg.buffer, 4);
1024
1025 cout << "Removing object with id " << id << endl;
1026
1027 if (!game->getMap()->removeObject(id))
1028 cout << "Did not remove the object" << endl;
1029
1030 break;
1031 }
1032 case MSG_TYPE_ATTACK:
1033 {
1034 cout << "Received START_ATTACK message" << endl;
1035
1036 unsigned int id, targetId;
1037 memcpy(&id, msg.buffer, 4);
1038 memcpy(&targetId, msg.buffer+4, 4);
1039
1040 cout << "source id: " << id << endl;
1041 cout << "target id: " << targetId << endl;
1042
1043 // need to check the target exists in the current game
1044 Player* source = game->getPlayers()[id];
1045 source->setTargetPlayer(targetId);
1046 source->isChasing = true;
1047
1048 break;
1049 }
1050 case MSG_TYPE_PROJECTILE:
1051 {
1052 cout << "Received a PROJECTILE message" << endl;
1053
1054 unsigned int projId, x, y, targetId;
1055
1056 memcpy(&projId, msg.buffer, 4);
1057 memcpy(&x, msg.buffer+4, 4);
1058 memcpy(&y, msg.buffer+8, 4);
1059 memcpy(&targetId, msg.buffer+12, 4);
1060
1061 cout << "projId: " << projId << endl;
1062 cout << "x: " << x << endl;
1063 cout << "y: " << y << endl;
1064 cout << "Target: " << targetId << endl;
1065
1066 Projectile proj(x, y, targetId, 0);
1067 proj.setId(projId);
1068
1069 game->addProjectile(proj);
1070
1071 break;
1072 }
1073 case MSG_TYPE_REMOVE_PROJECTILE:
1074 {
1075 cout << "Received a REMOVE_PROJECTILE message" << endl;
1076
1077 unsigned int id;
1078 memcpy(&id, msg.buffer, 4);
1079
1080 game->removeProjectile(id);
1081
1082 break;
1083 }
1084 case MSG_TYPE_PLAYER:
1085 {
1086 handleMsgPlayer(msg, mapPlayers, mapGames);
1087
1088 break;
1089 }
1090 case MSG_TYPE_GAME_INFO:
1091 {
1092 handleMsgGameInfo(msg, mapPlayers, mapGames);
1093
1094 break;
1095 }
1096 default:
1097 {
1098 cout << "Received invalid message of type " << msg.type << endl;
1099
1100 break;
1101 }
1102 }
1103
1104 break;
1105 }
1106 default:
1107 {
1108 cout << "The state has an invalid value: " << state << endl;
1109
1110 break;
1111 }
1112 }
1113}
1114
1115int getRefreshRate(int width, int height)
1116{
1117 int numRefreshRates = al_get_num_display_modes();
1118 ALLEGRO_DISPLAY_MODE displayMode;
1119
1120 for(int i=0; i<numRefreshRates; i++) {
1121 al_get_display_mode(i, &displayMode);
1122
1123 if (displayMode.width == width && displayMode.height == height)
1124 return displayMode.refresh_rate;
1125 }
1126
1127 return 0;
1128}
1129
1130void drawMessageStatus(ALLEGRO_FONT* font)
1131{
1132 int clientMsgOffset = 5;
1133 int serverMsgOffset = 950;
1134
1135 al_draw_text(font, al_map_rgb(0, 255, 255), 0+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
1136 al_draw_text(font, al_map_rgb(0, 255, 255), 20+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Type");
1137 al_draw_text(font, al_map_rgb(0, 255, 255), 240+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Acked?");
1138
1139 //al_draw_text(font, al_map_rgb(0, 255, 255), serverMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
1140
1141 map<unsigned int, map<unsigned long, MessageContainer> >& sentMessages = msgProcessor.getSentMessages();
1142 int id, type;
1143 bool acked;
1144 ostringstream ossId, ossAcked;
1145
1146 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
1147
1148 int msgCount = 0;
1149 for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
1150 map<unsigned long, MessageContainer> playerMessage = it->second;
1151 map<unsigned long, MessageContainer>::iterator it2;
1152 for (it2 = playerMessage.begin(); it2 != playerMessage.end(); it2++) {
1153
1154 id = it->first;
1155 ossId.str("");;
1156 ossId << id;
1157
1158 type = it2->second.getMessage()->type;
1159 string typeStr = MessageContainer::getMsgTypeString(type);
1160
1161 acked = it2->second.getAcked();
1162 ossAcked.str("");;
1163 ossAcked << boolalpha << acked;
1164
1165 al_draw_text(font, al_map_rgb(0, 255, 0), clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
1166 al_draw_text(font, al_map_rgb(0, 255, 0), 20+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, typeStr.c_str());
1167 al_draw_text(font, al_map_rgb(0, 255, 0), 240+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossAcked.str().c_str());
1168
1169 msgCount++;
1170 }
1171 }
1172
1173 if (msgProcessor.getAckedMessages().size() > 0) {
1174 map<unsigned int, unsigned long long> ackedMessages = msgProcessor.getAckedMessages()[0];
1175 map<unsigned int, unsigned long long>::iterator it3;
1176
1177 msgCount = 0;
1178 for (it3 = ackedMessages.begin(); it3 != ackedMessages.end(); it3++) {
1179 ossId.str("");;
1180 ossId << it3->first;
1181
1182 al_draw_text(font, al_map_rgb(255, 0, 0), 25+serverMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
1183
1184 msgCount++;
1185 }
1186 }
1187}
1188
1189// message handling functions
1190
1191void handleMsgPlayer(NETWORK_MSG &msg, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames) {
1192 cout << "Received MSG_TYPE_PLAYER" << endl;
1193
1194 Player p("", "");
1195 p.deserialize(msg.buffer);
1196 p.timeLastUpdated = getCurrentMillis();
1197 p.isChasing = false;
1198 if (p.health <= 0)
1199 p.isDead = true;
1200 else
1201 p.isDead = false;
1202
1203 if (mapPlayers.find(p.getId()) != mapPlayers.end())
1204 *(mapPlayers[p.getId()]) = p;
1205 else
1206 mapPlayers[p.getId()] = new Player(p);
1207}
1208
1209void handleMsgGameInfo(NETWORK_MSG &msg, map<unsigned int, Player*>& mapPlayers, map<string, int>& mapGames) {
1210 cout << "Received a GAME_INFO message" << endl;
1211
1212 string gameName(msg.buffer+4);
1213 int numPlayers;
1214
1215 memcpy(&numPlayers, msg.buffer, 4);
1216
1217 cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;
1218
1219 if (numPlayers > 0)
1220 mapGames[gameName] = numPlayers;
1221 else
1222 mapGames.erase(gameName);
1223}
1224
1225// Callback definitions
1226
1227void goToRegisterScreen()
1228{
1229 txtUsernameRegister->clear();
1230 txtPasswordRegister->clear();
1231 lblRegisterStatus->setText("");
1232 rblClasses->setSelectedButton(-1);
1233
1234 wndCurrent = wndRegister;
1235}
1236
1237void goToLoginScreen()
1238{
1239 txtUsername->clear();
1240 txtPassword->clear();
1241 lblLoginStatus->setText("");
1242
1243 wndCurrent = wndLogin;
1244}
1245
1246// maybe need a goToGameScreen function as well and add state changes to these functions as well
1247
1248void registerAccount()
1249{
1250 string username = txtUsernameRegister->getStr();
1251 string password = txtPasswordRegister->getStr();
1252
1253 txtUsernameRegister->clear();
1254 txtPasswordRegister->clear();
1255 // maybe clear rblClasses as well (add a method to RadioButtonList to enable this)
1256
1257 Player::PlayerClass playerClass;
1258
1259 switch (rblClasses->getSelectedButton()) {
1260 case 0:
1261 playerClass = Player::CLASS_WARRIOR;
1262 break;
1263 case 1:
1264 playerClass = Player::CLASS_RANGER;
1265 break;
1266 default:
1267 cout << "Invalid class selection" << endl;
1268 playerClass = Player::CLASS_NONE;
1269 break;
1270 }
1271
1272 msgTo.type = MSG_TYPE_REGISTER;
1273
1274 strcpy(msgTo.buffer, username.c_str());
1275 strcpy(msgTo.buffer+username.size()+1, password.c_str());
1276 memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
1277
1278 msgProcessor.sendMessage(&msgTo, &server);
1279}
1280
1281void login()
1282{
1283 string strUsername = txtUsername->getStr();
1284 string strPassword = txtPassword->getStr();
1285 username = strUsername;
1286
1287 txtUsername->clear();
1288 txtPassword->clear();
1289
1290 msgTo.type = MSG_TYPE_LOGIN;
1291
1292 strcpy(msgTo.buffer, strUsername.c_str());
1293 strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
1294
1295 msgProcessor.sendMessage(&msgTo, &server);
1296
1297 state = STATE_LOBBY;
1298}
1299
1300void logout()
1301{
1302 switch(state) {
1303 case STATE_LOBBY:
1304 txtJoinGame->clear();
1305 txtCreateGame->clear();
1306 break;
1307 default:
1308 cout << "Logout called from invalid state: " << state << endl;
1309 break;
1310 }
1311
1312 msgTo.type = MSG_TYPE_LOGOUT;
1313
1314 strcpy(msgTo.buffer, username.c_str());
1315
1316 msgProcessor.sendMessage(&msgTo, &server);
1317}
1318
1319void quit()
1320{
1321 doexit = true;
1322}
1323
1324void sendChatMessage()
1325{
1326 string msg = txtChat->getStr();
1327 txtChat->clear();
1328
1329 msgTo.type = MSG_TYPE_CHAT;
1330 strcpy(msgTo.buffer, msg.c_str());
1331
1332 msgProcessor.sendMessage(&msgTo, &server);
1333}
1334
1335void toggleDebugging()
1336{
1337 debugging = !debugging;
1338}
1339
1340void joinGame()
1341{
1342 cout << "Joining game" << endl;
1343
1344 string msg = txtJoinGame->getStr();
1345 txtJoinGame->clear();
1346
1347 msgTo.type = MSG_TYPE_JOIN_GAME;
1348 strcpy(msgTo.buffer, msg.c_str());
1349
1350 msgProcessor.sendMessage(&msgTo, &server);
1351}
1352
1353void createGame()
1354{
1355 cout << "Creating game" << endl;
1356
1357 string msg = txtCreateGame->getStr();
1358 txtCreateGame->clear();
1359
1360 cout << "Sending message: " << msg.c_str() << endl;
1361
1362 msgTo.type = MSG_TYPE_CREATE_GAME;
1363 strcpy(msgTo.buffer, msg.c_str());
1364
1365 msgProcessor.sendMessage(&msgTo, &server);
1366 cout << "Sent CREATE_GAME message" << endl;
1367}
1368
1369void joinWaitingArea() {
1370 cout << "joining waiting area" << endl;
1371 currentPlayer->team = -1;
1372
1373 msgTo.type = MSG_TYPE_JOIN_TEAM;
1374 memcpy(msgTo.buffer, &(currentPlayer->team), 4);
1375
1376 msgProcessor.sendMessage(&msgTo, &server);
1377}
1378
1379void joinBlueTeam() {
1380 cout << "joining blue team" << endl;
1381 currentPlayer->team = 0;
1382
1383 msgTo.type = MSG_TYPE_JOIN_TEAM;
1384 memcpy(msgTo.buffer, &(currentPlayer->team), 4);
1385
1386 msgProcessor.sendMessage(&msgTo, &server);
1387}
1388
1389void joinRedTeam() {
1390 cout << "joining red team" << endl;
1391 currentPlayer->team = 1;
1392
1393 msgTo.type = MSG_TYPE_JOIN_TEAM;
1394 memcpy(msgTo.buffer, &(currentPlayer->team), 4);
1395
1396 msgProcessor.sendMessage(&msgTo, &server);
1397}
1398
1399void startGame() {
1400 state = STATE_GAME;
1401 wndCurrent = wndGame;
1402}
1403
1404void leaveGame()
1405{
1406 cout << "Leaving game" << endl;
1407
1408 delete game;
1409 game = NULL;
1410
1411 state = STATE_LOBBY;
1412 wndCurrent = wndLobby;
1413
1414 msgTo.type = MSG_TYPE_LEAVE_GAME;
1415
1416 msgProcessor.sendMessage(&msgTo, &server);
1417}
1418
1419void closeGameSummary()
1420{
1421 delete gameSummary;
1422 gameSummary = NULL;
1423 wndCurrent = wndLobby;
1424 cout << "Processed button actions" << endl;
1425}
Note: See TracBrowser for help on using the repository browser.