forked from NetHack/NetHack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdungeon.c
More file actions
3419 lines (3087 loc) · 109 KB
/
Copy pathdungeon.c
File metadata and controls
3419 lines (3087 loc) · 109 KB
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.6 dungeon.c $NHDT-Date: 1580607225 2020/02/02 01:33:45 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.124 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Robert Patrick Rankin, 2012. */
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
#include "dgn_file.h"
#include "dlb.h"
#define DUNGEON_FILE "dungeon.lua"
#define X_START "x-strt"
#define X_LOCATE "x-loca"
#define X_GOAL "x-goal"
struct proto_dungeon {
struct tmpdungeon tmpdungeon[MAXDUNGEON];
struct tmplevel tmplevel[LEV_LIMIT];
s_level *final_lev[LEV_LIMIT]; /* corresponding level pointers */
struct tmpbranch tmpbranch[BRANCH_LIMIT];
int start; /* starting index of current dungeon sp levels */
int n_levs; /* number of tmplevel entries */
int n_brs; /* number of tmpbranch entries */
};
struct lchoice {
int idx;
schar lev[MAXLINFO];
schar playerlev[MAXLINFO];
xchar dgn[MAXLINFO];
char menuletter;
};
#if 0
static void FDECL(Fread, (genericptr_t, int, int, dlb *));
#endif
static xchar FDECL(dname_to_dnum, (const char *));
static int FDECL(find_branch, (const char *, struct proto_dungeon *));
static xchar FDECL(parent_dnum, (const char *, struct proto_dungeon *));
static int FDECL(level_range, (XCHAR_P, int, int, int,
struct proto_dungeon *, int *));
static xchar FDECL(parent_dlevel, (const char *, struct proto_dungeon *));
static int FDECL(correct_branch_type, (struct tmpbranch *));
static branch *FDECL(add_branch, (int, int, struct proto_dungeon *));
static void FDECL(add_level, (s_level *));
static void FDECL(init_level, (int, int, struct proto_dungeon *));
static int FDECL(possible_places, (int, boolean *,
struct proto_dungeon *));
static xchar FDECL(pick_level, (boolean *, int));
static boolean FDECL(place_level, (int, struct proto_dungeon *));
static int FDECL(get_dgn_flags, (lua_State *));
static boolean FDECL(unplaced_floater, (struct dungeon *));
static boolean FDECL(unreachable_level, (d_level *, BOOLEAN_P));
static void FDECL(tport_menu, (winid, char *, struct lchoice *, d_level *,
BOOLEAN_P));
static const char *FDECL(br_string, (int));
static char FDECL(chr_u_on_lvl, (d_level *));
static void FDECL(print_branch, (winid, int, int, int, BOOLEAN_P,
struct lchoice *));
static mapseen *FDECL(load_mapseen, (NHFILE *));
static void FDECL(save_mapseen, (NHFILE *, mapseen *));
static mapseen *FDECL(find_mapseen, (d_level *));
static mapseen *FDECL(find_mapseen_by_str, (const char *));
static void FDECL(print_mapseen, (winid, mapseen *, int, int, BOOLEAN_P));
static boolean FDECL(interest_mapseen, (mapseen *));
static void FDECL(traverse_mapseenchn, (BOOLEAN_P, winid,
int, int, int *));
static const char *FDECL(seen_string, (XCHAR_P, const char *));
static const char *FDECL(br_string2, (branch *));
static const char *FDECL(shop_string, (int));
static char *FDECL(tunesuffix, (mapseen *, char *));
#ifdef DEBUG
#define DD g.dungeons[i]
static void NDECL(dumpit);
static void
dumpit()
{
int i;
s_level *x;
branch *br;
if (!explicitdebug(__FILE__))
return;
for (i = 0; i < g.n_dgns; i++) {
fprintf(stderr, "\n#%d \"%s\" (%s):\n", i, DD.dname, DD.proto);
fprintf(stderr, " num_dunlevs %d, dunlev_ureached %d\n",
DD.num_dunlevs, DD.dunlev_ureached);
fprintf(stderr, " depth_start %d, ledger_start %d\n",
DD.depth_start, DD.ledger_start);
fprintf(stderr, " flags:%s%s%s\n",
DD.flags.rogue_like ? " rogue_like" : "",
DD.flags.maze_like ? " maze_like" : "",
DD.flags.hellish ? " hellish" : "");
getchar();
}
fprintf(stderr, "\nSpecial levels:\n");
for (x = g.sp_levchn; x; x = x->next) {
fprintf(stderr, "%s (%d): ", x->proto, x->rndlevs);
fprintf(stderr, "on %d, %d; ", x->dlevel.dnum, x->dlevel.dlevel);
fprintf(stderr, "flags:%s%s%s%s\n",
x->flags.rogue_like ? " rogue_like" : "",
x->flags.maze_like ? " maze_like" : "",
x->flags.hellish ? " hellish" : "",
x->flags.town ? " town" : "");
getchar();
}
fprintf(stderr, "\nBranches:\n");
for (br = g.branches; br; br = br->next) {
fprintf(stderr, "%d: %s, end1 %d %d, end2 %d %d, %s\n", br->id,
br->type == BR_STAIR
? "stair"
: br->type == BR_NO_END1
? "no end1"
: br->type == BR_NO_END2
? "no end2"
: br->type == BR_PORTAL
? "portal"
: "unknown",
br->end1.dnum, br->end1.dlevel, br->end2.dnum,
br->end2.dlevel, br->end1_up ? "end1 up" : "end1 down");
}
getchar();
fprintf(stderr, "\nDone\n");
getchar();
}
#endif
/* Save the dungeon structures. */
void
save_dungeon(nhfp, perform_write, free_data)
NHFILE *nhfp;
boolean perform_write, free_data;
{
int count;
branch *curr, *next;
mapseen *curr_ms, *next_ms;
if (perform_write) {
if(nhfp->structlevel) {
bwrite(nhfp->fd, (genericptr_t) &g.n_dgns, sizeof g.n_dgns);
bwrite(nhfp->fd, (genericptr_t) g.dungeons,
sizeof(dungeon) * (unsigned) g.n_dgns);
bwrite(nhfp->fd, (genericptr_t) &g.dungeon_topology, sizeof g.dungeon_topology);
bwrite(nhfp->fd, (genericptr_t) g.tune, sizeof tune);
}
for (count = 0, curr = g.branches; curr; curr = curr->next)
count++;
if (nhfp->structlevel)
bwrite(nhfp->fd, (genericptr_t) &count, sizeof(count));
for (curr = g.branches; curr; curr = curr->next) {
if (nhfp->structlevel)
bwrite(nhfp->fd, (genericptr_t) curr, sizeof(branch));
}
count = maxledgerno();
if (nhfp->structlevel) {
bwrite(nhfp->fd, (genericptr_t) &count, sizeof count);
bwrite(nhfp->fd, (genericptr_t) g.level_info,
(unsigned) count * sizeof(struct linfo));
bwrite(nhfp->fd, (genericptr_t) &g.inv_pos, sizeof g.inv_pos);
}
for (count = 0, curr_ms = g.mapseenchn; curr_ms;
curr_ms = curr_ms->next)
count++;
if (nhfp->structlevel)
bwrite(nhfp->fd, (genericptr_t) &count, sizeof(count));
for (curr_ms = g.mapseenchn; curr_ms; curr_ms = curr_ms->next) {
save_mapseen(nhfp, curr_ms);
}
}
if (free_data) {
for (curr = g.branches; curr; curr = next) {
next = curr->next;
free((genericptr_t) curr);
}
g.branches = 0;
for (curr_ms = g.mapseenchn; curr_ms; curr_ms = next_ms) {
next_ms = curr_ms->next;
if (curr_ms->custom)
free((genericptr_t) curr_ms->custom);
if (curr_ms->final_resting_place)
savecemetery(nhfp, &curr_ms->final_resting_place);
free((genericptr_t) curr_ms);
}
g.mapseenchn = 0;
}
}
/* Restore the dungeon structures. */
void
restore_dungeon(nhfp)
NHFILE *nhfp;
{
branch *curr, *last;
int count = 0, i;
mapseen *curr_ms, *last_ms;
if (nhfp->structlevel) {
mread(nhfp->fd, (genericptr_t) &g.n_dgns, sizeof(g.n_dgns));
mread(nhfp->fd, (genericptr_t) g.dungeons, sizeof(dungeon) * (unsigned) g.n_dgns);
mread(nhfp->fd, (genericptr_t) &g.dungeon_topology, sizeof g.dungeon_topology);
mread(nhfp->fd, (genericptr_t) g.tune, sizeof tune);
}
last = g.branches = (branch *) 0;
for (i = 0; i < g.n_dgns; i++)
g.dungeons[i].themelua = (lua_State *) 0;
if (nhfp->structlevel)
mread(nhfp->fd, (genericptr_t) &count, sizeof(count));
for (i = 0; i < count; i++) {
curr = (branch *) alloc(sizeof(branch));
if (nhfp->structlevel)
mread(nhfp->fd, (genericptr_t) curr, sizeof(branch));
curr->next = (branch *) 0;
if (last)
last->next = curr;
else
g.branches = curr;
last = curr;
}
if (nhfp->structlevel)
mread(nhfp->fd, (genericptr_t) &count, sizeof(count));
if (count >= MAXLINFO)
panic("level information count larger (%d) than allocated size",
count);
if (nhfp->structlevel)
mread(nhfp->fd, (genericptr_t) g.level_info,
(unsigned) count * sizeof(struct linfo));
if (nhfp->structlevel) {
mread(nhfp->fd, (genericptr_t) &g.inv_pos, sizeof g.inv_pos);
mread(nhfp->fd, (genericptr_t) &count, sizeof(count));
}
last_ms = (mapseen *) 0;
for (i = 0; i < count; i++) {
curr_ms = load_mapseen(nhfp);
curr_ms->next = (mapseen *) 0;
if (last_ms)
last_ms->next = curr_ms;
else
g.mapseenchn = curr_ms;
last_ms = curr_ms;
}
}
#if 0
static void
Fread(ptr, size, nitems, stream)
genericptr_t ptr;
int size, nitems;
dlb *stream;
{
int cnt;
if ((cnt = dlb_fread(ptr, size, nitems, stream)) != nitems) {
panic(
"Premature EOF on dungeon description file!\r\nExpected %d bytes - got %d.",
(size * nitems), (size * cnt));
nh_terminate(EXIT_FAILURE);
}
}
#endif
static xchar
dname_to_dnum(s)
const char *s;
{
xchar i;
for (i = 0; i < g.n_dgns; i++)
if (!strcmp(g.dungeons[i].dname, s))
return i;
panic("Couldn't resolve dungeon number for name \"%s\".", s);
/*NOT REACHED*/
return (xchar) 0;
}
s_level *
find_level(s)
const char *s;
{
s_level *curr;
for (curr = g.sp_levchn; curr; curr = curr->next)
if (!strcmpi(s, curr->proto))
break;
return curr;
}
/* Find the branch that links the named dungeon. */
static int
find_branch(s, pd)
const char *s; /* dungeon name */
struct proto_dungeon *pd;
{
int i;
if (pd) {
for (i = 0; i < pd->n_brs; i++)
if (!strcmp(pd->tmpbranch[i].name, s))
break;
if (i == pd->n_brs)
panic("find_branch: can't find %s", s);
} else {
/* support for level tport by name */
branch *br;
const char *dnam;
for (br = g.branches; br; br = br->next) {
dnam = g.dungeons[br->end2.dnum].dname;
if (!strcmpi(dnam, s)
|| (!strncmpi(dnam, "The ", 4) && !strcmpi(dnam + 4, s)))
break;
}
i = br ? ((ledger_no(&br->end1) << 8) | ledger_no(&br->end2)) : -1;
}
return i;
}
/*
* Find the "parent" by searching the prototype branch list for the branch
* listing, then figuring out to which dungeon it belongs.
*/
static xchar
parent_dnum(s, pd)
const char *s; /* dungeon name */
struct proto_dungeon *pd;
{
int i;
xchar pdnum;
i = find_branch(s, pd);
/*
* Got branch, now find parent dungeon. Stop if we have reached
* "this" dungeon (if we haven't found it by now it is an error).
*/
for (pdnum = 0; strcmp(pd->tmpdungeon[pdnum].name, s); pdnum++)
if ((i -= pd->tmpdungeon[pdnum].branches) < 0)
return pdnum;
panic("parent_dnum: couldn't resolve branch.");
/*NOT REACHED*/
return (xchar) 0;
}
/*
* Return a starting point and number of successive positions a level
* or dungeon entrance can occupy.
*
* Note: This follows the acouple (instead of the rcouple) rules for a
* negative random component (randc < 0). These rules are found
* in dgn_comp.y. The acouple [absolute couple] section says that
* a negative random component means from the (adjusted) base to the
* end of the dungeon.
*/
static int
level_range(dgn, base, randc, chain, pd, adjusted_base)
xchar dgn;
int base, randc, chain;
struct proto_dungeon *pd;
int *adjusted_base;
{
int lmax = g.dungeons[dgn].num_dunlevs;
if (chain >= 0) { /* relative to a special level */
s_level *levtmp = pd->final_lev[chain];
if (!levtmp)
panic("level_range: empty chain level!");
base += levtmp->dlevel.dlevel;
} else { /* absolute in the dungeon */
/* from end of dungeon */
if (base < 0)
base = (lmax + base + 1);
}
if (base < 1 || base > lmax)
panic("level_range: base value out of range");
*adjusted_base = base;
if (randc == -1) { /* from base to end of dungeon */
return (lmax - base + 1);
} else if (randc) {
/* make sure we don't run off the end of the dungeon */
return (((base + randc - 1) > lmax) ? lmax - base + 1 : randc);
} /* else only one choice */
return 1;
}
static xchar
parent_dlevel(s, pd)
const char *s;
struct proto_dungeon *pd;
{
int i, j, num, base, dnum = parent_dnum(s, pd);
branch *curr;
i = find_branch(s, pd);
num = level_range(dnum, pd->tmpbranch[i].lev.base,
pd->tmpbranch[i].lev.rand, pd->tmpbranch[i].chain, pd,
&base);
/* KMH -- Try our best to find a level without an existing branch */
i = j = rn2(num);
do {
if (++i >= num)
i = 0;
for (curr = g.branches; curr; curr = curr->next)
if ((curr->end1.dnum == dnum && curr->end1.dlevel == base + i)
|| (curr->end2.dnum == dnum && curr->end2.dlevel == base + i))
break;
} while (curr && i != j);
return (base + i);
}
/* Convert from the temporary branch type to the dungeon branch type. */
static int
correct_branch_type(tbr)
struct tmpbranch *tbr;
{
switch (tbr->type) {
case TBR_STAIR:
return BR_STAIR;
case TBR_NO_UP:
return tbr->up ? BR_NO_END1 : BR_NO_END2;
case TBR_NO_DOWN:
return tbr->up ? BR_NO_END2 : BR_NO_END1;
case TBR_PORTAL:
return BR_PORTAL;
}
impossible("correct_branch_type: unknown branch type");
return BR_STAIR;
}
/*
* Add the given branch to the branch list. The branch list is ordered
* by end1 dungeon and level followed by end2 dungeon and level. If
* extract_first is true, then the branch is already part of the list
* but needs to be repositioned.
*/
void
insert_branch(new_branch, extract_first)
branch *new_branch;
boolean extract_first;
{
branch *curr, *prev;
long new_val, curr_val, prev_val;
if (extract_first) {
for (prev = 0, curr = g.branches; curr; prev = curr, curr = curr->next)
if (curr == new_branch)
break;
if (!curr)
panic("insert_branch: not found");
if (prev)
prev->next = curr->next;
else
g.branches = curr->next;
}
new_branch->next = (branch *) 0;
/* Convert the branch into a unique number so we can sort them. */
#define branch_val(bp) \
((((long) (bp)->end1.dnum * (MAXLEVEL + 1) + (long) (bp)->end1.dlevel) \
* (MAXDUNGEON + 1) * (MAXLEVEL + 1)) \
+ ((long) (bp)->end2.dnum * (MAXLEVEL + 1) + (long) (bp)->end2.dlevel))
/*
* Insert the new branch into the correct place in the branch list.
*/
prev = (branch *) 0;
prev_val = -1;
new_val = branch_val(new_branch);
for (curr = g.branches; curr;
prev_val = curr_val, prev = curr, curr = curr->next) {
curr_val = branch_val(curr);
if (prev_val < new_val && new_val <= curr_val)
break;
}
if (prev) {
new_branch->next = curr;
prev->next = new_branch;
} else {
new_branch->next = g.branches;
g.branches = new_branch;
}
}
/* Add a dungeon branch to the branch list. */
static branch *
add_branch(dgn, child_entry_level, pd)
int dgn;
int child_entry_level;
struct proto_dungeon *pd;
{
static int branch_id = 0;
int branch_num;
branch *new_branch;
branch_num = find_branch(g.dungeons[dgn].dname, pd);
new_branch = (branch *) alloc(sizeof(branch));
(void) memset((genericptr_t)new_branch, 0, sizeof(branch));
new_branch->next = (branch *) 0;
new_branch->id = branch_id++;
new_branch->type = correct_branch_type(&pd->tmpbranch[branch_num]);
new_branch->end1.dnum = parent_dnum(g.dungeons[dgn].dname, pd);
new_branch->end1.dlevel = parent_dlevel(g.dungeons[dgn].dname, pd);
new_branch->end2.dnum = dgn;
new_branch->end2.dlevel = child_entry_level;
new_branch->end1_up = pd->tmpbranch[branch_num].up ? TRUE : FALSE;
insert_branch(new_branch, FALSE);
return new_branch;
}
/*
* Add new level to special level chain. Insert it in level order with the
* other levels in this dungeon. This assumes that we are never given a
* level that has a dungeon number less than the dungeon number of the
* last entry.
*/
static void
add_level(new_lev)
s_level *new_lev;
{
s_level *prev, *curr;
prev = (s_level *) 0;
for (curr = g.sp_levchn; curr; curr = curr->next) {
if (curr->dlevel.dnum == new_lev->dlevel.dnum
&& curr->dlevel.dlevel > new_lev->dlevel.dlevel)
break;
prev = curr;
}
if (!prev) {
new_lev->next = g.sp_levchn;
g.sp_levchn = new_lev;
} else {
new_lev->next = curr;
prev->next = new_lev;
}
}
static void
init_level(dgn, proto_index, pd)
int dgn, proto_index;
struct proto_dungeon *pd;
{
s_level *new_level;
struct tmplevel *tlevel = &pd->tmplevel[proto_index];
pd->final_lev[proto_index] = (s_level *) 0; /* no "real" level */
if (!wizard && tlevel->chance <= rn2(100))
return;
pd->final_lev[proto_index] = new_level =
(s_level *) alloc(sizeof(s_level));
(void) memset((genericptr_t)new_level, 0, sizeof(s_level));
/* load new level with data */
Strcpy(new_level->proto, tlevel->name);
new_level->boneid = tlevel->boneschar;
new_level->dlevel.dnum = dgn;
new_level->dlevel.dlevel = 0; /* for now */
new_level->flags.town = !!(tlevel->flags & TOWN);
new_level->flags.hellish = !!(tlevel->flags & HELLISH);
new_level->flags.maze_like = !!(tlevel->flags & MAZELIKE);
new_level->flags.rogue_like = !!(tlevel->flags & ROGUELIKE);
new_level->flags.align = ((tlevel->flags & D_ALIGN_MASK) >> 4);
if (!new_level->flags.align)
new_level->flags.align =
((pd->tmpdungeon[dgn].flags & D_ALIGN_MASK) >> 4);
new_level->rndlevs = tlevel->rndlevs;
new_level->next = (s_level *) 0;
}
static int
possible_places(idx, map, pd)
int idx; /* prototype index */
boolean *map; /* array MAXLEVEL+1 in length */
struct proto_dungeon *pd;
{
int i, start, count;
s_level *lev = pd->final_lev[idx];
/* init level possibilities */
for (i = 0; i <= MAXLEVEL; i++)
map[i] = FALSE;
/* get base and range and set those entries to true */
count = level_range(lev->dlevel.dnum, pd->tmplevel[idx].lev.base,
pd->tmplevel[idx].lev.rand, pd->tmplevel[idx].chain,
pd, &start);
for (i = start; i < start + count; i++)
map[i] = TRUE;
/* mark off already placed levels */
for (i = pd->start; i < idx; i++) {
if (pd->final_lev[i] && map[pd->final_lev[i]->dlevel.dlevel]) {
map[pd->final_lev[i]->dlevel.dlevel] = FALSE;
--count;
}
}
return count;
}
/* Pick the nth TRUE entry in the given boolean array. */
static xchar
pick_level(map, nth)
boolean *map; /* an array MAXLEVEL+1 in size */
int nth;
{
int i;
for (i = 1; i <= MAXLEVEL; i++)
if (map[i] && !nth--)
return (xchar) i;
panic("pick_level: ran out of valid levels");
return 0;
}
#ifdef DDEBUG
static void FDECL(indent, (int));
static void
indent(d)
int d;
{
while (d-- > 0)
fputs(" ", stderr);
}
#endif
/*
* Place a level. First, find the possible places on a dungeon map
* template. Next pick one. Then try to place the next level. If
* successful, we're done. Otherwise, try another (and another) until
* all possible places have been tried. If all possible places have
* been exhausted, return false.
*/
static boolean
place_level(proto_index, pd)
int proto_index;
struct proto_dungeon *pd;
{
boolean map[MAXLEVEL + 1]; /* valid levels are 1..MAXLEVEL inclusive */
s_level *lev;
int npossible;
#ifdef DDEBUG
int i;
#endif
if (proto_index == pd->n_levs)
return TRUE; /* at end of proto levels */
lev = pd->final_lev[proto_index];
/* No level created for this prototype, goto next. */
if (!lev)
return place_level(proto_index + 1, pd);
npossible = possible_places(proto_index, map, pd);
for (; npossible; --npossible) {
lev->dlevel.dlevel = pick_level(map, rn2(npossible));
#ifdef DDEBUG
indent(proto_index - pd->start);
fprintf(stderr, "%s: trying %d [ ", lev->proto, lev->dlevel.dlevel);
for (i = 1; i <= MAXLEVEL; i++)
if (map[i])
fprintf(stderr, "%d ", i);
fprintf(stderr, "]\n");
#endif
if (place_level(proto_index + 1, pd))
return TRUE;
map[lev->dlevel.dlevel] = FALSE; /* this choice didn't work */
}
#ifdef DDEBUG
indent(proto_index - pd->start);
fprintf(stderr, "%s: failed\n", lev->proto);
#endif
return FALSE;
}
static struct level_map {
const char *lev_name;
d_level *lev_spec;
} level_map[] = { { "air", &air_level },
{ "asmodeus", &asmodeus_level },
{ "astral", &astral_level },
{ "baalz", &baalzebub_level },
{ "bigrm", &bigroom_level },
{ "castle", &stronghold_level },
{ "earth", &earth_level },
{ "fakewiz1", &portal_level },
{ "fire", &fire_level },
{ "juiblex", &juiblex_level },
{ "knox", &knox_level },
{ "medusa", &medusa_level },
{ "oracle", &oracle_level },
{ "orcus", &orcus_level },
{ "rogue", &rogue_level },
{ "sanctum", &sanctum_level },
{ "valley", &valley_level },
{ "water", &water_level },
{ "wizard1", &wiz1_level },
{ "wizard2", &wiz2_level },
{ "wizard3", &wiz3_level },
{ "minend", &mineend_level },
{ "soko1", &sokoend_level },
{ X_START, &qstart_level },
{ X_LOCATE, &qlocate_level },
{ X_GOAL, &nemesis_level },
{ "", (d_level *) 0 } };
int
get_dgn_flags(L)
lua_State *L;
{
int dgn_flags = 0;
static const char *const flagstrs[] = {
"town", "hellish", "mazelike", "roguelike", NULL
};
static const int flagstrs2i[] = { TOWN, HELLISH, MAZELIKE, ROGUELIKE, 0 };
lua_getfield(L, -1, "flags");
if (lua_type(L, -1) == LUA_TTABLE) {
int f, nflags;
lua_len(L, -1);
nflags = (int) lua_tointeger(L, -1);
lua_pop(L, 1);
for (f = 0; f < nflags; f++) {
lua_pushinteger(L, f + 1);
lua_gettable(L, -2);
if (lua_type(L, -1) == LUA_TSTRING) {
dgn_flags |= flagstrs2i[luaL_checkoption(L, -1, NULL,
flagstrs)];
lua_pop(L, 1);
} else
impossible("flags[%i] is not a string", f);
}
} else if (lua_type(L, -1) == LUA_TSTRING) {
dgn_flags |= flagstrs2i[luaL_checkoption(L, -1, NULL, flagstrs)];
} else if (lua_type(L, -1) != LUA_TNIL)
impossible("flags is not an array or string");
lua_pop(L, 1);
return dgn_flags;
}
/* initialize the "dungeon" structs */
void
init_dungeons()
{
static const char *const dgnaligns[] = {
"unaligned", "noalign", "lawful", "neutral", "chaotic", NULL
};
static const int dgnaligns2i[] = {
D_ALIGN_NONE, D_ALIGN_NONE, D_ALIGN_LAWFUL,
D_ALIGN_NEUTRAL, D_ALIGN_CHAOTIC, D_ALIGN_NONE
};
lua_State *L;
register int i, cl = 0;
register s_level *x;
struct proto_dungeon pd;
struct level_map *lev_map;
int tidx;
(void) memset(&pd, 0, sizeof (struct proto_dungeon));
pd.n_levs = pd.n_brs = 0;
L = nhl_init();
if (!nhl_loadlua(L, DUNGEON_FILE)) {
char tbuf[BUFSZ];
Sprintf(tbuf, "Cannot open dungeon description - \"%s", DUNGEON_FILE);
#ifdef DLBRSRC /* using a resource from the executable */
Strcat(tbuf, "\" resource!");
#else /* using a file or DLB file */
#if defined(DLB)
Strcat(tbuf, "\" from ");
#ifdef PREFIXES_IN_USE
Strcat(tbuf, "\n\"");
if (g.fqn_prefix[DATAPREFIX])
Strcat(tbuf, g.fqn_prefix[DATAPREFIX]);
#else
Strcat(tbuf, "\"");
#endif
Strcat(tbuf, DLBFILE);
#endif
Strcat(tbuf, "\" file!");
#endif
#ifdef WIN32
interject_assistance(1, INTERJECT_PANIC, (genericptr_t) tbuf,
(genericptr_t) g.fqn_prefix[DATAPREFIX]);
#endif
panic1(tbuf);
}
if (iflags.window_inited)
clear_nhwindow(WIN_MAP);
g.sp_levchn = (s_level *) 0;
lua_settop(L, 0);
lua_getglobal(L, "dungeon");
if (!lua_istable(L, -1))
panic("dungeon is not a lua table");
lua_len(L, -1);
g.n_dgns = (int) lua_tointeger(L, -1);
lua_pop(L, 1);
pd.start = 0;
pd.n_levs = 0;
pd.n_brs = 0;
/*
* Read in each dungeon and transfer the results to the internal
* dungeon arrays.
*/
if (g.n_dgns >= MAXDUNGEON)
panic("init_dungeons: too many dungeons");
tidx = lua_gettop(L);
lua_pushnil(L); /* first key */
i = 0;
while (lua_next(L, tidx) != 0) {
char *dgn_name, *dgn_bonetag, *dgn_protoname, *dgn_fill;
char *dgn_themerms;
int dgn_base, dgn_range, dgn_align, dgn_entry, dgn_chance, dgn_flags;
if (!lua_istable(L, -1))
panic("dungeon[%i] is not a lua table", i);
dgn_name = get_table_str(L, "name");
dgn_bonetag = get_table_str_opt(L, "bonetag", emptystr); /* TODO: single char or "none" */
dgn_protoname = get_table_str_opt(L, "protofile", emptystr);
dgn_base = get_table_int(L, "base");
dgn_range = get_table_int_opt(L, "range", 0);
dgn_align = dgnaligns2i[get_table_option(L, "alignment",
"unaligned", dgnaligns)];
dgn_entry = get_table_int_opt(L, "entry", 0);
dgn_chance = get_table_int_opt(L, "chance", 100);
dgn_flags = get_dgn_flags(L);
dgn_fill = get_table_str_opt(L, "lvlfill", emptystr);
dgn_themerms = get_table_str_opt(L, "themerooms", emptystr);
debugpline4("DUNGEON[%i]: %s, base=(%i,%i)",
i, dgn_name, dgn_base, dgn_range);
if (!wizard && dgn_chance && (dgn_chance <= rn2(100))) {
debugpline1("IGNORING %s", dgn_name);
g.n_dgns--;
lua_pop(L, 1); /* pop the dungeon table */
free((genericptr_t) dgn_name);
free((genericptr_t) dgn_bonetag);
free((genericptr_t) dgn_protoname);
free((genericptr_t) dgn_fill);
free((genericptr_t) dgn_themerms);
continue;
}
/* levels begin */
lua_getfield(L, -1, "levels");
if (lua_type(L, -1) == LUA_TTABLE) {
char *lvl_name, *lvl_bonetag, *lvl_chain;
int lvl_base, lvl_range, lvl_nlevels, lvl_chance,
lvl_align, lvl_flags;
struct tmplevel *tmpl;
int bi, f, nlevels;
lua_len(L, -1);
nlevels = (int) lua_tointeger(L, -1);
pd.tmpdungeon[i].levels = nlevels;
lua_pop(L, 1);
for (f = 0; f < nlevels; f++) {
lua_pushinteger(L, f + 1);
lua_gettable(L, -2);
if (lua_type(L, -1) == LUA_TTABLE) {
lvl_name = get_table_str(L, "name");
lvl_bonetag = get_table_str_opt(L, "bonetag", emptystr);
lvl_chain = get_table_str_opt(L, "chainlevel", NULL);
lvl_base = get_table_int(L, "base");
lvl_range = get_table_int_opt(L, "range", 0);
lvl_nlevels = get_table_int_opt(L, "nlevels", 0);
lvl_chance = get_table_int_opt(L, "chance", 100);
lvl_align = dgnaligns2i[get_table_option(L, "alignment",
"unaligned", dgnaligns)];
lvl_flags = get_dgn_flags(L);
/* array index is offset by cumulative number of levels
defined for preceding branches (iterations of 'while'
loop we're inside, not branch connections below) */
tmpl = &pd.tmplevel[pd.n_levs + f];
debugpline4("LEVEL[%i]:%s,(%i,%i)",
f, lvl_name, lvl_base, lvl_range);
tmpl->name = lvl_name;
tmpl->chainlvl = lvl_chain;
tmpl->lev.base = lvl_base;
tmpl->lev.rand = lvl_range;
tmpl->chance = lvl_chance;
tmpl->rndlevs = lvl_nlevels;
tmpl->flags = lvl_flags | lvl_align;
tmpl->boneschar = *lvl_bonetag ? *lvl_bonetag : 0;
free(lvl_bonetag);
tmpl->chain = -1;
if (lvl_chain) {
debugpline1("CHAINLEVEL: %s", lvl_chain);
for (bi = 0; bi < pd.n_levs + f; bi++) {
debugpline2("checking(%i):%s",
bi, pd.tmplevel[bi].name);
if (!strcmp(pd.tmplevel[bi].name, lvl_chain)) {
tmpl->chain = bi;
break;
}
}
if (tmpl->chain == -1)
panic("Could not chain level %s to %s",
lvl_name, lvl_chain);
/* free(lvl_chain); -- recorded in pd.tmplevel[] */
}
} else
panic("dungeon[%i].levels[%i] is not a hash", i, f);
lua_pop(L, 1);
}
pd.n_levs += nlevels;
if (pd.n_levs > LEV_LIMIT)
panic("init_dungeon: too many special levels");
} else if (lua_type(L, -1) != LUA_TNIL)
panic("dungeon[%i].levels is not an array of hashes", i);
lua_pop(L, 1);
/* levels end */
/* branches begin */
lua_getfield(L, -1, "branches");
if (lua_type(L, -1) == LUA_TTABLE) {
static const char *const brdirstr[] = { "up", "down", 0 };
static const int brdirstr2i[] = { TRUE, FALSE, FALSE };
static const char *const brtypes[] = {
"stair", "portal", "no_down", "no_up", 0
};
static const int brtypes2i[] = {
TBR_STAIR, TBR_PORTAL, TBR_NO_DOWN, TBR_NO_UP, TBR_STAIR
};
char *br_name, *br_chain;
int br_base, br_range, br_type, br_up;
struct tmpbranch *tmpb;
int bi, f, nbranches;
lua_len(L, -1);
nbranches = (int) lua_tointeger(L, -1);
pd.tmpdungeon[i].branches = nbranches;
lua_pop(L, 1);
for (f = 0; f < nbranches; f++) {
lua_pushinteger(L, f + 1);
lua_gettable(L, -2);
if (lua_type(L, -1) == LUA_TTABLE) {
br_name = get_table_str(L, "name");
br_chain = get_table_str_opt(L, "chainlevel", NULL);
br_base = get_table_int(L, "base");
br_range = get_table_int_opt(L, "range", 0);
br_type = brtypes2i[get_table_option(L, "branchtype",
"stair", brtypes)];
br_up = brdirstr2i[get_table_option(L, "direction",
"down", brdirstr)];
tmpb = &pd.tmpbranch[pd.n_brs + f];
debugpline4("BRANCH[%i]:%s,(%i,%i)",
f, br_name, br_base, br_range);
tmpb->name = br_name;
tmpb->lev.base = br_base;
tmpb->lev.rand = br_range;
tmpb->type = br_type;
tmpb->up = br_up;
tmpb->chain = -1;
if (br_chain) {
debugpline1("CHAINBRANCH:%s", br_chain);
for (bi = 0; bi < pd.n_levs + f - 1; bi++)
if (!strcmp(pd.tmplevel[bi].name, br_chain)) {
tmpb->chain = bi;
break;