core/smn_float.cpp
author Phong Dang <teame06@gmail.com>
Wed Aug 01 07:29:40 2007 +0000 (2007-08-01)
changeset 1241 2e8046793bc9
parent 1231 17badaf66ce4
child 1330 ab94316bb52b
permissions -rw-r--r--
Added check to make sure player are alive to perform sm_slap, sm_burn, sm_slay.
     1 /**
     2  * vim: set ts=4 :
     3  * ================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * ================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or 
     9  * modify it under the terms of the GNU General Public License, 
    10  * version 3.0, as published by the Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  * GNU General Public License for more details.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to 
    21  * link the code of this program (as well as its derivative works) to 
    22  * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any 
    23  * Game MODs that run on software by the Valve Corporation.  You must 
    24  * obey the GNU General Public License in all respects for all other 
    25  * code used. Additionally, AlliedModders LLC grants this exception 
    26  * to all derivative works. AlliedModders LLC defines further 
    27  * exceptions, found in LICENSE.txt (as of this writing, version 
    28  * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #include <math.h>
    34 #include <string.h>
    35 #include <stdlib.h>
    36 #include "sm_globals.h"
    37 
    38 /****************************************
    39 *                                       *
    40 * FLOATING POINT NATIVE IMPLEMENTATIONS *
    41 *                                       *
    42 ****************************************/
    43 
    44 
    45 static cell_t sm_float(IPluginContext *pCtx, const cell_t *params)
    46 {
    47 	float val = static_cast<float>(params[1]);
    48 
    49 	return sp_ftoc(val);
    50 }
    51 
    52 static cell_t sm_FloatAbs(IPluginContext *pCtx, const cell_t *params)
    53 {
    54 	float val = sp_ctof(params[1]);
    55 	val = (val >= 0.0f) ? val : -val;
    56 
    57 	return sp_ftoc(val);
    58 }
    59 
    60 static cell_t sm_FloatAdd(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_FloatSub(IPluginContext *pCtx, const cell_t *params)
    68 {
    69 	float val = sp_ctof(params[1]) - sp_ctof(params[2]);
    70 
    71 	return sp_ftoc(val);
    72 }
    73 
    74 static cell_t sm_FloatMul(IPluginContext *pCtx, const cell_t *params)
    75 {
    76 	float val = sp_ctof(params[1]) * sp_ctof(params[2]);
    77 
    78 	return sp_ftoc(val);
    79 }
    80 
    81 static cell_t sm_FloatDiv(IPluginContext *pCtx, const cell_t *params)
    82 {
    83 	float val = sp_ctof(params[1]) / sp_ctof(params[2]);
    84 
    85 	return sp_ftoc(val);
    86 }
    87 
    88 static cell_t sm_FloatCompare(IPluginContext *pCtx, const cell_t *params)
    89 {
    90 	float val1 = sp_ctof(params[1]);
    91 	float val2 = sp_ctof(params[2]);
    92 
    93 	if (val1 > val2)
    94 	{
    95 		return 1;
    96 	} else if (val1 < val2) {
    97 		return -1;
    98 	}
    99 
   100 	return 0;
   101 }
   102 
   103 static cell_t sm_Logarithm(IPluginContext *pCtx, const cell_t *params)
   104 {
   105 	float val = sp_ctof(params[1]);
   106 	float base = sp_ctof(params[2]);
   107 
   108 	if ((val <= 0.0f) || (base <= 0.0f))
   109 	{
   110 		return pCtx->ThrowNativeError("Cannot evaluate the logarithm of zero or a negative number (val:%f base:%f)", val, base);
   111 	}
   112 	if (base == 10.0f)
   113 	{
   114 		val = log10(val);
   115 	} else {
   116 		val = log(val) / log(base);
   117 	}
   118 
   119 	return sp_ftoc(val);
   120 }
   121 
   122 static cell_t sm_Exponential(IPluginContext *pCtx, const cell_t *params)
   123 {
   124 	float val = sp_ctof(params[1]);
   125 
   126 	return sp_ftoc(exp(val));
   127 }
   128 
   129 static cell_t sm_Pow(IPluginContext *pCtx, const cell_t *params)
   130 {
   131 	float base = sp_ctof(params[1]);
   132 	float exponent = sp_ctof(params[2]);
   133 
   134 	return sp_ftoc(pow(base, exponent));
   135 }
   136 
   137 static cell_t sm_SquareRoot(IPluginContext *pCtx, const cell_t *params)
   138 {
   139 	float val = sp_ctof(params[1]);
   140 
   141 	if (val < 0.0f)
   142 	{
   143 		return pCtx->ThrowNativeError("Cannot evaluate the square root of a negative number (val:%f)", val);
   144 	}
   145 
   146 	return sp_ftoc(sqrt(val));
   147 }
   148 
   149 static cell_t sm_RoundToNearest(IPluginContext *pCtx, const cell_t *params)
   150 {
   151 	float val = sp_ctof(params[1]);
   152 	val = (float)floor(val + 0.5f);
   153 
   154 	return static_cast<int>(val);
   155 }
   156 
   157 static cell_t sm_RoundToFloor(IPluginContext *pCtx, const cell_t *params)
   158 {
   159 	float val = sp_ctof(params[1]);
   160 	val = floor(val);
   161 
   162 	return static_cast<int>(val);
   163 }
   164 
   165 static cell_t sm_RoundToCeil(IPluginContext *pCtx, const cell_t *params)
   166 {
   167 	float val = sp_ctof(params[1]);
   168 	val = ceil(val);
   169 
   170 	return static_cast<int>(val);
   171 }
   172 
   173 static cell_t sm_RoundToZero(IPluginContext *pCtx, const cell_t *params)
   174 {
   175 	float val = sp_ctof(params[1]);
   176 	if (val >= 0.0f)
   177 	{
   178 		val = floor(val);
   179 	} else {
   180 		val = ceil(val);
   181 	}
   182 
   183 	return static_cast<int>(val);
   184 }
   185 
   186 static cell_t sm_FloatFraction(IPluginContext *pCtx, const cell_t *params)
   187 {
   188 	float val = sp_ctof(params[1]);
   189 	val = val - floor(val);
   190 
   191 	return sp_ftoc(val);
   192 }
   193 
   194 static cell_t sm_Sine(IPluginContext *pCtx, const cell_t *params)
   195 {
   196 	float val = sp_ctof(params[1]);
   197 	val = sin(val);
   198 
   199 	return sp_ftoc(val);
   200 }
   201 
   202 static cell_t sm_Cosine(IPluginContext *pCtx, const cell_t *params)
   203 {
   204 	float val = sp_ctof(params[1]);
   205 	val = cos(val);
   206 
   207 	return sp_ftoc(val);
   208 }
   209 
   210 static cell_t sm_Tangent(IPluginContext *pCtx, const cell_t *params)
   211 {
   212 	float val = sp_ctof(params[1]);
   213 	val = tan(val);
   214 
   215 	return sp_ftoc(val);
   216 }
   217 
   218 static cell_t sm_ArcSine(IPluginContext *pCtx, const cell_t *params)
   219 {
   220 	float val = sp_ctof(params[1]);
   221 	val = asin(val);
   222 
   223 	return sp_ftoc(val);
   224 }
   225 
   226 static cell_t sm_ArcCosine(IPluginContext *pCtx, const cell_t *params)
   227 {
   228 	float val = sp_ctof(params[1]);
   229 	val = acos(val);
   230 
   231 	return sp_ftoc(val);
   232 }
   233 
   234 static cell_t sm_ArcTangent(IPluginContext *pCtx, const cell_t *params)
   235 {
   236 	float val = sp_ctof(params[1]);
   237 	val = atan(val);
   238 
   239 	return sp_ftoc(val);
   240 }
   241 
   242 static cell_t sm_ArcTangent2(IPluginContext *pCtx, const cell_t *params)
   243 {
   244 	float val1 = sp_ctof(params[1]);
   245 	float val2 = sp_ctof(params[2]);
   246 	val1 = atan2(val1, val2);
   247 
   248 	return sp_ftoc(val1);
   249 }
   250 
   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 
   287 REGISTER_NATIVES(floatnatives)
   288 {
   289 	{"float",			sm_float},
   290 	{"FloatMul",		sm_FloatMul},
   291 	{"FloatDiv",		sm_FloatDiv},
   292 	{"FloatAdd",		sm_FloatAdd},
   293 	{"FloatSub",		sm_FloatSub},
   294 	{"FloatFraction",	sm_FloatFraction},
   295 	{"RoundToZero",		sm_RoundToZero},
   296 	{"RoundToCeil",		sm_RoundToCeil},
   297 	{"RoundToFloor",	sm_RoundToFloor},
   298 	{"RoundToNearest",	sm_RoundToNearest},
   299 	{"FloatCompare",	sm_FloatCompare},
   300 	{"SquareRoot",		sm_SquareRoot},
   301 	{"Pow",				sm_Pow},
   302 	{"Exponential",		sm_Exponential},
   303 	{"Logarithm",		sm_Logarithm},
   304 	{"Sine",			sm_Sine},
   305 	{"Cosine",			sm_Cosine},
   306 	{"Tangent",			sm_Tangent},
   307 	{"FloatAbs",		sm_FloatAbs},
   308 	{"ArcTangent",		sm_ArcTangent},
   309 	{"ArcCosine",		sm_ArcCosine},
   310 	{"ArcSine",			sm_ArcSine},
   311 	{"ArcTangent2",		sm_ArcTangent2},
   312 	{"FloatRound",		sm_FloatRound},			/* Backwards compat shim */
   313 	{NULL,				NULL}
   314 };