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