PvPststem.cpp
Code:
/* PvPSystem
* Copyright (C) 2008 Spectre
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "PvPMgr.h"
#define REWARD_ITEM_ID 95084
#define REWARD_ITEM_AMT 1
PvPStorage *Storage;
void SysTrigger(Player* attacker, Player* victim)
{
PvPMgr *aMgr = Storage->GetMgr(attacker->GetLowGUID(), true);
PvPMgr *vMgr = Storage->GetMgr(victim->GetLowGUID(), true);
if(aMgr == NULL || vMgr == NULL)
return;
if(aMgr->ChecksPassed(attacker, victim) == false)
{
vMgr->CurrentKills = 0;
vMgr->TotalDeaths++;
return;
}
aMgr->CurrentKills++;
aMgr->AddItem(REWARD_ITEM_ID, REWARD_ITEM_AMT, true);
vMgr->CurrentKills = 0;
vMgr->TotalDeaths++;
attacker->BroadcastMessage("%sCurrent Kills: %u", MSG_COLOR_GOLD, aMgr->CurrentKills);
victim->BroadcastMessage("%sStats Reset", MSG_COLOR_GOLD);
}
void SetupPvPSystem(ScriptMgr *mgr)
{
Storage = PvPStorage::GetInstance();
mgr->register_hook(SERVER_HOOK_EVENT_ON_KILL_PLAYER, (void*)SysTrigger);
}
PvpMgr.cpp
Code:
/* PvPSystem
* Copyright (C) 2008 Spectre
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "PvPMgr.h"
PvPMgr::PvPMgr()
{
Index = 0;
CurrentKills = 0;
TotalDeaths = 0;
LastGuid = 0;
VP = 0;
IsInTurny = false;
}
bool PvPMgr::ChecksPassed(Player *Attcker, Player *Vct)
{
string Aip;
string Vip;
if(Attcker->GetSession()->HasGMPermissions() || Vct->GetSession()->HasGMPermissions())
return true; // gm over-ride for testing
if(Attcker == Vct)
return false;
if(Attcker->GetSession()->GetSocket() == NULL || Vct->GetSession()->GetSocket() == NULL)
return false;
Aip = Attcker->GetSession()->GetSocket()->GetRemoteIP();
Vip = Vct->GetSession()->GetSocket()->GetRemoteIP();
if(Aip == "" || Vip == "" || Aip == Vip)
return false;
if(Attcker->GetLowGUID() == LastGuid)
return false;
return true;
}
void PvPMgr::AddItem(const uint32 &ItemID, const uint32 &Amt, const bool &Stack)
{
Item *ItemToAdd;
SlotResult Slot;
Player *Plr = objmgr.GetPlayer(Index);
if(Plr == NULL)
return;
if(Stack)
{
ItemToAdd = Plr->GetItemInterface()->FindItemLessMax(ItemID, Amt, false);
if(ItemToAdd != NULL)
{
ItemToAdd->ModUnsigned32Value(ITEM_FIELD_STACK_COUNT, Amt);
ItemToAdd->m_isDirty = true;
Plr->BroadcastMessage("%s%s x%u Awarded", MSG_COLOR_GOLD, ItemToAdd->GetProto()->Name1, Amt);
return;
}
}
for(uint32 i = 1;i <= Amt;i++)
{
ItemToAdd = objmgr.CreateItem(ItemID, Plr);
if(ItemToAdd == NULL)
return; // failed creation, no memory to allocate, or invalid item id
Slot = Plr->GetItemInterface()->FindFreeInventorySlot(ItemToAdd->GetProto());
if(Slot.Result)
{
Plr->GetItemInterface()->SafeAddItem(ItemID, Slot.ContainerSlot, Slot.Slot);
Plr->BroadcastMessage("%s%s x1 Awarded", MSG_COLOR_GOLD, ItemToAdd->GetProto()->Name1);
}
else
Plr->BroadcastMessage("No free inventory slots could be located, aborting");
}
}
PvPStorage *PvPStorage::Instance = NULL;
PvPStorage::PvPStorage()
{
}
PvPStorage *PvPStorage::GetInstance()
{
if(Instance == NULL)
Instance = new PvPStorage();
return Instance;
}
PvPMgr *PvPStorage::CreateMgr(const uint32 &Index)
{
PvPMgr *Mgr = new PvPMgr();
if(Mgr == NULL)
return NULL; // no memory to allocate
Mgr->Index = Index;
StorageLock.Acquire();
Vect.push_back(Mgr);
StorageLock.Release();
return Mgr;
}
PvPMgr *PvPStorage::GetMgr(const uint32 &Index, const bool Create)
{
StorageLock.Acquire();
for(Itr = Vect.begin();Itr != Vect.end();Itr++)
{
if((*Itr)->Index == Index)
{
StorageLock.Release();
return (*Itr);
}
}
StorageLock.Release();
if(Create)
return CreateMgr(Index);
return NULL;
}
void PvPStorage::CleanVect()
{
vector<PvPMgr*> CleanVect;
uint32 Pos = 0;
if(Vect.empty())
return;
StorageLock.Acquire();
for(Itr = Vect.begin();Itr != Vect.end();Itr++)
{
Player *Eraser = objmgr.GetPlayer((*Itr)->Index);
if(Eraser == NULL || Eraser->IsInWorld() == false || Eraser->GetSession() == NULL)
{
(*Itr)->VP = Pos;
CleanVect.push_back((*Itr));
}
Pos++;
}
for(Itr = CleanVect.begin();Itr != CleanVect.end();Itr++)
{
Vect.erase(Vect.begin()+(*Itr)->VP);
delete (*Itr);
}
StorageLock.Release();
CleanVect.clear();
}
void PvPStorage::StartTimer()
{
CleanTimer = TimedEvent::Allocate(this, new CallbackP0<PvPStorage>(this, &PvPStorage::CleanVect), 1, CLEAN_DELAY, 0);
sWorld.event_AddEvent(CleanTimer);
}
PvpMgr.h
Code:
/* PvPSystem
* Copyright (C) 2008 Spectre
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef PvPMgr_H
#define PvPMgr_h
#include "StdAfx.h"
#include "Setup.h"
#define CLEAN_DELAY 20000
class PvPMgr
{
private:
bool IsInTurny;
public:
uint32 Index; // Player GUID
uint32 VP;
uint32 CurrentKills;
uint32 TotalDeaths;
uint32 LastGuid;
PvPMgr();
bool ChecksPassed(Player *Attcker, Player *Vct);
void AddItem(const uint32 &ItemID, const uint32 &Amt, const bool &Stack = false);
};
class PvPStorage
{
private:
static PvPStorage *Instance;
vector<PvPMgr*> Vect;
vector<PvPMgr*>::iterator Itr;
Mutex StorageLock;
TimedEvent *CleanTimer;
PvPStorage();
void CleanVect();
public:
static PvPStorage *GetInstance();
PvPMgr *CreateMgr(const uint32 &Index);
PvPMgr *GetMgr(const uint32 &Index, const bool Create);
void StartTimer();
vector<PvPMgr*> GetVect()
{
return Vect;
}
};
#endif
All credit go's to Spectre
Thanks to Aspire Dev