Skip to content

Commit abb374f

Browse files
committed
- list iterator method to do a lot less cycles
1 parent 55bbba7 commit abb374f

4 files changed

Lines changed: 70 additions & 68 deletions

File tree

DriverLevelTool/driver_routines/regions_d2.cpp

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ CELL_DATA* CDriver2LevelRegion::GetCellData(int num) const
314314
}
315315

316316
// cell iterator
317-
PACKED_CELL_OBJECT* CDriver2LevelRegion::StartIterator(CELL_ITERATOR_D2* iterator, int cellNumber, int cellLevel) const
317+
PACKED_CELL_OBJECT* CDriver2LevelRegion::StartIterator(CELL_ITERATOR_D2* iterator, int cellNumber) const
318318
{
319319
ushort cell_ptr = m_cellPointers[cellNumber];
320320

@@ -341,29 +341,18 @@ PACKED_CELL_OBJECT* CDriver2LevelRegion::StartIterator(CELL_ITERATOR_D2* iterato
341341
iterator->nearCell.z = (cellz - (mapInfo.cells_down / 2))* mapInfo.cell_size;
342342

343343
// get the packed cell data start and near cell
344-
CELL_DATA* cell = &m_cells[cell_ptr];
344+
CELL_DATA* celld = &m_cells[cell_ptr];
345345

346-
if (cellLevel == 0)
347-
{
348-
if (cell->num & 0x4000)
349-
return nullptr;
350-
}
351-
else
352-
{
353-
ushort num = cell->num;
354-
cell++;
355-
while (num != (cellLevel | 0x4000))
356-
{
357-
if (cell->num & 0x8000)
358-
return nullptr;
346+
iterator->listType = 0;
359347

360-
num = cell->num;
361-
cell++;
362-
}
348+
if (celld->num & 0x4000) // if we immediately got to the typed list
349+
{
350+
iterator->listType = celld->num;
351+
celld++; // get to the start
363352
}
364353

365-
PACKED_CELL_OBJECT* ppco = GetCellObject(cell->num & 0x3fff);
366-
iterator->pcd = cell;
354+
PACKED_CELL_OBJECT* ppco = GetCellObject(celld->num & 0x3fff);
355+
iterator->pcd = celld;
367356

368357
if (ppco->value == 0xffff && (ppco->pos.vy & 1))
369358
ppco = ((CDriver2LevelMap*)m_owner)->GetNextPackedCop(iterator);
@@ -542,7 +531,7 @@ int CDriver2LevelMap::MapHeight(const VECTOR_NOPAD& position) const
542531
//-------------------------------------------------------------
543532
// returns first cell object of cell
544533
//-------------------------------------------------------------
545-
PACKED_CELL_OBJECT* CDriver2LevelMap::GetFirstPackedCop(CELL_ITERATOR_D2* iterator, const XZPAIR& cell, int cellLevel) const
534+
PACKED_CELL_OBJECT* CDriver2LevelMap::GetFirstPackedCop(CELL_ITERATOR_D2* iterator, const XZPAIR& cell) const
546535
{
547536
// lookup region
548537
const int region_x = cell.x / m_mapInfo.region_size;
@@ -577,23 +566,23 @@ PACKED_CELL_OBJECT* CDriver2LevelMap::GetFirstPackedCop(CELL_ITERATOR_D2* iterat
577566
// get the packed cell data start and near cell
578567
CELL_DATA* celld = &region.m_cells[cell_ptr];
579568

580-
if (cellLevel == 0)
581-
{
582-
if (celld->num & 0x4000)
583-
return nullptr;
584-
}
585-
else
586-
{
587-
ushort num = celld->num;
588-
celld++;
589-
while (num != (cellLevel | 0x4000))
590-
{
591-
if (celld->num & 0x8000)
592-
return nullptr;
569+
/*
570+
Data looks like this:
593571
594-
num = celld->num;
595-
celld++;
596-
}
572+
45,34,773,456 - default list of cell objects
573+
0x4000 | 100 - list 1 header - type 100
574+
70,378,4557 - objects of list 1
575+
0x4000 | 14 - list 2 header - type 14
576+
8767,555,445,223 - objects of list 2
577+
0x8000 - end of cell objects
578+
*/
579+
580+
iterator->listType = 0;
581+
582+
if (celld->num & 0x4000) // if we immediately got to the typed list
583+
{
584+
iterator->listType = celld->num;
585+
celld++; // get to the start
597586
}
598587

599588
PACKED_CELL_OBJECT* ppco = region.GetCellObject(celld->num & 0x3fff);
@@ -617,16 +606,35 @@ PACKED_CELL_OBJECT* CDriver2LevelMap::GetNextPackedCop(CELL_ITERATOR_D2* iterato
617606
PACKED_CELL_OBJECT* ppco;
618607

619608
do {
620-
if (iterator->pcd->num & 0x8000)
609+
CELL_DATA* celld = iterator->pcd;
610+
611+
if (celld->num & 0x8000) // end of the cell objects?
621612
return nullptr;
622613

623-
iterator->pcd++;
624-
num = iterator->pcd->num;
614+
celld++;
625615

626-
if(num & 0x4000) // start of new list
627-
return nullptr;
616+
if (celld->num & 0x4000) // if we got new list
617+
{
618+
iterator->listType = celld->num;
619+
celld++; // get to the start
620+
}
621+
622+
iterator->pcd = celld;
623+
624+
/*
625+
celld++;
626+
num = celld->num;
628627
629-
ppco = iterator->region->GetCellObject(num & 0x3fff);
628+
if (celld->num & 0x4000) // if we immediately got to the typed list
629+
{
630+
iterator->listType = celld->num;
631+
celld++; // get to the start
632+
}
633+
634+
if(num & 0x4000) // start of new sub list?
635+
return nullptr;
636+
*/
637+
ppco = iterator->region->GetCellObject(celld->num & 0x3fff);
630638

631639
} while (ppco->value == 0xffff && (ppco->pos.vy & 1));
632640

DriverLevelTool/driver_routines/regions_d2.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ struct CELL_ITERATOR_D2
1616
CELL_DATA* pcd;
1717
PACKED_CELL_OBJECT* ppco;
1818
XZPAIR nearCell;
19+
int listType;
1920
};
2021

2122
typedef short* (*sdBspCallback)(sdNode* node, XZPAIR* pos);
@@ -35,7 +36,7 @@ class CDriver2LevelRegion : public CBaseLevelRegion
3536
CELL_DATA* GetCellData(int num) const;
3637

3738
// cell iterator
38-
PACKED_CELL_OBJECT* StartIterator(CELL_ITERATOR_D2* iterator, int cellNumber, int cellLevel) const;
39+
PACKED_CELL_OBJECT* StartIterator(CELL_ITERATOR_D2* iterator, int cellNumber) const;
3940

4041
sdPlane* SdGetCell(const VECTOR_NOPAD& position, int& sdLevel, sdBspCallback bspWalker) const;
4142

@@ -82,7 +83,7 @@ class CDriver2LevelMap : public CBaseLevelMap
8283

8384
//----------------------------------------
8485
// cell iterator
85-
PACKED_CELL_OBJECT* GetFirstPackedCop(CELL_ITERATOR_D2* iterator, const XZPAIR& cell, int cellLevel) const;
86+
PACKED_CELL_OBJECT* GetFirstPackedCop(CELL_ITERATOR_D2* iterator, const XZPAIR& cell) const;
8687
PACKED_CELL_OBJECT* GetNextPackedCop(CELL_ITERATOR_D2* iterator) const;
8788
static bool UnpackCellObject(CELL_OBJECT& co, PACKED_CELL_OBJECT* pco, const XZPAIR& nearCell);
8889

DriverLevelTool/exporter/export_regions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ int ExportRegionDriver2(CDriver2LevelRegion* region, IVirtualStream* levelFileSt
137137
else
138138
cellLevel = i;
139139

140-
PACKED_CELL_OBJECT* pco = region->StartIterator(&ci, i, cellLevel);
140+
PACKED_CELL_OBJECT* pco = region->StartIterator(&ci, i);
141141

142142
if (!pco)
143143
continue;

DriverLevelTool/viewer/renderlevel.cpp

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -155,33 +155,26 @@ void DrawLevelDriver2(const Vector3D& cameraPos, float cameraAngleY, const Volum
155155

156156
levMapDriver2->SpoolRegion(spoolContext, icell);
157157

158-
for (int i = 0; i < (g_displayAllCellLevels ? 20 : 1); i++)
158+
ppco = levMapDriver2->GetFirstPackedCop(&ci, icell);
159+
160+
// walk each cell object in cell
161+
while (ppco)
159162
{
160-
int cellLevel;
163+
if (ci.listType != 0 && !g_displayAllCellLevels)
164+
break;
161165

162-
if (i == 19)
163-
cellLevel = 100; // 100 is the special slot for event object placement
164-
else
165-
cellLevel = i;
166-
167-
ppco = levMapDriver2->GetFirstPackedCop(&ci, icell, cellLevel);
166+
PCO_PAIR_D2 pair;
167+
pair.nearCell = ci.nearCell;
168+
pair.pco = ppco;
169+
pair.editorEvent = (ci.listType) == (100 | 0x4000);
168170

169-
// walk each cell object in cell
170-
while (ppco)
171-
{
172-
PCO_PAIR_D2 pair;
173-
pair.nearCell = ci.nearCell;
174-
pair.pco = ppco;
175-
pair.editorEvent = cellLevel == 100;
171+
drawObjects.append(pair);
176172

177-
drawObjects.append(pair);
178-
179-
ppco = levMapDriver2->GetNextPackedCop(&ci);
180-
}
181-
182-
if(ppco)
183-
g_drawnCells++;
173+
ppco = levMapDriver2->GetNextPackedCop(&ci);
184174
}
175+
176+
if(ppco)
177+
g_drawnCells++;
185178
}
186179
}
187180

0 commit comments

Comments
 (0)