Skip to content

Commit 27f9ef5

Browse files
committed
Add dimension optimization
1 parent 2f2df9c commit 27f9ef5

File tree

4 files changed

+101
-70
lines changed

4 files changed

+101
-70
lines changed

Client/mods/deathmatch/logic/CClientStreamSector.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class CClientStreamSector
3737
void Remove(CClientStreamElement* pElement) { m_Elements.remove(pElement); }
3838
std::list<CClientStreamElement*>::iterator Begin() { return m_Elements.begin(); }
3939
std::list<CClientStreamElement*>::iterator End() { return m_Elements.end(); }
40+
std::list<CClientStreamElement*>& GetElements() { return m_Elements; }
4041

4142
void AddElements(std::list<CClientStreamElement*>* pList);
4243
void RemoveElements(std::list<CClientStreamElement*>* pList);

Client/mods/deathmatch/logic/CClientStreamSectorRow.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class CClientStreamSectorRow
3131
void Add(CClientStreamSector* pSector);
3232
void Remove(CClientStreamSector* pSector);
3333
unsigned int CountSectors() { return m_Sectors.size(); }
34+
std::list<CClientStreamSector*>& GetList() noexcept { return m_Sectors; }
3435

3536
bool DoesContain(CVector& vecPosition);
3637
bool DoesContain(float fY);

Client/mods/deathmatch/logic/CClientStreamer.cpp

Lines changed: 90 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
*****************************************************************************/
1010

1111
#include "StdInc.h"
12+
#include "CClientGame.h"
13+
14+
extern CClientGame* g_pClientGame;
1215
using std::list;
1316

1417
void* CClientStreamer::pAddingElement = NULL;
@@ -107,51 +110,8 @@ void CClientStreamer::ConnectRow(CClientStreamSectorRow* pRow)
107110
pRow->m_pBottom->m_pTop = pRow;
108111
}
109112

