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_Patch.h" |
8 |
#include "Daodan_Console.h" |
9 |
#include "BFW_ScriptLang.h" |
10 |
#include "Oni.h" |
11 |
#include "Oni_Character.h" |
12 |
#include "oni_gl.h" |
13 |
#include "dSFMT\dSFMT.h" |
14 |
#include "Daodan_Character.h" |
15 |
|
16 |
uint16_t ONICALL bsl_int32mul(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
17 |
{ |
18 |
ret->value_int32 = args[0].value_int32 * args[1].value_int32; |
19 |
ret->type = sl_int32; |
20 |
return 0; |
21 |
} |
22 |
|
23 |
uint16_t ONICALL bsl_mul(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
24 |
{ |
25 |
double val1; |
26 |
double val2; |
27 |
|
28 |
if (args[0].type == sl_int32) |
29 |
val1 = args[0].value_int32; |
30 |
else |
31 |
val1 = args[0].value_float; |
32 |
|
33 |
if (args[1].type == sl_int32) |
34 |
val2 = args[1].value_int32; |
35 |
else |
36 |
val2 = args[1].value_float; |
37 |
|
38 |
ret->value_float = (float)(val1 * val2); |
39 |
ret->type = sl_float; |
40 |
return 0; |
41 |
} |
42 |
|
43 |
uint16_t ONICALL bsl_int32div(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
44 |
{ |
45 |
ret->value_int32 = args[0].value_int32 / args[1].value_int32; |
46 |
ret->type = sl_int32; |
47 |
return 0; |
48 |
} |
49 |
|
50 |
uint16_t ONICALL bsl_div(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
51 |
{ |
52 |
double val1; |
53 |
double val2; |
54 |
|
55 |
if (args[0].type == sl_int32) |
56 |
val1 = args[0].value_int32; |
57 |
else |
58 |
val1 = args[0].value_float; |
59 |
|
60 |
if (args[1].type == sl_int32) |
61 |
val2 = args[1].value_int32; |
62 |
else |
63 |
val2 = args[1].value_float; |
64 |
|
65 |
ret->value_float = (float)(val1 / val2); |
66 |
ret->type = sl_float; |
67 |
return 0; |
68 |
} |
69 |
|
70 |
uint16_t ONICALL bsl_int32rand(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
71 |
{ |
72 |
int32_t start = 0; |
73 |
int32_t end = 0; |
74 |
|
75 |
if (args[0].value_int32 == args[1].value_int32) |
76 |
return 1; |
77 |
else if (args[0].value_int32 > args[1].value_int32) |
78 |
{ |
79 |
start = args[1].value_int32; |
80 |
end = args[0].value_int32; |
81 |
} |
82 |
else |
83 |
{ |
84 |
start = args[0].value_int32; |
85 |
end = args[1].value_int32; |
86 |
} |
87 |
|
88 |
ret->value_int32 = start + (dsfmt_gv_genrand_uint32() % (uint32_t)(end - start + 1)); |
89 |
ret->type = sl_int32; |
90 |
return 0; |
91 |
} |
92 |
|
93 |
uint16_t ONICALL bsl_getkills(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
94 |
{ |
95 |
int index; |
96 |
if (numargs == 0) index = 0; |
97 |
else if (args[0].type == sl_str32) index = DDrGetCharacterIndexFromName(args[0].value_str32); |
98 |
else index = args[0].value_int32; |
99 |
int* killcount = ONgGameState + index * 0x16A0 + 0x1260 + 0x1670; |
100 |
ret->value_int32 = *killcount; |
101 |
ret->type = sl_int32; |
102 |
return 0; |
103 |
} |
104 |
|
105 |
uint16_t ONICALL bsl_getdamage(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
106 |
{ |
107 |
int index; |
108 |
if (numargs == 0) index = 0; |
109 |
else if (args[0].type == sl_str32) index = DDrGetCharacterIndexFromName(args[0].value_str32); |
110 |
else index = args[0].value_int32; |
111 |
int* killcount = ONgGameState + index * 0x16A0 + 0x1260 + 0x1674; |
112 |
ret->value_int32 = *killcount; |
113 |
ret->type = sl_int32; |
114 |
return 0; |
115 |
} |
116 |
|
117 |
uint16_t ONICALL bsl_powerup(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
118 |
{ |
119 |
char powerup_list[7][16] = {"ammo", "cell", "hypo", "shield", "invis" , "lsi", "bossshield"}; |
120 |
int powerup_offset[7] = { 0xE, 0x12, 0x10, 0x24, 0x26, 0x20, -1}; |
121 |
char powerup_type[16] = "\0"; |
122 |
int index; |
123 |
if (numargs < 2 || args[1].type != sl_str32) return 1; |
124 |
else if (args[0].type == sl_str32) index = DDrGetCharacterIndexFromName(args[0].value_str32); |
125 |
else index = args[0].value_int32; |
126 |
int i; |
127 |
|
128 |
for(i = 0; i < 6; i++) |
129 |
{ |
130 |
if(!strcmp(args[1].value_str32,powerup_list[i])) |
131 |
{ |
132 |
sprintf(powerup_type,"%s",powerup_list[i]); |
133 |
break; |
134 |
} |
135 |
} |
136 |
|
137 |
//todo, add setting |
138 |
|
139 |
if (powerup_type[0] == 0) return 1; |
140 |
Character* Chr = ONgGameState + 0x1260; |
141 |
void* returnval = &(Chr[index]) + 0x194 + powerup_offset[i]; |
142 |
//*returnval = (int)Inv |
143 |
if (numargs >= 2) |
144 |
{ |
145 |
//*health = args[1].value_int32; |
146 |
} |
147 |
ret->value_int32 = (int)*(uint16_t*)returnval; |
148 |
ret->type = sl_int32; |
149 |
return 0; |
150 |
} |
151 |
|
152 |
uint16_t ONICALL bsl_health(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
153 |
{ |
154 |
int index; |
155 |
if (numargs == 0) index = 0; |
156 |
else if (args[0].type == sl_str32) index = DDrGetCharacterIndexFromName(args[0].value_str32); |
157 |
else index = args[0].value_int32; |
158 |
Character* Chr = ONgGameState + 0x1260 ; |
159 |
int* health = &Chr[index].Health; |
160 |
|
161 |
ret->value_int32 = *health; |
162 |
ret->type = sl_int32; |
163 |
|
164 |
if (numargs >= 2) { |
165 |
*health = args[1].value_int32; |
166 |
} |
167 |
ret->value_int32 = *health; |
168 |
ret->type = sl_int32; |
169 |
return 0; |
170 |
} |
171 |
|
172 |
uint16_t ONICALL bsl_maxhealth(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
173 |
{ |
174 |
int index; |
175 |
if (numargs == 0) index = 0; |
176 |
else if (args[0].type == sl_str32) index = DDrGetCharacterIndexFromName(args[0].value_str32); |
177 |
else index = args[0].value_int32; |
178 |
Character* Chr = ONgGameState + 0x1260 ; |
179 |
int* maxhealth = &Chr[index].MaxHealth; |
180 |
int oldmaxhealth = Chr[index].MaxHealth; |
181 |
int oldhealth = Chr->Health; |
182 |
if (numargs >= 2) { |
183 |
*maxhealth = args[1].value_int32; |
184 |
} |
185 |
if (numargs >= 3 && args[2].value_bool) { |
186 |
Chr->Health = (int)(((float)args[1].value_int32 / (float)oldmaxhealth) * (float)oldhealth); |
187 |
} |
188 |
ret->value_int32 = oldmaxhealth; |
189 |
ret->type = sl_int32; |
190 |
return 0; |
191 |
} |
192 |
|
193 |
uint16_t ONICALL bsl_getattacker(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
194 |
{ |
195 |
//broken...not only does it sometimes blam, but it returns a string when i want an int so i can study it :< |
196 |
int index; |
197 |
if (args[0].type == sl_str32) index = DDrGetCharacterIndexFromName(args[0].value_str32); |
198 |
else index = args[0].value_int32; |
199 |
|
200 |
|
201 |
if(ONrGetActiveCharacter(ONgGameState + 0x1260 + index * 0x16A0)) return 1; |
202 |
|
203 |
int attacker= (int)(ONrGetActiveCharacter(ONgGameState + 0x1260 + index * 0x16A0) + 0x120); |
204 |
ret->type = sl_int32; |
205 |
ret->value_int32 = attacker; |
206 |
return 0; |
207 |
} |
208 |
|
209 |
|
210 |
uint16_t ONICALL bsl_chrname(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
211 |
{ |
212 |
int index; |
213 |
if (numargs == 0) index = 0; |
214 |
else if (args[0].type == sl_str32) index = DDrGetCharacterIndexFromName(args[0].value_str32); |
215 |
else index = args[0].value_int32; |
216 |
if (index == -1) { |
217 |
ret->type = sl_str32; |
218 |
ret->value_str32 = "NULL"; |
219 |
return 0; |
220 |
} |
221 |
char* name = ONgGameState + 0x1260 + index * 0x16A0 + 0x14; |
222 |
if (numargs == 2) { |
223 |
strncpy(name, (char*)args[1].value_str32, 31); |
224 |
} |
225 |
|
226 |
ret->type = sl_str32; |
227 |
ret->value_str32 = name; |
228 |
|
229 |
return 0; |
230 |
} |
231 |
|
232 |
|
233 |
uint16_t ONICALL bsl_count(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
234 |
{ |
235 |
//testing numargs... |
236 |
ret->type = sl_int32; |
237 |
ret->value_int32 = numargs; |
238 |
return 0; |
239 |
} |
240 |
|
241 |
uint16_t ONICALL bsl_dprintcolored(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
242 |
{ |
243 |
//TODO: figure out why our implementation of dprint shows after dev mode is turned off |
244 |
RGBA color; |
245 |
RGBA shade; |
246 |
|
247 |
if(numargs == 0) return 0; |
248 |
if(numargs > 1 ) color.R = (char)args[1].value_int32; |
249 |
else color.R = 255; |
250 |
if(numargs > 2 ) color.G = (char)args[2].value_int32; |
251 |
else color.G = 255; |
252 |
if(numargs > 3 ) color.B = (char)args[3].value_int32; |
253 |
else color.B = 255; |
254 |
color.A = 0; |
255 |
if(numargs > 5 ) shade.R = (char)args[5].value_int32; |
256 |
else shade.R = 0x3F; |
257 |
if(numargs > 6 ) shade.G = (char)args[6].value_int32; |
258 |
else shade.G = 0x3F; |
259 |
if(numargs > 7 ) shade.B = (char)args[7].value_int32; |
260 |
else shade.B = 0x3F; |
261 |
shade.A = 0; |
262 |
|
263 |
DDrConsole_PrintColored(args[0].value_str32, 1, color, shade); |
264 |
return 0; |
265 |
} |
266 |
|
267 |
|
268 |
uint16_t ONICALL bsl_nametoindex(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
269 |
{ |
270 |
ret->type = sl_int32; |
271 |
ret->value_int32 = DDrGetCharacterIndexFromName(args[0].value_str32); |
272 |
return 0; |
273 |
} |
274 |
/* |
275 |
uint16_t ONICALL bsl_sprintf(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
276 |
{ |
277 |
if (numargs < 2) |
278 |
return 1; |
279 |
|
280 |
char output[255]; |
281 |
int i; |
282 |
for(i = 1; i < numargs; i++) { |
283 |
sprintf(output, args[0].value_str32, args[i].value_str32); |
284 |
} |
285 |
|
286 |
ret->value_str32 = output; |
287 |
ret->type = sl_str32; |
288 |
return 0; |
289 |
} |
290 |
*/ |
291 |
uint16_t ONICALL bsl_sprintf(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
292 |
{ |
293 |
DDrConsole_PrintF("%d", numargs); |
294 |
|
295 |
if (numargs < 1 || args[0].type != sl_str32) |
296 |
{ |
297 |
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); |
298 |
return 0; |
299 |
} |
300 |
|
301 |
if (!args[0].value_str32) |
302 |
args[0].value_str32 = ""; |
303 |
|
304 |
int ffi_ret; |
305 |
char* str = NULL; |
306 |
int size = 0; |
307 |
|
308 |
ffi_cif cif; |
309 |
ffi_type* ffi_args[256]; |
310 |
void* values[256]; |
311 |
|
312 |
ffi_args[0] = &ffi_type_pointer; |
313 |
values[0] = &str; |
314 |
ffi_args[1] = &ffi_type_uint32; |
315 |
values[1] = &size; |
316 |
|
317 |
int i; |
318 |
for(i = 2; i < numargs + 2; i ++) |
319 |
{ |
320 |
if (args[i - 2].type == sl_float) |
321 |
{ |
322 |
float value_float = args[i - 2].value_float; |
323 |
double* value_double = (double*)&(args[i - 2]); |
324 |
*value_double = value_float; |
325 |
|
326 |
ffi_args[i] = &ffi_type_double; |
327 |
values[i] = value_double; |
328 |
} |
329 |
else |
330 |
{ |
331 |
ffi_args[i] = &ffi_type_pointer; |
332 |
values[i] = &(args[i - 2].value); |
333 |
} |
334 |
} |
335 |
|
336 |
if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, i, &ffi_type_sint32, ffi_args) != FFI_OK) |
337 |
return 1; |
338 |
ffi_call(&cif, (void*)snprintf, (void*)&ffi_ret, values); |
339 |
str = malloc(ffi_ret + 1); |
340 |
size = ffi_ret + 1; |
341 |
ffi_call(&cif, (void*)snprintf, (void*)&ffi_ret, values); |
342 |
ret->value_str32 = str; |
343 |
ret->type = sl_str32; |
344 |
return 0; |
345 |
} |
346 |
|
347 |
// Widescreen patch for talking heads. |
348 |
uint16_t ONICALL cinematic_start_patch(sl_callinfo* callinfo, unsigned int numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
349 |
{ |
350 |
args[1].value_int32 = (double)args[1].value_int32 / (double)(gl->DisplayMode.Width) * (4.0 / 3.0 * (double)(gl->DisplayMode.Height)); |
351 |
return ((sl_func)(OniExe + 0x000f3830))(callinfo, numargs, args, dontuse1, dontuse2, ret); |
352 |
} |
353 |
|
354 |
void SLrDaodan_Initalize() |
355 |
{ |
356 |
SLrScript_Command_Register_ReturnType("int32mul", "Multiplies two numbers", "n1:int n2:int", sl_int32, bsl_int32mul); |
357 |
SLrScript_Command_Register_ReturnType("mul", "Multiplies two numbers", "[int1:int|float1:float] [int2:int|float2:float]", sl_float, bsl_mul); |
358 |
|
359 |
SLrScript_Command_Register_ReturnType("int32div", "Divides two numbers", "n1:int n2:int", sl_int32, bsl_int32div); |
360 |
SLrScript_Command_Register_ReturnType("div", "Divides two numbers", "[int1:int|float1:float] [int2:int|float2:float]", sl_float, bsl_div); |
361 |
|
362 |
dsfmt_gv_init_gen_rand((uint32_t)time(NULL)); |
363 |
SLrScript_Command_Register_ReturnType("int32rand", "Returns a pseudo-random number between two numbers (inclusive).", "start:int end:int", sl_int32, bsl_int32rand); |
364 |
|
365 |
SLrScript_Command_Register_ReturnType("d_getkills","Gets the number of kills a character has", "[ai_name:str | script_id:int]", sl_int32, bsl_getkills); |
366 |
SLrScript_Command_Register_ReturnType("d_getdamage","Gets the amount of damage a character has caused", "[ai_name:string | script_id:int]", sl_int32, bsl_getdamage); |
367 |
SLrScript_Command_Register_ReturnType("d_name","Gets or sets a character's name", "[ai_name:str | script_id:int] [newname:string]", sl_str32, bsl_chrname); |
368 |
SLrScript_Command_Register_ReturnType("d_getindex","Converts a character's name to its index", "script_id:int", sl_int32, bsl_nametoindex); |
369 |
SLrScript_Command_Register_ReturnType("d_health","Gets or sets a character's health", "[ai_name:str | script_id:int] [newhealth:int]", sl_str32, bsl_health); |
370 |
SLrScript_Command_Register_ReturnType("d_maxhealth","Gets or sets a character's maximum health", "[ai_name:str | script_id:int] [newmaxhealth:int] [scalehealth:bool]", sl_str32, bsl_maxhealth); |
371 |
SLrScript_Command_Register_ReturnType("d_powerup","Gets or sets a character's powerups", "ai_name:str|script_id:int powerup:str", sl_int32, bsl_powerup); |
372 |
//broken. sometimes crashes, and sometimes returns a string... : / |
373 |
SLrScript_Command_Register_ReturnType("d_getattacker","Gets the last person to hurt a character", "[ai_name:string | script_id:int]", sl_int32, bsl_chrname); |
374 |
|
375 |
SLrScript_Command_Register_ReturnType("sprintf", "C-style sprintf.", "format:string arg1 arg2 ...", sl_str32, bsl_sprintf); |
376 |
SLrScript_Command_Register_ReturnType("dprintcolor", "prints to console in color", "text:string [color: r b g] [color: r b g]", sl_void, bsl_dprintcolored); |
377 |
} |
378 |
|
379 |
void SLrDaodan_Patch() |
380 |
{ |
381 |
DDrPatch_Int32(OniExe + 0x000f3755, (int)cinematic_start_patch); |
382 |
} |