core/logic/smn_float.cpp
author David Anderson <dvander@alliedmods.net>
Thu Aug 08 22:02:42 2013 -0700 (2013-08-08)
changeset 3980 dc7dfcd855dc
parent 2833 e7e7e77e679d
child 4327 60e4042f2589
permissions -rw-r--r--
More Windows build fix.
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include <math.h>
    33 #include <string.h>
    34 #include <stdlib.h>
    35 #include "common_logic.h"
    36 #include "MersenneTwister.h"
    37 #include <IPluginSys.h>
    38 
    39 /****************************************
    40 *                                       *
    41 * FLOATING POINT NATIVE IMPLEMENTATIONS *
    42 *                                       *
    43 ****************************************/
    44 
    45 
    46 static cell_t sm_float(IPluginContext *pCtx, const cell_t *params)
    47 {
    48 	float val = static_cast<float>(params[1]);
    49 
    50 	return sp_ftoc(val);
    51 }
    52 
    53 static cell_t sm_FloatAbs(IPluginContext *pCtx, const cell_t *params)
    54 {
    55 	float val = sp_ctof(params[1]);
    56 	val = (val >= 0.0f) ? val : -val;
    57 
    58 	return sp_ftoc(val);
    59 }
    60 
    61 static cell_t sm_FloatAdd(IPluginContext *pCtx, const cell_t *params)
    62 {
    63 	float val = sp_ctof(params[1]) + sp_ctof(params[2]);
    64 
    65 	return sp_ftoc(val);
    66 }
    67 
    68 static cell_t sm_FloatSub(IPluginContext *pCtx, const cell_t *params)
    69 {
    70 	float val = sp_ctof(params[1]) - sp_ctof(params[2]);
    71 
    72 	return sp_ftoc(val);
    73 }
    74 
    75 static cell_t sm_FloatMul(IPluginContext *pCtx, const cell_t *params)
    76 {
    77 	float val = sp_ctof(params[1]) * sp_ctof(params[2]);
    78 
    79 	return sp_ftoc(val);
    80 }
    81 
    82 static cell_t sm_FloatDiv(IPluginContext *pCtx, const cell_t *params)
    83 {
    84 	float val = sp_ctof(params[1]) / sp_ctof(params[2]);
    85 
    86 	return sp_ftoc(val);
    87 }
    88 
    89 static cell_t sm_FloatCompare(IPluginContext *pCtx, const cell_t *params)
    90 {
    91 	float val1 = sp_ctof(params[1]);
    92 	float val2 = sp_ctof(params[2]);
    93 
    94 	if (val1 > val2)
    95 	{
    96 		return 1;
    97 	} else if (val1 < val2) {
    98 		return -1;
    99 	}
   100 
   101 	return 0;
   102 }
   103 
   104 static cell_t sm_Logarithm(IPluginContext *pCtx, const cell_t *params)
   105 {
   106 	float val = sp_ctof(params[1]);
   107 	float base = sp_ctof(params[2]);
   108 
   109 	if ((val <= 0.0f) || (base <= 0.0f))
   110 	{
   111 		return pCtx->ThrowNativeError("Cannot evaluate the logarithm of zero or a negative number (val:%f base:%f)", val, base);
   112 	}
   113 	if (base == 10.0f)
   114 	{
   115 		val = log10(val);
   116 	} else {
   117 		val = log(val) / log(base);
   118 	}
   119 
   120 	return sp_ftoc(val);
   121 }
   122 
   123 static cell_t sm_Exponential(IPluginContext *pCtx, const cell_t *params)
   124 {
   125 	float val = sp_ctof(params[1]);
   126 
   127 	return sp_ftoc(exp(val));
   128 }
   129 
   130 static cell_t sm_Pow(IPluginContext *pCtx, const cell_t *params)
   131 {
   132 	float base = sp_ctof(params[1]);
   133 	float exponent = sp_ctof(params[2]);
   134 
   135 	return sp_ftoc(pow(base, exponent));
   136 }
   137 
   138 static cell_t sm_SquareRoot(IPluginContext *pCtx, const cell_t *params)
   139 {
   140 	float val = sp_ctof(params[1]);
   141 
   142 	if (val < 0.0f)
   143 	{
   144 		return pCtx->ThrowNativeError("Cannot evaluate the square root of a negative number (val:%f)", val);
   145 	}
   146 
   147 	return sp_ftoc(sqrt(val));
   148 }
   149 
   150 static cell_t sm_RoundToNearest(IPluginContext *pCtx, const cell_t *params)
   151 {
   152 	float val = sp_ctof(params[1]);
   153 	val = (float)floor(val + 0.5f);
   154 
   155 	return static_cast<int>(val);
   156 }
   157 
   158 static cell_t sm_RoundToFloor(IPluginContext *pCtx, const cell_t *params)
   159 {
   160 	float val = sp_ctof(params[1]);
   161 	val = floor(val);
   162 
   163 	return static_cast<int>(val);
   164 }
   165 
   166 static cell_t sm_RoundToCeil(IPluginContext *pCtx, const cell_t *params)
   167 {
   168 	float val = sp_ctof(params[1]);
   169 	val = ceil(val);
   170 
   171 	return static_cast<int>(val);
   172 }
   173 
   174 static cell_t sm_RoundToZero(IPluginContext *pCtx, const cell_t *params)
   175 {
   176 	float val = sp_ctof(params[1]);
   177 	if (val >= 0.0f)
   178 	{
   179 		val = floor(val);
   180 	} else {
   181 		val = ceil(val);
   182 	}
   183 
   184 	return static_cast<int>(val);
   185 }
   186 
   187 static cell_t sm_FloatFraction(IPluginContext *pCtx, const cell_t *params)
   188 {
   189 	float val = sp_ctof(params[1]);
   190 	val = val - floor(val);
   191 
   192 	return sp_ftoc(val);
   193 }
   194 
   195 static cell_t sm_Sine(IPluginContext *pCtx, const cell_t *params)
   196 {
   197 	float val = sp_ctof(params[1]);
   198 	val = sin(val);
   199 
   200 	return sp_ftoc(val);
   201 }
   202 
   203 static cell_t sm_Cosine(IPluginContext *pCtx, const cell_t *params)
   204 {
   205 	float val = sp_ctof(params[1]);
   206 	val = cos(val);
   207 
   208 	return sp_ftoc(val);
   209 }
   210 
   211 static cell_t sm_Tangent(IPluginContext *pCtx, const cell_t *params)
   212 {
   213 	float val = sp_ctof(params[1]);
   214 	val = tan(val);
   215 
   216 	return sp_ftoc(val);
   217 }
   218 
   219 static cell_t sm_ArcSine(IPluginContext *pCtx, const cell_t *params)
   220 {
   221 	float val = sp_ctof(params[1]);
   222 	val = asin(val);
   223 
   224 	return sp_ftoc(val);
   225 }
   226 
   227 static cell_t sm_ArcCosine(IPluginContext *pCtx, const cell_t *params)
   228 {
   229 	float val = sp_ctof(params[1]);
   230 	val = acos(val);
   231 
   232 	return sp_ftoc(val);
   233 }
   234 
   235 static cell_t sm_ArcTangent(IPluginContext *pCtx, const cell_t *params)
   236 {
   237 	float val = sp_ctof(params[1]);
   238 	val = atan(val);
   239 
   240 	return sp_ftoc(val);
   241 }
   242 
   243 static cell_t sm_ArcTangent2(IPluginContext *pCtx, const cell_t *params)
   244 {
   245 	float val1 = sp_ctof(params[1]);
   246 	float val2 = sp_ctof(params[2]);
   247 	val1 = atan2(val1, val2);
   248 
   249 	return sp_ftoc(val1);
   250 }
   251 
   252 #if 0
   253 static cell_t sm_FloatRound(IPluginContext *pCtx, const cell_t *params)
   254 {
   255 	float val = sp_ctof(params[1]);
   256 
   257 	switch (params[2])
   258 	{
   259 	case 1:
   260 		{
   261 			val = floor(val);
   262 			break;
   263 		}
   264 	case 2:
   265 		{
   266 			val = ceil(val);
   267 			break;
   268 		}
   269 	case 3:
   270 		{
   271 			if (val >= 0.0f)
   272 			{
   273 				val = floor(val);
   274 			} else {
   275 				val = ceil(val);
   276 			}
   277 			break;
   278 		}
   279 	default:
   280 		{
   281 			val = (float)floor(val + 0.5f);
   282 			break;
   283 		}
   284 	}
   285 
   286 	return static_cast<int>(val);
   287 }
   288 #endif
   289 
   290 class RandomHelpers :
   291 	public SMGlobalClass,
   292 	public IPluginsListener
   293 {
   294 public:
   295 	void OnSourceModAllInitialized()
   296 	{
   297 		pluginsys->AddPluginsListener(this);
   298 	}
   299 
   300 	void OnSourceModShutdown()
   301 	{
   302 		pluginsys->RemovePluginsListener(this);
   303 	}
   304 
   305 	void OnPluginDestroyed(IPlugin *plugin)
   306 	{
   307 		MTRand *mtrand;
   308 		if (plugin->GetProperty("core.logic.mtrand", (void**)&mtrand, true))
   309 		{
   310 			delete mtrand;
   311 		}
   312 	}
   313 	
   314 	MTRand *RandObjForPlugin(IPluginContext *ctx)
   315 	{
   316 		IPlugin *plugin = pluginsys->FindPluginByContext(ctx->GetContext());
   317 		MTRand *mtrand;	
   318 		if (!plugin->GetProperty("core.logic.mtrand", (void**)&mtrand))
   319 		{
   320 			mtrand = new MTRand();
   321 			plugin->SetProperty("core.logic.mtrand", mtrand);
   322 		}
   323 		return mtrand;
   324 	}
   325 } s_RandHelpers;
   326 
   327 static cell_t GetURandomInt(IPluginContext *ctx, const cell_t *params)
   328 {
   329 	MTRand *randobj = s_RandHelpers.RandObjForPlugin(ctx);
   330 	/* Note the sign bit must be stripped off because cell_t is signed,
   331 	 * and this guarantees a range of [0,max_int]
   332 	 */
   333 	return randobj->randInt() & 0x7FFFFFFF;
   334 }
   335 
   336 static cell_t GetURandomFloat(IPluginContext *ctx, const cell_t *params)
   337 {
   338 	MTRand *randobj = s_RandHelpers.RandObjForPlugin(ctx);
   339 	return sp_ftoc((float)randobj->rand());
   340 }
   341 
   342 static cell_t SetURandomSeed(IPluginContext *ctx, const cell_t *params)
   343 {
   344 	MTRand *randobj = s_RandHelpers.RandObjForPlugin(ctx);
   345 	cell_t *addr;
   346 	ctx->LocalToPhysAddr(params[1], &addr);
   347 	/* We're 32-bit only. */
   348 	randobj->seed((MTRand::uint32*)addr, params[2]);
   349 	return 1;
   350 }
   351 
   352 REGISTER_NATIVES(floatnatives)
   353 {
   354 	{"float",			sm_float},
   355 	{"FloatMul",		sm_FloatMul},
   356 	{"FloatDiv",		sm_FloatDiv},
   357 	{"FloatAdd",		sm_FloatAdd},
   358 	{"FloatSub",		sm_FloatSub},
   359 	{"FloatFraction",	sm_FloatFraction},
   360 	{"RoundToZero",		sm_RoundToZero},
   361 	{"RoundToCeil",		sm_RoundToCeil},
   362 	{"RoundToFloor",	sm_RoundToFloor},
   363 	{"RoundToNearest",	sm_RoundToNearest},
   364 	{"FloatCompare",	sm_FloatCompare},
   365 	{"SquareRoot",		sm_SquareRoot},
   366 	{"Pow",				sm_Pow},
   367 	{"Exponential",		sm_Exponential},
   368 	{"Logarithm",		sm_Logarithm},
   369 	{"Sine",			sm_Sine},
   370 	{"Cosine",			sm_Cosine},
   371 	{"Tangent",			sm_Tangent},
   372 	{"FloatAbs",		sm_FloatAbs},
   373 	{"ArcTangent",		sm_ArcTangent},
   374 	{"ArcCosine",		sm_ArcCosine},
   375 	{"ArcSine",			sm_ArcSine},
   376 	{"ArcTangent2",		sm_ArcTangent2},
   377 	{"GetURandomInt",	GetURandomInt},
   378 	{"GetURandomFloat",	GetURandomFloat},
   379 	{"SetURandomSeed",	SetURandomSeed},
   380 	{NULL,				NULL}
   381 };
   382