110-
#include "..\deathmatch\logic\CClientGame.h"
111-
extern CClientGame* g_pClientGame;
112-
void CClientStreamer::DoPulse(CVector& vecPosition)
113+
void CClientStreamer::DoPulse(CVector& vecPosition)
113114
{
114-
/* Debug code
115-
CClientStreamSector * pSector;
116-
list < CClientStreamSector * > ::iterator iterSector;
117-
list < CClientStreamSectorRow * > ::iterator iterRow = m_WorldRows.begin ();
118-
for ( ; iterRow != m_WorldRows.end () ; iterRow++ )
119-
{
120-
iterSector = (*iterRow)->Begin ();
121-
for ( ; iterSector != (*iterRow)->End () ; iterSector++ )
122-
{
123-
pSector = *iterSector;
124-
if ( !pSector->m_pArea )
125-
{
126-
pSector->m_pArea = new CClientRadarArea ( g_pClientGame->GetManager (), INVALID_ELEMENT_ID );
127-
pSector->m_pArea->SetPosition ( pSector->m_vecBottomLeft );
128-
CVector2D vecSize ( pSector->m_vecTopRight.fX - pSector->m_vecBottomLeft.fX, pSector->m_vecTopRight.fY - pSector->m_vecBottomLeft.fY );
129-
pSector->m_pArea->SetSize ( vecSize );
130-
pSector->m_pArea->SetColor ( 255, 0, 0, 50 );
131-
}
132-
pSector->m_pArea->SetColor ( 255, 0, 0, 50 );
133-
}
134-
}
135-
iterRow = m_ExtraRows.begin ();
136-
for ( ; iterRow != m_ExtraRows.end () ; iterRow++ )
137-
{
138-
iterSector = (*iterRow)->Begin ();
139-
for ( ; iterSector != (*iterRow)->End () ; iterSector++ )
140-
{
141-
pSector = *iterSector;
142-
if ( !pSector->m_pArea )
143-
{
144-
pSector->m_pArea = new CClientRadarArea ( g_pClientGame->GetManager (), INVALID_ELEMENT_ID );
145-
pSector->m_pArea->SetPosition ( pSector->m_vecBottomLeft );
146-
CVector2D vecSize ( pSector->m_vecTopRight.fX - pSector->m_vecBottomLeft.fX, pSector->m_vecTopRight.fY - pSector->m_vecBottomLeft.fY );
147-
pSector->m_pArea->SetSize ( vecSize );
148-
pSector->m_pArea->SetColor ( 255, 0, 0, 50 );
149-
}
150-
pSector->m_pArea->SetColor ( 255, 0, 0, 50 );
151-
}
152-
}
153-
*/
154-
155115
bool bMovedFar = false;
156116
// Has our position changed?
157117
if (vecPosition != m_vecPosition)
@@ -183,27 +143,62 @@ void CClientStreamer::DoPulse(CVector& vecPosition)
183143
void CClientStreamer::SetDimension(unsigned short usDimension)
184144
{
185145
// Different dimension than before?
186-
if (usDimension != m_usDimension)
146+
if (usDimension == m_usDimension)
147+
return;
148+
149+
// Set the new dimension
150+
m_usDimension = usDimension;
151+
152+
const CClientStreamElement* lastOutsideElement = m_outsideCurrentDimensionElements.empty() ? nullptr : m_outsideCurrentDimensionElements.back();
153+
154+
auto filterElementInRows = [this](list<CClientStreamSectorRow*>& list)
187155
{
188-
// Set the new dimension
189-
m_usDimension = usDimension;
190-
191-
// That means all of the currently streamed in elements will have to
192-
// go. Unstream all elements that are streamed in.
193-
CClientStreamElement* pElement = NULL;
194-
list<CClientStreamElement*>::iterator iter = m_ActiveElements.begin();
195-
for (; iter != m_ActiveElements.end(); iter++)
156+
for (CClientStreamSectorRow* sectorRow : list)
196157
{
197-
pElement = *iter;
198-
if (pElement->IsStreamedIn())
158+
for (CClientStreamSector* sector : sectorRow->GetList())
199159
{
200-
if (!pElement->IsVisibleInAllDimensions())
160+
auto& elements = sector->GetElements();
161+
auto iter = elements.begin();
162+
while (iter != sector->End())
201163
{
202-
// Unstream it
203-
m_ToStreamOut.push_back(pElement);
164+
CClientStreamElement* element = *iter;
165+
166+
if (IsElementShouldVisibleInCurrentDimesnion(element))
167+
iter++;
168+
else
169+
{
170+
iter = elements.erase(iter);
171+
m_outsideCurrentDimensionElements.push_back(element);
172+
element->SetStreamSector(nullptr);
173+
174+
if (element->IsStreamedIn())
175+
m_ToStreamOut.push_back(element);
176+
}
204177
}
205178
}
206179
}
180+
};
181+
182+
filterElementInRows(m_WorldRows);
183+
filterElementInRows(m_ExtraRows);
184+
185+
if (!lastOutsideElement)
186+
return;
187+
188+
auto iter = m_outsideCurrentDimensionElements.begin();
189+
190+
while (*iter != lastOutsideElement)
191+
{
192+
CClientStreamElement* element = *iter;
193+
if (element->GetDimension() == usDimension)
194+
{
195+
iter = m_outsideCurrentDimensionElements.erase(iter);
196+
AddElementInSectors(element);
197+
}
198+
else
199+
{
200+
iter++;
201+
}
207202
}
208203
}
209204

@@ -281,6 +276,9 @@ CClientStreamSectorRow* CClientStreamer::FindRow(float fY)
281276

282277
void CClientStreamer::OnUpdateStreamPosition(CClientStreamElement* pElement)
283278
{
279+
if (!pElement->GetStreamSector())
280+
return;
281+
284282
CVector vecPosition = pElement->GetStreamPosition();
285283
CClientStreamSectorRow* pRow = pElement->GetStreamRow();
286284
CClientStreamSector* pSector = pElement->GetStreamSector();
@@ -305,7 +303,7 @@ void CClientStreamer::OnUpdateStreamPosition(CClientStreamElement* pElement)
305303
}
306304
}
307305

