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