Skip to content

Commit 13ecafe

Browse files
committed
- D1 Roads support
1 parent 5ca9481 commit 13ecafe

File tree

4 files changed

+165
-8
lines changed

4 files changed

+165
-8
lines changed

routines/d2_types.h

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ struct DRIVER2_JUNCTION
382382
};
383383

384384
//------------------------------------------------------------------------------------------------------------
385-
// Driver 1 PSX is so wasteful
385+
// Driver 1 PSX
386386

387387
struct ROAD_MAP_LUMP_DATA
388388
{
@@ -420,12 +420,57 @@ struct ROUTE_DATA
420420
int type;
421421
int height;
422422
int objectAngle;
423+
int roadIndex;
423424
uint value;
424425
};
425426

427+
enum DRIVER1_ROAD_FLAGS
428+
{
429+
ROAD_FLAG_PARKING = (1 << 0)
430+
};
431+
432+
struct DRIVER1_ROAD
433+
{
434+
short id;
435+
short ConnectIdx[2];
436+
char NumLanes, LaneDirs; // looks like the same as in D2
437+
short length;
438+
char flags;
439+
char pad;
440+
short x, z;
441+
};
442+
443+
struct DRIVER1_JUNC_ETRY
444+
{
445+
short fromJunc;
446+
short road;
447+
short flags;
448+
short extraFlags;
449+
};
450+
451+
struct DRIVER1_JUNCTION
452+
{
453+
short id;
454+
char type;
455+
char pad;
456+
short lightsTimer;
457+
char lightsNS, lightsWE;
458+
DRIVER1_JUNC_ETRY entries[4];
459+
short x, z;
460+
};
461+
462+
struct DRIVER1_ROADBOUNDS
463+
{
464+
int x1, y1;
465+
int x2, y2;
466+
uint8 dir;
467+
uint8 _pad[3];
468+
};
469+
426470
//------------------------------------------------------------------------------------------------------------
427471

428-
struct AreaDataStr {
472+
struct AreaDataStr
473+
{
429474
uint16 gfx_offset;
430475
uint16 model_offset;
431476
uint16 music_offset;

routines/level.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,13 @@ void CDriverLevelLoader::ProcessLumps(IVirtualStream* pFile)
232232
break;
233233
case LUMP_ROADS:
234234
DevMsg(SPEW_WARNING, "LUMP_ROADS ofs=%d size=%d\n", pFile->Tell(), lump.size);
235+
if (m_map)
236+
((CDriver1LevelMap*)m_map)->LoadRoadsLump(pFile);
235237
break;
236238
case LUMP_JUNCTIONS:
237239
DevMsg(SPEW_WARNING, "LUMP_JUNCTIONS ofs=%d size=%d\n", pFile->Tell(), lump.size);
240+
if (m_map)
241+
((CDriver1LevelMap*)m_map)->LoadJunctionsLump(pFile);
238242
break;
239243
case LUMP_ROADSURF:
240244
DevMsg(SPEW_WARNING, "LUMP_ROADSURF ofs=%d size=%d\n", pFile->Tell(), lump.size);
@@ -243,9 +247,13 @@ void CDriverLevelLoader::ProcessLumps(IVirtualStream* pFile)
243247
break;
244248
case LUMP_ROADBOUNDS:
245249
DevMsg(SPEW_WARNING, "LUMP_ROADBOUNDS ofs=%d size=%d\n", pFile->Tell(), lump.size);
250+
if (m_map)
251+
((CDriver1LevelMap*)m_map)->LoadRoadBoundsLump(pFile);
246252
break;
247253
case LUMP_JUNCBOUNDS:
248254
DevMsg(SPEW_WARNING, "LUMP_JUNCBOUNDS ofs=%d size=%d\n", pFile->Tell(), lump.size);
255+
if (m_map)
256+
((CDriver1LevelMap*)m_map)->LoadJuncBoundsLump(pFile);
249257
break;
250258
case LUMP_SUBDIVISION:
251259
DevMsg(SPEW_WARNING, "LUMP_SUBDIVISION ofs=%d size=%d\n", pFile->Tell(), lump.size);

routines/regions_d1.cpp

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ void CDriver1LevelRegion::FreeAll()
2222

2323
delete[] m_roadMap;
2424
m_roadMap = nullptr;
25+
26+
delete[] m_surfaceRoads;
27+
m_surfaceRoads = nullptr;
2528
}
2629

2730
void CDriver1LevelRegion::LoadRegionData(const SPOOL_CONTEXT& ctx)
@@ -58,8 +61,8 @@ void CDriver1LevelRegion::LoadRegionData(const SPOOL_CONTEXT& ctx)
5861
const int pvsDataOffset = cellObjectsOffset + m_spoolInfo->cell_data_size[2]; // FIXME: is it even there in Driver 1?
5962

6063
// read roadm (map?)
61-
//pFile->Seek(ctx.lumpInfo->spooled_offset + roadMOffset * SPOOL_CD_BLOCK_SIZE, VS_SEEK_SET);
62-
//pFile->Read(roadMapData, m_spoolInfo->roadm_size * SPOOL_CD_BLOCK_SIZE, sizeof(char));
64+
pFile->Seek(ctx.lumpInfo->spooled_offset + roadMOffset * SPOOL_CD_BLOCK_SIZE, VS_SEEK_SET);
65+
LoadRoadCellsData(pFile);
6366

6467
// read roadh (heights?)
6568
pFile->Seek(ctx.lumpInfo->spooled_offset + roadHOffset * SPOOL_CD_BLOCK_SIZE, VS_SEEK_SET);
@@ -137,6 +140,30 @@ void CDriver1LevelRegion::LoadRoadHeightMapData(IVirtualStream* pFile)
137140
delete[] roadMapData;
138141
}
139142

