This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
/
cmd.c
6137 lines (5677 loc) · 207 KB
/
cmd.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 cmd.c $NHDT-Date: 1575245052 2019/12/02 00:04:12 $ $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.350 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Robert Patrick Rankin, 2013. */
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
#include "lev.h"
#include "func_tab.h"
/* Macros for meta and ctrl modifiers:
* M and C return the meta/ctrl code for the given character;
* e.g., (C('c') is ctrl-c
*/
#ifndef M
#ifndef NHSTDC
#define M(c) (0x80 | (c))
#else
#define M(c) ((c) - 128)
#endif /* NHSTDC */
#endif
#ifndef C
#define C(c) (0x1f & (c))
#endif
#define unctrl(c) ((c) <= C('z') ? (0x60 | (c)) : (c))
#define unmeta(c) (0x7f & (c))
#ifdef ALTMETA
STATIC_VAR boolean alt_esc = FALSE;
#endif
struct cmd Cmd = { 0 }; /* flag.h */
extern const char *hu_stat[]; /* hunger status from eat.c */
extern const char *enc_stat[]; /* encumbrance status from botl.c */
#ifdef UNIX
/*
* Some systems may have getchar() return EOF for various reasons, and
* we should not quit before seeing at least NR_OF_EOFS consecutive EOFs.
*/
#if defined(SYSV) || defined(DGUX) || defined(HPUX)
#define NR_OF_EOFS 20
#endif
#endif
#define CMD_TRAVEL (char) 0x90
#define CMD_CLICKLOOK (char) 0x8F
#ifdef DEBUG
extern int NDECL(wiz_debug_cmd_bury);
#endif
#ifdef DUMB /* stuff commented out in extern.h, but needed here */
extern int NDECL(doapply); /**/
extern int NDECL(dorub); /**/
extern int NDECL(dojump); /**/
extern int NDECL(doextlist); /**/
extern int NDECL(enter_explore_mode); /**/
extern int NDECL(dodrop); /**/
extern int NDECL(doddrop); /**/
extern int NDECL(dodown); /**/
extern int NDECL(doup); /**/
extern int NDECL(donull); /**/
extern int NDECL(dowipe); /**/
extern int NDECL(docallcnd); /**/
extern int NDECL(dotakeoff); /**/
extern int NDECL(doremring); /**/
extern int NDECL(dowear); /**/
extern int NDECL(doputon); /**/
extern int NDECL(doddoremarm); /**/
extern int NDECL(dokick); /**/
extern int NDECL(dofire); /**/
extern int NDECL(dothrow); /**/
extern int NDECL(doeat); /**/
extern int NDECL(done2); /**/
extern int NDECL(vanquished); /**/
extern int NDECL(doengrave); /**/
extern int NDECL(dopickup); /**/
extern int NDECL(ddoinv); /**/
extern int NDECL(dotypeinv); /**/
extern int NDECL(dolook); /**/
extern int NDECL(doprgold); /**/
extern int NDECL(doprwep); /**/
extern int NDECL(doprarm); /**/
extern int NDECL(doprring); /**/
extern int NDECL(dopramulet); /**/
extern int NDECL(doprtool); /**/
extern int NDECL(dosuspend); /**/
extern int NDECL(doforce); /**/
extern int NDECL(doopen); /**/
extern int NDECL(doclose); /**/
extern int NDECL(dosh); /**/
extern int NDECL(dodiscovered); /**/
extern int NDECL(doclassdisco); /**/
extern int NDECL(doset); /**/
extern int NDECL(dotogglepickup); /**/
extern int NDECL(dowhatis); /**/
extern int NDECL(doquickwhatis); /**/
extern int NDECL(dowhatdoes); /**/
extern int NDECL(dohelp); /**/
extern int NDECL(dohistory); /**/
extern int NDECL(doloot); /**/
extern int NDECL(dodrink); /**/
extern int NDECL(dodip); /**/
extern int NDECL(dosacrifice); /**/
extern int NDECL(dopray); /**/
extern int NDECL(dotip); /**/
extern int NDECL(doturn); /**/
extern int NDECL(doredraw); /**/
extern int NDECL(doread); /**/
extern int NDECL(dosave); /**/
extern int NDECL(dosearch); /**/
extern int NDECL(doidtrap); /**/
extern int NDECL(dopay); /**/
extern int NDECL(dosit); /**/
extern int NDECL(dotalk); /**/
extern int NDECL(docast); /**/
extern int NDECL(dovspell); /**/
extern int NDECL(dotelecmd); /**/
extern int NDECL(dountrap); /**/
extern int NDECL(doversion); /**/
extern int NDECL(doextversion); /**/
extern int NDECL(doswapweapon); /**/
extern int NDECL(dowield); /**/
extern int NDECL(dowieldquiver); /**/
extern int NDECL(dozap); /**/
extern int NDECL(doorganize); /**/
#endif /* DUMB */
static int NDECL((*timed_occ_fn));
STATIC_PTR int NDECL(dosuspend_core);
STATIC_PTR int NDECL(dosh_core);
STATIC_PTR int NDECL(doherecmdmenu);
STATIC_PTR int NDECL(dotherecmdmenu);
STATIC_PTR int NDECL(doprev_message);
STATIC_PTR int NDECL(timed_occupation);
STATIC_PTR int NDECL(doextcmd);
STATIC_PTR int NDECL(dotravel);
STATIC_PTR int NDECL(doterrain);
STATIC_PTR int NDECL(wiz_wish);
STATIC_PTR int NDECL(wiz_identify);
STATIC_PTR int NDECL(wiz_intrinsic);
STATIC_PTR int NDECL(wiz_map);
STATIC_PTR int NDECL(wiz_makemap);
STATIC_PTR int NDECL(wiz_genesis);
STATIC_PTR int NDECL(wiz_where);
STATIC_PTR int NDECL(wiz_detect);
STATIC_PTR int NDECL(wiz_panic);
STATIC_PTR int NDECL(wiz_polyself);
STATIC_PTR int NDECL(wiz_level_tele);
STATIC_PTR int NDECL(wiz_level_change);
STATIC_PTR int NDECL(wiz_show_seenv);
STATIC_PTR int NDECL(wiz_show_vision);
STATIC_PTR int NDECL(wiz_smell);
STATIC_PTR int NDECL(wiz_show_wmodes);
STATIC_DCL void NDECL(wiz_map_levltyp);
STATIC_DCL void NDECL(wiz_levltyp_legend);
#if defined(__BORLANDC__) && !defined(_WIN32)
extern void FDECL(show_borlandc_stats, (winid));
#endif
#ifdef DEBUG_MIGRATING_MONS
STATIC_PTR int NDECL(wiz_migrate_mons);
#endif
STATIC_DCL int FDECL(size_monst, (struct monst *, BOOLEAN_P));
STATIC_DCL int FDECL(size_obj, (struct obj *));
STATIC_DCL void FDECL(count_obj, (struct obj *, long *, long *,
BOOLEAN_P, BOOLEAN_P));
STATIC_DCL void FDECL(obj_chain, (winid, const char *, struct obj *,
BOOLEAN_P, long *, long *));
STATIC_DCL void FDECL(mon_invent_chain, (winid, const char *, struct monst *,
long *, long *));
STATIC_DCL void FDECL(mon_chain, (winid, const char *, struct monst *,
BOOLEAN_P, long *, long *));
STATIC_DCL void FDECL(contained_stats, (winid, const char *, long *, long *));
STATIC_DCL void FDECL(misc_stats, (winid, long *, long *));
STATIC_PTR int NDECL(wiz_show_stats);
STATIC_DCL boolean FDECL(accept_menu_prefix, (int NDECL((*))));
STATIC_PTR int NDECL(wiz_rumor_check);
STATIC_PTR int NDECL(doattributes);
STATIC_DCL void FDECL(enlght_out, (const char *));
STATIC_DCL void FDECL(enlght_line, (const char *, const char *, const char *,
const char *));
STATIC_DCL char *FDECL(enlght_combatinc, (const char *, int, int, char *));
STATIC_DCL void FDECL(enlght_halfdmg, (int, int));
STATIC_DCL boolean NDECL(walking_on_water);
STATIC_DCL boolean FDECL(cause_known, (int));
STATIC_DCL char *FDECL(attrval, (int, int, char *));
STATIC_DCL void FDECL(background_enlightenment, (int, int));
STATIC_DCL void FDECL(basics_enlightenment, (int, int));
STATIC_DCL void FDECL(characteristics_enlightenment, (int, int));
STATIC_DCL void FDECL(one_characteristic, (int, int, int));
STATIC_DCL void FDECL(status_enlightenment, (int, int));
STATIC_DCL void FDECL(attributes_enlightenment, (int, int));
STATIC_DCL void FDECL(add_herecmd_menuitem, (winid, int NDECL((*)),
const char *));
STATIC_DCL char FDECL(here_cmd_menu, (BOOLEAN_P));
STATIC_DCL char FDECL(there_cmd_menu, (BOOLEAN_P, int, int));
STATIC_DCL char *NDECL(parse);
STATIC_DCL void FDECL(show_direction_keys, (winid, CHAR_P, BOOLEAN_P));
STATIC_DCL boolean FDECL(help_dir, (CHAR_P, int, const char *));
static const char *readchar_queue = "";
static coord clicklook_cc;
/* for rejecting attempts to use wizard mode commands */
static const char unavailcmd[] = "Unavailable command '%s'.";
/* for rejecting #if !SHELL, !SUSPEND */
static const char cmdnotavail[] = "'%s' command not available.";
STATIC_PTR int
doprev_message(VOID_ARGS)
{
return nh_doprev_message();
}
/* Count down by decrementing multi */
STATIC_PTR int
timed_occupation(VOID_ARGS)
{
(*timed_occ_fn)();
if (multi > 0)
multi--;
return multi > 0;
}
/* If you have moved since initially setting some occupations, they
* now shouldn't be able to restart.
*
* The basic rule is that if you are carrying it, you can continue
* since it is with you. If you are acting on something at a distance,
* your orientation to it must have changed when you moved.
*
* The exception to this is taking off items, since they can be taken
* off in a number of ways in the intervening time, screwing up ordering.
*
* Currently: Take off all armor.
* Picking Locks / Forcing Chests.
* Setting traps.
*/
void
reset_occupations()
{
reset_remarm();
reset_pick();
reset_trapset();
}
/* If a time is given, use it to timeout this function, otherwise the
* function times out by its own means.
*/
void
set_occupation(fn, txt, xtime)
int NDECL((*fn));
const char *txt;
int xtime;
{
if (xtime) {
occupation = timed_occupation;
timed_occ_fn = fn;
} else
occupation = fn;
occtxt = txt;
occtime = 0;
return;
}
STATIC_DCL char NDECL(popch);
/* Provide a means to redo the last command. The flag `in_doagain' is set
* to true while redoing the command. This flag is tested in commands that
* require additional input (like `throw' which requires a thing and a
* direction), and the input prompt is not shown. Also, while in_doagain is
* TRUE, no keystrokes can be saved into the saveq.
*/
#define BSIZE 20
static char pushq[BSIZE], saveq[BSIZE];
static NEARDATA int phead, ptail, shead, stail;
STATIC_OVL char
popch()
{
/* If occupied, return '\0', letting tgetch know a character should
* be read from the keyboard. If the character read is not the
* ABORT character (as checked in pcmain.c), that character will be
* pushed back on the pushq.
*/
if (occupation)
return '\0';
if (in_doagain)
return (char) ((shead != stail) ? saveq[stail++] : '\0');
else
return (char) ((phead != ptail) ? pushq[ptail++] : '\0');
}
char
pgetchar() /* courtesy of [email protected] */
{
register int ch;
if (iflags.debug_fuzzer)
return randomkey();
if (!(ch = popch()))
ch = nhgetch();
return (char) ch;
}
/* A ch == 0 resets the pushq */
void
pushch(ch)
char ch;
{
if (!ch)
phead = ptail = 0;
if (phead < BSIZE)
pushq[phead++] = ch;
return;
}
/* A ch == 0 resets the saveq. Only save keystrokes when not
* replaying a previous command.
*/
void
savech(ch)
char ch;
{
if (!in_doagain) {
if (!ch)
phead = ptail = shead = stail = 0;
else if (shead < BSIZE)
saveq[shead++] = ch;
}
return;
}
/* here after # - now read a full-word command */
STATIC_PTR int
doextcmd(VOID_ARGS)
{
int idx, retval;
int NDECL((*func));
/* keep repeating until we don't run help or quit */
do {
idx = get_ext_cmd();
if (idx < 0)
return 0; /* quit */
func = extcmdlist[idx].ef_funct;
if (!wizard && (extcmdlist[idx].flags & WIZMODECMD)) {
You("can't do that.");
return 0;
}
if (iflags.menu_requested && !accept_menu_prefix(func)) {
pline("'%s' prefix has no effect for the %s command.",
visctrl(Cmd.spkeys[NHKF_REQMENU]),
extcmdlist[idx].ef_txt);
iflags.menu_requested = FALSE;
}
retval = (*func)();
} while (func == doextlist);
return retval;
}
/* here after #? - now list all full-word commands and provid
some navigation capability through the long list */
int
doextlist(VOID_ARGS)
{
register const struct ext_func_tab *efp;
char buf[BUFSZ], searchbuf[BUFSZ], promptbuf[QBUFSZ];
winid menuwin;
anything any;
menu_item *selected;
int n, pass;
int menumode = 0, menushown[2], onelist = 0;
boolean redisplay = TRUE, search = FALSE;
static const char *headings[] = { "Extended commands",
"Debugging Extended Commands" };
searchbuf[0] = '\0';
menuwin = create_nhwindow(NHW_MENU);
while (redisplay) {
redisplay = FALSE;
any = zeroany;
start_menu(menuwin);
add_menu(menuwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
"Extended Commands List", MENU_UNSELECTED);
add_menu(menuwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
"", MENU_UNSELECTED);
Strcpy(buf, menumode ? "Show" : "Hide");
Strcat(buf, " commands that don't autocomplete");
if (!menumode)
Strcat(buf, " (those not marked with [A])");
any.a_int = 1;
add_menu(menuwin, NO_GLYPH, &any, 'a', 0, ATR_NONE, buf,
MENU_UNSELECTED);
if (!*searchbuf) {
any.a_int = 2;
/* was 's', but then using ':' handling within the interface
would only examine the two or three meta entries, not the
actual list of extended commands shown via separator lines;
having ':' as an explicit selector overrides the default
menu behavior for it; we retain 's' as a group accelerator */
add_menu(menuwin, NO_GLYPH, &any, ':', 's', ATR_NONE,
"Search extended commands", MENU_UNSELECTED);
} else {
Strcpy(buf, "Show all, clear search");
if (strlen(buf) + strlen(searchbuf) + strlen(" (\"\")") < QBUFSZ)
Sprintf(eos(buf), " (\"%s\")", searchbuf);
any.a_int = 3;
/* specifying ':' as a group accelerator here is mostly a
statement of intent (we'd like to accept it as a synonym but
also want to hide it from general menu use) because it won't
work for interfaces which support ':' to search; use as a
general menu command takes precedence over group accelerator */
add_menu(menuwin, NO_GLYPH, &any, 's', ':', ATR_NONE,
buf, MENU_UNSELECTED);
}
if (wizard) {
any.a_int = 4;
add_menu(menuwin, NO_GLYPH, &any, 'z', 0, ATR_NONE,
onelist ? "Show debugging commands in separate section"
: "Show all alphabetically, including debugging commands",
MENU_UNSELECTED);
}
any = zeroany;
add_menu(menuwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
"", MENU_UNSELECTED);
menushown[0] = menushown[1] = 0;
n = 0;
for (pass = 0; pass <= 1; ++pass) {
/* skip second pass if not in wizard mode or wizard mode
commands are being integrated into a single list */
if (pass == 1 && (onelist || !wizard))
break;
for (efp = extcmdlist; efp->ef_txt; efp++) {
int wizc;
if ((efp->flags & CMD_NOT_AVAILABLE) != 0)
continue;
/* if hiding non-autocomplete commands, skip such */
if (menumode == 1 && (efp->flags & AUTOCOMPLETE) == 0)
continue;
/* if searching, skip this command if it doesn't match */
if (*searchbuf
/* first try case-insensitive substring match */
&& !strstri(efp->ef_txt, searchbuf)
&& !strstri(efp->ef_desc, searchbuf)
/* wildcard support; most interfaces use case-insensitve
pmatch rather than regexp for menu searching */
&& !pmatchi(searchbuf, efp->ef_txt)
&& !pmatchi(searchbuf, efp->ef_desc))
continue;
/* skip wizard mode commands if not in wizard mode;
when showing two sections, skip wizard mode commands
in pass==0 and skip other commands in pass==1 */
wizc = (efp->flags & WIZMODECMD) != 0;
if (wizc && !wizard)
continue;
if (!onelist && pass != wizc)
continue;
/* We're about to show an item, have we shown the menu yet?
Doing menu in inner loop like this on demand avoids a
heading with no subordinate entries on the search
results menu. */
if (!menushown[pass]) {
Strcpy(buf, headings[pass]);
add_menu(menuwin, NO_GLYPH, &any, 0, 0,
iflags.menu_headings, buf, MENU_UNSELECTED);
menushown[pass] = 1;
}
Sprintf(buf, " %-14s %-3s %s",
efp->ef_txt,
(efp->flags & AUTOCOMPLETE) ? "[A]" : " ",
efp->ef_desc);
add_menu(menuwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
buf, MENU_UNSELECTED);
++n;
}
if (n)
add_menu(menuwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
"", MENU_UNSELECTED);
}
if (*searchbuf && !n)
add_menu(menuwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
"no matches", MENU_UNSELECTED);
end_menu(menuwin, (char *) 0);
n = select_menu(menuwin, PICK_ONE, &selected);
if (n > 0) {
switch (selected[0].item.a_int) {
case 1: /* 'a': toggle show/hide non-autocomplete */
menumode = 1 - menumode; /* toggle 0 -> 1, 1 -> 0 */
redisplay = TRUE;
break;
case 2: /* ':' when not searching yet: enable search */
search = TRUE;
break;
case 3: /* 's' when already searching: disable search */
search = FALSE;
searchbuf[0] = '\0';
redisplay = TRUE;
break;
case 4: /* 'z': toggle showing wizard mode commands separately */
search = FALSE;
searchbuf[0] = '\0';
onelist = 1 - onelist; /* toggle 0 -> 1, 1 -> 0 */
redisplay = TRUE;
break;
}
free((genericptr_t) selected);
} else {
search = FALSE;
searchbuf[0] = '\0';
}
if (search) {
Strcpy(promptbuf, "Extended command list search phrase");
Strcat(promptbuf, "?");
getlin(promptbuf, searchbuf);
(void) mungspaces(searchbuf);
if (searchbuf[0] == '\033')
searchbuf[0] = '\0';
if (*searchbuf)
redisplay = TRUE;
search = FALSE;
}
}
destroy_nhwindow(menuwin);
return 0;
}
#if defined(TTY_GRAPHICS) || defined(CURSES_GRAPHICS)
#define MAX_EXT_CMD 200 /* Change if we ever have more ext cmds */
/*
* This is currently used only by the tty interface and is
* controlled via runtime option 'extmenu'. (Most other interfaces
* already use a menu all the time for extended commands.)
*
* ``# ?'' is counted towards the limit of the number of commands,
* so we actually support MAX_EXT_CMD-1 "real" extended commands.
*
* Here after # - now show pick-list of possible commands.
*/
int
extcmd_via_menu()
{
const struct ext_func_tab *efp;
menu_item *pick_list = (menu_item *) 0;
winid win;
anything any;
const struct ext_func_tab *choices[MAX_EXT_CMD + 1];
char buf[BUFSZ];
char cbuf[QBUFSZ], prompt[QBUFSZ], fmtstr[20];
int i, n, nchoices, acount;
int ret, len, biggest;
int accelerator, prevaccelerator;
int matchlevel = 0;
boolean wastoolong, one_per_line;
ret = 0;
cbuf[0] = '\0';
biggest = 0;
while (!ret) {
i = n = 0;
any = zeroany;
/* populate choices */
for (efp = extcmdlist; efp->ef_txt; efp++) {
if ((efp->flags & CMD_NOT_AVAILABLE)
|| !(efp->flags & AUTOCOMPLETE)
|| (!wizard && (efp->flags & WIZMODECMD)))
continue;
if (!matchlevel || !strncmp(efp->ef_txt, cbuf, matchlevel)) {
choices[i] = efp;
if ((len = (int) strlen(efp->ef_desc)) > biggest)
biggest = len;
if (++i > MAX_EXT_CMD) {
#if (NH_DEVEL_STATUS != NH_STATUS_RELEASED)
impossible(
"Exceeded %d extended commands in doextcmd() menu; 'extmenu' disabled.",
MAX_EXT_CMD);
#endif /* NH_DEVEL_STATUS != NH_STATUS_RELEASED */
iflags.extmenu = 0;
return -1;
}
}
}
choices[i] = (struct ext_func_tab *) 0;
nchoices = i;
/* if we're down to one, we have our selection so get out of here */
if (nchoices <= 1) {
ret = (nchoices == 1) ? (int) (choices[0] - extcmdlist) : -1;
break;
}
/* otherwise... */
win = create_nhwindow(NHW_MENU);
start_menu(win);
Sprintf(fmtstr, "%%-%ds", biggest + 15);
prompt[0] = '\0';
wastoolong = FALSE; /* True => had to wrap due to line width
* ('w' in wizard mode) */
/* -3: two line menu header, 1 line menu footer (for prompt) */
one_per_line = (nchoices < ROWNO - 3);
accelerator = prevaccelerator = 0;
acount = 0;
for (i = 0; choices[i]; ++i) {
accelerator = choices[i]->ef_txt[matchlevel];
if (accelerator != prevaccelerator || one_per_line)
wastoolong = FALSE;
if (accelerator != prevaccelerator || one_per_line
|| (acount >= 2
/* +4: + sizeof " or " - sizeof "" */
&& (strlen(prompt) + 4 + strlen(choices[i]->ef_txt)
/* -6: enough room for 1 space left margin
* + "%c - " menu selector + 1 space right margin */
>= min(sizeof prompt, COLNO - 6)))) {
if (acount) {
/* flush extended cmds for that letter already in buf */
Sprintf(buf, fmtstr, prompt);
any.a_char = prevaccelerator;
add_menu(win, NO_GLYPH, &any, any.a_char, 0, ATR_NONE,
buf, FALSE);
acount = 0;
if (!(accelerator != prevaccelerator || one_per_line))
wastoolong = TRUE;
}
}
prevaccelerator = accelerator;
if (!acount || one_per_line) {
Sprintf(prompt, "%s%s [%s]", wastoolong ? "or " : "",
choices[i]->ef_txt, choices[i]->ef_desc);
} else if (acount == 1) {
Sprintf(prompt, "%s%s or %s", wastoolong ? "or " : "",
choices[i - 1]->ef_txt, choices[i]->ef_txt);
} else {
Strcat(prompt, " or ");
Strcat(prompt, choices[i]->ef_txt);
}
++acount;
}
if (acount) {
/* flush buf */
Sprintf(buf, fmtstr, prompt);
any.a_char = prevaccelerator;
add_menu(win, NO_GLYPH, &any, any.a_char, 0, ATR_NONE, buf,
FALSE);
}
Sprintf(prompt, "Extended Command: %s", cbuf);
end_menu(win, prompt);
n = select_menu(win, PICK_ONE, &pick_list);
destroy_nhwindow(win);
if (n == 1) {
if (matchlevel > (QBUFSZ - 2)) {
free((genericptr_t) pick_list);
#if (NH_DEVEL_STATUS != NH_STATUS_RELEASED)
impossible("Too many chars (%d) entered in extcmd_via_menu()",
matchlevel);
#endif
ret = -1;
} else {
cbuf[matchlevel++] = pick_list[0].item.a_char;
cbuf[matchlevel] = '\0';
free((genericptr_t) pick_list);
}
} else {
if (matchlevel) {
ret = 0;
matchlevel = 0;
} else
ret = -1;
}
}
return ret;
}
#endif /* TTY_GRAPHICS */
/* #monster command - use special monster ability while polymorphed */
int
domonability(VOID_ARGS)
{
if (can_breathe(youmonst.data))
return dobreathe();
else if (attacktype(youmonst.data, AT_SPIT))
return dospit();
else if (youmonst.data->mlet == S_NYMPH)
return doremove();
else if (attacktype(youmonst.data, AT_GAZE))
return dogaze();
else if (is_were(youmonst.data))
return dosummon();
else if (webmaker(youmonst.data))
return dospinweb();
else if (is_hider(youmonst.data))
return dohide();
else if (is_mind_flayer(youmonst.data))
return domindblast();
else if (u.umonnum == PM_GREMLIN) {
if (IS_FOUNTAIN(levl[u.ux][u.uy].typ)) {
if (split_mon(&youmonst, (struct monst *) 0))
dryup(u.ux, u.uy, TRUE);
} else
There("is no fountain here.");
} else if (is_unicorn(youmonst.data)) {
use_unicorn_horn((struct obj *) 0);
return 1;
} else if (youmonst.data->msound == MS_SHRIEK) {
You("shriek.");
if (u.uburied)
pline("Unfortunately sound does not carry well through rock.");
else
aggravate();
} else if (youmonst.data->mlet == S_VAMPIRE)
return dopoly();
else if (Upolyd)
pline("Any special ability you may have is purely reflexive.");
else
You("don't have a special ability in your normal form!");
return 0;
}
int
enter_explore_mode(VOID_ARGS)
{
if (wizard) {
You("are in debug mode.");
} else if (discover) {
You("are already in explore mode.");
} else {
#ifdef SYSCF
#if defined(UNIX)
if (!sysopt.explorers || !sysopt.explorers[0]
|| !check_user_string(sysopt.explorers)) {
You("cannot access explore mode.");
return 0;
}
#endif
#endif
pline(
"Beware! From explore mode there will be no return to normal game.");
if (paranoid_query(ParanoidQuit,
"Do you want to enter explore mode?")) {
clear_nhwindow(WIN_MESSAGE);
You("are now in non-scoring explore mode.");
discover = TRUE;
} else {
clear_nhwindow(WIN_MESSAGE);
pline("Resuming normal game.");
}
}
return 0;
}
/* ^W command - wish for something */
STATIC_PTR int
wiz_wish(VOID_ARGS) /* Unlimited wishes for debug mode by Paul Polderman */
{
if (wizard) {
boolean save_verbose = flags.verbose;
flags.verbose = FALSE;
makewish();
flags.verbose = save_verbose;
(void) encumber_msg();
} else
pline(unavailcmd, visctrl((int) cmd_from_func(wiz_wish)));
return 0;
}
/* ^I command - reveal and optionally identify hero's inventory */
STATIC_PTR int
wiz_identify(VOID_ARGS)
{
if (wizard) {
iflags.override_ID = (int) cmd_from_func(wiz_identify);
/* command remapping might leave #wizidentify as the only way
to invoke us, in which case cmd_from_func() will yield NUL;
it won't matter to display_inventory()/display_pickinv()
if ^I invokes some other command--what matters is that
display_pickinv() and xname() see override_ID as nonzero */
if (!iflags.override_ID)
iflags.override_ID = C('I');
(void) display_inventory((char *) 0, FALSE);
iflags.override_ID = 0;
} else
pline(unavailcmd, visctrl((int) cmd_from_func(wiz_identify)));
return 0;
}
/* #wizmakemap - discard current dungeon level and replace with a new one */
STATIC_PTR int
wiz_makemap(VOID_ARGS)
{
if (wizard) {
struct monst *mtmp;
boolean was_in_W_tower = In_W_tower(u.ux, u.uy, &u.uz);
rm_mapseen(ledger_no(&u.uz));
for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
if (mtmp->isgd) { /* vault is going away; get rid of guard */
mtmp->isgd = 0;
mongone(mtmp);
}
if (DEADMONSTER(mtmp))
continue;
if (mtmp->isshk)
setpaid(mtmp);
/* TODO?
* Reduce 'born' tally for each monster about to be discarded
* by savelev(), otherwise replacing heavily populated levels
* tends to make their inhabitants become extinct.
*/
}
if (Punished) {
ballrelease(FALSE);
unplacebc();
}
/* reset lock picking unless it's for a carried container */
maybe_reset_pick((struct obj *) 0);
/* reset interrupted digging if it was taking place on this level */
if (on_level(&context.digging.level, &u.uz))
(void) memset((genericptr_t) &context.digging, 0,
sizeof (struct dig_info));
/* reset cached targets */
iflags.travelcc.x = iflags.travelcc.y = 0; /* travel destination */
context.polearm.hitmon = (struct monst *) 0; /* polearm target */
/* escape from trap */
reset_utrap(FALSE);
check_special_room(TRUE); /* room exit */
u.ustuck = (struct monst *) 0;
u.uswallow = 0;
u.uinwater = 0;
u.uundetected = 0; /* not hidden, even if means are available */
dmonsfree(); /* purge dead monsters from 'fmon' */
/* keep steed and other adjacent pets after releasing them
from traps, stopping eating, &c as if hero were ascending */
keepdogs(TRUE); /* (pets-only; normally we'd be using 'FALSE' here) */
/* discard current level; "saving" is used to release dynamic data */
savelev(-1, ledger_no(&u.uz), FREE_SAVE);
/* create a new level; various things like bestowing a guardian
angel on Astral or setting off alarm on Ft.Ludios are handled
by goto_level(do.c) so won't occur for replacement levels */
mklev();
vision_reset();
vision_full_recalc = 1;
cls();
/* was using safe_teleds() but that doesn't honor arrival region
on levels which have such; we don't force stairs, just area */
u_on_rndspot((u.uhave.amulet ? 1 : 0) /* 'going up' flag */
| (was_in_W_tower ? 2 : 0));
losedogs();
kill_genocided_monsters();
/* u_on_rndspot() might pick a spot that has a monster, or losedogs()
might pick the hero's spot (only if there isn't already a monster
there), so we might have to move hero or the co-located monster */
if ((mtmp = m_at(u.ux, u.uy)) != 0)
u_collide_m(mtmp);
initrack();
if (Punished) {
unplacebc();
placebc();
}
docrt();
flush_screen(1);
deliver_splev_message(); /* level entry */
check_special_room(FALSE); /* room entry */
#ifdef INSURANCE
save_currentstate();
#endif
} else {
pline(unavailcmd, "#wizmakemap");
}
return 0;
}
/* ^F command - reveal the level map and any traps on it */
STATIC_PTR int
wiz_map(VOID_ARGS)
{
if (wizard) {
struct trap *t;
long save_Hconf = HConfusion, save_Hhallu = HHallucination;
HConfusion = HHallucination = 0L;
for (t = ftrap; t != 0; t = t->ntrap) {
t->tseen = 1;
map_trap(t, TRUE);
}
do_mapping();
HConfusion = save_Hconf;
HHallucination = save_Hhallu;
} else
pline(unavailcmd, visctrl((int) cmd_from_func(wiz_map)));
return 0;
}
/* ^G command - generate monster(s); a count prefix will be honored */
STATIC_PTR int
wiz_genesis(VOID_ARGS)
{
if (wizard)
(void) create_particular();
else
pline(unavailcmd, visctrl((int) cmd_from_func(wiz_genesis)));
return 0;
}
/* ^O command - display dungeon layout */
STATIC_PTR int
wiz_where(VOID_ARGS)
{
if (wizard)
(void) print_dungeon(FALSE, (schar *) 0, (xchar *) 0);
else
pline(unavailcmd, visctrl((int) cmd_from_func(wiz_where)));
return 0;
}
/* ^E command - detect unseen (secret doors, traps, hidden monsters) */
STATIC_PTR int
wiz_detect(VOID_ARGS)
{
if (wizard)
(void) findit();
else
pline(unavailcmd, visctrl((int) cmd_from_func(wiz_detect)));
return 0;
}
/* ^V command - level teleport */
STATIC_PTR int
wiz_level_tele(VOID_ARGS)
{
if (wizard)
level_tele();
else
pline(unavailcmd, visctrl((int) cmd_from_func(wiz_level_tele)));
return 0;
}
/* #levelchange command - adjust hero's experience level */
STATIC_PTR int
wiz_level_change(VOID_ARGS)
{
char buf[BUFSZ] = DUMMY;
int newlevel = 0;
int ret;
getlin("To what experience level do you want to be set?", buf);
(void) mungspaces(buf);
if (buf[0] == '\033' || buf[0] == '\0')
ret = 0;
else
ret = sscanf(buf, "%d", &newlevel);
if (ret != 1) {
pline1(Never_mind);
return 0;
}
if (newlevel == u.ulevel) {
You("are already that experienced.");
} else if (newlevel < u.ulevel) {
if (u.ulevel == 1) {
You("are already as inexperienced as you can get.");
return 0;
}
if (newlevel < 1)
newlevel = 1;
while (u.ulevel > newlevel)
losexp("#levelchange");
} else {
if (u.ulevel >= MAXULEV) {
You("are already as experienced as you can get.");
return 0;
}
if (newlevel > MAXULEV)
newlevel = MAXULEV;
while (u.ulevel < newlevel)
pluslvl(FALSE);
}
u.ulevelmax = u.ulevel;
return 0;
}
/* #panic command - test program's panic handling */
STATIC_PTR int
wiz_panic(VOID_ARGS)
{
if (iflags.debug_fuzzer) {