| 1 | #include <cstdlib>
|
|---|
| 2 | #include <cstdio>
|
|---|
| 3 | #include <unistd.h>
|
|---|
| 4 | #include <string>
|
|---|
| 5 | #include <iostream>
|
|---|
| 6 | #include <sstream>
|
|---|
| 7 | #include <cstring>
|
|---|
| 8 | #include <cmath>
|
|---|
| 9 |
|
|---|
| 10 | #include <vector>
|
|---|
| 11 | #include <map>
|
|---|
| 12 |
|
|---|
| 13 | #include <sys/time.h>
|
|---|
| 14 |
|
|---|
| 15 | #include <sys/socket.h>
|
|---|
| 16 | #include <netdb.h>
|
|---|
| 17 | #include <netinet/in.h>
|
|---|
| 18 | #include <arpa/inet.h>
|
|---|
| 19 |
|
|---|
| 20 | #include <crypt.h>
|
|---|
| 21 |
|
|---|
| 22 | /*
|
|---|
| 23 | #include <openssl/bio.h>
|
|---|
| 24 | #include <openssl/ssl.h>
|
|---|
| 25 | #include <openssl/err.h>
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | #include "../common/Compiler.h"
|
|---|
| 29 | #include "../common/Common.h"
|
|---|
| 30 | #include "../common/MessageProcessor.h"
|
|---|
| 31 | #include "../common/WorldMap.h"
|
|---|
| 32 | #include "../common/Player.h"
|
|---|
| 33 | #include "../common/Projectile.h"
|
|---|
| 34 |
|
|---|
| 35 | #include "DataAccess.h"
|
|---|
| 36 |
|
|---|
| 37 | using namespace std;
|
|---|
| 38 |
|
|---|
| 39 | // from used to be const. Removed that so I could take a reference
|
|---|
| 40 | // and use it to send messages
|
|---|
| 41 | bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed);
|
|---|
| 42 |
|
|---|
| 43 | void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
|
|---|
| 44 | void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles);
|
|---|
| 45 | void damagePlayer(Player *p, int damage);
|
|---|
| 46 |
|
|---|
| 47 | void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player>& mapPlayers, MessageProcessor &msgProcessor, int sock);
|
|---|
| 48 |
|
|---|
| 49 | // this should probably go somewhere in the common folder
|
|---|
| 50 | void error(const char *msg)
|
|---|
| 51 | {
|
|---|
| 52 | perror(msg);
|
|---|
| 53 | exit(0);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | Player *findPlayerByName(map<unsigned int, Player> &m, string name)
|
|---|
| 57 | {
|
|---|
| 58 | map<unsigned int, Player>::iterator it;
|
|---|
| 59 |
|
|---|
| 60 | for (it = m.begin(); it != m.end(); it++)
|
|---|
| 61 | {
|
|---|
| 62 | if ( it->second.name.compare(name) == 0 )
|
|---|
| 63 | return &(it->second);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | return NULL;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | Player *findPlayerByAddr(map<unsigned int, Player> &m, const sockaddr_in &addr)
|
|---|
| 70 | {
|
|---|
| 71 | map<unsigned int, Player>::iterator it;
|
|---|
| 72 |
|
|---|
| 73 | for (it = m.begin(); it != m.end(); it++)
|
|---|
| 74 | {
|
|---|
| 75 | if ( it->second.addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
|
|---|
| 76 | it->second.addr.sin_port == addr.sin_port )
|
|---|
| 77 | return &(it->second);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | return NULL;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | int main(int argc, char *argv[])
|
|---|
| 84 | {
|
|---|
| 85 | int sock, length, n;
|
|---|
| 86 | struct sockaddr_in server;
|
|---|
| 87 | struct sockaddr_in from; // info of client sending the message
|
|---|
| 88 | NETWORK_MSG clientMsg, serverMsg;
|
|---|
| 89 | MessageProcessor msgProcessor;
|
|---|
| 90 | map<unsigned int, Player> mapPlayers;
|
|---|
| 91 | map<unsigned int, Projectile> mapProjectiles;
|
|---|
| 92 | unsigned int unusedPlayerId = 1, unusedProjectileId = 1;
|
|---|
| 93 | int scoreBlue, scoreRed;
|
|---|
| 94 |
|
|---|
| 95 | scoreBlue = 0;
|
|---|
| 96 | scoreRed = 0;
|
|---|
| 97 |
|
|---|
| 98 | //SSL_load_error_strings();
|
|---|
| 99 | //ERR_load_BIO_strings();
|
|---|
| 100 | //OpenSSL_add_all_algorithms();
|
|---|
| 101 |
|
|---|
| 102 | if (argc < 2) {
|
|---|
| 103 | cerr << "ERROR, no port provided" << endl;
|
|---|
| 104 | exit(1);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | WorldMap* gameMap = WorldMap::loadMapFromFile("../data/map.txt");
|
|---|
| 108 |
|
|---|
| 109 | // add some items to the map. They will be sent out
|
|---|
| 110 | // to players when they login
|
|---|
| 111 | for (int y=0; y<gameMap->height; y++) {
|
|---|
| 112 | for (int x=0; x<gameMap->width; x++) {
|
|---|
| 113 | switch (gameMap->getStructure(x, y)) {
|
|---|
| 114 | case WorldMap::STRUCTURE_BLUE_FLAG:
|
|---|
| 115 | gameMap->addObject(WorldMap::OBJECT_BLUE_FLAG, x*25+12, y*25+12);
|
|---|
| 116 | break;
|
|---|
| 117 | case WorldMap::STRUCTURE_RED_FLAG:
|
|---|
| 118 | gameMap->addObject(WorldMap::OBJECT_RED_FLAG, x*25+12, y*25+12);
|
|---|
| 119 | break;
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
|---|
| 125 | if (sock < 0)
|
|---|
| 126 | error("Opening socket");
|
|---|
| 127 | length = sizeof(server);
|
|---|
| 128 | bzero(&server,length);
|
|---|
| 129 | server.sin_family=AF_INET;
|
|---|
| 130 | server.sin_port=htons(atoi(argv[1]));
|
|---|
| 131 | server.sin_addr.s_addr=INADDR_ANY;
|
|---|
| 132 | if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
|
|---|
| 133 | error("binding");
|
|---|
| 134 |
|
|---|
| 135 | set_nonblock(sock);
|
|---|
| 136 |
|
|---|
| 137 | bool broadcastResponse;
|
|---|
| 138 | timespec ts;
|
|---|
| 139 | int timeLastUpdated = 0, curTime = 0, timeLastBroadcast = 0;
|
|---|
| 140 | while (true) {
|
|---|
| 141 |
|
|---|
| 142 | usleep(5000);
|
|---|
| 143 |
|
|---|
| 144 | clock_gettime(CLOCK_REALTIME, &ts);
|
|---|
| 145 | // make the number smaller so millis can fit in an int
|
|---|
| 146 | ts.tv_sec -= 1368000000;
|
|---|
| 147 | curTime = ts.tv_sec*1000 + ts.tv_nsec/1000000;
|
|---|
| 148 |
|
|---|
| 149 | if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50) {
|
|---|
| 150 | timeLastUpdated = curTime;
|
|---|
| 151 |
|
|---|
| 152 | msgProcessor.cleanAckedMessages();
|
|---|
| 153 | msgProcessor.resendUnackedMessages(sock);
|
|---|
| 154 |
|
|---|
| 155 | map<unsigned int, Player>::iterator it;
|
|---|
| 156 |
|
|---|
| 157 | // set targets for all chasing players (or make them attack if they're close enough)
|
|---|
| 158 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
|
|---|
| 159 | // check if it's time to revive dead players
|
|---|
| 160 | if (it->second.isDead) {
|
|---|
| 161 | if (getCurrentMillis() - it->second.timeDied >= 10000) {
|
|---|
| 162 | it->second.isDead = false;
|
|---|
| 163 |
|
|---|
| 164 | POSITION spawnPos;
|
|---|
| 165 |
|
|---|
| 166 | switch (it->second.team) {
|
|---|
| 167 | case 0:// blue team
|
|---|
| 168 | spawnPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
|---|
| 169 | break;
|
|---|
| 170 | case 1:// red team
|
|---|
| 171 | spawnPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
|---|
| 172 | break;
|
|---|
| 173 | default:
|
|---|
| 174 | // should never go here
|
|---|
| 175 | cout << "Error: Invalid team" << endl;
|
|---|
| 176 | break;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | // spawn the player to the right of their flag location
|
|---|
| 180 | spawnPos.x = (spawnPos.x+1) * 25 + 12;
|
|---|
| 181 | spawnPos.y = spawnPos.y * 25 + 12;
|
|---|
| 182 |
|
|---|
| 183 | it->second.pos = spawnPos.toFloat();
|
|---|
| 184 | it->second.target = spawnPos;
|
|---|
| 185 | it->second.health = it->second.maxHealth;
|
|---|
| 186 |
|
|---|
| 187 | serverMsg.type = MSG_TYPE_PLAYER;
|
|---|
| 188 | it->second.serialize(serverMsg.buffer);
|
|---|
| 189 |
|
|---|
| 190 | map<unsigned int, Player>::iterator it2;
|
|---|
| 191 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
|---|
| 192 | {
|
|---|
| 193 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
|---|
| 194 | error("sendMessage");
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | continue;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | if (it->second.updateTarget(mapPlayers)) {
|
|---|
| 202 | serverMsg.type = MSG_TYPE_PLAYER;
|
|---|
| 203 | it->second.serialize(serverMsg.buffer);
|
|---|
| 204 |
|
|---|
| 205 | map<unsigned int, Player>::iterator it2;
|
|---|
| 206 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
|---|
| 207 | {
|
|---|
| 208 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
|---|
| 209 | error("sendMessage");
|
|---|
| 210 | }
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | // move all players
|
|---|
| 215 | // maybe put this in a separate method
|
|---|
| 216 | FLOAT_POSITION oldPos;
|
|---|
| 217 | bool broadcastMove = false;
|
|---|
| 218 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
|
|---|
| 219 | oldPos = it->second.pos;
|
|---|
| 220 | if (it->second.move(gameMap)) {
|
|---|
| 221 |
|
|---|
| 222 | // check if the move needs to be canceled
|
|---|
| 223 | switch(gameMap->getElement(it->second.pos.x/25, it->second.pos.y/25)) {
|
|---|
| 224 | case WorldMap::TERRAIN_NONE:
|
|---|
| 225 | case WorldMap::TERRAIN_OCEAN:
|
|---|
| 226 | case WorldMap::TERRAIN_ROCK:
|
|---|
| 227 | {
|
|---|
| 228 | it->second.pos = oldPos;
|
|---|
| 229 | it->second.target.x = it->second.pos.x;
|
|---|
| 230 | it->second.target.y = it->second.pos.y;
|
|---|
| 231 | it->second.isChasing = false;
|
|---|
| 232 | broadcastMove = true;
|
|---|
| 233 | break;
|
|---|
| 234 | }
|
|---|
| 235 | default:
|
|---|
| 236 | // if there are no obstacles, do nothing
|
|---|
| 237 | break;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | WorldMap::ObjectType flagType;
|
|---|
| 241 | POSITION pos;
|
|---|
| 242 | bool flagTurnedIn = false;
|
|---|
| 243 | bool flagReturned = false;
|
|---|
| 244 | bool ownFlagAtBase = false;
|
|---|
| 245 |
|
|---|
| 246 | switch(gameMap->getStructure(it->second.pos.x/25, it->second.pos.y/25)) {
|
|---|
| 247 | case WorldMap::STRUCTURE_BLUE_FLAG:
|
|---|
| 248 | {
|
|---|
| 249 | if (it->second.team == 0 && it->second.hasRedFlag)
|
|---|
| 250 | {
|
|---|
| 251 | // check that your flag is at your base
|
|---|
| 252 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
|---|
| 253 |
|
|---|
| 254 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
|---|
| 255 | vector<WorldMap::Object>::iterator itObjects;
|
|---|
| 256 |
|
|---|
| 257 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
|---|
| 258 | if (itObjects->type == WorldMap::OBJECT_BLUE_FLAG) {
|
|---|
| 259 | if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
|
|---|
| 260 | ownFlagAtBase = true;
|
|---|
| 261 | break;
|
|---|
| 262 | }
|
|---|
| 263 | }
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | if (ownFlagAtBase) {
|
|---|
| 267 | it->second.hasRedFlag = false;
|
|---|
| 268 | flagType = WorldMap::OBJECT_RED_FLAG;
|
|---|
| 269 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
|---|
| 270 | flagTurnedIn = true;
|
|---|
| 271 | scoreBlue++;
|
|---|
| 272 | }
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | break;
|
|---|
| 276 | }
|
|---|
| 277 | case WorldMap::STRUCTURE_RED_FLAG:
|
|---|
| 278 | {
|
|---|
| 279 | if (it->second.team == 1 && it->second.hasBlueFlag)
|
|---|
| 280 | {
|
|---|
| 281 | // check that your flag is at your base
|
|---|
| 282 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
|---|
| 283 |
|
|---|
| 284 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
|---|
| 285 | vector<WorldMap::Object>::iterator itObjects;
|
|---|
| 286 |
|
|---|
| 287 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
|---|
| 288 | if (itObjects->type == WorldMap::OBJECT_RED_FLAG) {
|
|---|
| 289 | if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
|
|---|
| 290 | ownFlagAtBase = true;
|
|---|
| 291 | break;
|
|---|
| 292 | }
|
|---|
| 293 | }
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | if (ownFlagAtBase) {
|
|---|
| 297 | it->second.hasBlueFlag = false;
|
|---|
| 298 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
|---|
| 299 | pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
|---|
| 300 | flagTurnedIn = true;
|
|---|
| 301 | scoreRed++;
|
|---|
| 302 | }
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | break;
|
|---|
| 306 | }
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | if (flagTurnedIn) {
|
|---|
| 310 | // send an OBJECT message to add the flag back to its spawn point
|
|---|
| 311 | pos.x = pos.x*25+12;
|
|---|
| 312 | pos.y = pos.y*25+12;
|
|---|
| 313 | gameMap->addObject(flagType, pos.x, pos.y);
|
|---|
| 314 |
|
|---|
| 315 | serverMsg.type = MSG_TYPE_OBJECT;
|
|---|
| 316 | gameMap->getObjects()->back().serialize(serverMsg.buffer);
|
|---|
| 317 |
|
|---|
| 318 | map<unsigned int, Player>::iterator it2;
|
|---|
| 319 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
|---|
| 320 | {
|
|---|
| 321 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
|---|
| 322 | error("sendMessage");
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | serverMsg.type = MSG_TYPE_SCORE;
|
|---|
| 326 | memcpy(serverMsg.buffer, &scoreBlue, 4);
|
|---|
| 327 | memcpy(serverMsg.buffer+4, &scoreRed, 4);
|
|---|
| 328 |
|
|---|
| 329 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
|---|
| 330 | {
|
|---|
| 331 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
|---|
| 332 | error("sendMessage");
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | // this means a PLAYER message will be sent
|
|---|
| 336 | broadcastMove = true;
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | // go through all objects and check if the player is close to one and if its their flag
|
|---|
| 340 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
|---|
| 341 | vector<WorldMap::Object>::iterator itObjects;
|
|---|
| 342 | POSITION structPos;
|
|---|
| 343 |
|
|---|
| 344 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
|---|
| 345 | POSITION pos = itObjects->pos;
|
|---|
| 346 |
|
|---|
| 347 | if (posDistance(it->second.pos, pos.toFloat()) < 10) {
|
|---|
| 348 | if (it->second.team == 0 &&
|
|---|
| 349 | itObjects->type == WorldMap::OBJECT_BLUE_FLAG) {
|
|---|
| 350 | structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
|---|
| 351 | flagReturned = true;
|
|---|
| 352 | break;
|
|---|
| 353 | } else if (it->second.team == 1 &&
|
|---|
| 354 | itObjects->type == WorldMap::OBJECT_RED_FLAG) {
|
|---|
| 355 | structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
|---|
| 356 | flagReturned = true;
|
|---|
| 357 | break;
|
|---|
| 358 | }
|
|---|
| 359 | }
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | if (flagReturned) {
|
|---|
| 363 | itObjects->pos.x = structPos.x*25+12;
|
|---|
| 364 | itObjects->pos.y = structPos.y*25+12;
|
|---|
| 365 |
|
|---|
| 366 | serverMsg.type = MSG_TYPE_OBJECT;
|
|---|
| 367 | itObjects->serialize(serverMsg.buffer);
|
|---|
| 368 |
|
|---|
| 369 | map<unsigned int, Player>::iterator it2;
|
|---|
| 370 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
|---|
| 371 | {
|
|---|
| 372 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
|---|
| 373 | error("sendMessage");
|
|---|
| 374 | }
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | if (broadcastMove) {
|
|---|
| 378 | serverMsg.type = MSG_TYPE_PLAYER;
|
|---|
| 379 | it->second.serialize(serverMsg.buffer);
|
|---|
| 380 |
|
|---|
| 381 | cout << "about to broadcast move" << endl;
|
|---|
| 382 | map<unsigned int, Player>::iterator it2;
|
|---|
| 383 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
|---|
| 384 | {
|
|---|
| 385 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
|---|
| 386 | error("sendMessage");
|
|---|
| 387 | }
|
|---|
| 388 | }
|
|---|
| 389 | }
|
|---|
| 390 |
|
|---|
| 391 | // check if the player's attack animation is complete
|
|---|
| 392 | if (it->second.isAttacking && it->second.timeAttackStarted+it->second.attackCooldown <= getCurrentMillis()) {
|
|---|
| 393 | it->second.isAttacking = false;
|
|---|
| 394 | cout << "Attack animation is complete" << endl;
|
|---|
| 395 |
|
|---|
| 396 | //send everyone an ATTACK message
|
|---|
| 397 | cout << "about to broadcast attack" << endl;
|
|---|
| 398 |
|
|---|
| 399 | serverMsg.type = MSG_TYPE_ATTACK;
|
|---|
| 400 | memcpy(serverMsg.buffer, &it->second.id, 4);
|
|---|
| 401 | memcpy(serverMsg.buffer+4, &it->second.targetPlayer, 4);
|
|---|
| 402 |
|
|---|
| 403 | map<unsigned int, Player>::iterator it2;
|
|---|
| 404 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
|---|
| 405 | {
|
|---|
| 406 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
|---|
| 407 | error("sendMessage");
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 | if (it->second.attackType == Player::ATTACK_MELEE) {
|
|---|
| 411 | cout << "Melee attack" << endl;
|
|---|
| 412 |
|
|---|
| 413 | Player* target = &mapPlayers[it->second.targetPlayer];
|
|---|
| 414 | damagePlayer(target, it->second.damage);
|
|---|
| 415 |
|
|---|
| 416 | if (target->isDead) {
|
|---|
| 417 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
|---|
| 418 | if (target->hasBlueFlag)
|
|---|
| 419 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
|---|
| 420 | else if (target->hasRedFlag)
|
|---|
| 421 | flagType = WorldMap::OBJECT_RED_FLAG;
|
|---|
| 422 |
|
|---|
| 423 | if (flagType != WorldMap::OBJECT_NONE) {
|
|---|
| 424 | addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, mapPlayers, msgProcessor, sock);
|
|---|
| 425 | }
|
|---|
| 426 | }
|
|---|
| 427 |
|
|---|
| 428 | serverMsg.type = MSG_TYPE_PLAYER;
|
|---|
| 429 | target->serialize(serverMsg.buffer);
|
|---|
| 430 | }else if (it->second.attackType == Player::ATTACK_RANGED) {
|
|---|
| 431 | cout << "Ranged attack" << endl;
|
|---|
| 432 |
|
|---|
| 433 | Projectile proj(it->second.pos.x, it->second.pos.y, it->second.targetPlayer, it->second.damage);
|
|---|
| 434 | proj.id = unusedProjectileId;
|
|---|
| 435 | updateUnusedProjectileId(unusedProjectileId, mapProjectiles);
|
|---|
| 436 | mapProjectiles[proj.id] = proj;
|
|---|
| 437 |
|
|---|
| 438 | int x = it->second.pos.x;
|
|---|
| 439 | int y = it->second.pos.y;
|
|---|
| 440 |
|
|---|
| 441 | serverMsg.type = MSG_TYPE_PROJECTILE;
|
|---|
| 442 | memcpy(serverMsg.buffer, &proj.id, 4);
|
|---|
| 443 | memcpy(serverMsg.buffer+4, &x, 4);
|
|---|
| 444 | memcpy(serverMsg.buffer+8, &y, 4);
|
|---|
| 445 | memcpy(serverMsg.buffer+12, &it->second.targetPlayer, 4);
|
|---|
| 446 | }else {
|
|---|
| 447 | cout << "Invalid attack type: " << it->second.attackType << endl;
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | // broadcast either a PLAYER or PROJECTILE message
|
|---|
| 451 | cout << "Broadcasting player or projectile message" << endl;
|
|---|
| 452 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
|---|
| 453 | {
|
|---|
| 454 | if (msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
|---|
| 455 | error("sendMessage");
|
|---|
| 456 | }
|
|---|
| 457 | cout << "Done broadcasting" << endl;
|
|---|
| 458 | }
|
|---|
| 459 | }
|
|---|
| 460 |
|
|---|
| 461 | // move all projectiles
|
|---|
| 462 | map<unsigned int, Projectile>::iterator itProj;
|
|---|
| 463 | for (itProj = mapProjectiles.begin(); itProj != mapProjectiles.end(); itProj++) {
|
|---|
| 464 | cout << "About to call projectile move" << endl;
|
|---|
| 465 | if (itProj->second.move(mapPlayers)) {
|
|---|
| 466 | // send a REMOVE_PROJECTILE message
|
|---|
| 467 | cout << "send a REMOVE_PROJECTILE message" << endl;
|
|---|
| 468 | serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE;
|
|---|
| 469 | memcpy(serverMsg.buffer, &itProj->second.id, 4);
|
|---|
| 470 | mapProjectiles.erase(itProj->second.id);
|
|---|
| 471 |
|
|---|
| 472 | map<unsigned int, Player>::iterator it2;
|
|---|
| 473 | cout << "Broadcasting REMOVE_PROJECTILE" << endl;
|
|---|
| 474 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
|---|
| 475 | {
|
|---|
| 476 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
|---|
| 477 | error("sendMessage");
|
|---|
| 478 | }
|
|---|
| 479 |
|
|---|
| 480 | cout << "send a PLAYER message after dealing damage" << endl;
|
|---|
| 481 | // send a PLAYER message after dealing damage
|
|---|
| 482 | Player* target = &mapPlayers[itProj->second.target];
|
|---|
| 483 |
|
|---|
| 484 | damagePlayer(target, itProj->second.damage);
|
|---|
| 485 |
|
|---|
| 486 | if (target->isDead) {
|
|---|
| 487 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
|---|
| 488 | if (target->hasBlueFlag)
|
|---|
| 489 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
|---|
| 490 | else if (target->hasRedFlag)
|
|---|
| 491 | flagType = WorldMap::OBJECT_RED_FLAG;
|
|---|
| 492 |
|
|---|
| 493 | if (flagType != WorldMap::OBJECT_NONE) {
|
|---|
| 494 | addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, mapPlayers, msgProcessor, sock);
|
|---|
| 495 | }
|
|---|
| 496 | }
|
|---|
| 497 |
|
|---|
| 498 | serverMsg.type = MSG_TYPE_PLAYER;
|
|---|
| 499 | target->serialize(serverMsg.buffer);
|
|---|
| 500 |
|
|---|
| 501 | cout << "Sending a PLAYER message" << endl;
|
|---|
| 502 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
|---|
| 503 | {
|
|---|
| 504 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
|
|---|
| 505 | error("sendMessage");
|
|---|
| 506 | }
|
|---|
| 507 | }
|
|---|
| 508 | cout << "Projectile was not moved" << endl;
|
|---|
| 509 | }
|
|---|
| 510 | }
|
|---|
| 511 |
|
|---|
| 512 | n = msgProcessor.receiveMessage(&clientMsg, sock, &from);
|
|---|
| 513 |
|
|---|
| 514 | if (n >= 0) {
|
|---|
| 515 | broadcastResponse = processMessage(clientMsg, from, msgProcessor, mapPlayers, gameMap, unusedPlayerId, serverMsg, sock, scoreBlue, scoreRed);
|
|---|
| 516 |
|
|---|
| 517 | if (broadcastResponse)
|
|---|
| 518 | {
|
|---|
| 519 | cout << "Should be broadcasting the message" << endl;
|
|---|
| 520 |
|
|---|
| 521 | map<unsigned int, Player>::iterator it;
|
|---|
| 522 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
|---|
| 523 | {
|
|---|
| 524 | cout << "Sent message back to " << it->second.name << endl;
|
|---|
| 525 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
|---|
| 526 | error("sendMessage");
|
|---|
| 527 | }
|
|---|
| 528 | }
|
|---|
| 529 | else
|
|---|
| 530 | {
|
|---|
| 531 | cout << "Should be sending back the message" << endl;
|
|---|
| 532 |
|
|---|
| 533 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 )
|
|---|
| 534 | error("sendMessage");
|
|---|
| 535 | }
|
|---|
| 536 | }
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | return 0;
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed)
|
|---|
| 543 | {
|
|---|
| 544 | DataAccess da;
|
|---|
| 545 |
|
|---|
| 546 | cout << "Inside processMessage" << endl;
|
|---|
| 547 |
|
|---|
| 548 | cout << "Received message" << endl;
|
|---|
| 549 | cout << "MSG: type: " << clientMsg.type << endl;
|
|---|
| 550 | cout << "MSG contents: " << clientMsg.buffer << endl;
|
|---|
| 551 |
|
|---|
| 552 | // maybe we should make a message class and have this be a member
|
|---|
| 553 | bool broadcastResponse = false;
|
|---|
| 554 |
|
|---|
| 555 | // Check that if an invalid message is sent, the client will correctly
|
|---|
| 556 | // receive and display the response. Maybe make a special error msg type
|
|---|
| 557 | switch(clientMsg.type)
|
|---|
| 558 | {
|
|---|
| 559 | case MSG_TYPE_REGISTER:
|
|---|
| 560 | {
|
|---|
| 561 | string username(clientMsg.buffer);
|
|---|
| 562 | string password(strchr(clientMsg.buffer, '\0')+1);
|
|---|
| 563 | Player::PlayerClass playerClass;
|
|---|
| 564 |
|
|---|
| 565 | memcpy(&playerClass, clientMsg.buffer+username.length()+password.length()+2, 4);
|
|---|
| 566 |
|
|---|
| 567 | cout << "username: " << username << endl;
|
|---|
| 568 | cout << "password: " << password << endl;
|
|---|
| 569 |
|
|---|
| 570 | if (playerClass == Player::CLASS_WARRIOR)
|
|---|
| 571 | cout << "class: WARRIOR" << endl;
|
|---|
| 572 | else if (playerClass == Player::CLASS_RANGER)
|
|---|
| 573 | cout << "class: RANGER" << endl;
|
|---|
| 574 | else
|
|---|
| 575 | cout << "Unknown player class detected" << endl;
|
|---|
| 576 |
|
|---|
| 577 | int error = da.insertPlayer(username, password, playerClass);
|
|---|
| 578 |
|
|---|
| 579 | if (!error)
|
|---|
| 580 | strcpy(serverMsg.buffer, "Registration successful.");
|
|---|
| 581 | else
|
|---|
| 582 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
|---|
| 583 |
|
|---|
| 584 | serverMsg.type = MSG_TYPE_REGISTER;
|
|---|
| 585 |
|
|---|
| 586 | break;
|
|---|
| 587 | }
|
|---|
| 588 | case MSG_TYPE_LOGIN:
|
|---|
| 589 | {
|
|---|
| 590 | cout << "Got login message" << endl;
|
|---|
| 591 |
|
|---|
| 592 | serverMsg.type = MSG_TYPE_LOGIN;
|
|---|
| 593 |
|
|---|
| 594 | string username(clientMsg.buffer);
|
|---|
| 595 | string password(strchr(clientMsg.buffer, '\0')+1);
|
|---|
| 596 |
|
|---|
| 597 | Player* p = da.getPlayer(username);
|
|---|
| 598 |
|
|---|
| 599 | if (p == NULL || !da.verifyPassword(password, p->password))
|
|---|
| 600 | {
|
|---|
| 601 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
|---|
| 602 | }
|
|---|
| 603 | else if(findPlayerByName(mapPlayers, username) != NULL)
|
|---|
| 604 | {
|
|---|
| 605 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
|---|
| 606 | }
|
|---|
| 607 | else
|
|---|
| 608 | {
|
|---|
| 609 | serverMsg.type = MSG_TYPE_PLAYER;
|
|---|
| 610 |
|
|---|
| 611 | updateUnusedPlayerId(unusedPlayerId, mapPlayers);
|
|---|
| 612 | p->id = unusedPlayerId;
|
|---|
| 613 | cout << "new player id: " << p->id << endl;
|
|---|
| 614 | p->setAddr(from);
|
|---|
| 615 |
|
|---|
| 616 | // choose a random team (either 0 or 1)
|
|---|
| 617 | p->team = rand() % 2;
|
|---|
| 618 |
|
|---|
| 619 | // tell the new player about all the existing players
|
|---|
| 620 | cout << "Sending other players to new player" << endl;
|
|---|
| 621 |
|
|---|
| 622 | map<unsigned int, Player>::iterator it;
|
|---|
| 623 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
|---|
| 624 | {
|
|---|
| 625 | it->second.serialize(serverMsg.buffer);
|
|---|
| 626 |
|
|---|
| 627 | cout << "sending info about " << it->second.name << endl;
|
|---|
| 628 | cout << "sending id " << it->second.id << endl;
|
|---|
| 629 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 )
|
|---|
| 630 | error("sendMessage");
|
|---|
| 631 | }
|
|---|
| 632 |
|
|---|
| 633 | // tell the new player about all map objects
|
|---|
| 634 | // (currently just the flags)
|
|---|
| 635 | serverMsg.type = MSG_TYPE_OBJECT;
|
|---|
| 636 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
|---|
| 637 | vector<WorldMap::Object>::iterator itObjects;
|
|---|
| 638 | cout << "sending items" << endl;
|
|---|
| 639 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
|---|
| 640 | itObjects->serialize(serverMsg.buffer);
|
|---|
| 641 | cout << "sending item id " << itObjects->id << endl;
|
|---|
| 642 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 )
|
|---|
| 643 | error("sendMessage");
|
|---|
| 644 | }
|
|---|
| 645 |
|
|---|
| 646 | // send the current score
|
|---|
| 647 | serverMsg.type = MSG_TYPE_SCORE;
|
|---|
| 648 | memcpy(serverMsg.buffer, &scoreBlue, 4);
|
|---|
| 649 | memcpy(serverMsg.buffer+4, &scoreRed, 4);
|
|---|
| 650 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 )
|
|---|
| 651 | error("sendMessage");
|
|---|
| 652 |
|
|---|
| 653 | serverMsg.type = MSG_TYPE_PLAYER;
|
|---|
| 654 | p->serialize(serverMsg.buffer);
|
|---|
| 655 | cout << "Should be broadcasting the message" << endl;
|
|---|
| 656 |
|
|---|
| 657 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
|---|
| 658 | {
|
|---|
| 659 | cout << "Sent message back to " << it->second.name << endl;
|
|---|
| 660 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
|---|
| 661 | error("sendMessage");
|
|---|
| 662 | }
|
|---|
| 663 |
|
|---|
| 664 | serverMsg.type = MSG_TYPE_LOGIN;
|
|---|
| 665 | mapPlayers[unusedPlayerId] = *p;
|
|---|
| 666 | }
|
|---|
| 667 |
|
|---|
| 668 | delete(p);
|
|---|
| 669 |
|
|---|
| 670 | break;
|
|---|
| 671 | }
|
|---|
| 672 | case MSG_TYPE_LOGOUT:
|
|---|
| 673 | {
|
|---|
| 674 | string name(clientMsg.buffer);
|
|---|
| 675 | cout << "Player logging out: " << name << endl;
|
|---|
| 676 |
|
|---|
| 677 | Player *p = findPlayerByName(mapPlayers, name);
|
|---|
| 678 |
|
|---|
| 679 | if (p == NULL)
|
|---|
| 680 | {
|
|---|
| 681 | strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
|---|
| 682 | cout << "Player not logged in" << endl;
|
|---|
| 683 | }
|
|---|
| 684 | else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
|---|
| 685 | p->addr.sin_port != from.sin_port )
|
|---|
| 686 | {
|
|---|
| 687 | strcpy(serverMsg.buffer, "That player is logged in using a differemt connection. This is either a bug, or you're trying to hack the server.");
|
|---|
| 688 | cout << "Player logged in using a different connection" << endl;
|
|---|
| 689 | }
|
|---|
| 690 | else
|
|---|
| 691 | {
|
|---|
| 692 | if (!p->isDead) {
|
|---|
| 693 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
|---|
| 694 | if (p->hasBlueFlag)
|
|---|
| 695 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
|---|
| 696 | else if (p->hasRedFlag)
|
|---|
| 697 | flagType = WorldMap::OBJECT_RED_FLAG;
|
|---|
| 698 |
|
|---|
| 699 | if (flagType != WorldMap::OBJECT_NONE) {
|
|---|
| 700 | addObjectToMap(flagType, p->pos.x, p->pos.y, gameMap, mapPlayers, msgProcessor, sock);
|
|---|
| 701 | }
|
|---|
| 702 | }
|
|---|
| 703 |
|
|---|
| 704 | if (p->id < unusedPlayerId)
|
|---|
| 705 | unusedPlayerId = p->id;
|
|---|
| 706 | mapPlayers.erase(p->id);
|
|---|
| 707 | strcpy(serverMsg.buffer, "You have successfully logged out.");
|
|---|
| 708 | }
|
|---|
| 709 |
|
|---|
| 710 | serverMsg.type = MSG_TYPE_LOGOUT;
|
|---|
| 711 |
|
|---|
| 712 | break;
|
|---|
| 713 | }
|
|---|
| 714 | case MSG_TYPE_CHAT:
|
|---|
| 715 | {
|
|---|
| 716 | cout << "Got a chat message" << endl;
|
|---|
| 717 |
|
|---|
| 718 | Player *p = findPlayerByAddr(mapPlayers, from);
|
|---|
| 719 |
|
|---|
| 720 | if (p == NULL)
|
|---|
| 721 | {
|
|---|
| 722 | strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server.");
|
|---|
| 723 | }
|
|---|
| 724 | else
|
|---|
| 725 | {
|
|---|
| 726 | broadcastResponse = true;
|
|---|
| 727 |
|
|---|
| 728 | ostringstream oss;
|
|---|
| 729 | oss << p->name << ": " << clientMsg.buffer;
|
|---|
| 730 |
|
|---|
| 731 | strcpy(serverMsg.buffer, oss.str().c_str());
|
|---|
| 732 | }
|
|---|
| 733 |
|
|---|
| 734 | serverMsg.type = MSG_TYPE_CHAT;
|
|---|
| 735 |
|
|---|
| 736 | break;
|
|---|
| 737 | }
|
|---|
| 738 | case MSG_TYPE_PLAYER_MOVE:
|
|---|
| 739 | {
|
|---|
| 740 | cout << "PLAYER_MOVE" << endl;
|
|---|
| 741 |
|
|---|
| 742 | int id, x, y;
|
|---|
| 743 |
|
|---|
| 744 | memcpy(&id, clientMsg.buffer, 4);
|
|---|
| 745 | memcpy(&x, clientMsg.buffer+4, 4);
|
|---|
| 746 | memcpy(&y, clientMsg.buffer+8, 4);
|
|---|
| 747 |
|
|---|
| 748 | cout << "x: " << x << endl;
|
|---|
| 749 | cout << "y: " << y << endl;
|
|---|
| 750 | cout << "id: " << id << endl;
|
|---|
| 751 |
|
|---|
| 752 | if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr &&
|
|---|
| 753 | mapPlayers[id].addr.sin_port == from.sin_port )
|
|---|
| 754 | {
|
|---|
| 755 | // we need to make sure the player can move here
|
|---|
| 756 | if (0 <= x && x < gameMap->width*25 && 0 <= y && y < gameMap->height*25 &&
|
|---|
| 757 | gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
|
|---|
| 758 | {
|
|---|
| 759 | cout << "valid terrain" << endl;
|
|---|
| 760 |
|
|---|
| 761 | mapPlayers[id].target.x = x;
|
|---|
| 762 | mapPlayers[id].target.y = y;
|
|---|
| 763 |
|
|---|
| 764 | mapPlayers[id].isChasing = false;
|
|---|
| 765 | mapPlayers[id].isAttacking = false;
|
|---|
| 766 |
|
|---|
| 767 | serverMsg.type = MSG_TYPE_PLAYER_MOVE;
|
|---|
| 768 |
|
|---|
| 769 | memcpy(serverMsg.buffer, &id, 4);
|
|---|
| 770 | memcpy(serverMsg.buffer+4, &mapPlayers[id].target.x, 4);
|
|---|
| 771 | memcpy(serverMsg.buffer+8, &mapPlayers[id].target.y, 4);
|
|---|
| 772 |
|
|---|
| 773 | broadcastResponse = true;
|
|---|
| 774 | }
|
|---|
| 775 | else
|
|---|
| 776 | cout << "Bad terrain detected" << endl;
|
|---|
| 777 | }
|
|---|
| 778 | else // nned to send back a message indicating failure
|
|---|
| 779 | cout << "Player id (" << id << ") doesn't match sender" << endl;
|
|---|
| 780 |
|
|---|
| 781 | break;
|
|---|
| 782 | }
|
|---|
| 783 | case MSG_TYPE_PICKUP_FLAG:
|
|---|
| 784 | {
|
|---|
| 785 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
|---|
| 786 | cout << "PICKUP_FLAG" << endl;
|
|---|
| 787 |
|
|---|
| 788 | int id;
|
|---|
| 789 |
|
|---|
| 790 | memcpy(&id, clientMsg.buffer, 4);
|
|---|
| 791 | cout << "id: " << id << endl;
|
|---|
| 792 |
|
|---|
| 793 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
|---|
| 794 | vector<WorldMap::Object>::iterator itObjects;
|
|---|
| 795 |
|
|---|
| 796 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end();) {
|
|---|
| 797 | POSITION pos = itObjects->pos;
|
|---|
| 798 | bool gotFlag = false;
|
|---|
| 799 |
|
|---|
| 800 | if (posDistance(mapPlayers[id].pos, pos.toFloat()) < 10) {
|
|---|
| 801 | switch (itObjects->type) {
|
|---|
| 802 | case WorldMap::OBJECT_BLUE_FLAG:
|
|---|
| 803 | if (mapPlayers[id].team == 1) {
|
|---|
| 804 | gotFlag = true;
|
|---|
| 805 | mapPlayers[id].hasBlueFlag = true;
|
|---|
| 806 | broadcastResponse = true;
|
|---|
| 807 | }
|
|---|
| 808 | break;
|
|---|
| 809 | case WorldMap::OBJECT_RED_FLAG:
|
|---|
| 810 | if (mapPlayers[id].team == 0) {
|
|---|
| 811 | gotFlag = true;
|
|---|
| 812 | mapPlayers[id].hasRedFlag = true;
|
|---|
| 813 | broadcastResponse = true;
|
|---|
| 814 | }
|
|---|
| 815 | break;
|
|---|
| 816 | }
|
|---|
| 817 |
|
|---|
| 818 | if (gotFlag) {
|
|---|
| 819 | serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
|
|---|
| 820 | memcpy(serverMsg.buffer, &itObjects->id, 4);
|
|---|
| 821 |
|
|---|
| 822 | map<unsigned int, Player>::iterator it;
|
|---|
| 823 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
|---|
| 824 | {
|
|---|
| 825 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
|---|
| 826 | error("sendMessage");
|
|---|
| 827 | }
|
|---|
| 828 |
|
|---|
| 829 | // remove the object from the server-side map
|
|---|
| 830 | cout << "size before: " << gameMap->getObjects()->size() << endl;
|
|---|
| 831 | itObjects = vctObjects->erase(itObjects);
|
|---|
| 832 | cout << "size after: " << gameMap->getObjects()->size() << endl;
|
|---|
| 833 | }
|
|---|
| 834 | }
|
|---|
| 835 |
|
|---|
| 836 | if (!gotFlag)
|
|---|
| 837 | itObjects++;
|
|---|
| 838 | }
|
|---|
| 839 |
|
|---|
| 840 | serverMsg.type = MSG_TYPE_PLAYER;
|
|---|
| 841 | mapPlayers[id].serialize(serverMsg.buffer);
|
|---|
| 842 |
|
|---|
| 843 | break;
|
|---|
| 844 | }
|
|---|
| 845 | case MSG_TYPE_DROP_FLAG:
|
|---|
| 846 | {
|
|---|
| 847 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
|---|
| 848 | cout << "DROP_FLAG" << endl;
|
|---|
| 849 |
|
|---|
| 850 | int id;
|
|---|
| 851 |
|
|---|
| 852 | memcpy(&id, clientMsg.buffer, 4);
|
|---|
| 853 | cout << "id: " << id << endl;
|
|---|
| 854 |
|
|---|
| 855 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
|---|
| 856 | if (mapPlayers[id].hasBlueFlag)
|
|---|
| 857 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
|---|
| 858 | else if (mapPlayers[id].hasRedFlag)
|
|---|
| 859 | flagType = WorldMap::OBJECT_RED_FLAG;
|
|---|
| 860 |
|
|---|
| 861 | addObjectToMap(flagType, mapPlayers[id].pos.x, mapPlayers[id].pos.y, gameMap, mapPlayers, msgProcessor, sock);
|
|---|
| 862 |
|
|---|
| 863 | mapPlayers[id].hasBlueFlag = false;
|
|---|
| 864 | mapPlayers[id].hasRedFlag = false;
|
|---|
| 865 |
|
|---|
| 866 | serverMsg.type = MSG_TYPE_PLAYER;
|
|---|
| 867 | mapPlayers[id].serialize(serverMsg.buffer);
|
|---|
| 868 |
|
|---|
| 869 | broadcastResponse = true;
|
|---|
| 870 |
|
|---|
| 871 | break;
|
|---|
| 872 | }
|
|---|
| 873 | case MSG_TYPE_START_ATTACK:
|
|---|
| 874 | {
|
|---|
| 875 | cout << "Received a START_ATTACK message" << endl;
|
|---|
| 876 |
|
|---|
| 877 | int id, targetId;
|
|---|
| 878 |
|
|---|
| 879 | memcpy(&id, clientMsg.buffer, 4);
|
|---|
| 880 | memcpy(&targetId, clientMsg.buffer+4, 4);
|
|---|
| 881 |
|
|---|
| 882 | Player* source = &mapPlayers[id];
|
|---|
| 883 | source->targetPlayer = targetId;
|
|---|
| 884 | source->isChasing = true;
|
|---|
| 885 |
|
|---|
| 886 | // this is irrelevant since the client doesn't even listen for START_ATTACK messages
|
|---|
| 887 | // actually, the client should not ignore this and should instead perform the same movement
|
|---|
| 888 | // algorithm on its end (following the target player until in range) that the server does.
|
|---|
| 889 | // Once the attacker is in range, the client should stop movement and wait for messages
|
|---|
| 890 | // from the server
|
|---|
| 891 | serverMsg.type = MSG_TYPE_START_ATTACK;
|
|---|
| 892 | memcpy(serverMsg.buffer, &id, 4);
|
|---|
| 893 | memcpy(serverMsg.buffer+4, &targetId, 4);
|
|---|
| 894 | broadcastResponse = true;
|
|---|
| 895 |
|
|---|
| 896 | break;
|
|---|
| 897 | }
|
|---|
| 898 | case MSG_TYPE_ATTACK:
|
|---|
| 899 | {
|
|---|
| 900 | cout << "Received am ATTACK message" << endl;
|
|---|
| 901 | cout << "ERROR: Clients should not send ATTACK messages" << endl;
|
|---|
| 902 |
|
|---|
| 903 | break;
|
|---|
| 904 | }
|
|---|
| 905 | default:
|
|---|
| 906 | {
|
|---|
| 907 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
|---|
| 908 |
|
|---|
| 909 | serverMsg.type = MSG_TYPE_CHAT;
|
|---|
| 910 |
|
|---|
| 911 | break;
|
|---|
| 912 | }
|
|---|
| 913 | }
|
|---|
| 914 |
|
|---|
| 915 | return broadcastResponse;
|
|---|
| 916 | }
|
|---|
| 917 |
|
|---|
| 918 | void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
|
|---|
| 919 | {
|
|---|
| 920 | while (mapPlayers.find(id) != mapPlayers.end())
|
|---|
| 921 | id++;
|
|---|
| 922 | }
|
|---|
| 923 |
|
|---|
| 924 | void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles)
|
|---|
| 925 | {
|
|---|
| 926 | while (mapProjectiles.find(id) != mapProjectiles.end())
|
|---|
| 927 | id++;
|
|---|
| 928 | }
|
|---|
| 929 |
|
|---|
| 930 | void damagePlayer(Player *p, int damage) {
|
|---|
| 931 | p->health -= damage;
|
|---|
| 932 | if (p->health < 0)
|
|---|
| 933 | p->health = 0;
|
|---|
| 934 | if (p->health == 0) {
|
|---|
| 935 | cout << "Player died" << endl;
|
|---|
| 936 | p->isDead = true;
|
|---|
| 937 | p->timeDied = getCurrentMillis();
|
|---|
| 938 | }
|
|---|
| 939 | }
|
|---|
| 940 |
|
|---|
| 941 | void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player>& mapPlayers, MessageProcessor &msgProcessor, int sock) {
|
|---|
| 942 | NETWORK_MSG serverMsg;
|
|---|
| 943 |
|
|---|
| 944 | gameMap->addObject(objectType, x, y);
|
|---|
| 945 |
|
|---|
| 946 | // need to send the OBJECT message too
|
|---|
| 947 | serverMsg.type = MSG_TYPE_OBJECT;
|
|---|
| 948 | gameMap->getObjects()->back().serialize(serverMsg.buffer);
|
|---|
| 949 |
|
|---|
| 950 | map<unsigned int, Player>::iterator it;
|
|---|
| 951 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
|---|
| 952 | {
|
|---|
| 953 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
|---|
| 954 | error("sendMessage");
|
|---|
| 955 | }
|
|---|
| 956 | }
|
|---|