143+
void CDriver1LevelRegion::LoadRoadCellsData(IVirtualStream* pFile)
144+
{
145+
m_surfaceRoads = new ushort[ROAD_MAP_REGION_CELLS];
146+
ushort* pRoadIds = m_surfaceRoads;
147+
int i = ROAD_MAP_REGION_CELLS;
148+
149+
do {
150+
short length;
151+
pFile->Read(&length, 1, sizeof(short));
152+
153+
if (length == -1)
154+
break;
155+
156+
ushort value;
157+
pFile->Read(&value, 1, sizeof(short));
158+
159+
int count = length;
160+
for (i = i - count; count > 0; count--)
161+
{
162+
*pRoadIds++ = value;
163+
}
164+
} while (i > 0);
165+
}
166+
140167
//----------------------------------------
141168
// cell iterator
142169
CELL_OBJECT* CDriver1LevelRegion::StartIterator(CELL_ITERATOR_D1* iterator, int cellNumber) const
@@ -175,6 +202,18 @@ void CDriver1LevelMap::FreeAll()
175202
Memory::free(m_surfaceData);
176203
m_surfaceData = nullptr;
177204

205+
delete[] m_roads;
206+
m_roads = nullptr;
207+
208+
delete[] m_roadBounds;
209+
m_roadBounds = nullptr;
210+
211+
delete[] m_junctions;
212+
m_junctions = nullptr;
213+
214+
delete[] m_junctionBounds;
215+
m_junctionBounds = nullptr;
216+
178217
CBaseLevelMap::FreeAll();
179218
}
180219

@@ -225,6 +264,36 @@ void CDriver1LevelMap::LoadRoadMapLump(IVirtualStream* pFile)
225264
//m_roadMapLumpData.unitZMid = 1500 * height / 2;
226265
}
227266

267+
void CDriver1LevelMap::LoadRoadsLump(IVirtualStream* pFile)
268+
{
269+
pFile->Read(&m_numRoads, 1, sizeof(int));
270+
m_roads = new DRIVER1_ROAD[m_numRoads];
271+
pFile->Read(m_roads, m_numRoads, sizeof(DRIVER1_ROAD));
272+
}
273+
274+
void CDriver1LevelMap::LoadJunctionsLump(IVirtualStream* pFile)
275+
{
276+
pFile->Read(&m_numJunctions, 1, sizeof(int));
277+
m_junctions = new DRIVER1_JUNCTION[m_numJunctions];
278+
pFile->Read(m_junctions, m_numJunctions, sizeof(DRIVER1_JUNCTION));
279+
}
280+
281+
void CDriver1LevelMap::LoadRoadBoundsLump(IVirtualStream* pFile)
282+
{
283+
int numRoadBounds;
284+
pFile->Read(&numRoadBounds, 1, sizeof(int));
285+
m_roadBounds = new DRIVER1_ROADBOUNDS[numRoadBounds];
286+
pFile->Read(m_roadBounds, numRoadBounds, sizeof(DRIVER1_ROADBOUNDS));
287+
}
288+
289+
void CDriver1LevelMap::LoadJuncBoundsLump(IVirtualStream* pFile)
290+
{
291+
int numJuncBounds;
292+
pFile->Read(&numJuncBounds, 1, sizeof(int));
293+
m_junctionBounds = new XYPAIR[numJuncBounds];
294+
pFile->Read(m_junctionBounds, numJuncBounds, sizeof(XYPAIR));
295+
}
296+
228297
void CDriver1LevelMap::LoadRoadSurfaceLump(IVirtualStream* pFile, int size)
229298
{
230299
int numSurfaces;
@@ -252,6 +321,12 @@ CBaseLevelRegion* CDriver1LevelMap::GetRegion(const XZPAIR& cell) const
252321
const int region_x = cell.x / m_mapInfo.region_size;
253322
const int region_z = cell.z / m_mapInfo.region_size;
254323

324+
if (region_x < 0 ||
325+
region_z < 0 ||
326+
region_x >= m_regions_across ||
327+
region_z >= m_regions_down)
328+
return nullptr;
329+
255330
return GetRegion(region_x + region_z * m_regions_across);
256331
}
257332

