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