OpenClonk
C4LSectors Class Reference

#include <C4Sector.h>

Collaboration diagram for C4LSectors:
[legend]

Public Member Functions

void Init (int Wdt, int Hgt)
 
void Clear ()
 
C4LSector * SectorAt (int ix, int iy)
 
void Add (C4Object *pObj, C4ObjectList *pMainList)
 
void Update (C4Object *pObj, C4ObjectList *pMainList)
 
void Remove (C4Object *pObj)
 
void ClearObjects ()
 
void AssertObjectNotInList (C4Object *pObj)
 
int getShapeSum () const
 
void Dump ()
 
bool CheckSort ()
 

Public Attributes

C4LSector * Sectors
 
int PxWdt
 
int PxHgt
 
int Wdt
 
int Hgt
 
int Size
 
C4LSector SectorOut
 

Detailed Description

Definition at line 58 of file C4Sector.h.

Member Function Documentation

◆ Add()

void C4LSectors::Add ( C4Object *  pObj,
C4ObjectList *  pMainList 
)

Definition at line 91 of file C4Sector.cpp.

92 {
93  assert(Sectors);
94  // Add to owning sector
95  C4LSector *pSct = SectorAt(pObj->GetX(), pObj->GetY());
96  pSct->Objects.Add(pObj, C4ObjectList::stMain, pMainList);
97  // Save position
98  pObj->old_x = pObj->GetX(); pObj->old_y = pObj->GetY();
99  // Add to all sectors in shape area
100  pObj->Area.Set(this, pObj);
101  for (pSct = pObj->Area.First(); pSct; pSct = pObj->Area.Next(pSct))
102  {
103  pSct->ObjectShapes.Add(pObj, C4ObjectList::stMain, pMainList);
104  }
105  if (Config.General.DebugRec)
106  pObj->Area.DebugRec(pObj, 'A');
107 }
C4Config Config
Definition: C4Config.cpp:930
int32_t DebugRec
Definition: C4Config.h:63
C4ConfigGeneral General
Definition: C4Config.h:255
void DebugRec(class C4Object *pObj, char cMarker)
Definition: C4Sector.cpp:331
C4LSector * First() const
Definition: C4Sector.h:111
void Set(C4LSectors *pSectors, const C4Rect &rect)
Definition: C4Sector.cpp:244
C4LSector * Next(C4LSector *pPrev) const
Definition: C4Sector.cpp:276
C4ObjectList ObjectShapes
Definition: C4Sector.h:49
C4ObjectList Objects
Definition: C4Sector.h:48
C4LSector * Sectors
Definition: C4Sector.h:61
C4LSector * SectorAt(int ix, int iy)
Definition: C4Sector.cpp:82
int32_t GetX() const
Definition: C4Object.h:285
int32_t GetY() const
Definition: C4Object.h:286
int32_t old_y
Definition: C4Object.h:112
int32_t old_x
Definition: C4Object.h:112
C4LArea Area
Definition: C4Object.h:112
virtual bool Add(C4Object *new_obj, SortType sort_type, C4ObjectList *sorted_list=nullptr)

References C4ObjectList::Add(), C4Object::Area, Config, C4ConfigGeneral::DebugRec, C4LArea::DebugRec(), C4LArea::First(), C4Config::General, C4Object::GetX(), C4Object::GetY(), C4LArea::Next(), C4LSector::Objects, C4LSector::ObjectShapes, C4Object::old_x, C4Object::old_y, SectorAt(), Sectors, C4LArea::Set(), and C4ObjectList::stMain.

Referenced by C4GameObjects::Add(), Update(), and C4GameObjects::UpdatePosResort().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AssertObjectNotInList()

void C4LSectors::AssertObjectNotInList ( C4Object *  pObj)

Definition at line 185 of file C4Sector.cpp.

