Updated for latest hl2sdk-dota changes.
3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #include "sm_globals.h"
34 #if SOURCE_ENGINE >= SE_ORANGEBOX
37 #include <math_base.h>
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);
45 static cell_t GetVectorLength(IPluginContext *pContext, const cell_t *params)
49 pContext->LocalToPhysAddr(params[1], &addr);
51 Vector source(sp_ctof(addr[0]), sp_ctof(addr[1]), sp_ctof(addr[2]));
55 return sp_ftoc(source.Length());
57 return sp_ftoc(source.LengthSqr());
61 static cell_t GetVectorDistance(IPluginContext *pContext, const cell_t *params)
63 cell_t *addr1, *addr2;
65 pContext->LocalToPhysAddr(params[1], &addr1);
66 pContext->LocalToPhysAddr(params[2], &addr2);
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]));
73 return sp_ftoc(source.DistTo(dest));
75 return sp_ftoc(source.DistToSqr(dest));
79 static cell_t GetVectorDotProduct(IPluginContext *pContext, const cell_t *params)
81 cell_t *addr1, *addr2;
83 pContext->LocalToPhysAddr(params[1], &addr1);
84 pContext->LocalToPhysAddr(params[2], &addr2);
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]));
89 return sp_ftoc(source.Dot(dest));
92 static cell_t GetVectorCrossProduct(IPluginContext *pContext, const cell_t *params)
94 cell_t *addr1, *addr2, *set;
96 pContext->LocalToPhysAddr(params[1], &addr1);
97 pContext->LocalToPhysAddr(params[2], &addr2);
98 pContext->LocalToPhysAddr(params[3], &set);
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]));
103 Vector vec3 = vec1.Cross(vec2);
105 SET_VECTOR(set, vec3);
110 static cell_t GetAngleVectors(IPluginContext *pContext, const cell_t *params)
114 pContext->LocalToPhysAddr(params[1], &ang_addr);
116 QAngle angle(sp_ctof(ang_addr[0]), sp_ctof(ang_addr[1]), sp_ctof(ang_addr[2]));
117 Vector fwd, right, up;
119 AngleVectors(angle, &fwd, &right, &up);
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);
126 SET_VECTOR(addr_fwd, fwd);
127 SET_VECTOR(addr_right, right);
128 SET_VECTOR(addr_up, up);
133 static cell_t GetVectorAngles(IPluginContext *pContext, const cell_t *params)
137 pContext->LocalToPhysAddr(params[1], &vec_addr);
139 Vector vec(sp_ctof(vec_addr[0]), sp_ctof(vec_addr[1]), sp_ctof(vec_addr[2]));
142 VectorAngles(vec, angle);
145 pContext->LocalToPhysAddr(params[2], &ang_addr);
147 SET_VECTOR(ang_addr, angle);
152 static cell_t GetVectorVectors(IPluginContext *pContext, const cell_t *params)
156 pContext->LocalToPhysAddr(params[1], &vec_addr);
158 Vector vec(sp_ctof(vec_addr[0]), sp_ctof(vec_addr[1]), sp_ctof(vec_addr[2]));
161 VectorVectors(vec, right, up);
163 cell_t *addr_right, *addr_up;
164 pContext->LocalToPhysAddr(params[2], &addr_right);
165 pContext->LocalToPhysAddr(params[3], &addr_up);
167 SET_VECTOR(addr_right, right);
168 SET_VECTOR(addr_up, up);
173 static cell_t NormalizeVector(IPluginContext *pContext, const cell_t *params)
177 pContext->LocalToPhysAddr(params[1], &addr);
179 Vector source(sp_ctof(addr[0]), sp_ctof(addr[1]), sp_ctof(addr[2]));
180 float length = VectorNormalize(source);
182 pContext->LocalToPhysAddr(params[2], &addr);
183 SET_VECTOR(addr, source);
185 return sp_ftoc(length);
188 REGISTER_NATIVES(vectorNatives)
190 {"GetAngleVectors", GetAngleVectors},
191 {"GetVectorAngles", GetVectorAngles},
192 {"GetVectorCrossProduct", GetVectorCrossProduct},
193 {"GetVectorDistance", GetVectorDistance},
194 {"GetVectorDotProduct", GetVectorDotProduct},
195 {"GetVectorLength", GetVectorLength},
196 {"GetVectorVectors", GetVectorVectors},
197 {"NormalizeVector", NormalizeVector},