Skip to content

Commit

Permalink
fix(Core/Grid): Address bugs and performance issues introduced by vis…
Browse files Browse the repository at this point in the history
…ibility notifier implementation (azerothcore#17480)

* Bug fixes

- Corrected std::chrono from seconds to milliseconds
- Got rid of leftover code that caused objects to not show up on time

* Removed logic to set gameobject as active

- More alignement with TC.
- Reduces CPU usage drastically

* Revert back to using time_t instead of std chrono

* Invoke SetNoCreate() method to reduce CPU usage drastically

* Remove setActive from static and motion transports

* Fix performance issues

* Added SetFarVisible to WG and some dungeon scripts

- Also removed setActive(true) from creatures in Wintergrasp. As for gameobjects they are set to active upon being damaged/destroyed and removed from active on rebuild (reset)

* Removed comments related to VISIBILITY_COMPENSATION

* Fix log

* Deleted unused files + corrected a check

* Added missing header

* Removed unused parameter

* Removed another unsued parameter

* Changed vector to set for i_visibleNow

- Changed vector to set for i_visibleNow in VisibleNotifer
- Adjusted HaveAtClient to accept Object*
- Adjusted SendUpdateToPlayer to send createobject packet only if not known to client
  • Loading branch information
AGandrup authored Oct 23, 2023
1 parent a56a224 commit 60e2751
Show file tree
Hide file tree
Showing 53 changed files with 509 additions and 586 deletions.
4 changes: 2 additions & 2 deletions src/server/game/Battlefield/Battlefield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ Creature* Battlefield::SpawnCreature(uint32 entry, float x, float y, float z, fl

// Set creature in world
map->AddToMap(creature);
creature->setActive(true);
creature->SetFarVisible(true);

return creature;
}
Expand All @@ -840,7 +840,7 @@ GameObject* Battlefield::SpawnGameObject(uint32 entry, float x, float y, float z

// Add to world
map->AddToMap(go);
go->setActive(true);
go->SetFarVisible(true);

return go;
}
Expand Down
9 changes: 9 additions & 0 deletions src/server/game/Battlefield/Zones/BattlefieldWG.h
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,7 @@ struct BfWGGameObjectBuilding
// Rebuild gameobject
go->SetDestructibleState(GO_DESTRUCTIBLE_REBUILDING, nullptr, true);
go->SetUInt32Value(GAMEOBJECT_FACTION, WintergraspFaction[m_Team]);
go->setActive(false); // Everything is reset, no need keep active at this point.
}