186 {
187 #ifndef NDEBUG
188  C4LSector *sct=Sectors;
189  for (int cnt=0; cnt<Size; cnt++, sct++)
190  {
191  assert(!sct->Objects.IsContained(pObj));
192  assert(!sct->ObjectShapes.IsContained(pObj));
193  }
194  assert(!SectorOut.Objects.IsContained(pObj));
195  assert(!SectorOut.ObjectShapes.IsContained(pObj));
196 #endif
197 }
C4LSector SectorOut
Definition: C4Sector.h:65
int Size
Definition: C4Sector.h:63
bool IsContained(const C4Object *obj) const

References C4ObjectList::IsContained(), C4LSector::Objects, C4LSector::ObjectShapes, SectorOut, Sectors, and Size.

Referenced by C4Object::~C4Object().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CheckSort()

bool C4LSectors::CheckSort ( )

Definition at line 216 of file C4Sector.cpp.

217 {
218  for (int cnt=0; cnt<Size; cnt++)
219  if (!Sectors[cnt].Objects.CheckSort(&::Objects))
220  return false;
221  if (!SectorOut.Objects.CheckSort(&::Objects)) return false;
222  return true;
223 }
C4GameObjects Objects
Definition: C4Globals.cpp:48
bool CheckSort(C4ObjectList *list)

References C4ObjectList::CheckSort(), Objects, C4LSector::Objects, SectorOut, Sectors, and Size.

Here is the call graph for this function:

◆ Clear()

void C4LSectors::Clear ( )

Definition at line 74 of file C4Sector.cpp.

75 {
76  // clear out-sector
77  SectorOut.Clear();
78  // free sectors
79  delete [] Sectors; Sectors=nullptr;
80 }
void Clear()
Definition: C4Sector.cpp:35

References C4LSector::Clear(), SectorOut, and Sectors.

Referenced by C4GameObjects::Default(), Init(), and C4GameObjects::~C4GameObjects().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ClearObjects()

void C4LSectors::ClearObjects ( )

Definition at line 225 of file C4Sector.cpp.

226 {
227  if (Sectors)
228  {
229  for (int cnt=0; cnt<Size; cnt++) Sectors[cnt].ClearObjects();
230  }
232 }
void ClearObjects()
Definition: C4Sector.cpp:49
void ClearObjects()
Definition: C4Sector.cpp:225

References C4LSector::ClearObjects(), SectorOut, Sectors, and Size.

Referenced by C4GameObjects::DeleteObjects().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Dump()

void C4LSectors::Dump ( )

Definition at line 207 of file C4Sector.cpp.

208 {
209  C4ValueNumbers numbers;
210  LogSilent(DecompileToBuf<StdCompilerINIWrite>(
213  "Sector")).getData());
214 }
bool LogSilent(const char *szMessage, bool fConsole)
Definition: C4Log.cpp:126
StdArrayAdapt< T, M > mkArrayAdaptMap(T *pArray, int iSize, M &&map)
Definition: StdAdaptors.h:340
StdParameterAdaptMaker< P > mkParAdaptMaker(P &&rPar)
Definition: StdAdaptors.h:503
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:92

References LogSilent(), mkArrayAdaptMap(), mkNamingAdapt(), mkParAdaptMaker(), Sectors, and Size.

Here is the call graph for this function:

◆ getShapeSum()

int C4LSectors::getShapeSum ( ) const

Definition at line 199 of file C4Sector.cpp.

200 {
201  int iSum = 0;
202  for (int cnt=0; cnt<Size; cnt++)
203  iSum += Sectors[cnt].ObjectShapes.ObjectCount();
204  return iSum;
205 }

References Sectors, and Size.

Referenced by C4ControlSyncCheck::Set().

Here is the caller graph for this function:

◆ Init()

void C4LSectors::Init ( int  Wdt,
int  Hgt 
)

Definition at line 58 of file C4Sector.cpp.

