Changeset f3fb980 in network-game for server/server.cpp


Ignore:
Timestamp:
Dec 25, 2013, 3:21:45 PM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
e0fd377
Parents:
9c18cb7
Message:

Removed global score variables from server

File:
1 edited

Legend:

Unmodified
Added
Removed
  • server/server.cpp

    r9c18cb7 rf3fb980  
    4242using namespace std;
    4343
    44 bool done;
    45 
    4644// from used to be const. Removed that so I could take a reference
    4745// and use it to send messages
    48 void processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int &scoreBlue, int &scoreRed);
     46void processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, MessageProcessor& msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, WorldMap* gameMap, unsigned int& unusedPlayerId);
     47
     48bool handleGameEvents(Game* game, map<unsigned int, Player*>& mapPlayers, MessageProcessor& msgPocessor);
     49bool handlePlayerEvents(Player* p, Game* game, MessageProcessor& msgProcessor);
    4950
    5051void broadcastMessage(MessageProcessor &msgProcessor, NETWORK_MSG &serverMsg, map<unsigned int, Player*>& players);
     
    5657void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player*>& mapPlayers, MessageProcessor &msgProcessor);
    5758
    58 void quit(int sig) {
    59    done = true;
    60 }
     59void quit(int sig);
     60
     61bool done;
    6162
    6263int main(int argc, char *argv[])
     
    7172   map<string, Game*> mapGames;
    7273   unsigned int unusedPlayerId = 1, unusedProjectileId = 1;
    73    int scoreBlue, scoreRed;
    7474   ofstream outputLog;
    7575
    7676   done = false;
    77 
    78    scoreBlue = 0;
    79    scoreRed = 0;
    8077
    8178   signal(SIGINT, quit);
     
    205202                  serverMsg.type = MSG_TYPE_PLAYER;
    206203                  p->serialize(serverMsg.buffer);
    207 
    208204                  broadcastMessage(msgProcessor, serverMsg, playersInGame);
    209205               }
     
    214210
    215211         // process players currently in a game
    216          FLOAT_POSITION oldPos;
    217212         map<string, Game*>::iterator itGames;
    218213         Game* game = NULL;
    219214         WorldMap* gameMap = NULL;
    220          bool gameFinished;
    221215
    222216         for (itGames = mapGames.begin(); itGames != mapGames.end();) {
    223             game = itGames->second;
    224             gameMap = game->getMap();
    225             map<unsigned int, Player*>& playersInGame = game->getPlayers();
    226             gameFinished = false;
    227 
    228             for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
    229             {
    230                Player* p = it->second;
    231 
    232                cout << "moving player" << endl;
    233                bool broadcastMove = false;
    234 
    235                // xompute playersInGame here
    236 
    237                // move player and perform associated tasks
    238                oldPos = p->pos;
    239                if (p->move(gameMap)) {
    240 
    241                   cout << "player moved" << endl;
    242                   if (game->processPlayerMovement(p, oldPos))
    243                       broadcastMove = true;
    244                   cout << "player move processed" << endl;
    245 
    246                   WorldMap::ObjectType flagType;
    247                   POSITION pos;
    248                   bool flagTurnedIn = false;
    249                   bool flagReturned = false;
    250                   bool ownFlagAtBase = false;
    251 
    252                   // need to figure out how to move this to a different file
    253                   // while still sending back flag type and position
    254                   switch(gameMap->getStructure(p->pos.x/25, p->pos.y/25))
    255                   {
    256                      case WorldMap::STRUCTURE_BLUE_FLAG:
    257                      {
    258                         if (p->team == 0 && p->hasRedFlag)
    259                         {
    260                            // check that your flag is at your base
    261                            pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
    262                            
    263                            vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
    264                            vector<WorldMap::Object>::iterator itObjects;
    265 
    266                            for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++)
    267                            {
    268                               if (itObjects->type == WorldMap::OBJECT_BLUE_FLAG)
    269                               {
    270                                  if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12)
    271                                  {
    272                                     ownFlagAtBase = true;
    273                                     break;
    274                                  }
    275                               }
    276                            }
    277 
    278                            if (ownFlagAtBase)
    279                            {
    280                               p->hasRedFlag = false;
    281                               flagType = WorldMap::OBJECT_RED_FLAG;
    282                               pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
    283                               flagTurnedIn = true;
    284                               scoreBlue++;
    285                            }
    286                         }
    287 
    288                         break;
    289                      }
    290                      case WorldMap::STRUCTURE_RED_FLAG:
    291                      {
    292                         if (p->team == 1 && p->hasBlueFlag)
    293                         {
    294                            // check that your flag is at your base
    295                            pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
    296                        
    297                            vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
    298                            vector<WorldMap::Object>::iterator itObjects;
    299 
    300                            for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++)
    301                            {
    302                               if (itObjects->type == WorldMap::OBJECT_RED_FLAG)
    303                               {
    304                                  if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12)
    305                                  {
    306                                     ownFlagAtBase = true;
    307                                     break;
    308                                  }
    309                               }
    310                            }
    311 
    312                            if (ownFlagAtBase)
    313                            {
    314                               p->hasBlueFlag = false;
    315                               flagType = WorldMap::OBJECT_BLUE_FLAG;
    316                               pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
    317                               flagTurnedIn = true;
    318                               scoreRed++;
    319                            }
    320                         }
    321 
    322                         break;
    323                      }
    324                   }
    325 
    326                   if (flagTurnedIn)
    327                   {
    328                      // send an OBJECT message to add the flag back to its spawn point
    329                      pos.x = pos.x*25+12;
    330                      pos.y = pos.y*25+12;
    331                      gameMap->addObject(flagType, pos.x, pos.y);
    332 
    333                      serverMsg.type = MSG_TYPE_OBJECT;
    334                      gameMap->getObjects()->back().serialize(serverMsg.buffer);
    335                      broadcastMessage(msgProcessor, serverMsg, playersInGame);
    336 
    337                      serverMsg.type = MSG_TYPE_SCORE;
    338                      memcpy(serverMsg.buffer, &scoreBlue, 4);
    339                      memcpy(serverMsg.buffer+4, &scoreRed, 4);
    340                      broadcastMessage(msgProcessor, serverMsg, playersInGame);
    341 
    342                      // check to see if the game should end
    343                      // move to its own method
    344                      if (scoreBlue == 3 || scoreRed == 3) {
    345                         gameFinished = true;
    346 
    347                         unsigned int winningTeam;
    348                         if (scoreBlue == 3)
    349                            winningTeam = 0;
    350                         else if (scoreRed == 3)
    351                            winningTeam = 1;
    352 
    353                         serverMsg.type = MSG_TYPE_FINISH_GAME;
    354 
    355                         // I should create an instance of the GameSummary object here and just serialize it into this message
    356                         memcpy(serverMsg.buffer, &winningTeam, 4);
    357                         memcpy(serverMsg.buffer+4, &scoreBlue, 4);
    358                         memcpy(serverMsg.buffer+8, &scoreRed, 4);
    359                         strcpy(serverMsg.buffer+12, game->getName().c_str());
    360                         broadcastMessage(msgProcessor, serverMsg, playersInGame);
    361                      }
    362 
    363                      // this means a PLAYER message will be sent
    364                      broadcastMove = true;
    365                   }
    366 
    367                   // go through all objects and check if the player is close to one and if its their flag
    368                   vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
    369                   vector<WorldMap::Object>::iterator itObjects;
    370                   POSITION structPos;
    371 
    372                   for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++)
    373                   {
    374                      POSITION pos = itObjects->pos;
    375 
    376                      if (posDistance(p->pos, pos.toFloat()) < 10)
    377                      {
    378                         if (p->team == 0 &&
    379                             itObjects->type == WorldMap::OBJECT_BLUE_FLAG)
    380                         {
    381                            structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
    382                            flagReturned = true;
    383                            break;
    384                         }
    385                         else if (p->team == 1 &&
    386                                  itObjects->type == WorldMap::OBJECT_RED_FLAG)
    387                         {
    388                            structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
    389                            flagReturned = true;
    390                            break;
    391                         }
    392                      }
    393                   }
    394 
    395                   if (flagReturned)
    396                   {
    397                      itObjects->pos.x = structPos.x*25+12;
    398                      itObjects->pos.y = structPos.y*25+12;
    399 
    400                      serverMsg.type = MSG_TYPE_OBJECT;
    401                      itObjects->serialize(serverMsg.buffer);
    402                      broadcastMessage(msgProcessor, serverMsg, playersInGame);
    403                   }
    404 
    405                   if (broadcastMove)
    406                   {
    407                      serverMsg.type = MSG_TYPE_PLAYER;
    408                      p->serialize(serverMsg.buffer);
    409                      broadcastMessage(msgProcessor, serverMsg, playersInGame);
    410                   }
    411                }
    412 
    413                cout << "processing player attack" << endl;
    414 
    415                // check if the player's attack animation is complete
    416                if (p->isAttacking && p->timeAttackStarted+p->attackCooldown <= getCurrentMillis())
    417                {
    418                   p->isAttacking = false;
    419                   cout << "Attack animation is complete" << endl;
    420 
    421                   //send everyone an ATTACK message
    422                   cout << "about to broadcast attack" << endl;
    423 
    424                   serverMsg.type = MSG_TYPE_ATTACK;
    425                   memcpy(serverMsg.buffer, &p->id, 4);
    426                   memcpy(serverMsg.buffer+4, &p->targetPlayer, 4);
    427                   broadcastMessage(msgProcessor, serverMsg, playersInGame);
    428 
    429                   if (p->attackType == Player::ATTACK_MELEE)
    430                   {
    431                      cout << "Melee attack" << endl;
    432 
    433                      Player* target = playersInGame[p->targetPlayer];
    434                      damagePlayer(target, p->damage);
    435 
    436                      if (target->isDead)
    437                      {
    438                         WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
    439                         if (target->hasBlueFlag)
    440                            flagType = WorldMap::OBJECT_BLUE_FLAG;
    441                         else if (target->hasRedFlag)
    442                            flagType = WorldMap::OBJECT_RED_FLAG;
    443 
    444                         if (flagType != WorldMap::OBJECT_NONE) {
    445                            addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, playersInGame, msgProcessor);
    446                         }
    447                      }
    448 
    449                      serverMsg.type = MSG_TYPE_PLAYER;
    450                      target->serialize(serverMsg.buffer);
    451                   }
    452                   else if (p->attackType == Player::ATTACK_RANGED)
    453                   {
    454                      cout << "Ranged attack" << endl;
    455 
    456                      Projectile proj(p->pos.x, p->pos.y, p->targetPlayer, p->damage);
    457                      game->assignProjectileId(&proj);
    458                      game->addProjectile(proj);
    459 
    460                      int x = p->pos.x;
    461                      int y = p->pos.y;
    462 
    463                      serverMsg.type = MSG_TYPE_PROJECTILE;
    464                      memcpy(serverMsg.buffer, &proj.id, 4);
    465                      memcpy(serverMsg.buffer+4, &x, 4);
    466                      memcpy(serverMsg.buffer+8, &y, 4);
    467                      memcpy(serverMsg.buffer+12, &p->targetPlayer, 4);
    468                   }
    469                   else
    470                      cout << "Invalid attack type: " << p->attackType << endl;
    471 
    472                   broadcastMessage(msgProcessor, serverMsg, playersInGame);
    473                }
    474             }
    475 
    476             if (gameFinished) {
    477                 // send a GAME_INFO message with 0 players to force clients to delete the game
    478                int numPlayers = 0;
    479                serverMsg.type = MSG_TYPE_GAME_INFO;
    480                memcpy(serverMsg.buffer, &numPlayers, 4);
    481                broadcastMessage(msgProcessor, serverMsg, mapPlayers);
    482 
    483                // erase game from server
     217            if (handleGameEvents(itGames->second, mapPlayers, msgProcessor)) {
    484218               mapGames.erase(itGames++);
    485                delete game;
     219               delete itGames->second;
    486220            }else
    487221               itGames++;
     
    534268      if (msgProcessor.receiveMessage(&clientMsg, &from) >= 0)
    535269      {
    536          processMessage(clientMsg, from, msgProcessor, mapPlayers, mapGames, gameMap, unusedPlayerId, serverMsg, scoreBlue, scoreRed);
     270         processMessage(clientMsg, from, msgProcessor, mapPlayers, mapGames, gameMap, unusedPlayerId);
    537271
    538272         cout << "Finished processing the message" << endl;
     
    559293}
    560294
    561 void processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int &scoreBlue, int &scoreRed)
     295void processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, WorldMap* gameMap, unsigned int& unusedPlayerId)
    562296{
     297   NETWORK_MSG serverMsg;
    563298   DataAccess da;
    564299
     
    683418               msgProcessor.sendMessage(&serverMsg, &from);
    684419            }
    685 
    686             // send the current score
    687             serverMsg.type = MSG_TYPE_SCORE;
    688             memcpy(serverMsg.buffer, &scoreBlue, 4);
    689             memcpy(serverMsg.buffer+4, &scoreRed, 4);
    690             msgProcessor.sendMessage(&serverMsg, &from);
    691420
    692421            serverMsg.type = MSG_TYPE_PLAYER;
     
    1045774         serverMsg.type = MSG_TYPE_SCORE;
    1046775
    1047          int game_blueScore = g->getBlueScore();
    1048          int game_redScore = g->getRedScore();
    1049          memcpy(serverMsg.buffer, &game_blueScore, 4);
    1050          memcpy(serverMsg.buffer+4, &game_redScore, 4);
     776         int blueScore = g->getBlueScore();
     777         int redScore = g->getRedScore();
     778         memcpy(serverMsg.buffer, &blueScore, 4);
     779         memcpy(serverMsg.buffer+4, &redScore, 4);
    1051780
    1052781         msgProcessor.sendMessage(&serverMsg, &from);
     
    1096825         break;
    1097826      }
     827   }
     828}
     829
     830bool handleGameEvents(Game* game, map<unsigned int, Player*>& mapPlayers, MessageProcessor& msgProcessor) {
     831   NETWORK_MSG serverMsg;
     832   map<unsigned int, Player*>::iterator it;
     833
     834   for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
     835   {
     836      if (handlePlayerEvents(it->second, game, msgProcessor)) {
     837                // send a GAME_INFO message with 0 players to force clients to delete the game
     838         int numPlayers = 0;
     839         serverMsg.type = MSG_TYPE_GAME_INFO;
     840         memcpy(serverMsg.buffer, &numPlayers, 4);
     841         broadcastMessage(msgProcessor, serverMsg, mapPlayers);
     842
     843         return true;
     844      }else
     845         return false;
     846   }
     847}
     848
     849bool handlePlayerEvents(Player* p, Game* game, MessageProcessor& msgProcessor) {
     850   NETWORK_MSG serverMsg;
     851   WorldMap* gameMap = game->getMap();
     852   map<unsigned int, Player*>& playersInGame = game->getPlayers();
     853   bool gameFinished = false;
     854   FLOAT_POSITION oldPos;
     855
     856   cout << "moving player" << endl;
     857   bool broadcastMove = false;
     858
     859   // move player and perform associated tasks
     860   oldPos = p->pos;
     861   if (p->move(gameMap)) {
     862
     863      cout << "player moved" << endl;
     864      if (game->processPlayerMovement(p, oldPos))
     865         broadcastMove = true;
     866      cout << "player move processed" << endl;
     867
     868      WorldMap::ObjectType flagType;
     869      POSITION pos;
     870      bool flagTurnedIn = false;
     871      bool flagReturned = false;
     872      bool ownFlagAtBase = false;
     873
     874      // need to figure out how to move this to a different file
     875      // while still sending back flag type and position
     876      switch(gameMap->getStructure(p->pos.x/25, p->pos.y/25))
     877      {
     878         case WorldMap::STRUCTURE_BLUE_FLAG:
     879         {
     880            if (p->team == 0 && p->hasRedFlag)
     881            {
     882               // check that your flag is at your base
     883               pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
     884                           
     885               vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
     886               vector<WorldMap::Object>::iterator itObjects;
     887
     888               for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++)
     889               {
     890                  if (itObjects->type == WorldMap::OBJECT_BLUE_FLAG)
     891                  {
     892                     if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12)
     893                     {
     894                        ownFlagAtBase = true;
     895                        break;
     896                     }
     897                  }
     898               }
     899
     900               if (ownFlagAtBase)
     901               {
     902                  p->hasRedFlag = false;
     903                  flagType = WorldMap::OBJECT_RED_FLAG;
     904                  pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
     905                  flagTurnedIn = true;
     906                  game->setBlueScore(game->getBlueScore()+1);
     907               }
     908            }
     909
     910            break;
     911         }
     912         case WorldMap::STRUCTURE_RED_FLAG:
     913         {
     914            if (p->team == 1 && p->hasBlueFlag)
     915            {
     916               // check that your flag is at your base
     917               pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
     918                       
     919               vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
     920               vector<WorldMap::Object>::iterator itObjects;
     921
     922               for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++)
     923               {
     924                  if (itObjects->type == WorldMap::OBJECT_RED_FLAG)
     925                  {
     926                     if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12)
     927                     {
     928                        ownFlagAtBase = true;
     929                        break;
     930                     }
     931                  }
     932               }
     933
     934               if (ownFlagAtBase)
     935               {
     936                  p->hasBlueFlag = false;
     937                  flagType = WorldMap::OBJECT_BLUE_FLAG;
     938                  pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
     939                  flagTurnedIn = true;
     940                  game->setRedScore(game->getRedScore()+1);
     941               }
     942            }
     943
     944            break;
     945         }
     946      }
     947
     948      if (flagTurnedIn)
     949      {
     950         unsigned int blueScore = game->getBlueScore();
     951         unsigned int redScore = game->getRedScore();
     952
     953         // send an OBJECT message to add the flag back to its spawn point
     954         pos.x = pos.x*25+12;
     955         pos.y = pos.y*25+12;
     956         gameMap->addObject(flagType, pos.x, pos.y);
     957
     958         serverMsg.type = MSG_TYPE_OBJECT;
     959         gameMap->getObjects()->back().serialize(serverMsg.buffer);
     960         broadcastMessage(msgProcessor, serverMsg, playersInGame);
     961
     962         serverMsg.type = MSG_TYPE_SCORE;
     963         memcpy(serverMsg.buffer, &blueScore, 4);
     964         memcpy(serverMsg.buffer+4, &redScore, 4);
     965         broadcastMessage(msgProcessor, serverMsg, playersInGame);
     966
     967         // check to see if the game should end
     968         // move to its own method
     969         if (game->getBlueScore() == 3 || game->getRedScore() == 3) {
     970            gameFinished = true;
     971
     972            unsigned int winningTeam;
     973            if (game->getBlueScore() == 3)
     974               winningTeam = 0;
     975            else if (game->getRedScore() == 3)
     976               winningTeam = 1;
     977
     978            serverMsg.type = MSG_TYPE_FINISH_GAME;
     979
     980            // I should create an instance of the GameSummary object here and just serialize it into this message
     981            memcpy(serverMsg.buffer, &winningTeam, 4);
     982            memcpy(serverMsg.buffer+4, &blueScore, 4);
     983            memcpy(serverMsg.buffer+8, &redScore, 4);
     984            strcpy(serverMsg.buffer+12, game->getName().c_str());
     985            broadcastMessage(msgProcessor, serverMsg, playersInGame);
     986         }
     987
     988         // this means a PLAYER message will be sent
     989         broadcastMove = true;
     990      }
     991
     992      // go through all objects and check if the player is close to one and if its their flag
     993      vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
     994      vector<WorldMap::Object>::iterator itObjects;
     995      POSITION structPos;
     996
     997      for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++)
     998      {
     999         POSITION pos = itObjects->pos;
     1000
     1001         if (posDistance(p->pos, pos.toFloat()) < 10)
     1002         {
     1003            if (p->team == 0 &&
     1004                itObjects->type == WorldMap::OBJECT_BLUE_FLAG)
     1005            {
     1006               structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
     1007               flagReturned = true;
     1008               break;
     1009            }
     1010            else if (p->team == 1 &&
     1011                     itObjects->type == WorldMap::OBJECT_RED_FLAG)
     1012            {
     1013               structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
     1014               flagReturned = true;
     1015               break;
     1016            }
     1017         }
     1018      }
     1019
     1020      if (flagReturned)
     1021      {
     1022         itObjects->pos.x = structPos.x*25+12;
     1023         itObjects->pos.y = structPos.y*25+12;
     1024
     1025         serverMsg.type = MSG_TYPE_OBJECT;
     1026         itObjects->serialize(serverMsg.buffer);
     1027         broadcastMessage(msgProcessor, serverMsg, playersInGame);
     1028      }
     1029
     1030      if (broadcastMove)
     1031      {
     1032         serverMsg.type = MSG_TYPE_PLAYER;
     1033         p->serialize(serverMsg.buffer);
     1034         broadcastMessage(msgProcessor, serverMsg, playersInGame);
     1035      }
     1036   }
     1037
     1038   cout << "processing player attack" << endl;
     1039
     1040   // check if the player's attack animation is complete
     1041   if (p->isAttacking && p->timeAttackStarted+p->attackCooldown <= getCurrentMillis())
     1042   {
     1043      p->isAttacking = false;
     1044      cout << "Attack animation is complete" << endl;
     1045
     1046      //send everyone an ATTACK message
     1047      cout << "about to broadcast attack" << endl;
     1048
     1049      serverMsg.type = MSG_TYPE_ATTACK;
     1050      memcpy(serverMsg.buffer, &p->id, 4);
     1051      memcpy(serverMsg.buffer+4, &p->targetPlayer, 4);
     1052      broadcastMessage(msgProcessor, serverMsg, playersInGame);
     1053
     1054      if (p->attackType == Player::ATTACK_MELEE)
     1055      {
     1056         cout << "Melee attack" << endl;
     1057
     1058         Player* target = playersInGame[p->targetPlayer];
     1059         damagePlayer(target, p->damage);
     1060
     1061         if (target->isDead)
     1062         {
     1063            WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
     1064            if (target->hasBlueFlag)
     1065               flagType = WorldMap::OBJECT_BLUE_FLAG;
     1066            else if (target->hasRedFlag)
     1067               flagType = WorldMap::OBJECT_RED_FLAG;
     1068
     1069            if (flagType != WorldMap::OBJECT_NONE) {
     1070               addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, playersInGame, msgProcessor);
     1071            }
     1072         }
     1073
     1074         serverMsg.type = MSG_TYPE_PLAYER;
     1075         target->serialize(serverMsg.buffer);
     1076      }
     1077      else if (p->attackType == Player::ATTACK_RANGED)
     1078      {
     1079         cout << "Ranged attack" << endl;
     1080
     1081         Projectile proj(p->pos.x, p->pos.y, p->targetPlayer, p->damage);
     1082         game->assignProjectileId(&proj);
     1083         game->addProjectile(proj);
     1084
     1085         int x = p->pos.x;
     1086         int y = p->pos.y;
     1087
     1088         serverMsg.type = MSG_TYPE_PROJECTILE;
     1089         memcpy(serverMsg.buffer, &proj.id, 4);
     1090         memcpy(serverMsg.buffer+4, &x, 4);
     1091         memcpy(serverMsg.buffer+8, &y, 4);
     1092         memcpy(serverMsg.buffer+12, &p->targetPlayer, 4);
     1093      }
     1094      else
     1095         cout << "Invalid attack type: " << p->attackType << endl;
     1096
     1097      broadcastMessage(msgProcessor, serverMsg, playersInGame);
    10981098   }
    10991099}
     
    11601160   broadcastMessage(msgProcessor, serverMsg, mapPlayers);
    11611161}
     1162
     1163void quit(int sig) {
     1164   done = true;
     1165}
Note: See TracChangeset for help on using the changeset viewer.