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