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