forked from NetHack/NetHack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsp_lev.c
6059 lines (5441 loc) · 170 KB
/
sp_lev.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.6 sp_lev.c $NHDT-Date: 1567805254 2019/09/06 21:27:34 $ $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.117 $ */
/* Copyright (c) 1989 by Jean-Christophe Collet */
/* NetHack may be freely redistributed. See license for details. */
/*
* This file contains the various functions that are related to the special
* levels.
*
* It contains also the special level loader.
*/
#include "hack.h"
#include "dlb.h"
#include "sp_lev.h"
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4244)
#endif
typedef void FDECL((*select_iter_func), (int, int, genericptr));
extern void FDECL(mkmap, (lev_init *));
STATIC_DCL void NDECL(solidify_map);
STATIC_DCL void FDECL(splev_stack_init, (struct splevstack *));
STATIC_DCL void FDECL(splev_stack_done, (struct splevstack *));
STATIC_DCL void FDECL(splev_stack_push, (struct splevstack *,
struct opvar *));
STATIC_DCL struct opvar *FDECL(splev_stack_pop, (struct splevstack *));
STATIC_DCL struct splevstack *FDECL(splev_stack_reverse,
(struct splevstack *));
STATIC_DCL struct opvar *FDECL(opvar_new_str, (char *));
STATIC_DCL struct opvar *FDECL(opvar_new_int, (long));
STATIC_DCL struct opvar *FDECL(opvar_new_coord, (int, int));
#if 0
STATIC_DCL struct opvar * FDECL(opvar_new_region, (int,int, int,int));
#endif /*0*/
STATIC_DCL struct opvar *FDECL(opvar_clone, (struct opvar *));
STATIC_DCL struct opvar *FDECL(opvar_var_conversion, (struct sp_coder *,
struct opvar *));
STATIC_DCL struct splev_var *FDECL(opvar_var_defined, (struct sp_coder *,
char *));
STATIC_DCL struct opvar *FDECL(splev_stack_getdat, (struct sp_coder *,
XCHAR_P));
STATIC_DCL struct opvar *FDECL(splev_stack_getdat_any, (struct sp_coder *));
STATIC_DCL void FDECL(variable_list_del, (struct splev_var *));
STATIC_DCL void FDECL(lvlfill_maze_grid, (int, int, int, int, SCHAR_P));
STATIC_DCL void FDECL(lvlfill_solid, (SCHAR_P, SCHAR_P));
STATIC_DCL void FDECL(set_wall_property, (XCHAR_P, XCHAR_P, XCHAR_P, XCHAR_P,
int));
STATIC_DCL void NDECL(shuffle_alignments);
STATIC_DCL void NDECL(count_features);
STATIC_DCL void NDECL(remove_boundary_syms);
STATIC_DCL void FDECL(set_door_orientation, (int, int));
STATIC_DCL void FDECL(maybe_add_door, (int, int, struct mkroom *));
STATIC_DCL void NDECL(link_doors_rooms);
STATIC_DCL void NDECL(fill_rooms);
STATIC_DCL int NDECL(rnddoor);
STATIC_DCL int NDECL(rndtrap);
STATIC_DCL void FDECL(get_location, (schar *, schar *, int, struct mkroom *));
STATIC_DCL boolean FDECL(is_ok_location, (SCHAR_P, SCHAR_P, int));
STATIC_DCL unpacked_coord FDECL(get_unpacked_coord, (long, int));
STATIC_DCL void FDECL(get_location_coord, (schar *, schar *, int,
struct mkroom *, long));
STATIC_DCL void FDECL(get_room_loc, (schar *, schar *, struct mkroom *));
STATIC_DCL void FDECL(get_free_room_loc, (schar *, schar *,
struct mkroom *, packed_coord));
STATIC_DCL boolean FDECL(create_subroom, (struct mkroom *, XCHAR_P, XCHAR_P,
XCHAR_P, XCHAR_P, XCHAR_P, XCHAR_P));
STATIC_DCL void FDECL(create_door, (room_door *, struct mkroom *));
STATIC_DCL void FDECL(create_trap, (spltrap *, struct mkroom *));
STATIC_DCL int FDECL(noncoalignment, (ALIGNTYP_P));
STATIC_DCL boolean FDECL(m_bad_boulder_spot, (int, int));
STATIC_DCL int FDECL(pm_to_humidity, (struct permonst *));
STATIC_DCL void FDECL(create_monster, (monster *, struct mkroom *));
STATIC_DCL void FDECL(create_object, (object *, struct mkroom *));
STATIC_DCL void FDECL(create_altar, (altar *, struct mkroom *));
STATIC_DCL void FDECL(replace_terrain, (replaceterrain *, struct mkroom *));
STATIC_DCL boolean FDECL(search_door, (struct mkroom *,
xchar *, xchar *, XCHAR_P, int));
STATIC_DCL void NDECL(fix_stair_rooms);
STATIC_DCL void FDECL(create_corridor, (corridor *));
STATIC_DCL struct mkroom *FDECL(build_room, (room *, struct mkroom *));
STATIC_DCL void FDECL(light_region, (region *));
STATIC_DCL void FDECL(wallify_map, (int, int, int, int));
STATIC_DCL void FDECL(maze1xy, (coord *, int));
STATIC_DCL void NDECL(fill_empty_maze);
STATIC_DCL boolean FDECL(sp_level_loader, (dlb *, sp_lev *));
STATIC_DCL boolean FDECL(sp_level_free, (sp_lev *));
STATIC_DCL void FDECL(splev_initlev, (lev_init *));
STATIC_DCL struct sp_frame *FDECL(frame_new, (long));
STATIC_DCL void FDECL(frame_del, (struct sp_frame *));
STATIC_DCL void FDECL(spo_frame_push, (struct sp_coder *));
STATIC_DCL void FDECL(spo_frame_pop, (struct sp_coder *));
STATIC_DCL long FDECL(sp_code_jmpaddr, (long, long));
STATIC_DCL void FDECL(spo_call, (struct sp_coder *));
STATIC_DCL void FDECL(spo_return, (struct sp_coder *));
STATIC_DCL void FDECL(spo_end_moninvent, (struct sp_coder *));
STATIC_DCL void FDECL(spo_pop_container, (struct sp_coder *));
STATIC_DCL void FDECL(spo_message, (struct sp_coder *));
STATIC_DCL void FDECL(spo_monster, (struct sp_coder *));
STATIC_DCL void FDECL(spo_object, (struct sp_coder *));
STATIC_DCL void FDECL(spo_level_flags, (struct sp_coder *));
STATIC_DCL void FDECL(spo_initlevel, (struct sp_coder *));
STATIC_DCL void FDECL(spo_engraving, (struct sp_coder *));
STATIC_DCL void FDECL(spo_mineralize, (struct sp_coder *));
STATIC_DCL void FDECL(spo_room, (struct sp_coder *));
STATIC_DCL void FDECL(spo_endroom, (struct sp_coder *));
STATIC_DCL void FDECL(spo_stair, (struct sp_coder *));
STATIC_DCL void FDECL(spo_ladder, (struct sp_coder *));
STATIC_DCL void FDECL(spo_grave, (struct sp_coder *));
STATIC_DCL void FDECL(spo_altar, (struct sp_coder *));
STATIC_DCL void FDECL(spo_trap, (struct sp_coder *));
STATIC_DCL void FDECL(spo_gold, (struct sp_coder *));
STATIC_DCL void FDECL(spo_corridor, (struct sp_coder *));
STATIC_DCL void FDECL(selection_setpoint, (int, int, struct opvar *, XCHAR_P));
STATIC_DCL struct opvar *FDECL(selection_not, (struct opvar *));
STATIC_DCL struct opvar *FDECL(selection_logical_oper, (struct opvar *,
struct opvar *, CHAR_P));
STATIC_DCL struct opvar *FDECL(selection_filter_mapchar, (struct opvar *,
struct opvar *));
STATIC_DCL void FDECL(selection_filter_percent, (struct opvar *, int));
STATIC_DCL int FDECL(selection_rndcoord, (struct opvar *, schar *, schar *,
BOOLEAN_P));
STATIC_DCL void FDECL(selection_do_grow, (struct opvar *, int));
STATIC_DCL int FDECL(floodfillchk_match_under, (int, int));
STATIC_DCL int FDECL(floodfillchk_match_accessible, (int, int));
STATIC_DCL boolean FDECL(sel_flood_havepoint, (int, int,
xchar *, xchar *, int));
STATIC_DCL void FDECL(selection_do_ellipse, (struct opvar *, int, int,
int, int, int));
STATIC_DCL long FDECL(line_dist_coord, (long, long, long, long, long, long));
STATIC_DCL void FDECL(selection_do_gradient, (struct opvar *, long, long, long,
long, long, long, long, long));
STATIC_DCL void FDECL(selection_do_line, (SCHAR_P, SCHAR_P, SCHAR_P, SCHAR_P,
struct opvar *));
STATIC_DCL void FDECL(selection_do_randline, (SCHAR_P, SCHAR_P, SCHAR_P,
SCHAR_P, SCHAR_P, SCHAR_P,
struct opvar *));
STATIC_DCL void FDECL(selection_iterate, (struct opvar *, select_iter_func,
genericptr_t));
STATIC_DCL void FDECL(sel_set_ter, (int, int, genericptr_t));
STATIC_DCL void FDECL(sel_set_feature, (int, int, genericptr_t));
STATIC_DCL void FDECL(sel_set_door, (int, int, genericptr_t));
STATIC_DCL void FDECL(spo_door, (struct sp_coder *));
STATIC_DCL void FDECL(spo_feature, (struct sp_coder *));
STATIC_DCL void FDECL(spo_terrain, (struct sp_coder *));
STATIC_DCL void FDECL(spo_replace_terrain, (struct sp_coder *));
STATIC_DCL boolean FDECL(generate_way_out_method, (int, int, struct opvar *));
STATIC_DCL void NDECL(ensure_way_out);
STATIC_DCL void FDECL(spo_levregion, (struct sp_coder *));
STATIC_DCL void FDECL(spo_region, (struct sp_coder *));
STATIC_DCL void FDECL(spo_drawbridge, (struct sp_coder *));
STATIC_DCL void FDECL(spo_mazewalk, (struct sp_coder *));
STATIC_DCL void FDECL(spo_wall_property, (struct sp_coder *));
STATIC_DCL void FDECL(spo_room_door, (struct sp_coder *));
STATIC_DCL void FDECL(sel_set_wallify, (int, int, genericptr_t));
STATIC_DCL void FDECL(spo_wallify, (struct sp_coder *));
STATIC_DCL void FDECL(spo_map, (struct sp_coder *));
STATIC_DCL void FDECL(spo_jmp, (struct sp_coder *, sp_lev *));
STATIC_DCL void FDECL(spo_conditional_jump, (struct sp_coder *, sp_lev *));
STATIC_DCL void FDECL(spo_var_init, (struct sp_coder *));
#if 0
STATIC_DCL long FDECL(opvar_array_length, (struct sp_coder *));
#endif /*0*/
STATIC_DCL void FDECL(spo_shuffle_array, (struct sp_coder *));
STATIC_DCL boolean FDECL(sp_level_coder, (sp_lev *));
#define LEFT 1
#define H_LEFT 2
#define CENTER 3
#define H_RIGHT 4
#define RIGHT 5
#define TOP 1
#define BOTTOM 5
#define sq(x) ((x) * (x))
#define XLIM 4
#define YLIM 3
#define Fread (void) dlb_fread
#define Fgetc (schar) dlb_fgetc
#define New(type) (type *) alloc(sizeof(type))
#define NewTab(type, size) (type **) alloc(sizeof(type *) * (unsigned) size)
#define Free(ptr) if (ptr) free((genericptr_t) (ptr))
extern struct engr *head_engr;
extern int min_rx, max_rx, min_ry, max_ry; /* from mkmap.c */
/* positions touched by level elements explicitly defined in the des-file */
static char SpLev_Map[COLNO][ROWNO];
static aligntyp ralign[3] = { AM_CHAOTIC, AM_NEUTRAL, AM_LAWFUL };
static NEARDATA xchar xstart, ystart;
static NEARDATA char xsize, ysize;
char *lev_message = 0;
lev_region *lregions = 0;
int num_lregions = 0;
static boolean splev_init_present = FALSE;
static boolean icedpools = FALSE;
static int mines_prize_count = 0, soko_prize_count = 0; /* achievements */
static struct obj *container_obj[MAX_CONTAINMENT];
static int container_idx = 0;
static struct monst *invent_carrying_monster = NULL;
#define SPLEV_STACK_RESERVE 128
void
solidify_map()
{
xchar x, y;
for (x = 0; x < COLNO; x++)
for (y = 0; y < ROWNO; y++)
if (IS_STWALL(levl[x][y].typ) && !SpLev_Map[x][y])
levl[x][y].wall_info |= (W_NONDIGGABLE | W_NONPASSWALL);
}
void
splev_stack_init(st)
struct splevstack *st;
{
if (st) {
st->depth = 0;
st->depth_alloc = SPLEV_STACK_RESERVE;
st->stackdata =
(struct opvar **) alloc(st->depth_alloc * sizeof (struct opvar *));
}
}
void
splev_stack_done(st)
struct splevstack *st;
{
if (st) {
int i;
if (st->stackdata && st->depth) {
for (i = 0; i < st->depth; i++) {
switch (st->stackdata[i]->spovartyp) {
default:
case SPOVAR_NULL:
case SPOVAR_COORD:
case SPOVAR_REGION:
case SPOVAR_MAPCHAR:
case SPOVAR_MONST:
case SPOVAR_OBJ:
case SPOVAR_INT:
break;
case SPOVAR_VARIABLE:
case SPOVAR_STRING:
case SPOVAR_SEL:
Free(st->stackdata[i]->vardata.str);
st->stackdata[i]->vardata.str = NULL;
break;
}
Free(st->stackdata[i]);
st->stackdata[i] = NULL;
}
}
Free(st->stackdata);
st->stackdata = NULL;
st->depth = st->depth_alloc = 0;
Free(st);
}
}
void
splev_stack_push(st, v)
struct splevstack *st;
struct opvar *v;
{
if (!st || !v)
return;
if (!st->stackdata)
panic("splev_stack_push: no stackdata allocated?");
if (st->depth >= st->depth_alloc) {
struct opvar **tmp = (struct opvar **) alloc(
(st->depth_alloc + SPLEV_STACK_RESERVE) * sizeof (struct opvar *));
(void) memcpy(tmp, st->stackdata,
st->depth_alloc * sizeof(struct opvar *));
Free(st->stackdata);
st->stackdata = tmp;
st->depth_alloc += SPLEV_STACK_RESERVE;
}
st->stackdata[st->depth] = v;
st->depth++;
}
struct opvar *
splev_stack_pop(st)
struct splevstack *st;
{
struct opvar *ret = NULL;
if (!st)
return ret;
if (!st->stackdata)
panic("splev_stack_pop: no stackdata allocated?");
if (st->depth) {
st->depth--;
ret = st->stackdata[st->depth];
st->stackdata[st->depth] = NULL;
return ret;
} else
impossible("splev_stack_pop: empty stack?");
return ret;
}
struct splevstack *
splev_stack_reverse(st)
struct splevstack *st;
{
long i;
struct opvar *tmp;
if (!st)
return NULL;
if (!st->stackdata)
panic("splev_stack_reverse: no stackdata allocated?");
for (i = 0; i < (st->depth / 2); i++) {
tmp = st->stackdata[i];
st->stackdata[i] = st->stackdata[st->depth - i - 1];
st->stackdata[st->depth - i - 1] = tmp;
}
return st;
}
#define OV_typ(o) (o->spovartyp)
#define OV_i(o) (o->vardata.l)
#define OV_s(o) (o->vardata.str)
#define OV_pop_i(x) (x = splev_stack_getdat(coder, SPOVAR_INT))
#define OV_pop_c(x) (x = splev_stack_getdat(coder, SPOVAR_COORD))
#define OV_pop_r(x) (x = splev_stack_getdat(coder, SPOVAR_REGION))
#define OV_pop_s(x) (x = splev_stack_getdat(coder, SPOVAR_STRING))
#define OV_pop(x) (x = splev_stack_getdat_any(coder))
#define OV_pop_typ(x, typ) (x = splev_stack_getdat(coder, typ))
struct opvar *
opvar_new_str(s)
char *s;
{
struct opvar *tmpov = (struct opvar *) alloc(sizeof (struct opvar));
tmpov->spovartyp = SPOVAR_STRING;
if (s) {
int len = strlen(s);
tmpov->vardata.str = (char *) alloc(len + 1);
(void) memcpy((genericptr_t) tmpov->vardata.str, (genericptr_t) s,
len);
tmpov->vardata.str[len] = '\0';
} else
tmpov->vardata.str = NULL;
return tmpov;
}
struct opvar *
opvar_new_int(i)
long i;
{
struct opvar *tmpov = (struct opvar *) alloc(sizeof (struct opvar));
tmpov->spovartyp = SPOVAR_INT;
tmpov->vardata.l = i;
return tmpov;
}
struct opvar *
opvar_new_coord(x, y)
int x, y;
{
struct opvar *tmpov = (struct opvar *) alloc(sizeof (struct opvar));
tmpov->spovartyp = SPOVAR_COORD;
tmpov->vardata.l = SP_COORD_PACK(x, y);
return tmpov;
}
#if 0
struct opvar *
opvar_new_region(x1,y1,x2,y2)
int x1,y1,x2,y2;
{
struct opvar *tmpov = (struct opvar *) alloc(sizeof (struct opvar));
tmpov->spovartyp = SPOVAR_REGION;
tmpov->vardata.l = SP_REGION_PACK(x1,y1,x2,y2);
return tmpov;
}
#endif /*0*/
void
opvar_free_x(ov)
struct opvar *ov;
{
if (!ov)
return;
switch (ov->spovartyp) {
case SPOVAR_COORD:
case SPOVAR_REGION:
case SPOVAR_MAPCHAR:
case SPOVAR_MONST:
case SPOVAR_OBJ:
case SPOVAR_INT:
break;
case SPOVAR_VARIABLE:
case SPOVAR_STRING:
case SPOVAR_SEL:
Free(ov->vardata.str);
break;
default:
impossible("Unknown opvar value type (%i)!", ov->spovartyp);
}
Free(ov);
}
/*
* Name of current function for use in messages:
* __func__ -- C99 standard;
* __FUNCTION__ -- gcc extension, starting before C99 and continuing after;
* picked up by other compilers (or vice versa?);
* __FUNC__ -- supported by Borland;
* nhFunc -- slightly intrusive but fully portable nethack construct
* for any version of any compiler.
*/
#define opvar_free(ov) \
do { \
if (ov) { \
opvar_free_x(ov); \
ov = NULL; \
} else \
impossible("opvar_free(), %s", nhFunc); \
} while (0)
struct opvar *
opvar_clone(ov)
struct opvar *ov;
{
struct opvar *tmpov;
if (!ov)
panic("no opvar to clone");
tmpov = (struct opvar *) alloc(sizeof(struct opvar));
tmpov->spovartyp = ov->spovartyp;
switch (ov->spovartyp) {
case SPOVAR_COORD:
case SPOVAR_REGION:
case SPOVAR_MAPCHAR:
case SPOVAR_MONST:
case SPOVAR_OBJ:
case SPOVAR_INT:
tmpov->vardata.l = ov->vardata.l;
break;
case SPOVAR_VARIABLE:
case SPOVAR_STRING:
case SPOVAR_SEL:
tmpov->vardata.str = dupstr(ov->vardata.str);
break;
default:
impossible("Unknown push value type (%i)!", ov->spovartyp);
}
return tmpov;
}
struct opvar *
opvar_var_conversion(coder, ov)
struct sp_coder *coder;
struct opvar *ov;
{
static const char nhFunc[] = "opvar_var_conversion";
struct splev_var *tmp;
struct opvar *tmpov;
struct opvar *array_idx = NULL;
if (!coder || !ov)
return NULL;
if (ov->spovartyp != SPOVAR_VARIABLE)
return ov;
tmp = coder->frame->variables;
while (tmp) {
if (!strcmp(tmp->name, OV_s(ov))) {
if ((tmp->svtyp & SPOVAR_ARRAY)) {
array_idx = opvar_var_conversion(coder,
splev_stack_pop(coder->stack));
if (!array_idx || OV_typ(array_idx) != SPOVAR_INT)
panic("array idx not an int");
if (tmp->array_len < 1)
panic("array len < 1");
OV_i(array_idx) = (OV_i(array_idx) % tmp->array_len);
tmpov = opvar_clone(tmp->data.arrayvalues[OV_i(array_idx)]);
opvar_free(array_idx);
return tmpov;
} else {
tmpov = opvar_clone(tmp->data.value);
return tmpov;
}
}
tmp = tmp->next;
}
return NULL;
}
struct splev_var *
opvar_var_defined(coder, name)
struct sp_coder *coder;
char *name;
{
struct splev_var *tmp;
if (!coder)
return NULL;
tmp = coder->frame->variables;
while (tmp) {
if (!strcmp(tmp->name, name))
return tmp;
tmp = tmp->next;
}
return NULL;
}
struct opvar *
splev_stack_getdat(coder, typ)
struct sp_coder *coder;
xchar typ;
{
static const char nhFunc[] = "splev_stack_getdat";
if (coder && coder->stack) {
struct opvar *tmp = splev_stack_pop(coder->stack);
struct opvar *ret = NULL;
if (!tmp)
panic("no value type %i in stack.", typ);
if (tmp->spovartyp == SPOVAR_VARIABLE) {
ret = opvar_var_conversion(coder, tmp);
opvar_free(tmp);
tmp = ret;
}
if (tmp->spovartyp == typ)
return tmp;
else opvar_free(tmp);
}
return NULL;
}
struct opvar *
splev_stack_getdat_any(coder)
struct sp_coder *coder;
{
static const char nhFunc[] = "splev_stack_getdat_any";
if (coder && coder->stack) {
struct opvar *tmp = splev_stack_pop(coder->stack);
if (tmp && tmp->spovartyp == SPOVAR_VARIABLE) {
struct opvar *ret = opvar_var_conversion(coder, tmp);
opvar_free(tmp);
return ret;
}
return tmp;
}
return NULL;
}
void
variable_list_del(varlist)
struct splev_var *varlist;
{
static const char nhFunc[] = "variable_list_del";
struct splev_var *tmp = varlist;
if (!tmp)
return;
while (tmp) {
Free(tmp->name);
if ((tmp->svtyp & SPOVAR_ARRAY)) {
long idx = tmp->array_len;
while (idx-- > 0) {
opvar_free(tmp->data.arrayvalues[idx]);
};
Free(tmp->data.arrayvalues);
} else {
opvar_free(tmp->data.value);
}
tmp = varlist->next;
Free(varlist);
varlist = tmp;
}
}
void
lvlfill_maze_grid(x1, y1, x2, y2, filling)
int x1, y1, x2, y2;
schar filling;
{
int x, y;
for (x = x1; x <= x2; x++)
for (y = y1; y <= y2; y++) {
if (level.flags.corrmaze)
levl[x][y].typ = STONE;
else
levl[x][y].typ = (y < 2 || ((x % 2) && (y % 2))) ? STONE
: filling;
}
}
void
lvlfill_solid(filling, lit)
schar filling;
schar lit;
{
int x, y;
for (x = 2; x <= x_maze_max; x++)
for (y = 0; y <= y_maze_max; y++) {
SET_TYPLIT(x, y, filling, lit);
}
}
/*
* Make walls of the area (x1, y1, x2, y2) non diggable/non passwall-able
*/
STATIC_OVL void
set_wall_property(x1, y1, x2, y2, prop)
xchar x1, y1, x2, y2;
int prop;
{
register xchar x, y;
struct rm *lev;
x1 = max(x1, 1);
x2 = min(x2, COLNO - 1);
y1 = max(y1, 0);
y2 = min(y2, ROWNO - 1);
for (y = y1; y <= y2; y++)
for (x = x1; x <= x2; x++) {
lev = &levl[x][y];
if (IS_STWALL(lev->typ) || IS_TREE(lev->typ)
/* 3.6.2: made iron bars eligible to be flagged nondiggable
(checked by chewing(hack.c) and zap_over_floor(zap.c)) */
|| lev->typ == IRONBARS)
lev->wall_info |= prop;
}
}
STATIC_OVL void
shuffle_alignments()
{
int i;
aligntyp atmp;
/* shuffle 3 alignments */
i = rn2(3);
atmp = ralign[2];
ralign[2] = ralign[i];
ralign[i] = atmp;
if (rn2(2)) {
atmp = ralign[1];
ralign[1] = ralign[0];
ralign[0] = atmp;
}
}
/*
* Count the different features (sinks, fountains) in the level.
*/
STATIC_OVL void
count_features()
{
xchar x, y;
level.flags.nfountains = level.flags.nsinks = 0;
for (y = 0; y < ROWNO; y++)
for (x = 0; x < COLNO; x++) {
int typ = levl[x][y].typ;
if (typ == FOUNTAIN)
level.flags.nfountains++;
else if (typ == SINK)
level.flags.nsinks++;
}
}
void
remove_boundary_syms()
{
/*
* If any CROSSWALLs are found, must change to ROOM after REGION's
* are laid out. CROSSWALLS are used to specify "invisible"
* boundaries where DOOR syms look bad or aren't desirable.
*/
xchar x, y;
boolean has_bounds = FALSE;
for (x = 0; x < COLNO - 1; x++)
for (y = 0; y < ROWNO - 1; y++)
if (levl[x][y].typ == CROSSWALL) {
has_bounds = TRUE;
break;
}
if (has_bounds) {
for (x = 0; x < x_maze_max; x++)
for (y = 0; y < y_maze_max; y++)
if ((levl[x][y].typ == CROSSWALL) && SpLev_Map[x][y])
levl[x][y].typ = ROOM;
}
}
/* used by sel_set_door() and link_doors_rooms() */
STATIC_OVL void
set_door_orientation(x, y)
int x, y;
{
boolean wleft, wright, wup, wdown;
/* If there's a wall or door on either the left side or right
* side (or both) of this secret door, make it be horizontal.
*
* It is feasible to put SDOOR in a corner, tee, or crosswall
* position, although once the door is found and opened it won't
* make a lot sense (diagonal access required). Still, we try to
* handle that as best as possible. For top or bottom tee, using
* horizontal is the best we can do. For corner or crosswall,
* either horizontal or vertical are just as good as each other;
* we produce horizontal for corners and vertical for crosswalls.
* For left or right tee, using vertical is best.
*
* A secret door with no adjacent walls is also feasible and makes
* even less sense. It will be displayed as a vertical wall while
* hidden and become a vertical door when found. Before resorting
* to that, we check for solid rock which hasn't been wallified
* yet (cf lower leftside of leader's room in Cav quest).
*/
wleft = (isok(x - 1, y) && (IS_WALL(levl[x - 1][y].typ)
|| IS_DOOR(levl[x - 1][y].typ)
|| levl[x - 1][y].typ == SDOOR));
wright = (isok(x + 1, y) && (IS_WALL(levl[x + 1][y].typ)
|| IS_DOOR(levl[x + 1][y].typ)
|| levl[x + 1][y].typ == SDOOR));
wup = (isok(x, y - 1) && (IS_WALL(levl[x][y - 1].typ)
|| IS_DOOR(levl[x][y - 1].typ)
|| levl[x][y - 1].typ == SDOOR));
wdown = (isok(x, y + 1) && (IS_WALL(levl[x][y + 1].typ)
|| IS_DOOR(levl[x][y + 1].typ)
|| levl[x][y + 1].typ == SDOOR));
if (!wleft && !wright && !wup && !wdown) {
/* out of bounds is treated as implicit wall; should be academic
because we don't expect to have doors so near the level's edge */
wleft = (!isok(x - 1, y) || IS_DOORJOIN(levl[x - 1][y].typ));
wright = (!isok(x + 1, y) || IS_DOORJOIN(levl[x + 1][y].typ));
wup = (!isok(x, y - 1) || IS_DOORJOIN(levl[x][y - 1].typ));
wdown = (!isok(x, y + 1) || IS_DOORJOIN(levl[x][y + 1].typ));
}
levl[x][y].horizontal = ((wleft || wright) && !(wup && wdown)) ? 1 : 0;
}
STATIC_OVL void
maybe_add_door(x, y, droom)
int x, y;
struct mkroom *droom;
{
if (droom->hx >= 0 && doorindex < DOORMAX && inside_room(droom, x, y))
add_door(x, y, droom);
}
STATIC_OVL void
link_doors_rooms()
{
int x, y;
int tmpi, m;
for (y = 0; y < ROWNO; y++)
for (x = 0; x < COLNO; x++)
if (IS_DOOR(levl[x][y].typ) || levl[x][y].typ == SDOOR) {
/* in case this door was a '+' or 'S' from the
MAP...ENDMAP section without an explicit DOOR
directive, set/clear levl[][].horizontal for it */
set_door_orientation(x, y);
for (tmpi = 0; tmpi < nroom; tmpi++) {
maybe_add_door(x, y, &rooms[tmpi]);
for (m = 0; m < rooms[tmpi].nsubrooms; m++) {
maybe_add_door(x, y, rooms[tmpi].sbrooms[m]);
}
}
}
}
void
fill_rooms()
{
int tmpi, m;
for (tmpi = 0; tmpi < nroom; tmpi++) {
if (rooms[tmpi].needfill)
fill_room(&rooms[tmpi], (rooms[tmpi].needfill == 2));
for (m = 0; m < rooms[tmpi].nsubrooms; m++)
if (rooms[tmpi].sbrooms[m]->needfill)
fill_room(rooms[tmpi].sbrooms[m], FALSE);
}
}
/*
* Choose randomly the state (nodoor, open, closed or locked) for a door
*/
STATIC_OVL int
rnddoor()
{
int i = 1 << rn2(5);
i >>= 1;
return i;
}
/*
* Select a random trap
*/
STATIC_OVL int
rndtrap()
{
int rtrap;
do {
rtrap = rnd(TRAPNUM - 1);
switch (rtrap) {
case HOLE: /* no random holes on special levels */
case VIBRATING_SQUARE:
case MAGIC_PORTAL:
rtrap = NO_TRAP;
break;
case TRAPDOOR:
if (!Can_dig_down(&u.uz))
rtrap = NO_TRAP;
break;
case LEVEL_TELEP:
case TELEP_TRAP:
if (level.flags.noteleport)
rtrap = NO_TRAP;
break;
case ROLLING_BOULDER_TRAP:
case ROCKTRAP:
if (In_endgame(&u.uz))
rtrap = NO_TRAP;
break;
}
} while (rtrap == NO_TRAP);
return rtrap;
}
/*
* Coordinates in special level files are handled specially:
*
* if x or y is < 0, we generate a random coordinate.
* The "humidity" flag is used to insure that engravings aren't
* created underwater, or eels on dry land.
*/
STATIC_OVL void
get_location(x, y, humidity, croom)
schar *x, *y;
int humidity;
struct mkroom *croom;
{
int cpt = 0;
int mx, my, sx, sy;
if (croom) {
mx = croom->lx;
my = croom->ly;
sx = croom->hx - mx + 1;
sy = croom->hy - my + 1;
} else {
mx = xstart;
my = ystart;
sx = xsize;
sy = ysize;
}
if (*x >= 0) { /* normal locations */
*x += mx;
*y += my;
} else { /* random location */
do {
if (croom) { /* handle irregular areas */
coord tmpc;
somexy(croom, &tmpc);
*x = tmpc.x;
*y = tmpc.y;
} else {
*x = mx + rn2((int) sx);
*y = my + rn2((int) sy);
}
if (is_ok_location(*x, *y, humidity))
break;
} while (++cpt < 100);
if (cpt >= 100) {
register int xx, yy;
/* last try */
for (xx = 0; xx < sx; xx++)
for (yy = 0; yy < sy; yy++) {
*x = mx + xx;
*y = my + yy;
if (is_ok_location(*x, *y, humidity))
goto found_it;
}
if (!(humidity & NO_LOC_WARN)) {
impossible("get_location: can't find a place!");
} else {
*x = *y = -1;
}
}
}
found_it:
;
if (!(humidity & ANY_LOC) && !isok(*x, *y)) {
if (!(humidity & NO_LOC_WARN)) {
/*warning("get_location: (%d,%d) out of bounds", *x, *y);*/
*x = x_maze_max;
*y = y_maze_max;
} else {
*x = *y = -1;
}
}
}
STATIC_OVL boolean
is_ok_location(x, y, humidity)
register schar x, y;
register int humidity;
{
register int typ;
if (Is_waterlevel(&u.uz))
return TRUE; /* accept any spot */
/* TODO: Should perhaps check if wall is diggable/passwall? */
if (humidity & ANY_LOC)
return TRUE;
if ((humidity & SOLID) && IS_ROCK(levl[x][y].typ))
return TRUE;
if (humidity & DRY) {
typ = levl[x][y].typ;
if (typ == ROOM || typ == AIR || typ == CLOUD || typ == ICE
|| typ == CORR)
return TRUE;
}
if ((humidity & SPACELOC) && SPACE_POS(levl[x][y].typ))
return TRUE;
if ((humidity & WET) && is_pool(x, y))
return TRUE;
if ((humidity & HOT) && is_lava(x, y))
return TRUE;
return FALSE;
}
unpacked_coord
get_unpacked_coord(loc, defhumidity)
long loc;
int defhumidity;
{
static unpacked_coord c;
if (loc & SP_COORD_IS_RANDOM) {
c.x = c.y = -1;
c.is_random = 1;
c.getloc_flags = (loc & ~SP_COORD_IS_RANDOM);
if (!c.getloc_flags)
c.getloc_flags = defhumidity;
} else {
c.is_random = 0;
c.getloc_flags = defhumidity;
c.x = SP_COORD_X(loc);
c.y = SP_COORD_Y(loc);
}
return c;
}
STATIC_OVL void
get_location_coord(x, y, humidity, croom, crd)
schar *x, *y;
int humidity;
struct mkroom *croom;
long crd;
{
unpacked_coord c;
c = get_unpacked_coord(crd, humidity);