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

Last change on this file since d05086b was b35b2b2, checked in by dportnoy <dmp1488@…>, 11 years ago

Added a basic ingame debug console

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