core/smn_vector.cpp
changeset 1069 8e475210cd4d
child 1070 de45bb175165
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/smn_vector.cpp	Sun Jul 08 17:33:10 2007 +0000
     1.3 @@ -0,0 +1,178 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * ===============================================================
     1.7 + * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     1.8 + * ===============================================================
     1.9 + *
    1.10 + * This file is not open source and may not be copied without explicit
    1.11 + * written permission of AlliedModders LLC.  This file may not be redistributed 
    1.12 + * in whole or significant part.
    1.13 + * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    1.14 + *
    1.15 + * Version: $Id$
    1.16 + */
    1.17 +
    1.18 +#include "sm_globals.h"
    1.19 +#include <vector.h>
    1.20 +#include <mathlib.h>
    1.21 +
    1.22 +#define SET_VECTOR(addr, vec) \
    1.23 +	addr[0] = vec.x; \
    1.24 +	addr[1] = vec.y; \
    1.25 +	addr[2] = vec.z; 
    1.26 +
    1.27 +static cell_t GetVectorLength(IPluginContext *pContext, const cell_t *params)
    1.28 +{
    1.29 +	cell_t *addr;
    1.30 +
    1.31 +	pContext->LocalToPhysAddr(params[1], &addr);
    1.32 +
    1.33 +	Vector source(sp_ctof(addr[0]), sp_ctof(addr[1]), sp_ctof(addr[2]));
    1.34 +	
    1.35 +	if (!params[2])
    1.36 +	{
    1.37 +		return sp_ftoc(source.Length());
    1.38 +	} else {
    1.39 +		return sp_ftoc(source.LengthSqr());
    1.40 +	}
    1.41 +}
    1.42 +
    1.43 +static cell_t GetVectorDistance(IPluginContext *pContext, const cell_t *params)
    1.44 +{
    1.45 +	cell_t *addr1, *addr2;
    1.46 +
    1.47 +	pContext->LocalToPhysAddr(params[1], &addr1);
    1.48 +	pContext->LocalToPhysAddr(params[2], &addr2);
    1.49 +
    1.50 +	Vector source(sp_ctof(addr1[0]), sp_ctof(addr1[1]), sp_ctof(addr1[2]));
    1.51 +	Vector dest(sp_ctof(addr2[0]), sp_ctof(addr2[1]), sp_ctof(addr2[2]));
    1.52 +
    1.53 +	if (!params[3])
    1.54 +	{
    1.55 +		return sp_ftoc(source.DistTo(dest));
    1.56 +	} else {
    1.57 +		return sp_ftoc(source.DistToSqr(dest));
    1.58 +	}
    1.59 +}
    1.60 +
    1.61 +static cell_t GetVectorDotProduct(IPluginContext *pContext, const cell_t *params)
    1.62 +{
    1.63 +	cell_t *addr1, *addr2;
    1.64 +
    1.65 +	pContext->LocalToPhysAddr(params[1], &addr1);
    1.66 +	pContext->LocalToPhysAddr(params[2], &addr2);
    1.67 +
    1.68 +	Vector source(sp_ctof(addr1[0]), sp_ctof(addr1[1]), sp_ctof(addr1[2]));
    1.69 +	Vector dest(sp_ctof(addr2[0]), sp_ctof(addr2[1]), sp_ctof(addr2[2]));
    1.70 +
    1.71 +	return sp_ftoc(source.Dot(dest));
    1.72 +}
    1.73 +
    1.74 +static cell_t GetVectorCrossProduct(IPluginContext *pContext, const cell_t *params)
    1.75 +{
    1.76 +	cell_t *addr1, *addr2, *set;
    1.77 +
    1.78 +	pContext->LocalToPhysAddr(params[1], &addr1);
    1.79 +	pContext->LocalToPhysAddr(params[2], &addr2);
    1.80 +	pContext->LocalToPhysAddr(params[3], &set);
    1.81 +
    1.82 +	Vector vec1(sp_ctof(addr1[0]), sp_ctof(addr1[1]), sp_ctof(addr1[2]));
    1.83 +	Vector vec2(sp_ctof(addr2[0]), sp_ctof(addr2[1]), sp_ctof(addr2[2]));
    1.84 +
    1.85 +	Vector vec3 = vec1.Cross(vec2);
    1.86 +
    1.87 +	SET_VECTOR(set, vec3);
    1.88 +
    1.89 +	return 1;
    1.90 +}
    1.91 +
    1.92 +static cell_t GetAngleVectors(IPluginContext *pContext, const cell_t *params)
    1.93 +{
    1.94 +	cell_t *ang_addr;
    1.95 +
    1.96 +	pContext->LocalToPhysAddr(params[1], &ang_addr);
    1.97 +
    1.98 +	QAngle angle(sp_ctof(ang_addr[0]), sp_ctof(ang_addr[1]), sp_ctof(ang_addr[2]));
    1.99 +	Vector fwd, right, up;
   1.100 +
   1.101 +	AngleVectors(angle, &fwd, &right, &up);
   1.102 +
   1.103 +	cell_t *addr_fwd, *addr_right, *addr_up;
   1.104 +	pContext->LocalToPhysAddr(params[2], &addr_fwd);
   1.105 +	pContext->LocalToPhysAddr(params[3], &addr_right);
   1.106 +	pContext->LocalToPhysAddr(params[4], &addr_up);
   1.107 +
   1.108 +	SET_VECTOR(addr_fwd, fwd);
   1.109 +	SET_VECTOR(addr_right, right);
   1.110 +	SET_VECTOR(addr_up, up);
   1.111 +
   1.112 +	return 1;
   1.113 +}
   1.114 +
   1.115 +static cell_t GetVectorAngles(IPluginContext *pContext, const cell_t *params)
   1.116 +{
   1.117 +	cell_t *vec_addr;
   1.118 +
   1.119 +	pContext->LocalToPhysAddr(params[1], &vec_addr);
   1.120 +
   1.121 +	Vector vec(sp_ctof(vec_addr[0]), sp_ctof(vec_addr[1]), sp_ctof(vec_addr[2]));
   1.122 +	QAngle angle;
   1.123 +
   1.124 +	VectorAngles(vec, angle);
   1.125 +
   1.126 +	cell_t *ang_addr;
   1.127 +	pContext->LocalToPhysAddr(params[2], &ang_addr);
   1.128 +
   1.129 +	SET_VECTOR(ang_addr, angle);
   1.130 +
   1.131 +	return 1;
   1.132 +}
   1.133 +
   1.134 +static cell_t GetVectorVectors(IPluginContext *pContext, const cell_t *params)
   1.135 +{
   1.136 +	cell_t *vec_addr;
   1.137 +
   1.138 +	pContext->LocalToPhysAddr(params[1], &vec_addr);
   1.139 +
   1.140 +	Vector vec(sp_ctof(vec_addr[0]), sp_ctof(vec_addr[1]), sp_ctof(vec_addr[2]));
   1.141 +	Vector right, up;
   1.142 +
   1.143 +	VectorVectors(vec, right, up);
   1.144 +
   1.145 +	cell_t *addr_right, *addr_up;
   1.146 +	pContext->LocalToPhysAddr(params[2], &addr_right);
   1.147 +	pContext->LocalToPhysAddr(params[3], &addr_up);
   1.148 +
   1.149 +	SET_VECTOR(addr_right, right);
   1.150 +	SET_VECTOR(addr_up, up);
   1.151 +
   1.152 +	return 1;
   1.153 +}
   1.154 +
   1.155 +static cell_t NormalizeVector(IPluginContext *pContext, const cell_t *params)
   1.156 +{
   1.157 +	cell_t *addr;
   1.158 +
   1.159 +	pContext->LocalToPhysAddr(params[1], &addr);
   1.160 +
   1.161 +	Vector source(sp_ctof(addr[0]), sp_ctof(addr[1]), sp_ctof(addr[2]));
   1.162 +	float length = VectorNormalize(source);
   1.163 +
   1.164 +	pContext->LocalToPhysAddr(params[2], &addr);
   1.165 +	SET_VECTOR(addr, source);
   1.166 +
   1.167 +	return sp_ftoc(length);
   1.168 +}
   1.169 +
   1.170 +REGISTER_NATIVES(vectorNatives)
   1.171 +{
   1.172 +	{"GetAngleVectors",			GetAngleVectors},
   1.173 +	{"GetVectorAngles",			GetVectorAngles},
   1.174 +	{"GetVectorCrossProduct",	GetVectorCrossProduct},
   1.175 +	{"GetVectorDistance",		GetVectorDistance},
   1.176 +	{"GetVectorDotProduct",		GetVectorDotProduct},
   1.177 +	{"GetVectorLength",			GetVectorLength},
   1.178 +	{"GetVectorVectors",		GetVectorVectors},
   1.179 +	{"NormalizeVector",			NormalizeVector},
   1.180 +	{NULL,					NULL}
   1.181 +};