Skip to content

Commit 8a007bc

Browse files
committed
- rewrote D1 and D2 region exporting
1 parent 04d8b3f commit 8a007bc

File tree

16 files changed

+387
-431
lines changed

16 files changed

+387
-431
lines changed

DriverLevelTool/driver_routines/level.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,15 @@ void ProcessLumps(IVirtualStream* pFile)
222222
}
223223
}
224224

225-
void LoadLevelFile(const char* filename)
225+
bool LoadLevelFile(const char* filename)
226226
{
227227
// try load driver2 lev file
228228
FILE* pReadFile = fopen(filename, "rb");
229229

230230
if (!pReadFile)
231231
{
232232
MsgError("Failed to open LEV file!\n");
233-
return;
233+
return false;
234234
}
235235

236236
CMemoryStream* stream = new CMemoryStream();
@@ -245,7 +245,7 @@ void LoadLevelFile(const char* filename)
245245
else
246246
{
247247
delete stream;
248-
return;
248+
return false;
249249
}
250250

251251
// seek to begin
@@ -267,7 +267,7 @@ void LoadLevelFile(const char* filename)
267267
if (curLump.type != LUMP_LUMPDESC)
268268
{
269269
MsgError("Not a LEV file!\n");
270-
return;
270+
return false;
271271
}
272272

273273
// read chunk offsets
@@ -325,6 +325,8 @@ void LoadLevelFile(const char* filename)
325325

326326
// read sublumps
327327
ProcessLumps(g_levStream);
328+
329+
return true;
328330
}
329331

330332
void FreeLevelData()

DriverLevelTool/driver_routines/level.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ extern ELevelFormat g_format;
6969
//------------------------------------------------------------------------------------------------------------
7070
// functions
7171

72-
void LoadLevelFile(const char* filename);
72+
bool LoadLevelFile(const char* filename);
7373
void FreeLevelData();
7474

7575
#endif // LEVEL_H

DriverLevelTool/driver_routines/regions.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ void CBaseLevelRegion::FreeAll()
5555
m_loaded = false;
5656
}
5757

58+
bool CBaseLevelRegion::IsEmpty() const
59+
{
60+
return m_spoolInfo == nullptr;
61+
}
62+
63+
int CBaseLevelRegion::GetNumber() const
64+
{
65+
return m_regionNumber;
66+
}
67+
5868
//-------------------------------------------------------------
5969
// Region unpacking function
6070
//-------------------------------------------------------------
@@ -186,6 +196,11 @@ int CBaseLevelMap::GetAreaDataCount() const
186196
return m_numAreas;
187197
}
188198

199+
const OUT_CELL_FILE_HEADER& CBaseLevelMap::GetMapInfo() const
200+
{
201+
return m_mapInfo;
202+
}
203+
189204
int CBaseLevelMap::GetCellsAcross() const
190205
{
191206
return m_mapInfo.cells_across;
@@ -196,6 +211,16 @@ int CBaseLevelMap::GetCellsDown() const
196211
return m_mapInfo.cells_down;
197212
}
198213

214+
int CBaseLevelMap::GetRegionsAcross() const
215+
{
216+
return m_regions_across;
217+
}
218+
219+
int CBaseLevelMap::GetRegionsDown() const
220+
{
221+
return m_regions_down;
222+
}
223+
199224
void CBaseLevelMap::WorldPositionToCellXZ(XZPAIR& cell, const VECTOR_NOPAD& position) const
200225
{
201226
// @TODO: constants
@@ -404,6 +429,23 @@ void CBaseLevelMap::LoadInAreaModels(IVirtualStream* pFile, int areaDataNum) con
404429
delete[] new_model_numbers;
405430
}
406431