// Update worldstate
Expand All @@ -1170,6 +1171,10 @@ struct BfWGGameObjectBuilding
// Called when associated gameobject is damaged
void Damaged()
{
GameObject* go = m_WG->GetGameObject(m_Build);
if (go)
go->setActive(true);

// Update worldstate
m_State = BATTLEFIELD_WG_OBJECTSTATE_ALLIANCE_DAMAGE - (m_Team * 3);
m_WG->SendUpdateWorldState(m_WorldState, m_State);
Expand All @@ -1193,6 +1198,10 @@ struct BfWGGameObjectBuilding
// Called when associated gameobject is destroyed
void Destroyed()
{
GameObject* go = m_WG->GetGameObject(m_Build);
if (go)
go->setActive(true);

// Update worldstate
m_State = BATTLEFIELD_WG_OBJECTSTATE_ALLIANCE_DESTROY - (m_Team * 3);
m_WG->SendUpdateWorldState(m_WorldState, m_State);
Expand Down
28 changes: 20 additions & 8 deletions src/server/game/Entities/Creature/Creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,6 @@ void Creature::RemoveFromWorld()
if (m_formation)
sFormationMgr->RemoveCreatureFromGroup(m_formation, this);

if (Transport* transport = GetTransport())
transport->RemovePassenger(this, true);

Unit::RemoveFromWorld();

if (m_spawnId)
Expand Down Expand Up @@ -1826,7 +1823,25 @@ void Creature::DeleteFromDB()
return;
}

GetMap()->RemoveCreatureRespawnTime(m_spawnId);
CreatureData const* data = sObjectMgr->GetCreatureData(m_spawnId);
if (!data)
return;

CharacterDatabaseTransaction charTrans = CharacterDatabase.BeginTransaction();

sMapMgr->DoForAllMapsWithMapId(data->mapid,
[this, charTrans](Map* map) -> void
{
// despawn all active creatures, and remove their respawns
std::vector<Creature*> toUnload;
for (auto const& pair : Acore::Containers::MapEqualRange(map->GetCreatureBySpawnIdStore(), m_spawnId))
toUnload.push_back(pair.second);
for (Creature* creature : toUnload)
map->AddObjectToRemoveList(creature);
map->RemoveCreatureRespawnTime(m_spawnId);
}
);

sObjectMgr->DeleteCreatureData(m_spawnId);

WorldDatabaseTransaction trans = WorldDatabase.BeginTransaction();
Expand Down Expand Up @@ -3060,10 +3075,7 @@ std::string const& Creature::GetNameForLocaleIdx(LocaleConstant loc_idx) const

void Creature::SetPosition(float x, float y, float z, float o)
{
if (!Acore::IsValidMapCoord(x, y, z, o))
return;

GetMap()->CreatureRelocation(this, x, y, z, o);
UpdatePosition(x, y, z, o, false);
}

bool Creature::IsDungeonBoss() const
Expand Down
1 change: 1 addition & 0 deletions src/server/game/Entities/Creature/Creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Creature : public Unit, public GridObject<Creature>, public MovableMapObje

void Update(uint32 time) override; // overwrited Unit::Update
void GetRespawnPosition(float& x, float& y, float& z, float* ori = nullptr, float* dist = nullptr) const;
bool IsSpawnedOnTransport() const { return m_creatureData && m_creatureData->mapid != GetMapId(); }

void SetCorpseDelay(uint32 delay) { m_corpseDelay = delay; }
void SetCorpseRemoveTime(uint32 delay);
Expand Down
19 changes: 3 additions & 16 deletions src/server/game/Entities/DynamicObject/DynamicObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ DynamicObject::~DynamicObject()

void DynamicObject::CleanupsBeforeDelete(bool finalCleanup /* = true */)
{
if (Transport* transport = GetTransport())
{
transport->RemovePassenger(this);
SetTransport(nullptr);
m_movementInfo.transport.Reset();
m_movementInfo.RemoveMovementFlag(MOVEMENTFLAG_ONTRANSPORT);
}

WorldObject::CleanupsBeforeDelete(finalCleanup);
}

Expand Down Expand Up @@ -88,9 +80,6 @@ void DynamicObject::RemoveFromWorld()

UnbindFromCaster();

if (Transport* transport = GetTransport())
transport->RemovePassenger(this, true);

WorldObject::RemoveFromWorld();

GetMap()->GetObjectsStore().Remove<DynamicObject>(GetGUID());
Expand Down Expand Up @@ -125,17 +114,15 @@ bool DynamicObject::CreateDynamicObject(ObjectGuid::LowType guidlow, Unit* caste
SetFloatValue(DYNAMICOBJECT_RADIUS, radius);
SetUInt32Value(DYNAMICOBJECT_CASTTIME, GameTime::GetGameTimeMS().count());

if (IsWorldObject())
setActive(true); //must before add to map to be put in world container

if (!GetMap()->AddToMap(this, true))
{
// Returning false will cause the object to be deleted - remove from transport
return false;
}

if (IsWorldObject())
{
setActive(true);
}

return true;
}

Expand Down
58 changes: 33 additions & 25 deletions src/server/game/Entities/GameObject/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "GridNotifiersImpl.h"
#include "Group.h"
#include "GroupMgr.h"
#include "MapMgr.h"
#include "ObjectMgr.h"
#include "OutdoorPvPMgr.h"
#include "PoolMgr.h"
Expand Down Expand Up @@ -102,18 +103,9 @@ std::string const& GameObject::GetAIName() const
return sObjectMgr->GetGameObjectTemplate(GetEntry())->AIName;
}

