core/smn_float.cpp
changeset 1952 b7aa2b626807
child 2289 44045ead61cd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/smn_float.cpp	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,314 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id$
    1.33 + */
    1.34 +
    1.35 +#include <math.h>
    1.36 +#include <string.h>
    1.37 +#include <stdlib.h>
    1.38 +#include "sm_globals.h"
    1.39 +
    1.40 +/****************************************
    1.41 +*                                       *
    1.42 +* FLOATING POINT NATIVE IMPLEMENTATIONS *
    1.43 +*                                       *
    1.44 +****************************************/
    1.45 +
    1.46 +
    1.47 +static cell_t sm_float(IPluginContext *pCtx, const cell_t *params)
    1.48 +{
    1.49 +	float val = static_cast<float>(params[1]);
    1.50 +
    1.51 +	return sp_ftoc(val);
    1.52 +}
    1.53 +
    1.54 +static cell_t sm_FloatAbs(IPluginContext *pCtx, const cell_t *params)
    1.55 +{
    1.56 +	float val = sp_ctof(params[1]);
    1.57 +	val = (val >= 0.0f) ? val : -val;
    1.58 +
    1.59 +	return sp_ftoc(val);
    1.60 +}
    1.61 +
    1.62 +static cell_t sm_FloatAdd(IPluginContext *pCtx, const cell_t *params)
    1.63 +{
    1.64 +	float val = sp_ctof(params[1]) + sp_ctof(params[2]);
    1.65 +
    1.66 +	return sp_ftoc(val);
    1.67 +}
    1.68 +
    1.69 +static cell_t sm_FloatSub(IPluginContext *pCtx, const cell_t *params)
    1.70 +{
    1.71 +	float val = sp_ctof(params[1]) - sp_ctof(params[2]);
    1.72 +
    1.73 +	return sp_ftoc(val);
    1.74 +}
    1.75 +
    1.76 +static cell_t sm_FloatMul(IPluginContext *pCtx, const cell_t *params)
    1.77 +{
    1.78 +	float val = sp_ctof(params[1]) * sp_ctof(params[2]);
    1.79 +
    1.80 +	return sp_ftoc(val);
    1.81 +}
    1.82 +
    1.83 +static cell_t sm_FloatDiv(IPluginContext *pCtx, const cell_t *params)
    1.84 +{
    1.85 +	float val = sp_ctof(params[1]) / sp_ctof(params[2]);
    1.86 +
    1.87 +	return sp_ftoc(val);
    1.88 +}
    1.89 +
    1.90 +static cell_t sm_FloatCompare(IPluginContext *pCtx, const cell_t *params)
    1.91 +{
    1.92 +	float val1 = sp_ctof(params[1]);
    1.93 +	float val2 = sp_ctof(params[2]);
    1.94 +
    1.95 +	if (val1 > val2)
    1.96 +	{
    1.97 +		return 1;
    1.98 +	} else if (val1 < val2) {
    1.99 +		return -1;
   1.100 +	}
   1.101 +
   1.102 +	return 0;
   1.103 +}
   1.104 +
   1.105 +static cell_t sm_Logarithm(IPluginContext *pCtx, const cell_t *params)
   1.106 +{
   1.107 +	float val = sp_ctof(params[1]);
   1.108 +	float base = sp_ctof(params[2]);
   1.109 +
   1.110 +	if ((val <= 0.0f) || (base <= 0.0f))
   1.111 +	{
   1.112 +		return pCtx->ThrowNativeError("Cannot evaluate the logarithm of zero or a negative number (val:%f base:%f)", val, base);
   1.113 +	}
   1.114 +	if (base == 10.0f)
   1.115 +	{
   1.116 +		val = log10(val);
   1.117 +	} else {
   1.118 +		val = log(val) / log(base);
   1.119 +	}
   1.120 +
   1.121 +	return sp_ftoc(val);
   1.122 +}
   1.123 +
   1.124 +static cell_t sm_Exponential(IPluginContext *pCtx, const cell_t *params)
   1.125 +{
   1.126 +	float val = sp_ctof(params[1]);
   1.127 +
   1.128 +	return sp_ftoc(exp(val));
   1.129 +}
   1.130 +
   1.131 +static cell_t sm_Pow(IPluginContext *pCtx, const cell_t *params)
   1.132 +{
   1.133 +	float base = sp_ctof(params[1]);
   1.134 +	float exponent = sp_ctof(params[2]);
   1.135 +
   1.136 +	return sp_ftoc(pow(base, exponent));
   1.137 +}
   1.138 +
   1.139 +static cell_t sm_SquareRoot(IPluginContext *pCtx, const cell_t *params)
   1.140 +{
   1.141 +	float val = sp_ctof(params[1]);
   1.142 +
   1.143 +	if (val < 0.0f)
   1.144 +	{
   1.145 +		return pCtx->ThrowNativeError("Cannot evaluate the square root of a negative number (val:%f)", val);
   1.146 +	}
   1.147 +
   1.148 +	return sp_ftoc(sqrt(val));
   1.149 +}
   1.150 +
   1.151 +static cell_t sm_RoundToNearest(IPluginContext *pCtx, const cell_t *params)
   1.152 +{
   1.153 +	float val = sp_ctof(params[1]);
   1.154 +	val = (float)floor(val + 0.5f);
   1.155 +
   1.156 +	return static_cast<int>(val);
   1.157 +}
   1.158 +
   1.159 +static cell_t sm_RoundToFloor(IPluginContext *pCtx, const cell_t *params)
   1.160 +{
   1.161 +	float val = sp_ctof(params[1]);
   1.162 +	val = floor(val);
   1.163 +
   1.164 +	return static_cast<int>(val);
   1.165 +}
   1.166 +
   1.167 +static cell_t sm_RoundToCeil(IPluginContext *pCtx, const cell_t *params)
   1.168 +{
   1.169 +	float val = sp_ctof(params[1]);
   1.170 +	val = ceil(val);
   1.171 +
   1.172 +	return static_cast<int>(val);
   1.173 +}
   1.174 +
   1.175 +static cell_t sm_RoundToZero(IPluginContext *pCtx, const cell_t *params)
   1.176 +{
   1.177 +	float val = sp_ctof(params[1]);
   1.178 +	if (val >= 0.0f)
   1.179 +	{
   1.180 +		val = floor(val);
   1.181 +	} else {
   1.182 +		val = ceil(val);
   1.183 +	}
   1.184 +
   1.185 +	return static_cast<int>(val);
   1.186 +}
   1.187 +
   1.188 +static cell_t sm_FloatFraction(IPluginContext *pCtx, const cell_t *params)
   1.189 +{
   1.190 +	float val = sp_ctof(params[1]);
   1.191 +	val = val - floor(val);
   1.192 +
   1.193 +	return sp_ftoc(val);
   1.194 +}
   1.195 +
   1.196 +static cell_t sm_Sine(IPluginContext *pCtx, const cell_t *params)
   1.197 +{
   1.198 +	float val = sp_ctof(params[1]);
   1.199 +	val = sin(val);
   1.200 +
   1.201 +	return sp_ftoc(val);
   1.202 +}
   1.203 +
   1.204 +static cell_t sm_Cosine(IPluginContext *pCtx, const cell_t *params)
   1.205 +{
   1.206 +	float val = sp_ctof(params[1]);
   1.207 +	val = cos(val);
   1.208 +
   1.209 +	return sp_ftoc(val);
   1.210 +}
   1.211 +
   1.212 +static cell_t sm_Tangent(IPluginContext *pCtx, const cell_t *params)
   1.213 +{
   1.214 +	float val = sp_ctof(params[1]);
   1.215 +	val = tan(val);
   1.216 +
   1.217 +	return sp_ftoc(val);
   1.218 +}
   1.219 +
   1.220 +static cell_t sm_ArcSine(IPluginContext *pCtx, const cell_t *params)
   1.221 +{
   1.222 +	float val = sp_ctof(params[1]);
   1.223 +	val = asin(val);
   1.224 +
   1.225 +	return sp_ftoc(val);
   1.226 +}
   1.227 +
   1.228 +static cell_t sm_ArcCosine(IPluginContext *pCtx, const cell_t *params)
   1.229 +{
   1.230 +	float val = sp_ctof(params[1]);
   1.231 +	val = acos(val);
   1.232 +
   1.233 +	return sp_ftoc(val);
   1.234 +}
   1.235 +
   1.236 +static cell_t sm_ArcTangent(IPluginContext *pCtx, const cell_t *params)
   1.237 +{
   1.238 +	float val = sp_ctof(params[1]);
   1.239 +	val = atan(val);
   1.240 +
   1.241 +	return sp_ftoc(val);
   1.242 +}
   1.243 +
   1.244 +static cell_t sm_ArcTangent2(IPluginContext *pCtx, const cell_t *params)
   1.245 +{
   1.246 +	float val1 = sp_ctof(params[1]);
   1.247 +	float val2 = sp_ctof(params[2]);
   1.248 +	val1 = atan2(val1, val2);
   1.249 +
   1.250 +	return sp_ftoc(val1);
   1.251 +}
   1.252 +
   1.253 +#if 0
   1.254 +static cell_t sm_FloatRound(IPluginContext *pCtx, const cell_t *params)
   1.255 +{
   1.256 +	float val = sp_ctof(params[1]);
   1.257 +
   1.258 +	switch (params[2])
   1.259 +	{
   1.260 +	case 1:
   1.261 +		{
   1.262 +			val = floor(val);
   1.263 +			break;
   1.264 +		}
   1.265 +	case 2:
   1.266 +		{
   1.267 +			val = ceil(val);
   1.268 +			break;
   1.269 +		}
   1.270 +	case 3:
   1.271 +		{
   1.272 +			if (val >= 0.0f)
   1.273 +			{
   1.274 +				val = floor(val);
   1.275 +			} else {
   1.276 +				val = ceil(val);
   1.277 +			}
   1.278 +			break;
   1.279 +		}
   1.280 +	default:
   1.281 +		{
   1.282 +			val = (float)floor(val + 0.5f);
   1.283 +			break;
   1.284 +		}
   1.285 +	}
   1.286 +
   1.287 +	return static_cast<int>(val);
   1.288 +}
   1.289 +#endif
   1.290 +
   1.291 +REGISTER_NATIVES(floatnatives)
   1.292 +{
   1.293 +	{"float",			sm_float},
   1.294 +	{"FloatMul",		sm_FloatMul},
   1.295 +	{"FloatDiv",		sm_FloatDiv},
   1.296 +	{"FloatAdd",		sm_FloatAdd},
   1.297 +	{"FloatSub",		sm_FloatSub},
   1.298 +	{"FloatFraction",	sm_FloatFraction},
   1.299 +	{"RoundToZero",		sm_RoundToZero},
   1.300 +	{"RoundToCeil",		sm_RoundToCeil},
   1.301 +	{"RoundToFloor",	sm_RoundToFloor},
   1.302 +	{"RoundToNearest",	sm_RoundToNearest},
   1.303 +	{"FloatCompare",	sm_FloatCompare},
   1.304 +	{"SquareRoot",		sm_SquareRoot},
   1.305 +	{"Pow",				sm_Pow},
   1.306 +	{"Exponential",		sm_Exponential},
   1.307 +	{"Logarithm",		sm_Logarithm},
   1.308 +	{"Sine",			sm_Sine},
   1.309 +	{"Cosine",			sm_Cosine},
   1.310 +	{"Tangent",			sm_Tangent},
   1.311 +	{"FloatAbs",		sm_FloatAbs},
   1.312 +	{"ArcTangent",		sm_ArcTangent},
   1.313 +	{"ArcCosine",		sm_ArcCosine},
   1.314 +	{"ArcSine",			sm_ArcSine},
   1.315 +	{"ArcTangent2",		sm_ArcTangent2},
   1.316 +	{NULL,				NULL}
   1.317 +};