432+
void CBaseLevelMap::InitRegion(CBaseLevelRegion* region, int index) const
433+
{
434+
ushort spoolOffset = m_regionSpoolInfoOffsets[index];
435+
436+
if (spoolOffset != REGION_EMPTY)
437+
region->m_spoolInfo = (Spool*)((ubyte*)m_regionSpoolInfo + spoolOffset);
438+
439+
const int region_x = index % m_regions_across;
440+
const int region_z = (index - region_x) / m_regions_across;
441+
442+
region->m_owner = (CBaseLevelMap*)this;
443+
region->m_regionX = region_x;
444+
region->m_regionZ = region_z;
445+
region->m_regionNumber = index;
446+
region->m_regionBarrelNumber = (region_x & 1) + (region_z & 1) * 2;
447+
}
448+
407449
void CBaseLevelMap::SetLoadingCallbacks(OnRegionLoaded_t onLoaded, OnRegionFreed_t onFreed)
408450
{
409451
m_onRegionLoaded = onLoaded;

DriverLevelTool/driver_routines/regions.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ class CBaseLevelRegion
3333
virtual ~CBaseLevelRegion();
3434

3535
virtual void FreeAll();
36-
virtual void LoadRegionData(IVirtualStream* pFile, Spool* spool) = 0;
36+
virtual void LoadRegionData(IVirtualStream* pFile) = 0;
3737
void LoadAreaData(IVirtualStream* pFile);
3838

39+
bool IsEmpty() const;
40+
int GetNumber() const;
41+
3942
protected:
4043
static int UnpackCellPointers(ushort* dest_ptrs, char* src_data, int cell_slots_add, int targetRegion = 0);
4144

@@ -90,11 +93,18 @@ class CBaseLevelMap
9093
// converters
9194
void WorldPositionToCellXZ(XZPAIR& cell, const VECTOR_NOPAD& position) const;
9295

96+
const OUT_CELL_FILE_HEADER& GetMapInfo() const;
97+
9398
int GetCellsAcross() const;
9499
int GetCellsDown() const;
95100

101+
int GetRegionsAcross() const;
102+
int GetRegionsDown() const;
103+
96104
protected:
97105

106+
void InitRegion(CBaseLevelRegion* region, int index) const;
107+
98108
void OnRegionLoaded(CBaseLevelRegion* region);
99109
void OnRegionFreed(CBaseLevelRegion* region);
100110

DriverLevelTool/driver_routines/regions_d1.cpp

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,58 +21,56 @@ void CDriver1LevelRegion::FreeAll()
2121
m_cellObjects = nullptr;
2222
}
2323

