1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/sdktools/trnatives.cpp Sun Jul 08 19:39:19 2007 +0000
1.3 @@ -0,0 +1,386 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * ===============================================================
1.7 + * SourceMod SDK Tools Extension
1.8 + * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
1.9 + * ===============================================================
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or
1.12 + * modify it under the terms of the GNU General Public License
1.13 + * as published by the Free Software Foundation; either version 2
1.14 + * of the License, or (at your option) any later version.
1.15 + * This program is distributed in the hope that it will be useful,
1.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.18 + * GNU General Public License for more details.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License
1.21 + * along with this program; if not, write to the Free Software
1.22 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1.23 + *
1.24 + * Version: $Id$
1.25 + */
1.26 +
1.27 +#include "extension.h"
1.28 +#include <worldsize.h>
1.29 +
1.30 +class CSMTraceFilter : public CTraceFilter
1.31 +{
1.32 +public:
1.33 + bool ShouldHitEntity(IHandleEntity *pEntity, int contentsMask)
1.34 + {
1.35 + cell_t res = 1;
1.36 + edict_t *pEdict = gameents->BaseEntityToEdict(reinterpret_cast<CBaseEntity *>(pEntity));
1.37 + m_pFunc->PushCell(engine->IndexOfEdict(pEdict));
1.38 + m_pFunc->PushCell(contentsMask);
1.39 + m_pFunc->Execute(&res);
1.40 +
1.41 + return (res) ? true : false;
1.42 + }
1.43 + void SetFunctionPtr(IPluginFunction *pFunc)
1.44 + {
1.45 + m_pFunc = pFunc;
1.46 + }
1.47 +private:
1.48 + IPluginFunction *m_pFunc;
1.49 +};
1.50 +
1.51 +/* Used for the global trace version */
1.52 +Ray_t g_Ray;
1.53 +trace_t g_Trace;
1.54 +Vector g_StartVec;
1.55 +Vector g_EndVec;
1.56 +QAngle g_DirAngles;
1.57 +CTraceFilterHitAll g_HitAllFilter;
1.58 +CSMTraceFilter g_SMTraceFilter;
1.59 +
1.60 +enum
1.61 +{
1.62 + RayType_EndPoint,
1.63 + RayType_Infinite
1.64 +};
1.65 +
1.66 +static cell_t smn_TRTraceRay(IPluginContext *pContext, const cell_t *params)
1.67 +{
1.68 + cell_t *startaddr, *endaddr;
1.69 + pContext->LocalToPhysAddr(params[1], &startaddr);
1.70 + pContext->LocalToPhysAddr(params[2], &endaddr);
1.71 +
1.72 + g_StartVec.Init(sp_ctof(startaddr[0]), sp_ctof(startaddr[1]), sp_ctof(startaddr[2]));
1.73 +
1.74 + switch (params[4])
1.75 + {
1.76 + case RayType_EndPoint:
1.77 + {
1.78 + g_EndVec.Init(sp_ctof(endaddr[0]), sp_ctof(endaddr[1]), sp_ctof(endaddr[2]));
1.79 + break;
1.80 + }
1.81 + case RayType_Infinite:
1.82 + {
1.83 + g_DirAngles.Init(sp_ctof(endaddr[0]), sp_ctof(endaddr[1]), sp_ctof(endaddr[2]));
1.84 + AngleVectors(g_DirAngles, &g_EndVec);
1.85 +
1.86 + /* Make it unitary and get the ending point */
1.87 + g_EndVec.NormalizeInPlace();
1.88 + g_EndVec = g_StartVec + g_EndVec * MAX_TRACE_LENGTH;
1.89 + break;
1.90 + }
1.91 + }
1.92 +
1.93 + g_Ray.Init(g_StartVec, g_EndVec);
1.94 + enginetrace->TraceRay(g_Ray, params[3], &g_HitAllFilter, &g_Trace);
1.95 +
1.96 + return 1;
1.97 +}
1.98 +
1.99 +static cell_t smn_TRTraceRayFilter(IPluginContext *pContext, const cell_t *params)
1.100 +{
1.101 + cell_t *startaddr, *endaddr;
1.102 + IPluginFunction *pFunc;
1.103 +
1.104 + pFunc = pContext->GetFunctionById(params[5]);
1.105 + if (!pFunc)
1.106 + {
1.107 + return pContext->ThrowNativeError("Invalid function id (%X)", params[5]);
1.108 + }
1.109 + g_SMTraceFilter.SetFunctionPtr(pFunc);
1.110 + pContext->LocalToPhysAddr(params[1], &startaddr);
1.111 + pContext->LocalToPhysAddr(params[2], &endaddr);
1.112 +
1.113 + g_StartVec.Init(sp_ctof(startaddr[0]), sp_ctof(startaddr[1]), sp_ctof(startaddr[2]));
1.114 +
1.115 + switch (params[4])
1.116 + {
1.117 + case RayType_EndPoint:
1.118 + {
1.119 + g_EndVec.Init(sp_ctof(endaddr[0]), sp_ctof(endaddr[1]), sp_ctof(endaddr[2]));
1.120 + break;
1.121 + }
1.122 + case RayType_Infinite:
1.123 + {
1.124 + g_DirAngles.Init(sp_ctof(endaddr[0]), sp_ctof(endaddr[1]), sp_ctof(endaddr[2]));
1.125 + AngleVectors(g_DirAngles, &g_EndVec);
1.126 +
1.127 + /* Make it unitary and get the ending point */
1.128 + g_EndVec.NormalizeInPlace();
1.129 + g_EndVec = g_StartVec + g_EndVec * MAX_TRACE_LENGTH;
1.130 + break;
1.131 + }
1.132 + }
1.133 +
1.134 + g_Ray.Init(g_StartVec, g_EndVec);
1.135 + enginetrace->TraceRay(g_Ray, params[3], &g_SMTraceFilter, &g_Trace);
1.136 +
1.137 + return 1;
1.138 +}
1.139 +
1.140 +static cell_t smn_TRTraceRayEx(IPluginContext *pContext, const cell_t *params)
1.141 +{
1.142 + cell_t *startaddr, *endaddr;
1.143 + pContext->LocalToPhysAddr(params[1], &startaddr);
1.144 + pContext->LocalToPhysAddr(params[2], &endaddr);
1.145 +
1.146 + Vector StartVec, EndVec;
1.147 + Ray_t ray;
1.148 +
1.149 + StartVec.Init(sp_ctof(startaddr[0]), sp_ctof(startaddr[1]), sp_ctof(startaddr[2]));
1.150 +
1.151 + switch (params[4])
1.152 + {
1.153 + case RayType_EndPoint:
1.154 + {
1.155 + EndVec.Init(sp_ctof(endaddr[0]), sp_ctof(endaddr[1]), sp_ctof(endaddr[2]));
1.156 + break;
1.157 + }
1.158 + case RayType_Infinite:
1.159 + {
1.160 + QAngle DirAngles;
1.161 + DirAngles.Init(sp_ctof(endaddr[0]), sp_ctof(endaddr[1]), sp_ctof(endaddr[2]));
1.162 + AngleVectors(DirAngles, &EndVec);
1.163 +
1.164 + /* Make it unitary and get the ending point */
1.165 + EndVec.NormalizeInPlace();
1.166 + EndVec = StartVec + EndVec * MAX_TRACE_LENGTH;
1.167 + break;
1.168 + }
1.169 + }
1.170 +
1.171 + trace_t *tr = new trace_t;
1.172 + ray.Init(StartVec, EndVec);
1.173 + enginetrace->TraceRay(ray, params[3], &g_HitAllFilter, tr);
1.174 +
1.175 + HandleError herr;
1.176 + Handle_t hndl;
1.177 + if (!(hndl=handlesys->CreateHandle(g_TraceHandle, tr, pContext->GetIdentity(), myself->GetIdentity(), &herr)))
1.178 + {
1.179 + delete tr;
1.180 + return pContext->ThrowNativeError("Unable to create a new trace handle (error %d)", herr);
1.181 + }
1.182 +
1.183 + return hndl;
1.184 +}
1.185 +
1.186 +static cell_t smn_TRTraceRayFilterEx(IPluginContext *pContext, const cell_t *params)
1.187 +{
1.188 + IPluginFunction *pFunc;
1.189 + cell_t *startaddr, *endaddr;
1.190 +
1.191 + pFunc = pContext->GetFunctionById(params[5]);
1.192 + if (!pFunc)
1.193 + {
1.194 + return pContext->ThrowNativeError("Invalid function id (%X)", params[5]);
1.195 + }
1.196 + pContext->LocalToPhysAddr(params[1], &startaddr);
1.197 + pContext->LocalToPhysAddr(params[2], &endaddr);
1.198 +
1.199 + Vector StartVec, EndVec;
1.200 + CSMTraceFilter smfilter;
1.201 + Ray_t ray;
1.202 +
1.203 + smfilter.SetFunctionPtr(pFunc);
1.204 + StartVec.Init(sp_ctof(startaddr[0]), sp_ctof(startaddr[1]), sp_ctof(startaddr[2]));
1.205 +
1.206 + switch (params[4])
1.207 + {
1.208 + case RayType_EndPoint:
1.209 + {
1.210 + EndVec.Init(sp_ctof(endaddr[0]), sp_ctof(endaddr[1]), sp_ctof(endaddr[2]));
1.211 + break;
1.212 + }
1.213 + case RayType_Infinite:
1.214 + {
1.215 + QAngle DirAngles;
1.216 + DirAngles.Init(sp_ctof(endaddr[0]), sp_ctof(endaddr[1]), sp_ctof(endaddr[2]));
1.217 + AngleVectors(DirAngles, &EndVec);
1.218 +
1.219 + /* Make it unitary and get the ending point */
1.220 + EndVec.NormalizeInPlace();
1.221 + EndVec = StartVec + EndVec * MAX_TRACE_LENGTH;
1.222 + break;
1.223 + }
1.224 + }
1.225 +
1.226 + trace_t *tr = new trace_t;
1.227 + ray.Init(StartVec, EndVec);
1.228 + enginetrace->TraceRay(ray, params[3], &smfilter, tr);
1.229 +
1.230 + HandleError herr;
1.231 + Handle_t hndl;
1.232 + if (!(hndl=handlesys->CreateHandle(g_TraceHandle, tr, pContext->GetIdentity(), myself->GetIdentity(), &herr)))
1.233 + {
1.234 + delete tr;
1.235 + return pContext->ThrowNativeError("Unable to create a new trace handle (error %d)", herr);
1.236 + }
1.237 +
1.238 + return hndl;
1.239 +}
1.240 +
1.241 +static cell_t smn_TRGetFraction(IPluginContext *pContext, const cell_t *params)
1.242 +{
1.243 + trace_t *tr;
1.244 + HandleError err;
1.245 + HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
1.246 +
1.247 + if (params[1] == BAD_HANDLE)
1.248 + {
1.249 + tr = &g_Trace;
1.250 + } else if ((err = handlesys->ReadHandle(params[1], g_TraceHandle, &sec, (void **)&tr)) != HandleError_None) {
1.251 + return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
1.252 + }
1.253 +
1.254 + return sp_ftoc(tr->fraction);
1.255 +}
1.256 +
1.257 +static cell_t smn_TRGetEndPosition(IPluginContext *pContext, const cell_t *params)
1.258 +{
1.259 + trace_t *tr;
1.260 + HandleError err;
1.261 + HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
1.262 +
1.263 + if (params[2] == BAD_HANDLE)
1.264 + {
1.265 + tr = &g_Trace;
1.266 + } else if ((err = handlesys->ReadHandle(params[2], g_TraceHandle, &sec, (void **)&tr)) != HandleError_None) {
1.267 + return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[2], err);
1.268 + }
1.269 +
1.270 + cell_t *addr;
1.271 + pContext->LocalToPhysAddr(params[1], &addr);
1.272 +
1.273 + addr[0] = sp_ftoc(tr->endpos.x);
1.274 + addr[1] = sp_ftoc(tr->endpos.y);
1.275 + addr[2] = sp_ftoc(tr->endpos.z);
1.276 +
1.277 + return 1;
1.278 +}
1.279 +
1.280 +static cell_t smn_TRDidHit(IPluginContext *pContext, const cell_t *params)
1.281 +{
1.282 + trace_t *tr;
1.283 + HandleError err;
1.284 + HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
1.285 +
1.286 + if (params[1] == BAD_HANDLE)
1.287 + {
1.288 + tr = &g_Trace;
1.289 + } else if ((err = handlesys->ReadHandle(params[1], g_TraceHandle, &sec, (void **)&tr)) != HandleError_None) {
1.290 + return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
1.291 + }
1.292 +
1.293 + return tr->DidHit() ? 1 : 0;
1.294 +}
1.295 +
1.296 +static cell_t smn_TRGetHitGroup(IPluginContext *pContext, const cell_t *params)
1.297 +{
1.298 + trace_t *tr;
1.299 + HandleError err;
1.300 + HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
1.301 +
1.302 + if (params[1] == BAD_HANDLE)
1.303 + {
1.304 + tr = &g_Trace;
1.305 + } else if ((err = handlesys->ReadHandle(params[1], g_TraceHandle, &sec, (void **)&tr)) != HandleError_None) {
1.306 + return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
1.307 + }
1.308 +
1.309 + return tr->hitgroup;
1.310 +}
1.311 +
1.312 +static cell_t smn_TRGetEntityIndex(IPluginContext *pContext, const cell_t *params)
1.313 +{
1.314 + trace_t *tr;
1.315 + HandleError err;
1.316 + HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
1.317 +
1.318 + if (params[1] == BAD_HANDLE)
1.319 + {
1.320 + tr = &g_Trace;
1.321 + } else if ((err = handlesys->ReadHandle(params[1], g_TraceHandle, &sec, (void **)&tr)) != HandleError_None) {
1.322 + return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
1.323 + }
1.324 +
1.325 + edict_t *pEdict = gameents->BaseEntityToEdict(tr->m_pEnt);
1.326 + return engine->IndexOfEdict(pEdict);
1.327 +}
1.328 +
1.329 +static cell_t smn_TRGetPointContents(IPluginContext *pContext, const cell_t *params)
1.330 +{
1.331 + cell_t *vec, *ent;
1.332 + IHandleEntity *hentity;
1.333 + Vector pos;
1.334 + int mask;
1.335 +
1.336 + pContext->LocalToPhysAddr(params[1], &vec);
1.337 + pContext->LocalToPhysAddr(params[2], &ent);
1.338 +
1.339 + pos.x = sp_ctof(vec[0]);
1.340 + pos.y = sp_ctof(vec[1]);
1.341 + pos.z = sp_ctof(vec[2]);
1.342 +
1.343 + if (*ent == -1)
1.344 + {
1.345 + mask = enginetrace->GetPointContents(pos);
1.346 + } else {
1.347 + mask = enginetrace->GetPointContents(pos, &hentity);
1.348 + edict_t *pEdict = gameents->BaseEntityToEdict(reinterpret_cast<CBaseEntity *>(hentity));
1.349 + *ent = engine->IndexOfEdict(pEdict);
1.350 + }
1.351 +
1.352 + return mask;
1.353 +}
1.354 +
1.355 +static cell_t smn_TRGetPointContentsEnt(IPluginContext *pContext, const cell_t *params)
1.356 +{
1.357 + edict_t *pEdict = engine->PEntityOfEntIndex(params[1]);
1.358 + if (!pEdict || pEdict->IsFree())
1.359 + {
1.360 + return pContext->ThrowNativeError("Entity %d is invalid", params[1]);
1.361 + }
1.362 +
1.363 + cell_t *addr;
1.364 + Vector pos;
1.365 +
1.366 + pContext->LocalToPhysAddr(params[2], &addr);
1.367 +
1.368 + pos.x = sp_ctof(addr[0]);
1.369 + pos.y = sp_ctof(addr[1]);
1.370 + pos.z = sp_ctof(addr[2]);
1.371 +
1.372 + return enginetrace->GetPointContents_Collideable(pEdict->GetCollideable(), pos);
1.373 +}
1.374 +
1.375 +sp_nativeinfo_t g_TRNatives[] =
1.376 +{
1.377 + {"TR_TraceRay", smn_TRTraceRay},
1.378 + {"TR_TraceRayEx", smn_TRTraceRayEx},
1.379 + {"TR_GetFraction", smn_TRGetFraction},
1.380 + {"TR_GetEndPosition", smn_TRGetEndPosition},
1.381 + {"TR_GetEntityIndex", smn_TRGetEntityIndex},
1.382 + {"TR_DidHit", smn_TRDidHit},
1.383 + {"TR_GetHitGroup", smn_TRGetHitGroup},
1.384 + {"TR_GetPointContents", smn_TRGetPointContents},
1.385 + {"TR_GetPointContentsEnt", smn_TRGetPointContentsEnt},
1.386 + {"TR_TraceRayFilter", smn_TRTraceRayFilter},
1.387 + {"TR_TraceRayFilterEx", smn_TRTraceRayFilterEx},
1.388 + {NULL, NULL}
1.389 +};