| 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 |
int index; |
| 120 |
if (numargs < 2 || args[1].type != sl_str32) return 1; |
| 121 |
else if (args[0].type == sl_str32) index = DDrGetCharacterIndexFromName(args[0].value_str32); |
| 122 |
else index = args[0].value_int32; |
| 123 |
void* returnval; |
| 124 |
bool is_lsi = 0; |
| 125 |
Character* Chr = ONgGameState + 0x1260; |
| 126 |
if(!strcmp(args[1].value_str32,"ammo")) |
| 127 |
{ |
| 128 |
returnval = &(Chr[index].Inventory_.AmmoUsed); |
| 129 |
} |
| 130 |
else if(!strcmp(args[1].value_str32,"hypo")) |
| 131 |
{ |
| 132 |
returnval = &(Chr[index].Inventory_.HypoUsed); |
| 133 |
} |
| 134 |
else if(!strcmp(args[1].value_str32,"cells")) |
| 135 |
{ |
| 136 |
returnval = &(Chr[index].Inventory_.CellsUsed); |
| 137 |
} |
| 138 |
else if(!strcmp(args[1].value_str32,"invis")) |
| 139 |
{ |
| 140 |
returnval = &(Chr[index].Inventory_.CloakUsed); |
| 141 |
} |
| 142 |
else if(!strcmp(args[1].value_str32,"shield")) |
| 143 |
{ |
| 144 |
returnval = &(Chr[index].Inventory_.ShieldUsed); |
| 145 |
} |
| 146 |
else if(!strcmp(args[1].value_str32,"lsi")) |
| 147 |
{ |
| 148 |
returnval = &(Chr[index].Inventory_.hasLSI); |
| 149 |
is_lsi = 1; |
| 150 |
} |
| 151 |
else if(!strcmp(args[1].value_str32,"bossshield")) |
| 152 |
{ |
| 153 |
ret->value_int32 = Chr[index].Flags & char_bossshield; |
| 154 |
ret->type = sl_int32; |
| 155 |
if (numargs >=3) { |
| 156 |
if (Chr[index].Flags & char_bossshield) Chr[index].Flags = Chr[index].Flags & ~char_bossshield; |
| 157 |
else Chr[index].Flags = Chr[index].Flags | char_bossshield; |
| 158 |
} |
| 159 |
return 0; |
| 160 |
} |
| 161 |
else return 1; |
| 162 |
//todo, add setting |
| 163 |
|
| 164 |
if(is_lsi) ret->value_int32 = (int)*(bool*)returnval; |
| 165 |
else ret->value_int32 = *(int*)returnval; |
| 166 |
ret->type = sl_int32; |
| 167 |
|
| 168 |
if (numargs >= 3) |
| 169 |
{ |
| 170 |
if(is_lsi) *(bool*)returnval = args[2].value_int32; |
| 171 |
else *(int*)returnval = args[2].value_int32; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
uint16_t ONICALL bsl_health(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
| 179 |
{ |
| 180 |
int index; |
| 181 |
if (numargs == 0) index = 0; |
| 182 |
else if (args[0].type == sl_str32) index = DDrGetCharacterIndexFromName(args[0].value_str32); |
| 183 |
else index = args[0].value_int32; |
| 184 |
Character* Chr = ONgGameState + 0x1260 ; |
| 185 |
int* health = &Chr[index].Health; |
| 186 |
|
| 187 |
ret->value_int32 = *health; |
| 188 |
ret->type = sl_int32; |
| 189 |
|
| 190 |
if (numargs >= 2) { |
| 191 |
*health = args[1].value_int32; |
| 192 |
} |
| 193 |
ret->value_int32 = *health; |
| 194 |
ret->type = sl_int32; |
| 195 |
return 0; |
| 196 |
} |
| 197 |
|
| 198 |
uint16_t ONICALL bsl_maxhealth(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
| 199 |
{ |
| 200 |
int index; |
| 201 |
if (numargs == 0) index = 0; |
| 202 |
else if (args[0].type == sl_str32) index = DDrGetCharacterIndexFromName(args[0].value_str32); |
| 203 |
else index = args[0].value_int32; |
| 204 |
Character* Chr = ONgGameState + 0x1260 ; |
| 205 |
int* maxhealth = &Chr[index].MaxHealth; |
| 206 |
int oldmaxhealth = Chr[index].MaxHealth; |
| 207 |
int oldhealth = Chr->Health; |
| 208 |
if (numargs >= 2) { |
| 209 |
*maxhealth = args[1].value_int32; |
| 210 |
} |
| 211 |
if (numargs >= 3 && args[2].value_bool) { |
| 212 |
Chr->Health = (int)(((float)args[1].value_int32 / (float)oldmaxhealth) * (float)oldhealth); |
| 213 |
} |
| 214 |
ret->value_int32 = oldmaxhealth; |
| 215 |
ret->type = sl_int32; |
| 216 |
return 0; |
| 217 |
} |
| 218 |
|
| 219 |
uint16_t ONICALL bsl_getattacker(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
| 220 |
{ |
| 221 |
//broken...not only does it sometimes blam, but it returns a string when i want an int so i can study it :< |
| 222 |
int index; |
| 223 |
if (numargs == 0) index = 0; |
| 224 |
else if (args[0].type == sl_str32) index = DDrGetCharacterIndexFromName(args[0].value_str32); |
| 225 |
else index = args[0].value_int32; |
| 226 |
|
| 227 |
Character* Chr = ONgGameState + 0x1260; |
| 228 |
ActiveCharacter* Active = (ActiveCharacter*)ONrGetActiveCharacter(&Chr[index]); |
| 229 |
if ((int)Active == 0) return 1; |
| 230 |
ret->value_int32 = Active->LastDamageSourceCharacter; |
| 231 |
ret->type = sl_int32; |
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
uint16_t ONICALL bsl_chrname(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
| 238 |
{ |
| 239 |
int index; |
| 240 |
if (numargs == 0) index = 0; |
| 241 |
else if (args[0].type == sl_str32) index = DDrGetCharacterIndexFromName(args[0].value_str32); |
| 242 |
else index = args[0].value_int32; |
| 243 |
if (index == -1) { |
| 244 |
ret->type = sl_str32; |
| 245 |
ret->value_str32 = "NULL"; |
| 246 |
return 0; |
| 247 |
} |
| 248 |
char* name = ONgGameState + 0x1260 + index * 0x16A0 + 0x14; |
| 249 |
if (numargs == 2) { |
| 250 |
strncpy(name, (char*)args[1].value_str32, 31); |
| 251 |
} |
| 252 |
|
| 253 |
ret->type = sl_str32; |
| 254 |
ret->value_str32 = name; |
| 255 |
|
| 256 |
return 0; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
uint16_t ONICALL bsl_count(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
| 261 |
{ |
| 262 |
//testing numargs... |
| 263 |
ret->type = sl_int32; |
| 264 |
ret->value_int32 = numargs; |
| 265 |
return 0; |
| 266 |
} |
| 267 |
|
| 268 |
uint16_t ONICALL bsl_dprintcolored(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
| 269 |
{ |
| 270 |
//TODO: figure out why our implementation of dprint shows after dev mode is turned off |
| 271 |
RGBA color; |
| 272 |
RGBA shade; |
| 273 |
|
| 274 |
if(numargs == 0) return 0; |
| 275 |
if(numargs > 1 ) color.R = (char)args[1].value_int32; |
| 276 |
else color.R = 255; |
| 277 |
if(numargs > 2 ) color.G = (char)args[2].value_int32; |
| 278 |
else color.G = 255; |
| 279 |
if(numargs > 3 ) color.B = (char)args[3].value_int32; |
| 280 |
else color.B = 255; |
| 281 |
color.A = 0; |
| 282 |
if(numargs > 5 ) shade.R = (char)args[5].value_int32; |
| 283 |
else shade.R = 0x3F; |
| 284 |
if(numargs > 6 ) shade.G = (char)args[6].value_int32; |
| 285 |
else shade.G = 0x3F; |
| 286 |
if(numargs > 7 ) shade.B = (char)args[7].value_int32; |
| 287 |
else shade.B = 0x3F; |
| 288 |
shade.A = 0; |
| 289 |
|
| 290 |
DDrConsole_PrintColored(args[0].value_str32, 1, color, shade); |
| 291 |
return 0; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
uint16_t ONICALL bsl_nametoindex(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
| 296 |
{ |
| 297 |
ret->type = sl_int32; |
| 298 |
ret->value_int32 = DDrGetCharacterIndexFromName(args[0].value_str32); |
| 299 |
return 0; |
| 300 |
} |
| 301 |
|
| 302 |
uint16_t ONICALL bsl_getactiveoffset(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
| 303 |
{ |
| 304 |
DDrConsole_PrintF("Character: 0x%x",(int)ONgGameState + 0x1260); |
| 305 |
DDrConsole_PrintF("ActiveChar: 0x%x",(int)ONrGetActiveCharacter((void*)((int)ONgGameState + 0x1260))); |
| 306 |
return 0; |
| 307 |
} |
| 308 |
/* |
| 309 |
uint16_t ONICALL bsl_sprintf(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
| 310 |
{ |
| 311 |
if (numargs < 2) |
| 312 |
return 1; |
| 313 |
|
| 314 |
char output[255]; |
| 315 |
int i; |
| 316 |
for(i = 1; i < numargs; i++) { |
| 317 |
sprintf(output, args[0].value_str32, args[i].value_str32); |
| 318 |
} |
| 319 |
|
| 320 |
ret->value_str32 = output; |
| 321 |
ret->type = sl_str32; |
| 322 |
return 0; |
| 323 |
} |
| 324 |
*/ |
| 325 |
uint16_t ONICALL bsl_sprintf(sl_callinfo* callinfo, uint32_t numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
| 326 |
{ |
| 327 |
DDrConsole_PrintF("%d", numargs); |
| 328 |
|
| 329 |
if (numargs < 1 || args[0].type != sl_str32) |
| 330 |
{ |
| 331 |
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); |
| 332 |
return 0; |
| 333 |
} |
| 334 |
|
| 335 |
if (!args[0].value_str32) |
| 336 |
args[0].value_str32 = ""; |
| 337 |
|
| 338 |
int ffi_ret; |
| 339 |
char* str = NULL; |
| 340 |
int size = 0; |
| 341 |
|
| 342 |
ffi_cif cif; |
| 343 |
ffi_type* ffi_args[256]; |
| 344 |
void* values[256]; |
| 345 |
|
| 346 |
ffi_args[0] = &ffi_type_pointer; |
| 347 |
values[0] = &str; |
| 348 |
ffi_args[1] = &ffi_type_uint32; |
| 349 |
values[1] = &size; |
| 350 |
|
| 351 |
int i; |
| 352 |
for(i = 2; i < numargs + 2; i ++) |
| 353 |
{ |
| 354 |
if (args[i - 2].type == sl_float) |
| 355 |
{ |
| 356 |
float value_float = args[i - 2].value_float; |
| 357 |
double* value_double = (double*)&(args[i - 2]); |
| 358 |
*value_double = value_float; |
| 359 |
|
| 360 |
ffi_args[i] = &ffi_type_double; |
| 361 |
values[i] = value_double; |
| 362 |
} |
| 363 |
else |
| 364 |
{ |
| 365 |
ffi_args[i] = &ffi_type_pointer; |
| 366 |
values[i] = &(args[i - 2].value); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, i, &ffi_type_sint32, ffi_args) != FFI_OK) |
| 371 |
return 1; |
| 372 |
ffi_call(&cif, (void*)snprintf, (void*)&ffi_ret, values); |
| 373 |
str = malloc(ffi_ret + 1); |
| 374 |
size = ffi_ret + 1; |
| 375 |
ffi_call(&cif, (void*)snprintf, (void*)&ffi_ret, values); |
| 376 |
ret->value_str32 = str; |
| 377 |
ret->type = sl_str32; |
| 378 |
return 0; |
| 379 |
} |
| 380 |
|
| 381 |
// Widescreen patch for talking heads. |
| 382 |
uint16_t ONICALL cinematic_start_patch(sl_callinfo* callinfo, unsigned int numargs, sl_arg args[], void* dontuse1, void* dontuse2, sl_arg* ret) |
| 383 |
{ |
| 384 |
args[1].value_int32 = (double)args[1].value_int32 / (double)(gl->DisplayMode.Width) * (4.0 / 3.0 * (double)(gl->DisplayMode.Height)); |
| 385 |
return ((sl_func)(OniExe + 0x000f3830))(callinfo, numargs, args, dontuse1, dontuse2, ret); |
| 386 |
} |
| 387 |
|
| 388 |
void SLrDaodan_Initalize() |
| 389 |
{ |
| 390 |
SLrScript_Command_Register_ReturnType("int32mul", "Multiplies two numbers", "n1:int n2:int", sl_int32, bsl_int32mul); |
| 391 |
SLrScript_Command_Register_ReturnType("mul", "Multiplies two numbers", "[int1:int|float1:float] [int2:int|float2:float]", sl_float, bsl_mul); |
| 392 |
|
| 393 |
SLrScript_Command_Register_ReturnType("int32div", "Divides two numbers", "n1:int n2:int", sl_int32, bsl_int32div); |
| 394 |
SLrScript_Command_Register_ReturnType("div", "Divides two numbers", "[int1:int|float1:float] [int2:int|float2:float]", sl_float, bsl_div); |
| 395 |
|
| 396 |
dsfmt_gv_init_gen_rand((uint32_t)time(NULL)); |
| 397 |
SLrScript_Command_Register_ReturnType("int32rand", "Returns a pseudo-random number between two numbers (inclusive).", "start:int end:int", sl_int32, bsl_int32rand); |
| 398 |
|
| 399 |
SLrScript_Command_Register_ReturnType("d_getkills","Gets the number of kills a character has", "[ai_name:str | script_id:int]", sl_int32, bsl_getkills); |
| 400 |
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); |
| 401 |
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); |
| 402 |
SLrScript_Command_Register_ReturnType("d_getindex","Converts a character's name to its index", "script_id:int", sl_int32, bsl_nametoindex); |
| 403 |
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); |
| 404 |
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); |
| 405 |
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); |
| 406 |
//broken. sometimes crashes, and sometimes returns a string... : / |
| 407 |
SLrScript_Command_Register_ReturnType("d_getattacker","Gets the last person to hurt a character", "[ai_name:string | script_id:int]", sl_int32, bsl_getattacker); |
| 408 |
|
| 409 |
SLrScript_Command_Register_ReturnType("d_active","Gets the last person to hurt a character", "[ai_name:string | script_id:int]", sl_int32, bsl_getactiveoffset); |
| 410 |
|
| 411 |
SLrScript_Command_Register_ReturnType("sprintf", "C-style sprintf.", "format:string arg1 arg2 ...", sl_str32, bsl_sprintf); |
| 412 |
SLrScript_Command_Register_ReturnType("dprintcolor", "prints to console in color", "text:string [color: r b g] [color: r b g]", sl_void, bsl_dprintcolored); |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
SLrScript_Command_Register_ReturnType("d_dprint", "prints to console in color", "text:string [color: r b g] [color: r b g]", sl_void, bsl_dprintcolored); |
| 417 |
} |
| 418 |
void SLrDaodan_Patch() |
| 419 |
{ |
| 420 |
DDrPatch_Int32(OniExe + 0x000f3755, (int)cinematic_start_patch); |
| 421 |
} |
| 422 |
|