[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 |
|
---|
[9a4fa04] | 521 | cout << "Inside processMessage" << endl;
|
---|
| 522 |
|
---|
[b8cb03f] | 523 | cout << "Received message" << endl;
|
---|
[8e540f4] | 524 | cout << "MSG: type: " << clientMsg.type << endl;
|
---|
| 525 | cout << "MSG contents: " << clientMsg.buffer << endl;
|
---|
| 526 |
|
---|
[da692b9] | 527 | // maybe we should make a message class and have this be a member
|
---|
[3b1efcc] | 528 | bool broadcastResponse = false;
|
---|
| 529 |
|
---|
[8e540f4] | 530 | // Check that if an invalid message is sent, the client will correctly
|
---|
| 531 | // receive and display the response. Maybe make a special error msg type
|
---|
| 532 | switch(clientMsg.type)
|
---|
| 533 | {
|
---|
| 534 | case MSG_TYPE_REGISTER:
|
---|
[d2b411a] | 535 | {
|
---|
[8e540f4] | 536 | string username(clientMsg.buffer);
|
---|
| 537 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[521c88b] | 538 | Player::PlayerClass playerClass;
|
---|
| 539 |
|
---|
| 540 | memcpy(&playerClass, clientMsg.buffer+username.length()+password.length()+2, 4);
|
---|
[d2b411a] | 541 |
|
---|
[8e540f4] | 542 | cout << "username: " << username << endl;
|
---|
| 543 | cout << "password: " << password << endl;
|
---|
[d2b411a] | 544 |
|
---|
[521c88b] | 545 | if (playerClass == Player::CLASS_WARRIOR)
|
---|
| 546 | cout << "class: WARRIOR" << endl;
|
---|
| 547 | else if (playerClass == Player::CLASS_RANGER)
|
---|
| 548 | cout << "class: RANGER" << endl;
|
---|
| 549 | else
|
---|
| 550 | cout << "Unknown player class detected" << endl;
|
---|
| 551 |
|
---|
| 552 | int error = da.insertPlayer(username, password, playerClass);
|
---|
[41ad8ed] | 553 |
|
---|
[3b1efcc] | 554 | if (!error)
|
---|
| 555 | strcpy(serverMsg.buffer, "Registration successful.");
|
---|
| 556 | else
|
---|
| 557 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
---|
[8e540f4] | 558 |
|
---|
| 559 | serverMsg.type = MSG_TYPE_REGISTER;
|
---|
[d2b411a] | 560 |
|
---|
[8e540f4] | 561 | break;
|
---|
| 562 | }
|
---|
| 563 | case MSG_TYPE_LOGIN:
|
---|
| 564 | {
|
---|
[60017fc] | 565 | cout << "Got login message" << endl;
|
---|
| 566 |
|
---|
[d211210] | 567 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
| 568 |
|
---|
[8e540f4] | 569 | string username(clientMsg.buffer);
|
---|
[41ad8ed] | 570 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[8e540f4] | 571 |
|
---|
[41ad8ed] | 572 | Player* p = da.getPlayer(username);
|
---|
[d2b411a] | 573 |
|
---|
[b128109] | 574 | if (p == NULL || !da.verifyPassword(password, p->password))
|
---|
[41ad8ed] | 575 | {
|
---|
| 576 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
---|
| 577 | }
|
---|
[01d0d00] | 578 | else if(findPlayerByName(mapPlayers, username) != NULL)
|
---|
[41ad8ed] | 579 | {
|
---|
| 580 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
| 581 | }
|
---|
| 582 | else
|
---|
[8e540f4] | 583 | {
|
---|
[d211210] | 584 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
| 585 |
|
---|
[8dad966] | 586 | updateUnusedPlayerId(unusedPlayerId, mapPlayers);
|
---|
| 587 | p->id = unusedPlayerId;
|
---|
[d211210] | 588 | cout << "new player id: " << p->id << endl;
|
---|
[df79cfd] | 589 | p->setAddr(from);
|
---|
| 590 |
|
---|
| 591 | // choose a random team (either 0 or 1)
|
---|
| 592 | p->team = rand() % 2;
|
---|
[d211210] | 593 |
|
---|
| 594 | // tell the new player about all the existing players
|
---|
| 595 | cout << "Sending other players to new player" << endl;
|
---|
| 596 |
|
---|
| 597 | map<unsigned int, Player>::iterator it;
|
---|
| 598 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 599 | {
|
---|
| 600 | it->second.serialize(serverMsg.buffer);
|
---|
| 601 |
|
---|
| 602 | cout << "sending info about " << it->second.name << endl;
|
---|
[5f868c0] | 603 | cout << "sending id " << it->second.id << endl;
|
---|
[9b5d30b] | 604 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
[5f868c0] | 605 | error("sendMessage");
|
---|
| 606 | }
|
---|
| 607 |
|
---|
| 608 | // tell the new player about all map objects
|
---|
| 609 | // (currently just the flags)
|
---|
| 610 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
[e487381] | 611 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
[5f868c0] | 612 | vector<WorldMap::Object>::iterator itObjects;
|
---|
| 613 | cout << "sending items" << endl;
|
---|
[e487381] | 614 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
[5f868c0] | 615 | itObjects->serialize(serverMsg.buffer);
|
---|
| 616 | cout << "sending item id " << itObjects->id << endl;
|
---|
[9b5d30b] | 617 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
[d211210] | 618 | error("sendMessage");
|
---|
| 619 | }
|
---|
[59061f6] | 620 |
|
---|
[b8601ee] | 621 | // send the current score
|
---|
| 622 | serverMsg.type = MSG_TYPE_SCORE;
|
---|
| 623 | memcpy(serverMsg.buffer, &scoreBlue, 4);
|
---|
| 624 | memcpy(serverMsg.buffer+4, &scoreRed, 4);
|
---|
[9b5d30b] | 625 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from) < 0 )
|
---|
[b8601ee] | 626 | error("sendMessage");
|
---|
| 627 |
|
---|
| 628 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
[594d2e9] | 629 | p->serialize(serverMsg.buffer);
|
---|
[d211210] | 630 | cout << "Should be broadcasting the message" << endl;
|
---|
| 631 |
|
---|
| 632 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 633 | {
|
---|
| 634 | cout << "Sent message back to " << it->second.name << endl;
|
---|
[9b5d30b] | 635 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
[d211210] | 636 | error("sendMessage");
|
---|
| 637 | }
|
---|
| 638 |
|
---|
| 639 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
[8dad966] | 640 | mapPlayers[unusedPlayerId] = *p;
|
---|
[07028b9] | 641 | }
|
---|
| 642 |
|
---|
[41ad8ed] | 643 | delete(p);
|
---|
[07028b9] | 644 |
|
---|
[8e540f4] | 645 | break;
|
---|
| 646 | }
|
---|
| 647 | case MSG_TYPE_LOGOUT:
|
---|
| 648 | {
|
---|
| 649 | string name(clientMsg.buffer);
|
---|
| 650 | cout << "Player logging out: " << name << endl;
|
---|
| 651 |
|
---|
[01d0d00] | 652 | Player *p = findPlayerByName(mapPlayers, name);
|
---|
[633f42a] | 653 |
|
---|
[8e540f4] | 654 | if (p == NULL)
|
---|
| 655 | {
|
---|
| 656 | strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
---|
[8a3ef42] | 657 | cout << "Player not logged in" << endl;
|
---|
[07028b9] | 658 | }
|
---|
[01d0d00] | 659 | else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
---|
| 660 | p->addr.sin_port != from.sin_port )
|
---|
[07028b9] | 661 | {
|
---|
[8e540f4] | 662 | 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] | 663 | cout << "Player logged in using a different connection" << endl;
|
---|
[2488852] | 664 | }
|
---|
[8e540f4] | 665 | else
|
---|
[2488852] | 666 | {
|
---|
[8dad966] | 667 | if (p->id < unusedPlayerId)
|
---|
| 668 | unusedPlayerId = p->id;
|
---|
[01d0d00] | 669 | mapPlayers.erase(p->id);
|
---|
[41ad8ed] | 670 | strcpy(serverMsg.buffer, "You have successfully logged out.");
|
---|
[8e540f4] | 671 | }
|
---|
[07028b9] | 672 |
|
---|
[d211210] | 673 | serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
[8a3ef42] | 674 |
|
---|
[8e540f4] | 675 | break;
|
---|
| 676 | }
|
---|
| 677 | case MSG_TYPE_CHAT:
|
---|
| 678 | {
|
---|
[da692b9] | 679 | cout << "Got a chat message" << endl;
|
---|
| 680 |
|
---|
[01d0d00] | 681 | Player *p = findPlayerByAddr(mapPlayers, from);
|
---|
[07028b9] | 682 |
|
---|
[8e540f4] | 683 | if (p == NULL)
|
---|
| 684 | {
|
---|
| 685 | 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] | 686 | }
|
---|
[8e540f4] | 687 | else
|
---|
| 688 | {
|
---|
[3b1efcc] | 689 | broadcastResponse = true;
|
---|
| 690 |
|
---|
[b128109] | 691 | ostringstream oss;
|
---|
| 692 | oss << p->name << ": " << clientMsg.buffer;
|
---|
[3b1efcc] | 693 |
|
---|
[b128109] | 694 | strcpy(serverMsg.buffer, oss.str().c_str());
|
---|
[8e540f4] | 695 | }
|
---|
| 696 |
|
---|
| 697 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
| 698 |
|
---|
| 699 | break;
|
---|
[e084950] | 700 | }
|
---|
[b128109] | 701 | case MSG_TYPE_PLAYER_MOVE:
|
---|
| 702 | {
|
---|
| 703 | cout << "PLAYER_MOVE" << endl;
|
---|
| 704 |
|
---|
| 705 | int id, x, y;
|
---|
| 706 |
|
---|
| 707 | memcpy(&id, clientMsg.buffer, 4);
|
---|
| 708 | memcpy(&x, clientMsg.buffer+4, 4);
|
---|
| 709 | memcpy(&y, clientMsg.buffer+8, 4);
|
---|
[7b43385] | 710 |
|
---|
[b128109] | 711 | cout << "x: " << x << endl;
|
---|
| 712 | cout << "y: " << y << endl;
|
---|
| 713 | cout << "id: " << id << endl;
|
---|
[7b43385] | 714 |
|
---|
[b128109] | 715 | if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr &&
|
---|
| 716 | mapPlayers[id].addr.sin_port == from.sin_port )
|
---|
| 717 | {
|
---|
[60017fc] | 718 | // we need to make sure the player can move here
|
---|
[694c3d2] | 719 | if (0 <= x && x < gameMap->width*25 && 0 <= y && y < gameMap->height*25 &&
|
---|
[60017fc] | 720 | gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
|
---|
| 721 | {
|
---|
[7b43385] | 722 | cout << "valid terrain" << endl;
|
---|
| 723 |
|
---|
[60017fc] | 724 | mapPlayers[id].target.x = x;
|
---|
| 725 | mapPlayers[id].target.y = y;
|
---|
| 726 |
|
---|
[5b1e31e] | 727 | mapPlayers[id].isChasing = false;
|
---|
| 728 | mapPlayers[id].isAttacking = false;
|
---|
| 729 |
|
---|
[60017fc] | 730 | serverMsg.type = MSG_TYPE_PLAYER_MOVE;
|
---|
| 731 |
|
---|
| 732 | memcpy(serverMsg.buffer, &id, 4);
|
---|
[d211210] | 733 | memcpy(serverMsg.buffer+4, &mapPlayers[id].target.x, 4);
|
---|
| 734 | memcpy(serverMsg.buffer+8, &mapPlayers[id].target.y, 4);
|
---|
[60017fc] | 735 |
|
---|
| 736 | broadcastResponse = true;
|
---|
| 737 | }
|
---|
| 738 | else
|
---|
| 739 | cout << "Bad terrain detected" << endl;
|
---|
[b128109] | 740 | }
|
---|
| 741 | else // nned to send back a message indicating failure
|
---|
| 742 | cout << "Player id (" << id << ") doesn't match sender" << endl;
|
---|
| 743 |
|
---|
| 744 | break;
|
---|
| 745 | }
|
---|
[5299436] | 746 | case MSG_TYPE_PICKUP_FLAG:
|
---|
| 747 | {
|
---|
| 748 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
| 749 | cout << "PICKUP_FLAG" << endl;
|
---|
| 750 |
|
---|
| 751 | int id;
|
---|
| 752 |
|
---|
| 753 | memcpy(&id, clientMsg.buffer, 4);
|
---|
| 754 | cout << "id: " << id << endl;
|
---|
| 755 |
|
---|
[5c84d54] | 756 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
| 757 | vector<WorldMap::Object>::iterator itObjects;
|
---|
| 758 |
|
---|
| 759 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end();) {
|
---|
| 760 | POSITION pos = itObjects->pos;
|
---|
| 761 | bool gotFlag = false;
|
---|
| 762 |
|
---|
| 763 | if (posDistance(mapPlayers[id].pos, pos.toFloat()) < 10) {
|
---|
| 764 | switch (itObjects->type) {
|
---|
| 765 | case WorldMap::OBJECT_BLUE_FLAG:
|
---|
| 766 | if (mapPlayers[id].team == 1) {
|
---|
| 767 | gotFlag = true;
|
---|
| 768 | mapPlayers[id].hasBlueFlag = true;
|
---|
| 769 | broadcastResponse = true;
|
---|
| 770 | }
|
---|
| 771 | break;
|
---|
| 772 | case WorldMap::OBJECT_RED_FLAG:
|
---|
| 773 | if (mapPlayers[id].team == 0) {
|
---|
| 774 | gotFlag = true;
|
---|
| 775 | mapPlayers[id].hasRedFlag = true;
|
---|
| 776 | broadcastResponse = true;
|
---|
| 777 | }
|
---|
| 778 | break;
|
---|
| 779 | }
|
---|
| 780 |
|
---|
| 781 | if (gotFlag) {
|
---|
| 782 | serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
|
---|
| 783 | memcpy(serverMsg.buffer, &itObjects->id, 4);
|
---|
| 784 |
|
---|
| 785 | map<unsigned int, Player>::iterator it;
|
---|
| 786 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 787 | {
|
---|
[9b5d30b] | 788 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
[5c84d54] | 789 | error("sendMessage");
|
---|
| 790 | }
|
---|
| 791 |
|
---|
| 792 | // remove the object from the server-side map
|
---|
| 793 | cout << "size before: " << gameMap->getObjects()->size() << endl;
|
---|
| 794 | itObjects = vctObjects->erase(itObjects);
|
---|
| 795 | cout << "size after: " << gameMap->getObjects()->size() << endl;
|
---|
| 796 | }
|
---|
| 797 | }
|
---|
| 798 |
|
---|
| 799 | if (!gotFlag)
|
---|
| 800 | itObjects++;
|
---|
| 801 | }
|
---|
| 802 |
|
---|
| 803 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
| 804 | mapPlayers[id].serialize(serverMsg.buffer);
|
---|
| 805 |
|
---|
[5299436] | 806 | break;
|
---|
| 807 | }
|
---|
[e487381] | 808 | case MSG_TYPE_DROP_FLAG:
|
---|
| 809 | {
|
---|
| 810 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
| 811 | cout << "DROP_FLAG" << endl;
|
---|
| 812 |
|
---|
| 813 | int id;
|
---|
| 814 |
|
---|
| 815 | memcpy(&id, clientMsg.buffer, 4);
|
---|
| 816 | cout << "id: " << id << endl;
|
---|
| 817 |
|
---|
| 818 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
---|
| 819 | if (mapPlayers[id].hasBlueFlag)
|
---|
| 820 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
| 821 | else if (mapPlayers[id].hasRedFlag)
|
---|
| 822 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
| 823 |
|
---|
| 824 | gameMap->addObject(flagType, mapPlayers[id].pos.x, mapPlayers[id].pos.y);
|
---|
| 825 |
|
---|
| 826 | // need to send the OBJECT message too
|
---|
| 827 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
| 828 | gameMap->getObjects()->back().serialize(serverMsg.buffer);
|
---|
| 829 |
|
---|
| 830 | map<unsigned int, Player>::iterator it;
|
---|
| 831 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 832 | {
|
---|
[9b5d30b] | 833 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
|
---|
[e487381] | 834 | error("sendMessage");
|
---|
| 835 | }
|
---|
| 836 |
|
---|
| 837 | mapPlayers[id].hasBlueFlag = false;
|
---|
| 838 | mapPlayers[id].hasRedFlag = false;
|
---|
| 839 |
|
---|
| 840 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
| 841 | mapPlayers[id].serialize(serverMsg.buffer);
|
---|
| 842 |
|
---|
| 843 | broadcastResponse = true;
|
---|
| 844 |
|
---|
| 845 | break;
|
---|
| 846 | }
|
---|
[4b4b153] | 847 | case MSG_TYPE_START_ATTACK:
|
---|
| 848 | {
|
---|
| 849 | cout << "Received a START_ATTACK message" << endl;
|
---|
| 850 |
|
---|
[8a4ed74] | 851 | int id, targetId;
|
---|
| 852 |
|
---|
| 853 | memcpy(&id, clientMsg.buffer, 4);
|
---|
| 854 | memcpy(&targetId, clientMsg.buffer+4, 4);
|
---|
| 855 |
|
---|
[8dad966] | 856 | Player* source = &mapPlayers[id];
|
---|
| 857 | source->targetPlayer = targetId;
|
---|
[11d21ee] | 858 | source->isChasing = true;
|
---|
[8dad966] | 859 |
|
---|
[11d21ee] | 860 | // this is irrelevant since the client doesn't even listen for START_ATTACK messages
|
---|
| 861 | // actually, the client should not ignore this and should instead perform the same movement
|
---|
| 862 | // algorithm on its end (following the target player until in range) that the server does.
|
---|
| 863 | // Once the attacker is in range, the client should stop movement and wait for messages
|
---|
| 864 | // from the server
|
---|
[4b4b153] | 865 | serverMsg.type = MSG_TYPE_START_ATTACK;
|
---|
[8dad966] | 866 | memcpy(serverMsg.buffer, &id, 4);
|
---|
| 867 | memcpy(serverMsg.buffer+4, &targetId, 4);
|
---|
[4b4b153] | 868 | broadcastResponse = true;
|
---|
[8a4ed74] | 869 |
|
---|
| 870 | break;
|
---|
[4b4b153] | 871 | }
|
---|
| 872 | case MSG_TYPE_ATTACK:
|
---|
| 873 | {
|
---|
| 874 | cout << "Received am ATTACK message" << endl;
|
---|
[8dad966] | 875 | cout << "ERROR: Clients should not send ATTACK messages" << endl;
|
---|
[8a4ed74] | 876 |
|
---|
| 877 | break;
|
---|
[4b4b153] | 878 | }
|
---|
[8e540f4] | 879 | default:
|
---|
| 880 | {
|
---|
| 881 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
[e084950] | 882 |
|
---|
[8e540f4] | 883 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
[e084950] | 884 |
|
---|
[8e540f4] | 885 | break;
|
---|
| 886 | }
|
---|
[e3535b3] | 887 | }
|
---|
[da692b9] | 888 |
|
---|
| 889 | return broadcastResponse;
|
---|
[e3535b3] | 890 | }
|
---|
[da692b9] | 891 |
|
---|
[8dad966] | 892 | void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
|
---|
[01d0d00] | 893 | {
|
---|
[1106210] | 894 | while (mapPlayers.find(id) != mapPlayers.end())
|
---|
| 895 | id++;
|
---|
[01d0d00] | 896 | }
|
---|
[8dad966] | 897 |
|
---|
| 898 | void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles)
|
---|
| 899 | {
|
---|
| 900 | while (mapProjectiles.find(id) != mapProjectiles.end())
|
---|
| 901 | id++;
|
---|
| 902 | }
|
---|
[c76134b] | 903 |
|
---|
| 904 | void damagePlayer(Player *p, int damage) {
|
---|
| 905 | p->health -= damage;
|
---|
| 906 | if (p->health < 0)
|
---|
| 907 | p->health = 0;
|
---|
| 908 | if (p->health == 0) {
|
---|
[66c4ec4] | 909 | cout << "Player died" << endl;
|
---|
[c76134b] | 910 | p->isDead = true;
|
---|
| 911 | p->timeDied = getCurrentMillis();
|
---|
| 912 | }
|
---|
| 913 | }
|
---|