void GameObject::CleanupsBeforeDelete(bool /*finalCleanup*/)
void GameObject::CleanupsBeforeDelete(bool finalCleanup)
{
if (GetTransport() && !ToTransport())
{
GetTransport()->RemovePassenger(this);
SetTransport(nullptr);
m_movementInfo.transport.Reset();
m_movementInfo.RemoveMovementFlag(MOVEMENTFLAG_ONTRANSPORT);
}

if (IsInWorld())
RemoveFromWorld();
WorldObject::CleanupsBeforeDelete(finalCleanup);

if (m_uint32Values) // field array can be not exist if GameOBject not loaded
RemoveFromOwner();
Expand Down Expand Up @@ -182,9 +174,6 @@ void GameObject::RemoveFromWorld()
if (GetMap()->ContainsGameObjectModel(*m_model))
GetMap()->RemoveGameObjectModel(*m_model);

if (Transport* transport = GetTransport())
transport->RemovePassenger(this, true);

// If linked trap exists, despawn it
if (GameObject* linkedTrap = GetLinkedTrap())
{
Expand Down Expand Up @@ -888,7 +877,11 @@ void GameObject::Update(uint32 diff)
if (!m_spawnedByDefault)
{
m_respawnTime = 0;
DestroyForNearbyPlayers(); // xinef: old UpdateObjectVisibility();
if (m_spawnId)
DestroyForNearbyPlayers(); // xinef: old UpdateObjectVisibility();
else
Delete();

return;
}

Expand Down Expand Up @@ -1184,7 +1177,31 @@ bool GameObject::LoadGameObjectFromDB(ObjectGuid::LowType spawnId, Map* map, boo

void GameObject::DeleteFromDB()
{
GetMap()->RemoveGORespawnTime(m_spawnId);
if (!m_spawnId)
{
LOG_ERROR("entities.gameobject", "Trying to delete not saved gameobject: {}", GetGUID().ToString());
return;
}

GameObjectData const* data = sObjectMgr->GetGameObjectData(m_spawnId);
if (!data)
return;

CharacterDatabaseTransaction charTrans = CharacterDatabase.BeginTransaction();

sMapMgr->DoForAllMapsWithMapId(data->mapid,
[this, charTrans](Map* map) -> void
{
// despawn all active objects, and remove their respawns
std::vector<GameObject*> toUnload;
for (auto const& pair : Acore::Containers::MapEqualRange(map->GetGameObjectBySpawnIdStore(), m_spawnId))
toUnload.push_back(pair.second);
for (GameObject* obj : toUnload)
map->AddObjectToRemoveList(obj);
map->RemoveGORespawnTime(m_spawnId);
}
);

sObjectMgr->DeleteGOData(m_spawnId);

WorldDatabasePreparedStatement* stmt = WorldDatabase.GetPreparedStatement(WORLD_DEL_GAMEOBJECT);
Expand Down Expand Up @@ -2173,15 +2190,6 @@ bool GameObject::IsInRange(float x, float y, float z, float radius) const
&& dz < (info->maxZ * scale) + radius && dz > (info->minZ * scale) - radius;
}

void GameObject::SendMessageToSetInRange(WorldPacket const* data, float dist, bool /*self*/, bool includeMargin, Player const* skipped_rcvr) const
{
dist += GetObjectSize();
if (includeMargin)
dist += VISIBILITY_COMPENSATION * 2.0f; // pussywizard: to ensure everyone receives all important packets
Acore::MessageDistDeliverer notifier(this, data, dist, false, skipped_rcvr);
Cell::VisitWorldObjects(this, notifier, dist);
}

void GameObject::EventInform(uint32 eventId)
{
if (!eventId)
Expand Down
2 changes: 0 additions & 2 deletions src/server/game/Entities/GameObject/GameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,6 @@ class GameObject : public WorldObject, public GridObject<GameObject>, public Mov
void SendCustomAnim(uint32 anim);
[[nodiscard]] bool IsInRange(float x, float y, float z, float radius) const;

void SendMessageToSetInRange(WorldPacket const* data, float dist, bool /*self*/, bool includeMargin = false, Player const* skipped_rcvr = nullptr) const override; // pussywizard!

void ModifyHealth(int32 change, Unit* attackerOrHealer = nullptr, uint32 spellId = 0);
void SetDestructibleBuildingModifyState(bool allow) { m_allowModifyDestructibleBuilding = allow; }
// sets GameObject type 33 destruction flags and optionally default health for that state
Expand Down
Loading

0 comments on commit 60e2751

Please sign in to comment.