308-
void CClientStreamer::AddElement(CClientStreamElement* pElement)
306+
void CClientStreamer::AddElementInSectors(CClientStreamElement* pElement)
309307
{
310308
assert(pAddingElement == NULL);
311309
pAddingElement = pElement;
@@ -316,13 +314,28 @@ void CClientStreamer::AddElement(CClientStreamElement* pElement)
316314
pAddingElement = NULL;
317315
}
318316

319-
void CClientStreamer::RemoveElement(CClientStreamElement* pElement)
317+
void CClientStreamer::RemoveElementFromSectors(CClientStreamElement* pElement)
320318
{
321-
OnElementEnterSector(pElement, NULL);
322-
m_ActiveElements.remove(pElement);
319+
OnElementEnterSector(pElement, nullptr);
323320
m_ToStreamOut.remove(pElement);
324321
}
325322

323+
void CClientStreamer::AddElement(CClientStreamElement* pElement)
324+
{
325+
if (IsElementShouldVisibleInCurrentDimesnion(pElement))
326+
AddElementInSectors(pElement);
327+
else
328+
m_outsideCurrentDimensionElements.push_back(pElement);
329+
}
330+
331+
void CClientStreamer::RemoveElement(CClientStreamElement* pElement)
332+
{
333+
if (pElement->GetStreamSector())
334+
RemoveElementFromSectors(pElement);
335+
else
336+
m_outsideCurrentDimensionElements.remove(pElement);
337+
}
338+
326339
void CClientStreamer::SetExpDistances(list<CClientStreamElement*>* pList)
327340
{
328341
// Run through our list setting distances to world center
@@ -735,16 +748,23 @@ void CClientStreamer::OnElementForceStreamOut(CClientStreamElement* pElement)
735748

736749
void CClientStreamer::OnElementDimension(CClientStreamElement* pElement)
737750
{
738-
// Grab its new dimenson
739-
unsigned short usDimension = pElement->GetDimension();
740-
// Is it streamed in?
741-
if (pElement->IsStreamedIn())
751+
if (IsElementShouldVisibleInCurrentDimesnion(pElement))
752+
{
753+
if (!pElement->GetStreamSector())
754+
{
755+
AddElementInSectors(pElement);
756+
m_outsideCurrentDimensionElements.remove(pElement);
757+
}
758+
}
759+
else
742760
{
743-
// Has it moved to a different dimension to us?
744-
if (usDimension != m_usDimension)
761+
if (pElement->GetStreamSector())
745762
{
746-
// Stream it out
747-
m_ToStreamOut.push_back(pElement);
763+
m_outsideCurrentDimensionElements.push_back(pElement);
764+
RemoveElementFromSectors(pElement);
765+
766+
if (pElement->IsStreamedIn())
767+
m_ToStreamOut.push_back(pElement);
748768
}
749769
}
750770
}

Client/mods/deathmatch/logic/CClientStreamer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ class CClientStreamer
4747
void AddElement(CClientStreamElement* pElement);
4848
void RemoveElement(CClientStreamElement* pElement);
4949

50+
void AddElementInSectors(CClientStreamElement* pElement);
51+
void RemoveElementFromSectors(CClientStreamElement* pElement);
52+
53+
bool IsElementShouldVisibleInCurrentDimesnion(CClientStreamElement* pElement) const noexcept
54+
{
55+
return pElement->GetDimension() == m_usDimension || pElement->IsVisibleInAllDimensions();
56+
}
57+
5058
void SetExpDistances(std::list<CClientStreamElement*>* pList);
5159
void AddToSortedList(std::list<CClientStreamElement*>* pList, CClientStreamElement* pElement);
5260

@@ -72,6 +80,7 @@ class CClientStreamer
7280
unsigned short m_usDimension;
7381
std::list<CClientStreamElement*> m_ActiveElements;
7482
std::list<CClientStreamElement*> m_ToStreamOut;
83+
std::list<CClientStreamElement*> m_outsideCurrentDimensionElements;
7584

7685
static void* pAddingElement;
7786
};

0 commit comments

Comments
 (0)