source: Daodan/src/Daodan_BSL.c@ 445

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

fixed sprintf

File size: 5.9 KB
Line 
1#include <stdio.h>
2#include <time.h>
3#include <ffi.h>
4
5#include "Daodan_BSL.h"
6#include "Daodan_Utility.h"
7#include "Daodan_Console.h"
8#include "BFW_ScriptLang.h"
9#include "Oni.h"
10#include "Oni_Character.h"
11#include "dSFMT\dSFMT.h"
12
13uint16_t ONICALL bsl_int32mul(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret)
14{
15 ret->value_int32 = args[0].value_int32 * args[1].value_int32;
16 ret->type = sl_int32;
17 return 0;
18}
19
20uint16_t ONICALL bsl_mul(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret)
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(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret)
41{
42 ret->value_int32 = args[0].value_int32 / args[1].value_int32;
43 ret->type = sl_int32;
44 return 0;
45}
46
47uint16_t ONICALL bsl_div(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret)
48{
49 double val1;
50 double val2;
51
52 if (args[0].type == sl_int32)
53 val1 = args[0].value_int32;
54 else
55 val1 = args[0].value_float;
56
57 if (args[1].type == sl_int32)
58 val2 = args[1].value_int32;
59 else
60 val2 = args[1].value_float;
61
62 ret->value_float = (float)(val1 / val2);
63 ret->type = sl_float;
64 return 0;
65}
66
67uint16_t ONICALL bsl_int32rand(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret)
68{
69 int32_t start = 0;
70 int32_t end = 0;
71
72 if (args[0].value_int32 == args[1].value_int32)
73 return 1;
74 else if (args[0].value_int32 > args[1].value_int32)
75 {
76 start = args[1].value_int32;
77 end = args[0].value_int32;
78 }
79 else
80 {
81 start = args[0].value_int32;
82 end = args[1].value_int32;
83 }
84
85 ret->value_int32 = start + (dsfmt_gv_genrand_uint32() % (uint32_t)(end - start + 1));
86 ret->type = sl_int32;
87 return 0;
88}
89
90uint16_t ONICALL bsl_getkills(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret)
91{
92 int index;
93 if (numargs == 0) index = 0;
94 else index = args[0].value_int32;
95 int* killcount = ONgGameState + index * 0x16A0 + 0x1260 + 0x1670;
96 ret->value_int32 = *killcount;
97 ret->type = sl_int32;
98 return 0;
99}
100
101uint16_t ONICALL bsl_getdamage(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret)
102{
103 int index;
104 if (numargs == 0) index = 0;
105 else index = args[0].value_int32;
106 int* killcount = ONgGameState + index * 0x16A0 + 0x1260 + 0x1674;
107 ret->value_int32 = *killcount;
108 ret->type = sl_int32;
109 return 0;
110}
111/*
112uint16_t ONICALL bsl_sprintf(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret)
113{
114 if (numargs < 2)
115 return 1;
116
117 char output[255];
118 int i;
119 for(i = 1; i < numargs; i++) {
120 sprintf(output, args[0].value_str32, args[i].value_str32);
121 }
122
123 ret->value_str32 = output;
124 ret->type = sl_str32;
125 return 0;
126}
127*/
128uint16_t ONICALL bsl_sprintf(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret)
129{
130 DDrConsole_PrintF("%d", numargs);
131
132 if (numargs < 1 || args[0].type != sl_str32)
133 {
134 DDrConsole_PrintF("Func \"%s\", File \"%s\", Line %d: semantic error, \"%s\": parameter list does not match: format:string arg1 arg2 ...", callinfo->name, callinfo->calllocation, callinfo->linenumber, callinfo->name);
135 return 0;
136 }
137
138 if (!args[0].value_str32)
139 args[0].value_str32 = "";
140
141 int ffi_ret;
142 char* str = NULL;
143 int size = 0;
144
145 ffi_cif cif;
146 ffi_type* ffi_args[256];
147 void* values[256];
148
149 ffi_args[0] = &ffi_type_pointer;
150 values[0] = &str;
151 ffi_args[1] = &ffi_type_uint32;
152 values[1] = &size;
153
154 int i;
155 for(i = 2; i < numargs + 2; i ++)
156 {
157 if (args[i - 2].type == sl_float)
158 {
159 float value_float = args[i - 2].value_float;
160 double* value_double = (double*)&(args[i - 2]);
161 *value_double = value_float;
162
163 ffi_args[i] = &ffi_type_double;
164 values[i] = value_double;
165 }
166 else
167 {
168 ffi_args[i] = &ffi_type_pointer;
169 values[i] = &(args[i - 2].value);
170 }
171 }
172
173 if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, i, &ffi_type_sint32, ffi_args) != FFI_OK)
174 return 1;
175 ffi_call(&cif, (void*)snprintf, (void*)&ffi_ret, values);
176 str = malloc(ffi_ret + 1);
177 size = ffi_ret + 1;
178 ffi_call(&cif, (void*)snprintf, (void*)&ffi_ret, values);
179 ret->value_str32 = str;
180 ret->type = sl_str32;
181 return 0;
182}
183
184void SLrDaodan_Initalize()
185{
186 SLrScript_Command_Register_ReturnType("int32mul", "Multiplies two numbers", "n1:int n2:int", sl_int32, bsl_int32mul);
187 SLrScript_Command_Register_ReturnType("mul", "Multiplies two numbers", "[int1:int|float1:float] [int2:int|float2:float]", sl_float, bsl_mul);
188
189 SLrScript_Command_Register_ReturnType("int32div", "Divides two numbers", "n1:int n2:int", sl_int32, bsl_int32div);
190 SLrScript_Command_Register_ReturnType("div", "Divides two numbers", "[int1:int|float1:float] [int2:int|float2:float]", sl_float, bsl_div);
191
192 dsfmt_gv_init_gen_rand((uint32_t)time(NULL));
193 SLrScript_Command_Register_ReturnType("int32rand", "Returns a pseudo-random number between two numbers (inclusive).", "start:int end:int", sl_int32, bsl_int32rand);
194
195 SLrScript_Command_Register_ReturnType("chr_getkills","Gets the number of kills a character has", "[chrindex:int]", sl_int32, bsl_getkills);
196 SLrScript_Command_Register_ReturnType("chr_getdamage","Gets the amount of damage a character has caused", "[chrindex:int]", sl_int32, bsl_getdamage);
197
198 SLrScript_Command_Register_ReturnType("sprintf", "C-style sprintf.", "format:string arg1 arg2 ...", sl_str32, bsl_sprintf);
199}
Note: See TracBrowser for help on using the repository browser.