extensions can now be built for multiple engines, and will autoload from auto.X.Y folders
3 * =============================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
35 #include "sm_globals.h"
37 /****************************************
39 * FLOATING POINT NATIVE IMPLEMENTATIONS *
41 ****************************************/
44 static cell_t sm_float(IPluginContext *pCtx, const cell_t *params)
46 float val = static_cast<float>(params[1]);
51 static cell_t sm_FloatAbs(IPluginContext *pCtx, const cell_t *params)
53 float val = sp_ctof(params[1]);
54 val = (val >= 0.0f) ? val : -val;
59 static cell_t sm_FloatAdd(IPluginContext *pCtx, const cell_t *params)
61 float val = sp_ctof(params[1]) + sp_ctof(params[2]);
66 static cell_t sm_FloatSub(IPluginContext *pCtx, const cell_t *params)
68 float val = sp_ctof(params[1]) - sp_ctof(params[2]);
73 static cell_t sm_FloatMul(IPluginContext *pCtx, const cell_t *params)
75 float val = sp_ctof(params[1]) * sp_ctof(params[2]);
80 static cell_t sm_FloatDiv(IPluginContext *pCtx, const cell_t *params)
82 float val = sp_ctof(params[1]) / sp_ctof(params[2]);
87 static cell_t sm_FloatCompare(IPluginContext *pCtx, const cell_t *params)
89 float val1 = sp_ctof(params[1]);
90 float val2 = sp_ctof(params[2]);
95 } else if (val1 < val2) {
102 static cell_t sm_Logarithm(IPluginContext *pCtx, const cell_t *params)
104 float val = sp_ctof(params[1]);
105 float base = sp_ctof(params[2]);
107 if ((val <= 0.0f) || (base <= 0.0f))
109 return pCtx->ThrowNativeError("Cannot evaluate the logarithm of zero or a negative number (val:%f base:%f)", val, base);
115 val = log(val) / log(base);
121 static cell_t sm_Exponential(IPluginContext *pCtx, const cell_t *params)
123 float val = sp_ctof(params[1]);
125 return sp_ftoc(exp(val));
128 static cell_t sm_Pow(IPluginContext *pCtx, const cell_t *params)
130 float base = sp_ctof(params[1]);
131 float exponent = sp_ctof(params[2]);
133 return sp_ftoc(pow(base, exponent));
136 static cell_t sm_SquareRoot(IPluginContext *pCtx, const cell_t *params)
138 float val = sp_ctof(params[1]);
142 return pCtx->ThrowNativeError("Cannot evaluate the square root of a negative number (val:%f)", val);
145 return sp_ftoc(sqrt(val));
148 static cell_t sm_RoundToNearest(IPluginContext *pCtx, const cell_t *params)
150 float val = sp_ctof(params[1]);
151 val = (float)floor(val + 0.5f);
153 return static_cast<int>(val);
156 static cell_t sm_RoundToFloor(IPluginContext *pCtx, const cell_t *params)
158 float val = sp_ctof(params[1]);
161 return static_cast<int>(val);
164 static cell_t sm_RoundToCeil(IPluginContext *pCtx, const cell_t *params)
166 float val = sp_ctof(params[1]);
169 return static_cast<int>(val);
172 static cell_t sm_RoundToZero(IPluginContext *pCtx, const cell_t *params)
174 float val = sp_ctof(params[1]);
182 return static_cast<int>(val);
185 static cell_t sm_FloatFraction(IPluginContext *pCtx, const cell_t *params)
187 float val = sp_ctof(params[1]);
188 val = val - floor(val);
193 static cell_t sm_Sine(IPluginContext *pCtx, const cell_t *params)
195 float val = sp_ctof(params[1]);
201 static cell_t sm_Cosine(IPluginContext *pCtx, const cell_t *params)
203 float val = sp_ctof(params[1]);
209 static cell_t sm_Tangent(IPluginContext *pCtx, const cell_t *params)
211 float val = sp_ctof(params[1]);
217 static cell_t sm_ArcSine(IPluginContext *pCtx, const cell_t *params)
219 float val = sp_ctof(params[1]);
225 static cell_t sm_ArcCosine(IPluginContext *pCtx, const cell_t *params)
227 float val = sp_ctof(params[1]);
233 static cell_t sm_ArcTangent(IPluginContext *pCtx, const cell_t *params)
235 float val = sp_ctof(params[1]);
241 static cell_t sm_ArcTangent2(IPluginContext *pCtx, const cell_t *params)
243 float val1 = sp_ctof(params[1]);
244 float val2 = sp_ctof(params[2]);
245 val1 = atan2(val1, val2);
247 return sp_ftoc(val1);
250 static cell_t sm_FloatRound(IPluginContext *pCtx, const cell_t *params)
252 float val = sp_ctof(params[1]);
278 val = (float)floor(val + 0.5f);
283 return static_cast<int>(val);
286 REGISTER_NATIVES(floatnatives)
289 {"FloatMul", sm_FloatMul},
290 {"FloatDiv", sm_FloatDiv},
291 {"FloatAdd", sm_FloatAdd},
292 {"FloatSub", sm_FloatSub},
293 {"FloatFraction", sm_FloatFraction},
294 {"RoundToZero", sm_RoundToZero},
295 {"RoundToCeil", sm_RoundToCeil},
296 {"RoundToFloor", sm_RoundToFloor},
297 {"RoundToNearest", sm_RoundToNearest},
298 {"FloatCompare", sm_FloatCompare},
299 {"SquareRoot", sm_SquareRoot},
301 {"Exponential", sm_Exponential},
302 {"Logarithm", sm_Logarithm},
304 {"Cosine", sm_Cosine},
305 {"Tangent", sm_Tangent},
306 {"FloatAbs", sm_FloatAbs},
307 {"ArcTangent", sm_ArcTangent},
308 {"ArcCosine", sm_ArcCosine},
309 {"ArcSine", sm_ArcSine},
310 {"ArcTangent2", sm_ArcTangent2},
311 {"FloatRound", sm_FloatRound}, /* Backwards compat shim */