core/smn_vector.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Nov 06 16:24:40 2009 -0600 (2009-11-06)
changeset 2863 d740d9cdf53a
parent 2400 76b7b4575e21
permissions -rw-r--r--
Added IMemoryUtils::ResolveSymbol() for resolving symbols hidden via GCC's -fvisibility=hidden (bug 4091, r=dvander,pred).

Right now, this is only used on L4D2 where it is needed.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "sm_globals.h"
    33 #include <vector.h>
    34 #if SOURCE_ENGINE >= SE_ORANGEBOX
    35 #include <mathlib.h>
    36 #else
    37 #include <math_base.h>
    38 #endif
    39 
    40 #define SET_VECTOR(addr, vec) \
    41 	addr[0] = sp_ftoc(vec.x); \
    42 	addr[1] = sp_ftoc(vec.y); \
    43 	addr[2] = sp_ftoc(vec.z); 
    44 
    45 static cell_t GetVectorLength(IPluginContext *pContext, const cell_t *params)
    46 {
    47 	cell_t *addr;
    48 
    49 	pContext->LocalToPhysAddr(params[1], &addr);
    50 
    51 	Vector source(sp_ctof(addr[0]), sp_ctof(addr[1]), sp_ctof(addr[2]));
    52 	
    53 	if (!params[2])
    54 	{
    55 		return sp_ftoc(source.Length());
    56 	} else {
    57 		return sp_ftoc(source.LengthSqr());
    58 	}
    59 }
    60 
    61 static cell_t GetVectorDistance(IPluginContext *pContext, const cell_t *params)
    62 {
    63 	cell_t *addr1, *addr2;
    64 
    65 	pContext->LocalToPhysAddr(params[1], &addr1);
    66 	pContext->LocalToPhysAddr(params[2], &addr2);
    67 
    68 	Vector source(sp_ctof(addr1[0]), sp_ctof(addr1[1]), sp_ctof(addr1[2]));
    69 	Vector dest(sp_ctof(addr2[0]), sp_ctof(addr2[1]), sp_ctof(addr2[2]));
    70 
    71 	if (!params[3])
    72 	{
    73 		return sp_ftoc(source.DistTo(dest));
    74 	} else {
    75 		return sp_ftoc(source.DistToSqr(dest));
    76 	}
    77 }
    78 
    79 static cell_t GetVectorDotProduct(IPluginContext *pContext, const cell_t *params)
    80 {
    81 	cell_t *addr1, *addr2;
    82 
    83 	pContext->LocalToPhysAddr(params[1], &addr1);
    84 	pContext->LocalToPhysAddr(params[2], &addr2);
    85 
    86 	Vector source(sp_ctof(addr1[0]), sp_ctof(addr1[1]), sp_ctof(addr1[2]));
    87 	Vector dest(sp_ctof(addr2[0]), sp_ctof(addr2[1]), sp_ctof(addr2[2]));
    88 
    89 	return sp_ftoc(source.Dot(dest));
    90 }
    91 
    92 static cell_t GetVectorCrossProduct(IPluginContext *pContext, const cell_t *params)
    93 {
    94 	cell_t *addr1, *addr2, *set;
    95 
    96 	pContext->LocalToPhysAddr(params[1], &addr1);
    97 	pContext->LocalToPhysAddr(params[2], &addr2);
    98 	pContext->LocalToPhysAddr(params[3], &set);
    99 
   100 	Vector vec1(sp_ctof(addr1[0]), sp_ctof(addr1[1]), sp_ctof(addr1[2]));
   101 	Vector vec2(sp_ctof(addr2[0]), sp_ctof(addr2[1]), sp_ctof(addr2[2]));
   102 
   103 	Vector vec3 = vec1.Cross(vec2);
   104 
   105 	SET_VECTOR(set, vec3);
   106 
   107 	return 1;
   108 }
   109 
   110 static cell_t GetAngleVectors(IPluginContext *pContext, const cell_t *params)
   111 {
   112 	cell_t *ang_addr;
   113 
   114 	pContext->LocalToPhysAddr(params[1], &ang_addr);
   115 
   116 	QAngle angle(sp_ctof(ang_addr[0]), sp_ctof(ang_addr[1]), sp_ctof(ang_addr[2]));
   117 	Vector fwd, right, up;
   118 
   119 	AngleVectors(angle, &fwd, &right, &up);
   120 
   121 	cell_t *addr_fwd, *addr_right, *addr_up;
   122 	pContext->LocalToPhysAddr(params[2], &addr_fwd);
   123 	pContext->LocalToPhysAddr(params[3], &addr_right);
   124 	pContext->LocalToPhysAddr(params[4], &addr_up);
   125 
   126 	SET_VECTOR(addr_fwd, fwd);
   127 	SET_VECTOR(addr_right, right);
   128 	SET_VECTOR(addr_up, up);
   129 
   130 	return 1;
   131 }
   132 
   133 static cell_t GetVectorAngles(IPluginContext *pContext, const cell_t *params)
   134 {
   135 	cell_t *vec_addr;
   136 
   137 	pContext->LocalToPhysAddr(params[1], &vec_addr);
   138 
   139 	Vector vec(sp_ctof(vec_addr[0]), sp_ctof(vec_addr[1]), sp_ctof(vec_addr[2]));
   140 	QAngle angle;
   141 
   142 	VectorAngles(vec, angle);
   143 
   144 	cell_t *ang_addr;
   145 	pContext->LocalToPhysAddr(params[2], &ang_addr);
   146 
   147 	SET_VECTOR(ang_addr, angle);
   148 
   149 	return 1;
   150 }
   151 
   152 static cell_t GetVectorVectors(IPluginContext *pContext, const cell_t *params)
   153 {
   154 	cell_t *vec_addr;
   155 
   156 	pContext->LocalToPhysAddr(params[1], &vec_addr);
   157 
   158 	Vector vec(sp_ctof(vec_addr[0]), sp_ctof(vec_addr[1]), sp_ctof(vec_addr[2]));
   159 	Vector right, up;
   160 
   161 	VectorVectors(vec, right, up);
   162 
   163 	cell_t *addr_right, *addr_up;
   164 	pContext->LocalToPhysAddr(params[2], &addr_right);
   165 	pContext->LocalToPhysAddr(params[3], &addr_up);
   166 
   167 	SET_VECTOR(addr_right, right);
   168 	SET_VECTOR(addr_up, up);
   169 
   170 	return 1;
   171 }
   172 
   173 static cell_t NormalizeVector(IPluginContext *pContext, const cell_t *params)
   174 {
   175 	cell_t *addr;
   176 
   177 	pContext->LocalToPhysAddr(params[1], &addr);
   178 
   179 	Vector source(sp_ctof(addr[0]), sp_ctof(addr[1]), sp_ctof(addr[2]));
   180 	float length = VectorNormalize(source);
   181 
   182 	pContext->LocalToPhysAddr(params[2], &addr);
   183 	SET_VECTOR(addr, source);
   184 
   185 	return sp_ftoc(length);
   186 }
   187 
   188 REGISTER_NATIVES(vectorNatives)
   189 {
   190 	{"GetAngleVectors",			GetAngleVectors},
   191 	{"GetVectorAngles",			GetVectorAngles},
   192 	{"GetVectorCrossProduct",	GetVectorCrossProduct},
   193 	{"GetVectorDistance",		GetVectorDistance},
   194 	{"GetVectorDotProduct",		GetVectorDotProduct},
   195 	{"GetVectorLength",			GetVectorLength},
   196 	{"GetVectorVectors",		GetVectorVectors},
   197 	{"NormalizeVector",			NormalizeVector},
   198 	{NULL,					NULL}
   199 };