core/smn_float.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Feb 08 21:41:28 2007 +0000 (2007-02-08)
changeset 457 9b8a85bf4175
parent 432 77112e9620fa
child 464 765517f654a5
permissions -rw-r--r--
Initial import of convar stuff
1) Natives to create, find, set, and get convars
2) "cvars" option added to sm menu
3) Some internal additions to CPlugin to store a convar list
Still some things left to do...
     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 "sm_globals.h"
    16 
    17 /****************************************
    18 *                                       *
    19 * FLOATING POINT NATIVE IMPLEMENTATIONS *
    20 *                                       *
    21 ****************************************/
    22 
    23 
    24 static cell_t sm_float(IPluginContext *pCtx, const cell_t *params)
    25 {
    26 	float val = static_cast<float>(params[1]);
    27 
    28 	return sp_ftoc(val);
    29 }
    30 
    31 static cell_t sm_FloatAbs(IPluginContext *pCtx, const cell_t *params)
    32 {
    33 	float val = sp_ctof(params[1]);
    34 	val = (val >= 0) ? val : -val;
    35 
    36 	return sp_ftoc(val);
    37 }
    38 
    39 static cell_t sm_FloatAdd(IPluginContext *pCtx, const cell_t *params)
    40 {
    41 	float val = sp_ctof(params[1]) + sp_ctof(params[2]);
    42 
    43 	return sp_ftoc(val);
    44 }
    45 
    46 static cell_t sm_FloatSub(IPluginContext *pCtx, const cell_t *params)
    47 {
    48 	float val = sp_ctof(params[1]) - sp_ctof(params[2]);
    49 
    50 	return sp_ftoc(val);
    51 }
    52 
    53 static cell_t sm_FloatMul(IPluginContext *pCtx, const cell_t *params)
    54 {
    55 	float val = sp_ctof(params[1]) * sp_ctof(params[2]);
    56 
    57 	return sp_ftoc(val);
    58 }
    59 
    60 static cell_t sm_FloatDiv(IPluginContext *pCtx, const cell_t *params)
    61 {
    62 	float val = sp_ctof(params[1]) / sp_ctof(params[2]);
    63 
    64 	return sp_ftoc(val);
    65 }
    66 
    67 static cell_t sm_FloatCompare(IPluginContext *pCtx, const cell_t *params)
    68 {
    69 	float val1 = sp_ctof(params[1]);
    70 	float val2 = sp_ctof(params[2]);
    71 
    72 	if (val1 > val2)
    73 	{
    74 		return 1;
    75 	} else if (val1 < val2) {
    76 		return -1;
    77 	}
    78 
    79 	return 0;
    80 }
    81 
    82 static cell_t sm_Logarithm(IPluginContext *pCtx, const cell_t *params)
    83 {
    84 	float val = sp_ctof(params[1]);
    85 	float base = sp_ctof(params[2]);
    86 
    87 	if ((val <= 0) || (base <= 0))
    88 	{
    89 		return pCtx->ThrowNativeError("Cannot evaluate the logarithm of zero or a negative number (val:%f base:%f).", val, base);
    90 	}
    91 	if (base == 10.0)
    92 	{
    93 		val = log10(val);
    94 	} else {
    95 		val = log(val) / log(base);
    96 	}
    97 
    98 	return sp_ftoc(val);
    99 }
   100 
   101 static cell_t sm_Exponential(IPluginContext *pCtx, const cell_t *params)
   102 {
   103 	float val = sp_ctof(params[1]);
   104 
   105 	return sp_ftoc(exp(val));
   106 }
   107 
   108 static cell_t sm_Pow(IPluginContext *pCtx, const cell_t *params)
   109 {
   110 	float base = sp_ctof(params[1]);
   111 	float exponent = sp_ctof(params[2]);
   112 
   113 	return sp_ftoc(pow(base, exponent));
   114 }
   115 
   116 static cell_t sm_SquareRoot(IPluginContext *pCtx, const cell_t *params)
   117 {
   118 	float val = sp_ctof(params[1]);
   119 
   120 	if (val < 0.0)
   121 	{
   122 		return pCtx->ThrowNativeError("Cannot evaluate the square root of a negative number (val:%f).", val);
   123 	}
   124 
   125 	return sp_ftoc(sqrt(val));
   126 }
   127 
   128 static cell_t sm_FloatRound(IPluginContext *pCtx, const cell_t *params)
   129 {
   130 	float val = sp_ctof(params[1]);
   131 
   132 	switch (params[2])
   133 	{
   134 	case 1:
   135 		{
   136 			val = floor(val);
   137 			break;
   138 		}
   139 	case 2:
   140 		{
   141 			val = ceil(val);
   142 			break;
   143 		}
   144 	case 3:
   145 		{
   146 			if (val >= 0.0)
   147 			{
   148 				val = floor(val);
   149 			} else {
   150 				val = ceil(val);
   151 			}
   152 			break;
   153 		}
   154 	default:
   155 		{
   156 			val = (float)floor(val + 0.5);
   157 			break;
   158 		}
   159 	}
   160 
   161 	return static_cast<int>(val);
   162 }
   163 
   164 static cell_t sm_FloatStr(IPluginContext *pCtx, const cell_t *params)
   165 {
   166 	char *str;
   167 
   168 	pCtx->LocalToString(params[1], &str);
   169 	if (!strlen(str))
   170 	{
   171 		return 0;
   172 	}
   173 
   174 	return sp_ftoc((float)atof(str));
   175 }
   176 
   177 static cell_t sm_FloatFraction(IPluginContext *pCtx, const cell_t *params)
   178 {
   179 	float val = sp_ctof(params[1]);
   180 	val = val - floor(val);
   181 
   182 	return sp_ftoc(val);
   183 }
   184 
   185 static cell_t sm_Sine(IPluginContext *pCtx, const cell_t *params)
   186 {
   187 	float val = sp_ctof(params[1]);
   188 	val = sin(val);
   189 
   190 	return sp_ftoc(val);
   191 }
   192 
   193 static cell_t sm_Cosine(IPluginContext *pCtx, const cell_t *params)
   194 {
   195 	float val = sp_ctof(params[1]);
   196 	val = cos(val);
   197 
   198 	return sp_ftoc(val);
   199 }
   200 
   201 static cell_t sm_Tangent(IPluginContext *pCtx, const cell_t *params)
   202 {
   203 	float val = sp_ctof(params[1]);
   204 	val = tan(val);
   205 
   206 	return sp_ftoc(val);
   207 }
   208 
   209 static cell_t sm_ArcSine(IPluginContext *pCtx, const cell_t *params)
   210 {
   211 	float val = sp_ctof(params[1]);
   212 	val = asin(val);
   213 
   214 	return sp_ftoc(val);
   215 }
   216 
   217 static cell_t sm_ArcCosine(IPluginContext *pCtx, const cell_t *params)
   218 {
   219 	float val = sp_ctof(params[1]);
   220 	val = acos(val);
   221 
   222 	return sp_ftoc(val);
   223 }
   224 
   225 static cell_t sm_ArcTangent(IPluginContext *pCtx, const cell_t *params)
   226 {
   227 	float val = sp_ctof(params[1]);
   228 	val = atan(val);
   229 
   230 	return sp_ftoc(val);
   231 }
   232 
   233 static cell_t sm_ArcTangent2(IPluginContext *pCtx, const cell_t *params)
   234 {
   235 	float val1 = sp_ctof(params[1]);
   236 	float val2 = sp_ctof(params[2]);
   237 	val1 = atan2(val1, val2);
   238 
   239 	return sp_ftoc(val1);
   240 }
   241 
   242 REGISTER_NATIVES(floatnatives)
   243 {
   244 	{"float",			sm_float},
   245 	{"FloatStr",		sm_FloatStr},
   246 	{"FloatMul",		sm_FloatMul},
   247 	{"FloatDiv",		sm_FloatDiv},
   248 	{"FloatAdd",		sm_FloatAdd},
   249 	{"FloatSub",		sm_FloatSub},
   250 	{"FloatFraction",	sm_FloatFraction},
   251 	{"FloatRound",		sm_FloatRound},
   252 	{"FloatCompare",	sm_FloatCompare},
   253 	{"SquareRoot",		sm_SquareRoot},
   254 	{"Pow",				sm_Pow},
   255 	{"Exponential",		sm_Exponential},
   256 	{"Logarithm",		sm_Logarithm},
   257 	{"Sine",			sm_Sine},
   258 	{"Cosine",			sm_Cosine},
   259 	{"Tangent",			sm_Tangent},
   260 	{"FloatAbs",		sm_FloatAbs},
   261 	{"ArcTangent",		sm_ArcTangent},
   262 	{"ArcCosine",		sm_ArcCosine},
   263 	{"ArcSine",			sm_ArcSine},
   264 	{"ArcTangent2",		sm_ArcTangent2},
   265 	{NULL,				NULL}
   266 };