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