59 {
60  // clear any previous initialization
61  Clear();
62  // store class members, calc size
63  Wdt = ((PxWdt=iWdt)-1)/C4LSectorWdt+1;
64  Hgt = ((PxHgt=iHgt)-1)/C4LSectorHgt+1;
65  // create sectors
66  Sectors = new C4LSector[Size=Wdt*Hgt];
67  // init sectors
68  C4LSector *sct=Sectors;
69  for (int cnt=0; cnt<Size; cnt++, sct++)
70  sct->Init(cnt%Wdt, cnt/Wdt);
71  SectorOut.Init(-1,-1); // outpos at -1,-1 - MUST NOT intersect with an inside sector!
72 }
const int32_t C4LSectorHgt
Definition: C4Sector.h:32
const int32_t C4LSectorWdt
Definition: C4Sector.h:31
void Init(int ix, int iy)
Definition: C4Sector.cpp:27
int Wdt
Definition: C4Sector.h:63
void Clear()
Definition: C4Sector.cpp:74
int PxHgt
Definition: C4Sector.h:62
int PxWdt
Definition: C4Sector.h:62
int Hgt
Definition: C4Sector.h:63

References C4LSectorHgt, C4LSectorWdt, Clear(), Hgt, C4LSector::Init(), PxHgt, PxWdt, SectorOut, Sectors, Size, and Wdt.

Referenced by C4GameObjects::Init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Remove()

void C4LSectors::Remove ( C4Object *  pObj)

Definition at line 151 of file C4Sector.cpp.

152 {
153  assert(Sectors); assert(pObj);
154  // Remove from owning sector
155  C4LSector *pSct = SectorAt(pObj->old_x, pObj->old_y);
156  if (!pSct->Objects.Remove(pObj))
157  {
158 #ifdef _DEBUG
159  LogF("WARNING: Object %d of type %s deleted but not found in pos sector list!", pObj->Number, pObj->id.ToString());
160 #endif
161  // if it was not found in owning sector, it must be somewhere else. yeah...
162  bool fFound = false;
163  for (pSct = pObj->Area.First(); pSct; pSct = pObj->Area.Next(pSct))
164  if (pSct->Objects.Remove(pObj)) { fFound=true; break; }
165  // yukh, somewhere else entirely...
166  if (!fFound)
167  {
168  fFound = !!SectorOut.Objects.Remove(pObj);
169  if (!fFound)
170  {
171  pSct = Sectors;
172  for (int cnt=0; cnt<Size; cnt++, pSct++)
173  if (pSct->Objects.Remove(pObj)) { fFound=true; break; }
174  }
175  assert(fFound);
176  }
177  }
178  // Remove from all sectors in shape area
179  for (pSct = pObj->Area.First(); pSct; pSct = pObj->Area.Next(pSct))
180  pSct->ObjectShapes.Remove(pObj);
181  if (Config.General.DebugRec)
182  pObj->Area.DebugRec(pObj, 'R');
183 }
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:262
const char * ToString() const
Definition: C4Id.h:56
C4ID id
Definition: C4Object.h:106
virtual bool Remove(C4Object *obj)

References C4Object::Area, Config, C4ConfigGeneral::DebugRec, C4LArea::DebugRec(), C4LArea::First(), C4Config::General, C4Object::id, LogF(), C4LArea::Next(), C4PropListNumbered::Number, C4LSector::Objects, C4LSector::ObjectShapes, C4Object::old_x, C4Object::old_y, C4ObjectList::Remove(), SectorAt(), SectorOut, Sectors, Size, and C4ID::ToString().

Referenced by C4GameObjects::Remove(), and C4GameObjects::UpdatePosResort().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SectorAt()

C4LSector * C4LSectors::SectorAt ( int  ix,
int  iy 
)

Definition at line 82 of file C4Sector.cpp.

83 {
84  // check bounds
85  if (ix<0 || iy<0 || ix>=PxWdt || iy>=PxHgt)
86  return &SectorOut;
87  // get sector
88  return Sectors+(iy/C4LSectorHgt)*Wdt+(ix/C4LSectorWdt);
89 }

