Advertisement
Guest User

Untitled

a guest
Jun 6th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #define MAX_PHASES  31
  2.  
  3. std::bitset<MAX_PHASES> phases;
  4.  
  5. struct MatchStruct {
  6.     MatchStruct(Player* _player1, Player* _player2) : phaseID(0) // cant phase, use 1 (2^0 = 1)
  7.     {
  8.         player1 = _player1->GetGUID();
  9.         player2 = _player2->GetGUID();
  10.     }
  11.  
  12.     uint64 player1;
  13.     uint64 player2;
  14.     uint8 phaseID;
  15. };
  16. typedef std::list<MatchStruct> MatchList;
  17. MatchList MatchData;
  18.  
  19. class DuelPhasing : public PlayerScript
  20. {
  21. public:
  22.     DuelPhasing() : PlayerScript("DuelPhasing") { }
  23.  
  24.     void OnDuelStart(Player* player1, Player* player2)
  25.     {
  26.         MatchStruct info(player1, player2);
  27.  
  28.         if(phases.all()) // if all phases full, check if players offline:
  29.             ErasePlayers();
  30.  
  31.         if(phases.all()) // full
  32.         {
  33.             ChatHandler(player1->GetSession()).SendSysMessage("You cannot be phased at the moment");// this won't happen often
  34.             ChatHandler(player2->GetSession()).SendSysMessage("You cannot be phased at the moment");// this won't happen often
  35.         }
  36.         else // has phases left
  37.         {
  38.             for (int i = 0; i < MAX_PHASES; ++i)
  39.             {
  40.                 if (!phases.test(i))
  41.                 {
  42.                     phases.set(i, true); // phase in use
  43.                     info.phaseID = i;
  44.                     break;
  45.                 }
  46.             }
  47.             ChatHandler(player1->GetSession()).SendSysMessage("You have been phased.");
  48.             ChatHandler(player2->GetSession()).SendSysMessage("You have been phased.");
  49.         }
  50.  
  51.         MatchData.push_back(info);
  52.         player1->SetPhaseMask(pow(2.0f, info.phaseID), true);
  53.         player2->SetPhaseMask(pow(2.0f, info.phaseID), true);
  54.         // teleport etc
  55.     };
  56.  
  57.     void ErasePlayers()
  58.     {
  59.         for(MatchList::iterator it = MatchData.begin(); it != MatchData.end();) // loop records
  60.         {
  61.             Player* player1 = sObjectAccessor->FindPlayer(it->player1);
  62.             Player* player2 = sObjectAccessor->FindPlayer(it->player2);
  63.             if(!player1 || !player2) // neither or both offline
  64.             {
  65.                 // Teleport players out or something
  66.  
  67.                 phases.set(it->phaseID, false); // set phase unused
  68.                 MatchData.erase(it++);
  69.                 continue;
  70.             }
  71.             ++it;
  72.         }
  73.     }
  74.  
  75.     void OnDuelEnd(Player* pWinner, Player* pLoser, DuelCompleteType /*type*/)
  76.     {
  77.         for(MatchList::iterator it = MatchData.begin(); it != MatchData.end();) // loop records
  78.         {
  79.  
  80.             double phase = sqrt(double(pWinner->GetPhaseMask()));
  81.             if(phase - uint8(phase)) // was not unique
  82.                 return;
  83.             uint8 phaseID = uint8(phase);
  84.             if(phaseID >= phases.size())
  85.                 return; // wrong ID (too high, will crash)
  86.             phases.set(phaseID, false);
  87.         }
  88.         pWinner->RemoveAllSpellCooldown();
  89.         pLoser->RemoveAllSpellCooldown();
  90.         pWinner->SetHealth(pWinner->GetMaxHealth());
  91.         if ( pWinner->getPowerType() == POWER_MANA )
  92.             pWinner->SetPower(POWER_MANA, pWinner->GetMaxPower(POWER_MANA));
  93.         pLoser->SetHealth(pLoser->GetMaxHealth());
  94.         if ( pLoser->getPowerType() == POWER_MANA )
  95.             pLoser->SetPower(POWER_MANA,  pLoser->GetMaxPower(POWER_MANA));
  96.     }
  97. };
  98.  
  99. void AddSC_DuelPhasing()
  100. {
  101.     new DuelPhasing();
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement