source: Daodan/src/Daodan_BSL.c@ 446

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