core/smn_float.cpp
author Scott Ehlert <ds@alliedmods.net>
Sun Mar 30 07:00:22 2008 +0000 (2008-03-30)
changeset 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
Spring Cleaning, Part Ichi (1)
Various minor things done to project files
Updated sample extension project file and updated makefile to the new unified version (more changes likely on the way)
Updated regex project file and makefile
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include <math.h>
    33 #include <string.h>
    34 #include <stdlib.h>
    35 #include "sm_globals.h"
    36 
    37 /****************************************
    38 *                                       *
    39 * FLOATING POINT NATIVE IMPLEMENTATIONS *
    40 *                                       *
    41 ****************************************/
    42 
    43 
    44 static cell_t sm_float(IPluginContext *pCtx, const cell_t *params)
    45 {
    46 	float val = static_cast<float>(params[1]);
    47 
    48 	return sp_ftoc(val);
    49 }
    50 
    51 static cell_t sm_FloatAbs(IPluginContext *pCtx, const cell_t *params)
    52 {
    53 	float val = sp_ctof(params[1]);
    54 	val = (val >= 0.0f) ? val : -val;
    55 
    56 	return sp_ftoc(val);
    57 }
    58 
    59 static cell_t sm_FloatAdd(IPluginContext *pCtx, const cell_t *params)
    60 {
    61 	float val = sp_ctof(params[1]) + sp_ctof(params[2]);
    62 
    63 	return sp_ftoc(val);
    64 }
    65 
    66 static cell_t sm_FloatSub(IPluginContext *pCtx, const cell_t *params)
    67 {
    68 	float val = sp_ctof(params[1]) - sp_ctof(params[2]);
    69 
    70 	return sp_ftoc(val);
    71 }
    72 
    73 static cell_t sm_FloatMul(IPluginContext *pCtx, const cell_t *params)
    74 {
    75 	float val = sp_ctof(params[1]) * sp_ctof(params[2]);
    76 
    77 	return sp_ftoc(val);
    78 }
    79 
    80 static cell_t sm_FloatDiv(IPluginContext *pCtx, const cell_t *params)
    81 {
    82 	float val = sp_ctof(params[1]) / sp_ctof(params[2]);
    83 
    84 	return sp_ftoc(val);
    85 }
    86 
    87 static cell_t sm_FloatCompare(IPluginContext *pCtx, const cell_t *params)
    88 {
    89 	float val1 = sp_ctof(params[1]);
    90 	float val2 = sp_ctof(params[2]);
    91 
    92 	if (val1 > val2)
    93 	{
    94 		return 1;
    95 	} else if (val1 < val2) {
    96 		return -1;
    97 	}
    98 
    99 	return 0;
   100 }
   101 
   102 static cell_t sm_Logarithm(IPluginContext *pCtx, const cell_t *params)
   103 {
   104 	float val = sp_ctof(params[1]);
   105 	float base = sp_ctof(params[2]);
   106 
   107 	if ((val <= 0.0f) || (base <= 0.0f))
   108 	{
   109 		return pCtx->ThrowNativeError("Cannot evaluate the logarithm of zero or a negative number (val:%f base:%f)", val, base);
   110 	}
   111 	if (base == 10.0f)
   112 	{
   113 		val = log10(val);
   114 	} else {
   115 		val = log(val) / log(base);
   116 	}
   117 
   118 	return sp_ftoc(val);
   119 }
   120 
   121 static cell_t sm_Exponential(IPluginContext *pCtx, const cell_t *params)
   122 {
   123 	float val = sp_ctof(params[1]);
   124 
   125 	return sp_ftoc(exp(val));
   126 }
   127 
   128 static cell_t sm_Pow(IPluginContext *pCtx, const cell_t *params)
   129 {
   130 	float base = sp_ctof(params[1]);
   131 	float exponent = sp_ctof(params[2]);
   132 
   133 	return sp_ftoc(pow(base, exponent));
   134 }
   135 
   136 static cell_t sm_SquareRoot(IPluginContext *pCtx, const cell_t *params)
   137 {
   138 	float val = sp_ctof(params[1]);
   139 
   140 	if (val < 0.0f)
   141 	{
   142 		return pCtx->ThrowNativeError("Cannot evaluate the square root of a negative number (val:%f)", val);
   143 	}
   144 
   145 	return sp_ftoc(sqrt(val));
   146 }
   147 
   148 static cell_t sm_RoundToNearest(IPluginContext *pCtx, const cell_t *params)
   149 {
   150 	float val = sp_ctof(params[1]);
   151 	val = (float)floor(val + 0.5f);
   152 
   153 	return static_cast<int>(val);
   154 }
   155 
   156 static cell_t sm_RoundToFloor(IPluginContext *pCtx, const cell_t *params)
   157 {
   158 	float val = sp_ctof(params[1]);
   159 	val = floor(val);
   160 
   161 	return static_cast<int>(val);
   162 }
   163 
   164 static cell_t sm_RoundToCeil(IPluginContext *pCtx, const cell_t *params)
   165 {
   166 	float val = sp_ctof(params[1]);
   167 	val = ceil(val);
   168 
   169 	return static_cast<int>(val);
   170 }
   171 
   172 static cell_t sm_RoundToZero(IPluginContext *pCtx, const cell_t *params)
   173 {
   174 	float val = sp_ctof(params[1]);
   175 	if (val >= 0.0f)
   176 	{
   177 		val = floor(val);
   178 	} else {
   179 		val = ceil(val);
   180 	}
   181 
   182 	return static_cast<int>(val);
   183 }
   184 
   185 static cell_t sm_FloatFraction(IPluginContext *pCtx, const cell_t *params)
   186 {
   187 	float val = sp_ctof(params[1]);
   188 	val = val - floor(val);
   189 
   190 	return sp_ftoc(val);
   191 }
   192 
   193 static cell_t sm_Sine(IPluginContext *pCtx, const cell_t *params)
   194 {
   195 	float val = sp_ctof(params[1]);
   196 	val = sin(val);
   197 
   198 	return sp_ftoc(val);
   199 }
   200 
   201 static cell_t sm_Cosine(IPluginContext *pCtx, const cell_t *params)
   202 {
   203 	float val = sp_ctof(params[1]);
   204 	val = cos(val);
   205 
   206 	return sp_ftoc(val);
   207 }
   208 
   209 static cell_t sm_Tangent(IPluginContext *pCtx, const cell_t *params)
   210 {
   211 	float val = sp_ctof(params[1]);
   212 	val = tan(val);
   213 
   214 	return sp_ftoc(val);
   215 }
   216 
   217 static cell_t sm_ArcSine(IPluginContext *pCtx, const cell_t *params)
   218 {
   219 	float val = sp_ctof(params[1]);
   220 	val = asin(val);
   221 
   222 	return sp_ftoc(val);
   223 }
   224 
   225 static cell_t sm_ArcCosine(IPluginContext *pCtx, const cell_t *params)
   226 {
   227 	float val = sp_ctof(params[1]);
   228 	val = acos(val);
   229 
   230 	return sp_ftoc(val);
   231 }
   232 
   233 static cell_t sm_ArcTangent(IPluginContext *pCtx, const cell_t *params)
   234 {
   235 	float val = sp_ctof(params[1]);
   236 	val = atan(val);
   237 
   238 	return sp_ftoc(val);
   239 }
   240 
   241 static cell_t sm_ArcTangent2(IPluginContext *pCtx, const cell_t *params)
   242 {
   243 	float val1 = sp_ctof(params[1]);
   244 	float val2 = sp_ctof(params[2]);
   245 	val1 = atan2(val1, val2);
   246 
   247 	return sp_ftoc(val1);
   248 }
   249 
   250 #if 0
   251 static cell_t sm_FloatRound(IPluginContext *pCtx, const cell_t *params)
   252 {
   253 	float val = sp_ctof(params[1]);
   254 
   255 	switch (params[2])
   256 	{
   257 	case 1:
   258 		{
   259 			val = floor(val);
   260 			break;
   261 		}
   262 	case 2:
   263 		{
   264 			val = ceil(val);
   265 			break;
   266 		}
   267 	case 3:
   268 		{
   269 			if (val >= 0.0f)
   270 			{
   271 				val = floor(val);
   272 			} else {
   273 				val = ceil(val);
   274 			}
   275 			break;
   276 		}
   277 	default:
   278 		{
   279 			val = (float)floor(val + 0.5f);
   280 			break;
   281 		}
   282 	}
   283 
   284 	return static_cast<int>(val);
   285 }
   286 #endif
   287 
   288 REGISTER_NATIVES(floatnatives)
   289 {
   290 	{"float",			sm_float},
   291 	{"FloatMul",		sm_FloatMul},
   292 	{"FloatDiv",		sm_FloatDiv},
   293 	{"FloatAdd",		sm_FloatAdd},
   294 	{"FloatSub",		sm_FloatSub},
   295 	{"FloatFraction",	sm_FloatFraction},
   296 	{"RoundToZero",		sm_RoundToZero},
   297 	{"RoundToCeil",		sm_RoundToCeil},
   298 	{"RoundToFloor",	sm_RoundToFloor},
   299 	{"RoundToNearest",	sm_RoundToNearest},
   300 	{"FloatCompare",	sm_FloatCompare},
   301 	{"SquareRoot",		sm_SquareRoot},
   302 	{"Pow",				sm_Pow},
   303 	{"Exponential",		sm_Exponential},
   304 	{"Logarithm",		sm_Logarithm},
   305 	{"Sine",			sm_Sine},
   306 	{"Cosine",			sm_Cosine},
   307 	{"Tangent",			sm_Tangent},
   308 	{"FloatAbs",		sm_FloatAbs},
   309 	{"ArcTangent",		sm_ArcTangent},
   310 	{"ArcCosine",		sm_ArcCosine},
   311 	{"ArcSine",			sm_ArcSine},
   312 	{"ArcTangent2",		sm_ArcTangent2},
   313 	{NULL,				NULL}
   314 };