References C4LSectorHgt, C4LSectorWdt, PxHgt, PxWdt, SectorOut, Sectors, and Wdt.

Referenced by Add(), Remove(), C4LArea::Set(), and Update().

Here is the caller graph for this function:

◆ Update()

void C4LSectors::Update ( C4Object *  pObj,
C4ObjectList *  pMainList 
)

Definition at line 109 of file C4Sector.cpp.

110 {
111  assert(Sectors);
112  // Not added yet?
113  if (pObj->Area.IsNull())
114  {
115  Add(pObj, pMainList);
116  return;
117  }
118  C4LSector *pOld, *pNew;
119  if (pObj->old_x != pObj->GetX() || pObj->old_y != pObj->GetY())
120  {
121  // Get involved sectors
122  pOld = SectorAt(pObj->old_x, pObj->old_y);
123  pNew = SectorAt(pObj->GetX(), pObj->GetY());
124  if (pOld != pNew)
125  {
126  pOld->Objects.Remove(pObj);
127  pNew->Objects.Add(pObj, C4ObjectList::stMain, pMainList);
128  }
129  // Save position
130  pObj->old_x = pObj->GetX(); pObj->old_y = pObj->GetY();
131  }
132  // New area
133  C4LArea NewArea(this, pObj);
134  if (pObj->Area == NewArea) return;
135  // Remove from all old sectors in shape area
136  for (pOld = pObj->Area.First(); pOld; pOld = pObj->Area.Next(pOld))
137  if (!NewArea.Contains(pOld))
138  pOld->ObjectShapes.Remove(pObj);
139  // Add to all new sectors in shape area
140  for (pNew = NewArea.First(); pNew; pNew = NewArea.Next(pNew))
141  if (!pObj->Area.Contains(pNew))
142  {
143  pNew->ObjectShapes.Add(pObj, C4ObjectList::stMain, pMainList);
144  }
145  // Update area
146  pObj->Area = NewArea;
147  if (Config.General.DebugRec)
148  pObj->Area.DebugRec(pObj, 'U');
149 }
bool Contains(C4LSector *pSct) const
Definition: C4Sector.cpp:291
bool IsNull() const
Definition: C4Sector.h:106
void Add(C4Object *pObj, C4ObjectList *pMainList)
Definition: C4Sector.cpp:91

References C4ObjectList::Add(), Add(), C4Object::Area, Config, C4LArea::Contains(), C4ConfigGeneral::DebugRec, C4LArea::DebugRec(), C4LArea::First(), C4Config::General, C4Object::GetX(), C4Object::GetY(), C4LArea::IsNull(), C4LArea::Next(), C4LSector::Objects, C4LSector::ObjectShapes, C4Object::old_x, C4Object::old_y, C4ObjectList::Remove(), SectorAt(), Sectors, and C4ObjectList::stMain.

Referenced by C4GameObjects::UpdatePos().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ Hgt

int C4LSectors::Hgt

Definition at line 63 of file C4Sector.h.

Referenced by Init().

◆ PxHgt

int C4LSectors::PxHgt

Definition at line 62 of file C4Sector.h.

Referenced by Init(), SectorAt(), and C4LArea::Set().

◆ PxWdt

int C4LSectors::PxWdt

Definition at line 62 of file C4Sector.h.

Referenced by Init(), SectorAt(), and C4LArea::Set().

◆ SectorOut

C4LSector C4LSectors::SectorOut

◆ Sectors

C4LSector* C4LSectors::Sectors

◆ Size

int C4LSectors::Size

Definition at line 63 of file C4Sector.h.

Referenced by AssertObjectNotInList(), CheckSort(), ClearObjects(), Dump(), getShapeSum(), Init(), and Remove().

◆ Wdt

int C4LSectors::Wdt

Definition at line 63 of file C4Sector.h.

Referenced by Init(), SectorAt(), and C4LArea::Set().


The documentation for this class was generated from the following files: