Skip to content

Commit f4244a5

Browse files
committed
- sync driver_routines from OpenDriverEngine
1 parent 72db110 commit f4244a5

11 files changed

Lines changed: 61 additions & 55 deletions

File tree

DriverLevelTool/driver_level.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ void ExportLevelFile()
105105
CFileStream stream(levTest);
106106
ELevelFormat levFormat = CDriverLevelLoader::DetectLevelFormat(&stream);
107107

108-
fclose(levTest);
109-
110108
CDriverLevelLoader levLoader;
111109

112110
// create map accordingly
@@ -117,11 +115,13 @@ void ExportLevelFile()
117115

118116
levLoader.Initialize(g_levInfo, &g_levTextures, &g_levModels, g_levMap);
119117

120-
if (levLoader.LoadFromFile(g_levname))
118+
if (levLoader.Load(&stream))
121119
{
122120
ExportLevelData();
123121
}
124122

123+
fclose(levTest);
124+
125125
MsgWarning("Freeing level data ...\n");
126126

127127
g_levMap->FreeAll();

DriverLevelTool/driver_routines/d2_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "core/dktypes.h"
55
#include "math/psx_math_types.h"
66

7-
#define TEXPAGE_SIZE_X (128)
7+
#define TEXPAGE_SIZE_X (128) // don't ask why. It's 4 bit.
88
#define TEXPAGE_SIZE_Y (256)
99

1010
#define TEXPAGE_4BIT_SIZE (TEXPAGE_SIZE_X*TEXPAGE_SIZE_Y)

DriverLevelTool/driver_routines/level.cpp

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -299,27 +299,16 @@ void CDriverLevelLoader::Release()
299299
//-------------------------------------------------------------
300300
// Loads the LEV file data
301301
//-------------------------------------------------------------
302-
bool CDriverLevelLoader::LoadFromFile(const char* fileName)
302+
bool CDriverLevelLoader::Load(IVirtualStream* pStream)
303303
{
304-
// try load driver2 lev file
305-
FILE* pReadFile = fopen(fileName, "rb");
306-
307-
if (!pReadFile)
308-
{
309-
MsgError("Failed to open LEV file!\n");
304+
if (!pStream)
310305
return false;
311-
}
312-
313-
CFileStream stream(pReadFile);
314-
315-
// seek to begin
316-
MsgWarning("-----------\nLoading LEV file '%s'\n", fileName);
317306

318307
//-------------------------------------------------------------------
319308

320309
// perform auto-detection if format is not specified
321310
if (m_format == LEV_FORMAT_AUTODETECT)
322-
m_format = DetectLevelFormat(&stream);
311+
m_format = DetectLevelFormat(pStream);
323312

324313
if (m_map)
325314
m_map->SetFormat(m_format);
@@ -328,17 +317,16 @@ bool CDriverLevelLoader::LoadFromFile(const char* fileName)
328317
m_textures->SetFormat(m_format);
329318

330319
LUMP curLump;
331-
stream.Read(&curLump, sizeof(curLump), 1);
320+
pStream->Read(&curLump, sizeof(curLump), 1);
332321

333322
if (curLump.type != LUMP_LUMPDESC)
334323
{
335324
MsgError("Not a LEV file!\n");
336-
fclose(pReadFile);
337325
return false;
338326
}
339327

340328
// read chunk offsets
341-
stream.Read(m_lumpInfo, sizeof(OUT_CITYLUMP_INFO), 1);
329+
pStream->Read(m_lumpInfo, sizeof(OUT_CITYLUMP_INFO), 1);
342330

343331
DevMsg(SPEW_NORM, "data1_offset = %d\n", m_lumpInfo->loadtime_offset);
344332
DevMsg(SPEW_NORM, "data1_size = %d\n", m_lumpInfo->loadtime_size);
@@ -356,53 +344,48 @@ bool CDriverLevelLoader::LoadFromFile(const char* fileName)
356344

357345
//-----------------------------------------------------
358346
// seek to section 1 - lump data 1
359-
stream.Seek(m_lumpInfo->loadtime_offset, VS_SEEK_SET);
347+
pStream->Seek(m_lumpInfo->loadtime_offset, VS_SEEK_SET);
360348

361349
// read lump
362-
stream.Read(&curLump, sizeof(curLump), 1);
350+
pStream->Read(&curLump, sizeof(curLump), 1);
363351

364352
if (curLump.type != LUMP_LOADTIME_DATA)
365353
{
366354
MsgError("Not a LUMP_LOADTIME_DATA!\n");
367-
fclose(pReadFile);
368355
return false;
369356
}
370357

371358
DevMsg(SPEW_INFO, "entering LUMP_LOADTIME_DATA size = %d\n--------------\n", curLump.size);
372359

373360
// read sublumps
374-
ProcessLumps(&stream);
361+
ProcessLumps(pStream);
375362

376363
//-----------------------------------------------------
377364
// read global textures
378365

379366
if (m_textures)
380367
{
381-
stream.Seek(m_lumpInfo->tpage_offset, VS_SEEK_SET);
382-
m_textures->LoadPermanentTPages(&stream);
368+
pStream->Seek(m_lumpInfo->tpage_offset, VS_SEEK_SET);
369+
m_textures->LoadPermanentTPages(pStream);
383370
}
384371

385372
//-----------------------------------------------------
386373
// seek to section 3 - lump data 2
387-
stream.Seek(m_lumpInfo->inmem_offset, VS_SEEK_SET);
374+
pStream->Seek(m_lumpInfo->inmem_offset, VS_SEEK_SET);
388375

389376
// read lump
390-
stream.Read(&curLump, sizeof(curLump), 1);
377+
pStream->Read(&curLump, sizeof(curLump), 1);
391378

392379
if (curLump.type != LUMP_INMEMORY_DATA)
393380
{
394381
MsgError("Not a lump LUMP_INMEMORY_DATA!\n");
395-
fclose(pReadFile);
396382
return false;
397383
}
398384

399385
DevMsg(SPEW_INFO, "entering LUMP_INMEMORY_DATA size = %d\n--------------\n", curLump.size);
400386

401387
// read sublumps
402-
ProcessLumps(&stream);
403-
404-
// completed!
405-
fclose(pReadFile);
388+
ProcessLumps(pStream);
406389

407390
return true;
408391
}

DriverLevelTool/driver_routines/level.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class CDriverLevelLoader
8383

8484
ELevelFormat GetFormat() const;
8585

86-
bool LoadFromFile(const char* fileName);
86+
bool Load(IVirtualStream* pStream);
8787

8888
protected:
8989
void ProcessLumps(IVirtualStream* pFile);

DriverLevelTool/driver_routines/models.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <malloc.h>
55
#include <nstd/HashSet.hpp>
6-
#include "util/util.h"
6+
77
#include "models.h"
88

99
#include <string.h>
@@ -348,9 +348,9 @@ void SwapValues(T& a, T& b)
348348

349349
// returns size of face and fills dface_t struct
350350
// TODO: rework, few variants of faces still looks bad
351-
int decode_poly(const char* polyList, dpoly_t* out)
351+
int decode_poly(const char* polyList, dpoly_t* out, int forceType /*= -1*/)
352352
{
353-
int ptype = *polyList & 0x1f;
353+
int ptype = forceType == -1 ? (*polyList & 0x1f) : forceType;
354354

355355
out->page = 0xFF;
356356
out->detail = 0xFF;
@@ -361,7 +361,7 @@ int decode_poly(const char* polyList, dpoly_t* out)
361361
switch (ptype)
362362
{
363363
case 1:
364-
// what a strange face type
364+
// what a strange face type. Hardcoded?
365365
*(uint*)out->vindices = *(uint*)&polyList[3];
366366
break;
367367
case 0:
@@ -479,5 +479,5 @@ int decode_poly(const char* polyList, dpoly_t* out)
479479
}
480480
}
481481

482-
return PolySizes[ptype];
482+
return PolySizes[*polyList & 0x1f];
483483
}

DriverLevelTool/driver_routines/models.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,18 @@ class CDriverLevelModels
123123

124124
Array<String> m_model_names;
125125

126-
OnModelLoaded_t m_onModelLoaded;
127-
OnModelFreed_t m_onModelFreed;
128-
OnCarModelLoaded_t m_onCarModelLoaded;
129-
OnCarModelFreed_t m_onCarModelFreed;
126+
OnModelLoaded_t m_onModelLoaded{ nullptr };
127+
OnModelFreed_t m_onModelFreed{ nullptr };
128+
OnCarModelLoaded_t m_onCarModelLoaded{ nullptr };
129+
OnCarModelFreed_t m_onCarModelFreed{ nullptr };
130130

131131
int m_numModelsInPack{0};
132132
};
133133

134134
//------------------------------------------------------------------------------------------------------------
135135

