From c1190fe89d4d0978e6118de2a80645f122d8d203 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sun, 8 Jul 2007 17:33:10 +0000 Subject: [PATCH] added vector stuff --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401075 --- core/Makefile | 4 +- core/msvc8/sourcemod_mm.vcproj | 10 +- core/smn_vector.cpp | 178 +++++++++++++++++++++++++++++++++ plugins/include/sourcemod.inc | 1 + plugins/include/vector.inc | 156 +++++++++++++++++++++++++++++ 5 files changed, 346 insertions(+), 3 deletions(-) create mode 100644 core/smn_vector.cpp create mode 100644 plugins/include/vector.inc diff --git a/core/Makefile b/core/Makefile index 67ba12ae..dae1fec3 100644 --- a/core/Makefile +++ b/core/Makefile @@ -29,7 +29,7 @@ OBJECTS += smn_admin.cpp smn_bitbuffer.cpp smn_console.cpp smn_core.cpp \ smn_datapacks.cpp smn_entities.cpp smn_events.cpp smn_fakenatives.cpp \ smn_filesystem.cpp smn_float.cpp smn_functions.cpp smn_gameconfigs.cpp smn_halflife.cpp smn_handles.cpp smn_keyvalues.cpp \ smn_lang.cpp smn_player.cpp smn_string.cpp smn_sorting.cpp smn_textparse.cpp smn_timers.cpp \ - smn_usermsgs.cpp smn_menus.cpp smn_database.cpp + smn_usermsgs.cpp smn_menus.cpp smn_database.cpp smn_vector.cpp OBJECTS += systems/ExtensionSys.cpp systems/ForwardSys.cpp systems/HandleSys.cpp \ systems/LibrarySys.cpp systems/PluginInfoDatabase.cpp systems/PluginSys.cpp \ systems/ShareSys.cpp vm/sp_vm_basecontext.cpp vm/sp_vm_engine.cpp \ @@ -38,7 +38,7 @@ OBJECTS_C = zlib/adler32.c zlib/compress.c zlib/crc32.c zlib/deflate.c zlib/gzio zlib/infback.c zlib/inffast.c zlib/inflate.c zlib/inftrees.c zlib/trees.c \ zlib/uncompr.c zlib/zutil.c -LINK = $(HL2LIB)/tier1_i486.a vstdlib_i486.so tier0_i486.so -static-libgcc +LINK = $(HL2LIB)/tier1_i486.a $(HL2LIB)/mathlib_i486.a vstdlib_i486.so tier0_i486.so -static-libgcc INCLUDE = -I. -I.. -I$(HL2PUB) -I$(HL2PUB)/dlls -I$(HL2PUB)/engine -I$(HL2PUB)/tier0 -I$(HL2PUB)/tier1 \ -I$(HL2PUB)/vstdlib -I$(HL2SDK)/tier1 -I$(SMM_TRUNK) -I$(SMM_TRUNK)/sourcehook -I$(SMM_TRUNK)/sourcemm \ diff --git a/core/msvc8/sourcemod_mm.vcproj b/core/msvc8/sourcemod_mm.vcproj index ac982567..fc4e1da1 100644 --- a/core/msvc8/sourcemod_mm.vcproj +++ b/core/msvc8/sourcemod_mm.vcproj @@ -62,7 +62,7 @@ /> + + + + diff --git a/core/smn_vector.cpp b/core/smn_vector.cpp new file mode 100644 index 00000000..35ed64c4 --- /dev/null +++ b/core/smn_vector.cpp @@ -0,0 +1,178 @@ +/** + * vim: set ts=4 : + * =============================================================== + * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved. + * =============================================================== + * + * This file is not open source and may not be copied without explicit + * written permission of AlliedModders LLC. This file may not be redistributed + * in whole or significant part. + * For information, see LICENSE.txt or http://www.sourcemod.net/license.php + * + * Version: $Id$ + */ + +#include "sm_globals.h" +#include +#include + +#define SET_VECTOR(addr, vec) \ + addr[0] = vec.x; \ + addr[1] = vec.y; \ + addr[2] = vec.z; + +static cell_t GetVectorLength(IPluginContext *pContext, const cell_t *params) +{ + cell_t *addr; + + pContext->LocalToPhysAddr(params[1], &addr); + + Vector source(sp_ctof(addr[0]), sp_ctof(addr[1]), sp_ctof(addr[2])); + + if (!params[2]) + { + return sp_ftoc(source.Length()); + } else { + return sp_ftoc(source.LengthSqr()); + } +} + +static cell_t GetVectorDistance(IPluginContext *pContext, const cell_t *params) +{ + cell_t *addr1, *addr2; + + pContext->LocalToPhysAddr(params[1], &addr1); + pContext->LocalToPhysAddr(params[2], &addr2); + + Vector source(sp_ctof(addr1[0]), sp_ctof(addr1[1]), sp_ctof(addr1[2])); + Vector dest(sp_ctof(addr2[0]), sp_ctof(addr2[1]), sp_ctof(addr2[2])); + + if (!params[3]) + { + return sp_ftoc(source.DistTo(dest)); + } else { + return sp_ftoc(source.DistToSqr(dest)); + } +} + +static cell_t GetVectorDotProduct(IPluginContext *pContext, const cell_t *params) +{ + cell_t *addr1, *addr2; + + pContext->LocalToPhysAddr(params[1], &addr1); + pContext->LocalToPhysAddr(params[2], &addr2); + + Vector source(sp_ctof(addr1[0]), sp_ctof(addr1[1]), sp_ctof(addr1[2])); + Vector dest(sp_ctof(addr2[0]), sp_ctof(addr2[1]), sp_ctof(addr2[2])); + + return sp_ftoc(source.Dot(dest)); +} + +static cell_t GetVectorCrossProduct(IPluginContext *pContext, const cell_t *params) +{ + cell_t *addr1, *addr2, *set; + + pContext->LocalToPhysAddr(params[1], &addr1); + pContext->LocalToPhysAddr(params[2], &addr2); + pContext->LocalToPhysAddr(params[3], &set); + + Vector vec1(sp_ctof(addr1[0]), sp_ctof(addr1[1]), sp_ctof(addr1[2])); + Vector vec2(sp_ctof(addr2[0]), sp_ctof(addr2[1]), sp_ctof(addr2[2])); + + Vector vec3 = vec1.Cross(vec2); + + SET_VECTOR(set, vec3); + + return 1; +} + +static cell_t GetAngleVectors(IPluginContext *pContext, const cell_t *params) +{ + cell_t *ang_addr; + + pContext->LocalToPhysAddr(params[1], &ang_addr); + + QAngle angle(sp_ctof(ang_addr[0]), sp_ctof(ang_addr[1]), sp_ctof(ang_addr[2])); + Vector fwd, right, up; + + AngleVectors(angle, &fwd, &right, &up); + + cell_t *addr_fwd, *addr_right, *addr_up; + pContext->LocalToPhysAddr(params[2], &addr_fwd); + pContext->LocalToPhysAddr(params[3], &addr_right); + pContext->LocalToPhysAddr(params[4], &addr_up); + + SET_VECTOR(addr_fwd, fwd); + SET_VECTOR(addr_right, right); + SET_VECTOR(addr_up, up); + + return 1; +} + +static cell_t GetVectorAngles(IPluginContext *pContext, const cell_t *params) +{ + cell_t *vec_addr; + + pContext->LocalToPhysAddr(params[1], &vec_addr); + + Vector vec(sp_ctof(vec_addr[0]), sp_ctof(vec_addr[1]), sp_ctof(vec_addr[2])); + QAngle angle; + + VectorAngles(vec, angle); + + cell_t *ang_addr; + pContext->LocalToPhysAddr(params[2], &ang_addr); + + SET_VECTOR(ang_addr, angle); + + return 1; +} + +static cell_t GetVectorVectors(IPluginContext *pContext, const cell_t *params) +{ + cell_t *vec_addr; + + pContext->LocalToPhysAddr(params[1], &vec_addr); + + Vector vec(sp_ctof(vec_addr[0]), sp_ctof(vec_addr[1]), sp_ctof(vec_addr[2])); + Vector right, up; + + VectorVectors(vec, right, up); + + cell_t *addr_right, *addr_up; + pContext->LocalToPhysAddr(params[2], &addr_right); + pContext->LocalToPhysAddr(params[3], &addr_up); + + SET_VECTOR(addr_right, right); + SET_VECTOR(addr_up, up); + + return 1; +} + +static cell_t NormalizeVector(IPluginContext *pContext, const cell_t *params) +{ + cell_t *addr; + + pContext->LocalToPhysAddr(params[1], &addr); + + Vector source(sp_ctof(addr[0]), sp_ctof(addr[1]), sp_ctof(addr[2])); + float length = VectorNormalize(source); + + pContext->LocalToPhysAddr(params[2], &addr); + SET_VECTOR(addr, source); + + return sp_ftoc(length); +} + +REGISTER_NATIVES(vectorNatives) +{ + {"GetAngleVectors", GetAngleVectors}, + {"GetVectorAngles", GetVectorAngles}, + {"GetVectorCrossProduct", GetVectorCrossProduct}, + {"GetVectorDistance", GetVectorDistance}, + {"GetVectorDotProduct", GetVectorDotProduct}, + {"GetVectorLength", GetVectorLength}, + {"GetVectorVectors", GetVectorVectors}, + {"NormalizeVector", NormalizeVector}, + {NULL, NULL} +}; diff --git a/plugins/include/sourcemod.inc b/plugins/include/sourcemod.inc index 55cfecd2..196774bd 100644 --- a/plugins/include/sourcemod.inc +++ b/plugins/include/sourcemod.inc @@ -32,6 +32,7 @@ struct Plugin #include #include +#include #include #include #include diff --git a/plugins/include/vector.inc b/plugins/include/vector.inc new file mode 100644 index 00000000..14b5c05d --- /dev/null +++ b/plugins/include/vector.inc @@ -0,0 +1,156 @@ +/** + * vim: set ts=4 : + * =============================================================== + * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved. + * =============================================================== + * + * This file is part of the SourceMod/SourcePawn SDK. This file may only be used + * or modified under the Terms and Conditions of its License Agreement, which is found + * in LICENSE.txt. The Terms and Conditions for making SourceMod extensions/plugins + * may change at any time. To view the latest information, see: + * http://www.sourcemod.net/license.php + * + * Version: $Id$ + */ + +#if defined _vector_included + #endinput +#endif +#define _vector_included + +/** + * Calculates a vector's length. + * + * @param vec Vector. + * @param squared If true, the result will be squared (for optimization). + * @return Vector length (magnitude). + */ +native Float:GetVectorLength(const Float:vec[3], bool:squared=false); + +/** + * Calculates the distance between two vectors. + * + * @param vec1 First vector. + * @param vec2 Second vector. + * @param squared If true, the result will be squared (for optimization). + * @return Vector distance. + */ +native Float:GetVectorDistance(const Float:vec1[3], const Float:vec2[3], bool:squared=false); + +/** + * Calculates the dot product of two vectors. + * + * @param vec1 First vector. + * @param vec2 Second vector. + * @return Dot product of the two vectors. + */ +native Float:GetVectorDotProduct(const Float:vec1[3], const Float:vec2[3]); + +/** + * Computes the cross product of two vectors. Any input array can be the same + * as the output array. + * + * @param vec1 First vector. + * @param vec2 Second vector. + * @param result Resultant vector. + * @noreturn + */ +native GetVectorCrossProduct(const Float:vec1[3], const Float:vec2[3], Float:result[3]); + +/** + * Normalizes a vector. The input array can be the same as the output array. + * + * @param vec Vector. + * @param result Resultant vector. + * @return Vector length. + */ +native Float:NormalizeVector(const Float:vec[3], Float:result[3]); + +/** + * Returns vectors in the direction of an angle. + * + * @param angle Angle. + * @param fwd Forward vector buffer or NULL_VECTOR. + * @param right Right vector buffer or NULL_VECTOR. + * @param up Up vector buffer or NULL_VECTOR. + * @noreturn + */ +native GetAngleVectors(const Float:angle[3], Float:fwd[3], Float:right[3], Float:up[3]); + +/** + * Returns angles from a vector. + * + * @param vec Vector. + * @param angle Angle buffer. + * @noreturn + */ +native GetVectorAngles(const Float:vec[3], Float:angle[3]); + +/** + * Returns direction vectors from a vector. + * + * @param vec Vector. + * @param right Right vector buffer or NULL_VECTOR. + * @param up Up vector buffer or NULL_VECTOR. + * @noreturn + */ +native GetVectorVectors(const Float:vec[3], Float:right[3], Float:up[3]); + +/** + * Adds two vectors. It is safe to use either input buffer as an output + * buffer. + * + * @param vec1 First vector. + * @param vec2 Second vector. + * @param result Result buffer. + * @noreturn + */ +stock AddVectors(const Float:vec1[3], const Float:vec2[3], Float:result[3]) +{ + result[0] = vec1[0] + vec2[0]; + result[1] = vec1[1] + vec2[1]; + result[2] = vec1[2] + vec2[2]; +} + +/** + * Subtracts a vector from another vector. It is safe to use either input + * buffer as an output buffer. + * + * @param vec1 First vector. + * @param vec2 Second vector to subtract from first. + * @param result Result buffer. + * @noreturn + */ +stock SubtractVectors(const Float:vec1[3], const Float:vec2[3], Float:result[3]) +{ + result[0] = vec1[0] - vec2[0]; + result[1] = vec1[1] - vec2[1]; + result[2] = vec1[2] - vec2[2]; +} + +/** + * Scales a vector. + * + * @param vec Vector. + * @param scale Scale value. + * @noreturn + */ +stock ScaleVector(Float:vec[3], Float:scale) +{ + vec[0] *= scale; + vec[1] *= scale; + vec[2] *= scale; +} + +/** + * Negatives a vector. + * + * @param vec Vector. + * @noreturn + */ +stock NegateVector(Float:vec[3]) +{ + vec[0] = -vec[0]; + vec[1] = -vec[1]; + vec[2] = -vec[2]; +}