/*
* Copyright (C) 2008-2015 TrinityCore
*
* 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 MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the 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, see .
*/
#include "Garrison.h"
#include "GameObject.h"
#include "GarrisonMgr.h"
#include "MapManager.h"
#include "ObjectMgr.h"
Garrison::Garrison(Player* owner) : _owner(owner), _siteLevel(nullptr), _followerActivationsRemainingToday(1)
{
}
bool Garrison::LoadFromDB(PreparedQueryResult garrison, PreparedQueryResult blueprints, PreparedQueryResult buildings,
PreparedQueryResult followers, PreparedQueryResult abilities)
{
if (!garrison)
return false;
Field* fields = garrison->Fetch();
_siteLevel = sGarrSiteLevelStore.LookupEntry(fields[0].GetUInt32());
_followerActivationsRemainingToday = fields[1].GetUInt32();
if (!_siteLevel)
return false;
InitializePlots();
if (blueprints)
{
do
{
fields = blueprints->Fetch();
if (GarrBuildingEntry const* building = sGarrBuildingStore.LookupEntry(fields[0].GetUInt32()))
_knownBuildings.insert(building->ID);
} while (blueprints->NextRow());
}
if (buildings)
{
do
{
fields = buildings->Fetch();
uint32 plotInstanceId = fields[0].GetUInt32();
uint32 buildingId = fields[1].GetUInt32();
time_t timeBuilt = time_t(fields[2].GetUInt64());
bool active = fields[3].GetBool();
Plot* plot = GetPlot(plotInstanceId);
if (!plot)
continue;
if (!sGarrBuildingStore.LookupEntry(buildingId))
continue;
plot->BuildingInfo.PacketInfo = boost::in_place();
plot->BuildingInfo.PacketInfo->GarrPlotInstanceID = plotInstanceId;
plot->BuildingInfo.PacketInfo->GarrBuildingID = buildingId;
plot->BuildingInfo.PacketInfo->TimeBuilt = timeBuilt;
plot->BuildingInfo.PacketInfo->Active = active;
} while (buildings->NextRow());
}
// 0 1 2 3 4 5 6 7 8 9
// SELECT dbId, followerId, quality, level, itemLevelWeapon, itemLevelArmor, xp, currentBuilding, currentMission, status FROM character_garrison_followers WHERE guid = ?
if (followers)
{
std::unordered_map followersByDbId;
do
{
Field* fields = followers->Fetch();
uint32 followerId = fields[1].GetUInt32();
if (!sGarrFollowerStore.LookupEntry(followerId))
continue;
Follower& follower = _followers[followerId];
follower.PacketInfo.DbID = fields[0].GetUInt64();
follower.PacketInfo.GarrFollowerID = followerId;
follower.PacketInfo.Quality = fields[2].GetUInt32();
follower.PacketInfo.FollowerLevel = fields[3].GetUInt32();
follower.PacketInfo.ItemLevelWeapon = fields[4].GetUInt32();
follower.PacketInfo.ItemLevelArmor = fields[5].GetUInt32();
follower.PacketInfo.Xp = fields[6].GetUInt32();
follower.PacketInfo.CurrentBuildingID = fields[7].GetUInt32();
follower.PacketInfo.CurrentMissionID = fields[8].GetUInt32();
follower.PacketInfo.FollowerStatus = fields[9].GetUInt32();
if (!sGarrBuildingStore.LookupEntry(follower.PacketInfo.CurrentBuildingID))
follower.PacketInfo.CurrentBuildingID = 0;
//if (!sGarrMissionStore.LookupEntry(follower.PacketInfo.CurrentMissionID))
// follower.PacketInfo.CurrentMissionID = 0;
followersByDbId[follower.PacketInfo.DbID] = &follower;
} while (followers->NextRow());
if (abilities)
{
do
{
Field* fields = abilities->Fetch();
uint64 dbId = fields[0].GetUInt64();
GarrAbilityEntry const* ability = sGarrAbilityStore.LookupEntry(fields[1].GetUInt32());
if (!ability)
continue;
auto itr = followersByDbId.find(dbId);
if (itr == followersByDbId.end())
continue;
itr->second->PacketInfo.AbilityID.push_back(ability);
} while (abilities->NextRow());
}
}
return true;
}
void Garrison::SaveToDB(SQLTransaction& trans)
{
PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_CHARACTER_GARRISON);
stmt->setUInt64(0, _owner->GetGUID().GetCounter());
trans->Append(stmt);
stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_CHARACTER_GARRISON_BLUEPRINTS);
stmt->setUInt64(0, _owner->GetGUID().GetCounter());
trans->Append(stmt);
stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_CHARACTER_GARRISON_BUILDINGS);
stmt->setUInt64(0, _owner->GetGUID().GetCounter());
trans->Append(stmt);
stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_CHARACTER_GARRISON_FOLLOWERS);
stmt->setUInt64(0, _owner->GetGUID().GetCounter());
trans->Append(stmt);
stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_CHARACTER_GARRISON);
stmt->setUInt64(0, _owner->GetGUID().GetCounter());
stmt->setUInt32(1, _siteLevel->ID);
stmt->setUInt32(2, _followerActivationsRemainingToday);
trans->Append(stmt);
for (uint32 building : _knownBuildings)
{
stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_CHARACTER_GARRISON_BLUEPRINTS);
stmt->setUInt64(0, _owner->GetGUID().GetCounter());
stmt->setUInt32(1, building);
trans->Append(stmt);
}
for (auto const& p : _plots)
{
Plot const& plot = p.second;
if (plot.BuildingInfo.PacketInfo)
{
stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_CHARACTER_GARRISON_BUILDINGS);
stmt->setUInt64(0, _owner->GetGUID().GetCounter());
stmt->setUInt32(1, plot.BuildingInfo.PacketInfo->GarrPlotInstanceID);
stmt->setUInt32(2, plot.BuildingInfo.PacketInfo->GarrBuildingID);
stmt->setUInt64(3, plot.BuildingInfo.PacketInfo->TimeBuilt);
stmt->setBool(4, plot.BuildingInfo.PacketInfo->Active);
trans->Append(stmt);
}
}
for (auto const& p : _followers)
{
Follower const& follower = p.second;
uint8 index = 0;
stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_CHARACTER_GARRISON_FOLLOWERS);
stmt->setUInt64(index++, follower.PacketInfo.DbID);
stmt->setUInt64(index++, _owner->GetGUID().GetCounter());
stmt->setUInt32(index++, follower.PacketInfo.GarrFollowerID);
stmt->setUInt32(index++, follower.PacketInfo.Quality);
stmt->setUInt32(index++, follower.PacketInfo.FollowerLevel);
stmt->setUInt32(index++, follower.PacketInfo.ItemLevelWeapon);
stmt->setUInt32(index++, follower.PacketInfo.ItemLevelArmor);
stmt->setUInt32(index++, follower.PacketInfo.Xp);
stmt->setUInt32(index++, follower.PacketInfo.CurrentBuildingID);
stmt->setUInt32(index++, follower.PacketInfo.CurrentMissionID);
stmt->setUInt32(index++, follower.PacketInfo.FollowerStatus);
trans->Append(stmt);
uint8 slot = 0;
for (GarrAbilityEntry const* ability : follower.PacketInfo.AbilityID)
{
stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_CHARACTER_GARRISON_FOLLOWER_ABILITIES);
stmt->setUInt64(0, follower.PacketInfo.DbID);
stmt->setUInt32(1, ability->ID);
stmt->setUInt8(2, slot++);
trans->Append(stmt);
}
}
}
bool Garrison::Create(uint32 garrSiteId)
{
_siteLevel = sGarrisonMgr.GetGarrSiteLevelEntry(garrSiteId, 1);
if (!_siteLevel)
return false;
InitializePlots();
WorldPackets::Garrison::GarrisonCreateResult garrisonCreateResult;
garrisonCreateResult.GarrSiteLevelID = _siteLevel->ID;
_owner->SendDirectMessage(garrisonCreateResult.Write());
_owner->SendUpdatePhasing();
SendRemoteInfo();
return true;
}
void Garrison::InitializePlots()
{
if (std::vector const* plots = sGarrisonMgr.GetGarrPlotInstForSiteLevel(_siteLevel->ID))
{
for (std::size_t i = 0; i < plots->size(); ++i)
{
uint32 garrPlotInstanceId = plots->at(i)->GarrPlotInstanceID;
GarrPlotInstanceEntry const* plotInstance = sGarrPlotInstanceStore.LookupEntry(garrPlotInstanceId);
GameObjectsEntry const* gameObject = sGarrisonMgr.GetPlotGameObject(_siteLevel->MapID, garrPlotInstanceId);
if (!plotInstance || !gameObject)
continue;
GarrPlotEntry const* plot = sGarrPlotStore.LookupEntry(plotInstance->GarrPlotID);
if (!plot)
continue;
Plot& plotInfo = _plots[garrPlotInstanceId];
plotInfo.PacketInfo.GarrPlotInstanceID = garrPlotInstanceId;
plotInfo.PacketInfo.PlotPos.Relocate(gameObject->Position.X, gameObject->Position.Y, gameObject->Position.Z, 2 * std::acos(gameObject->RotationW));
plotInfo.PacketInfo.PlotType = plot->PlotType;
plotInfo.EmptyGameObjectId = gameObject->ID;
plotInfo.GarrSiteLevelPlotInstId = plots->at(i)->ID;
}
}
}
void Garrison::Upgrade()
{
}
void Garrison::Enter() const
{
WorldLocation loc(_siteLevel->MapID);
loc.Relocate(_owner);
_owner->TeleportTo(loc, TELE_TO_SEAMLESS);
}
void Garrison::Leave() const
{
if (MapEntry const* map = sMapStore.LookupEntry(_siteLevel->MapID))
{
WorldLocation loc(map->ParentMapID);
loc.Relocate(_owner);
_owner->TeleportTo(loc, TELE_TO_SEAMLESS);
}
}
GarrisonFactionIndex Garrison::GetFaction() const
{
return _owner->GetTeam() == HORDE ? GARRISON_FACTION_INDEX_HORDE : GARRISON_FACTION_INDEX_ALLIANCE;
}
std::vector Garrison::GetPlots()
{
std::vector plots;
plots.reserve(_plots.size());
for (auto& p : _plots)
plots.push_back(&p.second);
return plots;
}
Garrison::Plot* Garrison::GetPlot(uint32 garrPlotInstanceId)
{
auto itr = _plots.find(garrPlotInstanceId);
if (itr != _plots.end())
return &itr->second;
return nullptr;
}
Garrison::Plot const* Garrison::GetPlot(uint32 garrPlotInstanceId) const
{
auto itr = _plots.find(garrPlotInstanceId);
if (itr != _plots.end())
return &itr->second;
return nullptr;
}
void Garrison::LearnBlueprint(uint32 garrBuildingId)
{
WorldPackets::Garrison::GarrisonLearnBlueprintResult learnBlueprintResult;
learnBlueprintResult.BuildingID = garrBuildingId;
learnBlueprintResult.Result = GARRISON_SUCCESS;
if (!sGarrBuildingStore.LookupEntry(garrBuildingId))
learnBlueprintResult.Result = GARRISON_ERROR_INVALID_BUILDINGID;
else if (_knownBuildings.count(garrBuildingId))
learnBlueprintResult.Result = GARRISON_ERROR_BLUEPRINT_KNOWN;
else
_knownBuildings.insert(garrBuildingId);
_owner->SendDirectMessage(learnBlueprintResult.Write());
}
void Garrison::UnlearnBlueprint(uint32 garrBuildingId)
{
WorldPackets::Garrison::GarrisonUnlearnBlueprintResult unlearnBlueprintResult;
unlearnBlueprintResult.BuildingID = garrBuildingId;
unlearnBlueprintResult.Result = GARRISON_SUCCESS;
if (!sGarrBuildingStore.LookupEntry(garrBuildingId))
unlearnBlueprintResult.Result = GARRISON_ERROR_INVALID_BUILDINGID;
else if (!_knownBuildings.count(garrBuildingId))
unlearnBlueprintResult.Result = GARRISON_ERROR_BLUEPRINT_NOT_KNOWN;
else
_knownBuildings.erase(garrBuildingId);
_owner->SendDirectMessage(unlearnBlueprintResult.Write());
}
void Garrison::PlaceBuilding(uint32 garrPlotInstanceId, uint32 garrBuildingId)
{
WorldPackets::Garrison::GarrisonPlaceBuildingResult placeBuildingResult;
placeBuildingResult.Result = CheckBuildingPlacement(garrPlotInstanceId, garrBuildingId);
if (placeBuildingResult.Result == GARRISON_SUCCESS)
{
placeBuildingResult.BuildingInfo.GarrPlotInstanceID = garrPlotInstanceId;
placeBuildingResult.BuildingInfo.GarrBuildingID = garrBuildingId;
placeBuildingResult.BuildingInfo.TimeBuilt = time(nullptr);
Plot* plot = GetPlot(garrPlotInstanceId);
uint32 oldBuildingId = 0;
Map* map = FindMap();
GarrBuildingEntry const* building = sGarrBuildingStore.AssertEntry(garrBuildingId);
if (map)
plot->DeleteGameObject(map);
if (plot->BuildingInfo.PacketInfo)
{
oldBuildingId = plot->BuildingInfo.PacketInfo->GarrBuildingID;
if (sGarrBuildingStore.AssertEntry(oldBuildingId)->Type != building->Type)
plot->ClearBuildingInfo(_owner);
}
plot->SetBuildingInfo(placeBuildingResult.BuildingInfo, _owner);
if (map)
if (GameObject* go = plot->CreateGameObject(map, GetFaction()))
map->AddToMap(go);
_owner->ModifyCurrency(building->CostCurrencyID, -building->CostCurrencyAmount, false, true);
_owner->ModifyMoney(-building->CostMoney * GOLD, false);
if (oldBuildingId)
{
WorldPackets::Garrison::GarrisonBuildingRemoved buildingRemoved;
buildingRemoved.Result = GARRISON_SUCCESS;
buildingRemoved.GarrPlotInstanceID = garrPlotInstanceId;
buildingRemoved.GarrBuildingID = oldBuildingId;
_owner->SendDirectMessage(buildingRemoved.Write());
}
}
_owner->SendDirectMessage(placeBuildingResult.Write());
}
void Garrison::CancelBuildingConstruction(uint32 garrPlotInstanceId)
{
WorldPackets::Garrison::GarrisonBuildingRemoved buildingRemoved;
buildingRemoved.Result = CheckBuildingRemoval(garrPlotInstanceId);
if (buildingRemoved.Result == GARRISON_SUCCESS)
{
Plot* plot = GetPlot(garrPlotInstanceId);
buildingRemoved.GarrPlotInstanceID = garrPlotInstanceId;
buildingRemoved.GarrBuildingID = plot->BuildingInfo.PacketInfo->GarrBuildingID;
Map* map = FindMap();
if (map)
plot->DeleteGameObject(map);
plot->ClearBuildingInfo(_owner);
_owner->SendDirectMessage(buildingRemoved.Write());
GarrBuildingEntry const* constructing = sGarrBuildingStore.AssertEntry(buildingRemoved.GarrBuildingID);
// Refund construction/upgrade cost
_owner->ModifyCurrency(constructing->CostCurrencyID, constructing->CostCurrencyAmount, false, true);
_owner->ModifyMoney(constructing->CostMoney * GOLD, false);
if (constructing->Level > 1)
{
// Restore previous level building
GarrBuildingEntry const* restored = sGarrisonMgr.GetPreviousLevelBuilding(constructing->Type, constructing->Level);
ASSERT(restored);
WorldPackets::Garrison::GarrisonPlaceBuildingResult placeBuildingResult;
placeBuildingResult.Result = GARRISON_SUCCESS;
placeBuildingResult.BuildingInfo.GarrPlotInstanceID = garrPlotInstanceId;
placeBuildingResult.BuildingInfo.GarrBuildingID = restored->ID;
placeBuildingResult.BuildingInfo.TimeBuilt = time(nullptr);
placeBuildingResult.BuildingInfo.Active = true;
plot->SetBuildingInfo(placeBuildingResult.BuildingInfo, _owner);
_owner->SendDirectMessage(placeBuildingResult.Write());
}
if (map)
if (GameObject* go = plot->CreateGameObject(map, GetFaction()))
map->AddToMap(go);
}
else
_owner->SendDirectMessage(buildingRemoved.Write());
}
void Garrison::AddFollower(uint32 garrFollowerId)
{
WorldPackets::Garrison::GarrisonAddFollowerResult addFollowerResult;
GarrFollowerEntry const* followerEntry = sGarrFollowerStore.LookupEntry(garrFollowerId);
if (_followers.count(garrFollowerId) || !followerEntry)
{
addFollowerResult.Result = GARRISON_GENERIC_UNKNOWN_ERROR;
_owner->SendDirectMessage(addFollowerResult.Write());
return;
}
Follower& follower = _followers[garrFollowerId];
follower.PacketInfo.DbID = sGarrisonMgr.GenerateFollowerDbId();
follower.PacketInfo.GarrFollowerID = garrFollowerId;
follower.PacketInfo.Quality = followerEntry->Quality; // TODO: handle magic upgrades
follower.PacketInfo.FollowerLevel = followerEntry->Level;
follower.PacketInfo.ItemLevelWeapon = followerEntry->ItemLevelWeapon;
follower.PacketInfo.ItemLevelArmor = followerEntry->ItemLevelArmor;
follower.PacketInfo.Xp = 0;
follower.PacketInfo.CurrentBuildingID = 0;
follower.PacketInfo.CurrentMissionID = 0;
follower.PacketInfo.AbilityID = sGarrisonMgr.RollFollowerAbilities(followerEntry, follower.PacketInfo.Quality, GetFaction(), true);
follower.PacketInfo.FollowerStatus = 0;
addFollowerResult.Follower = follower.PacketInfo;
_owner->SendDirectMessage(addFollowerResult.Write());
}
void Garrison::SendInfo()
{
WorldPackets::Garrison::GetGarrisonInfoResult garrisonInfo;
garrisonInfo.GarrSiteID = _siteLevel->SiteID;
garrisonInfo.GarrSiteLevelID = _siteLevel->ID;
garrisonInfo.FactionIndex = GetFaction();
garrisonInfo.NumFollowerActivationsRemaining = _followerActivationsRemainingToday;
for (auto& p : _plots)
{
Plot& plot = p.second;
garrisonInfo.Plots.push_back(&plot.PacketInfo);
if (plot.BuildingInfo.PacketInfo)
garrisonInfo.Buildings.push_back(plot.BuildingInfo.PacketInfo.get_ptr());
}
for (auto const& p : _followers)
garrisonInfo.Followers.push_back(&p.second.PacketInfo);
_owner->SendDirectMessage(garrisonInfo.Write());
}
void Garrison::SendRemoteInfo() const
{
MapEntry const* garrisonMap = sMapStore.LookupEntry(_siteLevel->MapID);
if (!garrisonMap || int32(_owner->GetMapId()) != garrisonMap->ParentMapID)
return;
WorldPackets::Garrison::GarrisonRemoteInfo remoteInfo;
remoteInfo.Sites.resize(1);
WorldPackets::Garrison::GarrisonRemoteSiteInfo& remoteSiteInfo = remoteInfo.Sites[0];
remoteSiteInfo.GarrSiteLevelID = _siteLevel->ID;
for (auto const& p : _plots)
if (p.second.BuildingInfo.PacketInfo)
remoteSiteInfo.Buildings.emplace_back(p.first, p.second.BuildingInfo.PacketInfo->GarrBuildingID);
_owner->SendDirectMessage(remoteInfo.Write());
}
void Garrison::SendBlueprintAndSpecializationData()
{
WorldPackets::Garrison::GarrisonRequestBlueprintAndSpecializationDataResult data;
data.BlueprintsKnown = &_knownBuildings;
_owner->SendDirectMessage(data.Write());
}
void Garrison::SendBuildingLandmarks(Player* receiver) const
{
WorldPackets::Garrison::GarrisonBuildingLandmarks buildingLandmarks;
buildingLandmarks.Landmarks.reserve(_plots.size());
for (auto const& p : _plots)
{
Plot const& plot = p.second;
if (plot.BuildingInfo.PacketInfo)
if (uint32 garrBuildingPlotInstId = sGarrisonMgr.GetGarrBuildingPlotInst(plot.BuildingInfo.PacketInfo->GarrBuildingID, plot.GarrSiteLevelPlotInstId))
buildingLandmarks.Landmarks.emplace_back(garrBuildingPlotInstId, plot.PacketInfo.PlotPos);
}
receiver->SendDirectMessage(buildingLandmarks.Write());
}
Map* Garrison::FindMap() const
{
return sMapMgr->FindMap(_siteLevel->MapID, _owner->GetGUID().GetCounter());
}
GarrisonError Garrison::CheckBuildingPlacement(uint32 garrPlotInstanceId, uint32 garrBuildingId) const
{
GarrPlotInstanceEntry const* plotInstance = sGarrPlotInstanceStore.LookupEntry(garrPlotInstanceId);
Plot const* plot = GetPlot(garrPlotInstanceId);
if (!plotInstance || !plot)
return GARRISON_ERROR_INVALID_PLOT;
GarrBuildingEntry const* building = sGarrBuildingStore.LookupEntry(garrBuildingId);
if (!building)
return GARRISON_ERROR_INVALID_BUILDINGID;
if (!sGarrisonMgr.IsPlotMatchingBuilding(plotInstance->GarrPlotID, garrBuildingId))
return GARRISON_ERROR_INVALID_PLOT_BUILDING;
// Cannot place buldings of higher level than garrison level
if (building->Level > _siteLevel->Level)
return GARRISON_ERROR_INVALID_BUILDINGID;
if (building->Flags & GARRISON_BUILDING_FLAG_NEEDS_PLAN)
{
if (!_knownBuildings.count(garrBuildingId))
return GARRISON_ERROR_BLUEPRINT_NOT_KNOWN;
}
else // Building is built as a quest reward
return GARRISON_ERROR_INVALID_BUILDINGID;
// Check all plots to find if we already have this building
GarrBuildingEntry const* existingBuilding;
for (auto const& p : _plots)
{
if (p.second.BuildingInfo.PacketInfo)
{
existingBuilding = sGarrBuildingStore.AssertEntry(p.second.BuildingInfo.PacketInfo->GarrBuildingID);
if (existingBuilding->Type == building->Type)
if (p.first != garrPlotInstanceId || existingBuilding->Level + 1 != building->Level) // check if its an upgrade in same plot
return GARRISON_ERROR_BUILDING_EXISTS;
}
}
if (!_owner->HasCurrency(building->CostCurrencyID, building->CostCurrencyAmount))
return GARRISON_ERROR_NOT_ENOUGH_CURRENCY;
if (!_owner->HasEnoughMoney(uint64(building->CostMoney * GOLD)))
return GARRISON_ERROR_NOT_ENOUGH_GOLD;
// New building cannot replace another building currently under construction
if (plot->BuildingInfo.PacketInfo)
if (!plot->BuildingInfo.PacketInfo->Active)
return GARRISON_ERROR_NO_BUILDING;
return GARRISON_SUCCESS;
}
GarrisonError Garrison::CheckBuildingRemoval(uint32 garrPlotInstanceId) const
{
Plot const* plot = GetPlot(garrPlotInstanceId);
if (!plot)
return GARRISON_ERROR_INVALID_PLOT;
if (!plot->BuildingInfo.PacketInfo)
return GARRISON_ERROR_NO_BUILDING;
if (plot->BuildingInfo.CanActivate())
return GARRISON_ERROR_BUILDING_EXISTS;
return GARRISON_SUCCESS;
}
GameObject* Garrison::Plot::CreateGameObject(Map* map, GarrisonFactionIndex faction)
{
uint32 entry = EmptyGameObjectId;
if (BuildingInfo.PacketInfo)
{
GarrPlotInstanceEntry const* plotInstance = sGarrPlotInstanceStore.AssertEntry(PacketInfo.GarrPlotInstanceID);
GarrPlotEntry const* plot = sGarrPlotStore.AssertEntry(plotInstance->GarrPlotID);
GarrBuildingEntry const* building = sGarrBuildingStore.AssertEntry(BuildingInfo.PacketInfo->GarrBuildingID);
if (BuildingInfo.PacketInfo->Active)
entry = faction == GARRISON_FACTION_INDEX_HORDE ? building->HordeGameObjectID : building->AllianceGameObjectID;
else
entry = faction == GARRISON_FACTION_INDEX_HORDE ? plot->HordeConstructionGameObjectID : plot->AllianceConstructionGameObjectID;
}
if (!sObjectMgr->GetGameObjectTemplate(entry))
{
TC_LOG_ERROR("garrison", "Garrison attempted to spawn gameobject whose template doesn't exist (%u)", entry);
return nullptr;
}
Position const& pos = PacketInfo.PlotPos;
GameObject* go = new GameObject();
if (!go->Create(map->GenerateLowGuid(), entry, map, 0, pos.GetPositionX(), pos.GetPositionY(), pos.GetPositionZ(), pos.GetOrientation(),
0.0f, 0.0f, 0.0f, 0.0f, 0, GO_STATE_ACTIVE))
{
delete go;
return nullptr;
}
BuildingInfo.Guid = go->GetGUID();
return go;
}
void Garrison::Plot::DeleteGameObject(Map* map)
{
if (BuildingInfo.Guid.IsEmpty())
return;
if (GameObject* oldBuilding = map->GetGameObject(BuildingInfo.Guid))
oldBuilding->Delete();
BuildingInfo.Guid.Clear();
}
void Garrison::Plot::ClearBuildingInfo(Player* owner)
{
WorldPackets::Garrison::GarrisonPlotPlaced plotPlaced;
plotPlaced.PlotInfo = &PacketInfo;
owner->SendDirectMessage(plotPlaced.Write());
BuildingInfo.PacketInfo = boost::none;
}
void Garrison::Plot::SetBuildingInfo(WorldPackets::Garrison::GarrisonBuildingInfo const& buildingInfo, Player* owner)
{
if (!BuildingInfo.PacketInfo)
{
WorldPackets::Garrison::GarrisonPlotRemoved plotRemoved;
plotRemoved.GarrPlotInstanceID = PacketInfo.GarrPlotInstanceID;
owner->SendDirectMessage(plotRemoved.Write());
}
BuildingInfo.PacketInfo = buildingInfo;
}
bool Garrison::Building::CanActivate() const
{
if (PacketInfo)
{
GarrBuildingEntry const* building = sGarrBuildingStore.AssertEntry(PacketInfo->GarrBuildingID);
if (PacketInfo->TimeBuilt + building->BuildDuration <= time(nullptr))
return true;
}
return false;
}