24-
void CDriver1LevelRegion::LoadRegionData(IVirtualStream* pFile, Spool* spool)
24+
void CDriver1LevelRegion::LoadRegionData(IVirtualStream* pFile)
2525
{
26-
m_spoolInfo = spool;
27-
2826
DevMsg(SPEW_NORM, "---------\nSpool %d %d\n", m_regionX, m_regionZ);
29-
DevMsg(SPEW_NORM, " - offset: %d\n", spool->offset);
27+
DevMsg(SPEW_NORM, " - offset: %d\n", m_spoolInfo->offset);
3028

31-
for (int i = 0; i < spool->num_connected_areas && i < 2; i++)
32-
DevMsg(SPEW_NORM, " - connected area %d: %d\n", i, spool->connected_areas[i]);
29+
for (int i = 0; i < m_spoolInfo->num_connected_areas && i < 2; i++)
30+
DevMsg(SPEW_NORM, " - connected area %d: %d\n", i, m_spoolInfo->connected_areas[i]);
3331

34-
DevMsg(SPEW_NORM, " - pvs_size: %d\n", spool->pvs_size);
35-
DevMsg(SPEW_NORM, " - cell_data_size: %d %d %d\n", spool->cell_data_size[0], spool->cell_data_size[1], spool->cell_data_size[2]);
32+
DevMsg(SPEW_NORM, " - pvs_size: %d\n", m_spoolInfo->pvs_size);
33+
DevMsg(SPEW_NORM, " - cell_data_size: %d %d %d\n", m_spoolInfo->cell_data_size[0], m_spoolInfo->cell_data_size[1], m_spoolInfo->cell_data_size[2]);
3634

37-
DevMsg(SPEW_NORM, " - super_region: %d\n", spool->super_region);
35+
DevMsg(SPEW_NORM, " - super_region: %d\n", m_spoolInfo->super_region);
3836

3937
// LoadRegionData - calculate offsets
40-
DevMsg(SPEW_NORM, " - cell pointers size: %d\n", spool->cell_data_size[1]);
41-
DevMsg(SPEW_NORM, " - cell data size: %d\n", spool->cell_data_size[0]);
42-
DevMsg(SPEW_NORM, " - cell objects size: %d\n", spool->cell_data_size[2]);
43-
DevMsg(SPEW_NORM, " - PVS data size: %d\n", spool->pvs_size);
44-
DevMsg(SPEW_NORM, " - roadmap data size: %dx%d\n", spool->roadm_size, spool->roadh_size);
38+
DevMsg(SPEW_NORM, " - cell pointers size: %d\n", m_spoolInfo->cell_data_size[1]);
39+
DevMsg(SPEW_NORM, " - cell data size: %d\n", m_spoolInfo->cell_data_size[0]);
40+
DevMsg(SPEW_NORM, " - cell objects size: %d\n", m_spoolInfo->cell_data_size[2]);
41+
DevMsg(SPEW_NORM, " - PVS data size: %d\n", m_spoolInfo->pvs_size);
42+
DevMsg(SPEW_NORM, " - roadmap data size: %dx%d\n", m_spoolInfo->roadm_size, m_spoolInfo->roadh_size);
4543

4644
//
4745
// Driver 1 use CELL_OBJECTS directly - 16 bytes, wasteful in RAM
4846
//
4947

50-
int cellPointersOffset = spool->offset + spool->roadm_size + spool->roadh_size; // SKIP road map
51-
int cellDataOffset = cellPointersOffset + spool->cell_data_size[1];
52-
int cellObjectsOffset = cellDataOffset + spool->cell_data_size[0];
53-
int pvsDataOffset = cellObjectsOffset + spool->cell_data_size[2]; // FIXME: is it even there in Driver 1?
48+
int cellPointersOffset = m_spoolInfo->offset + m_spoolInfo->roadm_size + m_spoolInfo->roadh_size; // SKIP road map
49+
int cellDataOffset = cellPointersOffset + m_spoolInfo->cell_data_size[1];
50+
int cellObjectsOffset = cellDataOffset + m_spoolInfo->cell_data_size[0];
51+
int pvsDataOffset = cellObjectsOffset + m_spoolInfo->cell_data_size[2]; // FIXME: is it even there in Driver 1?
5452

55-
char* packed_cell_pointers = new char[spool->cell_data_size[1] * SPOOL_CD_BLOCK_SIZE];
53+
char* packed_cell_pointers = new char[m_spoolInfo->cell_data_size[1] * SPOOL_CD_BLOCK_SIZE];
5654

5755
m_cellPointers = new ushort[m_owner->m_cell_objects_add[5]];
5856
memset(m_cellPointers, 0xFF, sizeof(ushort) * m_owner->m_cell_objects_add[5]);
5957

6058
// read packed cell pointers
6159
pFile->Seek(g_levInfo.spooldata_offset + cellPointersOffset * SPOOL_CD_BLOCK_SIZE, VS_SEEK_SET);
62-
pFile->Read(packed_cell_pointers, spool->cell_data_size[1] * SPOOL_CD_BLOCK_SIZE, sizeof(char));
60+
pFile->Read(packed_cell_pointers, m_spoolInfo->cell_data_size[1] * SPOOL_CD_BLOCK_SIZE, sizeof(char));
6361

6462
// unpack cell pointers so we can use them
6563
if (UnpackCellPointers(m_cellPointers, packed_cell_pointers, 0, 0) != -1)
6664
{
6765
// read cell data
68-
m_cells = (CELL_DATA_D1*)malloc(spool->cell_data_size[0] * SPOOL_CD_BLOCK_SIZE);
66+
m_cells = (CELL_DATA_D1*)malloc(m_spoolInfo->cell_data_size[0] * SPOOL_CD_BLOCK_SIZE);
6967
pFile->Seek(g_levInfo.spooldata_offset + cellDataOffset * SPOOL_CD_BLOCK_SIZE, VS_SEEK_SET);
70-
pFile->Read(m_cells, spool->cell_data_size[0] * SPOOL_CD_BLOCK_SIZE, sizeof(char));
68+
pFile->Read(m_cells, m_spoolInfo->cell_data_size[0] * SPOOL_CD_BLOCK_SIZE, sizeof(char));
7169

7270
// read cell objects
73-
m_cellObjects = (CELL_OBJECT*)malloc(spool->cell_data_size[2] * SPOOL_CD_BLOCK_SIZE * 2);
71+
m_cellObjects = (CELL_OBJECT*)malloc(m_spoolInfo->cell_data_size[2] * SPOOL_CD_BLOCK_SIZE * 2);
7472
pFile->Seek(g_levInfo.spooldata_offset + cellObjectsOffset * SPOOL_CD_BLOCK_SIZE, VS_SEEK_SET);
75-
pFile->Read(m_cellObjects, spool->cell_data_size[2] * SPOOL_CD_BLOCK_SIZE, sizeof(char));
73+
pFile->Read(m_cellObjects, m_spoolInfo->cell_data_size[2] * SPOOL_CD_BLOCK_SIZE, sizeof(char));
7674
}
7775
else
7876
MsgError("BAD PACKED CELL POINTER DATA, region = %d\n", m_regionNumber);
@@ -102,6 +100,25 @@ CELL_OBJECT* CDriver1LevelRegion::GetCellObject(int num) const
102100
return &owner->m_straddlers[num];
103101
}
104102

