forked from NetHack/NetHack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkmaze.c
2028 lines (1823 loc) · 65.3 KB
/
mkmaze.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* NetHack 3.7 mkmaze.c $NHDT-Date: 1648064596 2022/03/23 19:43:16 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.133 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Pasi Kallinen, 2018. */
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
#include "sp_lev.h"
static int iswall(coordxy, coordxy);
static int iswall_or_stone(coordxy, coordxy);
static boolean is_solid(coordxy, coordxy);
static int extend_spine(int[3][3], int, int, int);
static void wall_cleanup(coordxy, coordxy, coordxy, coordxy);
static boolean okay(coordxy, coordxy, coordxy);
static void maze0xy(coord *);
static boolean put_lregion_here(coordxy, coordxy, coordxy, coordxy, coordxy,
coordxy, xint16, boolean, d_level *);
static void baalz_fixup(void);
static void setup_waterlevel(void);
static void unsetup_waterlevel(void);
static void check_ransacked(const char *);
static void migr_booty_item(int, const char *);
static void migrate_orc(struct monst *, unsigned long);
static void shiny_orc_stuff(struct monst *);
static void stolen_booty(void);
static boolean maze_inbounds(coordxy, coordxy);
static void maze_remove_deadends(xint16);
/* adjust a coordinate one step in the specified direction */
#define mz_move(X, Y, dir) \
do { \
switch (dir) { \
case 0: --(Y); break; \
case 1: (X)++; break; \
case 2: (Y)++; break; \
case 3: --(X); break; \
default: panic("mz_move: bad direction %d", dir); \
} \
} while (0)
static int
iswall(coordxy x, coordxy y)
{
int type;
if (!isok(x, y))
return 0;
type = levl[x][y].typ;
return (IS_WALL(type) || IS_DOOR(type)
|| type == SDOOR || type == IRONBARS);
}
static int
iswall_or_stone(coordxy x, coordxy y)
{
/* out of bounds = stone */
if (!isok(x, y))
return 1;
return (levl[x][y].typ == STONE || iswall(x, y));
}
/* return TRUE if out of bounds, wall or rock */
static boolean
is_solid(coordxy x, coordxy y)
{
return (boolean) (!isok(x, y) || IS_STWALL(levl[x][y].typ));
}
/* set map terrain type, handling lava lit, ice melt timers, etc */
boolean
set_levltyp(coordxy x, coordxy y, schar newtyp)
{
if (isok(x, y) && newtyp >= STONE && newtyp < MAX_TYPE) {
if (CAN_OVERWRITE_TERRAIN(levl[x][y].typ)) {
schar oldtyp = levl[x][y].typ;
boolean was_ice = (levl[x][y].typ == ICE);
levl[x][y].typ = newtyp;
/* TODO?
* if oldtyp used flags or horizontal differently from
* from the way newtyp will use them, clear them.
*/
if (IS_LAVA(newtyp))
levl[x][y].lit = 1;
if (was_ice && newtyp != ICE)
spot_stop_timers(x, y, MELT_ICE_AWAY);
if ((IS_FOUNTAIN(oldtyp) != IS_FOUNTAIN(newtyp))
|| (IS_SINK(oldtyp) != IS_SINK(newtyp)))
count_level_features(); /* level.flags.nfountains,nsinks */
return TRUE;
}
#ifdef EXTRA_SANITY_CHECKS
} else {
impossible("set_levltyp(%d,%d,%d)%s%s",
(int) x, (int) y, (int) newtyp,
!isok(x, y) ? " not isok()" : "",
(newtyp < STONE || newtyp >= MAX_TYPE) ? " bad type" : "");
#endif /*EXTRA_SANITY_CHECKS*/
}
return FALSE;
}
/* set map terrain type and light state */
boolean
set_levltyp_lit(coordxy x, coordxy y, schar typ, schar lit)
{
boolean ret = set_levltyp(x, y, typ);
if (ret && isok(x, y)) {
if (lit != SET_LIT_NOCHANGE) {
#ifdef EXTRA_SANITY_CHECKS
if (lit < SET_LIT_NOCHANGE || lit > 1)
impossible("set_levltyp_lit(%d,%d,%d,%d)",
(int) x, (int) y, (int) typ, (int) lit);
#endif /*EXTRA_SANITY_CHECKS*/
if (IS_LAVA(typ))
lit = 1;
else if (lit == SET_LIT_RANDOM)
lit = rn2(2);
levl[x][y].lit = lit;
}
}
return ret;
}
/*
* Return 1 (not TRUE - we're doing bit vectors here) if we want to extend
* a wall spine in the (dx,dy) direction. Return 0 otherwise.
*
* To extend a wall spine in that direction, first there must be a wall there.
* Then, extend a spine unless the current position is surrounded by walls
* in the direction given by (dx,dy). E.g. if 'x' is our location, 'W'
* a wall, '.' a room, 'a' anything (we don't care), and our direction is
* (0,1) - South or down - then:
*
* a a a
* W x W This would not extend a spine from x down
* W W W (a corridor of walls is formed).
*
* a a a
* W x W This would extend a spine from x down.
* . W W
*/
static int
extend_spine(int locale[3][3], int wall_there, int dx, int dy)
{
int spine, nx, ny;
nx = 1 + dx;
ny = 1 + dy;
if (wall_there) { /* wall in that direction */
if (dx) {
if (locale[1][0] && locale[1][2] /* EW are wall/stone */
&& locale[nx][0] && locale[nx][2]) { /* diag are wall/stone */
spine = 0;
} else {
spine = 1;
}
} else { /* dy */
if (locale[0][1] && locale[2][1] /* NS are wall/stone */
&& locale[0][ny] && locale[2][ny]) { /* diag are wall/stone */
spine = 0;
} else {
spine = 1;
}
}
} else {
spine = 0;
}
return spine;
}
/* Remove walls totally surrounded by stone */
static void
wall_cleanup(coordxy x1, coordxy y1, coordxy x2, coordxy y2)
{
uchar type;
coordxy x, y;
struct rm *lev;
/* sanity check on incoming variables */
if (x1 < 0 || x2 >= COLNO || x1 > x2 || y1 < 0 || y2 >= ROWNO || y1 > y2)
panic("wall_cleanup: bad bounds (%d,%d) to (%d,%d)", x1, y1, x2, y2);
/* change walls surrounded by rock to rock. */
for (x = x1; x <= x2; x++)
for (y = y1; y <= y2; y++) {
if (within_bounded_area(x, y,
gb.bughack.inarea.x1, gb.bughack.inarea.y1,
gb.bughack.inarea.x2, gb.bughack.inarea.y2))
continue;
lev = &levl[x][y];
type = lev->typ;
if (IS_WALL(type) && type != DBWALL) {
if (is_solid(x - 1, y - 1) && is_solid(x - 1, y)
&& is_solid(x - 1, y + 1) && is_solid(x, y - 1)
&& is_solid(x, y + 1) && is_solid(x + 1, y - 1)
&& is_solid(x + 1, y) && is_solid(x + 1, y + 1))
lev->typ = STONE;
}
}
}
/* Correct wall types so they extend and connect to each other */
void
fix_wall_spines(coordxy x1, coordxy y1, coordxy x2, coordxy y2)
{
uchar type;
coordxy x, y;
struct rm *lev;
int (*loc_f)(coordxy, coordxy);
int bits;
int locale[3][3]; /* rock or wall status surrounding positions */
/*
* Value 0 represents a free-standing wall. It could be anything,
* so even though this table says VWALL, we actually leave whatever
* typ was there alone.
*/
static xint16 spine_array[16] = { VWALL, HWALL, HWALL, HWALL,
VWALL, TRCORNER, TLCORNER, TDWALL,
VWALL, BRCORNER, BLCORNER, TUWALL,
VWALL, TLWALL, TRWALL, CROSSWALL };
/* sanity check on incoming variables */
if (x1 < 0 || x2 >= COLNO || x1 > x2 || y1 < 0 || y2 >= ROWNO || y1 > y2)
panic("wall_extends: bad bounds (%d,%d) to (%d,%d)", x1, y1, x2, y2);
/* set the correct wall type. */
for (x = x1; x <= x2; x++)
for (y = y1; y <= y2; y++) {
lev = &levl[x][y];
type = lev->typ;
if (!(IS_WALL(type) && type != DBWALL))
continue;
/* set the locations TRUE if rock or wall or out of bounds */
loc_f = within_bounded_area(x, y, /* for baalz insect */
gb.bughack.inarea.x1, gb.bughack.inarea.y1,
gb.bughack.inarea.x2, gb.bughack.inarea.y2)
? iswall
: iswall_or_stone;
locale[0][0] = (*loc_f)(x - 1, y - 1);
locale[1][0] = (*loc_f)(x, y - 1);
locale[2][0] = (*loc_f)(x + 1, y - 1);
locale[0][1] = (*loc_f)(x - 1, y);
locale[2][1] = (*loc_f)(x + 1, y);
locale[0][2] = (*loc_f)(x - 1, y + 1);
locale[1][2] = (*loc_f)(x, y + 1);
locale[2][2] = (*loc_f)(x + 1, y + 1);
/* determine if wall should extend to each direction NSEW */
bits = (extend_spine(locale, iswall(x, y - 1), 0, -1) << 3)
| (extend_spine(locale, iswall(x, y + 1), 0, 1) << 2)
| (extend_spine(locale, iswall(x + 1, y), 1, 0) << 1)
| extend_spine(locale, iswall(x - 1, y), -1, 0);
/* don't change typ if wall is free-standing */
if (bits)
lev->typ = spine_array[bits];
}
}
void
wallification(coordxy x1, coordxy y1, coordxy x2, coordxy y2)
{
wall_cleanup(x1, y1, x2, y2);
fix_wall_spines(x1, y1, x2, y2);
}
static boolean
okay(coordxy x, coordxy y, coordxy dir)
{
mz_move(x, y, dir);
mz_move(x, y, dir);
if (x < 3 || y < 3 || x > gx.x_maze_max || y > gy.y_maze_max
|| levl[x][y].typ != STONE)
return FALSE;
return TRUE;
}
/* find random starting point for maze generation */
static void
maze0xy(coord *cc)
{
cc->x = 3 + 2 * rn2((gx.x_maze_max >> 1) - 1);
cc->y = 3 + 2 * rn2((gy.y_maze_max >> 1) - 1);
return;
}
static boolean
is_exclusion_zone(xint16 type, coordxy x, coordxy y)
{
struct exclusion_zone *ez = ge.exclusion_zones;
while (ez) {
if (((type == LR_DOWNTELE && (ez->zonetype == LR_DOWNTELE || ez->zonetype == LR_TELE))
|| (type == LR_UPTELE && (ez->zonetype == LR_UPTELE || ez->zonetype == LR_TELE))
|| type == ez->zonetype)
&& within_bounded_area(x, y, ez->lx, ez->ly, ez->hx, ez->hy))
return TRUE;
ez = ez->next;
}
return FALSE;
}
/*
* Bad if:
* pos is occupied OR
* pos is inside restricted region (nlx,nly,nhx,nhy) OR
* NOT (pos is corridor and a maze level OR pos is a room OR pos is air)
*/
boolean
bad_location(
coordxy x, coordxy y,
coordxy nlx, coordxy nly, coordxy nhx, coordxy nhy)
{
return (boolean) (occupied(x, y)
|| within_bounded_area(x, y, nlx, nly, nhx, nhy)
|| !((levl[x][y].typ == CORR
&& gl.level.flags.is_maze_lev)
|| levl[x][y].typ == ROOM
|| levl[x][y].typ == AIR));
}
/* pick a location in area (lx, ly, hx, hy) but not in (nlx, nly, nhx, nhy)
and place something (based on rtype) in that region */
void
place_lregion(
coordxy lx, coordxy ly, coordxy hx, coordxy hy,
coordxy nlx, coordxy nly, coordxy nhx, coordxy nhy,
xint16 rtype,
d_level *lev)
{
int trycnt;
boolean oneshot;
coordxy x, y;
if (!lx) { /* default to whole level */
/*
* if there are rooms and this a branch, let place_branch choose
* the branch location (to avoid putting branches in corridors).
*/
if (rtype == LR_BRANCH && gn.nroom) {
place_branch(Is_branchlev(&u.uz), 0, 0);
return;
}
lx = 1; /* column 0 is not used */
hx = COLNO - 1;
ly = 0; /* 3.6.0 and earlier erroneously had 1 here */
hy = ROWNO - 1;
}
/* first a probabilistic approach */
oneshot = (lx == hx && ly == hy);
for (trycnt = 0; trycnt < 200; trycnt++) {
x = rn1((hx - lx) + 1, lx);
y = rn1((hy - ly) + 1, ly);
if (put_lregion_here(x, y, nlx, nly, nhx, nhy, rtype, oneshot, lev))
return;
}
/* then a deterministic one */
for (x = lx; x <= hx; x++)
for (y = ly; y <= hy; y++)
if (put_lregion_here(x, y, nlx, nly, nhx, nhy, rtype, TRUE, lev))
return;
impossible("Couldn't place lregion type %d!", rtype);
}
static boolean
put_lregion_here(
coordxy x, coordxy y,
coordxy nlx, coordxy nly, coordxy nhx, coordxy nhy,
xint16 rtype,
boolean oneshot,
d_level *lev)
{
struct monst *mtmp;
if (bad_location(x, y, nlx, nly, nhx, nhy) || is_exclusion_zone(rtype, x, y)) {
if (!oneshot) {
return FALSE; /* caller should try again */
} else {
/* Must make do with the only location possible;
avoid failure due to a misplaced trap.
It might still fail if there's a dungeon feature here. */
struct trap *t = t_at(x, y);
if (t && !undestroyable_trap(t->ttyp)) {
if (((mtmp = m_at(x, y)) != 0) && mtmp->mtrapped)
mtmp->mtrapped = 0;
deltrap(t);
}
if (bad_location(x, y, nlx, nly, nhx, nhy) || is_exclusion_zone(rtype, x, y))
return FALSE;
}
}
switch (rtype) {
case LR_TELE:
case LR_UPTELE:
case LR_DOWNTELE:
/* "something" means the player in this case */
if ((mtmp = m_at(x, y)) != 0) {
/* move the monster if no choice, or just try again */
if (oneshot) {
if (!rloc(mtmp, RLOC_NOMSG))
m_into_limbo(mtmp);
} else
return FALSE;
}
u_on_newpos(x, y);
break;
case LR_PORTAL:
mkportal(x, y, lev->dnum, lev->dlevel);
break;
case LR_DOWNSTAIR:
case LR_UPSTAIR:
mkstairs(x, y, (char) rtype, (struct mkroom *) 0, FALSE);
break;
case LR_BRANCH:
place_branch(Is_branchlev(&u.uz), x, y);
break;
}
return TRUE;
}
/* fix up Baalzebub's lair, which depicts a level-sized beetle;
its legs are walls within solid rock--regular wallification
classifies them as superfluous and gets rid of them */
static void
baalz_fixup(void)
{
struct monst *mtmp;
int x, y, lastx, lasty;
/*
* baalz level's nondiggable region surrounds the "insect" and rooms.
* The outermost perimeter of that region is subject to wall cleanup
* (hence 'x + 1' and 'y + 1' for starting don't-clean column and row,
* 'lastx - 1' and 'lasty - 1' for ending don't-clean column and row)
* and the interior is protected against that (in wall_cleanup()).
*
* Assumes level.flags.corrmaze is True, otherwise the bug legs will
* have already been "cleaned" away by general wallification.
*/
/* find low and high x for to-be-wallified portion of level */
y = ROWNO / 2;
for (lastx = x = 0; x < COLNO; ++x)
if ((levl[x][y].wall_info & W_NONDIGGABLE) != 0) {
if (!lastx)
gb.bughack.inarea.x1 = x + 1;
lastx = x;
}
gb.bughack.inarea.x2 = ((lastx > gb.bughack.inarea.x1) ? lastx : x) - 1;
/* find low and high y for to-be-wallified portion of level */
x = gb.bughack.inarea.x1;
for (lasty = y = 0; y < ROWNO; ++y)
if ((levl[x][y].wall_info & W_NONDIGGABLE) != 0) {
if (!lasty)
gb.bughack.inarea.y1 = y + 1;
lasty = y;
}
gb.bughack.inarea.y2 = ((lasty > gb.bughack.inarea.y1) ? lasty : y) - 1;
/* two pools mark where special post-wallify fix-ups are needed */
for (x = gb.bughack.inarea.x1; x <= gb.bughack.inarea.x2; ++x)
for (y = gb.bughack.inarea.y1; y <= gb.bughack.inarea.y2; ++y)
if (levl[x][y].typ == POOL) {
levl[x][y].typ = HWALL;
if (gb.bughack.delarea.x1 == COLNO)
gb.bughack.delarea.x1 = x, gb.bughack.delarea.y1 = y;
else
gb.bughack.delarea.x2 = x, gb.bughack.delarea.y2 = y;
} else if (levl[x][y].typ == IRONBARS) {
/* novelty effect; allowing digging in front of 'eyes' */
if (isok(x - 1, y)
&& (levl[x - 1][y].wall_info & W_NONDIGGABLE) != 0) {
levl[x - 1][y].wall_info &= ~W_NONDIGGABLE;
if (isok(x - 2, y))
levl[x - 2][y].wall_info &= ~W_NONDIGGABLE;
} else if (isok(x + 1, y)
&& (levl[x + 1][y].wall_info & W_NONDIGGABLE) != 0) {
levl[x + 1][y].wall_info &= ~W_NONDIGGABLE;
if (isok(x + 2, y))
levl[x + 2][y].wall_info &= ~W_NONDIGGABLE;
}
}
wallification(max(gb.bughack.inarea.x1 - 2, 1),
max(gb.bughack.inarea.y1 - 2, 0),
min(gb.bughack.inarea.x2 + 2, COLNO - 1),
min(gb.bughack.inarea.y2 + 2, ROWNO - 1));
/* bughack hack for rear-most legs on baalz level; first joint on
both top and bottom gets a bogus extra connection to room area,
producing unwanted rectangles; change back to separated legs */
x = gb.bughack.delarea.x1, y = gb.bughack.delarea.y1;
if (isok(x, y) && (levl[x][y].typ == TLWALL || levl[x][y].typ == TRWALL)
&& isok(x, y + 1) && levl[x][y + 1].typ == TUWALL) {
levl[x][y].typ = (levl[x][y].typ == TLWALL) ? BRCORNER : BLCORNER;
levl[x][y + 1].typ = HWALL;
if ((mtmp = m_at(x, y)) != 0) /* something at temporary pool... */
(void) rloc(mtmp, RLOC_ERR|RLOC_NOMSG);
}
x = gb.bughack.delarea.x2, y = gb.bughack.delarea.y2;
if (isok(x, y) && (levl[x][y].typ == TLWALL || levl[x][y].typ == TRWALL)
&& isok(x, y - 1) && levl[x][y - 1].typ == TDWALL) {
levl[x][y].typ = (levl[x][y].typ == TLWALL) ? TRCORNER : TLCORNER;
levl[x][y - 1].typ = HWALL;
if ((mtmp = m_at(x, y)) != 0) /* something at temporary pool... */
(void) rloc(mtmp, RLOC_ERR|RLOC_NOMSG);
}
/* reset bughack region; set low end to <COLNO,ROWNO> so that
within_bounded_region() in fix_wall_spines() will fail
most quickly--on its first test--when loading other levels */
gb.bughack.inarea.x1 = gb.bughack.delarea.x1 = COLNO;
gb.bughack.inarea.y1 = gb.bughack.delarea.y1 = ROWNO;
gb.bughack.inarea.x2 = gb.bughack.delarea.x2 = 0;
gb.bughack.inarea.y2 = gb.bughack.delarea.y2 = 0;
}
/* this is special stuff that the level compiler cannot (yet) handle */
void
fixup_special(void)
{
lev_region *r = gl.lregions;
s_level *sp;
struct d_level lev;
int x, y;
struct mkroom *croom;
boolean added_branch = FALSE;
if (Is_waterlevel(&u.uz) || Is_airlevel(&u.uz)) {
gl.level.flags.hero_memory = 0;
/* water level is an odd beast - it has to be set up
before calling place_lregions etc. */
setup_waterlevel();
}
for (x = 0; x < gn.num_lregions; x++, r++) {
switch (r->rtype) {
case LR_BRANCH:
added_branch = TRUE;
goto place_it;
case LR_PORTAL:
if (*r->rname.str >= '0' && *r->rname.str <= '9') {
/* "chutes and ladders" */
lev = u.uz;
lev.dlevel = atoi(r->rname.str);
} else {
sp = find_level(r->rname.str);
lev = sp->dlevel;
}
/*FALLTHRU*/
case LR_UPSTAIR:
case LR_DOWNSTAIR:
place_it:
place_lregion(r->inarea.x1, r->inarea.y1, r->inarea.x2,
r->inarea.y2, r->delarea.x1, r->delarea.y1,
r->delarea.x2, r->delarea.y2, r->rtype, &lev);
break;
case LR_TELE:
case LR_UPTELE:
case LR_DOWNTELE:
/* save the region outlines for goto_level() */
if (r->rtype == LR_TELE || r->rtype == LR_UPTELE) {
gu.updest.lx = r->inarea.x1;
gu.updest.ly = r->inarea.y1;
gu.updest.hx = r->inarea.x2;
gu.updest.hy = r->inarea.y2;
gu.updest.nlx = r->delarea.x1;
gu.updest.nly = r->delarea.y1;
gu.updest.nhx = r->delarea.x2;
gu.updest.nhy = r->delarea.y2;
}
if (r->rtype == LR_TELE || r->rtype == LR_DOWNTELE) {
gd.dndest.lx = r->inarea.x1;
gd.dndest.ly = r->inarea.y1;
gd.dndest.hx = r->inarea.x2;
gd.dndest.hy = r->inarea.y2;
gd.dndest.nlx = r->delarea.x1;
gd.dndest.nly = r->delarea.y1;
gd.dndest.nhx = r->delarea.x2;
gd.dndest.nhy = r->delarea.y2;
}
/* place_lregion gets called from goto_level() */
break;
}
if (r->rname.str)
free((genericptr_t) r->rname.str), r->rname.str = 0;
}
/* place dungeon branch if not placed above */
if (!added_branch && Is_branchlev(&u.uz)) {
place_lregion(0, 0, 0, 0, 0, 0, 0, 0, LR_BRANCH, (d_level *) 0);
}
/* Still need to add some stuff to level file */
if (Is_medusa_level(&u.uz)) {
struct obj *otmp;
int tryct;
croom = &gr.rooms[0]; /* the first room defined on the medusa level */
for (tryct = rnd(4); tryct; tryct--) {
x = somex(croom);
y = somey(croom);
if (goodpos(x, y, (struct monst *) 0, 0)) {
int tryct2 = 0;
otmp = mk_tt_object(STATUE, x, y);
while (++tryct2 < 100 && otmp
&& (poly_when_stoned(&mons[otmp->corpsenm])
|| pm_resistance(&mons[otmp->corpsenm],
MR_STONE))) {
/* set_corpsenm() handles weight too */
set_corpsenm(otmp, rndmonnum());
}
}
}
if (rn2(2))
otmp = mk_tt_object(STATUE, somex(croom), somey(croom));
else /* Medusa statues don't contain books */
otmp =
mkcorpstat(STATUE, (struct monst *) 0, (struct permonst *) 0,
somex(croom), somey(croom), CORPSTAT_NONE);
if (otmp) {
tryct = 0;
while (++tryct < 100
&& (pm_resistance(&mons[otmp->corpsenm], MR_STONE)
|| poly_when_stoned(&mons[otmp->corpsenm]))) {
/* set_corpsenm() handles weight too */
set_corpsenm(otmp, rndmonnum());
}
}
} else if (Role_if(PM_CLERIC) && In_quest(&u.uz)) {
/* less chance for undead corpses (lured from lower morgues) */
gl.level.flags.graveyard = 1;
} else if (Is_stronghold(&u.uz)) {
gl.level.flags.graveyard = 1;
} else if (on_level(&u.uz, &baalzebub_level)) {
/* custom wallify the "beetle" potion of the level */
baalz_fixup();
} else if (u.uz.dnum == mines_dnum && gr.ransacked) {
stolen_booty();
}
if ((sp = Is_special(&u.uz)) != 0 && sp->flags.town) /* Mine Town */
gl.level.flags.has_town = 1;
if (gl.lregions)
free((genericptr_t) gl.lregions), gl.lregions = 0;
gn.num_lregions = 0;
}
static void
check_ransacked(const char *s)
{
/* this kludge only works as long as orctown is minetn-1 */
gr.ransacked = (u.uz.dnum == mines_dnum && !strcmp(s, "minetn-1"));
}
#define ORC_LEADER 1
static const char *const orcfruit[] = { "paddle cactus", "dwarven root" };
static void
migrate_orc(struct monst *mtmp, unsigned long mflags)
{
int nlev, max_depth, cur_depth;
d_level dest;
cur_depth = (int) depth(&u.uz);
max_depth = dunlevs_in_dungeon(&u.uz)
+ (gd.dungeons[u.uz.dnum].depth_start - 1);
if (mflags == ORC_LEADER) {
/* Note that the orc leader will take possession of any
* remaining stuff not already delivered to other
* orcs between here and the bottom of the mines.
*/
nlev = max_depth;
/* once in a blue moon, he won't be at the very bottom */
if (!rn2(40))
nlev--;
mtmp->migflags |= MIGR_LEFTOVERS;
} else {
nlev = rn2((max_depth - cur_depth) + 1) + cur_depth;
if (nlev == cur_depth)
nlev++;
if (nlev > max_depth)
nlev = max_depth;
mtmp->migflags = (mtmp->migflags & ~MIGR_LEFTOVERS);
}
get_level(&dest, nlev);
migrate_to_level(mtmp, ledger_no(&dest), MIGR_RANDOM, (coord *) 0);
}
static void
shiny_orc_stuff(struct monst* mtmp)
{
int gemprob, goldprob, otyp;
struct obj *otmp;
boolean is_captain = (mtmp->data == &mons[PM_ORC_CAPTAIN]);
/* probabilities */
goldprob = is_captain ? 600 : 300;
gemprob = goldprob / 4;
if (rn2(1000) < goldprob) {
if ((otmp = mksobj(GOLD_PIECE, TRUE, FALSE)) != 0) {
otmp->quan = 1L + rnd(goldprob);
otmp->owt = weight(otmp);
add_to_minv(mtmp, otmp);
}
}
if (rn2(1000) < gemprob) {
if ((otmp = mkobj(GEM_CLASS, FALSE)) != 0) {
if (otmp->otyp == ROCK)
dealloc_obj(otmp);
else
add_to_minv(mtmp, otmp);
}
}
if (is_captain || !rn2(8)) {
otyp = shiny_obj(RING_CLASS);
if (otyp != STRANGE_OBJECT && (otmp = mksobj(otyp, TRUE, FALSE)) != 0)
add_to_minv(mtmp, otmp);
}
}
static void
migr_booty_item(int otyp, const char* gang)
{
struct obj *otmp;
otmp = mksobj_migr_to_species(otyp, (unsigned long) M2_ORC, TRUE, FALSE);
if (otmp && gang) {
new_oname(otmp, Strlen(gang) + 1); /* removes old name if present */
Strcpy(ONAME(otmp), gang);
if (objects[otyp].oc_class == FOOD_CLASS) {
if (otyp == SLIME_MOLD)
otmp->spe = fruitadd((char *) orcfruit[rn2(SIZE(orcfruit))],
(struct fruit *) 0);
otmp->quan += (long) rn2(3);
otmp->owt = weight(otmp);
}
}
}
static void
stolen_booty(void)
{
char *gang, gang_name[BUFSZ];
struct monst *mtmp;
int cnt, i, otyp;
/*
* --------------------------------------------------------
* Mythos:
*
* A tragic accident has occurred in Frontier Town...
* It has been overrun by orcs.
*
* The booty that the orcs took from the town is now
* in the possession of the orcs that did this and
* have long since fled the level.
* --------------------------------------------------------
*/
gang = rndorcname(gang_name);
/* create the stuff that the gang took */
cnt = rnd(4);
for (i = 0; i < cnt; ++i)
migr_booty_item(rn2(4) ? TALLOW_CANDLE : WAX_CANDLE, gang);
cnt = rnd(3);
for (i = 0; i < cnt; ++i)
migr_booty_item(SKELETON_KEY, gang);
otyp = rn2((GAUNTLETS_OF_DEXTERITY - LEATHER_GLOVES) + 1) + LEATHER_GLOVES;
migr_booty_item(otyp, gang);
cnt = rnd(10);
for (i = 0; i < cnt; ++i) {
/* Food items - but no lembas! (or some other weird things) */
otyp = rn1(TIN - TRIPE_RATION + 1, TRIPE_RATION);
if (otyp != LEMBAS_WAFER
/* exclude meat <anything>, globs of <anything>, kelp
which all have random generation probability of 0
(K-/C-rations do too, but we want to include those) */
&& (objects[otyp].oc_prob != 0
|| otyp == C_RATION || otyp == K_RATION)
/* exclude food items which utilize obj->corpsenm because
that field is going to be overloaded for delivery purposes */
&& otyp != CORPSE && otyp != EGG && otyp != TIN)
migr_booty_item(otyp, gang);
}
migr_booty_item(rn2(2) ? LONG_SWORD : SILVER_SABER, gang);
/* create the leader of the orc gang */
mtmp = makemon(&mons[PM_ORC_CAPTAIN], 0, 0, MM_NONAME);
if (mtmp) {
mtmp = christen_monst(mtmp, upstart(gang));
mtmp->mpeaceful = 0;
shiny_orc_stuff(mtmp);
migrate_orc(mtmp, ORC_LEADER);
}
/* Make most of the orcs on the level be part of the invading gang */
for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
if (DEADMONSTER(mtmp))
continue;
if (is_orc(mtmp->data) && !has_mgivenname(mtmp) && rn2(10)) {
/*
* We'll consider the orc captain from the level
* description to be the captain of a rival orc horde
* who is there to see what has transpired, and to
* contemplate future action.
*
* Don't christen the orc captain as a subordinate
* member of the main orc horde.
*/
if (mtmp->data != &mons[PM_ORC_CAPTAIN])
mtmp = christen_orc(mtmp, upstart(gang), "");
}
}
/* Lastly, ensure there's several more orcs from the gang along the way.
* The mechanics are such that they aren't actually identified as
* members of the invading gang until they get their spoils assigned
* to the inventory; handled during that assignment.
*/
cnt = rn2(10) + 5;
for (i = 0; i < cnt; ++i) {
int mtyp;
mtyp = rn2((PM_ORC_SHAMAN - PM_ORC) + 1) + PM_ORC;
mtmp = makemon(&mons[mtyp], 0, 0, MM_NONAME);
if (mtmp) {
shiny_orc_stuff(mtmp);
migrate_orc(mtmp, 0UL);
}
}
gr.ransacked = 0;
}
#undef ORC_LEADER
static boolean
maze_inbounds(coordxy x, coordxy y)
{
return (x >= 2 && y >= 2
&& x < gx.x_maze_max && y < gy.y_maze_max
/* isok() test is superfluous here (unless something has
clobbered the static *_maze_max variables) */
&& isok(x, y));
}
static void
maze_remove_deadends(xint16 typ)
{
char dirok[4];
coordxy x, y, dir, idx, idx2, dx, dy, dx2, dy2;
dirok[0] = 0; /* lint suppression */
for (x = 2; x < gx.x_maze_max; x++)
for (y = 2; y < gy.y_maze_max; y++)
if (ACCESSIBLE(levl[x][y].typ) && (x % 2) && (y % 2)) {
idx = idx2 = 0;
for (dir = 0; dir < 4; dir++) {
/* note: mz_move() is a macro which modifies
one of its first two parameters */
dx = dx2 = x;
dy = dy2 = y;
mz_move(dx, dy, dir);
if (!maze_inbounds(dx, dy)) {
idx2++;
continue;
}
mz_move(dx2, dy2, dir);
mz_move(dx2, dy2, dir);
if (!maze_inbounds(dx2, dy2)) {
idx2++;
continue;
}
if (!ACCESSIBLE(levl[dx][dy].typ)
&& ACCESSIBLE(levl[dx2][dy2].typ)) {
dirok[idx++] = dir;
idx2++;
}
}
if (idx2 >= 3 && idx > 0) {
dx = x;
dy = y;
dir = dirok[rn2(idx)];
mz_move(dx, dy, dir);
levl[dx][dy].typ = typ;
}
}
}
/* Create a maze with specified corridor width and wall thickness
* TODO: rewrite walkfrom so it works on temp space, not levl
*/
void
create_maze(int corrwid, int wallthick, boolean rmdeadends)
{
coordxy x,y;
coord mm;
int tmp_xmax = gx.x_maze_max;
int tmp_ymax = gy.y_maze_max;
int rdx = 0;
int rdy = 0;
int scale;
if (corrwid == -1)
corrwid = rnd(4);
if (wallthick == -1)
wallthick = rnd(4) - corrwid;
if (wallthick < 1)
wallthick = 1;
else if (wallthick > 5)
wallthick = 5;
if (corrwid < 1)
corrwid = 1;
else if (corrwid > 5)
corrwid = 5;
scale = corrwid + wallthick;
rdx = (gx.x_maze_max / scale);
rdy = (gy.y_maze_max / scale);
if (gl.level.flags.corrmaze)
for (x = 2; x < (rdx * 2); x++)
for (y = 2; y < (rdy * 2); y++)
levl[x][y].typ = STONE;
else
for (x = 2; x <= (rdx * 2); x++)
for (y = 2; y <= (rdy * 2); y++)
levl[x][y].typ = ((x % 2) && (y % 2)) ? STONE : HWALL;
/* set upper bounds for maze0xy and walkfrom */
gx.x_maze_max = (rdx * 2);
gy.y_maze_max = (rdy * 2);
/* create maze */
maze0xy(&mm);
walkfrom((int) mm.x, (int) mm.y, 0);
if (rmdeadends)
maze_remove_deadends((gl.level.flags.corrmaze) ? CORR : ROOM);
/* restore bounds */
gx.x_maze_max = tmp_xmax;
gy.y_maze_max = tmp_ymax;
/* scale maze up if needed */
if (scale > 2) {
char tmpmap[COLNO][ROWNO];
int mx, my, dx, dy, rx = 1, ry = 1;
/* back up the existing smaller maze */
for (x = 1; x < gx.x_maze_max; x++)
for (y = 1; y < gy.y_maze_max; y++) {
tmpmap[x][y] = levl[x][y].typ;
}
/* do the scaling */
rx = x = 2;
while (rx < gx.x_maze_max) {
mx = (x % 2) ? corrwid : (x == 2 || x == rdx * 2) ? 1 : wallthick;
ry = y = 2;
while (ry < gy.y_maze_max) {
dx = dy = 0;
my = (y % 2) ? corrwid
: (y == 2 || y == rdy * 2) ? 1
: wallthick;
for (dx = 0; dx < mx; dx++)
for (dy = 0; dy < my; dy++) {
if (rx + dx >= gx.x_maze_max
|| ry + dy >= gy.y_maze_max)
break;
levl[rx + dx][ry + dy].typ = tmpmap[x][y];
}
ry += my;