Add new Traceray natives (#754)

This commit is contained in:
SlidyBat
2018-08-13 23:02:12 +01:00
committed by Asher Baker
parent e7c6b23439
commit 144fb907f1
2 changed files with 538 additions and 68 deletions
+356 -34
View File
@@ -8,7 +8,7 @@
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
@@ -74,6 +74,28 @@ private:
cell_t m_Data;
};
class CSMTraceEnumerator : public IEntityEnumerator
{
public:
bool EnumEntity(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;
}
void SetFunctionPtr(IPluginFunction *pFunc, cell_t data)
{
m_pFunc = pFunc;
m_Data = data;
}
private:
IPluginFunction *m_pFunc;
cell_t m_Data;
};
/* Used for the global trace version */
Ray_t g_Ray;
sm_trace_t g_Trace;
@@ -84,6 +106,7 @@ Vector g_HullMaxs;
QAngle g_DirAngles;
CTraceFilterHitAll g_HitAllFilter;
CSMTraceFilter g_SMTraceFilter;
CSMTraceEnumerator g_SMTraceEnumerator;
enum
{
@@ -145,6 +168,175 @@ static cell_t smn_TRTraceHull(IPluginContext *pContext, const cell_t *params)
return 1;
}
static cell_t smn_TREnumerateEntities(IPluginContext *pContext, const cell_t *params)
{
IPluginFunction *pFunc = pContext->GetFunctionById(params[5]);
if (!pFunc)
{
return pContext->ThrowNativeError("Invalid function id (%X)", params[5]);
}
cell_t data = 0;
if (params[0] >= 6)
{
data = params[6];
}
g_SMTraceEnumerator.SetFunctionPtr(pFunc, data);
cell_t *startaddr, *endaddr;
pContext->LocalToPhysAddr(params[1], &startaddr);
pContext->LocalToPhysAddr(params[2], &endaddr);
g_StartVec.Init(sp_ctof(startaddr[0]), sp_ctof(startaddr[1]), sp_ctof(startaddr[2]));
switch (params[4])
{
case RayType_EndPoint:
{
g_EndVec.Init(sp_ctof(endaddr[0]), sp_ctof(endaddr[1]), sp_ctof(endaddr[2]));
break;
}
case RayType_Infinite:
{
g_DirAngles.Init(sp_ctof(endaddr[0]), sp_ctof(endaddr[1]), sp_ctof(endaddr[2]));
AngleVectors(g_DirAngles, &g_EndVec);
/* Make it unitary and get the ending point */
g_EndVec.NormalizeInPlace();
g_EndVec = g_StartVec + g_EndVec * MAX_TRACE_LENGTH;
break;
}
}
g_Ray.Init(g_StartVec, g_EndVec);
bool triggers = (params[3]) ? true : false;
enginetrace->EnumerateEntities(g_Ray, triggers, &g_SMTraceEnumerator);
return 1;
}
static cell_t smn_TREnumerateEntitiesHull(IPluginContext *pContext, const cell_t *params)
{
IPluginFunction *pFunc = pContext->GetFunctionById(params[6]);
if (!pFunc)
{
return pContext->ThrowNativeError("Invalid function id (%X)", params[6]);
}
cell_t data = 0;
if (params[0] >= 7)
{
data = params[7];
}
g_SMTraceEnumerator.SetFunctionPtr(pFunc, data);
cell_t *startaddr, *endaddr, *mins, *maxs;
pContext->LocalToPhysAddr(params[1], &startaddr);
pContext->LocalToPhysAddr(params[2], &endaddr);
pContext->LocalToPhysAddr(params[3], &mins);
pContext->LocalToPhysAddr(params[4], &maxs);
g_StartVec.Init(sp_ctof(startaddr[0]), sp_ctof(startaddr[1]), sp_ctof(startaddr[2]));
g_HullMins.Init(sp_ctof(mins[0]), sp_ctof(mins[1]), sp_ctof(mins[2]));
g_HullMaxs.Init(sp_ctof(maxs[0]), sp_ctof(maxs[1]), sp_ctof(maxs[2]));
g_EndVec.Init(sp_ctof(endaddr[0]), sp_ctof(endaddr[1]), sp_ctof(endaddr[2]));
g_Ray.Init(g_StartVec, g_EndVec, g_HullMins, g_HullMaxs);
bool triggers = (params[5]) ? true : false;
enginetrace->EnumerateEntities(g_Ray, triggers, &g_SMTraceEnumerator);
return 1;
}
static cell_t smn_TRClipRayToEntity(IPluginContext *pContext, const cell_t *params)
{
cell_t *startaddr;
pContext->LocalToPhysAddr(params[1], &startaddr);
cell_t *endaddr;
pContext->LocalToPhysAddr(params[2], &endaddr);
g_StartVec.Init(sp_ctof(startaddr[0]), sp_ctof(startaddr[1]), sp_ctof(startaddr[2]));
switch (params[4])
{
case RayType_EndPoint:
{
g_EndVec.Init(sp_ctof(endaddr[0]), sp_ctof(endaddr[1]), sp_ctof(endaddr[2]));
break;
}
case RayType_Infinite:
{
g_DirAngles.Init(sp_ctof(endaddr[0]), sp_ctof(endaddr[1]), sp_ctof(endaddr[2]));
AngleVectors(g_DirAngles, &g_EndVec);
/* Make it unitary and get the ending point */
g_EndVec.NormalizeInPlace();
g_EndVec = g_StartVec + g_EndVec * MAX_TRACE_LENGTH;
break;
}
}
edict_t *pEdict = PEntityOfEntIndex(gamehelpers->ReferenceToIndex(params[5]));
if (!pEdict || pEdict->IsFree())
{
return pContext->ThrowNativeError("Entity %d is invalid", params[5]);
}
IHandleEntity *pEnt = reinterpret_cast<IHandleEntity*>(pEdict->GetUnknown()->GetBaseEntity());
g_Ray.Init(g_StartVec, g_EndVec);
enginetrace->ClipRayToEntity(g_Ray, params[3], pEnt, &g_Trace);
g_Trace.UpdateEntRef();
return 1;
}
static cell_t smn_TRClipRayHullToEntity(IPluginContext *pContext, const cell_t *params)
{
cell_t *startaddr, *endaddr, *mins, *maxs;
pContext->LocalToPhysAddr(params[1], &startaddr);
pContext->LocalToPhysAddr(params[2], &endaddr);
pContext->LocalToPhysAddr(params[3], &mins);
pContext->LocalToPhysAddr(params[4], &maxs);
edict_t *pEdict = PEntityOfEntIndex(gamehelpers->ReferenceToIndex(params[6]));
if (!pEdict || pEdict->IsFree())
{
return pContext->ThrowNativeError("Entity %d is invalid", params[6]);
}
IHandleEntity *pEnt = reinterpret_cast<IHandleEntity*>(pEdict->GetUnknown()->GetBaseEntity());
g_StartVec.Init(sp_ctof(startaddr[0]), sp_ctof(startaddr[1]), sp_ctof(startaddr[2]));
g_HullMins.Init(sp_ctof(mins[0]), sp_ctof(mins[1]), sp_ctof(mins[2]));
g_HullMaxs.Init(sp_ctof(maxs[0]), sp_ctof(maxs[1]), sp_ctof(maxs[2]));
g_EndVec.Init(sp_ctof(endaddr[0]), sp_ctof(endaddr[1]), sp_ctof(endaddr[2]));
g_Ray.Init(g_StartVec, g_EndVec, g_HullMins, g_HullMaxs);
enginetrace->ClipRayToEntity(g_Ray, params[5], pEnt, &g_Trace);
g_Trace.UpdateEntRef();
return 1;
}
static cell_t smn_TRClipCurrentRayToEntity(IPluginContext *pContext, const cell_t *params)
{
edict_t *pEdict = PEntityOfEntIndex(gamehelpers->ReferenceToIndex(params[2]));
if (!pEdict || pEdict->IsFree())
{
return pContext->ThrowNativeError("Entity %d is invalid", params[2]);
}
IHandleEntity *pEnt = reinterpret_cast<IHandleEntity*>(pEdict->GetUnknown()->GetBaseEntity());
enginetrace->ClipRayToEntity(g_Ray, params[1], pEnt, &g_Trace);
g_Trace.UpdateEntRef();
return 1;
}
static cell_t smn_TRTraceRayFilter(IPluginContext *pContext, const cell_t *params)
{
cell_t *startaddr, *endaddr;
@@ -311,6 +503,128 @@ static cell_t smn_TRTraceHullEx(IPluginContext *pContext, const cell_t *params)
return hndl;
}
static cell_t smn_TRClipRayToEntityEx(IPluginContext *pContext, const cell_t *params)
{
cell_t *startaddr;
pContext->LocalToPhysAddr(params[1], &startaddr);
cell_t *endaddr;
pContext->LocalToPhysAddr(params[2], &endaddr);
Vector StartVec, EndVec;
StartVec.Init(sp_ctof(startaddr[0]), sp_ctof(startaddr[1]), sp_ctof(startaddr[2]));
switch (params[4])
{
case RayType_EndPoint:
{
EndVec.Init(sp_ctof(endaddr[0]), sp_ctof(endaddr[1]), sp_ctof(endaddr[2]));
break;
}
case RayType_Infinite:
{
QAngle DirAngles;
DirAngles.Init(sp_ctof(endaddr[0]), sp_ctof(endaddr[1]), sp_ctof(endaddr[2]));
AngleVectors(DirAngles, &EndVec);
/* Make it unitary and get the ending point */
EndVec.NormalizeInPlace();
EndVec = StartVec + EndVec * MAX_TRACE_LENGTH;
break;
}
}
edict_t *pEdict = PEntityOfEntIndex(gamehelpers->ReferenceToIndex(params[5]));
if (!pEdict || pEdict->IsFree())
{
return pContext->ThrowNativeError("Entity %d is invalid", params[5]);
}
Ray_t ray;
sm_trace_t *tr = new sm_trace_t;
IHandleEntity *pEnt = reinterpret_cast<IHandleEntity*>(pEdict->GetUnknown()->GetBaseEntity());
ray.Init(StartVec, EndVec);
enginetrace->ClipRayToEntity(ray, params[3], pEnt, tr);
tr->UpdateEntRef();
HandleError herr;
Handle_t hndl;
if (!(hndl=handlesys->CreateHandle(g_TraceHandle, tr, pContext->GetIdentity(), myself->GetIdentity(), &herr)))
{
delete tr;
return pContext->ThrowNativeError("Unable to create a new trace handle (error %d)", herr);
}
return hndl;
}
static cell_t smn_TRClipRayHullToEntityEx(IPluginContext *pContext, const cell_t *params)
{
cell_t *startaddr, *endaddr, *mins, *maxs;
pContext->LocalToPhysAddr(params[1], &startaddr);
pContext->LocalToPhysAddr(params[2], &endaddr);
pContext->LocalToPhysAddr(params[3], &mins);
pContext->LocalToPhysAddr(params[4], &maxs);
edict_t *pEdict = PEntityOfEntIndex(gamehelpers->ReferenceToIndex(params[6]));
if (!pEdict || pEdict->IsFree())
{
return pContext->ThrowNativeError("Entity %d is invalid", params[6]);
}
IHandleEntity *pEnt = reinterpret_cast<IHandleEntity*>(pEdict->GetUnknown()->GetBaseEntity());
Ray_t ray;
Vector StartVec, EndVec, vmins, vmaxs;
StartVec.Init(sp_ctof(startaddr[0]), sp_ctof(startaddr[1]), sp_ctof(startaddr[2]));
vmins.Init(sp_ctof(mins[0]), sp_ctof(mins[1]), sp_ctof(mins[2]));
vmaxs.Init(sp_ctof(maxs[0]), sp_ctof(maxs[1]), sp_ctof(maxs[2]));
EndVec.Init(sp_ctof(endaddr[0]), sp_ctof(endaddr[1]), sp_ctof(endaddr[2]));
ray.Init(StartVec, EndVec, vmins, vmaxs);
sm_trace_t *tr = new sm_trace_t;
enginetrace->ClipRayToEntity(ray, params[5], pEnt, tr);
tr->UpdateEntRef();
HandleError herr;
Handle_t hndl;
if (!(hndl=handlesys->CreateHandle(g_TraceHandle, tr, pContext->GetIdentity(), myself->GetIdentity(), &herr)))
{
delete tr;
return pContext->ThrowNativeError("Unable to create a new trace handle (error %d)", herr);
}
return hndl;
}
static cell_t smn_TRClipCurrentRayToEntityEx(IPluginContext *pContext, const cell_t *params)
{
edict_t *pEdict = PEntityOfEntIndex(gamehelpers->ReferenceToIndex(params[2]));
if (!pEdict || pEdict->IsFree())
{
return pContext->ThrowNativeError("Entity %d is invalid", params[2]);
}
sm_trace_t *tr = new sm_trace_t;
IHandleEntity *pEnt = reinterpret_cast<IHandleEntity*>(pEdict->GetUnknown()->GetBaseEntity());
enginetrace->ClipRayToEntity(g_Ray, params[1], pEnt, tr);
tr->UpdateEntRef();
HandleError herr;
Handle_t hndl;
if (!(hndl=handlesys->CreateHandle(g_TraceHandle, tr, pContext->GetIdentity(), myself->GetIdentity(), &herr)))
{
delete tr;
return pContext->ThrowNativeError("Unable to create a new trace handle (error %d)", herr);
}
return hndl;
}
static cell_t smn_TRTraceRayFilterEx(IPluginContext *pContext, const cell_t *params)
{
IPluginFunction *pFunc;
@@ -586,38 +900,46 @@ static cell_t smn_TRGetPointContentsEnt(IPluginContext *pContext, const cell_t *
return enginetrace->GetPointContents_Collideable(pEdict->GetCollideable(), pos);
}
static cell_t smn_TRPointOutsideWorld(IPluginContext *pContext, const cell_t *params)
{
cell_t *vec;
Vector pos;
pContext->LocalToPhysAddr(params[1], &vec);
pos.x = sp_ctof(vec[0]);
pos.y = sp_ctof(vec[1]);
pos.z = sp_ctof(vec[2]);
return enginetrace->PointOutsideWorld(pos);
}
sp_nativeinfo_t g_TRNatives[] =
static cell_t smn_TRPointOutsideWorld(IPluginContext *pContext, const cell_t *params)
{
{"TR_TraceRay", smn_TRTraceRay},
{"TR_TraceHull", smn_TRTraceHull},
{"TR_TraceRayEx", smn_TRTraceRayEx},
{"TR_TraceHullEx", smn_TRTraceHullEx},
{"TR_GetFraction", smn_TRGetFraction},
{"TR_GetEndPosition", smn_TRGetEndPosition},
{"TR_GetEntityIndex", smn_TRGetEntityIndex},
{"TR_DidHit", smn_TRDidHit},
{"TR_GetHitGroup", smn_TRGetHitGroup},
{"TR_GetPointContents", smn_TRGetPointContents},
{"TR_GetPointContentsEnt", smn_TRGetPointContentsEnt},
{"TR_TraceRayFilter", smn_TRTraceRayFilter},
{"TR_TraceRayFilterEx", smn_TRTraceRayFilterEx},
{"TR_TraceHullFilter", smn_TRTraceHullFilter},
{"TR_TraceHullFilterEx", smn_TRTraceHullFilterEx},
{"TR_GetPlaneNormal", smn_TRGetPlaneNormal},
{"TR_PointOutsideWorld", smn_TRPointOutsideWorld},
{NULL, NULL}
cell_t *vec;
Vector pos;
pContext->LocalToPhysAddr(params[1], &vec);
pos.x = sp_ctof(vec[0]);
pos.y = sp_ctof(vec[1]);
pos.z = sp_ctof(vec[2]);
return enginetrace->PointOutsideWorld(pos);
}
sp_nativeinfo_t g_TRNatives[] =
{
{"TR_TraceRay", smn_TRTraceRay},
{"TR_TraceHull", smn_TRTraceHull},
{"TR_EnumerateEntities", smn_TREnumerateEntities},
{"TR_EnumerateEntitiesHull", smn_TREnumerateEntitiesHull},
{"TR_TraceRayEx", smn_TRTraceRayEx},
{"TR_TraceHullEx", smn_TRTraceHullEx},
{"TR_GetFraction", smn_TRGetFraction},
{"TR_GetEndPosition", smn_TRGetEndPosition},
{"TR_GetEntityIndex", smn_TRGetEntityIndex},
{"TR_DidHit", smn_TRDidHit},
{"TR_GetHitGroup", smn_TRGetHitGroup},
{"TR_ClipRayToEntity", smn_TRClipRayToEntity},
{"TR_ClipRayToEntityEx", smn_TRClipRayToEntityEx},
{"TR_ClipRayHullToEntity", smn_TRClipRayHullToEntity},
{"TR_ClipRayHullToEntityEx", smn_TRClipRayHullToEntityEx},
{"TR_ClipCurrentRayToEntity", smn_TRClipCurrentRayToEntity},
{"TR_ClipCurrentRayToEntityEx", smn_TRClipCurrentRayToEntityEx},
{"TR_GetPointContents", smn_TRGetPointContents},
{"TR_GetPointContentsEnt", smn_TRGetPointContentsEnt},
{"TR_TraceRayFilter", smn_TRTraceRayFilter},
{"TR_TraceRayFilterEx", smn_TRTraceRayFilterEx},
{"TR_TraceHullFilter", smn_TRTraceHullFilter},
{"TR_TraceHullFilterEx", smn_TRTraceHullFilterEx},
{"TR_GetPlaneNormal", smn_TRGetPlaneNormal},
{"TR_PointOutsideWorld", smn_TRPointOutsideWorld},
{NULL, NULL}
};