103+
//----------------------------------------
104+
// cell iterator
105+
CELL_OBJECT* CDriver1LevelRegion::StartIterator(CELL_ITERATOR_D1* iterator, int cellNumber) const
106+
{
107+
ushort cell_ptr = m_cellPointers[cellNumber];
108+
109+
if (cell_ptr == 0xFFFF)
110+
return nullptr;
111+
112+
// get the packed cell data start and near cell
113+
CELL_DATA_D1& cell = m_cells[cell_ptr];
114+
CELL_OBJECT* pco = GetCellObject(cell.num & 0x3fff);
115+
116+
iterator->region = (CDriver1LevelRegion*)this;
117+
iterator->pcd = &cell;
118+
iterator->pco = pco;
119+
120+
return pco;
121+
}
105122

106123
//-------------------------------------------------------------------------------------------
107124

@@ -156,16 +173,7 @@ void CDriver1LevelMap::LoadSpoolInfoLump(IVirtualStream* pFile)
156173
m_regions = new CDriver1LevelRegion[total_regions];
157174

158175
for (int i = 0; i < total_regions; i++)
159-
{
160-
const int region_x = i % m_regions_across;
161-
const int region_z = (i - region_x) / m_regions_across;
162-
163-
m_regions[i].m_owner = this;
164-
m_regions[i].m_regionX = region_x;
165-
m_regions[i].m_regionZ = region_z;
166-
m_regions[i].m_regionNumber = i;
167-
m_regions[i].m_regionBarrelNumber = (region_x & 1) + (region_z & 1) * 2;
168-
}
176+
InitRegion(&m_regions[i], i);
169177

170178
// seek back
171179
pFile->Seek(l_ofs, VS_SEEK_SET);
@@ -193,8 +201,7 @@ void CDriver1LevelMap::SpoolRegion(const XZPAIR& cell)
193201
{
194202
if (m_regionSpoolInfoOffsets[region->m_regionNumber] != REGION_EMPTY)
195203
{
196-
Spool* spool = (Spool*)((ubyte*)m_regionSpoolInfo + m_regionSpoolInfoOffsets[region->m_regionNumber]);
197-
region->LoadRegionData(g_levStream, spool);
204+
region->LoadRegionData(g_levStream);
198205
region->LoadAreaData(g_levStream);
199206
}
200207
else
@@ -210,8 +217,7 @@ void CDriver1LevelMap::SpoolRegion(int regionIdx)
210217
{
211218
if (m_regionSpoolInfoOffsets[region->m_regionNumber] != REGION_EMPTY)
212219
{
213-
Spool* spool = (Spool*)((ubyte*)m_regionSpoolInfo + m_regionSpoolInfoOffsets[region->m_regionNumber]);
214-
region->LoadRegionData(g_levStream, spool);
220+
region->LoadRegionData(g_levStream);
215221
region->LoadAreaData(g_levStream);
216222
}
217223
else

DriverLevelTool/driver_routines/regions_d1.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ class CDriver1LevelRegion : public CBaseLevelRegion
2020
friend class CDriver1LevelMap;
2121
public:
2222
void FreeAll() override;
23-
void LoadRegionData(IVirtualStream* pFile, Spool* spool) override;
23+
void LoadRegionData(IVirtualStream* pFile) override;
2424

2525
CELL_OBJECT* GetCellObject(int num) const;
2626

27+
// cell iterator
28+
CELL_OBJECT* StartIterator(CELL_ITERATOR_D1* iterator, int cellNumber) const;
29+
2730
protected:
2831
CELL_DATA_D1* m_cells{ nullptr }; // cell data that holding information about cell pointers. 3D world seeks cells first here
2932
CELL_OBJECT* m_cellObjects{ nullptr }; // cell objects that represents objects placed in the world

0 commit comments

Comments
 (0)