core/smn_float.cpp
author David Anderson <dvander@alliedmods.net>
Tue May 22 20:50:48 2007 +0000 (2007-05-22)
changeset 833 732bedfcbe3e
parent 831 1995c0dd5c04
child 836 baa0ec2b5ac7
permissions -rw-r--r--
added ReplaceString and ReplaceStringEx (phew!)
     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_RountToNearest(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 REGISTER_NATIVES(floatnatives)
   234 {
   235 	{"float",			sm_float},
   236 	{"FloatMul",		sm_FloatMul},
   237 	{"FloatDiv",		sm_FloatDiv},
   238 	{"FloatAdd",		sm_FloatAdd},
   239 	{"FloatSub",		sm_FloatSub},
   240 	{"FloatFraction",	sm_FloatFraction},
   241 	{"RoundToZero",		sm_RoundToZero},
   242 	{"RoundToCeil",		sm_RoundToCeil},
   243 	{"RoundToFloor",	sm_RoundToFloor},
   244 	{"RountToNearest",	sm_RountToNearest},
   245 	{"FloatCompare",	sm_FloatCompare},
   246 	{"SquareRoot",		sm_SquareRoot},
   247 	{"Pow",				sm_Pow},
   248 	{"Exponential",		sm_Exponential},
   249 	{"Logarithm",		sm_Logarithm},
   250 	{"Sine",			sm_Sine},
   251 	{"Cosine",			sm_Cosine},
   252 	{"Tangent",			sm_Tangent},
   253 	{"FloatAbs",		sm_FloatAbs},
   254 	{"ArcTangent",		sm_ArcTangent},
   255 	{"ArcCosine",		sm_ArcCosine},
   256 	{"ArcSine",			sm_ArcSine},
   257 	{"ArcTangent2",		sm_ArcTangent2},
   258 	{NULL,				NULL}
   259 };