@@ -370,13 +445,15 @@ bool CDriver1LevelMap::GetRoadInfo(ROUTE_DATA& outData, const VECTOR_NOPAD& posi
370445
return false;
371446
}
372447

373-
const uint value = region->m_roadMap[(cpos.x % road_region_size) +
374-
(cpos.z % road_region_size) * road_region_size];
448+
const int cellIdx = (cpos.x % road_region_size) + (cpos.z % road_region_size) * road_region_size;
449+
450+
const uint value = region->m_roadMap[cellIdx];
375451

376452
outData.height = *(short*)&value;
377-
outData.type = value >> 16 & 0x3ff;
453+
outData.type = value >> 16 & 1023;
378454
outData.objectAngle = (value >> 30) * 1024;
379455
outData.value = value;
456+
outData.roadIndex = region->m_surfaceRoads[cellIdx];
380457

381458
return true;
382459
}

routines/regions_d1.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
// DRIVER 1
77
//----------------------------------------------------------------------------------
88

9+
#define ROAD_MAP_REGION_CELLS 3600
10+
911
class CDriver1LevelRegion;
1012
class CDriver1LevelMap;
1113

@@ -32,9 +34,11 @@ class CDriver1LevelRegion : public CBaseLevelRegion
3234

3335
protected:
3436
void LoadRoadHeightMapData(IVirtualStream* pFile);
37+
void LoadRoadCellsData(IVirtualStream* pFile);
3538

3639
CELL_DATA_D1* m_cells{ nullptr }; // cell data that holding information about cell pointers. 3D world seeks cells first here
3740
uint* m_roadMap{ nullptr };
41+
ushort* m_surfaceRoads{ nullptr };
3842
};
3943

4044

@@ -50,6 +54,11 @@ class CDriver1LevelMap : public CBaseLevelMap
5054
void LoadMapLump(IVirtualStream* pFile) override;
5155
void LoadSpoolInfoLump(IVirtualStream* pFile) override;
5256

57+
void LoadRoadsLump(IVirtualStream* pFile);
58+
void LoadJunctionsLump(IVirtualStream* pFile);
59+
void LoadRoadBoundsLump(IVirtualStream* pFile);
60+
void LoadJuncBoundsLump(IVirtualStream* pFile);
61+
5362
void LoadRoadMapLump(IVirtualStream* pFile); // or NewProcessRoadMapLump in D1 & D2
5463
void LoadRoadSurfaceLump(IVirtualStream* pFile, int size);
5564

@@ -68,7 +77,17 @@ class CDriver1LevelMap : public CBaseLevelMap
6877

6978
//----------------------------------------
7079
// road map stuff
71-
bool GetRoadInfo(ROUTE_DATA& outData, const VECTOR_NOPAD& position) const;
80+
bool GetRoadInfo(ROUTE_DATA& outData, const VECTOR_NOPAD& position) const;
81+
82+
int GetNumRoads() const { return m_numRoads; }
83+
int GetNumJunctions() const { return m_numJunctions; }
84+
85+
const ROAD_MAP_LUMP_DATA& GetRoadMapLumpData() const { return m_roadMapLumpData; }
86+
87+
DRIVER1_ROAD* GetRoad(int idx) const { return &m_roads[idx]; }
88+
DRIVER1_ROADBOUNDS* GetRoadBounds(int idx) const { return &m_roadBounds[idx]; }
89+
DRIVER1_JUNCTION* GetJunction(int idx) const { return &m_junctions[idx]; }
90+
XYPAIR* GetJunctionBounds(int idx) const { return &m_junctionBounds[idx]; }
7291

7392
protected:
7493

@@ -79,6 +98,14 @@ class CDriver1LevelMap : public CBaseLevelMap
7998
CDriver1LevelRegion* m_regions{ nullptr }; // map of regions
8099
SURFACEINFO* m_surfacePtrs[900];
81100
char* m_surfaceData{ nullptr };
101+
102+
DRIVER1_ROAD* m_roads{ nullptr };
103+
DRIVER1_ROADBOUNDS* m_roadBounds{ nullptr };
104+
DRIVER1_JUNCTION* m_junctions{ nullptr };
105+
XYPAIR* m_junctionBounds{ nullptr };
106+
107+
int m_numRoads{ 0 };
108+
int m_numJunctions{ 0 };
82109
};
83110

84111

0 commit comments

Comments
 (0)