Advertisement
Rochet2

Player phasing with bitset

Jun 6th, 2013
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. std::bitset<31> phases;
  2.  
  3. struct MatchStruct {
  4.     MatchStruct(Player* _player1, Player* _player2) : phaseID(0) // cant phase, use 1 (2^0 = 1)
  5.     {
  6.         player1 = _player1->GetGUID();
  7.         player2 = _player2->GetGUID();
  8.     }
  9.  
  10.     uint64 player1;
  11.     uint64 player2;
  12.     uint8 phaseID;
  13. };
  14. typedef std::list<MatchStruct> MatchList;
  15. MatchList MatchData;
  16.  
  17. class DuelPhasing : public PlayerScript
  18. {
  19. public:
  20.     DuelPhasing() : PlayerScript("DuelPhasing") { }
  21.  
  22.     void OnDuelStart(Player* player1, Player* player2)
  23.     {
  24.         MatchStruct info(player1, player2);
  25.  
  26.         if(phases.all()) // if all phases full, check if players offline:
  27.             ErasePlayers();
  28.        
  29.         if(phases.all()) // full
  30.         {
  31.             ChatHandler(player1->GetSession()).SendSysMessage("You cannot be phased at the moment");// this won't happen often
  32.             ChatHandler(player2->GetSession()).SendSysMessage("You cannot be phased at the moment");// this won't happen often
  33.         }
  34.         else // has phases left
  35.         {
  36.             for (int i = 0; i < phases.size(); ++i)
  37.             {
  38.                 if (!phases.test(i))
  39.                 {
  40.                     phases.set(i, true); // phase in use
  41.                     info.phaseID = i;
  42.                     break;
  43.                 }
  44.             }
  45.             ChatHandler(player1->GetSession()).SendSysMessage("You have been phased.");
  46.             ChatHandler(player2->GetSession()).SendSysMessage("You have been phased.");
  47.         }
  48.  
  49.         MatchData.push_back(info);
  50.         player1->SetPhaseMask(pow(2.0f, info.phaseID), true);
  51.         player2->SetPhaseMask(pow(2.0f, info.phaseID), true);
  52.         // teleport etc
  53.     };
  54.  
  55.     void ErasePlayers()
  56.     {
  57.         for(MatchList::iterator it = MatchData.begin(); it != MatchData.end();) // loop records
  58.         {
  59.             Player* player1 = sObjectAccessor->FindPlayer(it->player1);
  60.             Player* player2 = sObjectAccessor->FindPlayer(it->player2);
  61.             if(!player1 || !player2) // neither or both offline
  62.             {
  63.                 // Teleport players out or something
  64.  
  65.                 phases.set(it->phaseID, false); // set phase unused
  66.                 MatchData.erase(it++);
  67.                 continue;
  68.             }
  69.             ++it;
  70.         }
  71.     }
  72. };
  73.  
  74. void AddSC_DuelPhasing()
  75. {
  76.     new DuelPhasing();
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement