- made the MapSectionGenerator a class to remove a global array.

This commit is contained in:
Christoph Oelckers 2018-04-02 08:25:07 +02:00
parent 4357f0dc40
commit 202d209eb8

View file

@ -51,13 +51,35 @@
//========================================================================== //==========================================================================
// //
// // Map section generation
// Map sections are physically separated parts of the map.
// If the player is in section A, any map part in other sections can
// often be quickly discarded to improve performance.
// //
//========================================================================== //==========================================================================
static TArray<subsector_t *> MapSectionCollector;
static void DoSetMapSection(subsector_t *sub, int num) struct MapSectionGenerator
{ {
struct cvertex_t
{
double X, Y;
operator int() const { return xs_FloorToInt(X) + 65536 * xs_FloorToInt(Y); }
bool operator!= (const cvertex_t &other) const { return fabs(X - other.X) >= EQUAL_EPSILON || fabs(Y - other.Y) >= EQUAL_EPSILON; }
cvertex_t& operator =(const vertex_t *v) { X = v->fX(); Y = v->fY(); return *this; }
};
typedef TMap<cvertex_t, int> FSectionVertexMap;
TArray<subsector_t *> MapSectionCollector;
//==========================================================================
//
//
//
//==========================================================================
void DoSetMapSection(subsector_t *sub, int num)
{
MapSectionCollector.Resize(1); MapSectionCollector.Resize(1);
MapSectionCollector[0] = sub; MapSectionCollector[0] = sub;
sub->mapsection = num; sub->mapsection = num;
@ -82,30 +104,20 @@ static void DoSetMapSection(subsector_t *sub, int num)
} }
} }
MapSectionCollector.Clear(); MapSectionCollector.Clear();
} }
//========================================================================== //==========================================================================
// //
// Merge sections. This is needed in case the map contains errors // Merge sections. This is needed in case the map contains errors
// like overlapping lines resulting in abnormal subsectors. // like overlapping lines resulting in abnormal subsectors.
// //
// This function ensures that any vertex position can only be in one section. // This function ensures that any vertex position can only be in one section.
// //
//========================================================================== //==========================================================================
struct cvertex_t
{
double X, Y;
operator int() const { return xs_FloorToInt(X) + 65536 * xs_FloorToInt(Y); } int MergeMapSections(int num)
bool operator!= (const cvertex_t &other) const { return fabs(X - other.X) >= EQUAL_EPSILON || fabs(Y - other.Y) >= EQUAL_EPSILON; } {
cvertex_t& operator =(const vertex_t *v) { X = v->fX(); Y = v->fY(); return *this; }
};
typedef TMap<cvertex_t, int> FSectionVertexMap;
static int MergeMapSections(int num)
{
FSectionVertexMap vmap; FSectionVertexMap vmap;
FSectionVertexMap::Pair *pair; FSectionVertexMap::Pair *pair;
TArray<int> sectmap; TArray<int> sectmap;
@ -168,16 +180,16 @@ static int MergeMapSections(int num)
assert(sub.mapsection != -1); assert(sub.mapsection != -1);
} }
return mergecount - 1; return mergecount - 1;
} }
//========================================================================== //==========================================================================
// //
// //
// //
//========================================================================== //==========================================================================
static void SetMapSections() void SetMapSections()
{ {
bool set; bool set;
int num = 0; int num = 0;
do do
@ -197,10 +209,11 @@ static void SetMapSections()
while (set); while (set);
num = MergeMapSections(num); num = MergeMapSections(num);
currentmapsection.Resize(1 + num/8); currentmapsection.Resize(1 + num/8);
#ifdef DEBUG #ifdef DEBUG
Printf("%d map sections found\n", num); Printf("%d map sections found\n", num);
#endif #endif
} }
};
//========================================================================== //==========================================================================
// //
@ -283,7 +296,8 @@ static void PrepareSectorData()
} }
} }
} }
SetMapSections(); MapSectionGenerator msg;
msg.SetMapSections();
} }
//========================================================================== //==========================================================================