ds@1952: /**
ds@1952: * vim: set ts=4 :
ds@1952: * =============================================================================
ds@1952: * SourceMod
ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
ds@1952: * =============================================================================
ds@1952: *
ds@1952: * This program is free software; you can redistribute it and/or modify it under
ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952: * Free Software Foundation.
ds@1952: *
ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1952: * details.
ds@1952: *
ds@1952: * You should have received a copy of the GNU General Public License along with
ds@1952: * this program. If not, see .
ds@1952: *
ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1952: * this exception to all derivative works. AlliedModders LLC defines further
ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952: * or .
ds@1952: *
ds@1952: * Version: $Id$
ds@1952: */
ds@1952:
ds@1952: #include "sm_globals.h"
ds@1952: #include
ds@1952: #if defined ORANGEBOX_BUILD
ds@1952: #include
ds@1952: #else
ds@1952: #include
ds@1952: #endif
ds@1952:
ds@1952: #define SET_VECTOR(addr, vec) \
ds@1952: addr[0] = sp_ftoc(vec.x); \
ds@1952: addr[1] = sp_ftoc(vec.y); \
ds@1952: addr[2] = sp_ftoc(vec.z);
ds@1952:
ds@1952: static cell_t GetVectorLength(IPluginContext *pContext, const cell_t *params)
ds@1952: {
ds@1952: cell_t *addr;
ds@1952:
ds@1952: pContext->LocalToPhysAddr(params[1], &addr);
ds@1952:
ds@1952: Vector source(sp_ctof(addr[0]), sp_ctof(addr[1]), sp_ctof(addr[2]));
ds@1952:
ds@1952: if (!params[2])
ds@1952: {
ds@1952: return sp_ftoc(source.Length());
ds@1952: } else {
ds@1952: return sp_ftoc(source.LengthSqr());
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: static cell_t GetVectorDistance(IPluginContext *pContext, const cell_t *params)
ds@1952: {
ds@1952: cell_t *addr1, *addr2;
ds@1952:
ds@1952: pContext->LocalToPhysAddr(params[1], &addr1);
ds@1952: pContext->LocalToPhysAddr(params[2], &addr2);
ds@1952:
ds@1952: Vector source(sp_ctof(addr1[0]), sp_ctof(addr1[1]), sp_ctof(addr1[2]));
ds@1952: Vector dest(sp_ctof(addr2[0]), sp_ctof(addr2[1]), sp_ctof(addr2[2]));
ds@1952:
ds@1952: if (!params[3])
ds@1952: {
ds@1952: return sp_ftoc(source.DistTo(dest));
ds@1952: } else {
ds@1952: return sp_ftoc(source.DistToSqr(dest));
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: static cell_t GetVectorDotProduct(IPluginContext *pContext, const cell_t *params)
ds@1952: {
ds@1952: cell_t *addr1, *addr2;
ds@1952:
ds@1952: pContext->LocalToPhysAddr(params[1], &addr1);
ds@1952: pContext->LocalToPhysAddr(params[2], &addr2);
ds@1952:
ds@1952: Vector source(sp_ctof(addr1[0]), sp_ctof(addr1[1]), sp_ctof(addr1[2]));
ds@1952: Vector dest(sp_ctof(addr2[0]), sp_ctof(addr2[1]), sp_ctof(addr2[2]));
ds@1952:
ds@1952: return sp_ftoc(source.Dot(dest));
ds@1952: }
ds@1952:
ds@1952: static cell_t GetVectorCrossProduct(IPluginContext *pContext, const cell_t *params)
ds@1952: {
ds@1952: cell_t *addr1, *addr2, *set;
ds@1952:
ds@1952: pContext->LocalToPhysAddr(params[1], &addr1);
ds@1952: pContext->LocalToPhysAddr(params[2], &addr2);
ds@1952: pContext->LocalToPhysAddr(params[3], &set);
ds@1952:
ds@1952: Vector vec1(sp_ctof(addr1[0]), sp_ctof(addr1[1]), sp_ctof(addr1[2]));
ds@1952: Vector vec2(sp_ctof(addr2[0]), sp_ctof(addr2[1]), sp_ctof(addr2[2]));
ds@1952:
ds@1952: Vector vec3 = vec1.Cross(vec2);
ds@1952:
ds@1952: SET_VECTOR(set, vec3);
ds@1952:
ds@1952: return 1;
ds@1952: }
ds@1952:
ds@1952: static cell_t GetAngleVectors(IPluginContext *pContext, const cell_t *params)
ds@1952: {
ds@1952: cell_t *ang_addr;
ds@1952:
ds@1952: pContext->LocalToPhysAddr(params[1], &ang_addr);
ds@1952:
ds@1952: QAngle angle(sp_ctof(ang_addr[0]), sp_ctof(ang_addr[1]), sp_ctof(ang_addr[2]));
ds@1952: Vector fwd, right, up;
ds@1952:
ds@1952: AngleVectors(angle, &fwd, &right, &up);
ds@1952:
ds@1952: cell_t *addr_fwd, *addr_right, *addr_up;
ds@1952: pContext->LocalToPhysAddr(params[2], &addr_fwd);
ds@1952: pContext->LocalToPhysAddr(params[3], &addr_right);
ds@1952: pContext->LocalToPhysAddr(params[4], &addr_up);
ds@1952:
ds@1952: SET_VECTOR(addr_fwd, fwd);
ds@1952: SET_VECTOR(addr_right, right);
ds@1952: SET_VECTOR(addr_up, up);
ds@1952:
ds@1952: return 1;
ds@1952: }
ds@1952:
ds@1952: static cell_t GetVectorAngles(IPluginContext *pContext, const cell_t *params)
ds@1952: {
ds@1952: cell_t *vec_addr;
ds@1952:
ds@1952: pContext->LocalToPhysAddr(params[1], &vec_addr);
ds@1952:
ds@1952: Vector vec(sp_ctof(vec_addr[0]), sp_ctof(vec_addr[1]), sp_ctof(vec_addr[2]));
ds@1952: QAngle angle;
ds@1952:
ds@1952: VectorAngles(vec, angle);
ds@1952:
ds@1952: cell_t *ang_addr;
ds@1952: pContext->LocalToPhysAddr(params[2], &ang_addr);
ds@1952:
ds@1952: SET_VECTOR(ang_addr, angle);
ds@1952:
ds@1952: return 1;
ds@1952: }
ds@1952:
ds@1952: static cell_t GetVectorVectors(IPluginContext *pContext, const cell_t *params)
ds@1952: {
ds@1952: cell_t *vec_addr;
ds@1952:
ds@1952: pContext->LocalToPhysAddr(params[1], &vec_addr);
ds@1952:
ds@1952: Vector vec(sp_ctof(vec_addr[0]), sp_ctof(vec_addr[1]), sp_ctof(vec_addr[2]));
ds@1952: Vector right, up;
ds@1952:
ds@1952: VectorVectors(vec, right, up);
ds@1952:
ds@1952: cell_t *addr_right, *addr_up;
ds@1952: pContext->LocalToPhysAddr(params[2], &addr_right);
ds@1952: pContext->LocalToPhysAddr(params[3], &addr_up);
ds@1952:
ds@1952: SET_VECTOR(addr_right, right);
ds@1952: SET_VECTOR(addr_up, up);
ds@1952:
ds@1952: return 1;
ds@1952: }
ds@1952:
ds@1952: static cell_t NormalizeVector(IPluginContext *pContext, const cell_t *params)
ds@1952: {
ds@1952: cell_t *addr;
ds@1952:
ds@1952: pContext->LocalToPhysAddr(params[1], &addr);
ds@1952:
ds@1952: Vector source(sp_ctof(addr[0]), sp_ctof(addr[1]), sp_ctof(addr[2]));
ds@1952: float length = VectorNormalize(source);
ds@1952:
ds@1952: pContext->LocalToPhysAddr(params[2], &addr);
ds@1952: SET_VECTOR(addr, source);
ds@1952:
ds@1952: return sp_ftoc(length);
ds@1952: }
ds@1952:
ds@1952: REGISTER_NATIVES(vectorNatives)
ds@1952: {
ds@1952: {"GetAngleVectors", GetAngleVectors},
ds@1952: {"GetVectorAngles", GetVectorAngles},
ds@1952: {"GetVectorCrossProduct", GetVectorCrossProduct},
ds@1952: {"GetVectorDistance", GetVectorDistance},
ds@1952: {"GetVectorDotProduct", GetVectorDotProduct},
ds@1952: {"GetVectorLength", GetVectorLength},
ds@1952: {"GetVectorVectors", GetVectorVectors},
ds@1952: {"NormalizeVector", NormalizeVector},
ds@1952: {NULL, NULL}
ds@1952: };