core/smn_float.cpp
author David Anderson <dvander@alliedmods.net>
Thu Jun 21 23:06:37 2007 +0000 (2007-06-21)
changeset 997 2fc3152c3118
parent 851 66d23844a8aa
child 1231 17badaf66ce4
permissions -rw-r--r--
bumped menu api again - added feature to get menu end reason
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  * This file is not open source and may not be copied without explicit
     8  * written permission of AlliedModders LLC.  This file may not be redistributed 
     9  * in whole or significant part.
    10  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #include <math.h>
    16 #include <string.h>
    17 #include <stdlib.h>
    18 #include "sm_globals.h"
    19 
    20 /****************************************
    21 *                                       *
    22 * FLOATING POINT NATIVE IMPLEMENTATIONS *
    23 *                                       *
    24 ****************************************/
    25 
    26 
    27 static cell_t sm_float(IPluginContext *pCtx, const cell_t *params)
    28 {
    29 	float val = static_cast<float>(params[1]);
    30 
    31 	return sp_ftoc(val);
    32 }
    33 
    34 static cell_t sm_FloatAbs(IPluginContext *pCtx, const cell_t *params)
    35 {
    36 	float val = sp_ctof(params[1]);
    37 	val = (val >= 0.0f) ? val : -val;
    38 
    39 	return sp_ftoc(val);
    40 }
    41 
    42 static cell_t sm_FloatAdd(IPluginContext *pCtx, const cell_t *params)
    43 {
    44 	float val = sp_ctof(params[1]) + sp_ctof(params[2]);
    45 
    46 	return sp_ftoc(val);
    47 }
    48 
    49 static cell_t sm_FloatSub(IPluginContext *pCtx, const cell_t *params)
    50 {
    51 	float val = sp_ctof(params[1]) - sp_ctof(params[2]);
    52 
    53 	return sp_ftoc(val);
    54 }
    55 
    56 static cell_t sm_FloatMul(IPluginContext *pCtx, const cell_t *params)
    57 {
    58 	float val = sp_ctof(params[1]) * sp_ctof(params[2]);
    59 
    60 	return sp_ftoc(val);
    61 }
    62 
    63 static cell_t sm_FloatDiv(IPluginContext *pCtx, const cell_t *params)
    64 {
    65 	float val = sp_ctof(params[1]) / sp_ctof(params[2]);
    66 
    67 	return sp_ftoc(val);
    68 }
    69 
    70 static cell_t sm_FloatCompare(IPluginContext *pCtx, const cell_t *params)
    71 {
    72 	float val1 = sp_ctof(params[1]);
    73 	float val2 = sp_ctof(params[2]);
    74 
    75 	if (val1 > val2)
    76 	{
    77 		return 1;
    78 	} else if (val1 < val2) {
    79 		return -1;
    80 	}
    81 
    82 	return 0;
    83 }
    84 
    85 static cell_t sm_Logarithm(IPluginContext *pCtx, const cell_t *params)
    86 {
    87 	float val = sp_ctof(params[1]);
    88 	float base = sp_ctof(params[2]);
    89 
    90 	if ((val <= 0.0f) || (base <= 0.0f))
    91 	{
    92 		return pCtx->ThrowNativeError("Cannot evaluate the logarithm of zero or a negative number (val:%f base:%f)", val, base);
    93 	}
    94 	if (base == 10.0f)
    95 	{
    96 		val = log10(val);
    97 	} else {
    98 		val = log(val) / log(base);
    99 	}
   100 
   101 	return sp_ftoc(val);
   102 }
   103 
   104 static cell_t sm_Exponential(IPluginContext *pCtx, const cell_t *params)
   105 {
   106 	float val = sp_ctof(params[1]);
   107 
   108 	return sp_ftoc(exp(val));
   109 }
   110 
   111 static cell_t sm_Pow(IPluginContext *pCtx, const cell_t *params)
   112 {
   113 	float base = sp_ctof(params[1]);
   114 	float exponent = sp_ctof(params[2]);
   115 
   116 	return sp_ftoc(pow(base, exponent));
   117 }
   118 
   119 static cell_t sm_SquareRoot(IPluginContext *pCtx, const cell_t *params)
   120 {
   121 	float val = sp_ctof(params[1]);
   122 
   123 	if (val < 0.0f)
   124 	{
   125 		return pCtx->ThrowNativeError("Cannot evaluate the square root of a negative number (val:%f)", val);
   126 	}
   127 
   128 	return sp_ftoc(sqrt(val));
   129 }
   130 
   131 static cell_t sm_RoundToNearest(IPluginContext *pCtx, const cell_t *params)
   132 {
   133 	float val = sp_ctof(params[1]);
   134 	val = (float)floor(val + 0.5f);
   135 
   136 	return static_cast<int>(val);
   137 }
   138 
   139 static cell_t sm_RoundToFloor(IPluginContext *pCtx, const cell_t *params)
   140 {
   141 	float val = sp_ctof(params[1]);
   142 	val = floor(val);
   143 
   144 	return static_cast<int>(val);
   145 }
   146 
   147 static cell_t sm_RoundToCeil(IPluginContext *pCtx, const cell_t *params)
   148 {
   149 	float val = sp_ctof(params[1]);
   150 	val = ceil(val);
   151 
   152 	return static_cast<int>(val);
   153 }
   154 
   155 static cell_t sm_RoundToZero(IPluginContext *pCtx, const cell_t *params)
   156 {
   157 	float val = sp_ctof(params[1]);
   158 	if (val >= 0.0f)
   159 	{
   160 		val = floor(val);
   161 	} else {
   162 		val = ceil(val);
   163 	}
   164 
   165 	return static_cast<int>(val);
   166 }
   167 
   168 static cell_t sm_FloatFraction(IPluginContext *pCtx, const cell_t *params)
   169 {
   170 	float val = sp_ctof(params[1]);
   171 	val = val - floor(val);
   172 
   173 	return sp_ftoc(val);
   174 }
   175 
   176 static cell_t sm_Sine(IPluginContext *pCtx, const cell_t *params)
   177 {
   178 	float val = sp_ctof(params[1]);
   179 	val = sin(val);
   180 
   181 	return sp_ftoc(val);
   182 }
   183 
   184 static cell_t sm_Cosine(IPluginContext *pCtx, const cell_t *params)
   185 {
   186 	float val = sp_ctof(params[1]);
   187 	val = cos(val);
   188 
   189 	return sp_ftoc(val);
   190 }
   191 
   192 static cell_t sm_Tangent(IPluginContext *pCtx, const cell_t *params)
   193 {
   194 	float val = sp_ctof(params[1]);
   195 	val = tan(val);
   196 
   197 	return sp_ftoc(val);
   198 }
   199 
   200 static cell_t sm_ArcSine(IPluginContext *pCtx, const cell_t *params)
   201 {
   202 	float val = sp_ctof(params[1]);
   203 	val = asin(val);
   204 
   205 	return sp_ftoc(val);
   206 }
   207 
   208 static cell_t sm_ArcCosine(IPluginContext *pCtx, const cell_t *params)
   209 {
   210 	float val = sp_ctof(params[1]);
   211 	val = acos(val);
   212 
   213 	return sp_ftoc(val);
   214 }
   215 
   216 static cell_t sm_ArcTangent(IPluginContext *pCtx, const cell_t *params)
   217 {
   218 	float val = sp_ctof(params[1]);
   219 	val = atan(val);
   220 
   221 	return sp_ftoc(val);
   222 }
   223 
   224 static cell_t sm_ArcTangent2(IPluginContext *pCtx, const cell_t *params)
   225 {
   226 	float val1 = sp_ctof(params[1]);
   227 	float val2 = sp_ctof(params[2]);
   228 	val1 = atan2(val1, val2);
   229 
   230 	return sp_ftoc(val1);
   231 }
   232 
   233 static cell_t sm_FloatRound(IPluginContext *pCtx, const cell_t *params)
   234 {
   235 	float val = sp_ctof(params[1]);
   236 
   237 	switch (params[2])
   238 	{
   239 	case 1:
   240 		{
   241 			val = floor(val);
   242 			break;
   243 		}
   244 	case 2:
   245 		{
   246 			val = ceil(val);
   247 			break;
   248 		}
   249 	case 3:
   250 		{
   251 			if (val >= 0.0f)
   252 			{
   253 				val = floor(val);
   254 			} else {
   255 				val = ceil(val);
   256 			}
   257 			break;
   258 		}
   259 	default:
   260 		{
   261 			val = (float)floor(val + 0.5f);
   262 			break;
   263 		}
   264 	}
   265 
   266 	return static_cast<int>(val);
   267 }
   268 
   269 REGISTER_NATIVES(floatnatives)
   270 {
   271 	{"float",			sm_float},
   272 	{"FloatMul",		sm_FloatMul},
   273 	{"FloatDiv",		sm_FloatDiv},
   274 	{"FloatAdd",		sm_FloatAdd},
   275 	{"FloatSub",		sm_FloatSub},
   276 	{"FloatFraction",	sm_FloatFraction},
   277 	{"RoundToZero",		sm_RoundToZero},
   278 	{"RoundToCeil",		sm_RoundToCeil},
   279 	{"RoundToFloor",	sm_RoundToFloor},
   280 	{"RoundToNearest",	sm_RoundToNearest},
   281 	{"FloatCompare",	sm_FloatCompare},
   282 	{"SquareRoot",		sm_SquareRoot},
   283 	{"Pow",				sm_Pow},
   284 	{"Exponential",		sm_Exponential},
   285 	{"Logarithm",		sm_Logarithm},
   286 	{"Sine",			sm_Sine},
   287 	{"Cosine",			sm_Cosine},
   288 	{"Tangent",			sm_Tangent},
   289 	{"FloatAbs",		sm_FloatAbs},
   290 	{"ArcTangent",		sm_ArcTangent},
   291 	{"ArcCosine",		sm_ArcCosine},
   292 	{"ArcSine",			sm_ArcSine},
   293 	{"ArcTangent2",		sm_ArcTangent2},
   294 	{"FloatRound",		sm_FloatRound},			/* Backwards compat shim */
   295 	{NULL,				NULL}
   296 };