Permalink
Browse files

Add additional TraceRay natives (#885)

* More TR natives

* Fix small typo

* Fix another typo

* Fix TR_AllSolid description

* Change Disp to Displacement

* Reorder params in TR_GetSurfaceName

* Fix TR_GetSurfaceName

* Another fix for TR_GetSurfaceName -_-

* Change if block to switch

* Make handle first param in TR_GetSurfaceName

* Reorder TR_GetStartPosition params

* Make TranslatePartitionFlags static

* Change tabs used for formatting to spaces

* Change inline comment style to be consistent with rest of file
  • Loading branch information...
SlidyBat authored and KyleSanderson committed Oct 7, 2018
1 parent 34e91fb commit 3add38f66581982a74c72702fd44b9b0480ff5df
@@ -61,6 +61,7 @@ SH_DECL_HOOK1_void_vafmt(IVEngineServer, ClientCommand, SH_NOATTRIB, 0, edict_t
SDKTools g_SdkTools; /**< Global singleton for extension's main interface */
IServerGameEnts *gameents = NULL;
IEngineTrace *enginetrace = NULL;
ISpatialPartition *partition = NULL;
IEngineSound *engsound = NULL;
INetworkStringTableContainer *netstringtables = NULL;
IServerPluginHelpers *pluginhelpers = NULL;
@@ -269,6 +270,7 @@ bool SDKTools::SDK_OnMetamodLoad(ISmmAPI *ismm, char *error, size_t maxlen, bool
GET_V_IFACE_ANY(GetServerFactory, gameents, IServerGameEnts, INTERFACEVERSION_SERVERGAMEENTS);
GET_V_IFACE_ANY(GetEngineFactory, engsound, IEngineSound, IENGINESOUND_SERVER_INTERFACE_VERSION);
GET_V_IFACE_ANY(GetEngineFactory, enginetrace, IEngineTrace, INTERFACEVERSION_ENGINETRACE_SERVER);
GET_V_IFACE_ANY(GetEngineFactory, partition, ISpatialPartition, INTERFACEVERSION_SPATIALPARTITION);
GET_V_IFACE_ANY(GetEngineFactory, netstringtables, INetworkStringTableContainer, INTERFACENAME_NETWORKSTRINGTABLESERVER);
GET_V_IFACE_ANY(GetEngineFactory, pluginhelpers, IServerPluginHelpers, INTERFACEVERSION_ISERVERPLUGINHELPERS);
GET_V_IFACE_ANY(GetServerFactory, serverClients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
@@ -42,6 +42,7 @@
#include <IPlayerHelpers.h>
#include <IGameHelpers.h>
#include <IEngineTrace.h>
#include <ispatialpartition.h>
#include <IEngineSound.h>
#include <ivoiceserver.h>
#include <iplayerinfo.h>
@@ -131,6 +132,7 @@ extern SDKTools g_SdkTools;
/* Interfaces from engine or gamedll */
extern IServerGameEnts *gameents;
extern IEngineTrace *enginetrace;
extern ISpatialPartition *partition;
extern IEngineSound *engsound;
extern INetworkStringTableContainer *netstringtables;
extern IServerPluginHelpers *pluginhelpers;
@@ -74,17 +74,17 @@ class CSMTraceFilter : public CTraceFilter
cell_t m_Data;
};

class CSMTraceEnumerator : public IEntityEnumerator
class CSMTraceEnumerator : public IPartitionEnumerator
{
public:
bool EnumEntity(IHandleEntity *pEntity) override
IterationRetval_t EnumElement(IHandleEntity *pEntity) override
{
cell_t res = 1;
m_pFunc->PushCell(gamehelpers->EntityToBCompatRef(reinterpret_cast<CBaseEntity*>(pEntity)));
m_pFunc->PushCell(m_Data);
m_pFunc->Execute(&res);

return (res) ? true : false;
return (res) ? ITERATION_CONTINUE : ITERATION_STOP;
}
void SetFunctionPtr(IPluginFunction *pFunc, cell_t data)
{
@@ -114,6 +114,20 @@ enum
RayType_Infinite
};

// For backwards compatibility, old EnumerateEntities functions accepted bool instead of flags
static int TranslatePartitionFlags(int input)
{
switch (input)
{
case 0:
return PARTITION_ENGINE_SOLID_EDICTS;
case 1:
return PARTITION_ENGINE_TRIGGER_EDICTS;
default:
return input >> 1;
}
}

static cell_t smn_TRTraceRay(IPluginContext *pContext, const cell_t *params)
{
cell_t *startaddr, *endaddr;
@@ -211,8 +225,8 @@ static cell_t smn_TREnumerateEntities(IPluginContext *pContext, const cell_t *pa

g_Ray.Init(g_StartVec, g_EndVec);

bool triggers = (params[3]) ? true : false;
enginetrace->EnumerateEntities(g_Ray, triggers, &g_SMTraceEnumerator);
int mask = TranslatePartitionFlags(params[3]);
partition->EnumerateElementsAlongRay(mask, g_Ray, false, &g_SMTraceEnumerator);

return 1;
}
@@ -246,8 +260,8 @@ static cell_t smn_TREnumerateEntitiesHull(IPluginContext *pContext, const cell_t

g_Ray.Init(g_StartVec, g_EndVec, g_HullMins, g_HullMaxs);

bool triggers = (params[5]) ? true : false;
enginetrace->EnumerateEntities(g_Ray, triggers, &g_SMTraceEnumerator);
int mask = TranslatePartitionFlags(params[5]);
partition->EnumerateElementsAlongRay(mask, g_Ray, false, &g_SMTraceEnumerator);

return 1;
}
@@ -752,6 +766,22 @@ static cell_t smn_TRGetFraction(IPluginContext *pContext, const cell_t *params)
return sp_ftoc(tr->fraction);
}

static cell_t smn_TRGetFractionLeftSolid(IPluginContext *pContext, const cell_t *params)
{
sm_trace_t *tr;
HandleError err;
HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());

if (params[1] == BAD_HANDLE)
{
tr = &g_Trace;
} else if ((err = handlesys->ReadHandle(params[1], g_TraceHandle, &sec, (void **)&tr)) != HandleError_None) {
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
}

return sp_ftoc(tr->fractionleftsolid);
}

static cell_t smn_TRGetPlaneNormal(IPluginContext *pContext, const cell_t *params)
{
sm_trace_t *tr;
@@ -776,6 +806,28 @@ static cell_t smn_TRGetPlaneNormal(IPluginContext *pContext, const cell_t *param
return 1;
}

static cell_t smn_TRGetStartPosition(IPluginContext *pContext, const cell_t *params)
{
sm_trace_t *tr;
HandleError err;
HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());

if (params[1] == BAD_HANDLE)
{
tr = &g_Trace;
} else if ((err = handlesys->ReadHandle(params[1], g_TraceHandle, &sec, (void **)&tr)) != HandleError_None) {
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
}

cell_t *addr;
pContext->LocalToPhysAddr(params[2], &addr);

addr[0] = sp_ftoc(tr->startpos.x);
addr[1] = sp_ftoc(tr->startpos.y);
addr[2] = sp_ftoc(tr->startpos.z);

return 1;
}

static cell_t smn_TRGetEndPosition(IPluginContext *pContext, const cell_t *params)
{
@@ -800,6 +852,120 @@ static cell_t smn_TRGetEndPosition(IPluginContext *pContext, const cell_t *param
return 1;
}

static cell_t smn_TRGetDisplacementFlags(IPluginContext *pContext, const cell_t *params)
{
sm_trace_t *tr;
HandleError err;
HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());

if (params[1] == BAD_HANDLE)
{
tr = &g_Trace;
} else if ((err = handlesys->ReadHandle(params[1], g_TraceHandle, &sec, (void **)&tr)) != HandleError_None) {
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
}

return tr->dispFlags;
}

static cell_t smn_TRGetSurfaceName(IPluginContext *pContext, const cell_t *params)
{
sm_trace_t *tr;
HandleError err;
HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());

if (params[1] == BAD_HANDLE)
{
tr = &g_Trace;
} else if ((err = handlesys->ReadHandle(params[1], g_TraceHandle, &sec, (void **)&tr)) != HandleError_None) {
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
}

pContext->StringToLocal(params[2], params[3], tr->surface.name);

return 1;
}

static cell_t smn_TRGetSurfaceProps(IPluginContext *pContext, const cell_t *params)
{
sm_trace_t *tr;
HandleError err;
HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());

if (params[1] == BAD_HANDLE)
{
tr = &g_Trace;
} else if ((err = handlesys->ReadHandle(params[1], g_TraceHandle, &sec, (void **)&tr)) != HandleError_None) {
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
}

return tr->surface.surfaceProps;
}

static cell_t smn_TRGetSurfaceFlags(IPluginContext *pContext, const cell_t *params)
{
sm_trace_t *tr;
HandleError err;
HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());

if (params[1] == BAD_HANDLE)
{
tr = &g_Trace;
} else if ((err = handlesys->ReadHandle(params[1], g_TraceHandle, &sec, (void **)&tr)) != HandleError_None) {
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
}

return tr->surface.flags;
}

