DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
shot_effect.cpp
1#include "shot_effect.hpp"
2
3#include <cstdio>
4#include <cstdlib>
5
6#include "collisiondata.hpp"
7#include "dataread.hpp"
8#include "dun/gameloop.hpp"
9#include "hit_machingun_effect.hpp"
10#include "itembombeffect.hpp"
11#include "itemdata.hpp"
12#include "mathutil.hpp"
13#include "mds.hpp"
14#include "mglib.hpp"
15#include "nowload.hpp"
16#include "shot_effect_pack.hpp"
17#include "shot_utils.hpp"
18#include "snd.hpp"
19#include "texture.hpp"
20
21int GetWeaponElementAttr(int element);
22
31 for (int shot = 0; shot < 12; shot++) {
32 if (used[shot] != 0) {
33 int texture_cell = NowWeaponHave->item_no - 299;
34 texture_cell--;
35
36 if (texture_cell <= 0) {
37 texture_cell = 0;
38 }
39
40 int x = (texture_cell % 4) << 5;
41 int y = (texture_cell / 4) << 5;
42 set3DCellModel(pos[shot], "basefx01", size[shot], x, y, 32, 32, 128);
43 }
44 }
45}
46
55 sceVu0FVECTOR hit_position;
56
57 for (int shot = 0; shot < 12; shot++) {
58 if (used[shot] == 0) {
59 continue;
60 }
61
62 if (halted[shot] == 0) {
63 SHOT_COLLISION_RESULT result = checkCollision(hit_position, pos[shot], vector[shot], 2, 2.0f);
64
65 if (result == SHOT_COLLISION_NONE) {
66 pos[shot][0] += vector[shot][0];
67 pos[shot][1] += vector[shot][1];
68 pos[shot][2] += vector[shot][2];
69 } else {
70 NowColData->Set(pos[shot], damage[shot], 1, 3.0f, 0.0f, 2, 2, 0, 0);
71 NowColData->SetUserID(1, 0);
72 s8 weapon_element = NowWeaponHave->best_elem;
73 CCollisionData *attr_col = NowColData;
74 attr_col->hit[attr_col->now_hit].flags = GetWeaponElementAttr(weapon_element);
75 NowColData->hit[NowColData->now_hit].weapon_flags = NowWeaponHave->flags;
76 NowColData->hit[NowColData->now_hit].vs_monster = NowWeaponHave->vs_monster;
77 used[shot] = 0;
78 }
79 }
80
81 life[shot]--;
82
83 if (life[shot] <= 0) {
84 used[shot] = 0;
85 }
86 }
87}
88
90 if (effect_data == NULL) {
91 return;
92 }
93
94 TexManager.ReloadTexture(Vif1Packet, texture_block);
95
96 for (int slot = 0; slot < 8; slot++) {
97 if (active[slot] != 0) {
98 CCharacter *model = &chara[slot];
99 sceVu0FVECTOR position;
100 sceVu0FVECTOR jittered;
101
102 model->Step();
103
104 if (!(random_rate[slot] < 0.0f)) {
105 // Drawing temporarily offsets the model by a random amount on each axis.
106 model->GetPosition(position);
107 float spread = random_rate[slot];
108 jittered[0] = position[0] + 2.0f * (spread * (float) rand()) / 2147483648.0f - spread;
109 spread = random_rate[slot];
110 jittered[1] = position[1] + 2.0f * (spread * (float) rand()) / 2147483648.0f - spread;
111 spread = random_rate[slot];
112 jittered[2] = position[2] + 2.0f * (spread * (float) rand()) / 2147483648.0f - spread;
113 jittered[3] = 1.0f;
114 model->SetPosition(jittered);
115 }
116
117 model->Draw();
118
119 if (!(random_rate[slot] < 0.0f)) {
120 model->SetPosition(position);
121 }
122 }
123 }
124}
125
127 for (int slot = 0; slot < 8; slot++) {
128 if (active[slot] == 0) {
129 continue;
130 }
131
132 int motion = effect_data->motion[phase[slot]];
133 float time = chara[slot].motion_type.state.time;
135 float end = (float) info[motion].end;
136
137 // A looping effect only steps its motion forward until it reaches the loop phase.
138 if (loop[slot] != -1) {
139 if (phase[slot] < loop[slot] && time >= end - 1.0f && time <= end) {
140 phase[slot]++;
141 chara[slot].SetMotion(effect_data->motion[phase[slot]], 4);
142 }
143
144 continue;
145 }
146
147 // The starting motion hands over to the flying one when it finishes.
148 if (phase[slot] == 0 && time >= end - 1.0f && time <= end) {
149 phase[slot]++;
150 motion = effect_data->motion[phase[slot]];
151
152 if (motion == -1) {
153 active[slot] = 0;
154 continue;
155 }
156
157 chara[slot].motion_type.state.time = (float) chara[slot].motion_type.motion_info[motion].start;
158 chara[slot].SetMotion(effect_data->motion[phase[slot]], 4);
159 }
160
161 sceVu0FVECTOR hit_position;
162 sceVu0FVECTOR position;
163 chara[slot].GetPosition(position);
164 chara[slot].GetPosition(hit_position);
165
166 SHOT_COLLISION_RESULT result = SHOT_COLLISION_NONE;
167
168 if (phase[slot] < 2) {
169 result = checkCollision(hit_position, position, velocity[slot], effect_data->target, effect_data->radius[phase[slot]]);
170 }
171
172 if (life_time[slot] > 0) {
173 life_time[slot]--;
174 }
175
176 if (!(effect_data->radius[phase[slot]] <= 0.0f) && life_time[slot] != 0) {
177 // After each hit the effect waits its delay before it can hit again.
178 if (wait_state[slot] <= 0) {
179 int hit = NowColData->Set(hit_position, damage[slot], 2, effect_data->radius[phase[slot]], 1.0f, effect_data->target, effect_data->hit_kind, effect_data->hit_flags, 0);
180
181 if (hit != -1) {
182 NowColData->SetUserID(user_id[slot], user_sub_id[slot]);
183 NowColData->hit[NowColData->now_hit].weapon_flags = weapon_status[slot];
184 NowColData->hit[NowColData->now_hit].vs_monster = vs_monster[slot];
185 NowColData->hit[NowColData->now_hit].monster_no = user_id_2[slot];
186 NowColData->hit[NowColData->now_hit].target_kind = enemy_attribute[slot];
187
188 // Hits of kind 3 throw along the flight, or at the player once it stops.
189 if (effect_data->hit_kind == 3) {
190 velocity[slot][3] = 1.0f;
191
192 if (effect_data->speed[phase[slot]] <= 0.0f) {
193 sceVu0FVECTOR player;
194 sceVu0FVECTOR direction;
195 sceVu0CopyVector(player, CharaMain.pos);
196 direction[0] = player[0] - position[0];
197 direction[1] = 0.0f;
198 direction[2] = player[2] - position[2];
199 direction[3] = 1.0f;
200 sceVu0Normalize(direction, direction);
201 NowColData->SetVelocity(hit, direction, 1.0f);
202 } else {
203 NowColData->SetVelocity(hit, velocity[slot], 1.0f);
204 }
205 }
206
207 wait_state[slot] = wait[slot];
208 }
209 } else {
210 wait_state[slot]--;
211 }
212 }
213
214 if (result == SHOT_COLLISION_NONE) {
215 position[0] += velocity[slot][0];
216 position[1] += velocity[slot][1];
217 position[2] += velocity[slot][2];
218 chara[slot].SetPosition(position);
219
220 // A flying effect that runs out of time skips straight to its last phase.
221 if (phase[slot] == 1 && phase_delay[slot] != -1) {
222 phase_delay[slot]--;
223
224 if (phase_delay[slot] == -1) {
225 phase[slot] += 2;
226 motion = effect_data->motion[phase[slot]];
227
228 if (motion == -1) {
229 active[slot] = 0;
230
231 switch (effect_data->end_effect) {
232 case SHOT_END_EFFECT_BOMB:
233 SetBombEffect(hit_position, effect_data->target, effect_data->bomb_damage, effect_data->bomb_scale);
234 break;
235 }
236
237 continue;
238 }
239
240 if (phase[slot] != -1) {
241 chara[slot].motion_type.state.time = (float) chara[slot].motion_type.motion_info[motion].start;
242 chara[slot].SetMotion(effect_data->motion[phase[slot]], 6);
243 sceVu0Normalize(velocity[slot], velocity[slot]);
244 sceVu0ScaleVectorXYZ(velocity[slot], velocity[slot], effect_data->speed[phase[slot]]);
245
246 if (effect_data->sound[phase[slot]] != -1 && no_sound[slot] == 0) {
247 SndSePlay(effect_data->sound[phase[slot]], -1, 0);
248 }
249 }
250 }
251 }
252 } else if (phase[slot] == 1) {
253 // A flying effect that hits something moves on to its impact phase.
254 phase[slot]++;
255 motion = effect_data->motion[phase[slot]];
256
257 if (motion == -1) {
258 active[slot] = 0;
259
260 switch (effect_data->end_effect) {
261 case SHOT_END_EFFECT_BOMB:
262 SetBombEffect(hit_position, effect_data->target, effect_data->bomb_damage, effect_data->bomb_scale);
263 break;
264 }
265
266 continue;
267 }
268
269 if (motion != -1) {
270 chara[slot].motion_type.state.time = (float) chara[slot].motion_type.motion_info[motion].start;
271 chara[slot].SetMotion(effect_data->motion[phase[slot]], 6);
272 sceVu0Normalize(velocity[slot], velocity[slot]);
273 sceVu0ScaleVectorXYZ(velocity[slot], velocity[slot], effect_data->speed[phase[slot]]);
274
275 if (effect_data->sound[phase[slot]] != -1 && no_sound[slot] == 0) {
276 SndSePlay(effect_data->sound[phase[slot]], -1, 0);
277 }
278 }
279 }
280
281 // The effect ends when the motion of its impact or last phase finishes.
282 if ((phase[slot] == 2 || phase[slot] == 3) && chara[slot].motion_type.state.time >= end - 1.0f && chara[slot].motion_type.state.time < end) {
283 active[slot] = 0;
284
285 switch (effect_data->end_effect) {
286 case SHOT_END_EFFECT_BOMB:
287 SetBombEffect(hit_position, effect_data->target, effect_data->bomb_damage, effect_data->bomb_scale);
288 break;
289 }
290 }
291 }
292}
293
295 for (int slot = 0; slot < 8; slot++) {
296 if (active[slot] != 0 && (phase[slot] == 1 || phase[slot] == 0)) {
297 phase[slot] = 2;
298 int motion = effect_data->motion[phase[slot]];
299
300 if (motion != -1) {
301 chara[slot].motion_type.state.time = (float) chara[slot].motion_type.motion_info[motion].start;
302 chara[slot].SetMotion(effect_data->motion[phase[slot]], 6);
303 }
304 }
305 }
306}
307
309 if (slot != -1) {
310 active[slot] = 0;
311 return;
312 }
313
314 for (s32 i = 0; i < 8; i++) {
315 active[i] = 0;
316 }
317}
318
319int CSHOT_EFFECT::Entry(BT_SHOT_EFFECT *description, unsigned int *pack, int tex_block, CDataAlloc2<1> *allocator, int slots) {
320 char name[64];
321
322 if (effect_data != NULL) {
323 return 0;
324 }
325
326 sprintf(name, "dun/effect/%s.chr", description->model_name);
327 LoadFile(name, pack, NULL);
328 wait_now_loading_vsync();
329 texture_block = tex_block;
330 sprintf(name, "%s.cfg", description->model_name);
331
333 template_chara.LoadPackData3(pack, name, allocator, texture_block, allocator, 1, 0x10);
334
335 slot_count = slots;
336
337 for (int slot = 0; slot < slots; slot++) {
338 chara[slot] = template_chara;
339 chara[slot].motion[0] = &chara[slot].motion_type;
340 chara[slot].frame = (CFrame *) CopyFrameVu1((CFrameVu1 *) template_chara.frame, allocator);
341 }
342
343 effect_data = description;
344 return effect_data == NULL ? 0 : 1;
345}
346
347int CSHOT_EFFECT::Entry2(BT_SHOT_EFFECT *description, unsigned int *pack, int tex_block, CDataAlloc2<1> *allocator, int slots) {
348 char name[64];
349
350 if (effect_data != NULL) {
351 return 0;
352 }
353
354 sprintf(name, "%s.cfg", description->model_name);
355 texture_block = tex_block;
357 template_chara.LoadPackData3(pack, name, allocator, tex_block, allocator, 1, 0x10);
358
359 slot_count = slots;
360
361 for (int slot = 0; slot < slots; slot++) {
362 chara[slot] = template_chara;
363 chara[slot].motion[0] = &chara[slot].motion_type;
364 chara[slot].frame = (CFrame *) CopyFrameVu1((CFrameVu1 *) template_chara.frame, allocator);
365 }
366
367 effect_data = description;
368 return effect_data == NULL ? 0 : 1;
369}
370
372 for (int slot = 0; slot < slot_count; slot++) {
373 chara[slot] = template_chara;
374 chara[slot].motion[0] = &chara[slot].motion_type;
375 chara[slot].frame = (CFrame *) CopyFrameVu1((CFrameVu1 *) template_chara.frame, allocator);
376 }
377
378 effect_data = description;
379 return 1;
380}
381
382void CSHOT_EFFECT::SetLoop(s32 loop) {
383 s32 slot;
384
385 slot = this->current_slot;
386
387 if (slot != -1) {
388 this->loop[slot] = loop;
389 }
390}
391
392int CSHOT_EFFECT::Set(float *position, float *target, int owner, int sub_id, int source, CFrame *parent, int initial_phase) {
393 int slot = -1;
394
395 current_slot = -1;
396
397 if (effect_data == NULL) {
398 printf("******** shot err ***********\n");
399 return;
400 }
401
402 for (int i = 0; i < slot_count; i++) {
403 if (active[i] == 0) {
404 slot = i;
405 break;
406 }
407 }
408
409 if (slot == -1) {
410 return;
411 }
412
413 // An effect with no first-phase motion starts in its flying phase.
414 int start_phase = 0;
415
416 if (effect_data->motion[0] == -1) {
417 start_phase++;
418 }
419
420 phase[slot] = start_phase;
421
422 if (initial_phase != -1) {
423 phase[slot] = initial_phase;
424 }
425
426 if (effect_data->stationary == 0) {
427 position[3] = 1.0f;
428 target[3] = 1.0f;
429 sceVu0SubVector(velocity[slot], target, position);
430 sceVu0Normalize(velocity[slot], velocity[slot]);
431
432 if (parent == NULL) {
433 if (effect_data->face_flight != 0) {
434 sceVu0FMATRIX rotation;
435 LookAtMatrixZ(rotation, velocity[slot]);
436 chara[slot].frame->SetTransMatrix(rotation);
437 }
438
439 chara[slot].frame->DeleteReference();
440 } else {
441 chara[slot].frame->SetReference(parent);
442 chara[slot].SetPosition(0.0f, 0.0f, 0.0f);
443 chara[slot].SetRotation(0.0f, -HALF_PI, 0.0f);
444 }
445
446 sceVu0ScaleVectorXYZ(velocity[slot], velocity[slot], effect_data->speed[phase[slot]]);
447 }
448
449 if (phase[slot] == 0) {
450 chara[slot].SetMotion(effect_data->motion[0], 6);
451 chara[slot].motion_type.state.time = (float) chara[slot].motion_type.motion_info[effect_data->motion[0]].start;
452 } else {
453 chara[slot].SetMotion(effect_data->motion[1], 4);
454 chara[slot].motion_type.state.time = (float) chara[slot].motion_type.motion_info[effect_data->motion[1]].start;
455 }
456
457 if (parent == NULL) {
458 chara[slot].SetPosition(position);
459 }
460
461 damage[slot] = effect_data->damage;
463 active[slot] = 1;
464 user_id[slot] = owner;
465 user_sub_id[slot] = sub_id;
466 weapon_status[slot] = 1;
467 source_id[slot] = source;
468 loop[slot] = -1;
469 random_rate[slot] = -1.0f;
470 life_time[slot] = -1;
471 status = 0;
472 current_slot = slot;
473 user_id_2[slot] = -1;
474 enemy_attribute[slot] = -1;
475 no_sound[slot] = 0;
476 wait[slot] = 0;
477 wait_state[slot] = 0;
478 return slot;
479}
480
481void CSHOT_EFFECT::SetWait(s32 wait) {
482 s32 slot;
483
484 slot = this->current_slot;
485
486 if (slot != -1) {
487 this->wait[slot] = (u8) wait;
488 this->wait_state[this->current_slot] = 0;
489 }
490}
491
493 s32 slot;
494
495 slot = this->current_slot;
496
497 if (slot != -1) {
498 this->no_sound[slot] = 1;
499 }
500}
501
503 s32 slot;
504
505 slot = this->current_slot;
506
507 if (slot != -1) {
508 this->random_rate[slot] = rate;
509 }
510}
511
512void CSHOT_EFFECT::SetLifeTime(s32 life_time) {
513 s32 slot;
514
515 slot = this->current_slot;
516
517 if (slot != -1) {
518 this->life_time[slot] = life_time;
519 }
520}
521
522void CSHOT_EFFECT::SetEnemyAttr(s32 attribute) {
523 s32 slot;
524
525 slot = this->current_slot;
526
527 if (slot != -1) {
528 this->enemy_attribute[slot] = attribute;
529 }
530}
531
532void CSHOT_EFFECT::SetDmg(s32 damage) {
533 s32 slot;
534
535 slot = this->current_slot;
536
537 if (slot != -1) {
538 this->damage[slot] = damage;
539 }
540}
541
542void CSHOT_EFFECT::SetAttribute(s32 attribute) {
543 if (this->current_slot != -1) {
544 this->effect_data->hit_flags = attribute;
545 }
546}
547
549 s32 slot;
550
551 slot = this->current_slot;
552
553 if (slot != -1) {
554 this->weapon_status[slot] = status;
555 }
556}
557
558void CSHOT_EFFECT::SetVsMonster(s8 *effectiveness) {
559 s32 slot;
560
561 slot = this->current_slot;
562
563 if (slot != -1) {
564 this->vs_monster[slot] = effectiveness;
565 }
566}
567
569 s32 slot;
570
571 slot = this->current_slot;
572
573 if (slot != -1) {
574 this->user_id_2[slot] = (s16) id;
575 }
576}
577
579 effect_data = NULL;
580
581 for (s32 i = 0; i < 8; i++) {
582 active[i] = 0;
583 user_id[i] = -1;
584 user_sub_id[i] = -1;
585 slot_count = 4;
586 }
587
588 current_slot = -1;
589}
590
591int CSHOT_EFFECT_PACK::Entry(BT_SHOT_EFFECT *description, unsigned int *resource, int texture_block, CDataAlloc2<1> *allocator, int slots) {
592 for (int index = 0; index < 5; index++) {
593 if (effect[index].effect_data == description) {
594 return index;
595 }
596 }
597
598 for (int index = 0; index < 5; index++) {
599 if (effect[index].Entry(description, resource, texture_block, allocator, slots) != 0) {
600 return index;
601 }
602 }
603
604 return -1;
605}
606
608 if (current_effect != -1) {
610 }
611}
612
614 if (current_effect != -1) {
616 }
617}
618
626int CSHOT_MACHINGUN::Set(float *origin, float *direction, int damage, int element) {
627 int slot = -1;
628
629 for (int i = 0; i < 16; i++) {
630 if (this->age[i] == 0) {
631 slot = i;
632 break;
633 }
634 }
635
636 if (slot == -1) {
637 return -1;
638 }
639
640 sceVu0CopyVector(this->position[slot], origin);
641 sceVu0CopyVector(this->velocity[slot], direction);
642 this->damage[slot] = damage;
643 this->element[slot] = element;
644 this->age[slot] = 1;
645 return slot;
646}
647
656 int slot;
657 SHOT_COLLISION_RESULT result;
658 float *pos;
659 int weapon_element;
660 CCollisionData *col;
661
662 for (slot = 0; slot < 16; slot++) {
663 if (age[slot] <= 0) {
664 continue;
665 }
666
667 age[slot]++;
668
669 if (age[slot] >= 240) {
670 age[slot] = 0;
671 continue;
672 }
673
674 pos = position[slot];
675 result = checkCollision(pos, pos, velocity[slot], 2, 2.0f);
676
677 if (result == SHOT_COLLISION_MAP) {
678 OzumondShotEffect.Set(pos);
679 age[slot] = 0;
680 }
681
682 if (result == SHOT_COLLISION_MONSTER) {
683 NowColData->Set(pos, damage[slot], 2, 4.0f, 1.0f, 2, 2, 0, 0);
684 weapon_element = NowWeaponHave->best_elem;
685 col = NowColData;
686 col->hit[col->now_hit].flags = GetWeaponElementAttr(weapon_element);
687 NowColData->hit[NowColData->now_hit].vs_monster = NowWeaponHave->vs_monster;
688 NowColData->hit[NowColData->now_hit].weapon_flags = NowWeaponHave->flags;
689 CCollisionData *target = NowColData;
690 target->hit[target->now_hit].owner = 5;
691 target->hit[target->now_hit].attack_no = 6;
692 age[slot] = 0;
693 }
694
695 pos[0] += velocity[slot][0];
696 position[slot][1] += velocity[slot][1];
697 position[slot][2] += velocity[slot][2];
698 }
699}
void SetMotion(int no, int mode, float speed=-1.0f)
CFrame * frame
virtual void GetPosition(float *out_position)
virtual void LoadPackData3(unsigned int *pack, char *name, CDataAlloc2< 1 > *alloc, int texture_block_no, CDataAlloc2< 1 > *image_alloc, int keep_textures, int visual_type)
virtual void Step()
tagMOTION_TYPE motion_type
virtual void SetRotation(float x, float y, float z)
virtual void Initialize()
virtual void Draw()
virtual void SetPosition(float x, float y, float z)
tagMOTION_TYPE * motion[CHARA_MOTION_MAX]
void SetVelocity(int index, float *direction, float scale)
int Set(float *position, int damage, int life, float radius, float scale_rate, int target_mask, int kind, int flags, int attribute)
COLLISION_HIT hit[96]
void SetUserID(int id, int attack)
void Set(float *position)
float pos[4]
Definition object.hpp:18
void SetDmg(int damage)
int Entry(BT_SHOT_EFFECT *description, unsigned int *resource, int texture_block, CDataAlloc2< 1 > *allocator, int slots)
void SetUserID2(int id)
CSHOT_EFFECT effect[5]
s32 phase_delay[8]
void SetVsMonster(char *effectiveness)
CCharacter chara[8]
void SetWepStatus(int status)
BT_SHOT_EFFECT * effect_data
char * vs_monster[8]
int ReEntry(BT_SHOT_EFFECT *description, CDataAlloc2< 1 > *allocator)
float random_rate[8]
void SetEnemyAttr(int attribute)
void SetDmg(int damage)
s32 weapon_status[8]
void OffEffect(int slot)
char wait_state[8]
void SetUserID2(int id)
int Entry(BT_SHOT_EFFECT *description, unsigned int *pack, int tex_block, CDataAlloc2< 1 > *allocator, int slots)
void SetRandomRate(float rate)
void SetLifeTime(int life_time)
void SetWait(int wait)
CCharacter template_chara
s32 user_sub_id[8]
void SetLoop(int loop)
int Entry2(BT_SHOT_EFFECT *description, unsigned int *pack, int tex_block, CDataAlloc2< 1 > *allocator, int slots)
s32 enemy_attribute[8]
char no_sound[8]
void SetAttribute(int attribute)
sceVu0FVECTOR velocity[8]
int Set(float *position, float *target, int owner, int sub_id, int source, CFrame *parent, int initial_phase)
float position[16][4]
float velocity[16][4]
int Set(float *origin, float *direction, int damage, int element)
void step()
s32 damage[12]
float size[12]
void draw()
s32 halted[12]
s32 used[12]
s32 life[12]
sceVu0FVECTOR vector[12]
sceVu0FVECTOR pos[12]
char vs_monster[10]
Definition itemdata.hpp:699
MOTION_INFO * motion_info
Definition gameutil.hpp:275
MOTION_STATE state
Definition gameutil.hpp:273