- Timestamp:
- Jun 3, 2013, 11:05:20 PM (11 years ago)
- Branches:
- master
- Children:
- d03ec0f
- Parents:
- 8a4ed74
- Location:
- server
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
server/makefile
r8a4ed74 r8dad966 3 3 FLAGS = $(LIB_FLAGS) 4 4 COMMON_PATH = ../common 5 DEPENDENCIES = Common.o Message.o Player.o WorldMap.o DataAccess.o 5 DEPENDENCIES = Common.o Message.o Player.o WorldMap.o DataAccess.o Projectile.o 6 6 7 7 server : server.cpp $(DEPENDENCIES) … … 20 20 $(CC) -c -o $@ $? 21 21 22 Projectile.o : $(COMMON_PATH)/Projectile.cpp 23 $(CC) -c -o $@ $? 24 22 25 %.o : %.cpp 23 26 $(CC) -c -o $@ $? -
server/server.cpp
r8a4ed74 r8dad966 31 31 #include "../common/WorldMap.h" 32 32 #include "../common/Player.h" 33 #include "../common/Projectile.h" 33 34 34 35 #include "DataAccess.h" … … 38 39 // from used to be const. Removed that so I could take a reference 39 40 // and use it to send messages 40 bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed); 41 42 void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers); 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); 42 43 void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers); 44 void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles); 43 45 44 46 // this should probably go somewhere in the common folder … … 83 85 NETWORK_MSG clientMsg, serverMsg; 84 86 map<unsigned int, Player> mapPlayers; 85 unsigned int unusedId = 1; 87 map<unsigned int, Projectile> mapProjectiles; 88 unsigned int unusedPlayerId = 1, unusedProjectileId = 1; 86 89 int scoreBlue, scoreRed; 87 90 … … 142 145 timeLastUpdated = curTime; 143 146 147 // move all players 144 148 // maybe put this in a separate method 145 149 map<unsigned int, Player>::iterator it; … … 317 321 } 318 322 } 323 324 // check if the player's attack animation is complete 325 if (it->second.isAttacking && it->second.timeAttackStarted+it->second.attackCooldown <= getCurrentMillis()) { 326 it->second.isAttacking = false; 327 328 //send everyone an ATTACK message 329 cout << "about to broadcast attack" << endl; 330 331 serverMsg.type = MSG_TYPE_ATTACK; 332 memcpy(serverMsg.buffer, &it->second.id, 4); 333 memcpy(serverMsg.buffer+4, &it->second.targetPlayer, 4); 334 335 map<unsigned int, Player>::iterator it2; 336 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 337 { 338 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 339 error("sendMessage"); 340 } 341 342 if (it->second.attackType == Player::ATTACK_MELEE) { 343 Player* target = &mapPlayers[it->second.targetPlayer]; 344 345 target->health -= it->second.damage; 346 if (target->health < 0) 347 target->health = 0; 348 349 serverMsg.type = MSG_TYPE_PLAYER; 350 target->serialize(serverMsg.buffer); 351 }else if (it->second.attackType == Player::ATTACK_RANGED) { 352 Projectile proj(it->second.pos.x, it->second.pos.y, it->second.targetPlayer, it->second.damage); 353 proj.id = unusedProjectileId; 354 updateUnusedProjectileId(unusedProjectileId, mapProjectiles); 355 mapProjectiles[proj.id] = proj; 356 357 serverMsg.type = MSG_TYPE_PROJECTILE; 358 memcpy(serverMsg.buffer, &it->second.pos.x, 4); 359 memcpy(serverMsg.buffer+4, &it->second.pos.y, 4); 360 memcpy(serverMsg.buffer+8, &it->second.targetPlayer, 4); 361 }else { 362 cout << "Invalid attack type: " << it->second.attackType << endl; 363 } 364 365 // broadcast either a PLAYER or PROJECTILE message 366 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 367 { 368 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 369 error("sendMessage"); 370 } 371 } 372 } 373 374 // move all projectiles 375 map<unsigned int, Projectile>::iterator itProj; 376 for (itProj = mapProjectiles.begin(); itProj != mapProjectiles.end(); itProj++) { 377 if (itProj->second.move(mapPlayers)) { 378 // send a REMOVE_PROJECTILE message 379 serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE; 380 memcpy(serverMsg.buffer, &itProj->second.id, 4); 381 mapProjectiles.erase(itProj->second.id); 382 383 map<unsigned int, Player>::iterator it2; 384 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 385 { 386 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 387 error("sendMessage"); 388 } 389 390 // send a PLAYER message after dealing damage 391 Player* target = &mapPlayers[it->second.targetPlayer]; 392 393 target->health -= itProj->second.damage; 394 if (target->health < 0) 395 target->health = 0; 396 397 serverMsg.type = MSG_TYPE_PLAYER; 398 target->serialize(serverMsg.buffer); 399 400 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++) 401 { 402 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 ) 403 error("sendMessage"); 404 } 405 } 319 406 } 320 407 } … … 323 410 324 411 if (n >= 0) { 325 broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unused Id, serverMsg, sock, scoreBlue, scoreRed);412 broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedPlayerId, serverMsg, sock, scoreBlue, scoreRed); 326 413 327 414 if (broadcastResponse) … … 350 437 } 351 438 352 bool processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unused Id, NETWORK_MSG& serverMsg, int sock, int &scoreBlue, int &scoreRed)439 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) 353 440 { 354 441 DataAccess da; … … 407 494 serverMsg.type = MSG_TYPE_PLAYER; 408 495 409 updateUnused Id(unusedId, mapPlayers);410 p->id = unused Id;496 updateUnusedPlayerId(unusedPlayerId, mapPlayers); 497 p->id = unusedPlayerId; 411 498 cout << "new player id: " << p->id << endl; 412 499 p->setAddr(from); … … 472 559 473 560 serverMsg.type = MSG_TYPE_LOGIN; 474 mapPlayers[unused Id] = *p;561 mapPlayers[unusedPlayerId] = *p; 475 562 } 476 563 … … 499 586 else 500 587 { 501 if (p->id < unused Id)502 unused Id = p->id;588 if (p->id < unusedPlayerId) 589 unusedPlayerId = p->id; 503 590 mapPlayers.erase(p->id); 504 591 strcpy(serverMsg.buffer, "You have successfully logged out."); … … 672 759 mapPlayers[id].serialize(serverMsg.buffer); 673 760 674 map<unsigned int, Player>::iterator it2;675 761 broadcastResponse = true; 676 762 … … 686 772 memcpy(&targetId, clientMsg.buffer+4, 4); 687 773 774 Player* source = &mapPlayers[id]; 775 source->timeAttackStarted = getCurrentMillis(); 776 source->targetPlayer = targetId; 777 688 778 serverMsg.type = MSG_TYPE_START_ATTACK; 779 memcpy(serverMsg.buffer, &id, 4); 780 memcpy(serverMsg.buffer+4, &targetId, 4); 689 781 broadcastResponse = true; 690 782 … … 694 786 { 695 787 cout << "Received am ATTACK message" << endl; 696 697 int id, targetId; 698 699 memcpy(&id, clientMsg.buffer, 4); 700 memcpy(&targetId, clientMsg.buffer+4, 4); 701 702 serverMsg.type = MSG_TYPE_ATTACK; 703 broadcastResponse = true; 788 cout << "ERROR: Clients should not send ATTACK messages" << endl; 704 789 705 790 break; … … 718 803 } 719 804 720 void updateUnused Id(unsigned int& id, map<unsigned int, Player>& mapPlayers)805 void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers) 721 806 { 722 807 while (mapPlayers.find(id) != mapPlayers.end()) 723 808 id++; 724 809 } 810 811 void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles) 812 { 813 while (mapProjectiles.find(id) != mapProjectiles.end()) 814 id++; 815 }
Note:
See TracChangeset
for help on using the changeset viewer.