Skip to content

Commit

Permalink
Player/Visibility: Rewrite login protocol to include all in one packe…
Browse files Browse the repository at this point in the history
…t in proper order
  • Loading branch information
killerwife committed Jan 24, 2025
1 parent 6beab3a commit 15ba646
Show file tree
Hide file tree
Showing 14 changed files with 216 additions and 116 deletions.
22 changes: 15 additions & 7 deletions src/game/Entities/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "Util/Errors.h"
#include "Entities/Player.h"

Camera::Camera(Player* pl) : m_owner(*pl), m_source(pl)
Camera::Camera(Player* pl) : m_owner(*pl), m_source(pl), m_sendInProgress(false)
{
m_source->GetViewPoint().Attach(this);
}
Expand Down Expand Up @@ -100,13 +100,13 @@ void Camera::ResetView(bool update_far_sight_field /*= true*/)
SetView(&m_owner, update_far_sight_field);
}

void Camera::Event_AddedToWorld()
void Camera::Event_AddedToWorld(UpdateData& data)
{
GridType* grid = m_source->GetViewPoint().m_grid;
MANGOS_ASSERT(grid);
grid->AddWorldObject(this);

UpdateVisibilityForOwner(true);
UpdateVisibilityForOwner(true, data);
}

void Camera::Event_RemovedFromWorld()
Expand All @@ -126,9 +126,9 @@ void Camera::Event_Moved()
m_source->GetViewPoint().m_grid->AddWorldObject(this);
}

void Camera::UpdateVisibilityOf(WorldObject* target) const
void Camera::UpdateVisibilityOf(WorldObject* target, UpdateData& data) const
{
m_owner.UpdateVisibilityOf(m_source, target);
m_owner.UpdateVisibilityOf(m_source, target, data);
}

template<class T>
Expand All @@ -143,11 +143,19 @@ template void Camera::UpdateVisibilityOf(Corpse*, UpdateData&, WorldObjectSet&);
template void Camera::UpdateVisibilityOf(GameObject*, UpdateData&, WorldObjectSet&);
template void Camera::UpdateVisibilityOf(DynamicObject*, UpdateData&, WorldObjectSet&);

void Camera::UpdateVisibilityForOwner(bool addToWorld)
void Camera::UpdateVisibilityForOwner()
{
MaNGOS::VisibleNotifier notifier(*this);
UpdateData data;
UpdateVisibilityForOwner(false, data);
}

void Camera::UpdateVisibilityForOwner(bool addToWorld, UpdateData& data)
{
m_sendInProgress = true;
MaNGOS::VisibleNotifier notifier(*this, data, !addToWorld);
Cell::VisitAllObjects(m_source, notifier, addToWorld ? MAX_VISIBILITY_DISTANCE : m_source->GetVisibilityData().GetVisibilityDistance(), false);
notifier.Notify();
m_sendInProgress = false;
}

//////////////////
Expand Down
28 changes: 16 additions & 12 deletions src/game/Entities/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,19 @@ class Camera

template<class T>
void UpdateVisibilityOf(T* target, UpdateData& data, WorldObjectSet& vis);
void UpdateVisibilityOf(WorldObject* target) const;
void UpdateVisibilityOf(WorldObject* target, UpdateData& data) const;

void ReceivePacket(WorldPacket const& data) const;

// updates visibility of worldobjects around viewpoint for camera's owner
void UpdateVisibilityForOwner() { UpdateVisibilityForOwner(false); }
void UpdateVisibilityForOwner(bool addToWorld);
void UpdateVisibilityForOwner();
void UpdateVisibilityForOwner(bool addToWorld, UpdateData& data);

bool IsSendInProgress() const { return m_sendInProgress; }

private:
// called when viewpoint changes visibility state
void Event_AddedToWorld();
void Event_AddedToWorld(UpdateData& data);
void Event_RemovedFromWorld();
void Event_Moved();
void Event_ViewPointVisibilityChanged();
Expand All @@ -74,6 +76,7 @@ class Camera
bool isActiveObject() const { return false; }
private:
GridReference<Camera> m_gridRef;
bool m_sendInProgress;
};

