View difference between Paste ID: 1V3m6dPh and zTHuFPcT
SHOW: | | - or go back to the newest paste.
1-
#define MAX_PHASES  31
1+
const uint8 MAX_PHASES = 31;
2
3-
std::bitset<MAX_PHASES> phases;
3+
4
    MatchStruct(Player* _player1, Player* _player2)
5
    {
6-
	MatchStruct(Player* _player1, Player* _player2) : phaseID(0) // cant phase, use 1 (2^0 = 1)
6+
        player1 = _player1->GetGUID();
7-
	{
7+
        player2 = _player2->GetGUID();
8-
		player1 = _player1->GetGUID();
8+
    }
9-
		player2 = _player2->GetGUID();
9+
10-
	}
10+
    uint64 player1;
11
    uint64 player2;
12-
	uint64 player1;
12+
13-
	uint64 player2;
13+
// typedef std::list<MatchStruct> MatchList;
14-
	uint8 phaseID;
14+
typedef std::map<uint8, MatchStruct> MatchList;
15
MatchList MatchData;
16-
typedef std::list<MatchStruct> MatchList;
16+
17
class DuelPhasing : public PlayerScript
18
{
19
public:
20
    DuelPhasing() : PlayerScript("DuelPhasing") { }
21
22-
	DuelPhasing() : PlayerScript("DuelPhasing") { }
22+
    void OnDuelStart(Player* player1, Player* player2) 
23
    {
24-
	void OnDuelStart(Player* player1, Player* player2) 
24+
        MatchStruct info(player1, player2);
25-
	{
25+
        uint8 phaseID = 0;
26-
		MatchStruct info(player1, player2);
26+
27
        if(MatchData.size() >= MAX_PHASES) // if all phases full, check if players offline:
28-
		if(phases.all()) // if all phases full, check if players offline:
28+
            ErasePlayers();
29-
			ErasePlayers();
29+
30
        if(MatchData.size() >= MAX_PHASES) // full
31-
		if(phases.all()) // full
31+
        {
32-
		{
32+
            ChatHandler(player1->GetSession()).SendSysMessage("You cannot be phased at the moment");// this won't happen often
33-
			ChatHandler(player1->GetSession()).SendSysMessage("You cannot be phased at the moment");// this won't happen often
33+
            ChatHandler(player2->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
34+
        }
35-
		}
35+
        else // has phases left
36-
		else // has phases left
36+
        {
37-
		{
37+
            for (uint8 i = phaseID+1; i < MAX_PHASES; ++i)
38-
			for (int i = 0; i < MAX_PHASES; ++i)
38+
            {
39-
			{
39+
                if (MatchData.find(i) == MatchData.end())
40-
				if (!phases.test(i))
40+
                {
41-
				{
41+
                    phaseID = i;
42-
					phases.set(i, true); // phase in use
42+
                    break;
43-
					info.phaseID = i;
43+
                }
44-
					break;
44+
            }
45-
				}
45+
            ChatHandler(player1->GetSession()).SendSysMessage("You have been phased.");
46-
			}
46+
            ChatHandler(player2->GetSession()).SendSysMessage("You have been phased.");
47-
			ChatHandler(player1->GetSession()).SendSysMessage("You have been phased.");
47+
        }
48-
			ChatHandler(player2->GetSession()).SendSysMessage("You have been phased.");
48+
49-
		}
49+
        // MatchData.push_back(info);
50
        if(phaseID)
51-
		MatchData.push_back(info);
51+
            MatchData[phaseID] = info;
52-
		player1->SetPhaseMask(pow(2.0f, info.phaseID), true);
52+
        player1->SetPhaseMask(pow(2.0f, phaseID), true);
53-
		player2->SetPhaseMask(pow(2.0f, info.phaseID), true);
53+
        player2->SetPhaseMask(pow(2.0f, phaseID), true);
54-
		// teleport etc
54+
        // teleport etc
55-
	};
55+
    };
56
57-
	void ErasePlayers()
57+
    void ErasePlayers()
58-
	{
58+
    {
59-
		for(MatchList::iterator it = MatchData.begin(); it != MatchData.end();) // loop records
59+
        for(MatchList::iterator it = MatchData.begin(); it != MatchData.end(); ++it) // loop records
60-
		{
60+
        {
61-
			Player* player1 = sObjectAccessor->FindPlayer(it->player1);
61+
            Player* player1 = sObjectAccessor->FindPlayer(it->second.player1);
62-
			Player* player2 = sObjectAccessor->FindPlayer(it->player2);
62+
            Player* player2 = sObjectAccessor->FindPlayer(it->second.player2);
63-
			if(!player1 || !player2) // neither or both offline
63+
            uint32 phase = pow(2.0f, it->first);
64-
			{
64+
            if(player1 && player2 && player1->GetPhaseMask() == phase && player2->GetPhaseMask() == phase)
65-
				// Teleport players out or something
65+
                continue;
66
67-
				phases.set(it->phaseID, false); // set phase unused
67+
            // teleport players out or something
68-
				MatchData.erase(it++);
68+
            MatchData.erase(it);
69-
				continue;
69+
        }
70-
			}
70+
    }
71-
			++it;
71+
72-
		}
72+
    void OnDuelEnd(Player* pWinner, Player* pLoser, DuelCompleteType /*type*/)
73-
	}
73+
    {
74
        double phase = sqrt(double(pWinner->GetPhaseMask()));
75-
	void OnDuelEnd(Player* pWinner, Player* pLoser, DuelCompleteType /*type*/)
75+
        if(phase - uint8(phase))
76-
	{
76+
            return; // was not unique
77-
		for(MatchList::iterator it = MatchData.begin(); it != MatchData.end();) // loop records
77+
        uint8 phaseID = uint8(phase);
78-
		{
78+
        if(phaseID >= MAX_PHASES)
79
            return; // wrong ID
80-
			double phase = sqrt(double(pWinner->GetPhaseMask()));
80+
        MatchData.erase(phaseID); // if not cleared right, still cleared if ErasePlayers is called
81-
			if(phase - uint8(phase)) // was not unique
81+
82-
				return;
82+
        pWinner->RemoveAllSpellCooldown();
83-
			uint8 phaseID = uint8(phase);
83+
        pLoser->RemoveAllSpellCooldown();
84-
			if(phaseID >= phases.size())
84+
        pWinner->SetHealth(pWinner->GetMaxHealth());
85-
				return; // wrong ID (too high, will crash)
85+
        if ( pWinner->getPowerType() == POWER_MANA )
86-
			phases.set(phaseID, false);
86+
            pWinner->SetPower(POWER_MANA, pWinner->GetMaxPower(POWER_MANA));
87-
		}
87+
        pLoser->SetHealth(pLoser->GetMaxHealth());
88-
		pWinner->RemoveAllSpellCooldown();
88+
        if ( pLoser->getPowerType() == POWER_MANA )
89-
		pLoser->RemoveAllSpellCooldown();
89+
            pLoser->SetPower(POWER_MANA,  pLoser->GetMaxPower(POWER_MANA));
90-
		pWinner->SetHealth(pWinner->GetMaxHealth());
90+
    }
91-
		if ( pWinner->getPowerType() == POWER_MANA )
91+
92-
			pWinner->SetPower(POWER_MANA, pWinner->GetMaxPower(POWER_MANA));
92+
93-
		pLoser->SetHealth(pLoser->GetMaxHealth());
93+
94-
		if ( pLoser->getPowerType() == POWER_MANA )
94+
95-
			pLoser->SetPower(POWER_MANA,  pLoser->GetMaxPower(POWER_MANA));
95+
    new DuelPhasing();
96-
	}
96+