forked from ground/cground
ground library version 1.1.5
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <limits.h>
|
||||
#include <float.h>
|
||||
|
||||
|
||||
GroundValue ground_sin(GroundScope* scope, List args) {
|
||||
@@ -101,6 +103,62 @@ GroundValue ground_clamp(GroundScope* scope, List args) {
|
||||
);
|
||||
}
|
||||
|
||||
GroundValue ground_round(GroundScope* scope, List args) {
|
||||
double x = args.values[0].data.doubleVal;
|
||||
|
||||
if (x >= 0.0)
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
(long long)(x + 0.5)
|
||||
);
|
||||
else
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
(long long)(x - 0.5)
|
||||
);
|
||||
}
|
||||
GroundValue ground_floor(GroundScope* scope, List args) {
|
||||
double x = args.values[0].data.doubleVal;
|
||||
|
||||
if (x >= (double)LLONG_MAX) return groundCreateValue(INT, LLONG_MAX);
|
||||
if (x <= (double)LLONG_MIN) return groundCreateValue(INT, LLONG_MIN);
|
||||
|
||||
long long i = (long long)x; // truncates toward zero
|
||||
|
||||
if (x < 0 && x != (double)i) {
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
i-1
|
||||
);
|
||||
}
|
||||
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
i
|
||||
);
|
||||
}
|
||||
GroundValue ground_ceil(GroundScope* scope, List args) {
|
||||
double x = args.values[0].data.doubleVal;
|
||||
|
||||
if (x >= (double)LLONG_MAX) return groundCreateValue(INT, LLONG_MAX);
|
||||
if (x <= (double)LLONG_MIN) return groundCreateValue(INT, LLONG_MIN);
|
||||
|
||||
|
||||
long long i = (long long)x; // truncates toward zero
|
||||
|
||||
if (x > 0 && x != (double)i) {
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
i+1
|
||||
);
|
||||
}
|
||||
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
i
|
||||
);
|
||||
}
|
||||
|
||||
GroundValue ground_random(GroundScope* scope, List args) {
|
||||
int64_t min = args.values[0].data.intVal;
|
||||
int64_t max = args.values[1].data.intVal;
|
||||
@@ -115,7 +173,7 @@ GroundValue ground_random_double(GroundScope* scope, List args) {
|
||||
double max = args.values[1].data.doubleVal;
|
||||
|
||||
return groundCreateValue(
|
||||
INT,
|
||||
DOUBLE,
|
||||
min + (double)rand() / RAND_MAX * (max - min)
|
||||
);
|
||||
}
|
||||
@@ -128,7 +186,8 @@ GroundValue ground_random_set_seed(GroundScope* scope, List args) {
|
||||
}
|
||||
|
||||
void ground_init(GroundScope* scope) {
|
||||
srand(time(NULL));
|
||||
srand((unsigned)time(NULL) ^ (unsigned)clock());
|
||||
|
||||
groundAddNativeFunction(scope, "math_Sin", ground_sin, DOUBLE, 1, DOUBLE, "radians");
|
||||
groundAddNativeFunction(scope, "math_Cos", ground_cos, DOUBLE, 1, DOUBLE, "radians");
|
||||
groundAddNativeFunction(scope, "math_Tan", ground_tan, DOUBLE, 1, DOUBLE, "radians");
|
||||
@@ -144,6 +203,10 @@ void ground_init(GroundScope* scope) {
|
||||
groundAddNativeFunction(scope, "math_Max", ground_max, DOUBLE, 2, DOUBLE, "number1", DOUBLE, "number2");
|
||||
groundAddNativeFunction(scope, "math_Clamp", ground_clamp, DOUBLE, 2, DOUBLE, "number", DOUBLE, "min", DOUBLE, "max");
|
||||
|
||||
groundAddNativeFunction(scope, "math_Round", ground_round, INT, 1, DOUBLE, "number");
|
||||
groundAddNativeFunction(scope, "math_Floor", ground_floor, INT, 1, DOUBLE, "number");
|
||||
groundAddNativeFunction(scope, "math_Ceil", ground_ceil, INT, 1, DOUBLE, "number");
|
||||
|
||||
groundAddNativeFunction(scope, "math_Random", ground_random, INT, 2, INT, "min", INT, "max");
|
||||
groundAddNativeFunction(scope, "math_RandomDouble", ground_random_double, DOUBLE, 2, DOUBLE, "min", DOUBLE, "max");
|
||||
groundAddNativeFunction(scope, "math_RandomSetSeed", ground_random_set_seed, INT, 1, INT, "seed");
|
||||
|
||||
Reference in New Issue
Block a user