source: Daodan/src/Daodan_BSL.c@ 440

Last change on this file since 440 was 440, checked in by rossy, 15 years ago

int32rand

File size: 2.9 KB
Line 
1#include <time.h>
2
3#include "Daodan_BSL.h"
4#include "BFW_ScriptingLanguage.h"
5
6#include "dSFMT\dSFMT.h"
7
8uint16_t ONICALL bsl_int32mul(void* dontuse0, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret)
9{
10 if (numargs < 2)
11 return 1;
12 ret->value_int32 = args[0].value_int32 * args[1].value_int32;
13 ret->type = sl_int32;
14 return 0;
15}
16
17uint16_t ONICALL bsl_mul(void* dontuse0, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret)
18{
19 if (numargs < 2)
20 return 1;
21
22 double val1;
23 double val2;
24
25 if (args[0].type == sl_int32)
26 val1 = args[0].value_int32;
27 else
28 val1 = args[0].value_float;
29
30 if (args[1].type == sl_int32)
31 val2 = args[1].value_int32;
32 else
33 val2 = args[1].value_float;
34
35 ret->value_float = (float)(val1 * val2);
36 ret->type = sl_float;
37 return 0;
38}
39
40uint16_t ONICALL bsl_int32div(void* dontuse0, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret)
41{
42 if (numargs < 2)
43 return 1;
44 ret->value_int32 = args[0].value_int32 / args[1].value_int32;
45 ret->type = sl_int32;
46 return 0;
47}
48
49uint16_t ONICALL bsl_div(void* dontuse0, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret)
50{
51 if (numargs < 2)
52 return 1;
53
54 double val1;
55 double val2;
56
57 if (args[0].type == sl_int32)
58 val1 = args[0].value_int32;
59 else
60 val1 = args[0].value_float;
61
62 if (args[1].type == sl_int32)
63 val2 = args[1].value_int32;
64 else
65 val2 = args[1].value_float;
66
67 ret->value_float = (float)(val1 / val2);
68 ret->type = sl_float;
69 return 0;
70}
71
72uint16_t ONICALL bsl_int32rand(void* dontuse0, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret)
73{
74 if (numargs < 2)
75 return 1;
76
77 int32_t start = 0;
78 int32_t end = 0;
79
80 if (args[0].value_int32 == args[1].value_int32)
81 return 1;
82 else if (args[0].value_int32 > args[1].value_int32)
83 {
84 start = args[1].value_int32;
85 end = args[0].value_int32;
86 }
87 else
88 {
89 start = args[0].value_int32;
90 end = args[1].value_int32;
91 }
92
93 ret->value_int32 = start + (dsfmt_gv_genrand_uint32() % (uint32_t)(end - start + 1));
94 ret->type = sl_int32;
95 return 0;
96}
97
98void SLrDaodan_Initalize()
99{
100 SLrScript_Command_Register_ReturnType("int32mul", "Multiplies two numbers", "n1:int n2:int", sl_int32, bsl_int32mul);
101 SLrScript_Command_Register_ReturnType("mul", "Multiplies two numbers", "[int1:int|float1:float] [int2:int|float2:float]", sl_float, bsl_mul);
102
103 SLrScript_Command_Register_ReturnType("int32div", "Divides two numbers", "n1:int n2:int", sl_int32, bsl_int32div);
104 SLrScript_Command_Register_ReturnType("div", "Divides two numbers", "[int1:int|float1:float] [int2:int|float2:float]", sl_float, bsl_div);
105
106 dsfmt_gv_init_gen_rand((uint32_t)time(NULL));
107 SLrScript_Command_Register_ReturnType("int32rand", "Returns a pseudo-random number between two numbers (inclusive).", "start:int end:int", sl_int32, bsl_int32rand);
108}
Note: See TracBrowser for help on using the repository browser.