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