1 /**
2  * @file drlg_l2.cpp
3  *
4  * Implementation of the catacombs level generation algorithms.
5  */
6 
7 #include <algorithm>
8 
9 #include "all.h"
10 
11 DEVILUTION_BEGIN_NAMESPACE
12 
13 BYTE predungeon[DMAXX][DMAXY];
14 
15 namespace {
16 
17 int nSx1;
18 int nSy1;
19 int nSx2;
20 int nSy2;
21 int nRoomCnt;
22 ROOMNODE RoomList[81];
23 HALLNODE *pHallList;
24 
25 int Area_Min = 2;
26 int Room_Max = 10;
27 int Room_Min = 4;
28 const int Dir_Xadd[5] = { 0, 0, 1, 0, -1 };
29 const int Dir_Yadd[5] = { 0, -1, 0, 1, 0 };
30 const ShadowStruct SPATSL2[2] = { { 6, 3, 0, 3, 48, 0, 50 }, { 9, 3, 0, 3, 48, 0, 50 } };
31 //short word_48489A = 0;
32 
33 const BYTE BTYPESL2[161] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 17, 18, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 2, 2, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 0, 3, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
34 const BYTE BSTYPESL2[161] = { 0, 1, 2, 3, 0, 0, 6, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 6, 6, 6, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 2, 2, 2, 0, 0, 0, 1, 1, 1, 1, 6, 2, 2, 2, 0, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 2, 2, 3, 3, 3, 3, 1, 1, 2, 2, 3, 3, 3, 3, 1, 1, 3, 3, 2, 2, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
35 
36 /** Miniset: Arch vertical. */
37 const BYTE VARCH1[] = {
38 	// clang-format off
39 	2, 4, // width, height
40 
41 	3, 0, // search
42 	3, 1,
43 	3, 4,
44 	0, 7,
45 
46 	48,  0, // replace
47 	51, 39,
48 	47, 44,
49 	 0,  0,
50 	// clang-format on
51 };
52 /** Miniset: Arch vertical. */
53 const BYTE VARCH2[] = {
54 	// clang-format off
55 	2, 4, // width, height
56 
57 	3, 0, // search
58 	3, 1,
59 	3, 4,
60 	0, 8,
61 
62 	48,  0, // replace
63 	51, 39,
64 	47, 44,
65 	 0,  0,
66 	// clang-format on
67 };
68 /** Miniset: Arch vertical. */
69 const BYTE VARCH3[] = {
70 	// clang-format off
71 	2, 4, // width, height
72 
73 	3, 0, // search
74 	3, 1,
75 	3, 4,
76 	0, 6,
77 
78 	48,  0, // replace
79 	51, 39,
80 	47, 44,
81 	 0,  0,
82 	// clang-format on
83 };
84 /** Miniset: Arch vertical. */
85 const BYTE VARCH4[] = {
86 	// clang-format off
87 	2, 4, // width, height
88 
89 	3, 0, // search
90 	3, 1,
91 	3, 4,
92 	0, 9,
93 
94 	48,  0, // replace
95 	51, 39,
96 	47, 44,
97 	 0,  0,
98 	// clang-format on
99 };
100 /** Miniset: Arch vertical. */
101 const BYTE VARCH5[] = {
102 	// clang-format off
103 	2, 4, // width, height
104 
105 	3, 0, // search
106 	3, 1,
107 	3, 4,
108 	0, 14,
109 
110 	48,  0, // replace
111 	51, 39,
112 	47, 44,
113 	 0,  0,
114 	// clang-format on
115 };
116 /** Miniset: Arch vertical. */
117 const BYTE VARCH6[] = {
118 	// clang-format off
119 	2, 4, // width, height
120 
121 	3, 0, // search
122 	3, 1,
123 	3, 4,
124 	0, 13,
125 
126 	48,  0, // replace
127 	51, 39,
128 	47, 44,
129 	 0,  0,
130 	// clang-format on
131 };
132 /** Miniset: Arch vertical. */
133 const BYTE VARCH7[] = {
134 	// clang-format off
135 	2, 4, // width, height
136 
137 	3, 0, // search
138 	3, 1,
139 	3, 4,
140 	0, 16,
141 
142 	48,  0, // replace
143 	51, 39,
144 	47, 44,
145 	 0,  0,
146 	// clang-format on
147 };
148 /** Miniset: Arch vertical. */
149 const BYTE VARCH8[] = {
150 	// clang-format off
151 	2, 4, // width, height
152 
153 	3, 0, // search
154 	3, 1,
155 	3, 4,
156 	0, 15,
157 
158 	48,  0, // replace
159 	51, 39,
160 	47, 44,
161 	 0,  0,
162 	// clang-format on
163 };
164 /** Miniset: Arch vertical - corner. */
165 const BYTE VARCH9[] = {
166 	// clang-format off
167 	2, 4, // width, height
168 
169 	3, 0, // search
170 	3, 8,
171 	3, 4,
172 	0, 7,
173 
174 	48,  0, // replace
175 	51, 42,
176 	47, 44,
177 	 0,  0,
178 	// clang-format on
179 };
180 /** Miniset: Arch vertical - corner. */
181 const BYTE VARCH10[] = {
182 	// clang-format off
183 	2, 4, // width, height
184 
185 	3, 0, // search
186 	3, 8,
187 	3, 4,
188 	0, 8,
189 
190 	48,  0, // replace
191 	51, 42,
192 	47, 44,
193 	 0,  0,
194 	// clang-format on
195 };
196 /** Miniset: Arch vertical - corner. */
197 const BYTE VARCH11[] = {
198 	// clang-format off
199 	2, 4, // width, height
200 
201 	3, 0, // search
202 	3, 8,
203 	3, 4,
204 	0, 6,
205 
206 	48,  0, // replace
207 	51, 42,
208 	47, 44,
209 	 0,  0,
210 	// clang-format on
211 };
212 /** Miniset: Arch vertical - corner. */
213 const BYTE VARCH12[] = {
214 	// clang-format off
215 	2, 4, // width, height
216 
217 	3, 0, // search
218 	3, 8,
219 	3, 4,
220 	0, 9,
221 
222 	48,  0, // replace
223 	51, 42,
224 	47, 44,
225 	 0,  0,
226 	// clang-format on
227 };
228 /** Miniset: Arch vertical - corner. */
229 const BYTE VARCH13[] = {
230 	// clang-format off
231 	2, 4, // width, height
232 
233 	3,  0, // search
234 	3,  8,
235 	3,  4,
236 	0, 14,
237 
238 	48,  0, // replace
239 	51, 42,
240 	47, 44,
241 	 0,  0,
242 	// clang-format on
243 };
244 /** Miniset: Arch vertical - corner. */
245 const BYTE VARCH14[] = {
246 	// clang-format off
247 	2, 4, // width, height
248 
249 	3,  0, // search
250 	3,  8,
251 	3,  4,
252 	0, 13,
253 
254 	48,  0, // replace
255 	51, 42,
256 	47, 44,
257 	 0,  0,
258 	// clang-format on
259 };
260 /** Miniset: Arch vertical - corner. */
261 const BYTE VARCH15[] = {
262 	// clang-format off
263 	2, 4, // width, height
264 
265 	3,  0, // search
266 	3,  8,
267 	3,  4,
268 	0, 16,
269 
270 	48,  0, // replace
271 	51, 42,
272 	47, 44,
273 	 0,  0,
274 	// clang-format on
275 };
276 /** Miniset: Arch vertical - corner. */
277 const BYTE VARCH16[] = {
278 	// clang-format off
279 	2, 4, // width, height
280 
281 	3,  0, // search
282 	3,  8,
283 	3,  4,
284 	0, 15,
285 
286 	48,  0, // replace
287 	51, 42,
288 	47, 44,
289 	 0,  0,
290 	// clang-format on
291 };
292 /** Miniset: Arch vertical - open wall. */
293 const BYTE VARCH17[] = {
294 	// clang-format off
295 	2, 3, // width, height
296 
297 	2, 7, // search
298 	3, 4,
299 	0, 7,
300 
301 	141, 39, // replace
302 	 47, 44,
303 	  0,  0,
304 	// clang-format on
305 };
306 /** Miniset: Arch vertical - open wall. */
307 const BYTE VARCH18[] = {
308 	// clang-format off
309 	2, 3, // width, height
310 
311 	2, 7, // search
312 	3, 4,
313 	0, 8,
314 
315 	141, 39, // replace
316 	 47, 44,
317 	  0,  0,
318 	// clang-format on
319 };
320 /** Miniset: Arch vertical - open wall. */
321 const BYTE VARCH19[] = {
322 	// clang-format off
323 	2, 3, // width, height
324 
325 	2, 7, // search
326 	3, 4,
327 	0, 6,
328 
329 	141, 39, // replace
330 	 47, 44,
331 	  0,  0,
332 	// clang-format on
333 };
334 /** Miniset: Arch vertical - open wall. */
335 const BYTE VARCH20[] = {
336 	// clang-format off
337 	2, 3, // width, height
338 
339 	2, 7, // search
340 	3, 4,
341 	0, 9,
342 
343 	141, 39, // replace
344 	 47, 44,
345 	  0,  0,
346 	// clang-format on
347 };
348 /** Miniset: Arch vertical - open wall. */
349 const BYTE VARCH21[] = {
350 	// clang-format off
351 	2, 3, // width, height
352 
353 	2,  7, // search
354 	3,  4,
355 	0, 14,
356 
357 	141, 39, // replace
358 	 47, 44,
359 	  0,  0,
360 	// clang-format on
361 };
362 /** Miniset: Arch vertical - open wall. */
363 const BYTE VARCH22[] = {
364 	// clang-format off
365 	2, 3, // width, height
366 
367 	2,  7, // search
368 	3,  4,
369 	0, 13,
370 
371 	141, 39, // replace
372 	 47, 44,
373 	  0,  0,
374 	// clang-format on
375 };
376 /** Miniset: Arch vertical - open wall. */
377 const BYTE VARCH23[] = {
378 	// clang-format off
379 	2, 3, // width, height
380 
381 	2,  7, // search
382 	3,  4,
383 	0, 16,
384 
385 	141, 39, // replace
386 	 47, 44,
387 	  0,  0,
388 	// clang-format on
389 };
390 /** Miniset: Arch vertical - open wall. */
391 const BYTE VARCH24[] = {
392 	// clang-format off
393 	2, 3, // width, height
394 
395 	2,  7, // search
396 	3,  4,
397 	0, 15,
398 
399 	141, 39, // replace
400 	 47, 44,
401 	  0,  0,
402 	// clang-format on
403 };
404 /** Miniset: Arch vertical. */
405 const BYTE VARCH25[] = {
406 	// clang-format off
407 	2, 4, // width, height
408 
409 	3, 0, // search
410 	3, 4,
411 	3, 1,
412 	0, 7,
413 
414 	48,  0, // replace
415 	51, 39,
416 	47, 44,
417 	 0,  0,
418 	// clang-format on
419 };
420 /** Miniset: Arch vertical. */
421 const BYTE VARCH26[] = {
422 	// clang-format off
423 	2, 4, // width, height
424 
425 	3, 0, // search
426 	3, 4,
427 	3, 1,
428 	0, 8,
429 
430 	48,  0, // replace
431 	51, 39,
432 	47, 44,
433 	 0,  0,
434 	// clang-format on
435 };
436 /** Miniset: Arch vertical. */
437 const BYTE VARCH27[] = {
438 	// clang-format off
439 	2, 4, // width, height
440 
441 	3, 0, // search
442 	3, 4,
443 	3, 1,
444 	0, 6,
445 
446 	48,  0, // replace
447 	51, 39,
448 	47, 44,
449 	 0,  0,
450 	// clang-format on
451 };
452 /** Miniset: Arch vertical. */
453 const BYTE VARCH28[] = {
454 	// clang-format off
455 	2, 4, // width, height
456 
457 	3, 0, // search
458 	3, 4,
459 	3, 1,
460 	0, 9,
461 
462 	48,  0, // replace
463 	51, 39,
464 	47, 44,
465 	 0,  0,
466 	// clang-format on
467 };
468 /** Miniset: Arch vertical. */
469 const BYTE VARCH29[] = {
470 	// clang-format off
471 	2, 4, // width, height
472 
473 	3,  0, // search
474 	3,  4,
475 	3,  1,
476 	0, 14,
477 
478 	48,  0, // replace
479 	51, 39,
480 	47, 44,
481 	 0,  0,
482 	// clang-format on
483 };
484 /** Miniset: Arch vertical. */
485 const BYTE VARCH30[] = {
486 	// clang-format off
487 	2, 4, // width, height
488 
489 	3,  0, // search
490 	3,  4,
491 	3,  1,
492 	0, 13,
493 
494 	48,  0, // replace
495 	51, 39,
496 	47, 44,
497 	 0,  0,
498 	// clang-format on
499 };
500 /** Miniset: Arch vertical. */
501 const BYTE VARCH31[] = {
502 	// clang-format off
503 	2, 4, // width, height
504 
505 	3,  0, // search
506 	3,  4,
507 	3,  1,
508 	0, 16,
509 
510 	48,  0, // replace
511 	51, 39,
512 	47, 44,
513 	 0,  0,
514 	// clang-format on
515 };
516 /** Miniset: Arch vertical. */
517 const BYTE VARCH32[] = {
518 	// clang-format off
519 	2, 4, // width, height
520 
521 	3,  0, // search
522 	3,  4,
523 	3,  1,
524 	0, 15,
525 
526 	48,  0, // replace
527 	51, 39,
528 	47, 44,
529 	 0,  0,
530 	// clang-format on
531 };
532 /** Miniset: Arch vertical - room west entrance. */
533 const BYTE VARCH33[] = {
534 	// clang-format off
535 	2, 4, // width, height
536 
537 	2, 0, // search
538 	3, 8,
539 	3, 4,
540 	0, 7,
541 
542 	142,  0, // replace
543 	 51, 42,
544 	 47, 44,
545 	  0,  0,
546 	// clang-format on
547 };
548 /** Miniset: Arch vertical - room west entrance. */
549 const BYTE VARCH34[] = {
550 	// clang-format off
551 	2, 4, // width, height
552 
553 	2, 0, // search
554 	3, 8,
555 	3, 4,
556 	0, 8,
557 
558 	142,  0, // replace
559 	 51, 42,
560 	 47, 44,
561 	  0,  0,
562 	// clang-format on
563 };
564 /** Miniset: Arch vertical - room west entrance. */
565 const BYTE VARCH35[] = {
566 	// clang-format off
567 	2, 4, // width, height
568 
569 	2, 0, // search
570 	3, 8,
571 	3, 4,
572 	0, 6,
573 
574 	142,  0, // replace
575 	 51, 42,
576 	 47, 44,
577 	  0,  0,
578 	// clang-format on
579 };
580 /** Miniset: Arch vertical - room west entrance. */
581 const BYTE VARCH36[] = {
582 	// clang-format off
583 	2, 4, // width, height
584 
585 	2, 0, // search
586 	3, 8,
587 	3, 4,
588 	0, 9,
589 
590 	142,  0, // replace
591 	 51, 42,
592 	 47, 44,
593 	  0,  0,
594 	// clang-format on
595 };
596 /** Miniset: Arch vertical - room west entrance. */
597 const BYTE VARCH37[] = {
598 	// clang-format off
599 	2, 4, // width, height
600 
601 	2,  0, // search
602 	3,  8,
603 	3,  4,
604 	0, 14,
605 
606 	142,  0, // replace
607 	 51, 42,
608 	 47, 44,
609 	  0,  0,
610 	// clang-format on
611 };
612 /** Miniset: Arch vertical - room west entrance. */
613 const BYTE VARCH38[] = {
614 	// clang-format off
615 	2, 4, // width, height
616 
617 	2,  0, // search
618 	3,  8,
619 	3,  4,
620 	0, 13,
621 
622 	142,  0, // replace
623 	 51, 42,
624 	 47, 44,
625 	  0,  0,
626 	// clang-format on
627 };
628 /** Miniset: Arch vertical - room west entrance. */
629 const BYTE VARCH39[] = {
630 	// clang-format off
631 	2, 4, // width, height
632 
633 	2,  0, // search
634 	3,  8,
635 	3,  4,
636 	0, 16,
637 
638 	142,  0, // replace
639 	 51, 42,
640 	 47, 44,
641 	  0,  0,
642 	// clang-format on
643 };
644 /** Miniset: Arch vertical - room west entrance. */
645 const BYTE VARCH40[] = {
646 	// clang-format off
647 	2, 4, // width, height
648 
649 	2,  0, // search
650 	3,  8,
651 	3,  4,
652 	0, 15,
653 
654 	142,  0, // replace
655 	 51, 42,
656 	 47, 44,
657 	  0,  0,
658 	// clang-format on
659 };
660 /** Miniset: Arch horizontal. */
661 const BYTE HARCH1[] = {
662 	// clang-format off
663 	3, 2, // width, height
664 
665 	3, 3, 0, // search
666 	2, 5, 9,
667 
668 	49, 46, 0, // replace
669 	40, 45, 0,
670 	// clang-format on
671 };
672 /** Miniset: Arch horizontal. */
673 const BYTE HARCH2[] = {
674 	// clang-format off
675 	3, 2, // width, height
676 
677 	3, 3, 0, // search
678 	2, 5, 6,
679 
680 	49, 46, 0, // replace
681 	40, 45, 0,
682 	// clang-format on
683 };
684 /** Miniset: Arch horizontal. */
685 const BYTE HARCH3[] = {
686 	// clang-format off
687 	3, 2, // width, height
688 
689 	3, 3, 0, // search
690 	2, 5, 8,
691 
692 	49, 46, 0, // replace
693 	40, 45, 0,
694 	// clang-format on
695 };
696 /** Miniset: Arch horizontal. */
697 const BYTE HARCH4[] = {
698 	// clang-format off
699 	3, 2, // width, height
700 
701 	3, 3, 0, // search
702 	2, 5, 7,
703 
704 	49, 46, 0, // replace
705 	40, 45, 0,
706 	// clang-format on
707 };
708 /** Miniset: Arch horizontal. */
709 const BYTE HARCH5[] = {
710 	// clang-format off
711 	3, 2, // width, height
712 
713 	3, 3,  0, // search
714 	2, 5, 15,
715 
716 	49, 46, 0, // replace
717 	40, 45, 0,
718 	// clang-format on
719 };
720 /** Miniset: Arch horizontal. */
721 const BYTE HARCH6[] = {
722 	// clang-format off
723 	3, 2, // width, height
724 
725 	3, 3,  0, // search
726 	2, 5, 16,
727 
728 	49, 46, 0, // replace
729 	40, 45, 0,
730 	// clang-format on
731 };
732 /** Miniset: Arch horizontal. */
733 const BYTE HARCH7[] = {
734 	// clang-format off
735 	3, 2, // width, height
736 
737 	3, 3,  0, // search
738 	2, 5, 13,
739 
740 	49, 46, 0, // replace
741 	40, 45, 0,
742 	// clang-format on
743 };
744 /** Miniset: Arch horizontal. */
745 const BYTE HARCH8[] = {
746 	// clang-format off
747 	3, 2, // width, height
748 
749 	3, 3,  0, // search
750 	2, 5, 14,
751 
752 	49, 46, 0, // replace
753 	40, 45, 0,
754 	// clang-format on
755 };
756 /** Miniset: Arch horizontal - north corner. */
757 const BYTE HARCH9[] = {
758 	// clang-format off
759 	3, 2, // width, height
760 
761 	3, 3, 0, // search
762 	8, 5, 9,
763 
764 	49, 46, 0, // replace
765 	43, 45, 0,
766 	// clang-format on
767 };
768 /** Miniset: Arch horizontal - north corner. */
769 const BYTE HARCH10[] = {
770 	// clang-format off
771 	3, 2, // width, height
772 
773 	3, 3, 0, // search
774 	8, 5, 6,
775 
776 	49, 46, 0, // replace
777 	43, 45, 0,
778 	// clang-format on
779 };
780 /** Miniset: Arch horizontal - north corner. */
781 const BYTE HARCH11[] = {
782 	// clang-format off
783 	3, 2, // width, height
784 
785 	3, 3, 0, // search
786 	8, 5, 8,
787 
788 	49, 46, 0, // replace
789 	43, 45, 0,
790 	// clang-format on
791 };
792 /** Miniset: Arch horizontal - north corner. */
793 const BYTE HARCH12[] = {
794 	// clang-format off
795 	3, 2, // width, height
796 
797 	3, 3, 0, // search
798 	8, 5, 7,
799 
800 	49, 46, 0, // replace
801 	43, 45, 0,
802 	// clang-format on
803 };
804 /** Miniset: Arch horizontal - north corner. */
805 const BYTE HARCH13[] = {
806 	// clang-format off
807 	3, 2, // width, height
808 
809 	3, 3,  0, // search
810 	8, 5, 15,
811 
812 	49, 46, 0, // replace
813 	43, 45, 0,
814 	// clang-format on
815 };
816 /** Miniset: Arch horizontal - north corner. */
817 const BYTE HARCH14[] = {
818 	// clang-format off
819 	3, 2, // width, height
820 
821 	3, 3,  0, // search
822 	8, 5, 16,
823 
824 	49, 46, 0, // replace
825 	43, 45, 0,
826 	// clang-format on
827 };
828 /** Miniset: Arch horizontal - north corner. */
829 const BYTE HARCH15[] = {
830 	// clang-format off
831 	3, 2, // width, height
832 
833 	3, 3,  0, // search
834 	8, 5, 13,
835 
836 	49, 46, 0, // replace
837 	43, 45, 0,
838 	// clang-format on
839 };
840 /** Miniset: Arch horizontal - north corner. */
841 const BYTE HARCH16[] = {
842 	// clang-format off
843 	3, 2, // width, height
844 
845 	3, 3,  0, // search
846 	8, 5, 14,
847 
848 	49, 46, 0, // replace
849 	43, 45, 0,
850 	// clang-format on
851 };
852 /** Miniset: Arch horizontal - wall. */
853 const BYTE HARCH17[] = {
854 	// clang-format off
855 	3, 2, // width, height
856 
857 	1, 3, 0, // search
858 	8, 5, 9,
859 
860 	140, 46, 0, // replace
861 	 43, 45, 0,
862 	// clang-format on
863 };
864 /** Miniset: Arch horizontal - wall. */
865 const BYTE HARCH18[] = {
866 	// clang-format off
867 	3, 2, // width, height
868 
869 	1, 3, 0, // search
870 	8, 5, 6,
871 
872 	140, 46, 0, // Replace
873 	 43, 45, 0,
874 	// clang-format on
875 };
876 /** Miniset: Arch horizontal - wall. */
877 const BYTE HARCH19[] = {
878 	// clang-format off
879 	3, 2, // width, height
880 
881 	1, 3, 0, // search
882 	8, 5, 8,
883 
884 	140, 46, 0, // replace
885 	 43, 45, 0,
886 	// clang-format on
887 };
888 /** Miniset: Arch horizontal - wall. */
889 const BYTE HARCH20[] = {
890 	// clang-format off
891 	3, 2, // width, height
892 
893 	1, 3, 0, // search
894 	8, 5, 7,
895 
896 	140, 46, 0, // replace
897 	 43, 45, 0,
898 	// clang-format on
899 };
900 /** Miniset: Arch horizontal - wall. */
901 const BYTE HARCH21[] = {
902 	// clang-format off
903 	3, 2, // width, height
904 
905 	1, 3, 0, // search
906 	8, 5, 15,
907 
908 	140, 46, 0, // replace
909 	 43, 45, 0,
910 	// clang-format on
911 };
912 /** Miniset: Arch horizontal - wall. */
913 const BYTE HARCH22[] = {
914 	// clang-format off
915 	3, 2, // width, height
916 
917 	1, 3, 0, // search
918 	8, 5, 16,
919 
920 	140, 46, 0, // replace
921 	 43, 45, 0,
922 	// clang-format on
923 };
924 /** Miniset: Arch horizontal - wall. */
925 const BYTE HARCH23[] = {
926 	// clang-format off
927 	3, 2, // width, height
928 
929 	1, 3, 0, // search
930 	8, 5, 13,
931 
932 	140, 46, 0, // replace
933 	 43, 45, 0,
934 	// clang-format on
935 };
936 /** Miniset: Arch horizontal - wall. */
937 const BYTE HARCH24[] = {
938 	// clang-format off
939 	3, 2, // width, height
940 
941 	1, 3, 0, // search
942 	8, 5, 14,
943 
944 	140, 46, 0, // replace
945 	 43, 45, 0,
946 	// clang-format on
947 };
948 /** Miniset: Arch horizontal. */
949 const BYTE HARCH25[] = {
950 	// clang-format off
951 	3, 2, // width, height
952 
953 	3, 3, 0, // search
954 	5, 2, 9,
955 
956 	49, 46, 0, // replace
957 	40, 45, 0,
958 	// clang-format on
959 };
960 /** Miniset: Arch horizontal. */
961 const BYTE HARCH26[] = {
962 	// clang-format off
963 	3, 2, // width, height
964 
965 	3, 3, 0, // search
966 	5, 2, 6,
967 
968 	49, 46, 0, // replace
969 	40, 45, 0,
970 	// clang-format on
971 };
972 /** Miniset: Arch horizontal. */
973 const BYTE HARCH27[] = {
974 	// clang-format off
975 	3, 2, // width, height
976 
977 	3, 3, 0, // search
978 	5, 2, 8,
979 
980 	49, 46, 0, // replace
981 	40, 45, 0,
982 	// clang-format on
983 };
984 /** Miniset: Arch horizontal. */
985 const BYTE HARCH28[] = {
986 	// clang-format off
987 	3, 2, // width, height
988 
989 	3, 3, 0, // search
990 	5, 2, 7,
991 
992 	49, 46, 0, // replace
993 	40, 45, 0,
994 	// clang-format on
995 };
996 /** Miniset: Arch horizontal. */
997 const BYTE HARCH29[] = {
998 	// clang-format off
999 	3, 2, // width, height
1000 
1001 	3, 3,  0, // search
1002 	5, 2, 15,
1003 
1004 	49, 46, 0, // replace
1005 	40, 45, 0,
1006 	// clang-format on
1007 };
1008 /** Miniset: Arch horizontal. */
1009 const BYTE HARCH30[] = {
1010 	// clang-format off
1011 	3, 2, // width, height
1012 
1013 	3, 3,  0, // search
1014 	5, 2, 16,
1015 
1016 	49, 46, 0, // replace
1017 	40, 45, 0,
1018 	// clang-format on
1019 };
1020 /** Miniset: Arch horizontal. */
1021 const BYTE HARCH31[] = {
1022 	// clang-format off
1023 	3, 2, // width, height
1024 
1025 	3, 3,  0, // search
1026 	5, 2, 13,
1027 
1028 	49, 46, 0, // replace
1029 	40, 45, 0,
1030 	// clang-format on
1031 };
1032 /** Miniset: Arch horizontal. */
1033 const BYTE HARCH32[] = {
1034 	// clang-format off
1035 	3, 2, // width, height
1036 
1037 	3, 3,  0, // search
1038 	5, 2, 14,
1039 
1040 	49, 46, 0, // replace
1041 	40, 45, 0,
1042 	// clang-format on
1043 };
1044 /** Miniset: Arch horizontal - west corner. */
1045 const BYTE HARCH33[] = {
1046 	// clang-format off
1047 	3, 2, // width, height
1048 
1049 	1, 3, 0, // search
1050 	9, 5, 9,
1051 
1052 	140, 46, 0, // replace
1053 	 40, 45, 0,
1054 	// clang-format on
1055 };
1056 /** Miniset: Arch horizontal - west corner. */
1057 const BYTE HARCH34[] = {
1058 	// clang-format off
1059 	3, 2, // width, height
1060 
1061 	1, 3, 0, // search
1062 	9, 5, 6,
1063 
1064 	140, 46, 0, // replace
1065 	 40, 45, 0,
1066 	// clang-format on
1067 };
1068 /** Miniset: Arch horizontal - west corner. */
1069 const BYTE HARCH35[] = {
1070 	// clang-format off
1071 	3, 2, // width, height
1072 
1073 	1, 3, 0, // search
1074 	9, 5, 8,
1075 
1076 	140, 46, 0, // replace
1077 	 40, 45, 0,
1078 	// clang-format on
1079 };
1080 /** Miniset: Arch horizontal - west corner. */
1081 const BYTE HARCH36[] = {
1082 	// clang-format off
1083 	3, 2, // width, height
1084 
1085 	1, 3, 0, // search
1086 	9, 5, 7,
1087 
1088 	140, 46, 0, // replace
1089 	 40, 45, 0,
1090 	// clang-format on
1091 };
1092 /** Miniset: Arch horizontal - west corner. */
1093 const BYTE HARCH37[] = {
1094 	// clang-format off
1095 	3, 2, // width, height
1096 
1097 	1, 3, 0, // search
1098 	9, 5, 15,
1099 
1100 	140, 46, 0, // replace
1101 	 40, 45, 0,
1102 	// clang-format on
1103 };
1104 /** Miniset: Arch horizontal - west corner. */
1105 const BYTE HARCH38[] = {
1106 	// clang-format off
1107 	3, 2, // width, height
1108 
1109 	1, 3,  0, // search
1110 	9, 5, 16,
1111 
1112 	140, 46, 0, // replace
1113 	 40, 45, 0,
1114 	// clang-format on
1115 };
1116 /** Miniset: Arch horizontal - west corner. */
1117 const BYTE HARCH39[] = {
1118 	// clang-format off
1119 	3, 2, // width, height
1120 
1121 	1, 3,  0, // search
1122 	9, 5, 13,
1123 
1124 	140, 46, 0, // replace
1125 	 40, 45, 0,
1126 	// clang-format on
1127 };
1128 /** Miniset: Arch horizontal - west corner. */
1129 const BYTE HARCH40[] = {
1130 	// clang-format off
1131 	3, 2, // width, height
1132 
1133 	1, 3,  0, // search
1134 	9, 5, 14,
1135 
1136 	140, 46, 0, // replace
1137 	 40, 45, 0,
1138 	// clang-format on
1139 };
1140 /** Miniset: Stairs up. */
1141 const BYTE USTAIRS[] = {
1142 	// clang-format off
1143 	4, 4, // width, height
1144 
1145 	3, 3, 3, 3, // search
1146 	3, 3, 3, 3,
1147 	3, 3, 3, 3,
1148 	3, 3, 3, 3,
1149 
1150 	0,  0,  0, 0, // replace
1151 	0, 72, 77, 0,
1152 	0, 76,  0, 0,
1153 	0,  0,  0, 0,
1154 	// clang-format on
1155 };
1156 /** Miniset: Stairs down. */
1157 const BYTE DSTAIRS[] = {
1158 	// clang-format off
1159 	4, 4, // width, height
1160 
1161 	3, 3, 3, 3, // search
1162 	3, 3, 3, 3,
1163 	3, 3, 3, 3,
1164 	3, 3, 3, 3,
1165 
1166 	0,  0,  0, 0, // replace
1167 	0, 48, 71, 0,
1168 	0, 50, 78, 0,
1169 	0,  0,  0, 0,
1170 	// clang-format on
1171 };
1172 /** Miniset: Stairs to town. */
1173 const BYTE WARPSTAIRS[] = {
1174 	// clang-format off
1175 	4, 4, // width, height
1176 
1177 	3, 3, 3, 3, // search
1178 	3, 3, 3, 3,
1179 	3, 3, 3, 3,
1180 	3, 3, 3, 3,
1181 
1182 	0,   0,   0, 0, // replace
1183 	0, 158, 160, 0,
1184 	0, 159,   0, 0,
1185 	0,   0,   0, 0,
1186 	// clang-format on
1187 };
1188 /** Miniset: Crumbled south pillar. */
1189 const BYTE CRUSHCOL[] = {
1190 	// clang-format off
1191 	3, 3, // width, height
1192 
1193 	3, 1, 3, // search
1194 	2, 6, 3,
1195 	3, 3, 3,
1196 
1197 	0,  0, 0, // replace
1198 	0, 83, 0,
1199 	0,  0, 0,
1200 	// clang-format on
1201 };
1202 /** Miniset: Vertical oil spill. */
1203 const BYTE BIG1[] = {
1204 	// clang-format off
1205 	2, 2, // width, height
1206 
1207 	3, 3, // search
1208 	3, 3,
1209 
1210 	113, 0, // replace
1211 	112, 0,
1212 	// clang-format on
1213 };
1214 /** Miniset: Horizontal oil spill. */
1215 const BYTE BIG2[] = {
1216 	// clang-format off
1217 	2, 2, // width, height
1218 
1219 	3, 3, // search
1220 	3, 3,
1221 
1222 	114, 115, // replace
1223 	  0,   0,
1224 	// clang-format on
1225 };
1226 /** Miniset: Horizontal platform. */
1227 const BYTE BIG3[] = {
1228 	// clang-format off
1229 	1, 2, // width, height
1230 
1231 	1, // search
1232 	1,
1233 
1234 	117, // replace
1235 	116,
1236 	// clang-format on
1237 };
1238 /** Miniset: Vertical platform. */
1239 const BYTE BIG4[] = {
1240 	// clang-format off
1241 	2, 1, // width, height
1242 
1243 	2, 2, // search
1244 
1245 	118, 119, // replace
1246 	// clang-format on
1247 };
1248 /** Miniset: Large oil spill. */
1249 const BYTE BIG5[] = {
1250 	// clang-format off
1251 	2, 2, // width, height
1252 
1253 	3, 3, // search
1254 	3, 3,
1255 
1256 	120, 122, // replace
1257 	121, 123,
1258 	// clang-format on
1259 };
1260 /** Miniset: Vertical wall with debris. */
1261 const BYTE BIG6[] = {
1262 	// clang-format off
1263 	1, 2, // width, height
1264 
1265 	1, // search
1266 	1,
1267 
1268 	125, // replace
1269 	124,
1270 	// clang-format on
1271 };
1272 /** Miniset: Horizontal wall with debris. */
1273 const BYTE BIG7[] = {
1274 	// clang-format off
1275 	2, 1, // width, height
1276 
1277 	2, 2, // search
1278 
1279 	126, 127, // replace
1280 	// clang-format on
1281 };
1282 /** Miniset: Rock pile. */
1283 const BYTE BIG8[] = {
1284 	// clang-format off
1285 	2, 2, // width, height
1286 
1287 	3, 3, // search
1288 	3, 3,
1289 
1290 	128, 130, // replace
1291 	129, 131,
1292 	// clang-format on
1293 };
1294 /** Miniset: Vertical wall collapsed. */
1295 const BYTE BIG9[] = {
1296 	// clang-format off
1297 	2, 2, // width, height
1298 
1299 	1, 3, // search
1300 	1, 3,
1301 
1302 	133, 135, // replace
1303 	132, 134,
1304 	// clang-format on
1305 };
1306 /** Miniset: Horizontal wall collapsed. */
1307 const BYTE BIG10[] = {
1308 	// clang-format off
1309 	2, 2, // width, height
1310 
1311 	2, 2, // search
1312 	3, 3,
1313 
1314 	136, 137, // replace
1315 	  3,   3,
1316 	// clang-format on
1317 };
1318 /** Miniset: Crumbled vertical wall 1. */
1319 const BYTE RUINS1[] = {
1320 	// clang-format off
1321 	1, 1, // width, height
1322 
1323 	1, // search
1324 
1325 	80, // replace
1326 	// clang-format on
1327 };
1328 /** Miniset: Crumbled vertical wall 2. */
1329 const BYTE RUINS2[] = {
1330 	// clang-format off
1331 	1, 1, // width, height
1332 
1333 	1, // search
1334 
1335 	81, // replace
1336 	// clang-format on
1337 };
1338 /** Miniset: Crumbled vertical wall 3. */
1339 const BYTE RUINS3[] = {
1340 	// clang-format off
1341 	1, 1, // width, height
1342 
1343 	1, // search
1344 
1345 	82, // replace
1346 	// clang-format on
1347 };
1348 /** Miniset: Crumbled horizontal wall 1. */
1349 const BYTE RUINS4[] = {
1350 	// clang-format off
1351 	1, 1, // width, height
1352 
1353 	2, // search
1354 
1355 	84, // replace
1356 	// clang-format on
1357 };
1358 /** Miniset: Crumbled horizontal wall 2. */
1359 const BYTE RUINS5[] = {
1360 	// clang-format off
1361 	1, 1, // width, height
1362 
1363 	2, // search
1364 
1365 	85, // replace
1366 	// clang-format on
1367 };
1368 /** Miniset: Crumbled horizontal wall 3. */
1369 const BYTE RUINS6[] = {
1370 	// clang-format off
1371 	1, 1, // width, height
1372 
1373 	2, // search
1374 
1375 	86, // replace
1376 	// clang-format on
1377 };
1378 /** Miniset: Crumbled north pillar. */
1379 const BYTE RUINS7[] = {
1380 	// clang-format off
1381 	1, 1, // width, height
1382 
1383 	8, // search
1384 
1385 	87, // replace
1386 	// clang-format on
1387 };
1388 /** Miniset: Bloody gib 1. */
1389 const BYTE PANCREAS1[] = {
1390 	// clang-format off
1391 	5, 3, // width, height
1392 
1393 	3, 3, 3, 3, 3, // search
1394 	3, 3, 3, 3, 3,
1395 	3, 3, 3, 3, 3,
1396 
1397 	0, 0,   0, 0, 0, // replace
1398 	0, 0, 108, 0, 0,
1399 	0, 0,   0, 0, 0,
1400 	// clang-format on
1401 };
1402 /** Miniset: Bloody gib 2. */
1403 const BYTE PANCREAS2[] = {
1404 	// clang-format off
1405 	5, 3, // width, height
1406 
1407 	3, 3, 3, 3, 3, // search
1408 	3, 3, 3, 3, 3,
1409 	3, 3, 3, 3, 3,
1410 
1411 	0, 0,   0, 0, 0, // replace
1412 	0, 0, 110, 0, 0,
1413 	0, 0,   0, 0, 0,
1414 	// clang-format on
1415 };
1416 /** Miniset: Move vertical doors away from west pillar 1. */
1417 const BYTE CTRDOOR1[] = {
1418 	// clang-format off
1419 	3, 3, // width, height
1420 
1421 	3, 1, 3,  // search
1422 	0, 4, 0,
1423 	0, 9, 0,
1424 
1425 	0, 4, 0, // replace
1426 	0, 1, 0,
1427 	0, 0, 0,
1428 	// clang-format on
1429 };
1430 /** Miniset: Move vertical doors away from west pillar 2. */
1431 const BYTE CTRDOOR2[] = {
1432 	// clang-format off
1433 	3, 3, // width, height
1434 
1435 	3, 1, 3, // search
1436 	0, 4, 0,
1437 	0, 8, 0,
1438 
1439 	0, 4, 0, // replace
1440 	0, 1, 0,
1441 	0, 0, 0,
1442 	// clang-format on
1443 };
1444 /** Miniset: Move vertical doors away from west pillar 3. */
1445 const BYTE CTRDOOR3[] = {
1446 	// clang-format off
1447 	3, 3, // width, height
1448 
1449 	3, 1, 3, // search
1450 	0, 4, 0,
1451 	0, 6, 0,
1452 
1453 	0, 4, 0, // replace
1454 	0, 1, 0,
1455 	0, 0, 0,
1456 	// clang-format on
1457 };
1458 /** Miniset: Move vertical doors away from west pillar 4. */
1459 const BYTE CTRDOOR4[] = {
1460 	// clang-format off
1461 	3, 3, // width, height
1462 
1463 	3, 1, 3, // search
1464 	0, 4, 0,
1465 	0, 7, 0,
1466 
1467 	0, 4, 0, // replace
1468 	0, 1, 0,
1469 	0, 0, 0,
1470 	// clang-format on
1471 };
1472 /** Miniset: Move vertical doors away from west pillar 5. */
1473 const BYTE CTRDOOR5[] = {
1474 	// clang-format off
1475 	3, 3, // width, height
1476 
1477 	3,  1, 3, // search
1478 	0,  4, 0,
1479 	0, 15, 0,
1480 
1481 	0, 4, 0, // replace
1482 	0, 1, 0,
1483 	0, 0, 0,
1484 	// clang-format on
1485 };
1486 /** Miniset: Move vertical doors away from west pillar 6. */
1487 const BYTE CTRDOOR6[] = {
1488 	// clang-format off
1489 	3, 3, // width, height
1490 
1491 	3,  1, 3, // search
1492 	0,  4, 0,
1493 	0, 13, 0,
1494 
1495 	0, 4, 0, // replace
1496 	0, 1, 0,
1497 	0, 0, 0,
1498 	// clang-format on
1499 };
1500 /** Miniset: Move vertical doors away from west pillar 7. */
1501 const BYTE CTRDOOR7[] = {
1502 	// clang-format off
1503 	3, 3, // width, height
1504 
1505 	3,  1, 3, // search
1506 	0,  4, 0,
1507 	0, 16, 0,
1508 
1509 	0, 4, 0, // replace
1510 	0, 1, 0,
1511 	0, 0, 0,
1512 	// clang-format on
1513 };
1514 /** Miniset: Move vertical doors away from west pillar 8. */
1515 const BYTE CTRDOOR8[] = {
1516 	// clang-format off
1517 	3, 3, // width, height
1518 
1519 	3,  1, 3, // search
1520 	0,  4, 0,
1521 	0, 14, 0,
1522 
1523 	0, 4, 0, // replace
1524 	0, 1, 0,
1525 	0, 0, 0,
1526 	// clang-format on
1527 };
1528 
1529 int Patterns[100][10] = {
1530 	{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
1531 	{ 0, 0, 0, 0, 2, 0, 0, 0, 0, 3 },
1532 	{ 0, 7, 0, 0, 1, 0, 0, 5, 0, 2 },
1533 	{ 0, 5, 0, 0, 1, 0, 0, 7, 0, 2 },
1534 	{ 0, 0, 0, 7, 1, 5, 0, 0, 0, 1 },
1535 	{ 0, 0, 0, 5, 1, 7, 0, 0, 0, 1 },
1536 	{ 0, 1, 0, 0, 3, 0, 0, 1, 0, 4 },
1537 	{ 0, 0, 0, 1, 3, 1, 0, 0, 0, 5 },
1538 	{ 0, 6, 0, 6, 1, 0, 0, 0, 0, 6 },
1539 	{ 0, 6, 0, 0, 1, 6, 0, 0, 0, 9 },
1540 	{ 0, 0, 0, 6, 1, 0, 0, 6, 0, 7 },
1541 	{ 0, 0, 0, 0, 1, 6, 0, 6, 0, 8 },
1542 	{ 0, 6, 0, 6, 6, 0, 8, 6, 0, 7 },
1543 	{ 0, 6, 8, 6, 6, 6, 0, 0, 0, 9 },
1544 	{ 0, 6, 0, 0, 6, 6, 0, 6, 8, 8 },
1545 	{ 6, 6, 6, 6, 6, 6, 0, 6, 0, 8 },
1546 	{ 2, 6, 6, 6, 6, 6, 0, 6, 0, 8 },
1547 	{ 7, 7, 7, 6, 6, 6, 0, 6, 0, 8 },
1548 	{ 6, 6, 2, 6, 6, 6, 0, 6, 0, 8 },
1549 	{ 6, 2, 6, 6, 6, 6, 0, 6, 0, 8 },
1550 	{ 2, 6, 6, 6, 6, 6, 0, 6, 0, 8 },
1551 	{ 6, 7, 7, 6, 6, 6, 0, 6, 0, 8 },
1552 	{ 4, 4, 6, 6, 6, 6, 2, 6, 2, 8 },
1553 	{ 2, 2, 2, 2, 6, 2, 2, 6, 2, 7 },
1554 	{ 2, 2, 2, 2, 6, 2, 6, 6, 6, 7 },
1555 	{ 2, 2, 6, 2, 6, 6, 2, 2, 6, 9 },
1556 	{ 2, 6, 2, 2, 6, 2, 2, 2, 2, 6 },
1557 	{ 2, 2, 2, 2, 6, 6, 2, 2, 2, 9 },
1558 	{ 2, 2, 2, 6, 6, 2, 2, 2, 2, 6 },
1559 	{ 2, 2, 0, 2, 6, 6, 2, 2, 0, 9 },
1560 	{ 0, 0, 0, 0, 4, 0, 0, 0, 0, 12 },
1561 	{ 0, 1, 0, 0, 1, 4, 0, 1, 0, 10 },
1562 	{ 0, 0, 0, 1, 1, 1, 0, 4, 0, 11 },
1563 	{ 0, 0, 0, 6, 1, 4, 0, 1, 0, 14 },
1564 	{ 0, 6, 0, 1, 1, 0, 0, 4, 0, 16 },
1565 	{ 0, 6, 0, 0, 1, 1, 0, 4, 0, 15 },
1566 	{ 0, 0, 0, 0, 1, 1, 0, 1, 4, 13 },
1567 	{ 8, 8, 8, 8, 1, 1, 0, 1, 1, 13 },
1568 	{ 8, 8, 4, 8, 1, 1, 0, 1, 1, 10 },
1569 	{ 0, 0, 0, 1, 1, 1, 1, 1, 1, 11 },
1570 	{ 1, 1, 1, 1, 1, 1, 2, 2, 8, 2 },
1571 	{ 0, 1, 0, 1, 1, 4, 1, 1, 0, 16 },
1572 	{ 0, 0, 0, 1, 1, 1, 1, 1, 4, 11 },
1573 	{ 1, 1, 4, 1, 1, 1, 0, 2, 2, 2 },
1574 	{ 1, 1, 1, 1, 1, 1, 6, 2, 6, 2 },
1575 	{ 4, 1, 1, 1, 1, 1, 6, 2, 6, 2 },
1576 	{ 2, 2, 2, 1, 1, 1, 4, 1, 1, 11 },
1577 	{ 4, 1, 1, 1, 1, 1, 2, 2, 2, 2 },
1578 	{ 1, 1, 4, 1, 1, 1, 2, 2, 1, 2 },
1579 	{ 4, 1, 1, 1, 1, 1, 1, 2, 2, 2 },
1580 	{ 2, 2, 6, 1, 1, 1, 4, 1, 1, 11 },
1581 	{ 4, 1, 1, 1, 1, 1, 2, 2, 6, 2 },
1582 	{ 1, 2, 2, 1, 1, 1, 4, 1, 1, 11 },
1583 	{ 0, 1, 1, 0, 1, 1, 0, 1, 1, 10 },
1584 	{ 2, 1, 1, 3, 1, 1, 2, 1, 1, 14 },
1585 	{ 1, 1, 0, 1, 1, 2, 1, 1, 0, 1 },
1586 	{ 0, 4, 0, 1, 1, 1, 0, 1, 1, 14 },
1587 	{ 4, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
1588 	{ 0, 1, 0, 4, 1, 1, 0, 1, 1, 15 },
1589 	{ 1, 1, 1, 1, 1, 1, 0, 2, 2, 2 },
1590 	{ 0, 1, 1, 2, 1, 1, 2, 1, 4, 10 },
1591 	{ 2, 1, 1, 1, 1, 1, 0, 4, 0, 16 },
1592 	{ 1, 1, 4, 1, 1, 2, 0, 1, 2, 1 },
1593 	{ 2, 1, 1, 2, 1, 1, 1, 1, 4, 10 },
1594 	{ 1, 1, 2, 1, 1, 2, 4, 1, 8, 1 },
1595 	{ 2, 1, 4, 1, 1, 1, 4, 4, 1, 16 },
1596 	{ 2, 1, 1, 1, 1, 1, 1, 1, 1, 16 },
1597 	{ 1, 1, 2, 1, 1, 1, 1, 1, 1, 15 },
1598 	{ 1, 1, 1, 1, 1, 1, 2, 1, 1, 14 },
1599 	{ 4, 1, 1, 1, 1, 1, 2, 1, 1, 14 },
1600 	{ 1, 1, 1, 1, 1, 1, 1, 1, 2, 8 },
1601 	{ 0, 0, 0, 0, 255, 0, 0, 0, 0, 0 },
1602 };
1603 
1604 } // namespace
1605 
DRLG_L2PlaceMiniSet(const BYTE * miniset,int tmin,int tmax,int cx,int cy,BOOL setview,int ldir)1606 static BOOL DRLG_L2PlaceMiniSet(const BYTE *miniset, int tmin, int tmax, int cx, int cy, BOOL setview, int ldir)
1607 {
1608 	int sx, sy, sw, sh, xx, yy, i, ii, numt, bailcnt;
1609 	BOOL found;
1610 
1611 	sw = miniset[0];
1612 	sh = miniset[1];
1613 
1614 	if (tmax - tmin == 0) {
1615 		numt = 1;
1616 	} else {
1617 		numt = random_(0, tmax - tmin) + tmin;
1618 	}
1619 
1620 	for (i = 0; i < numt; i++) {
1621 		sx = random_(0, DMAXX - sw);
1622 		sy = random_(0, DMAXY - sh);
1623 		found = FALSE;
1624 		for (bailcnt = 0; !found && bailcnt < 200; bailcnt++) {
1625 			found = TRUE;
1626 			if (sx >= nSx1 && sx <= nSx2 && sy >= nSy1 && sy <= nSy2) {
1627 				found = FALSE;
1628 			}
1629 			if (cx != -1 && sx >= cx - sw && sx <= cx + 12) {
1630 				sx = random_(0, DMAXX - sw);
1631 				sy = random_(0, DMAXY - sh);
1632 				found = FALSE;
1633 			}
1634 			if (cy != -1 && sy >= cy - sh && sy <= cy + 12) {
1635 				sx = random_(0, DMAXX - sw);
1636 				sy = random_(0, DMAXY - sh);
1637 				found = FALSE;
1638 			}
1639 			ii = 2;
1640 			for (yy = 0; yy < sh && found == TRUE; yy++) {
1641 				for (xx = 0; xx < sw && found == TRUE; xx++) {
1642 					if (miniset[ii] != 0 && dungeon[xx + sx][yy + sy] != miniset[ii]) {
1643 						found = FALSE;
1644 					}
1645 					if (dflags[xx + sx][yy + sy] != 0) {
1646 						found = FALSE;
1647 					}
1648 					ii++;
1649 				}
1650 			}
1651 			if (!found) {
1652 				sx++;
1653 				if (sx == DMAXX - sw) {
1654 					sx = 0;
1655 					sy++;
1656 					if (sy == DMAXY - sh) {
1657 						sy = 0;
1658 					}
1659 				}
1660 			}
1661 		}
1662 		if (bailcnt >= 200) {
1663 			return FALSE;
1664 		}
1665 		ii = sw * sh + 2;
1666 		for (yy = 0; yy < sh; yy++) {
1667 			for (xx = 0; xx < sw; xx++) {
1668 				if (miniset[ii] != 0) {
1669 					dungeon[xx + sx][yy + sy] = miniset[ii];
1670 				}
1671 				ii++;
1672 			}
1673 		}
1674 	}
1675 
1676 	if (setview == TRUE) {
1677 		ViewX = 2 * sx + 21;
1678 		ViewY = 2 * sy + 22;
1679 	}
1680 	if (ldir == 0) {
1681 		LvlViewX = 2 * sx + 21;
1682 		LvlViewY = 2 * sy + 22;
1683 	}
1684 	if (ldir == 6) {
1685 		LvlViewX = 2 * sx + 21;
1686 		LvlViewY = 2 * sy + 22;
1687 	}
1688 
1689 	return TRUE;
1690 }
1691 
DRLG_L2PlaceRndSet(const BYTE * miniset,int rndper)1692 static void DRLG_L2PlaceRndSet(const BYTE *miniset, int rndper)
1693 {
1694 	int sx, sy, sw, sh, xx, yy, ii, kk;
1695 	BOOL found;
1696 
1697 	sw = miniset[0];
1698 	sh = miniset[1];
1699 
1700 	for (sy = 0; sy < DMAXY - sh; sy++) {
1701 		for (sx = 0; sx < DMAXX - sw; sx++) {
1702 			found = TRUE;
1703 			ii = 2;
1704 			if (sx >= nSx1 && sx <= nSx2 && sy >= nSy1 && sy <= nSy2) {
1705 				found = FALSE;
1706 			}
1707 			for (yy = 0; yy < sh && found == TRUE; yy++) {
1708 				for (xx = 0; xx < sw && found == TRUE; xx++) {
1709 					if (miniset[ii] != 0 && dungeon[xx + sx][yy + sy] != miniset[ii]) {
1710 						found = FALSE;
1711 					}
1712 					if (dflags[xx + sx][yy + sy] != 0) {
1713 						found = FALSE;
1714 					}
1715 					ii++;
1716 				}
1717 			}
1718 			kk = sw * sh + 2;
1719 			if (found == TRUE) {
1720 				for (yy = std::max(sy - sh, 0); yy < std::min(sy + 2 * sh, DMAXY) && found == TRUE; yy++) {
1721 					for (xx = std::max(sx - sw, 0); xx < std::min(sx + 2 * sw, DMAXX); xx++) {
1722 						// BUGFIX: yy and xx can go out of bounds (fixed)
1723 						if (dungeon[xx][yy] == miniset[kk]) {
1724 							found = FALSE;
1725 						}
1726 					}
1727 				}
1728 			}
1729 			if (found == TRUE && random_(0, 100) < rndper) {
1730 				for (yy = 0; yy < sh; yy++) {
1731 					for (xx = 0; xx < sw; xx++) {
1732 						if (miniset[kk] != 0) {
1733 							dungeon[xx + sx][yy + sy] = miniset[kk];
1734 						}
1735 						kk++;
1736 					}
1737 				}
1738 			}
1739 		}
1740 	}
1741 }
1742 
DRLG_L2Subs()1743 static void DRLG_L2Subs()
1744 {
1745 	int x, y, i, j, k, rv;
1746 	BYTE c;
1747 
1748 	for (y = 0; y < DMAXY; y++) {
1749 		for (x = 0; x < DMAXX; x++) {
1750 			if ((x < nSx1 || x > nSx2) && (y < nSy1 || y > nSy2) && random_(0, 4) == 0) {
1751 				c = BTYPESL2[dungeon[x][y]];
1752 				if (c != 0) {
1753 					rv = random_(0, 16);
1754 					k = -1;
1755 					while (rv >= 0) {
1756 						k++;
1757 						if (k == sizeof(BTYPESL2)) {
1758 							k = 0;
1759 						}
1760 						if (c == BTYPESL2[k]) {
1761 							rv--;
1762 						}
1763 					}
1764 					for (j = y - 2; j < y + 2; j++) {
1765 						for (i = x - 2; i < x + 2; i++) {
1766 							if (dungeon[i][j] == k) {
1767 								j = y + 3;
1768 								i = x + 2;
1769 							}
1770 						}
1771 					}
1772 					if (j < y + 3) {
1773 						dungeon[x][y] = k;
1774 					}
1775 				}
1776 			}
1777 		}
1778 	}
1779 }
1780 
DRLG_L2Shadows()1781 static void DRLG_L2Shadows()
1782 {
1783 	int x, y, i;
1784 	BOOL patflag;
1785 	BYTE sd[2][2];
1786 
1787 	for (y = 1; y < DMAXY; y++) {
1788 		for (x = 1; x < DMAXX; x++) {
1789 			sd[0][0] = BSTYPESL2[dungeon[x][y]];
1790 			sd[1][0] = BSTYPESL2[dungeon[x - 1][y]];
1791 			sd[0][1] = BSTYPESL2[dungeon[x][y - 1]];
1792 			sd[1][1] = BSTYPESL2[dungeon[x - 1][y - 1]];
1793 			for (i = 0; i < 2; i++) {
1794 				if (SPATSL2[i].strig == sd[0][0]) {
1795 					patflag = TRUE;
1796 					if (SPATSL2[i].s1 != 0 && SPATSL2[i].s1 != sd[1][1]) {
1797 						patflag = FALSE;
1798 					}
1799 					if (SPATSL2[i].s2 != 0 && SPATSL2[i].s2 != sd[0][1]) {
1800 						patflag = FALSE;
1801 					}
1802 					if (SPATSL2[i].s3 != 0 && SPATSL2[i].s3 != sd[1][0]) {
1803 						patflag = FALSE;
1804 					}
1805 					if (patflag == TRUE) {
1806 						if (SPATSL2[i].nv1 != 0) {
1807 							dungeon[x - 1][y - 1] = SPATSL2[i].nv1;
1808 						}
1809 						if (SPATSL2[i].nv2 != 0) {
1810 							dungeon[x][y - 1] = SPATSL2[i].nv2;
1811 						}
1812 						if (SPATSL2[i].nv3 != 0) {
1813 							dungeon[x - 1][y] = SPATSL2[i].nv3;
1814 						}
1815 					}
1816 				}
1817 			}
1818 		}
1819 	}
1820 }
1821 
InitDungeon()1822 void InitDungeon()
1823 {
1824 	int i, j;
1825 
1826 	for (j = 0; j < DMAXY; j++) {
1827 		for (i = 0; i < DMAXX; i++) {
1828 			predungeon[i][j] = 32;
1829 			dflags[i][j] = 0;
1830 		}
1831 	}
1832 }
1833 
DRLG_LoadL2SP()1834 static void DRLG_LoadL2SP()
1835 {
1836 	setloadflag = FALSE;
1837 
1838 	if (QuestStatus(Q_BLIND)) {
1839 		pSetPiece = LoadFileInMem("Levels\\L2Data\\Blind1.DUN", NULL);
1840 		pSetPiece[26] = 154; // Close outer wall
1841 		pSetPiece[200] = 154; // Close outer wall
1842 		setloadflag = TRUE;
1843 	} else if (QuestStatus(Q_BLOOD)) {
1844 		pSetPiece = LoadFileInMem("Levels\\L2Data\\Blood1.DUN", NULL);
1845 		setloadflag = TRUE;
1846 	} else if (QuestStatus(Q_SCHAMB)) {
1847 		pSetPiece = LoadFileInMem("Levels\\L2Data\\Bonestr2.DUN", NULL);
1848 		setloadflag = TRUE;
1849 	}
1850 }
1851 
DRLG_FreeL2SP()1852 static void DRLG_FreeL2SP()
1853 {
1854 	MemFreeDbg(pSetPiece);
1855 }
1856 
DRLG_L2SetRoom(int rx1,int ry1)1857 static void DRLG_L2SetRoom(int rx1, int ry1)
1858 {
1859 	int rw, rh, i, j;
1860 	BYTE *sp;
1861 
1862 	rw = pSetPiece[0];
1863 	rh = pSetPiece[2];
1864 
1865 	setpc_x = rx1;
1866 	setpc_y = ry1;
1867 	setpc_w = rw;
1868 	setpc_h = rh;
1869 
1870 	sp = &pSetPiece[4];
1871 
1872 	for (j = 0; j < rh; j++) {
1873 		for (i = 0; i < rw; i++) {
1874 			if (*sp != 0) {
1875 				dungeon[i + rx1][j + ry1] = *sp;
1876 				dflags[i + rx1][j + ry1] |= DLRG_PROTECTED;
1877 			} else {
1878 				dungeon[i + rx1][j + ry1] = 3;
1879 			}
1880 			sp += 2;
1881 		}
1882 	}
1883 }
1884 
DefineRoom(int nX1,int nY1,int nX2,int nY2,BOOL ForceHW)1885 static void DefineRoom(int nX1, int nY1, int nX2, int nY2, BOOL ForceHW)
1886 {
1887 	int i, j;
1888 
1889 	predungeon[nX1][nY1] = 67;
1890 	predungeon[nX1][nY2] = 69;
1891 	predungeon[nX2][nY1] = 66;
1892 	predungeon[nX2][nY2] = 65;
1893 
1894 	nRoomCnt++;
1895 	RoomList[nRoomCnt].nRoomx1 = nX1;
1896 	RoomList[nRoomCnt].nRoomx2 = nX2;
1897 	RoomList[nRoomCnt].nRoomy1 = nY1;
1898 	RoomList[nRoomCnt].nRoomy2 = nY2;
1899 
1900 	if (ForceHW == TRUE) {
1901 		for (i = nX1; i < nX2; i++) {
1902 			/// BUGFIX: Should loop j between nY1 and nY2 instead of always using nY1.
1903 			while (i < nY2) {
1904 				dflags[i][nY1] |= DLRG_PROTECTED;
1905 				i++;
1906 			}
1907 		}
1908 	}
1909 	for (i = nX1 + 1; i <= nX2 - 1; i++) {
1910 		predungeon[i][nY1] = 35;
1911 		predungeon[i][nY2] = 35;
1912 	}
1913 	nY2--;
1914 	for (j = nY1 + 1; j <= nY2; j++) {
1915 		predungeon[nX1][j] = 35;
1916 		predungeon[nX2][j] = 35;
1917 		for (i = nX1 + 1; i < nX2; i++) {
1918 			predungeon[i][j] = 46;
1919 		}
1920 	}
1921 }
1922 
CreateDoorType(int nX,int nY)1923 static void CreateDoorType(int nX, int nY)
1924 {
1925 	BOOL fDoneflag;
1926 
1927 	fDoneflag = FALSE;
1928 
1929 	if (predungeon[nX - 1][nY] == 68) {
1930 		fDoneflag = TRUE;
1931 	}
1932 	if (predungeon[nX + 1][nY] == 68) {
1933 		fDoneflag = TRUE;
1934 	}
1935 	if (predungeon[nX][nY - 1] == 68) {
1936 		fDoneflag = TRUE;
1937 	}
1938 	if (predungeon[nX][nY + 1] == 68) {
1939 		fDoneflag = TRUE;
1940 	}
1941 	if (predungeon[nX][nY] == 66 || predungeon[nX][nY] == 67 || predungeon[nX][nY] == 65 || predungeon[nX][nY] == 69) {
1942 		fDoneflag = TRUE;
1943 	}
1944 
1945 	if (!fDoneflag) {
1946 		predungeon[nX][nY] = 68;
1947 	}
1948 }
1949 
PlaceHallExt(int nX,int nY)1950 static void PlaceHallExt(int nX, int nY)
1951 {
1952 	if (predungeon[nX][nY] == 32) {
1953 		predungeon[nX][nY] = 44;
1954 	}
1955 }
1956 
AddHall(int nX1,int nY1,int nX2,int nY2,int nHd)1957 static void AddHall(int nX1, int nY1, int nX2, int nY2, int nHd)
1958 {
1959 	HALLNODE *p1, *p2;
1960 
1961 	if (pHallList == NULL) {
1962 		pHallList = (HALLNODE *)DiabloAllocPtr(sizeof(*pHallList));
1963 		pHallList->nHallx1 = nX1;
1964 		pHallList->nHally1 = nY1;
1965 		pHallList->nHallx2 = nX2;
1966 		pHallList->nHally2 = nY2;
1967 		pHallList->nHalldir = nHd;
1968 		pHallList->pNext = NULL;
1969 	} else {
1970 		p1 = (HALLNODE *)DiabloAllocPtr(sizeof(*pHallList));
1971 		p1->nHallx1 = nX1;
1972 		p1->nHally1 = nY1;
1973 		p1->nHallx2 = nX2;
1974 		p1->nHally2 = nY2;
1975 		p1->nHalldir = nHd;
1976 		p1->pNext = NULL;
1977 		p2 = pHallList;
1978 		while (p2->pNext != NULL) {
1979 			p2 = p2->pNext;
1980 		}
1981 		p2->pNext = p1;
1982 	}
1983 }
1984 
1985 /**
1986  * Draws a random room rectangle, and then subdivides the rest of the passed in rectangle into 4 and recurses.
1987  * @param nX1 Lower X boundary of the area to draw into.
1988  * @param nY1 Lower Y boundary of the area to draw into.
1989  * @param nX2 Upper X boundary of the area to draw into.
1990  * @param nY2 Upper Y boundary of the area to draw into.
1991  * @param nRDest The room number of the parent room this call was invoked for. Zero for empty
1992  * @param nHDir The direction of the hall from nRDest to this room.
1993  * @param ForceHW If set, nH and nW are used for room size instead of random values.
1994  * @param nH Height of the room, if ForceHW is set.
1995  * @param nW Width of the room, if ForceHW is set.
1996  */
CreateRoom(int nX1,int nY1,int nX2,int nY2,int nRDest,int nHDir,BOOL ForceHW,int nH,int nW)1997 static void CreateRoom(int nX1, int nY1, int nX2, int nY2, int nRDest, int nHDir, BOOL ForceHW, int nH, int nW)
1998 {
1999 	int nAw, nAh, nRw, nRh, nRx1, nRy1, nRx2, nRy2, nHw, nHh, nHx1, nHy1, nHx2, nHy2, nRid;
2000 
2001 	if (nRoomCnt >= 80) {
2002 		return;
2003 	}
2004 
2005 	nAw = nX2 - nX1;
2006 	nAh = nY2 - nY1;
2007 	if (nAw < Area_Min || nAh < Area_Min) {
2008 		return;
2009 	}
2010 
2011 	if (nAw > Room_Max) {
2012 		nRw = random_(0, Room_Max - Room_Min) + Room_Min;
2013 	} else if (nAw > Room_Min) {
2014 		nRw = random_(0, nAw - Room_Min) + Room_Min;
2015 	} else {
2016 		nRw = nAw;
2017 	}
2018 	if (nAh > Room_Max) {
2019 		nRh = random_(0, Room_Max - Room_Min) + Room_Min;
2020 	} else if (nAh > Room_Min) {
2021 		nRh = random_(0, nAh - Room_Min) + Room_Min;
2022 	} else {
2023 		nRh = nAh;
2024 	}
2025 
2026 	if (ForceHW == TRUE) {
2027 		nRw = nW;
2028 		nRh = nH;
2029 	}
2030 
2031 	nRx1 = random_(0, nX2 - nX1) + nX1;
2032 	nRy1 = random_(0, nY2 - nY1) + nY1;
2033 	nRx2 = nRw + nRx1;
2034 	nRy2 = nRh + nRy1;
2035 	if (nRx2 > nX2) {
2036 		nRx2 = nX2;
2037 		nRx1 = nX2 - nRw;
2038 	}
2039 	if (nRy2 > nY2) {
2040 		nRy2 = nY2;
2041 		nRy1 = nY2 - nRh;
2042 	}
2043 
2044 	if (nRx1 >= 38) {
2045 		nRx1 = 38;
2046 	}
2047 	if (nRy1 >= 38) {
2048 		nRy1 = 38;
2049 	}
2050 	if (nRx1 <= 1) {
2051 		nRx1 = 1;
2052 	}
2053 	if (nRy1 <= 1) {
2054 		nRy1 = 1;
2055 	}
2056 	if (nRx2 >= 38) {
2057 		nRx2 = 38;
2058 	}
2059 	if (nRy2 >= 38) {
2060 		nRy2 = 38;
2061 	}
2062 	if (nRx2 <= 1) {
2063 		nRx2 = 1;
2064 	}
2065 	if (nRy2 <= 1) {
2066 		nRy2 = 1;
2067 	}
2068 	DefineRoom(nRx1, nRy1, nRx2, nRy2, ForceHW);
2069 
2070 	if (ForceHW == TRUE) {
2071 		nSx1 = nRx1 + 2;
2072 		nSy1 = nRy1 + 2;
2073 		nSx2 = nRx2;
2074 		nSy2 = nRy2;
2075 	}
2076 
2077 	nRid = nRoomCnt;
2078 	RoomList[nRid].nRoomDest = nRDest;
2079 
2080 	if (nRDest != 0) {
2081 		if (nHDir == 1) {
2082 			nHx1 = random_(0, nRx2 - nRx1 - 2) + nRx1 + 1;
2083 			nHy1 = nRy1;
2084 			nHw = RoomList[nRDest].nRoomx2 - RoomList[nRDest].nRoomx1 - 2;
2085 			nHx2 = random_(0, nHw) + RoomList[nRDest].nRoomx1 + 1;
2086 			nHy2 = RoomList[nRDest].nRoomy2;
2087 		}
2088 		if (nHDir == 3) {
2089 			nHx1 = random_(0, nRx2 - nRx1 - 2) + nRx1 + 1;
2090 			nHy1 = nRy2;
2091 			nHw = RoomList[nRDest].nRoomx2 - RoomList[nRDest].nRoomx1 - 2;
2092 			nHx2 = random_(0, nHw) + RoomList[nRDest].nRoomx1 + 1;
2093 			nHy2 = RoomList[nRDest].nRoomy1;
2094 		}
2095 		if (nHDir == 2) {
2096 			nHx1 = nRx2;
2097 			nHy1 = random_(0, nRy2 - nRy1 - 2) + nRy1 + 1;
2098 			nHx2 = RoomList[nRDest].nRoomx1;
2099 			nHh = RoomList[nRDest].nRoomy2 - RoomList[nRDest].nRoomy1 - 2;
2100 			nHy2 = random_(0, nHh) + RoomList[nRDest].nRoomy1 + 1;
2101 		}
2102 		if (nHDir == 4) {
2103 			nHx1 = nRx1;
2104 			nHy1 = random_(0, nRy2 - nRy1 - 2) + nRy1 + 1;
2105 			nHx2 = RoomList[nRDest].nRoomx2;
2106 			nHh = RoomList[nRDest].nRoomy2 - RoomList[nRDest].nRoomy1 - 2;
2107 			nHy2 = random_(0, nHh) + RoomList[nRDest].nRoomy1 + 1;
2108 		}
2109 		AddHall(nHx1, nHy1, nHx2, nHy2, nHDir);
2110 	}
2111 
2112 	if (nRh > nRw) {
2113 		CreateRoom(nX1 + 2, nY1 + 2, nRx1 - 2, nRy2 - 2, nRid, 2, 0, 0, 0);
2114 		CreateRoom(nRx2 + 2, nRy1 + 2, nX2 - 2, nY2 - 2, nRid, 4, 0, 0, 0);
2115 		CreateRoom(nX1 + 2, nRy2 + 2, nRx2 - 2, nY2 - 2, nRid, 1, 0, 0, 0);
2116 		CreateRoom(nRx1 + 2, nY1 + 2, nX2 - 2, nRy1 - 2, nRid, 3, 0, 0, 0);
2117 	} else {
2118 		CreateRoom(nX1 + 2, nY1 + 2, nRx2 - 2, nRy1 - 2, nRid, 3, 0, 0, 0);
2119 		CreateRoom(nRx1 + 2, nRy2 + 2, nX2 - 2, nY2 - 2, nRid, 1, 0, 0, 0);
2120 		CreateRoom(nX1 + 2, nRy1 + 2, nRx1 - 2, nY2 - 2, nRid, 2, 0, 0, 0);
2121 		CreateRoom(nRx2 + 2, nY1 + 2, nX2 - 2, nRy2 - 2, nRid, 4, 0, 0, 0);
2122 	}
2123 }
2124 
GetHall(int * nX1,int * nY1,int * nX2,int * nY2,int * nHd)2125 static void GetHall(int *nX1, int *nY1, int *nX2, int *nY2, int *nHd)
2126 {
2127 	HALLNODE *p1;
2128 
2129 	p1 = pHallList->pNext;
2130 	*nX1 = pHallList->nHallx1;
2131 	*nY1 = pHallList->nHally1;
2132 	*nX2 = pHallList->nHallx2;
2133 	*nY2 = pHallList->nHally2;
2134 	*nHd = pHallList->nHalldir;
2135 	MemFreeDbg(pHallList);
2136 	pHallList = p1;
2137 }
2138 
ConnectHall(int nX1,int nY1,int nX2,int nY2,int nHd)2139 static void ConnectHall(int nX1, int nY1, int nX2, int nY2, int nHd)
2140 {
2141 	int nCurrd, nDx, nDy, nRp, nOrigX1, nOrigY1, fMinusFlag, fPlusFlag;
2142 	BOOL fDoneflag, fInroom;
2143 
2144 	fDoneflag = FALSE;
2145 	fMinusFlag = random_(0, 100);
2146 	fPlusFlag = random_(0, 100);
2147 	nOrigX1 = nX1;
2148 	nOrigY1 = nY1;
2149 	CreateDoorType(nX1, nY1);
2150 	CreateDoorType(nX2, nY2);
2151 	nCurrd = nHd;
2152 	nX2 -= Dir_Xadd[nCurrd];
2153 	nY2 -= Dir_Yadd[nCurrd];
2154 	predungeon[nX2][nY2] = 44;
2155 	fInroom = FALSE;
2156 
2157 	while (!fDoneflag) {
2158 		if (nX1 >= 38 && nCurrd == 2) {
2159 			nCurrd = 4;
2160 		}
2161 		if (nY1 >= 38 && nCurrd == 3) {
2162 			nCurrd = 1;
2163 		}
2164 		if (nX1 <= 1 && nCurrd == 4) {
2165 			nCurrd = 2;
2166 		}
2167 		if (nY1 <= 1 && nCurrd == 1) {
2168 			nCurrd = 3;
2169 		}
2170 		if (predungeon[nX1][nY1] == 67 && (nCurrd == 1 || nCurrd == 4)) {
2171 			nCurrd = 2;
2172 		}
2173 		if (predungeon[nX1][nY1] == 66 && (nCurrd == 1 || nCurrd == 2)) {
2174 			nCurrd = 3;
2175 		}
2176 		if (predungeon[nX1][nY1] == 69 && (nCurrd == 4 || nCurrd == 3)) {
2177 			nCurrd = 1;
2178 		}
2179 		if (predungeon[nX1][nY1] == 65 && (nCurrd == 2 || nCurrd == 3)) {
2180 			nCurrd = 4;
2181 		}
2182 		nX1 += Dir_Xadd[nCurrd];
2183 		nY1 += Dir_Yadd[nCurrd];
2184 		if (predungeon[nX1][nY1] == 32) {
2185 			if (fInroom) {
2186 				CreateDoorType(nX1 - Dir_Xadd[nCurrd], nY1 - Dir_Yadd[nCurrd]);
2187 			} else {
2188 				if (fMinusFlag < 50) {
2189 					if (nCurrd != 1 && nCurrd != 3) {
2190 						PlaceHallExt(nX1, nY1 - 1);
2191 					} else {
2192 						PlaceHallExt(nX1 - 1, nY1);
2193 					}
2194 				}
2195 				if (fPlusFlag < 50) {
2196 					if (nCurrd != 1 && nCurrd != 3) {
2197 						PlaceHallExt(nX1, nY1 + 1);
2198 					} else {
2199 						PlaceHallExt(nX1 + 1, nY1);
2200 					}
2201 				}
2202 			}
2203 			predungeon[nX1][nY1] = 44;
2204 			fInroom = FALSE;
2205 		} else {
2206 			if (!fInroom && predungeon[nX1][nY1] == 35) {
2207 				CreateDoorType(nX1, nY1);
2208 			}
2209 			if (predungeon[nX1][nY1] != 44) {
2210 				fInroom = TRUE;
2211 			}
2212 		}
2213 		nDx = abs(nX2 - nX1);
2214 		nDy = abs(nY2 - nY1);
2215 		if (nDx > nDy) {
2216 			nRp = 2 * nDx;
2217 			if (nRp > 30) {
2218 				nRp = 30;
2219 			}
2220 			if (random_(0, 100) < nRp) {
2221 				if (nX2 <= nX1 || nX1 >= DMAXX) {
2222 					nCurrd = 4;
2223 				} else {
2224 					nCurrd = 2;
2225 				}
2226 			}
2227 		} else {
2228 			nRp = 5 * nDy;
2229 			if (nRp > 80) {
2230 				nRp = 80;
2231 			}
2232 			if (random_(0, 100) < nRp) {
2233 				if (nY2 <= nY1 || nY1 >= DMAXY) {
2234 					nCurrd = 1;
2235 				} else {
2236 					nCurrd = 3;
2237 				}
2238 			}
2239 		}
2240 		if (nDy < 10 && nX1 == nX2 && (nCurrd == 2 || nCurrd == 4)) {
2241 			if (nY2 <= nY1 || nY1 >= DMAXY) {
2242 				nCurrd = 1;
2243 			} else {
2244 				nCurrd = 3;
2245 			}
2246 		}
2247 		if (nDx < 10 && nY1 == nY2 && (nCurrd == 1 || nCurrd == 3)) {
2248 			if (nX2 <= nX1 || nX1 >= DMAXX) {
2249 				nCurrd = 4;
2250 			} else {
2251 				nCurrd = 2;
2252 			}
2253 		}
2254 		if (nDy == 1 && nDx > 1 && (nCurrd == 1 || nCurrd == 3)) {
2255 			if (nX2 <= nX1 || nX1 >= DMAXX) {
2256 				nCurrd = 4;
2257 			} else {
2258 				nCurrd = 2;
2259 			}
2260 		}
2261 		if (nDx == 1 && nDy > 1 && (nCurrd == 2 || nCurrd == 4)) {
2262 			if (nY2 <= nY1 || nX1 >= DMAXX) {
2263 				nCurrd = 1;
2264 			} else {
2265 				nCurrd = 3;
2266 			}
2267 		}
2268 		if (nDx == 0 && predungeon[nX1][nY1] != 32 && (nCurrd == 2 || nCurrd == 4)) {
2269 			if (nX2 <= nOrigX1 || nX1 >= DMAXX) {
2270 				nCurrd = 1;
2271 			} else {
2272 				nCurrd = 3;
2273 			}
2274 		}
2275 		if (nDy == 0 && predungeon[nX1][nY1] != 32 && (nCurrd == 1 || nCurrd == 3)) {
2276 			if (nY2 <= nOrigY1 || nY1 >= DMAXY) {
2277 				nCurrd = 4;
2278 			} else {
2279 				nCurrd = 2;
2280 			}
2281 		}
2282 		if (nX1 == nX2 && nY1 == nY2) {
2283 			fDoneflag = TRUE;
2284 		}
2285 	}
2286 }
2287 
DoPatternCheck(int i,int j)2288 static void DoPatternCheck(int i, int j)
2289 {
2290 	int k, l, x, y, nOk;
2291 
2292 	for (k = 0; Patterns[k][4] != 255; k++) {
2293 		x = i - 1;
2294 		y = j - 1;
2295 		nOk = 254;
2296 		for (l = 0; l < 9 && nOk == 254; l++) {
2297 			nOk = 255;
2298 			if (l == 3 || l == 6) {
2299 				y++;
2300 				x = i - 1;
2301 			}
2302 			if (x >= 0 && x < DMAXX && y >= 0 && y < DMAXY) {
2303 				switch (Patterns[k][l]) {
2304 				case 0:
2305 					nOk = 254;
2306 					break;
2307 				case 1:
2308 					if (predungeon[x][y] == 35) {
2309 						nOk = 254;
2310 					}
2311 					break;
2312 				case 2:
2313 					if (predungeon[x][y] == 46) {
2314 						nOk = 254;
2315 					}
2316 					break;
2317 				case 4:
2318 					if (predungeon[x][y] == 32) {
2319 						nOk = 254;
2320 					}
2321 					break;
2322 				case 3:
2323 					if (predungeon[x][y] == 68) {
2324 						nOk = 254;
2325 					}
2326 					break;
2327 				case 5:
2328 					if (predungeon[x][y] == 68 || predungeon[x][y] == 46) {
2329 						nOk = 254;
2330 					}
2331 					break;
2332 				case 6:
2333 					if (predungeon[x][y] == 68 || predungeon[x][y] == 35) {
2334 						nOk = 254;
2335 					}
2336 					break;
2337 				case 7:
2338 					if (predungeon[x][y] == 32 || predungeon[x][y] == 46) {
2339 						nOk = 254;
2340 					}
2341 					break;
2342 				case 8:
2343 					if (predungeon[x][y] == 68 || predungeon[x][y] == 35 || predungeon[x][y] == 46) {
2344 						nOk = 254;
2345 					}
2346 					break;
2347 				}
2348 			} else {
2349 				nOk = 254;
2350 			}
2351 			x++;
2352 		}
2353 		if (nOk == 254) {
2354 			dungeon[i][j] = Patterns[k][9];
2355 		}
2356 	}
2357 }
2358 
L2TileFix()2359 static void L2TileFix()
2360 {
2361 	int i, j;
2362 
2363 	for (j = 0; j < DMAXY; j++) {
2364 		for (i = 0; i < DMAXX; i++) {
2365 			if (dungeon[i][j] == 1 && dungeon[i][j + 1] == 3) {
2366 				dungeon[i][j + 1] = 1;
2367 			}
2368 			if (dungeon[i][j] == 3 && dungeon[i][j + 1] == 1) {
2369 				dungeon[i][j + 1] = 3;
2370 			}
2371 			if (dungeon[i][j] == 3 && dungeon[i + 1][j] == 7) {
2372 				dungeon[i + 1][j] = 3;
2373 			}
2374 			if (dungeon[i][j] == 2 && dungeon[i + 1][j] == 3) {
2375 				dungeon[i + 1][j] = 2;
2376 			}
2377 			if (dungeon[i][j] == 11 && dungeon[i + 1][j] == 14) {
2378 				dungeon[i + 1][j] = 16;
2379 			}
2380 		}
2381 	}
2382 }
2383 
DL2_Cont(BOOL x1f,BOOL y1f,BOOL x2f,BOOL y2f)2384 static BOOL DL2_Cont(BOOL x1f, BOOL y1f, BOOL x2f, BOOL y2f)
2385 {
2386 	if (x1f && x2f && y1f && y2f) {
2387 		return FALSE;
2388 	}
2389 	if (x1f && x2f && (y1f || y2f)) {
2390 		return TRUE;
2391 	}
2392 	if (y1f && y2f && (x1f || x2f)) {
2393 		return TRUE;
2394 	}
2395 
2396 	return FALSE;
2397 }
2398 
DL2_NumNoChar()2399 static int DL2_NumNoChar()
2400 {
2401 	int t, ii, jj;
2402 
2403 	t = 0;
2404 	for (jj = 0; jj < DMAXY; jj++) {
2405 		for (ii = 0; ii < DMAXX; ii++) {
2406 			if (predungeon[ii][jj] == 32) {
2407 				t++;
2408 			}
2409 		}
2410 	}
2411 
2412 	return t;
2413 }
2414 
DL2_DrawRoom(int x1,int y1,int x2,int y2)2415 static void DL2_DrawRoom(int x1, int y1, int x2, int y2)
2416 {
2417 	int ii, jj;
2418 
2419 	for (jj = y1; jj <= y2; jj++) {
2420 		for (ii = x1; ii <= x2; ii++) {
2421 			predungeon[ii][jj] = 46;
2422 		}
2423 	}
2424 	for (jj = y1; jj <= y2; jj++) {
2425 		predungeon[x1][jj] = 35;
2426 		predungeon[x2][jj] = 35;
2427 	}
2428 	for (ii = x1; ii <= x2; ii++) {
2429 		predungeon[ii][y1] = 35;
2430 		predungeon[ii][y2] = 35;
2431 	}
2432 }
2433 
DL2_KnockWalls(int x1,int y1,int x2,int y2)2434 static void DL2_KnockWalls(int x1, int y1, int x2, int y2)
2435 {
2436 	int ii, jj;
2437 
2438 	for (ii = x1 + 1; ii < x2; ii++) {
2439 		if (predungeon[ii][y1 - 1] == 46 && predungeon[ii][y1 + 1] == 46) {
2440 			predungeon[ii][y1] = 46;
2441 		}
2442 		if (predungeon[ii][y2 - 1] == 46 && predungeon[ii][y2 + 1] == 46) {
2443 			predungeon[ii][y2] = 46;
2444 		}
2445 		if (predungeon[ii][y1 - 1] == 68) {
2446 			predungeon[ii][y1 - 1] = 46;
2447 		}
2448 		if (predungeon[ii][y2 + 1] == 68) {
2449 			predungeon[ii][y2 + 1] = 46;
2450 		}
2451 	}
2452 	for (jj = y1 + 1; jj < y2; jj++) {
2453 		if (predungeon[x1 - 1][jj] == 46 && predungeon[x1 + 1][jj] == 46) {
2454 			predungeon[x1][jj] = 46;
2455 		}
2456 		if (predungeon[x2 - 1][jj] == 46 && predungeon[x2 + 1][jj] == 46) {
2457 			predungeon[x2][jj] = 46;
2458 		}
2459 		if (predungeon[x1 - 1][jj] == 68) {
2460 			predungeon[x1 - 1][jj] = 46;
2461 		}
2462 		if (predungeon[x2 + 1][jj] == 68) {
2463 			predungeon[x2 + 1][jj] = 46;
2464 		}
2465 	}
2466 }
2467 
DL2_FillVoids()2468 static BOOL DL2_FillVoids()
2469 {
2470 	int ii, jj, xx, yy, x1, x2, y1, y2;
2471 	BOOL xf1, xf2, yf1, yf2;
2472 	int to;
2473 
2474 	to = 0;
2475 	while (DL2_NumNoChar() > 700 && to < 100) {
2476 		xx = random_(0, 38) + 1;
2477 		yy = random_(0, 38) + 1;
2478 		if (predungeon[xx][yy] != 35) {
2479 			continue;
2480 		}
2481 		xf1 = xf2 = yf1 = yf2 = FALSE;
2482 		if (predungeon[xx - 1][yy] == 32 && predungeon[xx + 1][yy] == 46) {
2483 			if (predungeon[xx + 1][yy - 1] == 46
2484 			    && predungeon[xx + 1][yy + 1] == 46
2485 			    && predungeon[xx - 1][yy - 1] == 32
2486 			    && predungeon[xx - 1][yy + 1] == 32) {
2487 				xf1 = yf1 = yf2 = TRUE;
2488 			}
2489 		} else if (predungeon[xx + 1][yy] == 32 && predungeon[xx - 1][yy] == 46) {
2490 			if (predungeon[xx - 1][yy - 1] == 46
2491 			    && predungeon[xx - 1][yy + 1] == 46
2492 			    && predungeon[xx + 1][yy - 1] == 32
2493 			    && predungeon[xx + 1][yy + 1] == 32) {
2494 				xf2 = yf1 = yf2 = TRUE;
2495 			}
2496 		} else if (predungeon[xx][yy - 1] == 32 && predungeon[xx][yy + 1] == 46) {
2497 			if (predungeon[xx - 1][yy + 1] == 46
2498 			    && predungeon[xx + 1][yy + 1] == 46
2499 			    && predungeon[xx - 1][yy - 1] == 32
2500 			    && predungeon[xx + 1][yy - 1] == 32) {
2501 				yf1 = xf1 = xf2 = TRUE;
2502 			}
2503 		} else if (predungeon[xx][yy + 1] == 32 && predungeon[xx][yy - 1] == 46) {
2504 			if (predungeon[xx - 1][yy - 1] == 46
2505 			    && predungeon[xx + 1][yy - 1] == 46
2506 			    && predungeon[xx - 1][yy + 1] == 32
2507 			    && predungeon[xx + 1][yy + 1] == 32) {
2508 				yf2 = xf1 = xf2 = TRUE;
2509 			}
2510 		}
2511 		if (DL2_Cont(xf1, yf1, xf2, yf2)) {
2512 			if (xf1) {
2513 				x1 = xx - 1;
2514 			} else {
2515 				x1 = xx;
2516 			}
2517 			if (xf2) {
2518 				x2 = xx + 1;
2519 			} else {
2520 				x2 = xx;
2521 			}
2522 			if (yf1) {
2523 				y1 = yy - 1;
2524 			} else {
2525 				y1 = yy;
2526 			}
2527 			if (yf2) {
2528 				y2 = yy + 1;
2529 			} else {
2530 				y2 = yy;
2531 			}
2532 			if (!xf1) {
2533 				while (yf1 || yf2) {
2534 					if (y1 == 0) {
2535 						yf1 = FALSE;
2536 					}
2537 					if (y2 == DMAXY - 1) {
2538 						yf2 = FALSE;
2539 					}
2540 					if (y2 - y1 >= 14) {
2541 						yf1 = FALSE;
2542 						yf2 = FALSE;
2543 					}
2544 					if (yf1) {
2545 						y1--;
2546 					}
2547 					if (yf2) {
2548 						y2++;
2549 					}
2550 					if (predungeon[x2][y1] != 32) {
2551 						yf1 = FALSE;
2552 					}
2553 					if (predungeon[x2][y2] != 32) {
2554 						yf2 = FALSE;
2555 					}
2556 				}
2557 				y1 += 2;
2558 				y2 -= 2;
2559 				if (y2 - y1 > 5) {
2560 					while (xf2) {
2561 						if (x2 == 39) {
2562 							xf2 = FALSE;
2563 						}
2564 						if (x2 - x1 >= 12) {
2565 							xf2 = FALSE;
2566 						}
2567 						for (jj = y1; jj <= y2; jj++) {
2568 							if (predungeon[x2][jj] != 32) {
2569 								xf2 = FALSE;
2570 							}
2571 						}
2572 						if (xf2) {
2573 							x2++;
2574 						}
2575 					}
2576 					x2 -= 2;
2577 					if (x2 - x1 > 5) {
2578 						DL2_DrawRoom(x1, y1, x2, y2);
2579 						DL2_KnockWalls(x1, y1, x2, y2);
2580 					}
2581 				}
2582 			} else if (!xf2) {
2583 				while (yf1 || yf2) {
2584 					if (y1 == 0) {
2585 						yf1 = FALSE;
2586 					}
2587 					if (y2 == DMAXY - 1) {
2588 						yf2 = FALSE;
2589 					}
2590 					if (y2 - y1 >= 14) {
2591 						yf1 = FALSE;
2592 						yf2 = FALSE;
2593 					}
2594 					if (yf1) {
2595 						y1--;
2596 					}
2597 					if (yf2) {
2598 						y2++;
2599 					}
2600 					if (predungeon[x1][y1] != 32) {
2601 						yf1 = FALSE;
2602 					}
2603 					if (predungeon[x1][y2] != 32) {
2604 						yf2 = FALSE;
2605 					}
2606 				}
2607 				y1 += 2;
2608 				y2 -= 2;
2609 				if (y2 - y1 > 5) {
2610 					while (xf1) {
2611 						if (x1 == 0) {
2612 							xf1 = FALSE;
2613 						}
2614 						if (x2 - x1 >= 12) {
2615 							xf1 = FALSE;
2616 						}
2617 						for (jj = y1; jj <= y2; jj++) {
2618 							if (predungeon[x1][jj] != 32) {
2619 								xf1 = FALSE;
2620 							}
2621 						}
2622 						if (xf1) {
2623 							x1--;
2624 						}
2625 					}
2626 					x1 += 2;
2627 					if (x2 - x1 > 5) {
2628 						DL2_DrawRoom(x1, y1, x2, y2);
2629 						DL2_KnockWalls(x1, y1, x2, y2);
2630 					}
2631 				}
2632 			} else if (!yf1) {
2633 				while (xf1 || xf2) {
2634 					if (x1 == 0) {
2635 						xf1 = FALSE;
2636 					}
2637 					if (x2 == DMAXX - 1) {
2638 						xf2 = FALSE;
2639 					}
2640 					if (x2 - x1 >= 14) {
2641 						xf1 = FALSE;
2642 						xf2 = FALSE;
2643 					}
2644 					if (xf1) {
2645 						x1--;
2646 					}
2647 					if (xf2) {
2648 						x2++;
2649 					}
2650 					if (predungeon[x1][y2] != 32) {
2651 						xf1 = FALSE;
2652 					}
2653 					if (predungeon[x2][y2] != 32) {
2654 						xf2 = FALSE;
2655 					}
2656 				}
2657 				x1 += 2;
2658 				x2 -= 2;
2659 				if (x2 - x1 > 5) {
2660 					while (yf2) {
2661 						if (y2 == DMAXY - 1) {
2662 							yf2 = FALSE;
2663 						}
2664 						if (y2 - y1 >= 12) {
2665 							yf2 = FALSE;
2666 						}
2667 						for (ii = x1; ii <= x2; ii++) {
2668 							if (predungeon[ii][y2] != 32) {
2669 								yf2 = FALSE;
2670 							}
2671 						}
2672 						if (yf2) {
2673 							y2++;
2674 						}
2675 					}
2676 					y2 -= 2;
2677 					if (y2 - y1 > 5) {
2678 						DL2_DrawRoom(x1, y1, x2, y2);
2679 						DL2_KnockWalls(x1, y1, x2, y2);
2680 					}
2681 				}
2682 			} else if (!yf2) {
2683 				while (xf1 || xf2) {
2684 					if (x1 == 0) {
2685 						xf1 = FALSE;
2686 					}
2687 					if (x2 == DMAXX - 1) {
2688 						xf2 = FALSE;
2689 					}
2690 					if (x2 - x1 >= 14) {
2691 						xf1 = FALSE;
2692 						xf2 = FALSE;
2693 					}
2694 					if (xf1) {
2695 						x1--;
2696 					}
2697 					if (xf2) {
2698 						x2++;
2699 					}
2700 					if (predungeon[x1][y1] != 32) {
2701 						xf1 = FALSE;
2702 					}
2703 					if (predungeon[x2][y1] != 32) {
2704 						xf2 = FALSE;
2705 					}
2706 				}
2707 				x1 += 2;
2708 				x2 -= 2;
2709 				if (x2 - x1 > 5) {
2710 					while (yf1) {
2711 						if (y1 == 0) {
2712 							yf1 = FALSE;
2713 						}
2714 						if (y2 - y1 >= 12) {
2715 							yf1 = FALSE;
2716 						}
2717 						for (ii = x1; ii <= x2; ii++) {
2718 							if (predungeon[ii][y1] != 32) {
2719 								yf1 = FALSE;
2720 							}
2721 						}
2722 						if (yf1) {
2723 							y1--;
2724 						}
2725 					}
2726 					y1 += 2;
2727 					if (y2 - y1 > 5) {
2728 						DL2_DrawRoom(x1, y1, x2, y2);
2729 						DL2_KnockWalls(x1, y1, x2, y2);
2730 					}
2731 				}
2732 			}
2733 		}
2734 		to++;
2735 	}
2736 
2737 	return DL2_NumNoChar() <= 700;
2738 }
2739 
CreateDungeon()2740 static BOOL CreateDungeon()
2741 {
2742 	int i, j, nHx1, nHy1, nHx2, nHy2, nHd, ForceH, ForceW;
2743 	BOOL ForceHW;
2744 
2745 	ForceW = 0;
2746 	ForceH = 0;
2747 	ForceHW = FALSE;
2748 
2749 	switch (currlevel) {
2750 	case 5:
2751 		if (quests[Q_BLOOD]._qactive != QUEST_NOTAVAIL) {
2752 			ForceHW = TRUE;
2753 			ForceH = 20;
2754 			ForceW = 14;
2755 		}
2756 		break;
2757 	case 6:
2758 		if (quests[Q_SCHAMB]._qactive != QUEST_NOTAVAIL) {
2759 			ForceHW = TRUE;
2760 			ForceW = 10;
2761 			ForceH = 10;
2762 		}
2763 		break;
2764 	case 7:
2765 		if (quests[Q_BLIND]._qactive != QUEST_NOTAVAIL) {
2766 			ForceHW = TRUE;
2767 			ForceW = 15;
2768 			ForceH = 15;
2769 		}
2770 		break;
2771 	case 8:
2772 		break;
2773 	}
2774 
2775 	CreateRoom(2, 2, DMAXX - 1, DMAXY - 1, 0, 0, ForceHW, ForceH, ForceW);
2776 
2777 	while (pHallList != NULL) {
2778 		GetHall(&nHx1, &nHy1, &nHx2, &nHy2, &nHd);
2779 		ConnectHall(nHx1, nHy1, nHx2, nHy2, nHd);
2780 	}
2781 
2782 	for (j = 0; j < DMAXY; j++) {     /// BUGFIX: change '<=' to '<' (fixed)
2783 		for (i = 0; i < DMAXX; i++) { /// BUGFIX: change '<=' to '<' (fixed)
2784 			if (predungeon[i][j] == 67) {
2785 				predungeon[i][j] = 35;
2786 			}
2787 			if (predungeon[i][j] == 66) {
2788 				predungeon[i][j] = 35;
2789 			}
2790 			if (predungeon[i][j] == 69) {
2791 				predungeon[i][j] = 35;
2792 			}
2793 			if (predungeon[i][j] == 65) {
2794 				predungeon[i][j] = 35;
2795 			}
2796 			if (predungeon[i][j] == 44) {
2797 				predungeon[i][j] = 46;
2798 				if (predungeon[i - 1][j - 1] == 32) {
2799 					predungeon[i - 1][j - 1] = 35;
2800 				}
2801 				if (predungeon[i - 1][j] == 32) {
2802 					predungeon[i - 1][j] = 35;
2803 				}
2804 				if (predungeon[i - 1][1 + j] == 32) {
2805 					predungeon[i - 1][1 + j] = 35;
2806 				}
2807 				if (predungeon[i + 1][j - 1] == 32) {
2808 					predungeon[i + 1][j - 1] = 35;
2809 				}
2810 				if (predungeon[i + 1][j] == 32) {
2811 					predungeon[i + 1][j] = 35;
2812 				}
2813 				if (predungeon[i + 1][1 + j] == 32) {
2814 					predungeon[i + 1][1 + j] = 35;
2815 				}
2816 				if (predungeon[i][j - 1] == 32) {
2817 					predungeon[i][j - 1] = 35;
2818 				}
2819 				if (predungeon[i][j + 1] == 32) {
2820 					predungeon[i][j + 1] = 35;
2821 				}
2822 			}
2823 		}
2824 	}
2825 
2826 	if (!DL2_FillVoids()) {
2827 		return FALSE;
2828 	}
2829 
2830 	for (j = 0; j < DMAXY; j++) {
2831 		for (i = 0; i < DMAXX; i++) {
2832 			DoPatternCheck(i, j);
2833 		}
2834 	}
2835 
2836 	return TRUE;
2837 }
2838 
DRLG_L2Pass3()2839 static void DRLG_L2Pass3()
2840 {
2841 	int i, j, xx, yy;
2842 	long v1, v2, v3, v4, lv;
2843 	WORD *MegaTiles;
2844 
2845 	lv = 12 - 1;
2846 
2847 	MegaTiles = (WORD *)&pMegaTiles[lv * 8];
2848 	v1 = SDL_SwapLE16(*(MegaTiles + 0)) + 1;
2849 	v2 = SDL_SwapLE16(*(MegaTiles + 1)) + 1;
2850 	v3 = SDL_SwapLE16(*(MegaTiles + 2)) + 1;
2851 	v4 = SDL_SwapLE16(*(MegaTiles + 3)) + 1;
2852 
2853 	for (j = 0; j < MAXDUNY; j += 2) {
2854 		for (i = 0; i < MAXDUNX; i += 2) {
2855 			dPiece[i][j] = v1;
2856 			dPiece[i + 1][j] = v2;
2857 			dPiece[i][j + 1] = v3;
2858 			dPiece[i + 1][j + 1] = v4;
2859 		}
2860 	}
2861 
2862 	yy = 16;
2863 	for (j = 0; j < DMAXY; j++) {
2864 		xx = 16;
2865 		for (i = 0; i < DMAXX; i++) {
2866 			lv = dungeon[i][j] - 1;
2867 			MegaTiles = (WORD *)&pMegaTiles[lv * 8];
2868 			v1 = SDL_SwapLE16(*(MegaTiles + 0)) + 1;
2869 			v2 = SDL_SwapLE16(*(MegaTiles + 1)) + 1;
2870 			v3 = SDL_SwapLE16(*(MegaTiles + 2)) + 1;
2871 			v4 = SDL_SwapLE16(*(MegaTiles + 3)) + 1;
2872 			dPiece[xx][yy] = v1;
2873 			dPiece[xx + 1][yy] = v2;
2874 			dPiece[xx][yy + 1] = v3;
2875 			dPiece[xx + 1][yy + 1] = v4;
2876 			xx += 2;
2877 		}
2878 		yy += 2;
2879 	}
2880 }
2881 
DRLG_L2FTVR(int i,int j,int x,int y,int d)2882 static void DRLG_L2FTVR(int i, int j, int x, int y, int d)
2883 {
2884 	if (dTransVal[x][y] != 0 || dungeon[i][j] != 3) {
2885 		if (d == 1) {
2886 			dTransVal[x][y] = TransVal;
2887 			dTransVal[x][y + 1] = TransVal;
2888 		}
2889 		if (d == 2) {
2890 			dTransVal[x + 1][y] = TransVal;
2891 			dTransVal[x + 1][y + 1] = TransVal;
2892 		}
2893 		if (d == 3) {
2894 			dTransVal[x][y] = TransVal;
2895 			dTransVal[x + 1][y] = TransVal;
2896 		}
2897 		if (d == 4) {
2898 			dTransVal[x][y + 1] = TransVal;
2899 			dTransVal[x + 1][y + 1] = TransVal;
2900 		}
2901 		if (d == 5) {
2902 			dTransVal[x + 1][y + 1] = TransVal;
2903 		}
2904 		if (d == 6) {
2905 			dTransVal[x][y + 1] = TransVal;
2906 		}
2907 		if (d == 7) {
2908 			dTransVal[x + 1][y] = TransVal;
2909 		}
2910 		if (d == 8) {
2911 			dTransVal[x][y] = TransVal;
2912 		}
2913 	} else {
2914 		dTransVal[x][y] = TransVal;
2915 		dTransVal[x + 1][y] = TransVal;
2916 		dTransVal[x][y + 1] = TransVal;
2917 		dTransVal[x + 1][y + 1] = TransVal;
2918 		DRLG_L2FTVR(i + 1, j, x + 2, y, 1);
2919 		DRLG_L2FTVR(i - 1, j, x - 2, y, 2);
2920 		DRLG_L2FTVR(i, j + 1, x, y + 2, 3);
2921 		DRLG_L2FTVR(i, j - 1, x, y - 2, 4);
2922 		DRLG_L2FTVR(i - 1, j - 1, x - 2, y - 2, 5);
2923 		DRLG_L2FTVR(i + 1, j - 1, x + 2, y - 2, 6);
2924 		DRLG_L2FTVR(i - 1, j + 1, x - 2, y + 2, 7);
2925 		DRLG_L2FTVR(i + 1, j + 1, x + 2, y + 2, 8);
2926 	}
2927 }
2928 
DRLG_L2FloodTVal()2929 static void DRLG_L2FloodTVal()
2930 {
2931 	int i, j, xx, yy;
2932 
2933 	yy = 16;
2934 	for (j = 0; j < DMAXY; j++) {
2935 		xx = 16;
2936 		for (i = 0; i < DMAXX; i++) {
2937 			if (dungeon[i][j] == 3 && dTransVal[xx][yy] == 0) {
2938 				DRLG_L2FTVR(i, j, xx, yy, 0);
2939 				TransVal++;
2940 			}
2941 			xx += 2;
2942 		}
2943 		yy += 2;
2944 	}
2945 }
2946 
DRLG_L2TransFix()2947 static void DRLG_L2TransFix()
2948 {
2949 	int i, j, xx, yy;
2950 
2951 	yy = 16;
2952 	for (j = 0; j < DMAXY; j++) {
2953 		xx = 16;
2954 		for (i = 0; i < DMAXX; i++) {
2955 			if (dungeon[i][j] == 14 && dungeon[i][j - 1] == 10) {
2956 				dTransVal[xx + 1][yy] = dTransVal[xx][yy];
2957 				dTransVal[xx + 1][yy + 1] = dTransVal[xx][yy];
2958 			}
2959 			if (dungeon[i][j] == 15 && dungeon[i + 1][j] == 11) {
2960 				dTransVal[xx][yy + 1] = dTransVal[xx][yy];
2961 				dTransVal[xx + 1][yy + 1] = dTransVal[xx][yy];
2962 			}
2963 			if (dungeon[i][j] == 10) {
2964 				dTransVal[xx + 1][yy] = dTransVal[xx][yy];
2965 				dTransVal[xx + 1][yy + 1] = dTransVal[xx][yy];
2966 			}
2967 			if (dungeon[i][j] == 11) {
2968 				dTransVal[xx][yy + 1] = dTransVal[xx][yy];
2969 				dTransVal[xx + 1][yy + 1] = dTransVal[xx][yy];
2970 			}
2971 			if (dungeon[i][j] == 16) {
2972 				dTransVal[xx + 1][yy] = dTransVal[xx][yy];
2973 				dTransVal[xx][yy + 1] = dTransVal[xx][yy];
2974 				dTransVal[xx + 1][yy + 1] = dTransVal[xx][yy];
2975 			}
2976 			xx += 2;
2977 		}
2978 		yy += 2;
2979 	}
2980 }
2981 
L2DirtFix()2982 static void L2DirtFix()
2983 {
2984 	int i, j;
2985 
2986 	for (j = 0; j < DMAXY; j++) {
2987 		for (i = 0; i < DMAXX; i++) {
2988 			if (dungeon[i][j] == 13 && dungeon[i + 1][j] != 11) {
2989 				dungeon[i][j] = 146;
2990 			}
2991 			if (dungeon[i][j] == 11 && dungeon[i + 1][j] != 11) {
2992 				dungeon[i][j] = 144;
2993 			}
2994 			if (dungeon[i][j] == 15 && dungeon[i + 1][j] != 11) {
2995 				dungeon[i][j] = 148;
2996 			}
2997 			if (dungeon[i][j] == 10 && dungeon[i][j + 1] != 10) {
2998 				dungeon[i][j] = 143;
2999 			}
3000 			if (dungeon[i][j] == 13 && dungeon[i][j + 1] != 10) {
3001 				dungeon[i][j] = 146;
3002 			}
3003 			if (dungeon[i][j] == 14 && dungeon[i][j + 1] != 15) {
3004 				dungeon[i][j] = 147;
3005 			}
3006 		}
3007 	}
3008 }
3009 
L2LockoutFix()3010 void L2LockoutFix()
3011 {
3012 	int i, j;
3013 	BOOL doorok;
3014 
3015 	for (j = 0; j < DMAXY; j++) {
3016 		for (i = 0; i < DMAXX; i++) {
3017 			if (dungeon[i][j] == 4 && dungeon[i - 1][j] != 3) {
3018 				dungeon[i][j] = 1;
3019 			}
3020 			if (dungeon[i][j] == 5 && dungeon[i][j - 1] != 3) {
3021 				dungeon[i][j] = 2;
3022 			}
3023 		}
3024 	}
3025 	for (j = 1; j < DMAXY - 1; j++) {
3026 		for (i = 1; i < DMAXX - 1; i++) {
3027 			if (dflags[i][j] & DLRG_PROTECTED) {
3028 				continue;
3029 			}
3030 			if ((dungeon[i][j] == 2 || dungeon[i][j] == 5) && dungeon[i][j - 1] == 3 && dungeon[i][j + 1] == 3) {
3031 				doorok = FALSE;
3032 				while (1) {
3033 					if (dungeon[i][j] != 2 && dungeon[i][j] != 5) {
3034 						break;
3035 					}
3036 					if (dungeon[i][j - 1] != 3 || dungeon[i][j + 1] != 3) {
3037 						break;
3038 					}
3039 					if (dungeon[i][j] == 5) {
3040 						doorok = TRUE;
3041 					}
3042 					i++;
3043 				}
3044 				if (!doorok && !(dflags[i - 1][j] & DLRG_PROTECTED)) {
3045 					dungeon[i - 1][j] = 5;
3046 				}
3047 			}
3048 		}
3049 	}
3050 	for (j = 1; j < DMAXX - 1; j++) { /* check: might be flipped */
3051 		for (i = 1; i < DMAXY - 1; i++) {
3052 			if (dflags[j][i] & DLRG_PROTECTED) {
3053 				continue;
3054 			}
3055 			if ((dungeon[j][i] == 1 || dungeon[j][i] == 4) && dungeon[j - 1][i] == 3 && dungeon[j + 1][i] == 3) {
3056 				doorok = FALSE;
3057 				while (1) {
3058 					if (dungeon[j][i] != 1 && dungeon[j][i] != 4) {
3059 						break;
3060 					}
3061 					if (dungeon[j - 1][i] != 3 || dungeon[j + 1][i] != 3) {
3062 						break;
3063 					}
3064 					if (dungeon[j][i] == 4) {
3065 						doorok = TRUE;
3066 					}
3067 					i++;
3068 				}
3069 				if (!doorok && !(dflags[j][i - 1] & DLRG_PROTECTED)) {
3070 					dungeon[j][i - 1] = 4;
3071 				}
3072 			}
3073 		}
3074 	}
3075 }
3076 
L2DoorFix()3077 void L2DoorFix()
3078 {
3079 	int i, j;
3080 
3081 	for (j = 1; j < DMAXY; j++) {
3082 		for (i = 1; i < DMAXX; i++) {
3083 			if (dungeon[i][j] == 4 && dungeon[i][j - 1] == 3) {
3084 				dungeon[i][j] = 7;
3085 			}
3086 			if (dungeon[i][j] == 5 && dungeon[i - 1][j] == 3) {
3087 				dungeon[i][j] = 9;
3088 			}
3089 		}
3090 	}
3091 }
3092 
DRLG_L2(int entry)3093 static void DRLG_L2(int entry)
3094 {
3095 	int i, j;
3096 	BOOL doneflag;
3097 
3098 	doneflag = FALSE;
3099 	while (!doneflag) {
3100 		nRoomCnt = 0;
3101 		InitDungeon();
3102 		DRLG_InitTrans();
3103 		if (!CreateDungeon()) {
3104 			continue;
3105 		}
3106 		L2TileFix();
3107 		if (setloadflag) {
3108 			DRLG_L2SetRoom(nSx1, nSy1);
3109 		}
3110 		DRLG_L2FloodTVal();
3111 		DRLG_L2TransFix();
3112 		if (entry == ENTRY_MAIN) {
3113 			doneflag = DRLG_L2PlaceMiniSet(USTAIRS, 1, 1, -1, -1, TRUE, 0);
3114 			if (doneflag) {
3115 				doneflag = DRLG_L2PlaceMiniSet(DSTAIRS, 1, 1, -1, -1, FALSE, 1);
3116 				if (doneflag && currlevel == 5) {
3117 					doneflag = DRLG_L2PlaceMiniSet(WARPSTAIRS, 1, 1, -1, -1, FALSE, 6);
3118 				}
3119 			}
3120 			ViewY -= 2;
3121 		} else if (entry == ENTRY_PREV) {
3122 			doneflag = DRLG_L2PlaceMiniSet(USTAIRS, 1, 1, -1, -1, FALSE, 0);
3123 			if (doneflag) {
3124 				doneflag = DRLG_L2PlaceMiniSet(DSTAIRS, 1, 1, -1, -1, TRUE, 1);
3125 				if (doneflag && currlevel == 5) {
3126 					doneflag = DRLG_L2PlaceMiniSet(WARPSTAIRS, 1, 1, -1, -1, FALSE, 6);
3127 				}
3128 			}
3129 			ViewX--;
3130 		} else {
3131 			doneflag = DRLG_L2PlaceMiniSet(USTAIRS, 1, 1, -1, -1, FALSE, 0);
3132 			if (doneflag) {
3133 				doneflag = DRLG_L2PlaceMiniSet(DSTAIRS, 1, 1, -1, -1, FALSE, 1);
3134 				if (doneflag && currlevel == 5) {
3135 					doneflag = DRLG_L2PlaceMiniSet(WARPSTAIRS, 1, 1, -1, -1, TRUE, 6);
3136 				}
3137 			}
3138 			ViewY -= 2;
3139 		}
3140 	}
3141 
3142 	L2LockoutFix();
3143 	L2DoorFix();
3144 	L2DirtFix();
3145 
3146 	DRLG_PlaceThemeRooms(6, 10, 3, 0, 0);
3147 	DRLG_L2PlaceRndSet(CTRDOOR1, 100);
3148 	DRLG_L2PlaceRndSet(CTRDOOR2, 100);
3149 	DRLG_L2PlaceRndSet(CTRDOOR3, 100);
3150 	DRLG_L2PlaceRndSet(CTRDOOR4, 100);
3151 	DRLG_L2PlaceRndSet(CTRDOOR5, 100);
3152 	DRLG_L2PlaceRndSet(CTRDOOR6, 100);
3153 	DRLG_L2PlaceRndSet(CTRDOOR7, 100);
3154 	DRLG_L2PlaceRndSet(CTRDOOR8, 100);
3155 	DRLG_L2PlaceRndSet(VARCH33, 100);
3156 	DRLG_L2PlaceRndSet(VARCH34, 100);
3157 	DRLG_L2PlaceRndSet(VARCH35, 100);
3158 	DRLG_L2PlaceRndSet(VARCH36, 100);
3159 	DRLG_L2PlaceRndSet(VARCH37, 100);
3160 	DRLG_L2PlaceRndSet(VARCH38, 100);
3161 	DRLG_L2PlaceRndSet(VARCH39, 100);
3162 	DRLG_L2PlaceRndSet(VARCH40, 100);
3163 	DRLG_L2PlaceRndSet(VARCH1, 100);
3164 	DRLG_L2PlaceRndSet(VARCH2, 100);
3165 	DRLG_L2PlaceRndSet(VARCH3, 100);
3166 	DRLG_L2PlaceRndSet(VARCH4, 100);
3167 	DRLG_L2PlaceRndSet(VARCH5, 100);
3168 	DRLG_L2PlaceRndSet(VARCH6, 100);
3169 	DRLG_L2PlaceRndSet(VARCH7, 100);
3170 	DRLG_L2PlaceRndSet(VARCH8, 100);
3171 	DRLG_L2PlaceRndSet(VARCH9, 100);
3172 	DRLG_L2PlaceRndSet(VARCH10, 100);
3173 	DRLG_L2PlaceRndSet(VARCH11, 100);
3174 	DRLG_L2PlaceRndSet(VARCH12, 100);
3175 	DRLG_L2PlaceRndSet(VARCH13, 100);
3176 	DRLG_L2PlaceRndSet(VARCH14, 100);
3177 	DRLG_L2PlaceRndSet(VARCH15, 100);
3178 	DRLG_L2PlaceRndSet(VARCH16, 100);
3179 	DRLG_L2PlaceRndSet(VARCH17, 100);
3180 	DRLG_L2PlaceRndSet(VARCH18, 100);
3181 	DRLG_L2PlaceRndSet(VARCH19, 100);
3182 	DRLG_L2PlaceRndSet(VARCH20, 100);
3183 	DRLG_L2PlaceRndSet(VARCH21, 100);
3184 	DRLG_L2PlaceRndSet(VARCH22, 100);
3185 	DRLG_L2PlaceRndSet(VARCH23, 100);
3186 	DRLG_L2PlaceRndSet(VARCH24, 100);
3187 	DRLG_L2PlaceRndSet(VARCH25, 100);
3188 	DRLG_L2PlaceRndSet(VARCH26, 100);
3189 	DRLG_L2PlaceRndSet(VARCH27, 100);
3190 	DRLG_L2PlaceRndSet(VARCH28, 100);
3191 	DRLG_L2PlaceRndSet(VARCH29, 100);
3192 	DRLG_L2PlaceRndSet(VARCH30, 100);
3193 	DRLG_L2PlaceRndSet(VARCH31, 100);
3194 	DRLG_L2PlaceRndSet(VARCH32, 100);
3195 	DRLG_L2PlaceRndSet(HARCH1, 100);
3196 	DRLG_L2PlaceRndSet(HARCH2, 100);
3197 	DRLG_L2PlaceRndSet(HARCH3, 100);
3198 	DRLG_L2PlaceRndSet(HARCH4, 100);
3199 	DRLG_L2PlaceRndSet(HARCH5, 100);
3200 	DRLG_L2PlaceRndSet(HARCH6, 100);
3201 	DRLG_L2PlaceRndSet(HARCH7, 100);
3202 	DRLG_L2PlaceRndSet(HARCH8, 100);
3203 	DRLG_L2PlaceRndSet(HARCH9, 100);
3204 	DRLG_L2PlaceRndSet(HARCH10, 100);
3205 	DRLG_L2PlaceRndSet(HARCH11, 100);
3206 	DRLG_L2PlaceRndSet(HARCH12, 100);
3207 	DRLG_L2PlaceRndSet(HARCH13, 100);
3208 	DRLG_L2PlaceRndSet(HARCH14, 100);
3209 	DRLG_L2PlaceRndSet(HARCH15, 100);
3210 	DRLG_L2PlaceRndSet(HARCH16, 100);
3211 	DRLG_L2PlaceRndSet(HARCH17, 100);
3212 	DRLG_L2PlaceRndSet(HARCH18, 100);
3213 	DRLG_L2PlaceRndSet(HARCH19, 100);
3214 	DRLG_L2PlaceRndSet(HARCH20, 100);
3215 	DRLG_L2PlaceRndSet(HARCH21, 100);
3216 	DRLG_L2PlaceRndSet(HARCH22, 100);
3217 	DRLG_L2PlaceRndSet(HARCH23, 100);
3218 	DRLG_L2PlaceRndSet(HARCH24, 100);
3219 	DRLG_L2PlaceRndSet(HARCH25, 100);
3220 	DRLG_L2PlaceRndSet(HARCH26, 100);
3221 	DRLG_L2PlaceRndSet(HARCH27, 100);
3222 	DRLG_L2PlaceRndSet(HARCH28, 100);
3223 	DRLG_L2PlaceRndSet(HARCH29, 100);
3224 	DRLG_L2PlaceRndSet(HARCH30, 100);
3225 	DRLG_L2PlaceRndSet(HARCH31, 100);
3226 	DRLG_L2PlaceRndSet(HARCH32, 100);
3227 	DRLG_L2PlaceRndSet(HARCH33, 100);
3228 	DRLG_L2PlaceRndSet(HARCH34, 100);
3229 	DRLG_L2PlaceRndSet(HARCH35, 100);
3230 	DRLG_L2PlaceRndSet(HARCH36, 100);
3231 	DRLG_L2PlaceRndSet(HARCH37, 100);
3232 	DRLG_L2PlaceRndSet(HARCH38, 100);
3233 	DRLG_L2PlaceRndSet(HARCH39, 100);
3234 	DRLG_L2PlaceRndSet(HARCH40, 100);
3235 	DRLG_L2PlaceRndSet(CRUSHCOL, 99);
3236 	DRLG_L2PlaceRndSet(RUINS1, 10);
3237 	DRLG_L2PlaceRndSet(RUINS2, 10);
3238 	DRLG_L2PlaceRndSet(RUINS3, 10);
3239 	DRLG_L2PlaceRndSet(RUINS4, 10);
3240 	DRLG_L2PlaceRndSet(RUINS5, 10);
3241 	DRLG_L2PlaceRndSet(RUINS6, 10);
3242 	DRLG_L2PlaceRndSet(RUINS7, 50);
3243 	DRLG_L2PlaceRndSet(PANCREAS1, 1);
3244 	DRLG_L2PlaceRndSet(PANCREAS2, 1);
3245 	DRLG_L2PlaceRndSet(BIG1, 3);
3246 	DRLG_L2PlaceRndSet(BIG2, 3);
3247 	DRLG_L2PlaceRndSet(BIG3, 3);
3248 	DRLG_L2PlaceRndSet(BIG4, 3);
3249 	DRLG_L2PlaceRndSet(BIG5, 3);
3250 	DRLG_L2PlaceRndSet(BIG6, 20);
3251 	DRLG_L2PlaceRndSet(BIG7, 20);
3252 	DRLG_L2PlaceRndSet(BIG8, 3);
3253 	DRLG_L2PlaceRndSet(BIG9, 20);
3254 	DRLG_L2PlaceRndSet(BIG10, 20);
3255 	DRLG_L2Subs();
3256 	DRLG_L2Shadows();
3257 
3258 	for (j = 0; j < DMAXY; j++) {
3259 		for (i = 0; i < DMAXX; i++) {
3260 			pdungeon[i][j] = dungeon[i][j];
3261 		}
3262 	}
3263 
3264 	DRLG_Init_Globals();
3265 	DRLG_CheckQuests(nSx1, nSy1);
3266 }
3267 
DRLG_InitL2Vals()3268 static void DRLG_InitL2Vals()
3269 {
3270 	int i, j, pc;
3271 
3272 	for (j = 0; j < MAXDUNY; j++) {
3273 		for (i = 0; i < MAXDUNX; i++) {
3274 			if (dPiece[i][j] == 541) {
3275 				pc = 5;
3276 			} else if (dPiece[i][j] == 178) {
3277 				pc = 5;
3278 			} else if (dPiece[i][j] == 551) {
3279 				pc = 5;
3280 			} else if (dPiece[i][j] == 542) {
3281 				pc = 6;
3282 			} else if (dPiece[i][j] == 553) {
3283 				pc = 6;
3284 			} else {
3285 				continue;
3286 			}
3287 			dSpecial[i][j] = pc;
3288 		}
3289 	}
3290 	for (j = 0; j < MAXDUNY; j++) {
3291 		for (i = 0; i < MAXDUNX; i++) {
3292 			if (dPiece[i][j] == 132) {
3293 				dSpecial[i][j + 1] = 2;
3294 				dSpecial[i][j + 2] = 1;
3295 			} else if (dPiece[i][j] == 135 || dPiece[i][j] == 139) {
3296 				dSpecial[i + 1][j] = 3;
3297 				dSpecial[i + 2][j] = 4;
3298 			}
3299 		}
3300 	}
3301 }
3302 
LoadL2Dungeon(const char * sFileName,int vx,int vy)3303 void LoadL2Dungeon(const char *sFileName, int vx, int vy)
3304 {
3305 	int i, j, rw, rh, pc;
3306 	BYTE *pLevelMap, *lm;
3307 
3308 	InitDungeon();
3309 	DRLG_InitTrans();
3310 	pLevelMap = LoadFileInMem(sFileName, NULL);
3311 
3312 	for (j = 0; j < DMAXY; j++) {
3313 		for (i = 0; i < DMAXX; i++) {
3314 			dungeon[i][j] = 12;
3315 			dflags[i][j] = 0;
3316 		}
3317 	}
3318 
3319 	lm = pLevelMap;
3320 	rw = *lm;
3321 	lm += 2;
3322 	rh = *lm;
3323 	lm += 2;
3324 
3325 	for (j = 0; j < rh; j++) {
3326 		for (i = 0; i < rw; i++) {
3327 			if (*lm != 0) {
3328 				dungeon[i][j] = *lm;
3329 				dflags[i][j] |= DLRG_PROTECTED;
3330 			} else {
3331 				dungeon[i][j] = 3;
3332 			}
3333 			lm += 2;
3334 		}
3335 	}
3336 	for (j = 0; j < DMAXY; j++) {
3337 		for (i = 0; i < DMAXX; i++) {
3338 			if (dungeon[i][j] == 0) {
3339 				dungeon[i][j] = 12;
3340 			}
3341 		}
3342 	}
3343 
3344 	DRLG_L2Pass3();
3345 	DRLG_Init_Globals();
3346 
3347 	for (j = 0; j < MAXDUNY; j++) {
3348 		for (i = 0; i < MAXDUNX; i++) {
3349 			pc = 0;
3350 			if (dPiece[i][j] == 541) {
3351 				pc = 5;
3352 			}
3353 			if (dPiece[i][j] == 178) {
3354 				pc = 5;
3355 			}
3356 			if (dPiece[i][j] == 551) {
3357 				pc = 5;
3358 			}
3359 			if (dPiece[i][j] == 542) {
3360 				pc = 6;
3361 			}
3362 			if (dPiece[i][j] == 553) {
3363 				pc = 6;
3364 			}
3365 			dSpecial[i][j] = pc;
3366 		}
3367 	}
3368 	for (j = 0; j < MAXDUNY; j++) {
3369 		for (i = 0; i < MAXDUNX; i++) {
3370 			if (dPiece[i][j] == 132) {
3371 				dSpecial[i][j + 1] = 2;
3372 				dSpecial[i][j + 2] = 1;
3373 			} else if (dPiece[i][j] == 135 || dPiece[i][j] == 139) {
3374 				dSpecial[i + 1][j] = 3;
3375 				dSpecial[i + 2][j] = 4;
3376 			}
3377 		}
3378 	}
3379 
3380 	ViewX = vx;
3381 	ViewY = vy;
3382 	SetMapMonsters(pLevelMap, 0, 0);
3383 	SetMapObjects(pLevelMap, 0, 0);
3384 	mem_free_dbg(pLevelMap);
3385 }
3386 
LoadPreL2Dungeon(const char * sFileName,int vx,int vy)3387 void LoadPreL2Dungeon(const char *sFileName, int vx, int vy)
3388 {
3389 	int i, j, rw, rh;
3390 	BYTE *pLevelMap, *lm;
3391 
3392 	InitDungeon();
3393 	DRLG_InitTrans();
3394 	pLevelMap = LoadFileInMem(sFileName, NULL);
3395 
3396 	for (j = 0; j < DMAXY; j++) {
3397 		for (i = 0; i < DMAXX; i++) {
3398 			dungeon[i][j] = 12;
3399 			dflags[i][j] = 0;
3400 		}
3401 	}
3402 
3403 	lm = pLevelMap;
3404 	rw = *lm;
3405 	lm += 2;
3406 	rh = *lm;
3407 	lm += 2;
3408 
3409 	for (j = 0; j < rh; j++) {
3410 		for (i = 0; i < rw; i++) {
3411 			if (*lm != 0) {
3412 				dungeon[i][j] = *lm;
3413 				dflags[i][j] |= DLRG_PROTECTED;
3414 			} else {
3415 				dungeon[i][j] = 3;
3416 			}
3417 			lm += 2;
3418 		}
3419 	}
3420 	for (j = 0; j < DMAXY; j++) {
3421 		for (i = 0; i < DMAXX; i++) {
3422 			if (dungeon[i][j] == 0) {
3423 				dungeon[i][j] = 12;
3424 			}
3425 		}
3426 	}
3427 	for (j = 0; j < DMAXY; j++) {
3428 		for (i = 0; i < DMAXX; i++) {
3429 			pdungeon[i][j] = dungeon[i][j];
3430 		}
3431 	}
3432 
3433 	mem_free_dbg(pLevelMap);
3434 }
3435 
CreateL2Dungeon(DWORD rseed,int entry)3436 void CreateL2Dungeon(DWORD rseed, int entry)
3437 {
3438 	if (!gbIsMultiplayer) {
3439 		if (currlevel == 7 && quests[Q_BLIND]._qactive == QUEST_NOTAVAIL) {
3440 			currlevel = 6;
3441 			CreateL2Dungeon(glSeedTbl[6], 4);
3442 			currlevel = 7;
3443 		}
3444 		if (currlevel == 8) {
3445 			if (quests[Q_BLIND]._qactive == QUEST_NOTAVAIL) {
3446 				currlevel = 6;
3447 				CreateL2Dungeon(glSeedTbl[6], 4);
3448 				currlevel = 8;
3449 			} else {
3450 				currlevel = 7;
3451 				CreateL2Dungeon(glSeedTbl[7], 4);
3452 				currlevel = 8;
3453 			}
3454 		}
3455 	}
3456 
3457 	SetRndSeed(rseed);
3458 
3459 	dminx = 16;
3460 	dminy = 16;
3461 	dmaxx = 96;
3462 	dmaxy = 96;
3463 
3464 	DRLG_InitTrans();
3465 	DRLG_InitSetPC();
3466 	DRLG_LoadL2SP();
3467 	DRLG_L2(entry);
3468 	DRLG_L2Pass3();
3469 	DRLG_FreeL2SP();
3470 	DRLG_InitL2Vals();
3471 	DRLG_SetPC();
3472 }
3473 
3474 DEVILUTION_END_NAMESPACE
3475