DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
dungeonmap.cpp
1#pragma helper_mask_gpr 0x30
2#pragma helper_mask_fpr 0x1000
3#pragma name_counter 1008
4#pragma argument_flag 0
5#pragma argument_flag_ones 69, 109, 232, 233, 278, 307, 308, 353, 409, 424
6#pragma argument_flag_ones 426, 480, 481, 497, 498, 499, 959
7
8#include "dungeonmap.hpp"
9
10#include <libvu0.h>
11
12#include <cmath>
13#include <cstdio>
14#include <cstdlib>
15#include <cstring>
16
17#include "camera.hpp"
18#include "character.hpp"
19#include "dataset.hpp"
20#include "dun/gameloop.hpp"
21#include "dungeonparts.hpp"
22#include "frame.hpp"
23#include "frameattr.hpp"
24#include "framevu1.hpp"
25#include "mathutil.hpp"
26#include "mglib.hpp"
27#include "rect.hpp"
28#include "snd.hpp"
29#include "texture.hpp"
30#include "textureanime.hpp"
31#include "userstatus.hpp"
32
36// clang-format off
37enum BuildCellFlag {
38 BUILD_CELL_ROOM = 0x001,
39 BUILD_CELL_CORRIDOR = 0x002,
40 BUILD_CELL_DOOR = 0x004,
41 BUILD_CELL_HEAL_SPRING = 0x008,
42 BUILD_CELL_DIVIDING_WALL = 0x010,
43 BUILD_CELL_DIVIDING_DOOR = 0x020,
44 BUILD_CELL_PILLAR = 0x040,
45 BUILD_CELL_STAIR_DOWN = 0x080,
46 BUILD_CELL_STAIR_UP = 0x100,
47 BUILD_CELL_URA_ENTRANCE = 0x200,
48 BUILD_CELL_ROOM_LINK = 0x400,
49};
50
51// clang-format on
52
57 s32 kind;
59 s32 link[16];
60 s32 content;
61};
62
63STATIC_ASSERT(sizeof(BUILD_MAP_INFO) == 0x4C);
64
69 int offset[4][2];
70};
71
72/* Tables that translate the cells that touch a cell into the map part it
73 * draws, one row of adjacency mask, map part and direction each. */
74
76s32 chainTableDoor[4][3] = {
78 {13, MAP_PARTS_ROOM_DOOR_EAST, 0},
79 {7, MAP_PARTS_ROOM_DOOR_SOUTH, 0},
80 {11, MAP_PARTS_ROOM_DOOR_WEST, 0},
81};
82
84s32 chainTableRoad[15][3] = {
89 {10, MAP_PARTS_ROAD_CORNER, 2},
90 {12, MAP_PARTS_ROAD_CORNER, 3},
92 {1, MAP_PARTS_ROAD_TEE, 0},
93 {2, MAP_PARTS_ROAD_TEE, 1},
94 {8, MAP_PARTS_ROAD_TEE, 2},
95 {4, MAP_PARTS_ROAD_TEE, 3},
100};
101
103s32 chainTableRoom[8][3] = {
104 {1, MAP_PARTS_ROOM_WALL_NORTH, 0},
105 {2, MAP_PARTS_ROOM_WALL_EAST, 0},
106 {8, MAP_PARTS_ROOM_WALL_SOUTH, 0},
107 {4, MAP_PARTS_ROOM_WALL_WEST, 0},
108 {5, MAP_PARTS_ROOM_CORNER_NORTH_WEST, 0},
109 {3, MAP_PARTS_ROOM_CORNER_NORTH_EAST, 0},
110 {10, MAP_PARTS_ROOM_CORNER_SOUTH_EAST, 0},
111 {12, MAP_PARTS_ROOM_CORNER_SOUTH_WEST, 0},
112};
113
115s32 chainTableDivid[6][3] = {
117 {0xD4, MAP_PARTS_DIVIDE_END_EAST, 0},
118 {0x71, MAP_PARTS_DIVIDE_END_SOUTH, 0},
119 {0xB2, MAP_PARTS_DIVIDE_END_WEST, 0},
121 {0xF6, MAP_PARTS_DIVIDE_WALL_EAST, 0},
122};
123
125s32 chainTableDividDoor[2][3] = {
127 {6, MAP_PARTS_DIVIDE_DOOR_EAST, 0},
128};
129
130void CDungeonMap::SetNPC(int npc_no, unsigned int *pack, int parts_no, sceVu0FVECTOR pos, sceVu0FVECTOR rot, int visible, int motion_no, CDataAlloc2<1> *alloc) {
131 int i;
132
133 if (this->npc[npc_no].used != 0) {
134 return;
135 }
136
137 if (npc_no >= 4 || npc_no < 0) {
138 return;
139 }
140
141 CFrameAttr attr;
142
143 attr.fog_enable = true;
144
145 for (i = 0; i < 32; i++) {
146 BtNPCTexAnimeData[npc_no][i].Initialize();
147 }
148
149 this->npc[npc_no].chara.InitializeTexAnime(BtNPCTexAnimeData[npc_no], 32);
150 this->npc[npc_no].chara.LoadPackData2(pack, info_cfg_literal, alloc, npc_no + 64, alloc, 0);
151
152 if (this->npc[npc_no].chara.frame == NULL) {
153 printf("******* NPCEntryErr\n");
154 }
155
156 this->npc[npc_no].chara.frame->SetAttr(attr, 1, 64);
157 SetFrameAttr(this->npc[npc_no].chara.frame, 1);
158
159 sceVu0CopyVector(this->npc[npc_no].pos, pos);
160 sceVu0CopyVector(this->npc[npc_no].rot, rot);
161 this->npc[npc_no].parts_no = parts_no;
162 this->npc[npc_no].visible = visible;
163 this->npc[npc_no].used = 1;
164 this->npc[npc_no].motion_no = motion_no;
165 this->npc[npc_no].chara.motion_no = motion_no;
166 this->npc[npc_no].chara.motion_flags = 0;
167 this->npc[npc_no].chara.motion_speed = -1.0f;
168}
169
171 for (int i = 0; i < 4; i++) {
172 this->npc[i].draw_num = 0;
173 }
174}
175
176void CDungeonMap::ReservNPC_Draw(int npc_no, float x, float y, float z, int direction) {
177 if (this->npc[npc_no].used != 0 && this->npc[npc_no].visible != 0 && this->npc[npc_no].chara.frame != NULL) {
178 this->npc[npc_no].draw_pos[this->npc[npc_no].draw_num][0] = x;
179 this->npc[npc_no].draw_pos[this->npc[npc_no].draw_num][1] = y;
180 this->npc[npc_no].draw_pos[this->npc[npc_no].draw_num][2] = z;
181 this->npc[npc_no].draw_param[this->npc[npc_no].draw_num] = direction;
182 this->npc[npc_no].draw_num++;
183 }
184}
185
187 float pos[4];
188 float unused[4];
189 int i;
190 int j;
191
192 for (i = 0; i < 4; i++) {
193 for (j = 0; j < this->npc[i].draw_num; j++) {
194 int direction;
195 float angle;
196 float deg;
197 float turn;
198
199 if (this->npc[i].chara.frame == NULL) {
200 break;
201 }
202
203 sceVu0CopyVector(pos, this->npc[i].draw_pos[j]);
204 sceVu0CopyVector(unused, this->npc[i].draw_pos[j]);
205 pos[0] += this->npc[i].pos[0];
206 pos[1] += this->npc[i].pos[1];
207 pos[2] += this->npc[i].pos[2];
208 pos[3] = 1.0f;
209 this->npc[i].chara.SetPosition(pos);
210
211 direction = this->npc[i].draw_param[j];
212
213 if (direction > 3) {
214 direction -= 3;
215 }
216
217 if (direction == 3) {
218 direction = -1;
219 }
220
221 deg = -90.0f * direction;
222 angle = (PI * deg) / 180.0f;
223 angle += this->npc[i].rot[1];
224
225 if (!(angle <= PI)) {
226 angle -= TWO_PI;
227 }
228
229 if (angle < -PI) {
230 angle += TWO_PI;
231 }
232
233 this->npc[i].chara.SetRotation(0.0f, angle, 0.0f);
234 TexManager.ReloadTexture(Vif1Packet, i + 64);
235 this->npc[i].chara.TextureAnime(i + 64);
236 this->npc[i].chara.Draw();
237 }
238 }
239}
240
242 for (int i = 0; i < 4; i++) {
243 if (this->npc[i].used != 0 && this->npc[i].visible != 0 && this->npc[i].chara.frame != NULL && this->npc[i].motion_no != -1) {
244 this->npc[i].chara.Step();
245 }
246 }
247}
248
249void CDungeonMap::NPCSetMotion(int npc_no, int motion_no) {
250 this->npc[npc_no].chara.motion_no = motion_no;
251 this->npc[npc_no].chara.motion_flags = 0;
252 this->npc[npc_no].chara.motion_speed = -1.0f;
253}
254
255void CDungeonMap::NPCSetMotion(int npc_no, int motion_no, float speed, int motion_flags) {
256 this->npc[npc_no].chara.motion_no = motion_no;
257 this->npc[npc_no].chara.motion_flags = motion_flags;
258 this->npc[npc_no].chara.motion_speed = -1.0f;
259 this->npc[npc_no].chara.motion_speed = speed;
260#ifdef PAL_TIMING
261 this->npc[npc_no].chara.motion_speed = 6.0f * speed / 5.0f;
262#endif
263}
264
266 for (int i = 0; i < 72; i++) {
267 CFrame *frame = this->parts[i].GetSearchFrame(name);
268
269 if (frame != NULL) {
270 return frame;
271 }
272 }
273
274 return NULL;
275}
276
278 int i;
279 int npc_no;
280
281 this->ClearNPC_Cash();
282
283 for (i = 0; this->parts[i].frame[0] != NULL; i++) {
284 this->parts[i].pos[0] = 0.0;
285 this->parts[i].pos[1] = 0.0;
286 this->parts[i].pos[2] = 0.0;
287 this->parts[i].pos[3] = 1.0;
288 this->parts[i].direction = 0;
289 this->parts[i].Draw();
290
291 for (npc_no = 0; npc_no < 4; npc_no++) {
292 if (this->npc[npc_no].parts_no == i) {
293 this->ReservNPC_Draw(npc_no, 0.0, 0.0, 0.0, 0);
294 }
295 }
296 }
297}
298
300 int row, col;
301 int i;
302
303 if (mode == 1) {
304
305 for (row = 0; row < 20; row++) {
306 for (col = 0; col < 20; col++) {
307 int cell_no = col + row * 20;
308
309 if (this->cells[cell_no].parts_no != MAP_PARTS_NONE) {
310 CDungeonParts *part;
311 int dir = this->cells[cell_no].direction;
312
313 part = &this->parts[this->cells[cell_no].parts_no];
314 part->direction = dir;
315 part = &this->parts[this->cells[cell_no].parts_no];
316 part->pos[0] = 160.0f * col;
317 part->pos[1] = 0.0f;
318 part->pos[2] = 160.0f * row;
319 part->pos[3] = 1.0f;
320 this->parts[this->cells[cell_no].parts_no].DrawCalc(col, row, this->cells[cell_no].direction, mode);
321 }
322 }
323 }
324 } else {
325 for (i = 0; this->parts[i].frame[0] != NULL; i++) {
326 this->parts[i].DrawCalc(0, 0, 0, mode);
327 }
328 }
329}
330
332 float cam_pos[4];
333 float view_delta[4];
334 float cam_ref[4];
335 float view[4];
336 float player_pos[4];
337 float sound_pos[4];
338 float cell_delta[4];
339 float ambient[4];
340 float old_ambient[4];
341 ITEM_FREE_AREA *free_area;
342 float volume;
343 float pan;
344 float dist;
345 float delta_x;
346 float nearest;
347 float world_x;
348 float world_z;
349 float delta_z;
350 int sound_no;
351 int row;
352 int col;
353 int cell_no;
354 int draw;
355 int npc_no;
356 int area_no;
357 int rect_no;
358 int player_cell_x;
359 int player_cell_y;
360
361 ((CCamera *) camera)->GetPos(cam_pos);
362 ((CCamera *) camera)->GetRef(cam_ref);
363 view_delta[0] = cam_ref[0] - cam_pos[0];
364 view_delta[1] = cam_ref[1] - cam_pos[1];
365 view_delta[2] = cam_ref[2] - cam_pos[2];
366 view_delta[3] = 1.0f;
367 sceVu0Normalize(view, view_delta);
368 sceVu0CopyVector(player_pos, player->position);
369 player_cell_x = (int) (player_pos[0] / 160.0f);
370 player_cell_y = (int) (player_pos[2] / 160.0f);
371 free_area = ItemFreeAreaAll[selectMapNo];
372
373 this->ClearNPC_Cash();
374 nearest = 10000.0f;
375
376 for (row = 0; row < 20; row++) {
377 for (col = 0; col < 20; col++) {
378 world_x = 160.0f * col;
379 delta_x = world_x - cam_pos[0];
380 world_z = 160.0f * row;
381 delta_z = world_z - cam_pos[2];
382
383 cell_delta[0] = delta_x;
384 cell_delta[1] = 0.0f;
385 cell_delta[2] = delta_z;
386 cell_delta[3] = 0.0f;
387 dist = DistVector(cell_delta);
388 cell_no = col + row * 20;
389 this->cells[cell_no].camera_dist = dist;
390
391 if (this->cells[cell_no].parts_no == MAP_PARTS_NONE) {
392 this->cells[cell_no].visible = false;
393 continue;
394 }
395
396 if (this->cells[cell_no].parts_no == MAP_PARTS_KEY_XIAO) {
397 if (UserStatus->cur_georama == 2 && dist < nearest) {
398 nearest = dist;
399 sound_pos[0] = world_x;
400 sound_pos[1] = 0.0f;
401 sound_pos[2] = world_z;
402 sound_pos[3] = 1.0f;
403 sound_no = 65;
404 }
405 }
406
407 // Part 75 is unknown
408 if (this->cells[cell_no].parts_no == 75 && dist < nearest) {
409 nearest = dist;
410 sound_pos[0] = world_x;
411 sound_pos[1] = 0.0f;
412 sound_pos[2] = world_z;
413 sound_pos[3] = 1.0f;
414 sound_no = 75;
415 }
416
417 if (!(dist < 160.0f * this->draw_dist_scale)) {
418 draw = false;
419 } else {
420 int georama;
421
422 cam_ref[0] = delta_x - view_delta[0];
423 cam_ref[1] = 0.0f;
424 cam_ref[2] = delta_z - view_delta[2];
425 cam_ref[3] = 1.0f;
426 sceVu0Normalize(cam_ref, cam_ref);
427 float facing = sceVu0InnerProduct(view, cam_ref);
428 draw = true;
429 georama = UserStatus->cur_georama;
430
431 if (georama == 5 && this->cells[cell_no].parts_no == MAP_PARTS_URA_ROAD) {
432 facing = 1.0f;
433 }
434
435 if (facing <= 0.0f && !(dist < 160.0f)) {
436 draw = false;
437 }
438
439 if (this->cells[cell_no].parts_no >= MAP_PARTS_URA_ENTRANCE_NORTH && this->cells[cell_no].parts_no <= MAP_PARTS_URA_ENTRANCE_WEST && georama == 5) {
440 draw = true;
441 }
442 }
443
444 this->cells[cell_no].visible = draw;
445
446 if (this->cells[cell_no].parts_no == MAP_PARTS_NONE) {
447 continue;
448 }
449
450 if (draw != 1) {
451 continue;
452 }
453
454 int direction = this->cells[cell_no].direction;
455 CDungeonParts *direction_part = &this->parts[this->cells[cell_no].parts_no];
456 direction_part->direction = direction;
457 {
458 CDungeonParts *position_part = &this->parts[this->cells[cell_no].parts_no];
459 position_part->pos[0] = world_x;
460 position_part->pos[1] = 0.0f;
461 position_part->pos[2] = world_z;
462 position_part->pos[3] = 1.0f;
463 }
464
465 for (npc_no = 0; npc_no < 4; npc_no++) {
466 if (this->npc[npc_no].parts_no == this->cells[cell_no].parts_no) {
467 this->ReservNPC_Draw(npc_no, world_x, 0.0f, world_z, this->cells[cell_no].direction);
468 }
469 }
470
471 if (UserStatus->cur_georama == 4) {
472 MGGetAmbient(old_ambient);
473 MGGetAmbient(ambient);
474
475 if (dist <= 480.0f) {
476 ambient[3] = 128.0f;
477 } else {
478 ambient[3] = 128.0f - (dist - 480.0f);
479
480 if (ambient[3] < 0.0f) {
481 ambient[3] = 0.0f;
482 }
483 }
484
485 MGSetAmbient(ambient);
486 }
487
488 this->parts[this->cells[cell_no].parts_no].Draw();
489
490 if (UserStatus->cur_georama == 4) {
491 MGSetAmbient(old_ambient);
492 }
493
494 // Debug status six shows each configured free area on its map part.
495 if (DebugStatus[6] != 0) {
496 for (area_no = 0; free_area[area_no].parts_no != MAP_PARTS_NONE; area_no++) {
497 if (free_area[area_no].parts_no != this->cells[cell_no].parts_no) {
498 continue;
499 }
500
501 for (rect_no = 0; rect_no < free_area[area_no].rect_num; rect_no++) {
502 float corner[4][4];
503 int screen[4][4];
504 int all_visible;
505 int corner_no;
506 int rotation;
507
508 rotation = (int) (float) free_area[area_no].direction;
509 rotation = rotation + this->cells[cell_no].direction;
510
511 if (rotation > 3) {
512 rotation -= 4;
513 }
514
515 all_visible = 1;
516 float radians = (PI * ((4 - rotation) * 90)) / 180.0f;
517 float x[4];
518 float z[4];
519 float height;
520 float left = free_area[area_no].rect[rect_no].x0;
521 x[0] = 10.0f * left;
522 height = 10.0f * free_area[area_no].rect[rect_no].y0;
523 z[0] = 10.0f * free_area[area_no].rect[rect_no].z0;
524 x[3] = 10.0f * free_area[area_no].rect[rect_no].x1;
525 z[3] = 10.0f * free_area[area_no].rect[rect_no].z1;
526 x[1] = x[3];
527 z[1] = z[0];
528 x[2] = x[0];
529 z[2] = z[3];
530
531 for (corner_no = 0; corner_no < 4; corner_no++) {
532 corner[corner_no][0] = -z[corner_no] * sinf(radians) - x[corner_no] * cosf(radians);
533 corner[corner_no][2] = -x[corner_no] * sinf(radians) + z[corner_no] * cosf(radians);
534 corner[corner_no][0] *= -1.0f;
535 corner[corner_no][0] += world_x;
536 corner[corner_no][1] = 2.0f + height;
537 corner[corner_no][2] += world_z;
538 corner[corner_no][3] = 1.0f;
539
540 if (MGRotTransPers(screen[corner_no], corner[corner_no], 0) == 0) {
541 all_visible = 0;
542 }
543 }
544
545 if (all_visible != 0) {
546 setColSprite(Vif1Packet, screen[0], screen[1], screen[2], screen[3], 0x80, 0, 0, 0x40);
547 }
548 }
549 }
550 }
551 }
552 }
553
554 if (nearest < 10000.0f) {
555 SndGetVolPan(&volume, &pan, sound_pos, 10.0f, 500.0f);
556 SndSetSeVolf(sound_no, volume, 0);
557 SndSetSePanf(sound_no, pan, 0);
558 }
559}
560
562 float pos[4];
563
564 // The background follows the camera, so that it stays at the horizon.
565 camera->GetPos(pos);
566 pos[1] = 0.0f;
567
568 for (int i = 0; i < 6; i++) {
569 if (this->bg_model[i] != NULL) {
570 this->bg_model[i]->SetPosition(pos);
571 MGDraw(this->bg_model[i]);
572 }
573 }
574}
575
577 float pos[4];
578
579 if (this->dummy_num == 0) {
580 return;
581 }
582
583 // A dummy model only draws while the camera is near enough to it.
584 camera->GetPos(pos);
585
586 for (int i = 0; i < this->dummy_num; i++) {
587 if (this->dummy_frame[this->dummy_model[i]] != NULL) {
588 if (DistVector(this->dummy_pos[i], pos) < 160.0f * (1.0f + this->draw_dist_scale)) {
589 this->dummy_frame[this->dummy_model[i]]->SetPosition(this->dummy_pos[i]);
590 MGDraw(this->dummy_frame[this->dummy_model[i]]);
591 }
592 }
593 }
594}
595
596void CDungeonMap::DrawMiniMap(float *pos, float angle) {
597 int status;
598 unsigned char red;
599 int saved_map;
600 int saved_crystal;
601 unsigned char green;
602 unsigned char blue;
603 int row;
604 int col;
605 int direction;
606 int map_x;
607 int map_y;
608 int cellx;
609 int visible;
610 int show;
611 float player_screen_x;
612 CTexture *map_texture;
613 CTexture *symbol_texture;
614 unsigned char alpha;
615 int masked;
616 int cell_no;
617 CRect_i_ map_dst;
618 CRect_i_ map_src;
619 CRect_i_ player_dst;
620 CRect_i_ player_src;
621 CRect_i_ trap_dst;
622 CRect_i_ trap_src;
623 CRect_i_ box_dst;
624 CRect_i_ box_src;
625 CRect_i_ atra_dst;
626 CRect_i_ atra_src;
627 status = UserStatus->minimap_status;
628
629 if (status == 3) {
630 return;
631 }
632
633 unsigned char red_table[3] = {0x20, 0x40, 0x60};
634 unsigned char green_table[3] = {0x60, 0x80, 0xA2};
635 unsigned char blue_table[3] = {0x40, 0x60, 0xA2};
636
637 red = red_table[status];
638 green = green_table[status];
639 blue = blue_table[status];
640 saved_map = BtEquipMap;
641 saved_crystal = BtEquipMasuisyou;
642
643 if (DebugStatus[3] != 0) {
644 BtEquipMap = 1;
645 BtEquipMasuisyou = 1;
646 }
647
648 visible = BtEquipMap ? 0 : 1;
649 map_texture = TexManager.GetTexture("minimap1", -1);
650 symbol_texture = TexManager.GetTexture("itempack", -1);
651
652 for (row = 0; row < 20; row++) {
653 for (col = 0; col < 20; col++) {
654 cell_no = col + row * 20;
655 masked = this->mask[cell_no];
656 show = masked;
657
658 if (visible == 0 && masked == 0) {
659 show = 1;
660 alpha = red;
661 } else {
662 alpha = green;
663 }
664
665 if (this->cells[cell_no].parts_no != MAP_PARTS_NONE && show == 1) {
666 direction = this->cells[cell_no].direction + this->parts[this->cells[cell_no].parts_no].direction_offset;
667 map_src.x = (direction % 8) * 16;
668 map_src.y = (direction / 8) * 16;
669 map_src.width = 16;
670 map_src.height = 16;
671 map_dst.x = col * 16 + 380;
672 map_dst.y = row * 16 + 64;
673 map_dst.width = 16;
674 map_dst.height = 16;
675 set2DSprite(Vif1Packet, map_texture, map_dst, map_src, alpha, alpha, alpha, blue);
676 }
677 }
678 }
679
680 pos[0] *= 0.1f;
681 pos[2] *= 0.1f;
682 player_src.x = 88;
683 player_src.y = 96;
684 player_src.width = 12;
685 player_src.height = 12;
686 map_x = (int) (pos[2] + 64.0f + 8.0f);
687 player_screen_x = pos[0] + 380.0f + 8.0f;
688 map_y = (int) player_screen_x;
689 player_dst.x = (int) player_screen_x;
690 player_dst.y = map_x;
691 player_dst.width = 12;
692 player_dst.height = 12;
693 set2DSprite(Vif1Packet, symbol_texture, player_dst, player_src, 7, 7, angle);
694
695 for (int i = 0; i < 3; i++) {
696 int cell;
697
698 if (this->trap_circle[i].state == 0) {
699 continue;
700 }
701
702 map_x = (int) (this->trap_circle[i].pos[0] * 0.1f);
703 map_y = (int) (this->trap_circle[i].pos[2] * 0.1f);
704 cellx = (int) ((80.0f + this->trap_circle[i].pos[0]) / 160.0f);
705 cell = cellx + (int) ((80.0f + this->trap_circle[i].pos[2]) / 160.0f) * 20;
706 show = this->mask[cell];
707
708 if (BtEquipMasuisyou == 1) {
709 show = 1;
710 }
711
712 if (this->cells[cell].parts_no != MAP_PARTS_NONE && show == 1) {
713 trap_src.x = 88;
714 trap_src.y = 112;
715 trap_src.width = 8;
716 trap_src.height = 8;
717 trap_dst.x = map_x + 384;
718 trap_dst.y = map_y + 68;
719 trap_dst.width = 8;
720 trap_dst.height = 8;
721 set2DSprite(Vif1Packet, symbol_texture, trap_dst, trap_src, blue);
722 }
723 }
724
725 for (int i = 0; i < 24; i++) {
726 int cell;
727
728 if (this->boxes[i].used == 0 || this->boxes[i].closed == 0) {
729 continue;
730 }
731
732 map_x = (int) (this->boxes[i].pos[0] * 0.1f);
733 map_y = (int) (this->boxes[i].pos[2] * 0.1f);
734 cellx = (int) ((80.0f + this->boxes[i].pos[0]) / 160.0f);
735 cell = cellx + (int) ((80.0f + this->boxes[i].pos[2]) / 160.0f) * 20;
736 show = this->mask[cell];
737
738 if (BtEquipMasuisyou == 1) {
739 show = 1;
740 }
741
742 if (this->cells[cell].parts_no != MAP_PARTS_NONE && show == 1) {
743 box_src.x = 80;
744 box_src.y = 96;
745 box_src.width = 8;
746 box_src.height = 8;
747 box_dst.x = map_x + 384;
748 box_dst.y = map_y + 68;
749 box_dst.width = 8;
750 box_dst.height = 8;
751 set2DSprite(Vif1Packet, symbol_texture, box_dst, box_src, blue);
752 }
753 }
754
755 for (int i = 0; i < 8; i++) {
756 int cell;
757
758 if (this->atra[i].used == 0) {
759 continue;
760 }
761
762 map_x = (int) (this->atra[i].pos[0] * 0.1f);
763 map_y = (int) (this->atra[i].pos[2] * 0.1f);
764 cellx = (int) ((80.0f + this->atra[i].pos[0]) / 160.0f);
765 cell = cellx + (int) ((80.0f + this->atra[i].pos[2]) / 160.0f) * 20;
766 show = this->mask[cell];
767
768 if (BtEquipMasuisyou == 1) {
769 show = 1;
770 }
771
772 if (this->cells[cell].parts_no != MAP_PARTS_NONE && show == 1) {
773 atra_src.x = 72;
774 atra_src.y = 104;
775 atra_src.width = 8;
776 atra_src.height = 8;
777 atra_dst.x = map_x + 384;
778 atra_dst.y = map_y + 68;
779 atra_dst.width = 8;
780 atra_dst.height = 8;
781 set2DSprite(Vif1Packet, symbol_texture, atra_dst, atra_src, blue);
782 }
783 }
784
785 BtEquipMap = saved_map;
786 BtEquipMasuisyou = saved_crystal;
787}
788
789void CDungeonMap::checkMask(float x, float z) {
790 int cell_x;
791 int cell_z;
792 int room;
793
794 room = -1;
795 cell_x = (int) ((80.0f + x) / 160.0f);
796 cell_z = (int) ((80.0f + z) / 160.0f);
797
798 // Find the room that holds the cell that the player stands in.
799 for (int i = 0; i < this->room_num; i++) {
800 if (cell_x >= this->rooms[i].x && cell_x < this->rooms[i].x + this->rooms[i].width) {
801 if (cell_z >= this->rooms[i].y && cell_z < this->rooms[i].y + this->rooms[i].height) {
802 room = i;
803 }
804 }
805 }
806
807 // Mark the entire room as seen when the player enters it for the first time.
808 if (room != -1) {
809 if (this->room_seen[room] == 0) {
810 if (this->rooms[room].width != 0) {
811 int col;
812 int row;
813
814 this->room_seen[room] = 1;
815
816 for (row = this->rooms[room].y; row < this->rooms[room].y + this->rooms[room].height; row++) {
817 for (col = this->rooms[room].x; col < this->rooms[room].x + this->rooms[room].width; col++) {
818 this->mask[col + row * 20] = 1;
819 }
820 }
821 }
822 }
823 }
824
825 this->mask[cell_x + cell_z * 20] = 1;
826}
827
829 int i;
830
831 // Hide every cell of the mini map and forget every room that the player found.
832 for (i = 0; i < 400; i++) {
833 this->mask[i] = 0;
834 }
835
836 for (i = 0; i < 16; i++) {
837 this->room_seen[i] = 0;
838 }
839
840 // The entrance should always show on the mini map.
841 for (i = 0; i < 400; i++) {
842 if (this->cells[i].parts_no == MAP_PARTS_STAIR_DOWN) {
843 this->mask[i] = 1;
844 }
845 }
846}
847
852 float fire_pos[4];
853 float part_pos[4];
854 float cam_pos[4];
855 float best_pos[4];
856 float frame_pos[4];
857 float vol;
858 float pan;
859 float near_dist;
860 int j;
861 int i;
862
863 sceVu0CopyVector(frame_pos, frame->position);
864 ((CCamera *) camera)->GetPos(cam_pos);
865 sceVu0ScaleVectorXYZ(cam_pos, cam_pos, 0.1f);
866
867 this->fire.SetTexture(TexManager.GetTexture("lightling", -1), TexManager.GetTexture("blender", -1));
868 this->fire.FireCreate();
869
870 near_dist = 16000.0f;
871
872 for (i = 0; this->parts[i].frame[0] != NULL; i++) {
873 sceVu0CopyVector(part_pos, this->parts[i].frame_offset[0]);
874
875 for (j = 0; j < this->parts[i].fire_num; j++) {
876 float angle;
877 float turn;
878 float rot_x;
879 float rot_z;
880 float dist;
881 float world_z;
882 float world_y;
883 int param;
884
885 fire_pos[0] = this->parts[i].fire_pos[j][0];
886 fire_pos[1] = this->parts[i].fire_pos[j][1];
887 fire_pos[2] = this->parts[i].fire_pos[j][2];
888 fire_pos[3] = 1.0f;
889 param = this->parts[i].fire_param[j];
890
891 // The point turns with the part that holds it.
892 angle = this->parts[i].frame_turn[0];
893
894 if (angle != 0.0f) {
895 angle = (PI * (90.0f * (4.0f - angle))) / 180.0f;
896 turn = -fire_pos[2] * sinf(angle);
897 rot_x = turn - fire_pos[0] * cosf(angle);
898 turn = -fire_pos[0] * sinf(angle);
899 rot_z = turn + fire_pos[2] * cosf(angle);
900 rot_x *= -1.0f;
901 } else {
902 rot_x = fire_pos[0];
903 rot_z = fire_pos[2];
904 }
905
906 fire_pos[0] = part_pos[0] / 10.0f + rot_x;
907 fire_pos[1] += part_pos[1] / 10.0f;
908 fire_pos[2] = world_z = part_pos[2] / 10.0f + rot_z;
909 world_y = fire_pos[1];
910
911 this->fire.pos[0] = 10.0f * fire_pos[0];
912 this->fire.pos[1] = 10.0f * world_y;
913 this->fire.pos[2] = 10.0f * world_z;
914 this->fire.pos[3] = 1.0f;
915 this->fire.DrawFire(1, 1, (CCamera *) camera, frame_pos, 1.0f, param, 15.0f);
916
917 // The crackle comes from the fire that is nearest to the camera.
918 dist = DistVector(cam_pos, fire_pos);
919
920 if (!(near_dist < dist)) {
921 near_dist = dist;
922 sceVu0CopyVector(best_pos, fire_pos);
923 sceVu0ScaleVectorXYZ(best_pos, best_pos, 10.0f);
924 }
925 }
926 }
927
928 if (near_dist <= 30.0f) {
929 SndGetVolPan(&vol, &pan, best_pos, 10.0f, 500.0f);
930 SndSetSeVolf(SE_AMBIENT_CAVE_DRIPS, vol, 0);
931 SndSetSePanf(53, pan, 0);
932 } else {
933 SndSetSeVol(53, 0, 0);
934 }
935}
936
938 float fire_pos[4];
939 float frame_pos[4];
940 float cam_near[4];
941 float cam_pos[4];
942 float best_pos[4];
943 float frame_near[4];
944 float vol;
945 float pan;
946 float near_dist;
947 int cam_col;
948 int cam_row;
949 int j;
950 int col;
951 int row;
952 int param;
953
954 sceVu0CopyVector(frame_pos, frame->position);
955 ((CCamera *) camera)->GetPos(cam_pos);
956 ((CCamera *) camera)->GetPos(cam_near);
957 sceVu0ScaleVectorXYZ(cam_near, cam_near, 0.1f);
958
959 this->fire.SetTexture(TexManager.GetTexture("lightling", -1), TexManager.GetTexture("blender", -1));
960 this->fire.FireCreate();
961
962 sceVu0CopyVector(frame_near, frame->position);
963 frame_near[0] /= 10.0f;
964 frame_near[1] /= 10.0f;
965 frame_near[2] /= 10.0f;
966
967 cam_col = (int) (cam_pos[0] / 160.0f);
968 cam_row = (int) (cam_pos[2] / 160.0f);
969 near_dist = 16000.0f;
970
971 // Only the cells around the camera show their fires.
972 for (row = cam_row - 5; row < cam_row + 5; row++) {
973 for (col = cam_col - 5; col < cam_col + 5; col++) {
974 if (row >= 0 && row < 20 && col >= 0 && col < 20) {
975 int cell_no = col + row * 20;
976
977 if (this->cells[cell_no].parts_no != MAP_PARTS_NONE && this->cells[cell_no].camera_dist <= 160.0f * (3.0f + this->draw_dist_scale)) {
978 CDungeonParts *part;
979
980 for (j = 0; j < (part = &this->parts[this->cells[cell_no].parts_no])->fire_num; j++) {
981 float turn;
982 float rot_x;
983 float rot_z;
984 float height;
985 float angle;
986 float dist;
987
988 fire_pos[0] = part->fire_pos[j][0];
989 fire_pos[1] = part->fire_pos[j][1];
990 fire_pos[2] = part->fire_pos[j][2];
991 fire_pos[3] = 1.0f;
992 param = part->fire_param[j];
993
994 // The point turns with the part that holds it.
995 angle = (PI * (float) ((4 - this->cells[cell_no].direction) * 90)) / 180.0f;
996
997 if (this->cells[cell_no].direction != 0) {
998 turn = -fire_pos[2] * sinf(angle);
999 rot_x = turn - fire_pos[0] * cosf(angle);
1000 turn = -fire_pos[0] * sinf(angle);
1001 rot_z = turn + fire_pos[2] * cosf(angle);
1002 rot_x *= -1.0f;
1003 } else {
1004 rot_x = fire_pos[0];
1005 rot_z = fire_pos[2];
1006 }
1007
1008 fire_pos[0] = rot_x + 16.0f * (float) col;
1009 rot_z += 16.0f * (float) row;
1010 fire_pos[2] = rot_z;
1011 height = fire_pos[1];
1012
1013 this->fire.pos[0] = 10.0f * fire_pos[0];
1014 this->fire.pos[1] = 10.0f * height;
1015 this->fire.pos[2] = 10.0f * rot_z;
1016 this->fire.pos[3] = 1.0f;
1017 this->fire.DrawFire(1, 1, (CCamera *) camera, frame_pos, 1.0f, param, 15.0f);
1018
1019 // The crackle comes from the fire that is nearest to the camera.
1020 dist = DistVector(cam_near, fire_pos);
1021
1022 if (!(near_dist < dist)) {
1023 near_dist = dist;
1024 sceVu0CopyVector(best_pos, fire_pos);
1025 sceVu0ScaleVectorXYZ(best_pos, best_pos, 10.0f);
1026 }
1027 }
1028 }
1029 }
1030 }
1031 }
1032
1033 if (near_dist <= 30.0f) {
1034 SndGetVolPan(&vol, &pan, best_pos, 10.0f, 500.0f);
1035 SndSetSeVolf(SE_AMBIENT_CAVE_DRIPS, vol, 0);
1036 SndSetSePanf(53, pan, 0);
1037 } else {
1038 SndSetSeVol(53, 0, 0);
1039 }
1040}
1041
1042/* 189 of 202 instructions. Retail's loop test forms `&parts[parts_no]` and the
1043 * body reads the fire positions at 0x20 from it; mwcc carries only the scaled
1044 * index across the test and folds the 0x490 into the load displacement. A
1045 * `CDungeonParts *` local recovers the 0x20 offsets but forms the base inside
1046 * the body, which trades three differences for four. */
1048 float pos[4];
1049 int i;
1050 int col;
1051 int row;
1052 int cell_z;
1053 int cell_x;
1054
1055 sceVu0CopyVector(pos, frame->position);
1056 cell_x = (int) (pos[0] / 160.0f);
1057 cell_z = (int) (pos[2] / 160.0f);
1058
1059 // Only the cells around the player cast a raster.
1060 for (row = cell_z - 4; row < cell_z + 4; row++) {
1061 for (col = cell_x - 4; col < cell_x + 4; col++) {
1062 if (row >= 0 && row < 20 && col >= 0 && col < 20) {
1063 int cell_no = col + row * 20;
1064
1065 if (this->cells[cell_no].parts_no != MAP_PARTS_NONE && this->cells[cell_no].camera_dist <= 240.0f && this->cells[cell_no].visible == 1) {
1066 CDungeonParts *part;
1067
1068 for (i = 0; i < (part = &this->parts[this->cells[cell_no].parts_no])->fire_num; i++) {
1069 float x;
1070 float y;
1071 float z;
1072 float rot_x;
1073 float rot_z;
1074 float angle;
1075 int dir;
1076
1077 x = part->fire_pos[i][0];
1078 y = part->fire_pos[i][1];
1079 z = part->fire_pos[i][2];
1080 // The point turns with the part that holds it.
1081 dir = this->cells[cell_no].direction;
1082 angle = (PI * (float) ((4 - dir) * 90)) / 180.0f;
1083
1084 if (dir != 0) {
1085 rot_x = -z * sinf(angle) - x * cosf(angle);
1086 rot_z = -x * sinf(angle) + z * cosf(angle);
1087 rot_x *= -1.0f;
1088 } else {
1089 rot_x = x;
1090 rot_z = z;
1091 }
1092
1093 x = rot_x + 16.0f * (float) col;
1094 z = rot_z + 16.0f * (float) row;
1095
1096 this->fire.pos[0] = 10.0f * x;
1097 this->fire.pos[1] = 10.0f * y;
1098 this->fire.pos[2] = 10.0f * z;
1099 this->fire.pos[3] = 1.0f;
1100 this->fire.DrawRaster();
1101 }
1102 }
1103 }
1104 }
1105 }
1106}
1107
1108void CDungeonMap::DrawWater(float *pos, int mute) {
1109 static int wait;
1110 static char init;
1111
1112 float vec[4];
1113 float best_pos[4];
1114 float vol;
1115 float pan;
1116 float near_dist;
1117 float sound_dist;
1118 int shake;
1119 int col;
1120 int row;
1121 PARTS_WATER *best;
1123 CDungeonParts *part;
1124 int best_col;
1125 int best_row;
1126
1127 best = NULL;
1128 near_dist = 320.0f;
1129 sound_dist = 300.0f;
1130 shake = false;
1131
1132 // The surface only shakes once every thirty frames.
1133 if (init == 0) {
1134 wait = 0;
1135 init = 1;
1136 }
1137
1138 if (wait >= 30) {
1139 wait = 0;
1140 shake = true;
1141 } else {
1142 wait++;
1143 }
1144
1145 // One surface draws for the whole floor: the one nearest to the player.
1146 for (row = 0; row < 16; row++) {
1147 for (col = 0; col < 16; col++) {
1148 int cell_no = col + row * 20;
1149 float dist;
1150
1151 if (this->cells[cell_no].parts_no == MAP_PARTS_NONE) {
1152 continue;
1153 }
1154
1155 part = &this->parts[this->cells[cell_no].parts_no];
1156 water = &part->water;
1157
1158 if (this->parts[this->cells[cell_no].parts_no].water.used == 0) {
1159 continue;
1160 }
1161
1162 vec[0] = 160.0f * (float) col;
1163 vec[1] = water->vertex[0][1];
1164 vec[2] = 160.0f * (float) row;
1165 dist = DistVector(pos, vec);
1166
1167 if (dist < near_dist) {
1168 best = water;
1169 near_dist = dist;
1170 best_pos[0] = vec[0];
1171 best_pos[1] = vec[1];
1172 best_pos[2] = vec[2];
1173 best_col = col;
1174 best_row = row;
1175 }
1176
1177 if (sound_dist >= dist) {
1178 sound_dist = dist;
1179 }
1180 }
1181 }
1182
1183 if (best != NULL) {
1184 this->water.SetVertex(best->vertex[0], best->vertex[1], best->vertex[2], best->vertex[3]);
1185 this->water.frame.SetPosition(best_pos);
1186 u8 red = best->red;
1187 u8 green = best->green;
1188 u8 blue = best->blue;
1189
1190 this->water.SetColor(red, green, blue, 0x80);
1191
1192 if (shake != 0) {
1193 if (selectMapNo == DUNGEON_DIVINE_BEAST_CAVE && selectMapNo == DUNGEON_DEMON_SHAFT) {
1194 float height = 1.0f;
1195
1196 this->water.SetParam(0.12f, 0.018f, height, 11.0f);
1197 } else {
1198 float speed = 11.0f;
1199
1200 this->water.SetParam(0.12f, 0.018f, 0.0f, speed);
1201 }
1202
1203 this->water.Shake(16, 4, 2.15f);
1204 }
1205
1206 this->water.Hamon();
1207 DrawVu1__6CWaterFP10RenderInfoP13sceVif1PacketP1(&this->water, &mgRenderInfo, Vif1Packet, NULL);
1208 }
1209
1210 if (best != NULL) {
1211
1212 // A waterfall stands at the cell that the surface came from.
1213 if (best->has_fall != 0 && this->fall_model != NULL) {
1214 float x = 160.0f * (float) best_col;
1215 float z = 160.0f * (float) best_row;
1216
1217 this->fall_model->SetPosition(x, 0.0f, z);
1218 MGDraw(this->fall_model);
1219 }
1220
1221 if (sound_dist < 300.0f) {
1222 SndGetVolPan(&vol, &pan, best_pos, 50.0f, 300.0f);
1223
1224 if (mute != 0) {
1225 vol = 0.0f;
1226 }
1227
1228 SndSetSeVolf(SE_AMBIENT_DUNGEON_HISS, vol, 0);
1229 SndSetSePanf(61, pan, 0);
1230 } else {
1231 SndSetSeVol(61, 0, 0);
1232 }
1233 } else {
1234 float vol;
1235 float pan;
1236
1237 if (sound_dist < 300.0f) {
1238 SndGetVolPan(&vol, &pan, best_pos, 50.0f, 300.0f);
1239
1240 if (mute != 0) {
1241 vol = 0.0f;
1242 }
1243
1244 SndSetSeVolf(SE_AMBIENT_TOWN_NOISE, vol, 0);
1245 SndSetSePanf(52, pan, 0);
1246 } else {
1247 SndSetSeVol(52, 0, 0);
1248 }
1249 }
1250}
1251
1253 float lid[4];
1254 int i;
1255 float angle;
1256
1257 if (this->box_lid_model == NULL || this->box_body_model == NULL) {
1258 return;
1259 }
1260
1261 if (this->chest_body_model == NULL || this->chest_lid_model == NULL) {
1262 return;
1263 }
1264
1265 for (i = 0; i < 24; i++) {
1266 if (this->boxes[i].used == 0) {
1267 continue;
1268 }
1269
1270 if (!(DistVector(this->boxes[i].pos, pos) <= 160.0f * this->draw_dist_scale)) {
1271 continue;
1272 }
1273
1274 // The lid sits on the box, tilted by however far it has opened.
1275 switch (this->boxes[i].kind) {
1276 case TREASURE_BOX_LARGE:
1277 this->box_body_model->SetPosition(this->boxes[i].pos);
1278 sceVu0CopyVector(lid, this->box_body_model->position);
1279 lid[1] += 8.0f;
1280 lid[2] -= 5.0f;
1281 this->box_lid_model->SetPosition(lid);
1282 angle = this->boxes[i].lid_angle;
1283 this->box_lid_model->SetRotation((PI * angle) / 180.0f, 0.0f, 0.0f);
1284 MGDraw(this->box_body_model);
1285 MGDraw(this->box_lid_model);
1286 break;
1287 case TREASURE_BOX_SMALL:
1288 this->chest_body_model->SetPosition(this->boxes[i].pos);
1289 sceVu0CopyVector(lid, this->chest_body_model->position);
1290 lid[1] += 3.0f;
1291 lid[2] -= 3.0f;
1292 this->chest_lid_model->SetPosition(lid);
1293 angle = this->boxes[i].lid_angle;
1294 this->chest_lid_model->SetRotation((PI * angle) / 180.0f, 0.0f, 0.0f);
1295 MGDraw(this->chest_body_model);
1296 MGDraw(this->chest_lid_model);
1297 break;
1298 }
1299 }
1300}
1301
1303 float draw_pos[4];
1304
1305 if (this->atra_model == NULL) {
1306 return;
1307 }
1308
1309 for (int i = 0; i < this->atra_num; i++) {
1310 if (this->atra[i].used != 0) {
1311 if (DistVector(this->atra[i].pos, pos) <= 160.0f * this->draw_dist_scale) {
1312 sceVu0CopyVector(draw_pos, this->atra[i].pos);
1313
1314 // The ball bobs up and down on the spot.
1315 draw_pos[1] += sinf(this->atra[i].phase);
1316 this->atra_model->SetPosition(draw_pos);
1317 MGDraw(this->atra_model);
1318
1319 if (this->atra[i].phase <= 360.0f) {
1320 this->atra[i].phase += 0.1f;
1321 } else {
1322 this->atra[i].phase = 0.0f;
1323 }
1324 }
1325 }
1326 }
1327}
1328
1330 float pos[4];
1331 CFrame *frame;
1332
1333 sceVu0CopyVector(pos, CharaMain.frame->position);
1334 frame = this->collision_model;
1335
1336 // Retail returns no value here, which leaves whatever v0 happens to hold.
1337 if (frame == NULL) {
1338 return;
1339 }
1340
1341 for (int i = 0; i < this->atra_num; i++) {
1342 if (this->atra[i].used != 0) {
1343 if (DistVector(this->atra[i].pos, pos) <= 400.0f) {
1344 frame->SetPosition(this->atra[i].pos);
1345 num += frame->PickUpNearPoly(&poly[num], box);
1346 }
1347 }
1348 }
1349
1350 return num;
1351}
1352
1354s32 scanRoomInfoWork[256];
1355
1357BUILD_MAP_INFO buildMapDat[400];
1358
1360BUILD_MAP_INFO buildMapWork[400];
1361
1363ROOM_INFO roomStack[16];
1364
1366s32 roomStackCnt;
1367
1368static int buildRoom(int x, int y, int w, int h, int room_no) {
1369 int ok = 1;
1370 int i, j;
1371
1372 // The room needs its own cells and a border of one cell around them, so
1373 // that two rooms never come out sharing a wall.
1374 for (j = -1; j < h + 2; j++) {
1375 for (i = -1; i < w + 2; i++) {
1376 if (i + x >= 0 && i + x < 20 && j + y >= 0 && j + y < 20) {
1377 if (buildMapDat[x + (j + y) * 20 + i].kind != MAP_PARTS_NONE) {
1378 ok = 0;
1379 }
1380 }
1381 }
1382 }
1383
1384 if (!ok) {
1385 return ok;
1386 }
1387
1388 for (j = 0; j < h; j++) {
1389 for (i = 0; i < w; i++) {
1390 buildMapDat[x + (y + j) * 20 + i].kind = MAP_PARTS_ROOM_FLOOR;
1391 buildMapDat[x + (y + j) * 20 + i].link[room_no] = 1;
1392 buildMapDat[x + (y + j) * 20 + i].content |= BUILD_CELL_ROOM;
1393 }
1394 }
1395
1396 return 1;
1397}
1398
1402void mapPartsFilter() {
1403 int col;
1404 int row;
1405 int room_no;
1406 int table_no;
1407 int north;
1408 int south;
1409 int east;
1410 int west;
1411
1412 for (row = 0; row < 20; row++) {
1413 for (col = 0; col < 20; col++) {
1414 // A corridor cell joins every neighbour that shares one of its rooms.
1415 if ((buildMapDat[col + row * 20].content & BUILD_CELL_CORRIDOR) == BUILD_CELL_CORRIDOR) {
1416 int mask = 15;
1417 int north = buildMapDat[col + (row - 1) * 20].content;
1418 int south = buildMapDat[col + (row + 1) * 20].content;
1419 int east = buildMapDat[col + row * 20 + 1].content;
1420 int west = buildMapDat[col + row * 20 - 1].content;
1421
1422 int allow_north = 1;
1423
1424 if ((north & (BUILD_CELL_ROOM | BUILD_CELL_DOOR)) == BUILD_CELL_ROOM) {
1425 allow_north = 0;
1426 }
1427
1428 if ((north & (BUILD_CELL_STAIR_DOWN | BUILD_CELL_STAIR_UP)) != 0 && buildMapDat[col + (row - 1) * 20].direction != 0) {
1429 allow_north = 0;
1430 }
1431
1432 int allow_south = 1;
1433
1434 if ((south & (BUILD_CELL_ROOM | BUILD_CELL_DOOR)) == BUILD_CELL_ROOM) {
1435 allow_south = 0;
1436 }
1437
1438 if ((south & (BUILD_CELL_STAIR_DOWN | BUILD_CELL_STAIR_UP)) != 0 && buildMapDat[col + (row + 1) * 20].direction != 2) {
1439 allow_south = 0;
1440 }
1441
1442 int allow_east = 1;
1443
1444 if ((east & (BUILD_CELL_ROOM | BUILD_CELL_DOOR)) == BUILD_CELL_ROOM) {
1445 allow_east = 0;
1446 }
1447
1448 if ((east & (BUILD_CELL_STAIR_DOWN | BUILD_CELL_STAIR_UP)) != 0 && buildMapDat[col + row * 20 + 1].direction != 1) {
1449 allow_east = 0;
1450 }
1451
1452 int allow_west = 1;
1453
1454 if ((west & (BUILD_CELL_ROOM | BUILD_CELL_DOOR)) == BUILD_CELL_ROOM) {
1455 allow_west = 0;
1456 }
1457
1458 if ((west & (BUILD_CELL_STAIR_DOWN | BUILD_CELL_STAIR_UP)) != 0 && buildMapDat[col + row * 20 - 1].direction != 3) {
1459 allow_west = 0;
1460 }
1461
1462 for (room_no = 0; room_no < 16; room_no++) {
1463 if (buildMapDat[col + row * 20].link[room_no] == 1) {
1464 if (buildMapDat[col + (row - 1) * 20].link[room_no] == 1 && allow_north != 0) {
1465 mask &= ~1;
1466 }
1467
1468 if (buildMapDat[col + (row + 1) * 20].link[room_no] == 1 && allow_south != 0) {
1469 mask &= ~8;
1470 }
1471
1472 if (buildMapDat[col + row * 20 + 1].link[room_no] == 1 && allow_east != 0) {
1473 mask &= ~2;
1474 }
1475
1476 if (buildMapDat[col + row * 20 - 1].link[room_no] == 1 && allow_west != 0) {
1477 mask &= ~4;
1478 }
1479 }
1480 }
1481
1482 for (table_no = 0; table_no < 15; table_no++) {
1483 if (mask == chainTableRoad[table_no][0]) {
1484 buildMapDat[col + row * 20].kind = chainTableRoad[table_no][1];
1485 buildMapDat[col + row * 20].direction = chainTableRoad[table_no][2];
1486 }
1487 }
1488 }
1489
1490 // A room cell joins the neighbouring room cells.
1491 if ((buildMapDat[col + row * 20].content & BUILD_CELL_ROOM) == BUILD_CELL_ROOM) {
1492 int mask = 15;
1493 north = row != 0 ? buildMapDat[col + (row - 1) * 20].content & BUILD_CELL_ROOM : 0;
1494 south = row != 19 ? buildMapDat[col + (row + 1) * 20].content & BUILD_CELL_ROOM : 0;
1495 east = col != 19 ? buildMapDat[col + row * 20 + 1].content & BUILD_CELL_ROOM : 0;
1496 west = col != 0 ? buildMapDat[col + row * 20 - 1].content & BUILD_CELL_ROOM : 0;
1497
1498 if (north == BUILD_CELL_ROOM) {
1499 mask &= ~1;
1500 }
1501
1502 if (south == BUILD_CELL_ROOM) {
1503 mask &= ~8;
1504 }
1505
1506 if (east == BUILD_CELL_ROOM) {
1507 mask &= ~2;
1508 }
1509
1510 if (west == BUILD_CELL_ROOM) {
1511 mask &= ~4;
1512 }
1513
1514 for (table_no = 0; table_no < 8; table_no++) {
1515 if (mask == chainTableRoom[table_no][0]) {
1516 buildMapDat[col + row * 20].kind = chainTableRoom[table_no][1];
1517 buildMapDat[col + row * 20].direction = chainTableRoom[table_no][2];
1518 }
1519 }
1520 }
1521
1522 // A door faces the room cell beside it.
1523 if ((buildMapDat[col + row * 20].content & (BUILD_CELL_DOOR | BUILD_CELL_ROOM_LINK)) == BUILD_CELL_DOOR) {
1524 int mask = 0;
1525
1526 if ((buildMapDat[col + (row - 1) * 20].content & BUILD_CELL_ROOM) == BUILD_CELL_ROOM) {
1527 mask |= 1;
1528 }
1529
1530 if ((buildMapDat[col + (row + 1) * 20].content & BUILD_CELL_ROOM) == BUILD_CELL_ROOM) {
1531 mask |= 8;
1532 }
1533
1534 if ((buildMapDat[col + row * 20 + 1].content & BUILD_CELL_ROOM) == BUILD_CELL_ROOM) {
1535 mask |= 2;
1536 }
1537
1538 if ((buildMapDat[col + row * 20 - 1].content & BUILD_CELL_ROOM) == BUILD_CELL_ROOM) {
1539 mask |= 4;
1540 }
1541
1542 for (table_no = 0; table_no < 4; table_no++) {
1543 if (mask == chainTableDoor[table_no][0]) {
1544 buildMapDat[col + row * 20].kind = chainTableDoor[table_no][1];
1545 }
1546 }
1547 }
1548
1549 if ((buildMapDat[col + row * 20].content & BUILD_CELL_HEAL_SPRING) == BUILD_CELL_HEAL_SPRING) {
1550 buildMapDat[col + row * 20].kind = MAP_PARTS_HEAL_SPRING_NORTH;
1551 buildMapDat[col + row * 20].direction = 0;
1552 }
1553
1554 if ((buildMapDat[col + row * 20].content & BUILD_CELL_PILLAR) == BUILD_CELL_PILLAR) {
1555 if ((100.0f * (float) rand()) / 2147483648.0f <= 50.0f) {
1556 buildMapDat[col + row * 20].kind = MAP_PARTS_PILLAR_1;
1557 } else {
1558 buildMapDat[col + row * 20].kind = MAP_PARTS_PILLAR_2;
1559 }
1560
1561 buildMapDat[col + row * 20].direction = 0;
1562 }
1563
1564 // A dividing wall joins the walls and rooms around it.
1565 if ((buildMapDat[col + row * 20].content & BUILD_CELL_DIVIDING_WALL) == BUILD_CELL_DIVIDING_WALL) {
1566 int mask = 0;
1567
1568 north = south = east = west = 0;
1569
1570 if (row != 0) {
1571 north = buildMapDat[col + (row - 1) * 20].content;
1572 }
1573
1574 if (row != 19) {
1575 south = buildMapDat[col + (row + 1) * 20].content;
1576 }
1577
1578 if (col != 19) {
1579 east = buildMapDat[col + row * 20 + 1].content;
1580 }
1581
1582 if (col != 0) {
1583 west = buildMapDat[col + row * 20 - 1].content;
1584 }
1585
1586 if ((north & BUILD_CELL_DIVIDING_WALL) == BUILD_CELL_DIVIDING_WALL) {
1587 mask |= 1;
1588 }
1589
1590 if ((south & BUILD_CELL_DIVIDING_WALL) == BUILD_CELL_DIVIDING_WALL) {
1591 mask |= 8;
1592 }
1593
1594 if ((east & BUILD_CELL_DIVIDING_WALL) == BUILD_CELL_DIVIDING_WALL) {
1595 mask |= 2;
1596 }
1597
1598 if ((west & BUILD_CELL_DIVIDING_WALL) == BUILD_CELL_DIVIDING_WALL) {
1599 mask |= 4;
1600 }
1601
1602 if ((north & BUILD_CELL_ROOM) == BUILD_CELL_ROOM) {
1603 mask |= 0x10;
1604 }
1605
1606 if ((south & BUILD_CELL_ROOM) == BUILD_CELL_ROOM) {
1607 mask |= 0x80;
1608 }
1609
1610 if ((east & BUILD_CELL_ROOM) == BUILD_CELL_ROOM) {
1611 mask |= 0x20;
1612 }
1613
1614 if ((west & BUILD_CELL_ROOM) == BUILD_CELL_ROOM) {
1615 mask |= 0x40;
1616 }
1617
1618 for (table_no = 0; table_no < 6; table_no++) {
1619 if (mask == chainTableDivid[table_no][0]) {
1620 buildMapDat[col + row * 20].kind = chainTableDivid[table_no][1];
1621 }
1622 }
1623 }
1624
1625 if ((buildMapDat[col + row * 20].content & BUILD_CELL_DIVIDING_DOOR) == BUILD_CELL_DIVIDING_DOOR) {
1626 int mask = 0;
1627
1628 north = south = east = west = 0;
1629
1630 if (row != 0) {
1631 north = buildMapDat[col + (row - 1) * 20].content;
1632 }
1633
1634 if (row != 19) {
1635 south = buildMapDat[col + (row + 1) * 20].content;
1636 }
1637
1638 if (col != 19) {
1639 east = buildMapDat[col + row * 20 + 1].content;
1640 }
1641
1642 if (col != 0) {
1643 west = buildMapDat[col + row * 20 - 1].content;
1644 }
1645
1646 if ((north & BUILD_CELL_DIVIDING_WALL) == BUILD_CELL_DIVIDING_WALL) {
1647 mask |= 1;
1648 }
1649
1650 if ((south & BUILD_CELL_DIVIDING_WALL) == BUILD_CELL_DIVIDING_WALL) {
1651 mask |= 8;
1652 }
1653
1654 if ((east & BUILD_CELL_DIVIDING_WALL) == BUILD_CELL_DIVIDING_WALL) {
1655 mask |= 2;
1656 }
1657
1658 if ((west & BUILD_CELL_DIVIDING_WALL) == BUILD_CELL_DIVIDING_WALL) {
1659 mask |= 4;
1660 }
1661
1662 for (table_no = 0; table_no < 2; table_no++) {
1663 if (mask == chainTableDividDoor[table_no][0]) {
1664 buildMapDat[col + row * 20].kind = chainTableDividDoor[table_no][1];
1665 }
1666 }
1667 }
1668
1669 if ((buildMapDat[col + row * 20].content & BUILD_CELL_STAIR_DOWN) == BUILD_CELL_STAIR_DOWN) {
1670 buildMapDat[col + row * 20].kind = MAP_PARTS_STAIR_DOWN;
1671 }
1672
1673 if ((buildMapDat[col + row * 20].content & BUILD_CELL_STAIR_UP) == BUILD_CELL_STAIR_UP) {
1674 buildMapDat[col + row * 20].kind = MAP_PARTS_STAIR_UP;
1675 }
1676
1677 if ((buildMapDat[col + row * 20].content & BUILD_CELL_URA_ENTRANCE) == BUILD_CELL_URA_ENTRANCE) {
1678 buildMapDat[col + row * 20].kind = MAP_PARTS_URA_ENTRANCE_NORTH;
1679 }
1680 }
1681 }
1682}
1683
1684static void copyMapInfo(BUILD_MAP_INFO *dst, BUILD_MAP_INFO *src) {
1685 for (int i = 0; i < 400; i++) {
1686 dst[i].kind = src[i].kind;
1687
1688 for (int j = 0; j < 16; j++) {
1689 dst[i].link[j] = src[i].link[j];
1690 }
1691
1692 dst[i].content = src[i].content;
1693 }
1694}
1695
1703int joinRoom(int from_room, int to_room) {
1704 int done;
1705 int target_x;
1706 int target_y;
1707 int dir;
1708 int distance;
1709 int step;
1710 int i;
1711 int cx;
1712 int cy;
1713 int steps;
1714 register int dx;
1715 register int dy;
1716
1717 copyMapInfo(buildMapWork, buildMapDat);
1718
1719 // The corridor leaves the room by whichever side faces the other room.
1720 dx = roomStack[from_room].x - roomStack[to_room].x;
1721 dy = roomStack[from_room].y - roomStack[to_room].y;
1722
1723 if (dx * dx > dy * dy) {
1724 if (dx < 0) {
1725 dir = MAP_DIR_EAST;
1726 } else {
1727 dir = MAP_DIR_WEST;
1728 }
1729 } else {
1730 if (dy < 0) {
1731 dir = MAP_DIR_SOUTH;
1732 } else {
1733 dir = MAP_DIR_NORTH;
1734 }
1735 }
1736
1737 switch (dir) {
1738 case MAP_DIR_NORTH:
1739 cx = roomStack[from_room].x + (roomStack[from_room].width >> 1);
1740 cy = roomStack[from_room].y - 1;
1741 buildMapWork[cx + (cy + 1) * 20].content |= BUILD_CELL_DOOR;
1742 buildMapWork[cx + (cy + 1) * 20].link[from_room] = 1;
1743 steps = roomStack[from_room].y - (roomStack[to_room].y + roomStack[to_room].height);
1744 break;
1745 case MAP_DIR_SOUTH:
1746 cx = roomStack[from_room].x + (roomStack[from_room].width >> 1);
1747 buildMapWork[cx + (roomStack[from_room].y + roomStack[from_room].height - 1) * 20].content |= BUILD_CELL_DOOR;
1748 buildMapWork[cx + (roomStack[from_room].y + roomStack[from_room].height - 1) * 20].link[from_room] = 1;
1749 cy = roomStack[from_room].y + roomStack[from_room].height;
1750 steps = roomStack[to_room].y - cy;
1751 break;
1752 case MAP_DIR_EAST:
1753 cx = roomStack[from_room].x + roomStack[from_room].width;
1754 cy = roomStack[from_room].y + (roomStack[from_room].height >> 1);
1755 buildMapWork[cx + cy * 20 - 1].content |= BUILD_CELL_DOOR;
1756 buildMapWork[cx + cy * 20 - 1].link[from_room] = 1;
1757 steps = roomStack[to_room].x - (roomStack[from_room].x + roomStack[from_room].width);
1758 break;
1759 case MAP_DIR_WEST:
1760 cx = roomStack[from_room].x - 1;
1761 cy = roomStack[from_room].y + (roomStack[from_room].height >> 1);
1762 buildMapWork[cx + cy * 20 + 1].content |= BUILD_CELL_DOOR;
1763 buildMapWork[cx + cy * 20 + 1].link[from_room] = 1;
1764 steps = roomStack[from_room].x - (roomStack[to_room].x + roomStack[to_room].width);
1765 break;
1766 }
1767
1768 target_x = roomStack[to_room].x + (roomStack[to_room].width >> 1);
1769 target_y = roomStack[to_room].y + (roomStack[to_room].height >> 1);
1770 steps = (steps >> 1) + 1;
1771
1772 // Two rooms that share a wall still need a cell between them.
1773 if (roomStack[from_room].y == roomStack[to_room].y + roomStack[to_room].height) {
1774 if (dir == MAP_DIR_NORTH || dir == MAP_DIR_SOUTH) {
1775 if (steps < 2) {
1776 steps = 2;
1777 }
1778 }
1779 }
1780
1781 if (roomStack[to_room].y == roomStack[from_room].y + roomStack[from_room].height) {
1782 if (dir == MAP_DIR_NORTH || dir == MAP_DIR_SOUTH) {
1783 if (steps < 2) {
1784 steps = 2;
1785 }
1786 }
1787 }
1788
1789 done = 0;
1790
1791 while (done == 0) {
1792 if (steps != 0) {
1793 if (buildMapWork[cx + cy * 20].kind == MAP_PARTS_NONE) {
1794 buildMapWork[cx + cy * 20].kind = from_room;
1795 buildMapWork[cx + cy * 20].link[from_room] = 1;
1796 buildMapWork[cx + cy * 20].content |= BUILD_CELL_CORRIDOR;
1797 steps--;
1798
1799 if (steps != 0) {
1800 switch (dir) {
1801 case MAP_DIR_NORTH:
1802 cy--;
1803 break;
1804 case MAP_DIR_SOUTH:
1805 cy++;
1806 break;
1807 case MAP_DIR_EAST:
1808 cx++;
1809 break;
1810 case MAP_DIR_WEST:
1811 cx--;
1812 break;
1813 }
1814 }
1815 } else {
1816 if ((buildMapWork[cx + cy * 20].content & BUILD_CELL_ROOM) == BUILD_CELL_ROOM) {
1817 buildMapWork[cx + cy * 20].content |= BUILD_CELL_DOOR;
1818 buildMapWork[cx + cy * 20].link[from_room] = 1;
1819 } else {
1820 buildMapWork[cx + cy * 20].link[from_room] = 1;
1821 }
1822
1823 done = 1;
1824 }
1825 } else {
1826
1827 // The corridor has come far enough out; turn and run at the room.
1828 switch (dir) {
1829 case MAP_DIR_NORTH:
1830 case MAP_DIR_SOUTH:
1831 distance = target_x - cx;
1832
1833 if (distance < 0) {
1834 distance = -distance;
1835 step = -1;
1836 } else {
1837 step = 1;
1838 }
1839
1840 for (i = 0; i < distance; i++) {
1841 cx += step;
1842
1843 if (buildMapWork[cx + cy * 20].kind == MAP_PARTS_NONE) {
1844 buildMapWork[cx + cy * 20].kind = from_room;
1845 buildMapWork[cx + cy * 20].link[from_room] = 1;
1846 buildMapWork[cx + cy * 20].content |= BUILD_CELL_CORRIDOR;
1847 } else {
1848 if ((buildMapWork[cx + cy * 20].content & BUILD_CELL_ROOM) == BUILD_CELL_ROOM) {
1849 buildMapWork[cx + cy * 20].content |= BUILD_CELL_DOOR;
1850 buildMapWork[cx + cy * 20].link[from_room] = 1;
1851 } else {
1852 buildMapWork[cx + cy * 20].link[from_room] = 1;
1853 }
1854
1855 done = 1;
1856 }
1857 }
1858
1859 break;
1860 case MAP_DIR_EAST:
1861 case MAP_DIR_WEST:
1862 distance = target_y - cy;
1863
1864 if (distance < 0) {
1865 distance = -distance;
1866 step = -1;
1867 } else {
1868 step = 1;
1869 }
1870
1871 for (i = 0; i < distance; i++) {
1872 cy += step;
1873
1874 if (buildMapWork[cx + cy * 20].kind == MAP_PARTS_NONE) {
1875 buildMapWork[cx + cy * 20].kind = from_room;
1876 buildMapWork[cx + cy * 20].link[from_room] = 1;
1877 buildMapWork[cx + cy * 20].content |= BUILD_CELL_CORRIDOR;
1878 } else {
1879 if ((buildMapWork[cx + cy * 20].content & BUILD_CELL_ROOM) == BUILD_CELL_ROOM) {
1880 buildMapWork[cx + cy * 20].content |= BUILD_CELL_DOOR;
1881 buildMapWork[cx + cy * 20].link[from_room] = 1;
1882 } else {
1883 buildMapWork[cx + cy * 20].link[from_room] = 1;
1884 }
1885
1886 done = 1;
1887 }
1888 }
1889
1890 break;
1891 }
1892
1893 switch (dir) {
1894 case MAP_DIR_NORTH:
1895 cy--;
1896 break;
1897 case MAP_DIR_SOUTH:
1898 cy++;
1899 break;
1900 case MAP_DIR_EAST:
1901 cx++;
1902 break;
1903 case MAP_DIR_WEST:
1904 cx--;
1905 break;
1906 }
1907
1908 steps = 999;
1909 }
1910 }
1911
1912 copyMapInfo(buildMapDat, buildMapWork);
1913 return 1;
1914}
1915
1916static void setRoomObject() {
1917 int i;
1918 int x;
1919 int y;
1920 int found;
1921 int found_x;
1922 int found_y;
1923 int num;
1924
1925 num = 0;
1926
1927 for (i = 0; i < roomStackCnt; i++) {
1928
1929 // Only two rooms on the floor take an object against their top wall.
1930 if (num < 2) {
1931 found = 0;
1932
1933 for (x = 1; x < roomStack[i].width - 1; x++) {
1934 if ((buildMapDat[roomStack[i].x + (x + roomStack[i].y * 20)].content & (BUILD_CELL_ROOM | BUILD_CELL_DOOR)) == BUILD_CELL_ROOM) {
1935 found_x = x + roomStack[i].x;
1936 found_y = roomStack[i].y;
1937 found = 1;
1938 }
1939 }
1940
1941 if ((100.0f * (float) rand()) / 2147483648.0f <= 30.0f) {
1942 if (found == 1) {
1943 buildMapDat[found_x + found_y * 20].content |= BUILD_CELL_HEAL_SPRING;
1944 num++;
1945 }
1946 }
1947 }
1948
1949 // Every room can take an object anywhere inside it.
1950 found = 0;
1951
1952 for (y = 1; y < roomStack[i].height - 1; y++) {
1953 for (x = 1; x < roomStack[i].width - 1; x++) {
1954 if ((buildMapDat[roomStack[i].x + (x + (y + roomStack[i].y) * 20)].content & (BUILD_CELL_ROOM | BUILD_CELL_DOOR | BUILD_CELL_HEAL_SPRING | BUILD_CELL_PILLAR)) == BUILD_CELL_ROOM) {
1955 found_x = x + roomStack[i].x;
1956 found_y = y + roomStack[i].y;
1957 found = 1;
1958 }
1959 }
1960 }
1961
1962 if ((100.0f * (float) rand()) / 2147483648.0f <= 30.0f) {
1963 if (found == 1) {
1964 buildMapDat[found_x + found_y * 20].content |= BUILD_CELL_PILLAR;
1965 }
1966 }
1967 }
1968}
1969
1970static void setRoomDivid() {
1971 int line_x[64];
1972 int line_y[64];
1973 int line_len[64];
1974 int line_dir[64];
1975 int i;
1976 int line_num;
1977 int ok;
1978 int x;
1979 int y;
1980 int line_no;
1981
1982 for (i = 0; i < roomStackCnt; i++) {
1983 line_num = 0;
1984
1985 // A room can be divided along any row that is free from end to end.
1986 for (y = 1; y < roomStack[i].height - 1; y++) {
1987 ok = 1;
1988
1989 for (x = 0; x < roomStack[i].width; x++) {
1990 if ((buildMapDat[roomStack[i].x + (x + (y + roomStack[i].y) * 20)].content & (BUILD_CELL_ROOM | BUILD_CELL_DOOR | BUILD_CELL_HEAL_SPRING | BUILD_CELL_PILLAR)) != BUILD_CELL_ROOM) {
1991 ok = 0;
1992 }
1993 }
1994
1995 if (ok == 1) {
1996 line_x[line_num] = roomStack[i].x;
1997 line_y[line_num] = y + roomStack[i].y;
1998 line_len[line_num] = roomStack[i].width;
1999 line_dir[line_num] = 0;
2000 line_num++;
2001 }
2002 }
2003
2004 // ...or along any such column.
2005 for (x = 1; x < roomStack[i].width - 1; x++) {
2006 ok = 1;
2007
2008 for (y = 0; y < roomStack[i].height; y++) {
2009 if ((buildMapDat[roomStack[i].x + (x + (y + roomStack[i].y) * 20)].content & (BUILD_CELL_ROOM | BUILD_CELL_DOOR | BUILD_CELL_HEAL_SPRING | BUILD_CELL_PILLAR)) != BUILD_CELL_ROOM) {
2010 ok = 0;
2011 }
2012 }
2013
2014 if (ok == 1) {
2015 line_x[line_num] = x + roomStack[i].x;
2016 line_y[line_num] = roomStack[i].y;
2017 line_len[line_num] = roomStack[i].height;
2018 line_dir[line_num] = 1;
2019 line_num++;
2020 }
2021 }
2022
2023 if (line_num == 0) {
2024 continue;
2025 }
2026
2027 if (!((100.0f * (float) rand()) / 2147483648.0f <= 40.0f)) {
2028 continue;
2029 }
2030
2031 // The wall runs the whole length of the line, with one gap in it.
2032 line_no = (int) (((float) line_num * (float) rand()) / 2147483648.0f);
2033
2034 if (line_dir[line_no] == 0) {
2035 for (x = line_x[line_no]; x < line_x[line_no] + line_len[line_no]; x++) {
2036 buildMapDat[x + line_y[line_no] * 20].content |= BUILD_CELL_DIVIDING_WALL;
2037 }
2038
2039 buildMapDat[line_x[line_no] + line_y[line_no] * 20 + (int) (((float) (line_len[line_no] - 2) * (float) rand()) / 2147483648.0f) + 1]
2040 .content |= BUILD_CELL_DIVIDING_DOOR;
2041 } else {
2042 for (y = line_y[line_no]; y < line_y[line_no] + line_len[line_no]; y++) {
2043 buildMapDat[y * 20 + line_x[line_no]].content |= BUILD_CELL_DIVIDING_WALL;
2044 }
2045
2046 buildMapDat[line_x[line_no] + (line_y[line_no] + (int) (((float) (line_len[line_no] - 2) * (float) rand()) / 2147483648.0f) + 1) * 20]
2047 .content |= BUILD_CELL_DIVIDING_DOOR;
2048 }
2049 }
2050}
2051
2052static void setUnderDungeonStart() {
2053 int safe[120];
2054 int safe_num;
2055 int i;
2056 int x;
2057 int y;
2058 int searching;
2059 int tries;
2060 int pick;
2061
2062 safe_num = 0;
2063
2064 for (i = 0; i < 120; i++) {
2065 safe[i] = -1;
2066 }
2067
2068 // A room cell with no door and no dividing wall can hold the way down.
2069 for (y = 0; y < 20; y++) {
2070 for (x = 0; x < 20; x++) {
2071 if ((buildMapDat[x + y * 20].content & (BUILD_CELL_ROOM | BUILD_CELL_DOOR | BUILD_CELL_DIVIDING_WALL)) == BUILD_CELL_ROOM && safe_num < 60) {
2072 safe[safe_num * 2] = x;
2073 safe[safe_num * 2 + 1] = y;
2074 safe_num++;
2075 }
2076 }
2077 }
2078
2079 // Keep picking cells until one comes out with an empty cell above it and a
2080 // door-free room cell on both sides.
2081 searching = 1;
2082 tries = 0;
2083
2084 while (searching) {
2085 pick = (int) (((float) safe_num * (float) rand()) / 2147483648.0f);
2086 printf("[s]%d\n", pick);
2087 x = safe[pick * 2];
2088 y = safe[pick * 2 + 1];
2089
2090 if (buildMapDat[x + (y - 1) * 20].content == 0) {
2091 if ((buildMapDat[x + y * 20 + 1].content & (BUILD_CELL_ROOM | BUILD_CELL_DOOR)) == BUILD_CELL_ROOM) {
2092 if ((buildMapDat[x + y * 20 - 1].content & (BUILD_CELL_ROOM | BUILD_CELL_DOOR)) == BUILD_CELL_ROOM) {
2093 buildMapDat[x + y * 20].content = BUILD_CELL_ROOM | BUILD_CELL_URA_ENTRANCE;
2094 buildMapDat[x + y * 20].kind = MAP_PARTS_ROOM_GATE_NORTH;
2095 buildMapDat[x + y * 20].direction = 0;
2096 searching = 0;
2097 }
2098 }
2099 }
2100
2101 tries++;
2102
2103 if (tries >= 5000) {
2104 searching = 0;
2105 }
2106 }
2107
2108 if (searching == 0) {
2109 printf("safeNum = %d, escapeCnt = %d\n", safe_num, tries);
2110 }
2111}
2112
2113static void setStair() {
2114 int safe[120];
2115 int safe_num;
2116 int i;
2117 int x;
2118 int y;
2119 int pick;
2120
2121 // The stair down stands above a corridor that has a free cell over it.
2122 safe_num = 0;
2123
2124 for (i = 0; i < 120; i++) {
2125 safe[i] = -1;
2126 }
2127
2128 for (y = 0; y < 20; y++) {
2129 for (x = 0; x < 20; x++) {
2130 if ((buildMapDat[x + y * 20].content & BUILD_CELL_CORRIDOR) == BUILD_CELL_CORRIDOR) {
2131 if (buildMapDat[x + (y - 1) * 20].content == 0) {
2132 safe[safe_num * 2] = x;
2133 safe[safe_num * 2 + 1] = y;
2134 safe_num++;
2135 }
2136 }
2137 }
2138 }
2139
2140 pick = (int) (((float) safe_num * (float) rand()) / 2147483648.0f);
2141
2142 if (safe_num <= 0) {
2143 printf("ERR: DownStair!!\n");
2144 y = 1;
2145 x = 1;
2146 } else {
2147 x = safe[pick * 2];
2148 y = safe[pick * 2 + 1];
2149 }
2150
2151 buildMapDat[x + (y - 1) * 20].content = BUILD_CELL_STAIR_DOWN;
2152 buildMapDat[x + (y - 1) * 20].kind = buildMapDat[x + y * 20].kind;
2153 buildMapDat[x + (y - 1) * 20].link[buildMapDat[x + y * 20].kind] = 1;
2154 buildMapDat[x + (y - 1) * 20].direction = 0;
2155
2156 // The stair up stands below one that has a free cell under it.
2157 safe_num = 0;
2158
2159 for (i = 0; i < 120; i++) {
2160 safe[i] = -1;
2161 }
2162
2163 for (y = 0; y < 20; y++) {
2164 for (x = 0; x < 20; x++) {
2165 if ((buildMapDat[x + y * 20].content & BUILD_CELL_CORRIDOR) == BUILD_CELL_CORRIDOR) {
2166 if (buildMapDat[x + (y + 1) * 20].content == 0) {
2167 safe[safe_num * 2] = x;
2168 safe[safe_num * 2 + 1] = y;
2169 safe_num++;
2170 }
2171 }
2172 }
2173 }
2174
2175 pick = (int) (((float) safe_num * (float) rand()) / 2147483648.0f);
2176
2177 if (safe_num <= 0) {
2178 printf("ERR: UpStair!!\n");
2179 y = 1;
2180 x = 1;
2181 } else {
2182 x = safe[pick * 2];
2183 y = safe[pick * 2 + 1];
2184 }
2185
2186 buildMapDat[x + (y + 1) * 20].content = BUILD_CELL_STAIR_UP;
2187 buildMapDat[x + (y + 1) * 20].kind = buildMapDat[x + y * 20].kind;
2188 buildMapDat[x + (y + 1) * 20].link[buildMapDat[x + y * 20].kind] = 1;
2189 buildMapDat[x + (y + 1) * 20].direction = 2;
2190}
2191
2193 for (int i = 0; i < 3; i++) {
2194 this->trap_circle[i].state = 0;
2195 }
2196}
2197
2198float *CDungeonMap::CheckTrapCircle(float *pos, float dist) {
2199 for (int i = 0; i < 3; i++) {
2200 if (this->trap_circle[i].state != 0) {
2201 if (DistVector(this->trap_circle[i].pos, pos) < dist) {
2202 return this->trap_circle[i].pos;
2203 }
2204 }
2205 }
2206
2207 return NULL;
2208}
2209
2211 for (int i = 0; i < 3; i++) {
2212 bool is_free = !this->trap_circle[i].state;
2213
2214 // Populate the first free slot with a trap circle of a random kind.
2215 if (is_free) {
2216 sceVu0CopyVector(this->trap_circle[i].pos, pos);
2217 this->trap_circle[i].state = 1;
2218 this->trap_circle[i].kind = (int) ((10.0f * (float) rand()) / 2147483648.0f);
2219
2220 if (this->trap_circle[i].kind >= TRAP_CIRCLE_COUNT) {
2221 this->trap_circle[i].kind = 9;
2222 }
2223
2224 this->trap_circle[i].timer = 0.0f;
2225 break;
2226 }
2227 }
2228}
2229
2231 float pos[4];
2232 int loaded;
2233 int i;
2234
2235 loaded = false;
2236 sceVu0CopyVector(pos, CharaMain.pos);
2237
2238 for (i = 0; i < 3; i++) {
2239 if (this->trap_circle[i].state != 0) {
2240 if (DistVector(this->trap_circle[i].pos, pos) <= 160.0f * this->draw_dist_scale) {
2241 float wide;
2242 float tall;
2243
2244 // The texture is only wanted once, however many circles draw.
2245 if (!loaded) {
2246 loaded = true;
2247 TexManager.ReloadTexture(Vif1Packet, 71);
2248 Trap_Circle.TextureAnime(71);
2249 }
2250
2251 tall = 1.0f + sinf(this->trap_circle[i].timer);
2252 wide = 1.0f + sinf(this->trap_circle[i].timer) / 2.0f;
2253 Trap_Circle.SetScale(wide, tall, wide);
2254 Trap_Circle.SetPosition(this->trap_circle[i].pos);
2255 Trap_Circle.Draw();
2256 }
2257 }
2258 }
2259}
2260
2262 float pos[4];
2263
2264 sceVu0CopyVector(pos, CharaMain.pos);
2265
2266 for (int i = 0; i < 3; i++) {
2267 if (this->trap_circle[i].state == 1) {
2268 if (DistVector(this->trap_circle[i].pos, pos) <= 5.0f) {
2269 this->trap_circle[i].state = 2;
2270 return &this->trap_circle[i];
2271 }
2272 }
2273 }
2274
2275 return NULL;
2276}
2277
2279 for (int i = 0; i < 3; i++) {
2280 if (this->trap_circle[i].state == 2) {
2281 this->trap_circle[i].timer += 0.10471976f;
2282
2283 if (this->trap_circle[i].timer >= THREE_HALVES_PI) {
2284 this->trap_circle[i].state = 0;
2285 }
2286 }
2287 }
2288}
2289
2290int CDungeonMap::CheckTreasureBox(float *pos, float dist) {
2291 for (int i = 0; i < this->box_num; i++) {
2292 if (this->boxes[i].used != 0 && DistVector(this->boxes[i].pos, pos) < dist) {
2293 return 0;
2294 }
2295 }
2296
2297 return 1;
2298}
2299
2300int CDungeonMap::CheckAtra(float *pos, float dist) {
2301 // Demon Shaft does not have atla, only the first five maps do.
2302 if (selectMapNo >= DUNGEON_DEMON_SHAFT) {
2303 return 1;
2304 }
2305
2306 for (int i = 0; i < this->atra_num; i++) {
2307 if (this->atra[i].used != 0 && DistVector(this->atra[i].pos, pos) < dist) {
2308 return 0;
2309 }
2310 }
2311
2312 return 1;
2313}
2314
2315void CDungeonMap::SetAtraBoll(float *pos, int atra_no) {
2316 int i;
2317 int event_no;
2318
2319 sceVu0CopyVector(this->atra[this->atra_num].pos, pos);
2320 this->atra[this->atra_num].phase = 0.0f;
2321 this->atra[this->atra_num].atra_no = atra_no;
2322 this->atra[this->atra_num].used = 1;
2323
2324 for (i = 0; i < 48; i++) {
2325 if (this->events[i].kind == DNG_EVENT_NONE) {
2326 event_no = i;
2327 goto found;
2328 }
2329 }
2330
2331 printf("ivent buffer over !!\n");
2332 exit__2(-1);
2333
2334found:
2335 this->events[event_no].kind = DNG_EVENT_ATRA;
2336 this->events[event_no].placed_flag = 0;
2337 this->events[event_no].index = this->atra_num;
2338 sceVu0CopyVector(this->events[event_no].pos, pos);
2339 this->events[event_no].radius = 13.0f;
2340 this->atra_num++;
2341}
2342
2347int CDungeonMap::SetTreasureBox(float *pos, int item_no, int kind, int trap_no) {
2348 int box_no;
2349 int event_no;
2350 int i;
2351 int j;
2352
2353 box_no = -1;
2354
2355 for (i = 0; i < 24; i++) {
2356 if (this->boxes[i].used == 0) {
2357 box_no = i;
2358 break;
2359 }
2360 }
2361
2362 if (box_no == -1) {
2363 return -1;
2364 }
2365
2366 sceVu0CopyVector(this->boxes[box_no].pos, pos);
2367 this->boxes[box_no].used = 1;
2368 this->boxes[box_no].lid_angle = 0;
2369 this->boxes[box_no].kind = kind;
2370 this->boxes[box_no].item_no = item_no;
2371 this->boxes[box_no].closed = 1;
2372 this->boxes[box_no].trap_no = trap_no;
2373
2374 for (j = 0; j < 48; j++) {
2375 if (this->events[j].kind == DNG_EVENT_NONE) {
2376 event_no = j;
2377 goto found;
2378 }
2379 }
2380
2381 printf("ivent buffer over !!\n");
2382 exit__2(-1);
2383
2384found:
2385 this->events[event_no].kind = DNG_EVENT_TREASURE_BOX;
2386 this->events[event_no].placed_flag = 0;
2387 this->events[event_no].origin_x = (int) pos[0];
2388 this->events[event_no].origin_z = (int) pos[2];
2389 this->events[event_no].index = this->box_num;
2390
2391 // The event sits in front of the box, and a small box stands nearer to it.
2392 switch (kind) {
2393 case TREASURE_BOX_LARGE:
2394 this->events[event_no].pos[0] = pos[0];
2395 this->events[event_no].pos[1] = pos[1];
2396 this->events[event_no].pos[2] = pos[2] + 10.0f;
2397 this->events[event_no].radius = 8.0f;
2398 break;
2399 case TREASURE_BOX_SMALL:
2400 this->events[event_no].pos[0] = pos[0];
2401 this->events[event_no].pos[1] = pos[1];
2402 this->events[event_no].pos[2] = pos[2] + 8.0f;
2403 this->events[event_no].radius = 8.0f;
2404 break;
2405 }
2406
2407 this->box_num++;
2408 return event_no;
2409}
2410
2414void CDungeonMap::buildEventData(int floor_no, int enabled, int place_atla) {
2415 float box_pos[4];
2416 float object_pos[4];
2417 int atra_no[8]; /* BtAtraFloorCyoice fills and counts all eight of a floor's atla slots. */
2418 int special;
2419 int object_count;
2420 int valid;
2421 int event_no;
2422 int event_x;
2423 int event_y;
2424 int target_count;
2425
2426 if (enabled != 1) {
2427 return;
2428 }
2429
2430 special = 0;
2431
2432 if (place_atla == 0) {
2433 special = 1;
2434 }
2435
2436 object_count = 0;
2437 target_count = (int) ((4.0f * (float) rand()) / 2147483648.0f) + 1;
2438
2439 while (object_count < target_count + 3) {
2440 SearchiDoPutArea(this->cells, 0, 0, 20, 20, box_pos);
2441 valid = 1;
2442
2443 if (this->CheckTreasureBox(box_pos, 20.0f) == 0) {
2444 valid = 0;
2445 }
2446
2447 if (this->CheckAtra(box_pos, 20.0f) == 0) {
2448 valid = 0;
2449 }
2450
2451 if (this->CheckTrapCircle(box_pos, 20.0f) != NULL) {
2452 valid = 0;
2453 }
2454
2455 if (valid != 0) {
2456 if ((100.0f * (float) rand()) / 2147483648.0f >= 88.0f && object_count >= 2) {
2457 int item_no = PresetSmallItemNo_Get(selectMapNo, floor_no, special, 0);
2458
2459 if (item_no != -1) {
2460 int trap_no = (int) ((6.0f * (float) rand()) / 2147483648.0f);
2461
2462 if (trap_no < 0 || trap_no >= 6) {
2463 trap_no = 1;
2464 }
2465
2466 this->SetTreasureBox(box_pos, item_no, TREASURE_BOX_LARGE, trap_no);
2467 object_count++;
2468 }
2469 } else {
2470 int item_no = PresetSmallItemNo_Get(selectMapNo, floor_no, special, 1);
2471
2472 if (place_atla != 0) {
2473 if (object_count == 0) {
2474 item_no = ITEM_MAP;
2475 }
2476
2477 if (object_count == 1) {
2478 item_no = ITEM_MAGICAL_CRYSTAL;
2479 }
2480 }
2481
2482 if (item_no != -1) {
2483 this->SetTreasureBox(box_pos, item_no, TREASURE_BOX_SMALL, 0);
2484 object_count++;
2485 }
2486 }
2487 }
2488 }
2489
2490 this->initTrapCircle();
2491
2492 for (int i = 0; i < 3; i++) {
2493 if ((int) ((100.0f * (float) rand()) / 2147483648.0f) < 21) {
2494 SearchiDoPutArea(this->cells, 0, 0, 20, 20, object_pos);
2495 valid = 1;
2496
2497 if (this->CheckTreasureBox(object_pos, 20.0f) == 0) {
2498 valid = 0;
2499 }
2500
2501 if (this->CheckAtra(object_pos, 20.0f) == 0) {
2502 valid = 0;
2503 }
2504
2505 if (this->CheckTrapCircle(object_pos, 20.0f) != NULL) {
2506 valid = 0;
2507 }
2508
2509 if (valid != 0) {
2510 this->SetupTrapCircle(object_pos);
2511 }
2512 }
2513 }
2514
2515 if (place_atla != 0) {
2516 object_count = 0;
2517 int atra_count = BtAtraFloorCyoice(selectMapNo, floor_no, atra_no);
2518
2519 while (object_count < atra_count) {
2520 SearchiDoPutArea(this->cells, 0, 0, 20, 20, object_pos);
2521 valid = 1;
2522
2523 if (this->CheckTreasureBox(object_pos, 20.0f) == 0) {
2524 valid = 0;
2525 }
2526
2527 if (this->CheckAtra(object_pos, 20.0f) == 0) {
2528 valid = 0;
2529 }
2530
2531 if (this->CheckTrapCircle(object_pos, 20.0f) != NULL) {
2532 valid = 0;
2533 }
2534
2535 if (valid != 0) {
2536 sceVu0CopyVector(this->atra[this->atra_num].pos, object_pos);
2537 this->atra[this->atra_num].phase = 0.0f;
2538 this->atra[this->atra_num].atra_no = atra_no[object_count];
2539 this->atra[this->atra_num].used = 1;
2540
2541 for (int k = 0; k < 48; k++) {
2542 if (this->events[k].kind == DNG_EVENT_NONE) {
2543 event_no = k;
2544 goto found_atla_event;
2545 }
2546 }
2547
2548 printf("ivent buffer over !!\n");
2549 exit__2(-1);
2550
2551 found_atla_event:
2552 this->events[event_no].kind = DNG_EVENT_ATRA;
2553 this->events[event_no].placed_flag = 0;
2554 this->events[event_no].origin_x = event_x;
2555 this->events[event_no].origin_z = event_y;
2556 this->events[event_no].index = this->atra_num;
2557 sceVu0CopyVector(this->events[event_no].pos, object_pos);
2558 this->events[event_no].radius = 13.0f;
2559 this->atra_num++;
2560 object_count++;
2561 }
2562 }
2563 }
2564}
2565
2566void CDungeonMap::SetMimicEvent(float x, float y, float z, int item_no, int kind) {
2567 float pos[4];
2568 int box_no;
2569 int event_no;
2570 int i;
2571 int j;
2572
2573 pos[0] = x;
2574 pos[1] = y;
2575 pos[2] = z;
2576
2577 box_no = -1;
2578
2579 for (i = 0; i < 24; i++) {
2580 if (this->boxes[i].used == 0) {
2581 box_no = i;
2582 break;
2583 }
2584 }
2585
2586 if (box_no == -1) {
2587 return;
2588 }
2589
2590 for (j = 0; j < 48; j++) {
2591 if (this->events[j].kind == DNG_EVENT_NONE) {
2592 event_no = j;
2593 goto found;
2594 }
2595 }
2596
2597 printf("ivent buffer over !!\n");
2598 exit__2(-1);
2599
2600found:
2601 if (event_no == -1) {
2602 return;
2603 }
2604
2605 // The mimic looks like a treasure box, so it takes a box slot as well.
2606 this->events[event_no].kind = DNG_EVENT_MIMIC;
2607 this->events[event_no].placed_flag = 0;
2608 this->events[event_no].index = box_no;
2609
2610 sceVu0CopyVector(this->boxes[box_no].pos, pos);
2611 this->boxes[box_no].used = 1;
2612 this->boxes[box_no].lid_angle = 0;
2613 this->boxes[box_no].kind = kind;
2614 this->boxes[box_no].item_no = item_no;
2615 this->boxes[box_no].closed = 1;
2616
2617 switch (kind) {
2618 case TREASURE_BOX_LARGE:
2619 this->events[event_no].pos[0] = x;
2620 this->events[event_no].pos[1] = y;
2621 this->events[event_no].pos[2] = z + 10.0f;
2622 this->events[event_no].radius = 8.0f;
2623 break;
2624 case TREASURE_BOX_SMALL:
2625 this->events[event_no].pos[0] = x;
2626 this->events[event_no].pos[1] = y;
2627 this->events[event_no].pos[2] = z + 8.0f;
2628 this->events[event_no].radius = 8.0f;
2629 break;
2630 }
2631}
2632
2634 for (int i = 0; i < 48; i++) {
2635 if (this->events[i].kind != DNG_EVENT_NONE) {
2636 if (this->events[i].kind == DNG_EVENT_MIMIC) {
2637 this->events[i].kind = DNG_EVENT_NONE;
2638 this->boxes[this->events[i].index].used = 0;
2639 }
2640 }
2641 }
2642}
2643
2645 float pos[4];
2646
2647 sceVu0CopyVector(pos, frame->position);
2648
2649 // The grid cell that the player stands on. Calculated but not used.
2650 (void) (int) ((float) (int) (80.0f + pos[0]) / 160.0f);
2651 (void) (int) ((float) (int) (80.0f + pos[2]) / 160.0f);
2652
2653 for (int i = 0; i < 48; i++) {
2654 if (this->events[i].kind != DNG_EVENT_NONE && DistVector(this->events[i].pos, pos) <= this->events[i].radius) {
2655 return i;
2656 }
2657 }
2658
2659 return -1;
2660}
2661
2663 float pos[4];
2664 float stair_pos[4];
2665 int i;
2666 int j;
2667 int kind;
2668 int num;
2669 int tries;
2670 int x;
2671 int z;
2672 int shape;
2673
2674 num = 0;
2675 tries = 0;
2676 this->dummy_num = 0;
2677
2678 while (num < 8) {
2679 x = (int) ((21.0f * (float) rand()) / 2147483648.0f) - 1;
2680 z = (int) ((21.0f * (float) rand()) / 2147483648.0f) - 1;
2681 shape = (int) ((3.0f * (float) rand()) / 2147483648.0f);
2682 kind = -1;
2683
2684 // Each kind covers a different footprint, and only goes down where
2685 // every cell it would cover is empty.
2686 switch (shape) {
2687 case 0:
2688 kind = 0;
2689
2690 for (j = 0; j < 2; j++) {
2691 for (i = 0; i < 2; i++) {
2692 if (this->cells[x + (i + (j + z) * 20)].parts_no != MAP_PARTS_NONE) {
2693 kind = -1;
2694 }
2695 }
2696 }
2697
2698 if (kind != -1) {
2699 pos[0] = 80.0f + 160.0f * x;
2700 pos[1] = 0.0f;
2701 pos[2] = 80.0f + 160.0f * z;
2702 pos[3] = 1.0f;
2703 }
2704
2705 break;
2706 case 1:
2707 kind = 1;
2708
2709 for (j = 0; j < 2; j++) {
2710 for (i = 0; i < 2; i++) {
2711 if (this->cells[x + (i + (j + z) * 20)].parts_no != MAP_PARTS_NONE) {
2712 kind = -1;
2713 }
2714 }
2715 }
2716
2717 if (kind != -1) {
2718 pos[0] = 160.0f + 160.0f * x;
2719 pos[1] = 0.0f;
2720 pos[2] = 160.0f + 160.0f * z;
2721 pos[3] = 1.0f;
2722 }
2723
2724 break;
2725 case 2:
2726 kind = 2;
2727
2728 for (j = 0; j < 3; j++) {
2729 for (i = 0; i < 3; i++) {
2730 if (this->cells[x + (i + (j + z) * 20)].parts_no != MAP_PARTS_NONE) {
2731 kind = -1;
2732 }
2733 }
2734 }
2735
2736 if (kind != -1) {
2737 pos[0] = 240.0f + 160.0f * x;
2738 pos[1] = 0.0f;
2739 pos[2] = 240.0f + 160.0f * z;
2740 pos[3] = 1.0f;
2741 }
2742
2743 break;
2744 }
2745
2746 if (kind != -1) {
2747
2748 // A bigger model needs more room around it.
2749 for (x = 0; x < num; x++) {
2750 if (DistVector(pos, this->dummy_pos[x]) < 40.0f * kind) {
2751 kind = -1;
2752 break;
2753 }
2754 }
2755
2756 for (x = 0; x < 20; x++) {
2757 for (z = 0; z < 20; z++) {
2758 if (this->cells[z + x * 20].parts_no == MAP_PARTS_URA_ENTRANCE_NORTH) {
2759 stair_pos[0] = 80.0f + 160.0f * z;
2760 stair_pos[1] = 0.0f;
2761 stair_pos[2] = 80.0f + 160.0f * x;
2762 stair_pos[3] = 1.0f;
2763
2764 if (DistVector(stair_pos, pos) < 160.0f * (kind + 2)) {
2765 x = 21;
2766 z = x;
2767 kind = -1;
2768 }
2769 }
2770 }
2771 }
2772
2773 if (kind != -1) {
2774 if (this->dummy_num < 8) {
2775 sceVu0CopyVector(this->dummy_pos[this->dummy_num], pos);
2776 this->dummy_model[this->dummy_num] = kind;
2777 this->dummy_num++;
2778 }
2779
2780 num++;
2781 }
2782 }
2783
2784 if (tries > 10000) {
2785 break;
2786 }
2787
2788 tries++;
2789 }
2790}
2791
2796 int door_pos[32][2];
2797 int room_list[32];
2798 float pos[4];
2799 int door_count;
2800 int stair_x;
2801 int stair_y;
2802 int door_part;
2803 int result_no;
2804 int direction_mask;
2805 int room_count;
2806 int x;
2807 int propagation_mask;
2808 int active;
2809 int y;
2810
2811 result_no = 0;
2812
2813 for (int i = 0; i < 4; i++) {
2814 this->room_link[i].used = 0;
2815 }
2816
2817 for (int i = 0; i < 256; i++) {
2818 scanRoomInfoWork[i] = -1;
2819 }
2820
2821 // Record the stair cell and every candidate room-to-corridor door.
2822 door_count = 0;
2823
2824 for (y = 0; y < 16; y++) {
2825 for (x = 0; x < 16; x++) {
2826 if (this->cells[x + y * 20].parts_no == MAP_PARTS_STAIR_DOWN) {
2827 stair_x = x;
2828 stair_y = y;
2829 break;
2830 }
2831 }
2832 }
2833
2834 for (y = 0; y < 16; y++) {
2835 for (x = 0; x < 16; x++) {
2836 if ((buildMapDat[x + y * 20].content & BUILD_CELL_DOOR) != 0) {
2837 door_pos[door_count][0] = x;
2838 door_pos[door_count][1] = y;
2839 door_count++;
2840
2841 if (door_count >= 32) {
2842 y = 16;
2843 break;
2844 }
2845 }
2846 }
2847 }
2848
2849 int door_no = (int) (((float) door_count * (float) rand()) / 2147483648.0f);
2850 x = door_pos[door_no][0];
2851 y = door_pos[door_no][1];
2852 scanRoomInfoWork[x + y * 16] = 9;
2853 buildMapDat[x + y * 20].content |= BUILD_CELL_ROOM_LINK;
2854 this->room_link[0].door_x = x;
2855 this->room_link[0].door_y = y;
2856 this->room_link[0].unk_14 = 0;
2857
2858 direction_mask = 0;
2859
2860 if ((buildMapDat[x + (y - 1) * 20].content & BUILD_CELL_CORRIDOR) == BUILD_CELL_CORRIDOR) {
2861 direction_mask = MAP_DIR_NORTH;
2862 }
2863
2864 if ((buildMapDat[x + (y + 1) * 20].content & BUILD_CELL_CORRIDOR) == BUILD_CELL_CORRIDOR) {
2865 direction_mask = MAP_DIR_SOUTH;
2866 }
2867
2868 if ((buildMapDat[x + y * 20 + 1].content & BUILD_CELL_CORRIDOR) == BUILD_CELL_CORRIDOR) {
2869 direction_mask = MAP_DIR_EAST;
2870 }
2871
2872 if ((buildMapDat[x + y * 20 - 1].content & BUILD_CELL_CORRIDOR) == BUILD_CELL_CORRIDOR) {
2873 direction_mask = MAP_DIR_WEST;
2874 }
2875
2876 switch (direction_mask) {
2877 case MAP_DIR_NORTH:
2878 door_part = MAP_PARTS_ROOM_GATE_NORTH;
2879 scanRoomInfoWork[x + (y - 1) * 16] = 0;
2880 scanRoomInfoWork[x + (y + 1) * 16] = 1;
2881 break;
2882 case MAP_DIR_EAST:
2883 door_part = MAP_PARTS_ROOM_GATE_EAST;
2884 scanRoomInfoWork[x + y * 16 + 1] = 0;
2885 scanRoomInfoWork[x + y * 16 - 1] = 1;
2886 break;
2887 case MAP_DIR_SOUTH:
2888 door_part = MAP_PARTS_ROOM_GATE_SOUTH;
2889 scanRoomInfoWork[x + (y - 1) * 16] = 0;
2890 scanRoomInfoWork[x + (y + 1) * 16] = 1;
2891 break;
2892 case MAP_DIR_WEST:
2893 door_part = MAP_PARTS_ROOM_GATE_WEST;
2894 scanRoomInfoWork[x + y * 16 + 1] = 0;
2895 scanRoomInfoWork[x + y * 16 - 1] = 1;
2896 break;
2897 default:
2898 printf("ERROR !!\n");
2899 return;
2900 }
2901
2902 // Spread each side label only through cells that share a room membership.
2903 active = true;
2904
2905 while (active != 0) {
2906 active = false;
2907
2908 for (y = 1; y < 15; y++) {
2909 for (x = 1; x < 15; x++) {
2910 if (scanRoomInfoWork[x + y * 16] == -1 || scanRoomInfoWork[x + y * 16] == 9) {
2911 continue;
2912 }
2913
2914 switch (buildMapDat[x + y * 20].content & (BUILD_CELL_ROOM | BUILD_CELL_CORRIDOR | BUILD_CELL_DOOR)) {
2915 case BUILD_CELL_CORRIDOR:
2916 propagation_mask = BUILD_CELL_CORRIDOR | BUILD_CELL_DOOR | BUILD_CELL_STAIR_DOWN;
2917 break;
2918 case BUILD_CELL_ROOM:
2919 propagation_mask = BUILD_CELL_ROOM | BUILD_CELL_DOOR;
2920 break;
2921 case BUILD_CELL_ROOM | BUILD_CELL_DOOR:
2922 propagation_mask = BUILD_CELL_ROOM | BUILD_CELL_CORRIDOR;
2923 break;
2924 default:
2925 direction_mask = 0;
2926 break;
2927 }
2928
2929 if (buildMapDat[x + (y - 1) * 20].kind != MAP_PARTS_NONE && scanRoomInfoWork[x + (y - 1) * 16] == -1 && (propagation_mask & buildMapDat[x + (y - 1) * 20].content) != 0) {
2930 int shared = 0;
2931
2932 for (int i = 0; i < 16; i++) {
2933 if (buildMapDat[x + y * 20].link[i] == 1 && buildMapDat[x + (y - 1) * 20].link[i] == 1) {
2934 shared = 1;
2935 }
2936 }
2937
2938 if (shared != 0) {
2939 scanRoomInfoWork[x + (y - 1) * 16] = scanRoomInfoWork[x + y * 16];
2940 active = true;
2941 }
2942 }
2943
2944 if (buildMapDat[x + (y + 1) * 20].kind != MAP_PARTS_NONE && scanRoomInfoWork[x + (y + 1) * 16] == -1 && (propagation_mask & buildMapDat[x + (y + 1) * 20].content) != 0) {
2945 int shared = 0;
2946
2947 for (int i = 0; i < 16; i++) {
2948 if (buildMapDat[x + y * 20].link[i] == 1 && buildMapDat[x + (y + 1) * 20].link[i] == 1) {
2949 shared = 1;
2950 }
2951 }
2952
2953 if (shared != 0) {
2954 scanRoomInfoWork[x + (y + 1) * 16] = scanRoomInfoWork[x + y * 16];
2955 active = true;
2956 }
2957 }
2958
2959 if (buildMapDat[x + y * 20 - 1].kind != MAP_PARTS_NONE && scanRoomInfoWork[x + y * 16 - 1] == -1 && (propagation_mask & buildMapDat[x + y * 20 - 1].content) != 0) {
2960 int shared = 0;
2961
2962 for (int i = 0; i < 16; i++) {
2963 if (buildMapDat[x + y * 20].link[i] == 1 && buildMapDat[x + y * 20 - 1].link[i] == 1) {
2964 shared = 1;
2965 }
2966 }
2967
2968 if (shared != 0) {
2969 scanRoomInfoWork[x + y * 16 - 1] = scanRoomInfoWork[x + y * 16];
2970 active = true;
2971 }
2972 }
2973
2974 if (buildMapDat[x + y * 20 + 1].kind != MAP_PARTS_NONE && scanRoomInfoWork[x + y * 16 + 1] == -1 && (propagation_mask & buildMapDat[x + y * 20 + 1].content) != 0) {
2975 int shared = 0;
2976
2977 for (int i = 0; i < 16; i++) {
2978 if (buildMapDat[x + y * 20].link[i] == 1 && buildMapDat[x + y * 20 + 1].link[i] == 1) {
2979 shared = 1;
2980 }
2981 }
2982
2983 if (shared != 0) {
2984 scanRoomInfoWork[x + y * 16 + 1] = scanRoomInfoWork[x + y * 16];
2985 active = true;
2986 }
2987 }
2988 }
2989 }
2990 }
2991
2992 // The item goes in a room on the stair's side of the door.
2993 int target_label = scanRoomInfoWork[stair_x + (stair_y + 1) * 16];
2994 room_count = 0;
2995 active = true;
2996
2997 for (int i = 0; i < this->room_num; i++) {
2998 if (target_label == scanRoomInfoWork[this->rooms[i].x + (this->rooms[i].y + 1) * 16 + 1]) {
2999 room_list[room_count++] = i;
3000 }
3001 }
3002
3003 if (room_count == 0) {
3004 printf("//////--------> SET NOOMAL DOOR PUTERR!![%d] ///////// \n", target_label);
3005 active = false;
3006 }
3007
3008 while (active != 0) {
3009 int valid;
3010
3011 int list_no = (int) (((float) room_count * (float) rand()) / 2147483648.0f);
3012
3013 if (list_no < 0 || list_no > room_count) {
3014 list_no = 0;
3015 }
3016
3017 SearchiDoPutArea(this->cells, this->rooms[room_list[list_no]].x, this->rooms[room_list[list_no]].y, this->rooms[room_list[list_no]].width, this->rooms[room_list[list_no]].height, pos);
3018 valid = 1;
3019
3020 if (this->CheckTreasureBox(pos, 20.0f) == 0) {
3021 valid = 0;
3022 }
3023
3024 if (this->CheckAtra(pos, 20.0f) == 0) {
3025 valid = 0;
3026 }
3027
3028 if (this->CheckTrapCircle(pos, 20.0f) != NULL) {
3029 valid = 0;
3030 }
3031
3032 if (valid != 0) {
3033 if (this->SetTreasureBox(pos, selectMapNo + ITEM_BONE_KEY, TREASURE_BOX_SMALL, 0) == -1) {
3034 active = false;
3035 return;
3036 }
3037
3038 this->room_link[0].item_x = (pos[0] + 80.0f) / 160.0f;
3039 this->room_link[0].item_y = (pos[2] + 80.0f) / 160.0f;
3040 this->cells[this->room_link[result_no].door_x + this->room_link[result_no].door_y * 20].parts_no = door_part;
3041 this->cells[this->room_link[result_no].door_x + this->room_link[result_no].door_y * 20].direction = 0;
3042 this->room_link[0].used = 1;
3043 result_no++;
3044 active = false;
3045 }
3046 }
3047}
3048
3050 for (int i = 0; i < 20; i++) {
3051 this->cells[i * 20 + 19].parts_no = MAP_PARTS_URA_ROAD;
3052 this->cells[i * 20 + 19].direction = 0;
3053 }
3054}
3055
3056int CDungeonMap::CreatPartsList(int *list, int max, int first_part, int last_part) {
3057 int num = 0;
3058 PARTS_ADJACENT adjacent = {
3059 {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}
3060 };
3061 int row;
3062 int col;
3063 int free;
3064 int i;
3065
3066 for (row = 1; row < 15; row++) {
3067 for (col = 1; col < 15; col++) {
3068 if (last_part != -1) {
3069
3070 // Any cell that holds a part of a kind in the range will do.
3071 if (this->cells[col + row * 20].parts_no >= first_part && this->cells[col + row * 20].parts_no <= last_part) {
3072 list[num * 2] = col;
3073 list[num * 2 + 1] = row;
3074 num++;
3075
3076 if (num >= max - 1) {
3077 row = 16;
3078 break;
3079 }
3080 }
3081 } else {
3082 free = 1;
3083
3084 // A cell that touches one that is already spoken for will not.
3085 for (i = 0; i < 4; i++) {
3086 int dx = adjacent.offset[i][0];
3087 int dy = adjacent.offset[i][1];
3088 int cell_no = dx + (col + (row + dy) * 20);
3089
3090 if (this->cells[cell_no].parts_no != MAP_PARTS_NONE && (buildMapDat[cell_no].content & BUILD_CELL_ROOM_LINK) == BUILD_CELL_ROOM_LINK) {
3091 free = 0;
3092 }
3093 }
3094
3095 if (this->cells[col + row * 20].parts_no == first_part && free != 0) {
3096 list[num * 2] = col;
3097 list[num * 2 + 1] = row;
3098 num++;
3099
3100 if (num >= max - 1) {
3101 row = 16;
3102 break;
3103 }
3104 }
3105 }
3106 }
3107 }
3108
3109 return num;
3110}
3111
3112/* The integer address accumulation is a rare pointer-arithmetic exception.
3113 * Retail adds the cell offset to `this` with the offset on the left, and MWCC
3114 * emits that order only while the address accumulates in an integer: written as
3115 * `&this->cells[no]` the operands of that one `addu` come out the other way
3116 * round at every site. `->cells[0]` keeps the member offset symbolic. */
3118 int list[128];
3119 int roll;
3120 int num;
3121 int floor;
3122 int pick;
3123 MAP_CELL *cell;
3124 unsigned int address;
3125
3126 if (selectMapNo < DUNGEON_DEMON_SHAFT) {
3127 int zone = UserStatus->res_limit_zone_current;
3128
3129 // A resurrection zone puts that character's door on the floor and
3130 // nothing else.
3131 if (zone >= 0 && zone < 6) {
3132 if (zone == RES_LIMIT_ZONE_XIAO) {
3133 if ((int) ((100.0f * (float) rand()) / 2147483648.0f) >= 50) {
3134 this->SetCharaDoor(CHARA_TOAN);
3135 } else {
3136 this->SetCharaDoor(CHARA_TOAN);
3137 }
3138
3139 return;
3140 }
3141
3142 this->SetCharaDoor(zone);
3143 return;
3144 }
3145
3146 // The first floors of the first dungeon never take a special part.
3147 roll = (int) ((100.0f * (float) rand()) / 2147483648.0f);
3148
3149 if (selectMapNo == DUNGEON_DIVINE_BEAST_CAVE && UserStatus->cur_floor < 8) {
3150 roll = 0;
3151 }
3152
3153 if (UserStatus->party_size >= 2 && roll > 0x32) {
3154 num = this->CreatPartsList(list, 0x40, 0, -1);
3155
3156 if (num > 0) {
3157 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3158 this->cells[list[pick * 2] + list[pick * 2 + 1] * 20].parts_no = MAP_PARTS_KEY_XIAO;
3159 }
3160 }
3161
3162 // Which special part a floor takes depends on the dungeon, how far the
3163 // party has come and a roll of the dice.
3164 roll = (int) ((100.0f * (float) rand()) / 2147483648.0f);
3165 floor = UserStatus->cur_floor;
3166
3167 switch (selectMapNo) {
3168 case DUNGEON_WISE_OWL_FOREST:
3169 if (UserStatus->party_size >= 3 && roll > 0xA && floor >= 9) {
3170 num = this->CreatPartsList(list, 0x40, MAP_PARTS_ROOM_DOOR_NORTH, MAP_PARTS_ROOM_DOOR_WEST);
3171
3172 if (num > 0) {
3173 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3174 address = (list[pick * 2] + list[pick * 2 + 1] * 20) * sizeof(MAP_CELL);
3175 address += (unsigned int) this;
3176 cell = &((CDungeonMap *) address)->cells[0];
3178 return;
3179 }
3180 }
3181
3182 break;
3183 case DUNGEON_SHIPWRECK:
3184 if (UserStatus->party_size >= 3 && roll < 0x28) {
3185 num = this->CreatPartsList(list, 0x40, MAP_PARTS_ROOM_DOOR_NORTH, MAP_PARTS_ROOM_DOOR_WEST);
3186
3187 if (num > 0) {
3188 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3189 address = (list[pick * 2] + list[pick * 2 + 1] * 20) * sizeof(MAP_CELL);
3190 address += (unsigned int) this;
3191 cell = &((CDungeonMap *) address)->cells[0];
3193 return;
3194 }
3195 }
3196
3197 if (UserStatus->party_size >= 4 && roll >= 0x28 && floor >= 9) {
3198 num = this->CreatPartsList(list, 0x40, MAP_PARTS_ROOM_DOOR_NORTH, MAP_PARTS_ROOM_DOOR_WEST);
3199
3200 if (num > 0) {
3201 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3202 address = (list[pick * 2] + list[pick * 2 + 1] * 20) * sizeof(MAP_CELL);
3203 address += (unsigned int) this;
3204 cell = &((CDungeonMap *) address)->cells[0];
3206 return;
3207 }
3208 }
3209
3210 break;
3211 case DUNGEON_SUN_MOON_TEMPLE:
3212 if (UserStatus->party_size >= 3 && roll < 0x14) {
3213 num = this->CreatPartsList(list, 0x40, MAP_PARTS_ROOM_DOOR_NORTH, MAP_PARTS_ROOM_DOOR_WEST);
3214
3215 if (num > 0) {
3216 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3217 address = (list[pick * 2] + list[pick * 2 + 1] * 20) * sizeof(MAP_CELL);
3218 address += (unsigned int) this;
3219 cell = &((CDungeonMap *) address)->cells[0];
3221 return;
3222 }
3223 }
3224
3225 if (UserStatus->party_size >= 4 && roll < 0x28) {
3226 num = this->CreatPartsList(list, 0x40, MAP_PARTS_ROOM_DOOR_NORTH, MAP_PARTS_ROOM_DOOR_WEST);
3227
3228 if (num > 0) {
3229 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3230 address = (list[pick * 2] + list[pick * 2 + 1] * 20) * sizeof(MAP_CELL);
3231 address += (unsigned int) this;
3232 cell = &((CDungeonMap *) address)->cells[0];
3234 return;
3235 }
3236 }
3237
3238 if (UserStatus->party_size >= 5 && roll >= 0x28 && floor >= 9) {
3239 num = this->CreatPartsList(list, 0x40, 0, -1);
3240
3241 if (num > 0) {
3242 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3243 this->cells[list[pick * 2] + list[pick * 2 + 1] * 20].parts_no = MAP_PARTS_KEY_UNGAGA;
3244 return;
3245 }
3246 }
3247
3248 break;
3249 case DUNGEON_MOON_SEA:
3250 if (UserStatus->party_size >= 3 && roll < 0xA) {
3251 num = this->CreatPartsList(list, 0x40, MAP_PARTS_ROOM_DOOR_NORTH, MAP_PARTS_ROOM_DOOR_WEST);
3252
3253 if (num > 0) {
3254 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3255 address = (list[pick * 2] + list[pick * 2 + 1] * 20) * sizeof(MAP_CELL);
3256 address += (unsigned int) this;
3257 cell = &((CDungeonMap *) address)->cells[0];
3259 return;
3260 }
3261 }
3262
3263 if (UserStatus->party_size >= 4 && roll < 0x14) {
3264 num = this->CreatPartsList(list, 0x40, MAP_PARTS_ROOM_DOOR_NORTH, MAP_PARTS_ROOM_DOOR_WEST);
3265
3266 if (num > 0) {
3267 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3268 address = (list[pick * 2] + list[pick * 2 + 1] * 20) * sizeof(MAP_CELL);
3269 address += (unsigned int) this;
3270 cell = &((CDungeonMap *) address)->cells[0];
3272 return;
3273 }
3274 }
3275
3276 if (UserStatus->party_size >= 5 && roll < 0x1E) {
3277 num = this->CreatPartsList(list, 0x40, 0, -1);
3278
3279 if (num > 0) {
3280 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3281 this->cells[list[pick * 2] + list[pick * 2 + 1] * 20].parts_no = MAP_PARTS_KEY_UNGAGA;
3282 return;
3283 }
3284 }
3285
3286 if (UserStatus->party_size >= 6 && roll > 0x28 && floor >= 8) {
3287 num = this->CreatPartsList(list, 0x40, 0, -1);
3288
3289 if (num > 0) {
3290 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3291 this->cells[list[pick * 2] + list[pick * 2 + 1] * 20].parts_no = MAP_PARTS_KEY_OSMOND;
3292 return;
3293 }
3294 }
3295
3296 break;
3297 case DUNGEON_GALLERY_OF_TIME:
3298 if (UserStatus->party_size >= 3 && roll < 0xA) {
3299 num = this->CreatPartsList(list, 0x40, MAP_PARTS_ROOM_DOOR_NORTH, MAP_PARTS_ROOM_DOOR_WEST);
3300
3301 if (num > 0) {
3302 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3303 address = (list[pick * 2] + list[pick * 2 + 1] * 20) * sizeof(MAP_CELL);
3304 address += (unsigned int) this;
3305 cell = &((CDungeonMap *) address)->cells[0];
3307 return;
3308 }
3309 }
3310
3311 if (UserStatus->party_size >= 4 && roll < 0x14) {
3312 num = this->CreatPartsList(list, 0x40, MAP_PARTS_ROOM_DOOR_NORTH, MAP_PARTS_ROOM_DOOR_WEST);
3313
3314 if (num > 0) {
3315 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3316 address = (list[pick * 2] + list[pick * 2 + 1] * 20) * sizeof(MAP_CELL);
3317 address += (unsigned int) this;
3318 cell = &((CDungeonMap *) address)->cells[0];
3320 return;
3321 }
3322 }
3323
3324 if (UserStatus->party_size >= 5 && roll < 0x1E) {
3325 num = this->CreatPartsList(list, 0x40, 0, -1);
3326
3327 if (num > 0) {
3328 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3329 this->cells[list[pick * 2] + list[pick * 2 + 1] * 20].parts_no = MAP_PARTS_KEY_UNGAGA;
3330 return;
3331 }
3332 }
3333
3334 if (UserStatus->party_size >= 6 && roll > 0x28) {
3335 num = this->CreatPartsList(list, 0x40, 0, -1);
3336
3337 if (num > 0) {
3338 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3339 this->cells[list[pick * 2] + list[pick * 2 + 1] * 20].parts_no = MAP_PARTS_KEY_OSMOND;
3340 }
3341 }
3342
3343 break;
3344 }
3345 }
3346}
3347
3348/* The constants that the functions above name. These stand here rather than
3349 * at the foot of the file because MWCC's own .rodata for SetCharaDoor's jump
3350 * table is spliced in at this point, and retail puts the table after all of
3351 * the constants below and before the two that follow the function. */
3352
3353/* The integer address accumulation is the same exception as above, and for the
3354 * same reason. */
3355int CDungeonMap::SetCharaDoor(int chara_no) {
3356 int list[128];
3357 int num;
3358 int pick;
3359 MAP_CELL *cell;
3360 unsigned int address;
3361
3362 num = 0;
3363
3364 // Each character's door stands on a map part that suits it.
3365 switch (chara_no) {
3366 case CHARA_TOAN:
3367 if (UserStatus->party_size >= 2) {
3368 num = this->CreatPartsList(list, 0x40, 0, -1);
3369
3370 if (num > 0) {
3371 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3372 this->cells[list[pick * 2] + list[pick * 2 + 1] * 20].parts_no = MAP_PARTS_KEY_XIAO;
3373 }
3374 }
3375
3376 break;
3377 case CHARA_XIAO:
3378 num = this->CreatPartsList(list, 0x40, MAP_PARTS_ROOM_DOOR_NORTH, MAP_PARTS_ROOM_DOOR_WEST);
3379
3380 if (num > 0) {
3381 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3382 address = (list[pick * 2] + list[pick * 2 + 1] * 20) * sizeof(MAP_CELL);
3383 address += (unsigned int) this;
3384 cell = &((CDungeonMap *) address)->cells[0];
3386 }
3387
3388 break;
3389 case CHARA_GORO:
3390 num = this->CreatPartsList(list, 0x40, MAP_PARTS_ROOM_DOOR_NORTH, MAP_PARTS_ROOM_DOOR_WEST);
3391
3392 if (num > 0) {
3393 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3394 address = (list[pick * 2] + list[pick * 2 + 1] * 20) * sizeof(MAP_CELL);
3395 address += (unsigned int) this;
3396 cell = &((CDungeonMap *) address)->cells[0];
3398 }
3399
3400 break;
3401 case CHARA_RUBY:
3402 num = this->CreatPartsList(list, 0x40, MAP_PARTS_ROOM_DOOR_NORTH, MAP_PARTS_ROOM_DOOR_WEST);
3403
3404 if (num > 0) {
3405 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3406 address = (list[pick * 2] + list[pick * 2 + 1] * 20) * sizeof(MAP_CELL);
3407 address += (unsigned int) this;
3408 cell = &((CDungeonMap *) address)->cells[0];
3410 }
3411
3412 break;
3413 case CHARA_UNGAGA:
3414 num = this->CreatPartsList(list, 0x40, 0, -1);
3415
3416 if (num > 0) {
3417 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3418 this->cells[list[pick * 2] + list[pick * 2 + 1] * 20].parts_no = MAP_PARTS_KEY_UNGAGA;
3419 }
3420
3421 break;
3422 case CHARA_OSMOND:
3423 num = this->CreatPartsList(list, 0x40, 0, -1);
3424
3425 if (num > 0) {
3426 pick = (int) (((float) num * (float) rand()) / 2147483648.0f);
3427 this->cells[list[pick * 2] + list[pick * 2 + 1] * 20].parts_no = MAP_PARTS_KEY_OSMOND;
3428 }
3429
3430 break;
3431 }
3432
3433 return num;
3434}
3435
3436/* MWCC emits its own jump table for the switch, which lands where retail keeps
3437 * @3191, and a .data template for `gate_keys`, which is retail's @3162 at the end
3438 * of this unit's data run. */
3439void CDungeonMap::buildRandomMap(int room_max, int first_build) {
3440 int w;
3441 int h;
3442 int j;
3443 int i;
3444 int col;
3445 int row;
3446 int x;
3447 int y;
3448 int done;
3449 int retry;
3450 int dist;
3451 int near_no;
3452 int near_dist;
3453
3454 // A floor that is built for the first time picks the seed it is built from.
3455 if (first_build != 0) {
3456 this->map_seed = (int) ((float) rand() / (float) 100000);
3457 srand(this->map_seed);
3458 }
3459
3460 for (i = 0; i < 48; i++) {
3461 this->events[i].kind = DNG_EVENT_NONE;
3462 this->events[i].reset_flag = 0;
3463 }
3464
3465 for (i = 0; i < 24; i++) {
3466 this->boxes[i].used = 0;
3467 this->boxes[i].lid_angle = 0;
3468 this->boxes[i].trap_no = 0;
3469 }
3470
3471 this->box_num = 0;
3472
3473 for (i = 0; i < 8; i++) {
3474 this->atra[i].used = 0;
3475 }
3476
3477 this->atra_num = 0;
3478
3479 for (i = 0; i < 4; i++) {
3480 this->room_link[i].used = 0;
3481 }
3482
3483 for (row = 0; row < 20; row++) {
3484 for (col = 0; col < 20; col++) {
3485 buildMapDat[col + row * 20].kind = MAP_PARTS_NONE;
3486 buildMapDat[col + row * 20].direction = 0;
3487
3488 for (j = 0; j < 16; j++) {
3489 buildMapDat[col + row * 20].link[j] = 0;
3490 }
3491
3492 buildMapDat[col + row * 20].content = 0;
3493 this->cells[col + row * 20].direction = 0;
3494 }
3495 }
3496
3497 // Two rooms have to stand before any other can join to one.
3498 roomStackCnt = 0;
3499 retry = 0;
3500
3501 while (roomStackCnt < 2 && retry < 0x40) {
3502 done = 0;
3503 retry = 0;
3504
3505 while (done == 0) {
3506 w = (int) ((2.0f * (float) rand()) / 2147483648.0f) + 3;
3507 h = (int) ((2.0f * (float) rand()) / 2147483648.0f) + 3;
3508 x = (int) (((float) (16 - w) * (float) rand()) / 2147483648.0f);
3509 y = (int) (((float) (16 - h) * (float) rand()) / 2147483648.0f);
3510
3511 if (buildRoom(x, y, w, h, roomStackCnt) == 1) {
3512 roomStack[roomStackCnt].x = x;
3513 roomStack[roomStackCnt].y = y;
3514 roomStack[roomStackCnt].width = w;
3515 roomStack[roomStackCnt].height = h;
3516 roomStackCnt++;
3517 done = 1;
3518 } else {
3519 retry++;
3520
3521 if (retry >= 0x40) {
3522 done = 1;
3523 }
3524 }
3525 }
3526 }
3527
3528 joinRoom(0, 1);
3529
3530 // Every further room joins to whichever room its middle is nearest to.
3531 while (roomStackCnt <= room_max - 1 && retry < 0x200) {
3532 done = 0;
3533 retry = 0;
3534
3535 while (done == 0) {
3536 w = (int) ((2.0f * (float) rand()) / 2147483648.0f) + 3;
3537 h = (int) ((2.0f * (float) rand()) / 2147483648.0f) + 3;
3538 x = (int) (((float) (16 - w) * (float) rand()) / 2147483648.0f);
3539 y = (int) (((float) (16 - h) * (float) rand()) / 2147483648.0f);
3540
3541 if (buildRoom(x, y, w, h, roomStackCnt) == 1) {
3542 roomStack[roomStackCnt].x = x;
3543 roomStack[roomStackCnt].y = y;
3544 roomStack[roomStackCnt].width = w;
3545 roomStack[roomStackCnt].height = h;
3546 roomStackCnt++;
3547 near_no = -1;
3548 near_dist = 10000;
3549
3550 for (j = 0; j < roomStackCnt - 1; j++) {
3551 w = (roomStack[roomStackCnt - 1].x + roomStack[roomStackCnt - 1].width / 2) - (roomStack[j].x + roomStack[j].width / 2);
3552 h = (roomStack[roomStackCnt - 1].y + roomStack[roomStackCnt - 1].height / 2) - (roomStack[j].y + roomStack[j].height / 2);
3553
3554 dist = (int) sqrt((double) (w * w + h * h));
3555
3556 if (dist < near_dist) {
3557 near_no = j;
3558 near_dist = dist;
3559 }
3560 }
3561
3562 joinRoom(roomStackCnt - 1, near_no);
3563 done = 1;
3564 } else {
3565 retry++;
3566
3567 if (retry >= 0x200) {
3568 done = 1;
3569 }
3570 }
3571 }
3572 }
3573
3574 setRoomObject();
3575
3576 if (first_build != 0) {
3577 setRoomDivid();
3578 }
3579
3580 if (first_build != 0) {
3581 setUnderDungeonStart();
3582 } else {
3583 setUnderDungeonStart();
3584 }
3585
3586 if (first_build != 0) {
3587 setStair();
3588 }
3589
3591
3592 this->room_num = roomStackCnt;
3593 memcpy(this->rooms, roomStack, 0x100);
3594
3595 for (row = 0; row < 20; row++) {
3596 for (col = 0; col < 20; col++) {
3597 this->cells[col + row * 20].parts_no = buildMapDat[col + row * 20].kind;
3598 this->cells[col + row * 20].direction = buildMapDat[col + row * 20].direction;
3599 }
3600 }
3601
3602 if (selectMapNo == DUNGEON_MOON_SEA) {
3603 this->buildDummyModel();
3604 }
3605
3606 if ((int) ((100.0f * (float) rand()) / 2147483648.0f) < 0x33 && first_build != 0) {
3607 this->GetRoomLinkInfo();
3608 }
3609
3610 if (first_build != 0) {
3611 this->BuildCharaSpecialParts();
3612 }
3613
3614 // Which key opens the floor's gate.
3615 switch (selectMapNo) {
3616 case DUNGEON_DIVINE_BEAST_CAVE:
3617 this->gate_key_no = ITEM_DRAN_S_CREST;
3618 break;
3619 case DUNGEON_SHIPWRECK:
3620 this->gate_key_no = ITEM_HOOK;
3621 break;
3622 case DUNGEON_SUN_MOON_TEMPLE:
3623 this->gate_key_no = ITEM_KING_S_SLATE;
3624 break;
3625 case DUNGEON_MOON_SEA:
3626 this->gate_key_no = ITEM_GUN_POWDER;
3627 break;
3628 case DUNGEON_GALLERY_OF_TIME:
3629 this->gate_key_no = ITEM_CLOCK_HANDS;
3630 break;
3631 case DUNGEON_DEMON_SHAFT:
3632 this->gate_key_no = ITEM_BLACK_KNIGHT_CREST;
3633 break;
3634 }
3635
3636 if (selectMapNo == DUNGEON_WISE_OWL_FOREST) {
3637 int pick = (int) ((3.0f * (float) rand()) / 2147483648.0f);
3638 int gate_keys[3] = {0xC4, 0xC6, 0xCD};
3639
3640 if (pick < 0 || pick > 2) {
3641 pick = 0;
3642 }
3643
3644 this->gate_key_no = gate_keys[pick];
3645 }
3646
3647 this->SetUnderLoad();
3648 printf("map build success!!\n");
3649}
3650
3652 int i;
3653 int j;
3654
3655 for (i = 0; i < 6; i++) {
3656 this->bg_model[i] = NULL;
3657 }
3658
3659 for (i = 0; i < 3; i++) {
3660 this->dummy_frame[i] = NULL;
3661 }
3662
3663 this->box_lid_model = NULL;
3664 this->box_body_model = NULL;
3665 this->box_collision_model = NULL;
3666 this->chest_lid_model = NULL;
3667 this->chest_body_model = NULL;
3668 this->chest_collision_model = NULL;
3669 this->fall_model = NULL;
3670
3671 for (i = 0; i < 24; i++) {
3672 this->boxes[i].used = 0;
3673 }
3674
3675 for (i = 0; i < this->atra_num; i++) {
3676 this->atra[i].used = 0;
3677 }
3678
3679 this->bg_model_num = 0;
3680 this->dummy_frame_num = 0;
3681 this->dummy_num = 0;
3682 this->water.SetSize(32, 32, alloc);
3683
3684 for (i = 0; i < 72; i++) {
3685 this->parts[i].initalize();
3686 }
3687
3688 for (i = 0; i < 400; i++) {
3689 this->cells[i].parts_no = MAP_PARTS_NONE;
3690 this->cells[i].camera_dist = -1.0f;
3691 this->mask[i] = 0;
3692 this->cells[i].visible = false;
3693 }
3694
3695 for (i = 0; i < 16; i++) {
3696 this->room_seen[i] = 0;
3697 }
3698
3699 for (i = 0; i < 4; i++) {
3700 this->npc[i].chara.Initialize();
3701 this->npc[i].used = 0;
3702 this->npc[i].draw_num = 0;
3703 }
3704
3705 for (j = 0; j < 48; j++) {
3706 this->events[j].kind = DNG_EVENT_NONE;
3707 this->events[j].reset_flag = 0;
3708 }
3709
3710 for (j = 0; j < 24; j++) {
3711 this->boxes[j].used = 0;
3712 this->boxes[j].lid_angle = 0;
3713 this->boxes[j].trap_no = 0;
3714 }
3715
3716 this->box_num = 0;
3717
3718 for (j = 0; j < 8; j++) {
3719 this->atra[j].used = 0;
3720 }
3721
3722 this->atra_num = 0;
3723
3724 for (j = 0; j < 4; j++) {
3725 this->room_link[j].used = 0;
3726 }
3727}
3728
3730 int i;
3731 int j;
3732
3733 for (i = 0; i < 6; i++) {
3734 this->bg_model[i] = NULL;
3735 }
3736
3737 for (i = 0; i < 3; i++) {
3738 this->dummy_frame[i] = NULL;
3739 }
3740
3741 this->box_lid_model = NULL;
3742 this->box_body_model = NULL;
3743 this->box_collision_model = NULL;
3744 this->chest_lid_model = NULL;
3745 this->chest_body_model = NULL;
3746 this->chest_collision_model = NULL;
3747 this->fall_model = NULL;
3748
3749 for (i = 0; i < 24; i++) {
3750 this->boxes[i].used = 0;
3751 }
3752
3753 for (i = 0; i < this->atra_num; i++) {
3754 this->atra[i].used = 0;
3755 }
3756
3757 this->bg_model_num = 0;
3758 this->dummy_frame_num = 0;
3759 this->dummy_num = 0;
3760
3761 for (i = 0; i < 72; i++) {
3762 this->parts[i].initalize();
3763 }
3764
3765 for (i = 0; i < 400; i++) {
3766 this->cells[i].parts_no = MAP_PARTS_NONE;
3767 this->cells[i].camera_dist = -1.0f;
3768 this->mask[i] = 0;
3769 this->cells[i].visible = false;
3770 }
3771
3772 for (i = 0; i < 16; i++) {
3773 this->room_seen[i] = 0;
3774 }
3775
3776 for (i = 0; i < 4; i++) {
3777 this->npc[i].chara.Initialize();
3778 this->npc[i].used = 0;
3779 this->npc[i].draw_num = 0;
3780 }
3781
3782 for (j = 0; j < 48; j++) {
3783 this->events[j].kind = DNG_EVENT_NONE;
3784 this->events[j].reset_flag = 0;
3785 }
3786
3787 for (j = 0; j < 24; j++) {
3788 this->boxes[j].used = 0;
3789 this->boxes[j].lid_angle = 0;
3790 this->boxes[j].trap_no = 0;
3791 }
3792
3793 this->box_num = 0;
3794
3795 for (j = 0; j < 8; j++) {
3796 this->atra[j].used = 0;
3797 }
3798
3799 this->atra_num = 0;
3800
3801 for (j = 0; j < 4; j++) {
3802 this->room_link[j].used = 0;
3803 }
3804
3805 this->draw_dist_scale = 4.0f;
3806}
void GetPos(float *pos)
Definition camera.cpp:230
CFrame * frame
virtual void LoadPackData2(unsigned int *pack, char *name, CDataAlloc2< 1 > *alloc, int texture_block_no, CDataAlloc2< 1 > *image_alloc, int visual_type)
virtual void SetScale(float x, float y, float z)
void InitializeTexAnime(CTexAnimeData *data, int count)
virtual void Step()
virtual void SetRotation(float x, float y, float z)
void TextureAnime(int step)
virtual void Initialize()
float motion_speed
virtual void Draw()
virtual void SetPosition(float x, float y, float z)
sceVu0FVECTOR dummy_pos[8]
CFrame * GetFrameSearch(char *name)
int SetTreasureBox(float *pos, int item_no, int kind, int trap_no)
ROOM_LINK_RESULT room_link[4]
s32 room_seen[16]
void DrawFire(CFrameVu1 *frame, CCameraFollow *camera)
void DrawFireFreeStyle(CFrameVu1 *frame, CCameraFollow *camera)
MAP_TRAP_CIRCLE * DistTrapCircle()
void DrawItemBox(float *pos)
MAP_TRAP_CIRCLE trap_circle[3]
void SetupTrapCircle(float *pos)
MAP_NPC_MODEL npc[4]
void SetUnderLoad()
s32 mask[400]
int SetCharaDoor(int chara_no)
void DrawMapFreeStyle()
void DrawRaster(CFrameVu1 *frame)
TREASURE_BOX boxes[24]
void ClearNPC_Cash()
int CreatPartsList(int *list, int max, int first_part, int last_part)
void initSubmap(CDataAlloc2< 1 > *alloc)
s32 dummy_model[8]
void DrawWater(float *pos, int mute)
void DrawBGModel(CCamera *camera)
void buildDummyModel()
CFireOmni fire
void NPCSetMotion(int npc_no, int motion_no)
CFrame * dummy_frame[3]
int CreateCollision(CCPoly *poly, CBoxVu0 box, int num)
int CheckTreasureBox(float *pos, float dist)
MAP_CELL cells[400]
float draw_dist_scale
ATRA_BOLL atra[8]
CFrame * box_collision_model
CFrame * chest_collision_model
DUNGEON_EVENT events[48]
ROOM_INFO rooms[16]
void FlushCheckMask()
CFrame * bg_model[6]
void DrawDummyModel(CCamera *camera)
int GetActiveIvent(CFrameVu1 *frame)
void SetNPC(int npc_no, unsigned int *pack, int parts_no, sceVu0FVECTOR pos, sceVu0FVECTOR rot, int visible, int motion_no, CDataAlloc2< 1 > *alloc)
void DrawMap(CCameraFollow *camera, CFrameVu1 *player)
CFrame * collision_model
void DrawMiniMap(float *pos, float angle)
CFrame * atra_model
CFrame * box_body_model
void BuildCharaSpecialParts()
CFrame * box_lid_model
void DrawNPCDraw()
void StepTrapCircle()
void SetMimicEvent(float x, float y, float z, int item_no, int kind)
void buildEventData(int floor_no, int enabled, int place_atla)
float * CheckTrapCircle(float *pos, float dist)
CDungeonParts parts[72]
CFrame * chest_body_model
void RsetMimicEvent()
void GetRoomLinkInfo()
CFrame * chest_lid_model
void checkMask(float x, float z)
void DrawMapCalc(int mode)
void DrawTrapCircle()
void initTrapCircle()
int CheckAtra(float *pos, float dist)
void SetAtraBoll(float *pos, int atra_no)
void ReservNPC_Draw(int npc_no, float x, float y, float z, int direction)
void buildRandomMap(int room_max, int first_build)
void DrawAtraBoll(float *pos)
CFrame * fall_model
float fire_pos[6][4]
float frame_turn[6]
CFrame * GetSearchFrame(char *name)
PARTS_WATER water
CFrame * frame[6]
void DrawCalc(int x, int z, int turn, int map_type)
sceVu0FVECTOR pos
Definition op_a.cpp:108
void DrawRaster()
Definition fireomni.cpp:206
void SetTexture(CTexture *core_texture, CTexture *glow_texture)
Definition fireomni.cpp:62
char fog_enable
Definition frame.hpp:33
sceVu0FVECTOR position
Definition frame.hpp:76
float pos[4]
Definition object.hpp:18
s32 x
Definition rect.hpp:18
s32 width
Definition rect.hpp:20
s32 height
Definition rect.hpp:21
s32 y
Definition rect.hpp:19
s32 res_limit_zone_current
sceVu0FVECTOR vertex[4]
Definition op_c.cpp:142
void mapPartsFilter()
@ TREASURE_BOX_LARGE
@ TREASURE_BOX_SMALL
@ MAP_DIR_SOUTH
@ MAP_DIR_WEST
@ MAP_DIR_NORTH
@ MAP_DIR_EAST
@ DNG_EVENT_ATRA
@ DNG_EVENT_TREASURE_BOX
@ DNG_EVENT_MIMIC
@ DNG_EVENT_NONE
@ TRAP_CIRCLE_COUNT
@ MAP_PARTS_STAIR_UP
@ MAP_PARTS_PILLAR_1
@ MAP_PARTS_DIVIDE_WALL_NORTH
@ MAP_PARTS_STAIR_DOWN
@ MAP_PARTS_ROOM_GATE_NORTH
@ MAP_PARTS_KEY_OSMOND
@ MAP_PARTS_KEY_UNGAGA
@ MAP_PARTS_KEY_DOOR_RUBY_NORTH
@ MAP_PARTS_KEY_DOOR_GORO_NORTH
@ MAP_PARTS_ROOM_DOOR_NORTH
@ MAP_PARTS_ROAD_CROSS
@ MAP_PARTS_URA_ROAD
@ MAP_PARTS_DIVIDE_END_NORTH
@ MAP_PARTS_KEY_XIAO
@ MAP_PARTS_ROAD_DEAD_END
@ MAP_PARTS_ROAD_STRAIGHT
@ MAP_PARTS_ROAD_TEE
@ MAP_PARTS_NONE
@ MAP_PARTS_ROAD_CORNER
@ MAP_PARTS_URA_ENTRANCE_NORTH
@ MAP_PARTS_ROOM_FLOOR
@ MAP_PARTS_KEY_DOOR_XIAO_NORTH
@ MAP_PARTS_HEAL_SPRING_NORTH
@ MAP_PARTS_DIVIDE_DOOR_NORTH
ITEM_FREE_RECT rect[4]
s32 direction
float camera_dist
CCharacter chara
float draw_pos[16][4]
s32 draw_param[16]
int offset[4][2]
float vertex[4][4]