136136
void PrintUnknownPolys();
137-
int decode_poly(const char* face, dpoly_t* out);
137+
int decode_poly(const char* face, dpoly_t* out, int forceType = -1);
138138

139139
//-------------------------------------------------------------------------------
140140

DriverLevelTool/driver_routines/regions.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,27 @@ void CBaseLevelMap::LoadInAreaModels(const SPOOL_CONTEXT& ctx, int areaDataNum)
446446
delete[] new_model_numbers;
447447
}
448448

449+
bool CBaseLevelMap::IsRegionSpooled(const XZPAIR& cell) const
450+
{
451+
CBaseLevelRegion* reg = GetRegion(cell);
452+
return reg ? reg->m_loaded : false;
453+
}
454+
455+
bool CBaseLevelMap::IsRegionSpooled(int index) const
456+
{
457+
CBaseLevelRegion* reg = GetRegion(index);
458+
return reg ? reg->m_loaded : false;
459+
}
460+
461+
int CBaseLevelMap::GetRegionIndex(const XZPAIR& cell) const
462+
{
463+
// lookup region
464+
const int region_x = cell.x / m_mapInfo.region_size;
465+
const int region_z = cell.z / m_mapInfo.region_size;
466+
467+
return region_x + region_z * m_regions_across;
468+
}
469+
449470
void CBaseLevelMap::InitRegion(CBaseLevelRegion* region, int index) const
450471
{
451472
ushort spoolOffset = m_regionSpoolInfoOffsets[index];

DriverLevelTool/driver_routines/regions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ class CBaseLevelMap
9494
virtual void SpoolRegion(const SPOOL_CONTEXT& ctx, const XZPAIR& cell) = 0;
9595
virtual void SpoolRegion(const SPOOL_CONTEXT& ctx, int regionIdx) = 0;
9696

97+
int GetRegionIndex(const XZPAIR& cell) const;
98+
99+
bool IsRegionSpooled(const XZPAIR& cell) const;
100+
bool IsRegionSpooled(int index) const;
101+
97102
virtual CBaseLevelRegion* GetRegion(const XZPAIR& cell) const = 0;
98103
virtual CBaseLevelRegion* GetRegion(int regionIdx) const = 0;
99104

DriverLevelTool/driver_routines/regions_d2.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,11 +651,11 @@ bool CDriver2LevelMap::UnpackCellObject(CELL_OBJECT& co, PACKED_CELL_OBJECT* pco
651651
if (!pco)
652652
return false;
653653

654-
co.pos.vx = nearCell.x + (((pco->pos.vx - nearCell.x) << 0x10) >> 0x10);
655-
co.pos.vz = nearCell.z + (((pco->pos.vz - nearCell.z) << 0x10) >> 0x10);
654+
co.pos.vx = nearCell.x + (short)(pco->pos.vx - nearCell.x);
655+
co.pos.vz = nearCell.z + (short)(pco->pos.vz - nearCell.z);
656656

657657
// cell height should be negated
658-
co.pos.vy = ((pco->pos.vy << 0x10) >> 0x11);
658+
co.pos.vy = (short)pco->pos.vy >> 1;
659659

660660
co.yang = pco->value & 0x3f;
661661
co.type = (pco->value >> 6) | ((pco->pos.vy & 1) << 10);

DriverLevelTool/driver_routines/textures.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,10 @@ void CTexturePage::ConvertIndexedTextureToRGBA(uint* dest_color_data, int detail
9898
int h = texInfo.height;
9999

100100
if (w == 0)
101-
w = 256;
101+
w = TEXPAGE_SIZE_Y;
102102

103103
if (h == 0)
104-
h = 256;
105-
106-
//char* textureName = g_textureNamesData + m_details[detail].nameoffset;
107-
//MsgWarning("Applying detail %d '%s' (xywh: %d %d %d %d)\n", detail, textureName, ox, oy, w, h);
104+
h = TEXPAGE_SIZE_Y;
108105

109106
int tp_wx = ox + w;
110107
int tp_hy = oy + h;
@@ -113,7 +110,7 @@ void CTexturePage::ConvertIndexedTextureToRGBA(uint* dest_color_data, int detail
113110
{
114111
for (int x = ox; x < tp_wx; x++)
115112
{
116-
ubyte clindex = bitmap.data[y * 128 + x / 2];
113+
ubyte clindex = bitmap.data[y * TEXPAGE_SIZE_X + x / 2];
117114

118115
if (0 != (x & 1))
119116
clindex >>= 4;

0 commit comments

Comments
 (0)