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

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

Add status messages for login and registration and remove the Message class (MessageProcessor includes all that functionality now)

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