Skip to content

Commit 8046a77

Browse files
committed
- add surface index to world Lua interface
1 parent 060a550 commit 8046a77

File tree

7 files changed

+69
-0
lines changed

7 files changed

+69
-0
lines changed

Diff for: game/shared/world.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ void CWorld::Lua_Init(sol::state& lua)
6969
world[LUADOC_M("MapHeight", "(position: fix.VECTOR) : int - returns height value at specified 3D point")]
7070
= &MapHeight;
7171

72+
world[LUADOC_M("GetSurfaceIndex", "(position: fix.VECTOR) : int - returns surface (road) index value at specified 3D point")]
73+
= &GetSurfaceIndex;
74+
7275
world[LUADOC_M("QueryCollision", "(queryPos: fix.VECTOR, queryDist: int, func: function(box: BUILDING_BOX, cellObj: CELL_OBJECT)) - performs cell object query")]
7376
= [](const VECTOR_NOPAD& queryPos, int queryDist, sol::function& func) {
7477
QueryCollision(queryPos, queryDist, [&func](const BUILDING_BOX& box, CELL_OBJECT* co) {
@@ -700,6 +703,11 @@ void CWorld::FindSurface(const VECTOR_NOPAD& position, VECTOR_NOPAD& outNormal,
700703
}
701704
}
702705

706+
int CWorld::GetSurfaceIndex(const VECTOR_NOPAD& position)
707+
{
708+
return g_levMap->GetSurfaceIndex(position);
709+
}
710+
703711
//-------------------------------------------------------------
704712

705713
void CWorld::QueryCollision(const VECTOR_NOPAD& queryPos, int queryDist, const BoxCollisionFn& func)

Diff for: game/shared/world.h

+19
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ struct MODEL_EFEFCTS
5959
Array<EFFECT_DESC> effects;
6060
};
6161

62+
struct ROAD_PROPERTIES
63+
{
64+
int index;
65+
// TODO: accessors to short ConnectIdx[4];
66+
// TODO: accessors to int8 NumLanes;
67+
// TODO: accessors to int8 LaneDirs;
68+
// TODO: accessors to int8 AILanes;
69+
70+
// TODO: type
71+
72+
// TODO: accessor to curve, straight, junction
73+
};
74+
6275
// TODO: replace std::function with custom impl with allocator support
6376
using CellObjectIterateFn = std::function<bool(int listType, CELL_OBJECT* co)>;
6477
using BoxCollisionFn = std::function<bool(const BUILDING_BOX& box, CELL_OBJECT* co)>;
@@ -105,8 +118,14 @@ class CWorld
105118
static ModelRef_t* GetModelByIndex(int modelIndex);
106119
static ModelRef_t* GetModelByName(const char* name);
107120

121+
//------------------------------------------
122+
// road surfaces
123+
108124
static int MapHeight(const VECTOR_NOPAD& position);
109125
static void FindSurface(const VECTOR_NOPAD& position, VECTOR_NOPAD& outNormal, VECTOR_NOPAD& outPoint, sdPlane& outPlane);
126+
static int GetSurfaceIndex(const VECTOR_NOPAD& position);
127+
128+
static ROAD_PROPERTIES GetRoadProperties(int surfaceIndex);
110129

111130
//------------------------------------------
112131
// objects and collision

Diff for: routines/regions.h

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class CBaseLevelMap
106106
virtual CBaseLevelRegion* GetRegion(int regionIdx) const = 0;
107107

108108
virtual void FindSurface(const VECTOR_NOPAD& position, VECTOR_NOPAD& outPoint, sdPlane& outPlane) const = 0;
109+
virtual int GetSurfaceIndex(const VECTOR_NOPAD& position) const = 0;
109110

110111
// converters
111112
void WorldPositionToCellXZ(XZPAIR& cell, const VECTOR_NOPAD& position, const XZPAIR& offset = {0}) const;

Diff for: routines/regions_d1.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "core/cmdlib.h"
66
#include "core/IVirtualStream.h"
77
#include "math/isin.h"
8+
#include <climits>
89

910
extern sdPlane g_defaultPlane;
1011
extern sdPlane g_seaPlane;
@@ -596,6 +597,20 @@ void CDriver1LevelMap::FindSurface(const VECTOR_NOPAD& position, VECTOR_NOPAD& o
596597
}
597598
}
598599

600+
int CDriver1LevelMap::GetSurfaceIndex(const VECTOR_NOPAD& position) const
601+
{
602+
ROUTE_DATA routeData;
603+
if (!GetRoadInfo(routeData, position))
604+
{
605+
return -32;
606+
}
607+
608+
if (routeData.roadIndex >= SHRT_MAX)
609+
return -32;
610+
611+
return routeData.roadIndex;
612+
}
613+
599614
//-------------------------------------------------------------
600615
// returns first cell object of cell
601616
//-------------------------------------------------------------

Diff for: routines/regions_d1.h

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class CDriver1LevelMap : public CBaseLevelMap
6969
CBaseLevelRegion* GetRegion(int regionIdx) const override;
7070

7171
void FindSurface(const VECTOR_NOPAD& position, VECTOR_NOPAD& outPoint, sdPlane& outPlane) const override;
72+
int GetSurfaceIndex(const VECTOR_NOPAD& position) const override;
7273

7374
//----------------------------------------
7475
// cell iterator

Diff for: routines/regions_d2.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,30 @@ void CDriver2LevelMap::FindSurface(const VECTOR_NOPAD& position, VECTOR_NOPAD& o
753753
}
754754
}
755755

756+
int CDriver2LevelMap::GetSurfaceIndex(const VECTOR_NOPAD& position) const
757+
{
758+
VECTOR_NOPAD cellPos;
759+
XZPAIR cell;
760+
int level = 0;
761+
762+
cellPos.vx = position.vx - 512; // FIXME: is that a quarter of a cell?
763+
cellPos.vy = position.vy;
764+
cellPos.vz = position.vz - 512;
765+
766+
WorldPositionToCellXZ(cell, cellPos);
767+
const CDriver2LevelRegion* region = (CDriver2LevelRegion*)GetRegion(cell);
768+
769+
if (!region)
770+
return -32;
771+
772+
const sdPlane* plane = region->SdGetCell(cellPos, level, SdGetBSP);
773+
774+
if (!plane)
775+
return -32;
776+
777+
return plane->surfaceType - 32;
778+
}
779+
756780
int CDriver2LevelMap::GetRoadIndex(VECTOR_NOPAD& position) const
757781
{
758782
VECTOR_NOPAD cellPos;

Diff for: routines/regions_d2.h

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class CDriver2LevelMap : public CBaseLevelMap
134134
CBaseLevelRegion* GetRegion(int regionIdx) const override;
135135

136136
void FindSurface(const VECTOR_NOPAD& position, VECTOR_NOPAD& outPoint, sdPlane& outPlane) const override;
137+
int GetSurfaceIndex(const VECTOR_NOPAD& position) const override;
137138

138139
int GetRoadIndex(VECTOR_NOPAD& position) const;
139140

0 commit comments

Comments
 (0)