/// Object-observer, notifies farsight object state to cameras that attached to it
Expand All @@ -89,14 +92,15 @@ class ViewPoint
void Attach(Camera* c) { m_cameras.push_back(c); }
void Detach(Camera* c) { m_cameras.remove(c); }

void CameraCall(void (Camera::*handler)())
template <typename F>
void CameraCall(F handler)
{
if (!m_cameras.empty())
{
for (CameraList::iterator itr = m_cameras.begin(); itr != m_cameras.end();)
{
Camera* c = *(itr++);
(c->*handler)();
handler(c);
}
}
}
Expand All @@ -109,32 +113,32 @@ class ViewPoint
bool hasViewers() const { return !m_cameras.empty(); }

// these events are called when viewpoint changes visibility state
void Event_AddedToWorld(GridType* grid)
void Event_AddedToWorld(GridType* grid, UpdateData& data)
{
m_grid = grid;
CameraCall(&Camera::Event_AddedToWorld);
CameraCall([&](Camera* c) { c->Event_AddedToWorld(data); });
}

void Event_RemovedFromWorld()
{
m_grid = nullptr;
CameraCall(&Camera::Event_RemovedFromWorld);
CameraCall([&](Camera* c) { c->Event_RemovedFromWorld(); });
}

void Event_GridChanged(GridType* grid)
{
m_grid = grid;
CameraCall(&Camera::Event_Moved);
CameraCall([&](Camera* c) { c->Event_Moved(); });
}

void Event_ViewPointVisibilityChanged()
{
CameraCall(&Camera::Event_ViewPointVisibilityChanged);
CameraCall([&](Camera* c) { c->Event_ViewPointVisibilityChanged(); });
}

void Call_UpdateVisibilityForOwner()
{
CameraCall(&Camera::UpdateVisibilityForOwner);
CameraCall([&](Camera* c) { c->UpdateVisibilityForOwner(); });
}
};

Expand Down
8 changes: 6 additions & 2 deletions src/game/Entities/ItemHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,13 +585,17 @@ void WorldSession::HandleSellItemOpcode(WorldPacket& recv_data)
_player->InterruptNonMeleeSpells(false);
pItem->SetCount(pItem->GetCount() - count);
_player->ItemRemovedQuestCheck(pItem->GetEntry(), count);
UpdateData data;
if (_player->IsInWorld())
pItem->SendCreateUpdateToPlayer(_player);
pItem->SendCreateUpdateToPlayer(_player, data);
pItem->SetState(ITEM_CHANGED, _player);

_player->AddItemToBuyBackSlot(pNewItem, money);
if (_player->IsInWorld())
pNewItem->SendCreateUpdateToPlayer(_player);
pNewItem->SendCreateUpdateToPlayer(_player, data);

if (_player->IsInWorld())
data.SendData(*_player->GetSession());
}
else
{
Expand Down
11 changes: 2 additions & 9 deletions src/game/Entities/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,10 @@ void Object::BuildCreateUpdateBlockForPlayer(UpdateData* data, Player* target) c
data->AddUpdateBlock(buf);
}

void Object::SendCreateUpdateToPlayer(Player* player) const
void Object::SendCreateUpdateToPlayer(Player* player, UpdateData& data) const
{
// send create update to player
UpdateData updateData;
BuildCreateUpdateBlockForPlayer(&updateData, player);

for (size_t i = 0; i < updateData.GetPacketCount(); ++i)
{
WorldPacket packet = updateData.BuildPacket(i);
player->GetSession()->SendPacket(packet);
}
BuildCreateUpdateBlockForPlayer(&data, player);
}

void Object::BuildValuesUpdateBlockForPlayer(UpdateData& data, Player* target) const
Expand Down
2 changes: 1 addition & 1 deletion src/game/Entities/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ class Object
bool isType(TypeMask mask) const { return (mask & m_objectType) != 0; }

virtual void BuildCreateUpdateBlockForPlayer(UpdateData* data, Player* target) const;
void SendCreateUpdateToPlayer(Player* player) const;
void SendCreateUpdateToPlayer(Player* player, UpdateData& data) const;

// must be overwrite in appropriate subclasses (WorldObject, Item currently), or will crash
virtual void AddToClientUpdateList();
Expand Down
Loading

0 comments on commit 15ba646

Please sign in to comment.