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