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