core/smn_float.cpp
author Borja Ferrer <faluco@alliedmods.net>
Thu Dec 28 00:48:09 2006 +0000 (2006-12-28)
changeset 226 4663ea3fc2d3
parent 225 dd5851605e26
child 233 968f92668138
permissions -rw-r--r--
Added PlayerManager basic hookings
Moved ftoc and ctof to an utility file
Added some more string natives
     1 #include <math.h>
     2 #include <string.h>
     3 #include "sp_vm_api.h"
     4 #include "sp_vm_typeutil.h"
     5 
     6 using namespace SourcePawn;
     7 
     8 #define PI 3.14159265358979323846
     9 
    10 inline float AngleToRadians(float val, int mode)
    11 {
    12 	switch (mode)
    13 	{
    14 	case 1:
    15 		{
    16 			return (float)((val * PI) / 180.0);
    17 		}
    18 	case 2:
    19 		{
    20 			return (float)((val * PI) / 200.0);
    21 		}
    22 	default:
    23 		{
    24 			return val;
    25 		}
    26 	}
    27 }
    28 
    29 inline float RadiansToAngle(float val, int mode)
    30 {
    31 	switch (mode)
    32 	{
    33 	case 1:
    34 		{
    35 			return (float)((val / PI) * 180.0);
    36 		}
    37 	case 2:
    38 		{
    39 			return (float)((val / PI) * 200.0);
    40 		}
    41 	default:
    42 		{
    43 			return val;
    44 		}
    45 	}
    46 }
    47 
    48 
    49 /****************************************
    50 *                                       *
    51 * FLOATING POINT NATIVE IMPLEMENTATIONS *
    52 *                                       *
    53 ****************************************/
    54 
    55 
    56 static cell_t sm_float(IPluginContext *pCtx, const cell_t *params)
    57 {
    58 	float val = static_cast<float>(params[1]);
    59 
    60 	return ftoc(val);
    61 }
    62 
    63 static cell_t sm_floatabs(IPluginContext *pCtx, const cell_t *params)
    64 {
    65 	float val = ctof(params[1]);
    66 	val = (val >= 0) ? val : -val;
    67 
    68 	return ftoc(val);
    69 }
    70 
    71 static cell_t sm_floatadd(IPluginContext *pCtx, const cell_t *params)
    72 {
    73 	float val = ctof(params[1]) + ctof(params[2]);
    74 
    75 	return ftoc(val);
    76 }
    77 
    78 static cell_t sm_floatsub(IPluginContext *pCtx, const cell_t *params)
    79 {
    80 	float val = ctof(params[1]) - ctof(params[2]);
    81 
    82 	return ftoc(val);
    83 }
    84 
    85 static cell_t sm_floatmul(IPluginContext *pCtx, const cell_t *params)
    86 {
    87 	float val = ctof(params[1]) * ctof(params[2]);
    88 
    89 	return ftoc(val);
    90 }
    91 
    92 static cell_t sm_floatdiv(IPluginContext *pCtx, const cell_t *params)
    93 {
    94 	float val = ctof(params[1]) / ctof(params[2]);
    95 
    96 	return ftoc(val);
    97 }
    98 
    99 static cell_t sm_floatcmp(IPluginContext *pCtx, const cell_t *params)
   100 {
   101 	float val1 = ctof(params[1]);
   102 	float val2 = ctof(params[2]);
   103 
   104 	if (val1 > val2)
   105 	{
   106 		return 1;
   107 	} else if (val1 < val2) {
   108 		return -1;
   109 	}
   110 
   111 	return 0;
   112 }
   113 
   114 static cell_t sm_floatlog(IPluginContext *pCtx, const cell_t *params)
   115 {
   116 	float val = ctof(params[1]);
   117 	float base = ctof(params[2]);
   118 
   119 	if ((val <= 0) || (base <= 0))
   120 	{
   121 		//:TODO: error out! logs cant take in negative numbers and log 0=-inf
   122 	}
   123 	if (base == 10.0)
   124 	{
   125 		val = log10(val);
   126 	} else {
   127 		val = log(val) / log(base);
   128 	}
   129 
   130 	return ftoc(val);
   131 }
   132 
   133 static cell_t sm_floatexp(IPluginContext *pCtx, const cell_t *params)
   134 {
   135 	float val = ctof(params[1]);
   136 
   137 	return ftoc(exp(val));
   138 }
   139 
   140 static cell_t sm_floatpower(IPluginContext *pCtx, const cell_t *params)
   141 {
   142 	float base = ctof(params[1]);
   143 	float exponent = ctof(params[2]);
   144 
   145 	return ftoc(pow(base, exponent));
   146 }
   147 
   148 static cell_t sm_floatsqroot(IPluginContext *pCtx, const cell_t *params)
   149 {
   150 	float val = ctof(params[1]);
   151 
   152 	if (val < 0.0)
   153 	{
   154 		//:TODO: error out! we dont support complex numbers
   155 	}
   156 
   157 	return ftoc(sqrt(val));
   158 }
   159 
   160 static cell_t sm_floatround(IPluginContext *pCtx, const cell_t *params)
   161 {
   162 	float val = ctof(params[1]);
   163 
   164 	switch (params[2])
   165 	{
   166 	case 1:
   167 		{
   168 			val = floor(val);
   169 			break;
   170 		}
   171 	case 2:
   172 		{
   173 			val = ceil(val);
   174 			break;
   175 		}
   176 	case 3:
   177 		{
   178 			if (val >= 0.0)
   179 			{
   180 				val = floor(val);
   181 			} else {
   182 				val = ceil(val);
   183 			}
   184 			break;
   185 		}
   186 	default:
   187 		{
   188 			val = (float)floor(val + 0.5);
   189 			break;
   190 		}
   191 	}
   192 
   193 	return ftoc(val);
   194 }
   195 
   196 static cell_t sm_floatstr(IPluginContext *pCtx, const cell_t *params)
   197 {
   198 	char *str;
   199 
   200 	pCtx->LocalToString(params[1], &str);
   201 	if (!strlen(str))
   202 	{
   203 		return 0;
   204 	}
   205 
   206 	return ftoc((float)atof(str));
   207 }
   208 
   209 static cell_t sm_floatfract(IPluginContext *pCtx, const cell_t *params)
   210 {
   211 	float val = ctof(params[1]);
   212 	val = val - floor(val);
   213 
   214 	return ftoc(val);
   215 }
   216 
   217 static cell_t sm_floatsin(IPluginContext *pCtx, const cell_t *params)
   218 {
   219 	float val = ctof(params[1]);
   220 	val = sin(AngleToRadians(val, params[2]));
   221 
   222 	return ftoc(val);
   223 }
   224 
   225 static cell_t sm_floatcos(IPluginContext *pCtx, const cell_t *params)
   226 {
   227 	float val = ctof(params[1]);
   228 	val = cos(AngleToRadians(val, params[2]));
   229 
   230 	return ftoc(val);
   231 }
   232 
   233 static cell_t sm_floattan(IPluginContext *pCtx, const cell_t *params)
   234 {
   235 	float val = ctof(params[1]);
   236 	val = tan(AngleToRadians(val, params[2]));
   237 
   238 	return ftoc(val);
   239 }
   240 
   241 static cell_t sm_floatasin(IPluginContext *pCtx, const cell_t *params)
   242 {
   243 	float val = ctof(params[1]);
   244 	val = asin(val);
   245 
   246 	return ftoc(RadiansToAngle(val, params[2]));
   247 }
   248 
   249 static cell_t sm_floatacos(IPluginContext *pCtx, const cell_t *params)
   250 {
   251 	float val = ctof(params[1]);
   252 	val = acos(val);
   253 
   254 	return ftoc(RadiansToAngle(val, params[2]));
   255 }
   256 
   257 static cell_t sm_floatatan(IPluginContext *pCtx, const cell_t *params)
   258 {
   259 	float val = ctof(params[1]);
   260 	val = atan(val);
   261 
   262 	return ftoc(RadiansToAngle(val, params[2]));
   263 }
   264 
   265 static cell_t sm_floatatan2(IPluginContext *pCtx, const cell_t *params)
   266 {
   267 	float val1 = ctof(params[1]);
   268 	float val2 = ctof(params[2]);
   269 	val1 = atan2(val1, val2);
   270 
   271 	return ftoc(RadiansToAngle(val1, params[3]));
   272 }