DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
fish.cpp
1#pragma name_counter 407
2
3#include "fish.hpp"
4
5#include <cstring>
6
7#include "dataread.hpp"
8#include "mathutil.hpp"
9
10FishInfo fish_info[18] = {
11 {17.0f, 10.0f, 20.0f, 20, 50, {1.0f, 0.0f, 0.0f, 0.2f, 0.2f, 0.2f, 0.0f, 0.2f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}},
12 {19.5f, 8.0f, 16.0f, 10, 30, {0.0f, 0.5f, 0.5f, 0.2f, 0.2f, 0.2f, 0.0f, 0.2f, 0.0f, 0.0f, 0.5f, 0.5f, 1.0f}},
13 {25.5f, 8.0f, 19.0f, 8, 25, {0.0f, 0.5f, 0.5f, 0.2f, 0.2f, 0.2f, 0.0f, 0.2f, 0.0f, 1.0f, 0.5f, 0.5f, 0.0f}},
14 {35.4f, 12.0f, 24.0f, 30, 60, {0.5f, 0.0f, 0.0f, 0.2f, 0.2f, 0.2f, 0.0f, 0.2f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f}},
15 {28.0f, 8.0f, 16.0f, 10, 40, {0.0f, 0.5f, 0.5f, 0.2f, 0.2f, 0.2f, 0.0f, 0.2f, 0.0f, 0.0f, 0.5f, 0.5f, 1.0f}},
16 {21.0f, 10.0f, 16.0f, 200, 400, {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}},
17 {20.0f, 6.0f, 12.0f, 15, 40, {0.0f, 1.0f, 0.5f, 0.2f, 0.2f, 0.2f, 0.0f, 0.2f, 0.0f, 0.5f, 0.5f, 0.5f, 0.0f}},
18 {20.0f, 6.0f, 10.0f, 20, 50, {0.0f, 0.5f, 1.0f, 0.2f, 0.2f, 0.2f, 0.0f, 0.2f, 0.0f, 0.5f, 0.5f, 0.5f, 0.0f}},
19 {1.0f, 1.0f, 2.0f, 1, 2, {0.0f, 0.0f, 0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30
31#include <libvu0.h>
32
33#include <cmath>
34#include <cstdlib>
35
36#include "collision.hpp"
37#include "mathutil.hpp"
38
39int PickUpNearPoly(CCPoly *out, CBoxVu0 box, CCPoly *polys, int count);
40
41int rand_check(float probability) {
42 int roll = rand();
43 float threshold = probability * 1000.0f;
44 (int) threshold;
45 return roll % 1000 < (int) threshold ? 1 : 0;
46}
47
48int GetActCnt() {
49 int count = 20;
50 count += rand() % 100;
51 return count;
52}
53
54int GetActCnt2() {
55 int count = 20;
56 count += rand() % 30;
57 return count;
58}
59
61 float position[4];
62
63 if (food_kind < 0) {
64 return 0;
65 }
66
67 GetPosition(position);
68
69 if (food_radius <= 0.0f) {
70 return 0;
71 }
72
73 if (DistVector(food_position, position) > food_radius) {
74 return 0;
75 }
76
77 return rand_check(info.food_affinity[food_kind] * 0.3f) != 0 ? 1 : 0;
78}
79
81 float position[4];
82
83 GetPosition(position);
84
85 if (food_radius <= 0.0f) {
86 return 0;
87 }
88
89 if (DistVector(food_position, position) > 1.0f) {
90 return 0;
91 }
92
93 return rand_check(info.food_affinity[food_kind] * 0.5f) != 0 ? 1 : 0;
94}
95
97 float position[4];
98
99 GetPosition(position);
100
101 if (food_radius <= 0.0f) {
102 return 0;
103 }
104
105 if (DistVector(food_position, position) > 1.0f) {
106 return 0;
107 }
108
109 return rand_check(info.food_affinity[food_kind] * 0.5f) != 0 ? 1 : 0;
110}
111
113 if (food_kind < 0) {
114 return 1;
115 }
116
117 return rand_check(0.1f) != 0 ? 1 : 0;
118}
119
121 if (food_kind < 0) {
122 return 1;
123 }
124
125 return rand_check(0.1f) != 0 ? 1 : 0;
126}
127
128void CFish::SetCPoly(CCPoly *polys, int count) {
129 collision_polys = polys;
130 collision_poly_count = count;
131}
132
134 float position[4];
135 float rotation[4];
136 float velocity[4];
137 float next_position[4];
138 float hit_position[4];
139 CCPoly nearby_polys[256];
140 CBoxVu0 box;
141
142 if (fish_kind < 0) {
143 return;
144 }
145
146 float turn_step = 0.05f;
147 float speed_step = 0.01f;
148 GetPosition(position);
149
150 if (action_timer < 0 || action == FISH_ACTION_NONE) {
151 action_timer = GetActCnt2();
152
153 switch (action) {
154 case FISH_ACTION_NONE:
155 if (FindFood()) {
156 action = FISH_ACTION_APPROACH_FOOD;
157 }
158
159 break;
160 case FISH_ACTION_APPROACH_FOOD:
161 if (EatFood()) {
162 action = FISH_ACTION_EAT_FOOD;
163 }
164
165 if (LeaveFood()) {
166 action = FISH_ACTION_LEAVE;
167 action_timer >>= 1;
168 }
169
170 break;
171 case FISH_ACTION_EAT_FOOD:
172 if (BiteHook()) {
173 action = FISH_ACTION_BITE_HOOK;
174 }
175
176 if (LeaveHook()) {
177 action = FISH_ACTION_LEAVE;
178 }
179
180 break;
181 case FISH_ACTION_BITE_HOOK:
182 action = FISH_ACTION_LEAVE;
183 action_timer *= 2;
184 break;
185 case FISH_ACTION_LEAVE:
186 action = FISH_ACTION_NONE;
187 break;
188 }
189 }
190
191 if (move_timer <= 0) {
192 if (move_mode == FISH_MOVE_UNSET) {
193 move_mode = FISH_MOVE_SWIM;
194 } else {
195 move_mode = (FishMoveMode) (rand() % 2);
196 }
197
198 move_timer = GetActCnt();
199 target_speed = 0.0f;
200
201 if (move_mode != FISH_MOVE_STOP) {
202 target_yaw = TWO_PI * ((float) rand() / 2147483648.0f);
203 target_yaw = AngleLimit(target_yaw);
204 move_timer += 30;
205 target_speed = 0.2f + (0.5f * (float) rand() / 2147483648.0f);
206 }
207 }
208
209 if (action == FISH_ACTION_APPROACH_FOOD) {
210 target_yaw = atan2f(food_position[0] - position[0], food_position[2] - position[2]);
211 target_speed = 0.1f;
212
213 if (DistVector(food_position, position) < 1.0f) {
214 target_speed = 0.0f;
215 }
216 }
217
218 if (action == FISH_ACTION_EAT_FOOD) {
219 target_yaw = atan2f(food_position[0] - position[0], food_position[2] - position[2]);
220 target_speed = 0.0f;
221 }
222
223 if (action == FISH_ACTION_BITE_HOOK) {
224 float food_yaw = atan2f(food_position[0] - position[0], food_position[2] - position[2]);
225 target_yaw = food_yaw + 0.2f * ((float) rand() / 2147483648.0f);
226 target_yaw = AngleLimit(target_yaw);
227 target_speed = 0.0f;
228 }
229
230 if (action == FISH_ACTION_BATTLE) {
231 target_yaw = TWO_PI * ((float) rand() / 2147483648.0f);
232 target_yaw = AngleLimit(target_yaw);
233 target_speed = 0.5f;
234 turn_step *= 2.0f;
235 speed_step *= 2.0f;
236 }
237
238 if (action == FISH_ACTION_LEAVE) {
239 target_yaw = atan2f(food_position[0] - position[0], food_position[2] - position[2]);
240 target_yaw = AngleLimit(target_yaw - PI);
241 target_speed = 0.5f;
242 }
243
244 --move_timer;
245 --action_timer;
246 GetRotation(rotation);
247 rotation[2] = 0.0f;
248 rotation[0] = 0.0f;
249 rotation[1] = AngleInterpolate(rotation[1], target_yaw, turn_step, INTERPOLATE_STEP);
250 SetRotation(rotation);
251
252 if (speed < target_speed) {
253 speed += speed_step;
254
255 if (speed > target_speed) {
257 }
258 }
259
260 if (speed > target_speed) {
261 speed -= speed_step;
262
263 if (speed < target_speed) {
265 }
266 }
267
268 velocity[0] = speed * sinf(rotation[1]);
269 velocity[1] = 0.0f;
270 velocity[2] = speed * cosf(rotation[1]);
271 velocity[3] = 0.0f;
272
273 switch (move_mode) {
274 case FISH_MOVE_STOP:
275 SetMotion(6, 0);
276 break;
277 case FISH_MOVE_SWIM:
278 SetMotion(0, 0);
279 break;
280 case FISH_MOVE_FAST:
281 SetMotion(1, 0);
282 break;
283 }
284
285 if (action == FISH_ACTION_BITE_HOOK || action == FISH_ACTION_BATTLE) {
286 SetMotion(1, 0);
287 }
288
291
292 if (use_angle_model) {
294 } else {
295 model.Step();
296 }
297
298 GetPosition(position);
299 sceVu0AddVector(next_position, position, velocity);
300
301 if (collision_polys != NULL) {
302 box.max[0] = position[0] + 10.0f;
303 box.max[1] = position[1] + 10.0f;
304 box.max[2] = position[2];
305 box.min[0] = position[0] - 10.0f;
306 box.min[1] = position[1];
307 box.min[2] = position[2] - 10.0f;
308 int count = PickUpNearPoly(nearby_polys, box, collision_polys, collision_poly_count);
309
310 if (CheckHit(nearby_polys, count, position, next_position, hit_position, 0, 0) < 0) {
311 SetPosition(next_position);
312 }
313
314 GetPosition(position);
315 CheckWidth(nearby_polys, count, position, 2.0f * angle_model_scale, next_position, 0);
316 SetPosition(next_position);
317 }
318
319 if (move_mode == FISH_MOVE_ANGLE) {
320 float zero[4] = {0.0f, 0.0f, 0.0f, 0.0f};
321 SetPosition(zero);
322 SetRotation(zero);
323 }
324}
325
327 if (fish_kind < 0) {
328 return;
329 }
330
331 if (use_angle_model != 0) {
333 } else {
334 model.Draw();
335 }
336}
337
338void CFish::SetFoodPos(int kind, float *position, float radius) {
339 food_kind = kind;
340 sceVu0CopyVector(food_position, position);
341 food_radius = radius;
342}
343
345 move_mode = FISH_MOVE_ANGLE;
346 action = FISH_ACTION_ANGLE;
347 target_speed = 0.0f;
348}
349
351 move_mode = FISH_MOVE_BATTLE;
352 action = FISH_ACTION_BATTLE;
353}
354
356 float deviation = nrnd();
358
359 if (deviation >= 0.0f) {
360 size += deviation * (info.max_size - info.min_size) / 4.0f;
361 } else {
362 size += deviation * (info.max_size - info.min_size) / 8.0f;
363 }
364
365 if (size < 0.5f * info.min_size) {
366 size = 0.5f * info.min_size;
367 }
368
369 if (size > info.max_size) {
371 }
372
374 model_scale = size / 25.0f;
375}
376
378 float points;
379
380 if (size >= info.min_size) {
382 } else {
383 points = info.min_fp - 0.5f * (info.min_fp * (info.min_size - size) / (info.min_size - 0.5f * info.min_size));
384 }
385
386 return (int) points;
387}
388
389void CFish::SetMotion(int motion_no, int flags) {
390 model.motion_no = motion_no;
391 model.motion_flags = flags;
392 model.motion_speed = -1.0f;
393 angle_model.motion_no = motion_no;
396}
397
398void CFish::SetPosition(float *position) {
399 model.SetPosition(position);
400 angle_model.SetPosition(position);
401}
402
403void CFish::GetPosition(float *position) { angle_model.GetPosition(position); }
404
405void CFish::SetRotation(float *rotation) {
406 model.SetRotation(rotation);
407 angle_model.SetRotation(rotation);
408}
409
410void CFish::GetRotation(float *rotation) { angle_model.GetRotation(rotation); }
411
412void CFish::SetReference(CFrame *reference) {
413 if (model.frame != NULL) {
414 model.frame->SetReference(reference);
415 }
416
417 if (angle_model.frame != NULL) {
418 angle_model.frame->SetReference(reference);
419 }
420}
421
423 if (model.frame != NULL) {
424 model.frame->DeleteReference();
425 }
426
427 if (angle_model.frame != NULL) {
428 angle_model.frame->DeleteReference();
429 }
430}
431
433 fish_kind = -1;
434 action = FISH_ACTION_NONE;
435 move_mode = FISH_MOVE_UNSET;
436 move_timer = 0;
437 action_timer = 0;
438 use_angle_model = false;
439 angle_model_scale = 1.0f;
440 size = 10.0f;
441 model_scale = 1.0f;
444 collision_polys = NULL;
446 unk_78 = 0;
447 target_yaw = 0.0f;
448 unk_70 = 0;
449 target_speed = 0.0f;
450 speed = 0.0f;
451 food_radius = -1.0f;
452}
453
454/* Nothing reads these: the link this file was built by removed them. They are here because
455 the compiler writes a string constant where it first meets it, and the six the table in
456 GetFishFileName shares with them stand ahead of the two LoadFish uses. */
457static char *fish_file_01 = "chara/f01a.chr";
458static char *fish_file_02 = "chara/f02a.chr";
459static char *fish_file_03 = "chara/f03a.chr";
460static char *fish_file_04 = "chara/f04a.chr";
461static char *fish_file_05 = "chara/f05a.chr";
462static char *fish_file_06 = "chara/f06a.chr";
463
464void LoadFish(CFish *fish, int fish_kind, int texture_slot, CDataAlloc2<1> *alloc, int loaded_before) {
465 if (fish_kind < 0 || fish_kind >= 18) {
466 return;
467 }
468
469 LoadFile("chara/f00s.chr", read_buffer, NULL);
470 fish->Initialize();
471 fish->model.LoadPackData3(read_buffer, "info.cfg", alloc, texture_slot, alloc, loaded_before, 0);
472 memcpy(&fish->info, &fish_info[fish_kind], sizeof(FishInfo));
473 fish->SetScale();
474 fish->fish_kind = fish_kind;
475}
476
477char *GetFishFileName(int fish_kind) {
478 static char *name[18] = {
479 "chara/f01a.chr",
480 "chara/f02a.chr",
481 "chara/f03a.chr",
482 "chara/f04a.chr",
483 "chara/f05a.chr",
484 "chara/f06a.chr",
485 "chara/f07a.chr",
486 "chara/f08a.chr",
487 "chara/f09a.chr",
488 "chara/f10a.chr",
489 "chara/f11a.chr",
490 "chara/f12a.chr",
491 "chara/f13a.chr",
492 "chara/f14a.chr",
493 "chara/f15a.chr",
494 "chara/f16a.chr",
495 "chara/f17a.chr",
496 "chara/f18a.chr",
497 };
498
499 if (fish_kind < 0 || fish_kind >= 18) {
500 return NULL;
501 }
502
503 return name[fish_kind];
504}
sceVu0FVECTOR min
Definition collision.hpp:29
sceVu0FVECTOR max
Definition collision.hpp:28
CFrame * frame
virtual void GetPosition(float *out_position)
virtual void SetScale(float x, float y, float z)
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()
virtual void SetRotation(float x, float y, float z)
virtual void Initialize()
float motion_speed
virtual void Draw()
virtual void SetPosition(float x, float y, float z)
Definition fish.hpp:57
s32 move_timer
Definition fish.hpp:63
void Step()
Definition fish.cpp:133
void Draw()
Definition fish.cpp:326
void DeleteReference()
Definition fish.cpp:422
float target_yaw
Definition fish.hpp:71
CCharacter angle_model
Definition fish.hpp:80
void GetRotation(float *rotation)
Definition fish.cpp:410
void SetMotion(int motion_no, int flags)
Definition fish.cpp:389
s32 action_timer
Definition fish.hpp:64
int LeaveFood()
Definition fish.cpp:112
int BiteHook()
Definition fish.cpp:96
FishMoveMode move_mode
Definition fish.hpp:61
float food_radius
Definition fish.hpp:77
void SetPosition(float *position)
Definition fish.cpp:398
void SetScale()
Definition fish.cpp:355
float size
Definition fish.hpp:66
void SetRotation(float *rotation)
Definition fish.cpp:405
void SetAngleMode()
Definition fish.cpp:344
FishAction action
Definition fish.hpp:62
int LeaveHook()
Definition fish.cpp:120
float model_scale
Definition fish.hpp:68
FishInfo info
Definition fish.hpp:60
CCharacter model
Definition fish.hpp:79
s32 fish_kind
Definition fish.hpp:59
void SetCPoly(CCPoly *polys, int count)
Definition fish.cpp:128
void SetBattleMode()
Definition fish.cpp:350
int EatFood()
Definition fish.cpp:80
sceVu0FVECTOR food_position
Definition fish.hpp:78
void SetFoodPos(int kind, float *position, float radius)
Definition fish.cpp:338
float target_speed
Definition fish.hpp:74
void SetReference(CFrame *reference)
Definition fish.cpp:412
float speed
Definition fish.hpp:75
s32 collision_poly_count
Definition fish.hpp:82
void GetPosition(float *position)
Definition fish.cpp:403
s32 food_kind
Definition fish.hpp:76
void Initialize()
Definition fish.cpp:432
int GetFP()
Definition fish.cpp:377
s32 use_angle_model
Definition fish.hpp:65
int FindFood()
Definition fish.cpp:60
float angle_model_scale
Definition fish.hpp:67
CCPoly * collision_polys
Definition fish.hpp:81
virtual void GetRotation(float *out_rotation)
Definition object.cpp:116
@ INTERPOLATE_STEP
Definition mathutil.hpp:15
s32 max_fp
Definition fish.hpp:23
float min_size
Definition fish.hpp:20
float max_size
Definition fish.hpp:21
s32 min_fp
Definition fish.hpp:22
float model_size
Definition fish.hpp:19
float food_affinity[FISH_FOOD_KIND_COUNT]
Definition fish.hpp:24