DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
actionseq.cpp
1#include "actionseq.hpp"
2#include "sce/libvu0.h"
3
4#include <cmath>
5
6#include "character.hpp"
7#include "mathutil.hpp"
8
9void CActionSeq::Initialize(ACT_SEQ *records, int count) {
10 ClearSeq();
11 pool = records;
12 capacity = count;
13}
14
18
20 character = NULL;
21 move_tail = NULL;
22 move_head = NULL;
23 move_frame = 0;
24 rotation_mode = 0;
26 rotation_tail = NULL;
27 rotation_head = NULL;
29 motion_tail = NULL;
30 motion_head = NULL;
31 anime_tail = NULL;
32 anime_head = NULL;
34 anime_frame = 0;
36 motion_delay = 0;
37 anime_trigger = 0;
38 anime_delay = 0;
39}
40
42 ACT_SEQ *sequence = pool;
43 int index = 0;
44
45 for (; index < capacity; index++, sequence++) {
46 if (sequence->operation == ACT_SEQ_UNUSED) {
47 sequence->next = NULL;
48 return sequence;
49 }
50 }
51
52 return NULL;
53}
54
56 sceVu0FVECTOR location;
57 sceVu0FVECTOR angles;
58 character = target;
59
60 if (target != NULL) {
61 target->GetPosition(location);
62 target->GetRotation(angles);
63 SetPos(location);
64 SetRot(angles);
65 }
66}
67
69 ACT_SEQ *sequence = GetNextSeq();
70
71 if (sequence == NULL) {
72 return NULL;
73 }
74
75 if (move_tail != NULL) {
76 move_tail->next = sequence;
77 }
78
79 move_tail = sequence;
80 sequence->next = NULL;
81
82 if (move_head == NULL) {
83 move_head = sequence;
84 }
85
86 return sequence;
87}
88
90 ACT_SEQ *sequence = GetNextSeq();
91
92 if (sequence == NULL) {
93 return NULL;
94 }
95
96 if (motion_tail != NULL) {
97 motion_tail->next = sequence;
98 }
99
100 motion_tail = sequence;
101 sequence->next = NULL;
102
103 if (motion_head == NULL) {
104 motion_head = sequence;
105 }
106
107 return sequence;
108}
109
111 ACT_SEQ *sequence = GetNextSeq();
112
113 if (sequence == NULL) {
114 return NULL;
115 }
116
117 if (anime_tail != NULL) {
118 anime_tail->next = sequence;
119 }
120
121 anime_tail = sequence;
122 sequence->next = NULL;
123
124 if (anime_head == NULL) {
125 anime_head = sequence;
126 }
127
128 return sequence;
129}
130
131void CActionSeq::MoveSeq(float *destination, int frames) {
132 ACT_SEQ *sequence = NextMoveSeq();
133
134 if (sequence != NULL) {
135 sequence->operation = ACT_SEQ_MOVE;
136 sceVu0CopyVector(sequence->arguments.vector, destination);
137 sceVu0CopyVector(queued_position, destination);
138 sequence->duration = frames;
139 }
140}
141
142void CActionSeq::MoveSeq(float *destination, float speed) {
143 float duration = DistVector(destination, queued_position) / speed;
144 int frames = duration;
145
146 if (duration - frames > 0.0) {
147 frames++;
148 }
149
150 MoveSeq(destination, frames);
151}
152
153void CActionSeq::MoveSeq(int frames) {
154 MoveSeq(queued_position, frames);
155}
156
157void CActionSeq::SetPos(float *destination) {
158 ACT_SEQ *sequence = NextMoveSeq();
159
160 if (sequence != NULL) {
161 sequence->operation = ACT_SEQ_SET_POSITION;
162 sceVu0CopyVector(sequence->arguments.vector, destination);
163 sceVu0CopyVector(queued_position, destination);
164 sequence->duration = 0;
165 }
166}
167
168void CActionSeq::RotRefSeq(float *reference, float speed) {
169 ACT_SEQ *sequence = NextMoveSeq();
170
171 if (sequence != NULL) {
172 sequence->operation = ACT_SEQ_ROTATE_REFERENCE;
173 sceVu0CopyVector(sequence->arguments.vector, reference);
174 sequence->arguments.vector[3] = speed;
175 sequence->duration = 0;
176 }
177}
178
179void CActionSeq::RotAngleSeq(float angle, float speed) {
180 ACT_SEQ *sequence = NextMoveSeq();
181
182 if (sequence != NULL) {
183 sequence->operation = ACT_SEQ_ROTATE_ANGLE;
184 sequence->arguments.vector[0] = 0;
185 sequence->arguments.vector[1] = angle;
186 sequence->arguments.vector[2] = 0;
187 sequence->arguments.vector[3] = speed;
188 sequence->duration = 0;
189 }
190}
191
192void CActionSeq::RotMoveSeq(float speed) {
193 ACT_SEQ *sequence = NextMoveSeq();
194
195 if (sequence != NULL) {
196 sequence->operation = ACT_SEQ_ROTATE_MOVEMENT;
197 sequence->arguments.vector[3] = speed;
198 sequence->duration = 0;
199 }
200}
201
203 ACT_SEQ *sequence = NextMoveSeq();
204
205 if (sequence != NULL) {
206 sequence->operation = ACT_SEQ_CLEAR_ROTATION;
207 sequence->duration = 0;
208 }
209}
210
212 ACT_SEQ *sequence = NextMoveSeq();
213
214 if (sequence != NULL) {
215 sequence->operation = ACT_SEQ_WAIT_ROTATION;
216 sequence->duration = 0;
217 }
218}
219
220void CActionSeq::SetRot(float *angles) {
221 ACT_SEQ *sequence = NextMoveSeq();
222
223 if (sequence != NULL) {
224 sequence->operation = ACT_SEQ_SET_ROTATION;
225 sceVu0CopyVector(sequence->arguments.vector, angles);
226 sequence->duration = 0;
227 }
228}
229
230void CActionSeq::SetDelayRot(int delay) {
231 ACT_SEQ *sequence = NextMoveSeq();
232
233 if (sequence != NULL) {
234 sequence->operation = ACT_SEQ_DELAY_ROTATION;
235 sequence->duration = delay;
236 }
237}
238
239void CActionSeq::MotionTrg(int delay) {
240 ACT_SEQ *sequence = NextMoveSeq();
241
242 if (sequence != NULL) {
243 sequence->operation = ACT_SEQ_TRIGGER_MOTION;
244 sequence->duration = delay;
245 }
246}
247
248void CActionSeq::AnimeTrg(int delay) {
249 ACT_SEQ *sequence = NextMoveSeq();
250
251 if (sequence != NULL) {
252 sequence->operation = ACT_SEQ_TRIGGER_ANIMATION;
253 sequence->duration = delay;
254 }
255}
256
257void CActionSeq::MotionSeq(int motion, int mode, float speed, int flags) {
258 ACT_SEQ *sequence = NextMotionSeq();
259
260 if (sequence != NULL) {
261 sequence->operation = ACT_SEQ_MOTION;
262 sequence->duration = 0;
263 sequence->arguments.animation.id = motion;
264 sequence->arguments.animation.mode = mode;
265 sequence->arguments.animation.playback.speed = speed;
266 sequence->arguments.animation.flags = flags & 4;
267 }
268}
269
270void CActionSeq::AnimeSeq(int animation, int enabled, int frames, int disable_after) {
271 ACT_SEQ *sequence = NextAnimeSeq();
272
273 if (sequence != NULL) {
274 sequence->operation = ACT_SEQ_ANIMATION;
275 sequence->duration = frames;
276 sequence->arguments.animation.id = animation;
277 sequence->arguments.animation.mode = enabled;
279
280 if (frames > 0) {
281 sequence->arguments.animation.playback.disable_after = disable_after;
282 }
283 }
284}
285
287 int motion_finished = 0;
288
289 if (motion_head != NULL) {
291 motion_finished = 1;
292 }
293 } else {
294 motion_finished = 1;
295 }
296
297 return move_head == NULL && motion_finished && anime_head == NULL;
298}
299
303static ACT_SEQ *DeleteSeq(ACT_SEQ *sequence) {
304 if (sequence == NULL) {
305 return NULL;
306 }
307
308 ACT_SEQ *next = sequence->next;
309 sequence->operation = ACT_SEQ_UNUSED;
310 sequence->next = NULL;
311 return next;
312}
313
322 int animation_shown;
323 ACT_SEQ *sequence;
324
325 if (move_head == NULL && motion_head == NULL && anime_head == NULL) {
326 return;
327 }
328
329 while ((sequence = move_head) != NULL) {
330 switch (sequence->operation) {
331 case ACT_SEQ_TRIGGER_MOTION:
332 if (motion_trigger != 0) {
333 motion_head = DeleteSeq(motion_head);
334
335 if (motion_head == NULL) {
336 motion_tail = NULL;
337 }
338 }
339
340 if (motion_head != NULL) {
341 motion_trigger = 1;
342 motion_delay = sequence->duration;
343 }
344
345 move_head = DeleteSeq(sequence);
346 continue;
347 case ACT_SEQ_TRIGGER_ANIMATION:
348 anime_trigger = 1;
349 anime_delay = sequence->duration;
350 move_head = DeleteSeq(sequence);
351 continue;
352 case ACT_SEQ_SET_POSITION:
353 sceVu0CopyVector(position, sequence->arguments.vector);
354 sceVu0CopyVector(start_position, sequence->arguments.vector);
355 move_head = DeleteSeq(sequence);
356 move_frame = 0;
357 continue;
358 case ACT_SEQ_SET_ROTATION:
359 // The rotation is written twice; the start rotation keeps its old value.
360 sceVu0CopyVector(rotation, sequence->arguments.vector);
361 sceVu0CopyVector(rotation, sequence->arguments.vector);
362 move_head = DeleteSeq(sequence);
363 move_frame = 0;
364 continue;
365 case ACT_SEQ_ROTATE_REFERENCE:
366 rotation_mode = 1;
368 sceVu0CopyVector(rotation_target, sequence->arguments.vector);
369 move_head = DeleteSeq(sequence);
370 continue;
371 case ACT_SEQ_ROTATE_ANGLE:
372 rotation_mode = 3;
374 sceVu0CopyVector(rotation_target, sequence->arguments.vector);
375 move_head = DeleteSeq(sequence);
376 continue;
377 case ACT_SEQ_DELAY_ROTATION:
378 rotation_delay = sequence->duration;
379 move_head = DeleteSeq(sequence);
380 continue;
381 case ACT_SEQ_ROTATE_MOVEMENT:
382 rotation_mode = 2;
384 sceVu0CopyVector(rotation_target, sequence->arguments.vector);
385 move_head = DeleteSeq(sequence);
386 continue;
387 case ACT_SEQ_CLEAR_ROTATION:
388 rotation_mode = 0;
390 move_head = DeleteSeq(sequence);
391 continue;
392 case ACT_SEQ_WAIT_ROTATION:
393 if (rotation_complete != 0) {
394 rotation_mode = 0;
396 move_head = DeleteSeq(sequence);
397 }
398
399 break;
400 case ACT_SEQ_MOVE:
401 move_frame++;
402
403 if (move_frame >= sequence->duration) {
404 sceVu0CopyVector(position, sequence->arguments.vector);
405 sceVu0CopyVector(start_position, sequence->arguments.vector);
406 move_head = DeleteSeq(sequence);
407 move_frame = 0;
408 } else {
409 sceVu0FVECTOR offset;
410
411 sceVu0SubVector(offset, sequence->arguments.vector, start_position);
412 sceVu0ScaleVector(offset, offset, (float) move_frame / (float) sequence->duration);
413 sceVu0AddVector(position, start_position, offset);
414 }
415
416 break;
417 }
418
419 break;
420 }
421
422 if (move_head == NULL) {
423 move_tail = NULL;
424 }
425
426 while ((sequence = rotation_head) != NULL) {
427 if (sequence->operation == ACT_SEQ_SET_ROTATION) {
428 sceVu0CopyVector(rotation, sequence->arguments.vector);
429 sceVu0CopyVector(start_rotation, sequence->arguments.vector);
430 rotation_head = DeleteSeq(sequence);
431 rotation_frame = 0;
432 continue;
433 }
434
436
437 if (rotation_frame >= sequence->duration) {
438 sceVu0CopyVector(rotation, sequence->arguments.vector);
439 sceVu0CopyVector(start_rotation, sequence->arguments.vector);
440 rotation_head = DeleteSeq(sequence);
441 rotation_frame = 0;
442 } else {
443 sceVu0FVECTOR offset;
444
445 sceVu0SubVector(offset, sequence->arguments.vector, start_rotation);
446 sceVu0ScaleVector(offset, offset, (float) rotation_frame / (float) sequence->duration);
447 sceVu0AddVector(rotation, start_rotation, offset);
448 }
449
450 break;
451 }
452
453 if (rotation_mode != 0 && rotation_delay == 0) {
454 sceVu0FVECTOR direction = {0.0f, 0.0f, 0.0f, 0.0f};
455
456 if (rotation_mode == 1) {
457 sceVu0SubVector(direction, rotation_target, position);
458 }
459
460 if (rotation_mode == 4) {
461 sceVu0CopyVector(direction, rotation_target);
462 }
463
464 if (rotation_mode == 2) {
465 sceVu0SubVector(direction, position, start_position);
466 }
467
468 if (rotation_mode == 3) {
469 rotation[1] = AngleInterpolate(rotation[1], rotation_target[1], rotation_target[3], INTERPOLATE_STEP);
470
471 if (AngleCmp(rotation[1], rotation_target[1], 0.001f) == 0) {
473 }
474 } else if (direction[0] != 0.0f || direction[2] != 0.0f) {
475 float target_yaw = atan2f(direction[0], direction[2]);
476
477 rotation[1] = AngleInterpolate(rotation[1], target_yaw, rotation_target[3], INTERPOLATE_STEP);
478
479 if (AngleCmp(rotation[1], target_yaw, 0.001f) == 0) {
481 }
482 } else {
484 }
485 }
486
488
489 if (rotation_delay < 0) {
490 rotation_delay = 0;
491 }
492
493 if (character != NULL) {
494 float motion_time = character->GetNowTime();
495
498
499 if (motion_info != NULL && motion_head->arguments.animation.mode == 7) {
500#ifdef PAL
501 // The last step is the one the character plays at, once its speed is set.
502 float step = motion_info->speed;
503
504 if (character->motion_speed > 0.0f) {
505 step = character->motion_speed;
506 }
507
508 if (!(motion_info->end <= motion_time) && motion_info->start < motion_time && motion_info->end - step - 0.01f <= motion_time) {
509#else
510 if (!(motion_info->end <= motion_time) && motion_info->start < motion_time && motion_info->end - motion_info->speed - 0.01f <= motion_time) {
511#endif
512 int motion_index;
513 tagMOTION_TYPE *motion_type = character->GetMotionParam(character->motion_no, &motion_index, NULL, NULL, NULL);
514
515 if (motion_type != NULL) {
516 motion_type->state.time = (int) (0.999f + motion_type->state.time);
517 }
518
519 motion_head = DeleteSeq(motion_head);
520
521 if (motion_head == NULL) {
522 motion_tail = NULL;
523 }
524 }
525 }
526 }
527 }
528
529 if (motion_trigger != 0 && motion_delay <= 0) {
530 ACT_SEQ *queued_motion = motion_head;
531
532 if (queued_motion != NULL && character != NULL) {
533 int flags = 0;
534
535 switch (queued_motion->arguments.animation.mode) {
536 case 6:
537 case 7:
538 flags = 2;
539 }
540
541 if (character->motion_no != queued_motion->arguments.animation.id) {
542 flags |= queued_motion->arguments.animation.flags;
543 }
544
545 character->SetMotion(queued_motion->arguments.animation.id, flags);
547 }
548 }
549
550 if (anime_trigger != 0 && anime_delay <= 0 && character != NULL) {
551 animation_shown = 0;
552
553 while ((sequence = anime_head) != NULL) {
554 switch (sequence->operation) {
555 case ACT_SEQ_ANIMATION:
556 if (sequence->duration == 0) {
557 if (sequence->arguments.animation.id == -1 && sequence->arguments.animation.mode == 0) {
559 }
560
561 if (sequence->arguments.animation.mode != 0) {
563 } else {
565 }
566
567 anime_head = DeleteSeq(sequence);
568 anime_frame = 0;
569 continue;
570 }
571
572 if (anime_frame >= sequence->duration) {
573 if (sequence->arguments.animation.playback.disable_after != 0) {
575 }
576
577 anime_head = DeleteSeq(sequence);
578 anime_frame = 0;
579 continue;
580 }
581
582 if (sequence->arguments.animation.id == -1 && sequence->arguments.animation.mode == 0) {
584 }
585
586 if (sequence->arguments.animation.mode != 0) {
588 } else {
590 }
591
592 animation_shown = 1;
593 break;
594 default:
595 anime_head = DeleteSeq(sequence);
596 break;
597 }
598
599 if (animation_shown != 0) {
600 break;
601 }
602 }
603
604 anime_frame++;
605
606 if (anime_head == NULL) {
607 anime_tail = NULL;
608 anime_trigger = 0;
609 }
610 }
611
612 motion_delay--;
613
614 if (motion_delay < 0) {
615 motion_delay = 0;
616 }
617
618 anime_delay--;
619
620 if (anime_delay < 0) {
621 anime_delay = 0;
622 }
623
624 if (character != NULL) {
625 sceVu0FVECTOR wrapped_rotation;
626 float turns;
627 int whole_turns;
628
630 wrapped_rotation[0] = rotation[0];
631 wrapped_rotation[1] = rotation[1];
632 wrapped_rotation[2] = rotation[2];
633 wrapped_rotation[3] = 0.0f;
634 // Wrap the yaw into [-pi, pi]; the first truncation's result is unused.
635 turns = wrapped_rotation[1] / TWO_PI;
636 whole_turns = (int) turns;
637 wrapped_rotation[1] -= PI * (2.0f * (int) turns);
638
639 if (wrapped_rotation[1] > PI) {
640 wrapped_rotation[1] -= TWO_PI;
641 }
642
643 if (wrapped_rotation[1] < -PI) {
644 wrapped_rotation[1] += TWO_PI;
645 }
646
647 character->SetRotation(wrapped_rotation);
648 }
649}
int rotation_delay
Definition actionseq.hpp:72
ACT_SEQ * GetNextSeq()
Definition actionseq.cpp:41
int anime_trigger
Definition actionseq.hpp:75
float rotation[4]
Definition actionseq.hpp:82
int rotation_complete
Definition actionseq.hpp:71
void SetDelayRot(int delay)
void ClearSeq()
Definition actionseq.cpp:19
void MotionSeq(int motion, int mode, float speed, int flags)
ACT_SEQ * anime_tail
Definition actionseq.hpp:88
float position[4]
Definition actionseq.hpp:65
float start_position[4]
Definition actionseq.hpp:66
void Initialize(ACT_SEQ *records, int count)
Definition actionseq.cpp:9
ACT_SEQ * NextAnimeSeq()
ACT_SEQ * pool
Definition actionseq.hpp:90
void SyncChara(CCharacter *target)
Definition actionseq.cpp:55
void AnimeTrg(int delay)
int motion_trigger
Definition actionseq.hpp:73
float rotation_target[4]
Definition actionseq.hpp:78
int rotation_mode
Definition actionseq.hpp:70
void RotAngleSeq(float angle, float speed)
ACT_SEQ * motion_tail
Definition actionseq.hpp:86
int CheckEnd()
void SetPos(float *destination)
ACT_SEQ * NextMoveSeq()
Definition actionseq.cpp:68
ACT_SEQ * NextMotionSeq()
Definition actionseq.cpp:89
ACT_SEQ * rotation_head
Definition actionseq.hpp:79
int anime_frame
Definition actionseq.hpp:69
float start_rotation[4]
Definition actionseq.hpp:83
int motion_delay
Definition actionseq.hpp:74
ACT_SEQ * move_tail
Definition actionseq.hpp:63
void SetRot(float *angles)
CCharacter * character
Definition actionseq.hpp:91
ACT_SEQ * rotation_tail
Definition actionseq.hpp:80
ACT_SEQ * move_head
Definition actionseq.hpp:62
void AnimeSeq(int animation, int enabled, int frames, int disable_after)
int rotation_frame
Definition actionseq.hpp:84
ACT_SEQ * motion_head
Definition actionseq.hpp:85
void MoveSeq(float *destination, int frames)
void RotMoveSeq(float speed)
void MotionTrg(int delay)
float queued_position[4]
Definition actionseq.hpp:67
void ClearRotSeq()
ACT_SEQ * anime_head
Definition actionseq.hpp:87
void RotRefSeq(float *reference, float speed)
void Play()
void WaitRotSeq()
int anime_delay
Definition actionseq.hpp:76
void TexAnimeOn(int no)
void SetMotion(int no, int mode, float speed=-1.0f)
virtual void GetPosition(float *out_position)
MOTION_INFO * GetMotionInfo(int motion_no)
tagMOTION_TYPE * GetMotionParam(int motion_no, int *out_index, int *out_start, int *out_end, int *out_set)
void SetMotionSpeed(float speed)
void ClearTexAnime()
virtual void SetRotation(float x, float y, float z)
void TexAnimeOff(int no)
float motion_speed
float GetNowTime()
virtual void SetPosition(float x, float y, float z)
virtual void GetRotation(float *out_rotation)
Definition object.cpp:116
@ INTERPOLATE_STEP
Definition mathutil.hpp:15
int duration
Definition actionseq.hpp:33
float speed
Definition actionseq.hpp:45
ACT_SEQ_OPERATION operation
Definition actionseq.hpp:32
struct ACT_SEQ::@2::@3 animation
int flags
Definition actionseq.hpp:49
union ACT_SEQ::@2::@3::@4 playback
float vector[4]
Definition actionseq.hpp:38
union ACT_SEQ::@2 arguments
int disable_after
Definition actionseq.hpp:46
ACT_SEQ * next
Definition actionseq.hpp:35
MOTION_STATE state
Definition gameutil.hpp:273