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