Files
TCPlayerbotCore/src/server/game/Instances/InstanceData.cpp
T

381 lines
13 KiB
C++
Raw Normal View History

2008-10-02 16:23:55 -05:00
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
2008-10-14 11:57:03 -05:00
*
2010-01-16 20:19:18 +03:00
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
2008-10-02 16:23:55 -05:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) 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
2008-10-14 11:57:03 -05:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2008-10-02 16:23:55 -05:00
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
2008-10-14 11:57:03 -05:00
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2008-10-02 16:23:55 -05:00
*/
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#include "InstanceData.h"
#include "DatabaseEnv.h"
2008-10-02 16:23:55 -05:00
#include "Map.h"
2009-08-14 20:56:01 +02:00
#include "Player.h"
2009-05-06 15:36:55 +02:00
#include "GameObject.h"
#include "Creature.h"
2009-05-16 14:49:30 -05:00
#include "CreatureAI.h"
#include "Log.h"
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void InstanceData::SaveToDB()
{
2009-05-11 03:17:13 -05:00
std::string data = GetSaveData();
if (data.empty())
2009-05-11 03:17:13 -05:00
return;
2008-10-02 16:23:55 -05:00
CharacterDatabase.escape_string(data);
CharacterDatabase.PExecute("UPDATE instance SET data = '%s' WHERE id = '%d'", data.c_str(), instance->GetInstanceId());
}
2009-10-17 15:51:44 -07:00
2009-04-06 21:14:51 +02:00
void InstanceData::HandleGameObject(uint64 GUID, bool open, GameObject *go)
{
if (!go)
go = instance->GetGameObject(GUID);
if (go)
go->SetGoState(open ? GO_STATE_ACTIVE : GO_STATE_READY);
else
sLog.outDebug("TSCR: InstanceData: HandleGameObject failed");
}
2009-10-17 15:51:44 -07:00
bool InstanceData::IsEncounterInProgress() const
{
2009-10-17 16:20:24 -07:00
for (std::vector<BossInfo>::const_iterator itr = bosses.begin(); itr != bosses.end(); ++itr)
if (itr->state == IN_PROGRESS)
return true;
2009-10-17 15:51:44 -07:00
return false;
}
2009-10-17 15:51:44 -07:00
2009-05-16 14:49:30 -05:00
void InstanceData::LoadMinionData(const MinionData *data)
{
2010-04-07 19:13:19 +02:00
while (data->entry)
2009-05-16 14:49:30 -05:00
{
if (data->bossId < bosses.size())
2009-05-16 14:49:30 -05:00
minions.insert(std::make_pair(data->entry, MinionInfo(&bosses[data->bossId])));
2009-10-17 15:51:44 -07:00
2009-05-16 14:49:30 -05:00
++data;
}
sLog.outDebug("InstanceData::LoadMinionData: %u minions loaded.", doors.size());
}
2009-10-17 15:51:44 -07:00
2009-05-09 14:08:42 -05:00
void InstanceData::LoadDoorData(const DoorData *data)
{
2010-04-07 19:13:19 +02:00
while (data->entry)
{
if (data->bossId < bosses.size())
doors.insert(std::make_pair(data->entry, DoorInfo(&bosses[data->bossId], data->type, BoundaryType(data->boundary))));
2009-10-17 15:51:44 -07:00
2009-05-09 14:08:42 -05:00
++data;
}
sLog.outDebug("InstanceData::LoadDoorData: %u doors loaded.", doors.size());
}
2009-10-17 15:51:44 -07:00
2009-05-16 14:49:30 -05:00
void InstanceData::UpdateMinionState(Creature *minion, EncounterState state)
{
switch (state)
2009-05-16 14:49:30 -05:00
{
case NOT_STARTED:
if (!minion->isAlive())
2009-05-16 14:49:30 -05:00
minion->Respawn();
else if (minion->isInCombat())
2009-05-16 14:49:30 -05:00
minion->AI()->EnterEvadeMode();
break;
case IN_PROGRESS:
if (!minion->isAlive())
2009-05-16 14:49:30 -05:00
minion->Respawn();
else if (!minion->getVictim())
2009-05-16 14:49:30 -05:00
minion->AI()->DoZoneInCombat();
break;
}
}
2009-10-17 15:51:44 -07:00
2009-05-09 14:08:42 -05:00
void InstanceData::UpdateDoorState(GameObject *door)
{
DoorInfoMap::iterator lower = doors.lower_bound(door->GetEntry());
DoorInfoMap::iterator upper = doors.upper_bound(door->GetEntry());
if (lower == upper)
2009-05-09 14:08:42 -05:00
return;
2009-10-17 15:51:44 -07:00
2009-05-09 14:08:42 -05:00
bool open = true;
2009-10-17 16:20:24 -07:00
for (DoorInfoMap::iterator itr = lower; itr != upper; ++itr)
2009-05-09 14:08:42 -05:00
{
if (itr->second.type == DOOR_TYPE_ROOM)
2009-05-06 00:06:38 -05:00
{
if (itr->second.bossInfo->state == IN_PROGRESS)
2009-05-09 14:08:42 -05:00
{
open = false;
break;
}
}
else if (itr->second.type == DOOR_TYPE_PASSAGE)
2009-05-09 14:08:42 -05:00
{
if (itr->second.bossInfo->state != DONE)
2009-05-09 14:08:42 -05:00
{
open = false;
break;
}
2009-05-06 00:06:38 -05:00
}
}
2009-10-17 15:51:44 -07:00
2009-05-09 14:08:42 -05:00
door->SetGoState(open ? GO_STATE_ACTIVE : GO_STATE_READY);
//sLog.outError("Door %u is %s.", door->GetEntry(), open ? "opened" : "closed");
}
2009-10-17 15:51:44 -07:00
2009-05-09 14:08:42 -05:00
void InstanceData::AddDoor(GameObject *door, bool add)
{
2009-05-09 14:08:42 -05:00
DoorInfoMap::iterator lower = doors.lower_bound(door->GetEntry());
DoorInfoMap::iterator upper = doors.upper_bound(door->GetEntry());
if (lower == upper)
2009-05-09 14:08:42 -05:00
return;
2009-10-17 15:51:44 -07:00
2009-10-17 16:20:24 -07:00
for (DoorInfoMap::iterator itr = lower; itr != upper; ++itr)
{
if (add)
{
2009-05-09 14:08:42 -05:00
itr->second.bossInfo->door[itr->second.type].insert(door);
switch (itr->second.boundary)
{
default:
case BOUNDARY_NONE:
break;
case BOUNDARY_N:
case BOUNDARY_S:
itr->second.bossInfo->boundary[itr->second.boundary] = door->GetPositionX();
break;
case BOUNDARY_E:
case BOUNDARY_W:
itr->second.bossInfo->boundary[itr->second.boundary] = door->GetPositionY();
break;
case BOUNDARY_NW:
case BOUNDARY_SE:
itr->second.bossInfo->boundary[itr->second.boundary] = door->GetPositionX() + door->GetPositionY();
break;
case BOUNDARY_NE:
case BOUNDARY_SW:
itr->second.bossInfo->boundary[itr->second.boundary] = door->GetPositionX() - door->GetPositionY();
break;
}
}
2009-05-06 00:06:38 -05:00
else
2009-05-09 14:08:42 -05:00
itr->second.bossInfo->door[itr->second.type].erase(door);
}
2009-10-17 15:51:44 -07:00
if (add)
2009-05-09 14:08:42 -05:00
UpdateDoorState(door);
}
2009-10-17 15:51:44 -07:00
2009-05-16 14:49:30 -05:00
void InstanceData::AddMinion(Creature *minion, bool add)
{
MinionInfoMap::iterator itr = minions.find(minion->GetEntry());
if (itr == minions.end())
2009-05-16 14:49:30 -05:00
return;
2009-10-17 15:51:44 -07:00
if (add)
2009-05-16 14:49:30 -05:00
itr->second.bossInfo->minion.insert(minion);
else
itr->second.bossInfo->minion.erase(minion);
}
2009-10-17 15:51:44 -07:00
bool InstanceData::SetBossState(uint32 id, EncounterState state)
{
if (id < bosses.size())
{
BossInfo *bossInfo = &bosses[id];
if (bossInfo->state == TO_BE_DECIDED) // loading
{
2009-05-11 03:17:13 -05:00
bossInfo->state = state;
//sLog.outError("Inialize boss %u state as %u.", id, (uint32)state);
return false;
}
2009-05-11 03:17:13 -05:00
else
{
if (bossInfo->state == state)
return false;
2009-10-17 15:51:44 -07:00
if (state == DONE)
2009-10-17 16:20:24 -07:00
for (MinionSet::iterator i = bossInfo->minion.begin(); i != bossInfo->minion.end(); ++i)
2010-04-07 19:14:10 +02:00
if ((*i)->isWorldBoss() && (*i)->isAlive())
return false;
2009-10-17 15:51:44 -07:00
2009-05-11 03:17:13 -05:00
bossInfo->state = state;
SaveToDB();
}
2009-10-17 16:20:24 -07:00
for (uint32 type = 0; type < MAX_DOOR_TYPES; ++type)
for (DoorSet::iterator i = bossInfo->door[type].begin(); i != bossInfo->door[type].end(); ++i)
2009-05-09 14:08:42 -05:00
UpdateDoorState(*i);
2009-10-17 15:51:44 -07:00
2009-10-17 16:20:24 -07:00
for (MinionSet::iterator i = bossInfo->minion.begin(); i != bossInfo->minion.end(); ++i)
2009-05-16 14:49:30 -05:00
UpdateMinionState(*i, state);
2009-10-17 15:51:44 -07:00
return true;
}
return false;
}
2009-10-17 15:51:44 -07:00
2009-05-11 03:17:13 -05:00
std::string InstanceData::LoadBossState(const char * data)
{
2010-04-07 19:14:10 +02:00
if (!data)
return NULL;
2009-05-11 03:17:13 -05:00
std::istringstream loadStream(data);
uint32 buff;
uint32 bossId = 0;
2009-10-17 16:20:24 -07:00
for (std::vector<BossInfo>::iterator i = bosses.begin(); i != bosses.end(); ++i, ++bossId)
2009-05-11 03:17:13 -05:00
{
loadStream >> buff;
if (buff < TO_BE_DECIDED)
2009-05-11 03:17:13 -05:00
SetBossState(bossId, (EncounterState)buff);
}
return loadStream.str();
}
2009-10-17 15:51:44 -07:00
2009-05-11 03:17:13 -05:00
std::string InstanceData::GetBossSaveData()
{
std::ostringstream saveStream;
2009-10-17 16:20:24 -07:00
for (std::vector<BossInfo>::iterator i = bosses.begin(); i != bosses.end(); ++i)
2009-05-11 03:17:13 -05:00
saveStream << (uint32)i->state << " ";
return saveStream.str();
2009-10-17 16:20:24 -07:00
}
2009-10-17 15:51:44 -07:00
2009-06-17 07:16:40 +02:00
void InstanceData::DoUseDoorOrButton(uint64 uiGuid, uint32 uiWithRestoreTime, bool bUseAlternativeState)
{
if (!uiGuid)
return;
2009-10-17 15:51:44 -07:00
2009-06-17 07:16:40 +02:00
GameObject* pGo = instance->GetGameObject(uiGuid);
2009-10-17 15:51:44 -07:00
2009-06-17 07:16:40 +02:00
if (pGo)
{
if (pGo->GetGoType() == GAMEOBJECT_TYPE_DOOR || pGo->GetGoType() == GAMEOBJECT_TYPE_BUTTON)
{
if (pGo->getLootState() == GO_READY)
pGo->UseDoorOrButton(uiWithRestoreTime,bUseAlternativeState);
else if (pGo->getLootState() == GO_ACTIVATED)
pGo->ResetDoorOrButton();
}
else
sLog.outError("SD2: Script call DoUseDoorOrButton, but gameobject entry %u is type %u.",pGo->GetEntry(),pGo->GetGoType());
2009-06-17 07:16:40 +02:00
}
}
2009-10-17 15:51:44 -07:00
2009-06-17 07:16:40 +02:00
void InstanceData::DoRespawnGameObject(uint64 uiGuid, uint32 uiTimeToDespawn)
{
if (GameObject* pGo = instance->GetGameObject(uiGuid))
{
//not expect any of these should ever be handled
2010-04-07 23:25:02 +02:00
if (pGo->GetGoType() == GAMEOBJECT_TYPE_FISHINGNODE || pGo->GetGoType() == GAMEOBJECT_TYPE_DOOR ||
pGo->GetGoType() == GAMEOBJECT_TYPE_BUTTON || pGo->GetGoType() == GAMEOBJECT_TYPE_TRAP)
2009-06-17 07:16:40 +02:00
return;
2009-10-17 15:51:44 -07:00
2009-06-17 07:16:40 +02:00
if (pGo->isSpawned())
return;
2009-10-17 15:51:44 -07:00
2009-06-17 07:16:40 +02:00
pGo->SetRespawnTime(uiTimeToDespawn);
}
}
2009-10-17 15:51:44 -07:00
2009-08-14 20:56:01 +02:00
void InstanceData::DoUpdateWorldState(uint32 uiStateId, uint32 uiStateData)
{
Map::PlayerList const& lPlayers = instance->GetPlayers();
2009-10-17 15:51:44 -07:00
2009-08-14 20:56:01 +02:00
if (!lPlayers.isEmpty())
{
2009-10-17 16:20:24 -07:00
for (Map::PlayerList::const_iterator itr = lPlayers.begin(); itr != lPlayers.end(); ++itr)
if (Player *pPlayer = itr->getSource())
2009-08-14 20:56:01 +02:00
pPlayer->SendUpdateWorldState(uiStateId, uiStateData);
}
else
sLog.outDebug("TSCR: DoUpdateWorldState attempt send data but no players in map.");
2009-08-14 20:56:01 +02:00
}
2009-10-17 15:51:44 -07:00
// Send Notify to all players in instance
void InstanceData::DoSendNotifyToInstance(const char *format, ...)
{
InstanceMap::PlayerList const &PlayerList = instance->GetPlayers();
InstanceMap::PlayerList::const_iterator i;
2009-10-17 15:51:44 -07:00
if (!PlayerList.isEmpty())
2009-10-18 15:09:43 -07:00
for (Map::PlayerList::const_iterator i = PlayerList.begin(); i != PlayerList.end(); ++i)
if (Player *pPlayer = i->getSource())
if (WorldSession *pSession = pPlayer->GetSession())
pSession->SendNotification(format);
}
// Complete Achievement for all players in instance
void InstanceData::DoCompleteAchievement(uint32 achievement)
{
AchievementEntry const* pAE = GetAchievementStore()->LookupEntry(achievement);
Map::PlayerList const &PlayerList = instance->GetPlayers();
2009-10-17 15:51:44 -07:00
if (!pAE)
{
sLog.outError("TSCR: DoCompleteAchievement called for not existing achievement %u", achievement);
return;
}
if (!PlayerList.isEmpty())
for (Map::PlayerList::const_iterator i = PlayerList.begin(); i != PlayerList.end(); ++i)
if (Player *pPlayer = i->getSource())
pPlayer->CompletedAchievement(pAE);
}
2009-10-17 15:51:44 -07:00
// Update Achievement Criteria for all players in instance
void InstanceData::DoUpdateAchievementCriteria(AchievementCriteriaTypes type, uint32 miscvalue1, uint32 miscvalue2, Unit *unit, uint32 time)
{
Map::PlayerList const &PlayerList = instance->GetPlayers();
if (!PlayerList.isEmpty())
for (Map::PlayerList::const_iterator i = PlayerList.begin(); i != PlayerList.end(); ++i)
if (Player *pPlayer = i->getSource())
pPlayer->UpdateAchievementCriteria(type, miscvalue1, miscvalue2, unit, time);
}
// Start timed achievement for all players in instance
void InstanceData::DoStartTimedAchievement(AchievementCriteriaTimedTypes type, uint32 entry)
{
Map::PlayerList const &PlayerList = instance->GetPlayers();
if (!PlayerList.isEmpty())
for (Map::PlayerList::const_iterator i = PlayerList.begin(); i != PlayerList.end(); ++i)
if (Player *pPlayer = i->getSource())
pPlayer->GetAchievementMgr().StartTimedAchievement(type, entry);
}
// Stop timed achievement for all players in instance
void InstanceData::DoStopTimedAchievement(AchievementCriteriaTimedTypes type, uint32 entry)
{
Map::PlayerList const &PlayerList = instance->GetPlayers();
if (!PlayerList.isEmpty())
for (Map::PlayerList::const_iterator i = PlayerList.begin(); i != PlayerList.end(); ++i)
if (Player *pPlayer = i->getSource())
pPlayer->GetAchievementMgr().RemoveTimedAchievement(type, entry);
}
// Remove Auras due to Spell on all players in instance
void InstanceData::DoRemoveAurasDueToSpellOnPlayers(uint32 spell)
{
Map::PlayerList const &PlayerList = instance->GetPlayers();
2009-10-17 15:51:44 -07:00
if (!PlayerList.isEmpty())
for (Map::PlayerList::const_iterator i = PlayerList.begin(); i != PlayerList.end(); ++i)
if (Player* pPlayer = i->getSource())
pPlayer->RemoveAurasDueToSpell(spell);
}
2010-04-07 22:59:46 +02:00
bool InstanceData::CheckAchievementCriteriaMeet(uint32 criteria_id, Player const* /*source*/, Unit const* /*target*/ /*= NULL*/, uint32 /*miscvalue1*/ /*= 0*/)
{
sLog.outError("Achievement system call InstanceData::CheckAchievementCriteriaMeet but instance script for map %u not have implementation for achievement criteria %u",
instance->GetId(),criteria_id);
return false;
}