static cell_t smn_TRGetPhysicsBone(IPluginContext *pContext, const cell_t *params)
{
sm_trace_t *tr;
HandleError err;
HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());

if (params[1] == BAD_HANDLE)
{
tr = &g_Trace;
} else if ((err = handlesys->ReadHandle(params[1], g_TraceHandle, &sec, (void **)&tr)) != HandleError_None) {
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
}

return tr->physicsbone;
}

static cell_t smn_TRAllSolid(IPluginContext *pContext, const cell_t *params)
{
sm_trace_t *tr;
HandleError err;
HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());

if (params[1] == BAD_HANDLE)
{
tr = &g_Trace;
} else if ((err = handlesys->ReadHandle(params[1], g_TraceHandle, &sec, (void **)&tr)) != HandleError_None) {
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
}

return tr->allsolid ? 1 : 0;
}

static cell_t smn_TRStartSolid(IPluginContext *pContext, const cell_t *params)
{
sm_trace_t *tr;
HandleError err;
HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());

if (params[1] == BAD_HANDLE)
{
tr = &g_Trace;
} else if ((err = handlesys->ReadHandle(params[1], g_TraceHandle, &sec, (void **)&tr)) != HandleError_None) {
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
}

return tr->startsolid ? 1 : 0;
}

static cell_t smn_TRDidHit(IPluginContext *pContext, const cell_t *params)
{
sm_trace_t *tr;
@@ -923,8 +1089,17 @@ sp_nativeinfo_t g_TRNatives[] =
{"TR_TraceRayEx", smn_TRTraceRayEx},
{"TR_TraceHullEx", smn_TRTraceHullEx},
{"TR_GetFraction", smn_TRGetFraction},
{"TR_GetFractionLeftSolid", smn_TRGetFractionLeftSolid},
{"TR_GetStartPosition", smn_TRGetStartPosition},
{"TR_GetEndPosition", smn_TRGetEndPosition},
{"TR_GetEntityIndex", smn_TRGetEntityIndex},
{"TR_GetDisplacementFlags", smn_TRGetDisplacementFlags },
{"TR_GetSurfaceName", smn_TRGetSurfaceName},
{"TR_GetSurfaceProps", smn_TRGetSurfaceProps},
{"TR_GetSurfaceFlags", smn_TRGetSurfaceFlags},
{"TR_GetPhysicsBone", smn_TRGetPhysicsBone},
{"TR_AllSolid", smn_TRAllSolid},
{"TR_StartSolid", smn_TRStartSolid},
{"TR_DidHit", smn_TRDidHit},
{"TR_GetHitGroup", smn_TRGetHitGroup},
{"TR_ClipRayToEntity", smn_TRClipRayToEntity},
Oops, something went wrong.

0 comments on commit 3add38f

Please sign in to comment.