Paper Mario DX
Paper Mario (N64) modding
 
Loading...
Searching...
No Matches
f8f60_len_1560.c
Go to the documentation of this file.
1#include "common.h"
2#include "functions.h"
3#include "game_modes.h"
4
5enum {
6 LERP_VAR_0 = 0x0, // (out float) cur
7 LERP_VAR_1 = 0x1, // (out bool) in-progress
8 LERP_VAR_B = 0xB, // mode
9 LERP_VAR_C = 0xC, // start
10 LERP_VAR_D = 0xD, // end
11 LERP_VAR_E = 0xE, // elapsed
12 LERP_VAR_F = 0xF, // duration
13};
14
15// args: start, end, duration, EasingType
16API_CALLABLE(MakeLerp) {
17 Bytecode* ptrReadPos = script->ptrReadPos;
18
19 script->varTableF[LERP_VAR_C] = evt_get_float_variable(script, *ptrReadPos++); // start
20 script->varTableF[LERP_VAR_D] = evt_get_float_variable(script, *ptrReadPos++); // end
21 script->varTable[LERP_VAR_F] = evt_get_variable(script, *ptrReadPos++); // duration
22 script->varTable[LERP_VAR_B] = evt_get_variable(script, *ptrReadPos++); // easing type
23 script->varTable[LERP_VAR_E] = 0; // elapsed
24
25 return ApiStatus_DONE2;
26}
27
28API_CALLABLE(UpdateLerp) {
30 script->varTable[LERP_VAR_B],
31 script->varTableF[LERP_VAR_C],
32 script->varTableF[LERP_VAR_D],
33 script->varTable[LERP_VAR_E],
34 script->varTable[LERP_VAR_F]
35 ));
36
37 if (script->varTable[LERP_VAR_E] >= script->varTable[LERP_VAR_F]) {
38 script->varTable[LERP_VAR_1] = false; // finished
39 } else {
40 script->varTable[LERP_VAR_1] = true; // lerping
41 }
42 script->varTable[LERP_VAR_E]++;
43
44 return ApiStatus_DONE2;
45}
46
47API_CALLABLE(RandInt) {
48 Bytecode* args = script->ptrReadPos;
49
50 s32 max = evt_get_variable(script, *args++);
51 Bytecode outVar = *args++;
52
54
55 return ApiStatus_DONE2;
56}
57
58API_CALLABLE(GetAngleBetweenNPCs) {
59 Bytecode* args = script->ptrReadPos;
60
61 s32 aID = evt_get_variable(script, *args++);
62 s32 bID = evt_get_variable(script, *args++);
63 Bytecode outVar = *args++;
64
67 evt_set_variable(script, outVar, atan2(a->pos.x, a->pos.z, b->pos.x, b->pos.z));
68
69 return ApiStatus_DONE2;
70}
71
72API_CALLABLE(GetAngleToNPC) {
74 Bytecode* args = script->ptrReadPos;
75
76 s32 npcID = evt_get_variable(script, *args++);
77 Bytecode outVar = *args++;
78
79 Npc* npc = resolve_npc(script, npcID);
80 evt_set_variable(script, outVar, atan2(playerStatus->pos.x, playerStatus->pos.z, npc->pos.x, npc->pos.z));
81
82 return ApiStatus_DONE2;
83}
84
85API_CALLABLE(GetAngleToPlayer) {
87 Bytecode* args = script->ptrReadPos;
88
89 s32 npcID = evt_get_variable(script, *args++);
90 Bytecode outVar = *args++;
91
92 Npc* npc = resolve_npc(script, npcID);
93 evt_set_variable(script, outVar, atan2(npc->pos.x, npc->pos.z, playerStatus->pos.x, playerStatus->pos.z));
94
95 return ApiStatus_DONE2;
96}
97
98API_CALLABLE(AwaitPlayerApproach) {
99 Bytecode* args = script->ptrReadPos;
101
102 s32* targetX = &script->functionTemp[0];
103 s32* targetZ = &script->functionTemp[1];
104 s32* distanceRequired = &script->functionTemp[2];
105
107
108 if (isInitialCall) {
109 *targetX = evt_get_variable(script, *args++);
110 *targetZ = evt_get_variable(script, *args++);
112 }
113
115 playerStatus->pos.x, playerStatus->pos.z,
116 *targetX, *targetZ
117 );
118
119 if (distance < *distanceRequired) {
120 return ApiStatus_DONE2;
121 } else {
122 return ApiStatus_BLOCK;
123 }
124}
125
126API_CALLABLE(IsPlayerWithin) {
127 Bytecode* args = script->ptrReadPos;
129
130 s32* targetX = &script->functionTemp[0];
131 s32* targetZ = &script->functionTemp[1];
132 s32* distanceRequired = &script->functionTemp[2];
133
136
137 if (isInitialCall) {
138 *targetX = evt_get_variable(script, *args++);
139 *targetZ = evt_get_variable(script, *args++);
141 outVar = *args++;
142 }
143
145 playerStatus->pos.x, playerStatus->pos.z,
146 *targetX, *targetZ
147 );
148
150 if (distance < *distanceRequired) {
152 }
153
154 return ApiStatus_DONE2;
155}
156
157API_CALLABLE(AwaitPlayerLeave) {
158 Bytecode* ptrReadPos = script->ptrReadPos;
160
161 s32* targetX = &script->functionTemp[0];
162 s32* targetZ = &script->functionTemp[1];
163 s32* distanceRequired = &script->functionTemp[2];
164
166
167 if (isInitialCall) {
168 *targetX = evt_get_variable(script, *ptrReadPos++);
169 *targetZ = evt_get_variable(script, *ptrReadPos++);
170 *distanceRequired = evt_get_variable(script, *ptrReadPos++);
171 }
172
174 playerStatus->pos.x, playerStatus->pos.z,
175 *targetX, *targetZ
176 );
177
178 if (distance > *distanceRequired) {
179 return ApiStatus_DONE2;
180 } else {
181 return ApiStatus_BLOCK;
182 }
183}
184
185API_CALLABLE(AddVectorPolar) {
186 Bytecode* args = script->ptrReadPos;
187
188 Bytecode xVar = *args++;
190
191 Bytecode yVar = *args++;
193
194 f32 r = evt_get_float_variable(script, *args++);
195
196 add_vec2D_polar(&x, &y, r, evt_get_float_variable(script, *args++));
197
200
201 return ApiStatus_DONE2;
202}
203
204API_CALLABLE(func_802D4BDC) {
205 s32* ready = &script->functionTemp[0];
206 s32* progress = &script->functionTemp[1];
207 s32 t1v;
208
209 if (isInitialCall) {
210 *ready = 0;
211 *progress = 0;
212 }
213
214 // always zero?
215 if (*ready == 0) {
216 t1v = *progress;
217 if (t1v == 255) {
218 return ApiStatus_DONE2;
219 }
220
221 t1v += 10;
222 *progress = t1v;
223 if (t1v > 255) {
224 *progress = 255;
225 }
226
228 }
229
230 return ApiStatus_BLOCK;
231}
232
233API_CALLABLE(func_802D4C4C) {
234 s32* ready = &script->functionTemp[0];
235 s32* progress = &script->functionTemp[1];
236 s32 t1v;
237
238 if (isInitialCall) {
239 *ready = 0;
240 *progress = 255;
241 }
242
243 // always zero?
244 if (*ready == 0) {
245 t1v = *progress;
246 if (t1v == 0) {
248 return ApiStatus_DONE2;
249 }
250 t1v -= 10;
251 *progress = t1v;
252 if (t1v < 0) {
253 *progress = 0;
254 }
256 }
257
258 return ApiStatus_BLOCK;
259}
260
261API_CALLABLE(func_802D4CC4) {
262 Bytecode* args = script->ptrReadPos;
263 s32 value = evt_get_variable(script, *args++);
264
265 if (value < 0) {
267 } else {
269 }
270
271 return ApiStatus_DONE2;
272}
273
274API_CALLABLE(func_802D4D14) {
275 Bytecode* args = script->ptrReadPos;
276 s32 value = evt_get_float_variable(script, *args++);
277
281
282 return ApiStatus_DONE2;
283}
284
285API_CALLABLE(func_802D4D88) {
287 return ApiStatus_DONE2;
288}
289
291 f32* lenBuf = heap_malloc(num * sizeof(f32));
292 Vec3f* vecBuf = heap_malloc(num * sizeof(Vec3f));
293 s32 i;
294
295 // compute the distance of each vector along the path and map to the range [0,1]
296 normalizedLengths[0] = 0.0f;
297 for (i = 1; i < num; i++) {
298 f32 dx = pathPositions[i].x - pathPositions[i-1].x;
299 f32 dy = pathPositions[i].y - pathPositions[i-1].y;
300 f32 dz = pathPositions[i].z - pathPositions[i-1].z;
301 f32 length = sqrtf(SQ(dx) + SQ(dy) + SQ(dz));
302 normalizedLengths[i] = normalizedLengths[i-1] + length;
303 }
304 for (i = 1; i < num; i++) {
306 }
307
308 // end points
309 outVectors[0].x = 0.0f;
310 outVectors[0].y = 0.0f;
311 outVectors[0].z = 0.0f;
312 outVectors[num-1].x = 0.0f;
313 outVectors[num-1].y = 0.0f;
314 outVectors[num-1].z = 0.0f;
315
316 for (i = 0; i < num - 1; i++) {
318 vecBuf[i+1].x = (pathPositions[i+1].x - pathPositions[i].x) / lenBuf[i];
319 vecBuf[i+1].y = (pathPositions[i+1].y - pathPositions[i].y) / lenBuf[i];
320 vecBuf[i+1].z = (pathPositions[i+1].z - pathPositions[i].z) / lenBuf[i];
321 }
322
323 // n = 1
324 outVectors[1].x = vecBuf[2].x - vecBuf[1].x;
325 outVectors[1].y = vecBuf[2].y - vecBuf[1].y;
326 outVectors[1].z = vecBuf[2].z - vecBuf[1].z;
327 vecBuf[1].x = 2.0f * (normalizedLengths[2] - normalizedLengths[0]);
328 vecBuf[1].y = 2.0f * (normalizedLengths[2] - normalizedLengths[0]);
329 vecBuf[1].z = 2.0f * (normalizedLengths[2] - normalizedLengths[0]);
330
331 // 1 < n < N - 2
332 for (i = 1; i < num - 2; i++) {
333 f32 sx = lenBuf[i] / vecBuf[i].x;
334 f32 sy = lenBuf[i] / vecBuf[i].y;
335 f32 sz = lenBuf[i] / vecBuf[i].z;
336 outVectors[i+1].x = (vecBuf[i+2].x - vecBuf[i+1].x) - outVectors[i].x * sx;
337 outVectors[i+1].y = (vecBuf[i+2].y - vecBuf[i+1].y) - outVectors[i].y * sy;
338 outVectors[i+1].z = (vecBuf[i+2].z - vecBuf[i+1].z) - outVectors[i].z * sz;
339 vecBuf[i+1].x = 2.0f * (normalizedLengths[i+2] - normalizedLengths[i]) - lenBuf[i] * sx;
340 vecBuf[i+1].y = 2.0f * (normalizedLengths[i+2] - normalizedLengths[i]) - lenBuf[i] * sy;
341 vecBuf[i+1].z = 2.0f * (normalizedLengths[i+2] - normalizedLengths[i]) - lenBuf[i] * sz;
342 }
343
344 // n = N - 2
345 outVectors[num-2].x -= (lenBuf[num-2] * outVectors[num-1].x);
346 outVectors[num-2].y -= (lenBuf[num-2] * outVectors[num-1].y);
347 outVectors[num-2].z -= (lenBuf[num-2] * outVectors[num-1].z);
348
349 for (i = num - 2; i > 0; i--) {
350 outVectors[i].x = (outVectors[i].x - (lenBuf[i] * outVectors[i+1].x)) / vecBuf[i].x;
351 outVectors[i].y = (outVectors[i].y - (lenBuf[i] * outVectors[i+1].y)) / vecBuf[i].y;
352 outVectors[i].z = (outVectors[i].z - (lenBuf[i] * outVectors[i+1].z)) / vecBuf[i].z;
353 }
354
357}
358
360 s32 limit = numVectors - 1;
363 f32 ax, ay, az, bx, by, bz, dx, dy, dz;
364 s32 i;
365
366 for (i = 0; i < limit;) {
367 s32 temp_v1 = (i + limit) / 2;
368
369 if (normalizedLengths[temp_v1] < alpha) {
370 i = temp_v1 + 1;
371 } else {
372 limit = temp_v1;
373 }
374 }
375
376 if (i > 0) {
377 i--;
378 }
379
381 curProgress = alpha - normalizedLengths[i];
382
383 dx = (pathPoints[i+1].x - pathPoints[i].x) / curLength;
384 ax = (((vectors[i+1].x - vectors[i].x) * curProgress / curLength) + (3.0f * vectors[i].x)) * curProgress;
385 bx = dx - (((2.0f * vectors[i].x) + vectors[i+1].x) * curLength);
386 outPos->x = ((ax + bx) * curProgress) + pathPoints[i].x;
387
388 dy = (pathPoints[i+1].y - pathPoints[i].y) / curLength;
389 ay = (((vectors[i+1].y - vectors[i].y) * curProgress / curLength) + (3.0f * vectors[i].y)) * curProgress;
390 by = dy - (((2.0f * vectors[i].y) + vectors[i+1].y) * curLength);
391 outPos->y = ((ay + by) * curProgress) + pathPoints[i].y;
392
393 dz = (pathPoints[i+1].z - pathPoints[i].z) / curLength;
394 az = (((vectors[i+1].z - vectors[i].z) * curProgress / curLength) + (3.0f * vectors[i].z)) * curProgress;
395 bz = dz - (((2.0f * vectors[i].z) + vectors[i+1].z) * curLength);
396 outPos->z = ((az + bz) * curProgress) + pathPoints[i].z;
397}
398
399API_CALLABLE(LoadPath) {
400 Bytecode* args = script->ptrReadPos;
401 s32 time = evt_get_variable(script, *args++);
403 s32 numVectors = evt_get_variable(script, *args++);
404 s32 easingType = evt_get_variable(script, *args++);
405 Path* path = heap_malloc(sizeof(*path));
406
407 script->varTablePtr[15] = path;
408 path->numVectors = numVectors;
409 path->lengths = heap_malloc(numVectors * sizeof(f32));
411 path->vectors = heap_malloc(numVectors * sizeof(Vec3f));
412 load_path_data(path->numVectors, path->lengths, path->staticVectorList, path->vectors);
413
414 path->timeElapsed = 0;
415 path->timeLeft = time - 1;
416 path->easingType = easingType;
417
418 return ApiStatus_DONE2;
419}
420
421API_CALLABLE(GetNextPathPos) {
422 Path* path = script->varTablePtr[0xF];
423 Vec3f pos;
424 f32 alpha;
425 f32 diff;
426
427 switch (path->easingType) {
428 case EASING_LINEAR:
429 alpha = 1.0f / path->timeLeft * path->timeElapsed;
430 break;
432 alpha = 1.0f / SQ(path->timeLeft) * SQ(path->timeElapsed);
433 break;
435 diff = path->timeLeft - path->timeElapsed;
436 alpha = 1.0f - (SQ(diff) / SQ(path->timeLeft));
437 break;
439 alpha = (1.0f - cos_rad((PI / path->timeLeft) * path->timeElapsed)) * 0.5f;
440 break;
441 default:
442 alpha = 0.0f;
443 break;
444 }
445
446 get_path_position(alpha, &pos, path->numVectors, path->lengths, path->staticVectorList, path->vectors);
447 script->varTable[1] = FLOAT_TO_FIXED(pos.x);
448 script->varTable[2] = FLOAT_TO_FIXED(pos.y);
449 script->varTable[3] = FLOAT_TO_FIXED(pos.z);
450
451 if (path->timeElapsed < path->timeLeft) {
452 path->timeElapsed = path->timeElapsed + 1;
453 script->varTable[0] = 1;
454 } else {
455 heap_free(path->lengths);
456 heap_free(path->vectors);
457 heap_free(script->varTablePtr[15]);
458 script->varTable[0] = 0;
459 }
460
461 return ApiStatus_DONE2;
462}
463
464API_CALLABLE(GetDist2D) {
465 Bytecode* args = script->ptrReadPos;
466 Bytecode outVar = *args++;
471
473
474 return ApiStatus_DONE2;
475}
476
477//TODO code split? math_api from ???
478
479API_CALLABLE(SetTimeFreezeMode) {
480 Bytecode* args = script->ptrReadPos;
481
483 return ApiStatus_DONE2;
484}
485
486API_CALLABLE(ModifyGlobalOverrideFlags) {
487 Bytecode* args = script->ptrReadPos;
489 s32 flags = evt_get_variable(script, *args++);
490
491 if (setMode) {
493 } else {
495 }
496
497 return ApiStatus_DONE2;
498}
499
500API_CALLABLE(SetValueByRef) {
501 Bytecode* args = script->ptrReadPos;
502
503 s32 dest = evt_get_variable(script, *args++); /* Reference */
504 s32 src = evt_get_variable(script, *args++);
506
507 return ApiStatus_DONE2;
508}
509
510API_CALLABLE(GetValueByRef) {
511 Bytecode* args = script->ptrReadPos;
512 s32 src = evt_get_variable(script, *args++); /* Reference */
513 Bytecode dest = *args++;
514
516
517 return ApiStatus_DONE2;
518}
519
520API_CALLABLE(EnableWorldStatusBar) {
521 Bytecode* args = script->ptrReadPos;
523
524 if (shouldEnable) {
526 } else {
528 }
529
530 return ApiStatus_DONE2;
531}
532
533API_CALLABLE(ShowWorldStatusBar) {
534 Bytecode* args = script->ptrReadPos;
536
537 if (shouldShow) {
540 } else {
542 }
543
544 return ApiStatus_DONE2;
545}
546
547API_CALLABLE(SetGameMode) {
548 Bytecode* args = script->ptrReadPos;
549 s16 mode = evt_get_variable(script, *args++);
550
551 set_game_mode(mode);
552
553 return ApiStatus_DONE2;
554}
555
556API_CALLABLE(ClampAngleInt) {
557 Bytecode* args = script->ptrReadPos;
558 s32 angle = evt_get_variable(script, *args);
559
560 evt_set_variable(script, *args++, clamp_angle(angle));
561
562 return ApiStatus_DONE2;
563}
564
565API_CALLABLE(ClampAngleFloat) {
566 Bytecode* args = script->ptrReadPos;
567 f32 angle = evt_get_float_variable(script, *args);
568
569 evt_set_float_variable(script, *args++, clamp_angle(angle));
570
571 return ApiStatus_DONE2;
572}
573
574#if VERSION_PAL
575API_CALLABLE(GetLanguage) {
576 Bytecode* args = script->ptrReadPos;
577
579 return ApiStatus_DONE2;
580}
581#endif
BSS s32 PopupMenu_SelectedIndex
Vec3f * vectors
s32 numVectors
s32 timeElapsed
s32 b32
s32 easingType
Vec3f * staticVectorList
f32 * lengths
s8 flags
Definition demo_api.c:15
Vec3s pos
Definition demo_api.c:17
#define sqrtf
#define rand_int
#define clamp_angle
#define atan2
@ OVERLAY_NONE
Definition enums.h:2373
@ OVERLAY_BLUR
Definition enums.h:2386
@ OVERLAY_START_BATTLE
Definition enums.h:2384
@ SCREEN_LAYER_FRONT
Definition enums.h:2368
@ EASING_QUADRATIC_IN
Definition enums.h:511
@ EASING_COS_IN_OUT
Definition enums.h:520
@ EASING_QUADRATIC_OUT
Definition enums.h:514
@ EASING_LINEAR
Definition enums.h:510
#define ApiStatus_DONE2
Definition evt.h:119
s32 Bytecode
Definition evt.h:7
#define ApiStatus_BLOCK
Definition evt.h:117
@ LERP_VAR_0
@ LERP_VAR_C
@ LERP_VAR_B
@ LERP_VAR_D
@ LERP_VAR_F
@ LERP_VAR_E
@ LERP_VAR_1
void get_path_position(f32 alpha, Vec3f *outPos, s32 numVectors, f32 *normalizedLengths, Vec3f *pathPoints, Vec3f *vectors)
void load_path_data(s32 num, f32 *normalizedLengths, Vec3f *pathPositions, Vec3f *outVectors)
void set_screen_overlay_params_back(u8, f32)
s32 evt_get_variable(Evt *script, Bytecode var)
Definition evt.c:1725
f32 update_lerp(s32 easing, f32 start, f32 end, s32 elapsed, s32 duration)
Definition 43F0.c:735
void status_bar_ignore_changes(void)
Definition inventory.c:1507
void decrement_status_bar_disabled(void)
Definition inventory.c:1656
f32 evt_set_float_variable(Evt *script, Bytecode var, f32 value)
Definition evt.c:2012
s32 evt_set_variable(Evt *script, Bytecode var, s32 value)
Definition evt.c:1882
void set_screen_overlay_params_front(u8, f32)
f32 cos_rad(f32 x)
Definition 43F0.c:717
void close_status_bar(void)
Definition inventory.c:1450
f32 dist2D(f32 ax, f32 ay, f32 bx, f32 by)
Definition 43F0.c:670
s32 heap_free(void *ptr)
Definition heap.c:42
void set_screen_overlay_center(s32, s32, s32, s32)
Npc * resolve_npc(Evt *script, s32 npcIdOrPtr)
Definition npc_api.c:8
f32 evt_get_float_variable(Evt *script, Bytecode var)
Definition evt.c:1965
void set_time_freeze_mode(s32)
Time freeze modes: 0: none 1: NPCs move, can't be interacted with 2: NPCs don't move,...
Definition main_loop.c:356
void add_vec2D_polar(f32 *x, f32 *y, f32 r, f32 theta)
Definition 43F0.c:685
void * heap_malloc(s32 size)
Definition heap.c:34
void status_bar_respond_to_changes(void)
Definition inventory.c:1518
void increment_status_bar_disabled(void)
Definition inventory.c:1664
void set_game_mode(s32 modeID)
Definition game_modes.c:127
ApiStatus EnableWorldStatusBar(Evt *script, b32 isInitialCall)
ApiStatus SetTimeFreezeMode(Evt *script, b32 isInitialCall)
ApiStatus ModifyGlobalOverrideFlags(Evt *script, b32 isInitialCall)
ApiStatus AddVectorPolar(Evt *script, b32 isInitialCall)
ApiStatus GetAngleToPlayer(Evt *script, b32 isInitialCall)
ApiStatus ClampAngleInt(Evt *script, b32 isInitialCall)
ApiStatus AwaitPlayerLeave(Evt *script, b32 isInitialCall)
ApiStatus GetDist2D(Evt *script, b32 isInitialCall)
ApiStatus GetAngleToNPC(Evt *script, b32 isInitialCall)
ApiStatus UpdateLerp(Evt *script, b32 isInitialCall)
ApiStatus AwaitPlayerApproach(Evt *script, b32 isInitialCall)
ApiStatus func_802D4D14(Evt *script, b32 isInitialCall)
ApiStatus ShowWorldStatusBar(Evt *script, b32 isInitialCall)
ApiStatus func_802D4CC4(Evt *script, b32 isInitialCall)
ApiStatus func_802D4BDC(Evt *script, b32 isInitialCall)
ApiStatus ClampAngleFloat(Evt *script, b32 isInitialCall)
ApiStatus SetValueByRef(Evt *script, b32 isInitialCall)
ApiStatus GetAngleBetweenNPCs(Evt *script, b32 isInitialCall)
ApiStatus IsPlayerWithin(Evt *script, b32 isInitialCall)
ApiStatus SetGameMode(Evt *script, b32 isInitialCall)
ApiStatus RandInt(Evt *script, b32 isInitialCall)
ApiStatus LoadPath(Evt *script, b32 isInitialCall)
ApiStatus func_802D4D88(Evt *script, b32 isInitialCall)
ApiStatus MakeLerp(Evt *script, b32 isInitialCall)
ApiStatus GetValueByRef(Evt *script, b32 isInitialCall)
ApiStatus GetNextPathPos(Evt *script, b32 isInitialCall)
ApiStatus func_802D4C4C(Evt *script, b32 isInitialCall)
#define FLOAT_TO_FIXED(x)
Progammatically converts f32 --> Float.
Definition macros.h:58
#define LocalVar(INDEX)
Local Word.
Definition macros.h:68
#define PI
Definition macros.h:137
#define SQ(x)
Definition macros.h:177
#define LVar0
Definition macros.h:149
Vec3f pos
s32 gOverrideFlags
Definition main_loop.c:10
#define gCurrentLanguage
Definition variables.h:119
PlayerStatus gPlayerStatus
Definition 77480.c:38