forked from NetHack/NetHack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.c
6809 lines (6231 loc) · 223 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.7 cmd.c $NHDT-Date: 1684791777 2023/05/22 21:42:57 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.677 $ */
/* 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 "func_tab.h"
#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
#if (NH_DEVEL_STATUS != NH_STATUS_RELEASED) || defined(DEBUG)
static int wiz_display_macros(void);
static int wiz_mon_diff(void);
#endif
#ifdef DUMB /* stuff commented out in extern.h, but needed here */
extern int doapply(void); /**/
extern int dorub(void); /**/
extern int dojump(void); /**/
extern int doextlist(void); /**/
extern int enter_explore_mode(void); /**/
extern int dodrop(void); /**/
extern int doddrop(void); /**/
extern int dodown(void); /**/
extern int doup(void); /**/
extern int donull(void); /**/
extern int dowipe(void); /**/
extern int docallcnd(void); /**/
extern int dotakeoff(void); /**/
extern int doremring(void); /**/
extern int dowear(void); /**/
extern int doputon(void); /**/
extern int doddoremarm(void); /**/
extern int dokick(void); /**/
extern int dofire(void); /**/
extern int dothrow(void); /**/
extern int doeat(void); /**/
extern int done2(void); /**/
extern int dovanquished(void); /**/
extern int doengrave(void); /**/
extern int dopickup(void); /**/
extern int ddoinv(void); /**/
extern int dotypeinv(void); /**/
extern int dolook(void); /**/
extern int doprgold(void); /**/
extern int doprwep(void); /**/
extern int doprarm(void); /**/
extern int doprring(void); /**/
extern int dopramulet(void); /**/
extern int doprtool(void); /**/
extern int dosuspend(void); /**/
extern int doforce(void); /**/
extern int doopen(void); /**/
extern int doclose(void); /**/
extern int dosh(void); /**/
extern int dodiscovered(void); /**/
extern int doclassdisco(void); /**/
extern int doset_simple(void); /**/
extern int doset(void); /**/
extern int dotogglepickup(void); /**/
extern int dowhatis(void); /**/
extern int doquickwhatis(void); /**/
extern int dowhatdoes(void); /**/
extern int dohelp(void); /**/
extern int dohistory(void); /**/
extern int doloot(void); /**/
extern int dodrink(void); /**/
extern int dodip(void); /**/
extern int dosacrifice(void); /**/
extern int dopray(void); /**/
extern int dotip(void); /**/
extern int doturn(void); /**/
extern int doredraw(void); /**/
extern int doread(void); /**/
extern int dosave(void); /**/
extern int dosearch(void); /**/
extern int doidtrap(void); /**/
extern int dopay(void); /**/
extern int dosit(void); /**/
extern int dotalk(void); /**/
extern int docast(void); /**/
extern int dovspell(void); /**/
extern int dotelecmd(void); /**/
extern int dountrap(void); /**/
extern int doversion(void); /**/
extern int doextversion(void); /**/
extern int doswapweapon(void); /**/
extern int dowield(void); /**/
extern int dowieldquiver(void); /**/
extern int dozap(void); /**/
extern int doorganize(void); /**/
#endif /* DUMB */
static const char *ecname_from_fn(int (*)(void));
static int dosuspend_core(void);
static int dosh_core(void);
static int doherecmdmenu(void);
static int dotherecmdmenu(void);
static int doprev_message(void);
static int timed_occupation(void);
static boolean can_do_extcmd(const struct ext_func_tab *);
static int dotravel(void);
static int dotravel_target(void);
static int doclicklook(void);
static int domouseaction(void);
static int doterrain(void);
static int wiz_wish(void);
static int wiz_identify(void);
static int wiz_map(void);
static int wiz_makemap(void);
static int wiz_genesis(void);
static int wiz_where(void);
static int wiz_detect(void);
static int wiz_panic(void);
static int wiz_fuzzer(void);
static int wiz_polyself(void);
static int wiz_kill(void);
static int wiz_load_lua(void);
static int wiz_level_tele(void);
static int wiz_level_change(void);
static int wiz_flip_level(void);
static int wiz_show_seenv(void);
static int wiz_show_vision(void);
static int wiz_smell(void);
static int wiz_intrinsic(void);
static int wiz_show_wmodes(void);
static int wiz_show_stats(void);
static int wiz_rumor_check(void);
static int wiz_migrate_mons(void);
static void makemap_unmakemon(struct monst *, boolean);
static void makemap_remove_mons(void);
static void wiz_map_levltyp(void);
static void wiz_levltyp_legend(void);
#if defined(__BORLANDC__) && !defined(_WIN32)
extern void show_borlandc_stats(winid);
#endif
static int size_monst(struct monst *, boolean);
static int size_obj(struct obj *);
static void count_obj(struct obj *, long *, long *, boolean, boolean);
static void obj_chain(winid, const char *, struct obj *, boolean, long *,
long *);
static void mon_invent_chain(winid, const char *, struct monst *, long *,
long *);
static void mon_chain(winid, const char *, struct monst *, boolean, long *,
long *);
static void contained_stats(winid, const char *, long *, long *);
static void misc_stats(winid, long *, long *);
static void you_sanity_check(void);
static boolean accept_menu_prefix(const struct ext_func_tab *);
static void reset_cmd_vars(boolean);
static void mcmd_addmenu(winid, int, const char *);
static int there_cmd_menu_self(winid, coordxy, coordxy, int *);
static int there_cmd_menu_next2u(winid, coordxy, coordxy, int, int *);
static int there_cmd_menu_far(winid, coordxy, coordxy, int);
static int there_cmd_menu_common(winid, coordxy, coordxy, int, int *);
static void act_on_act(int, coordxy, coordxy);
static char there_cmd_menu(coordxy, coordxy, int);
static char here_cmd_menu(void);
static char readchar_core(coordxy *, coordxy *, int *);
static char *parse(void);
static void show_direction_keys(winid, char, boolean);
static boolean help_dir(char, uchar, const char *);
static int QSORTCALLBACK migrsort_cmp(const genericptr, const genericptr);
static void list_migrating_mons(d_level *);
static void handler_rebind_keys_add(boolean);
static boolean bind_key_fn(uchar, int (*)(void));
static void commands_init(void);
static boolean keylist_func_has_key(const struct ext_func_tab *, boolean *);
static int keylist_putcmds(winid, boolean, int, int, boolean *);
static const char *spkey_name(int);
static int (*timed_occ_fn)(void);
static char *doc_extcmd_flagstr(winid, const struct ext_func_tab *);
static const char *readchar_queue = "";
/* 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.";
/* the #prevmsg command */
static int
doprev_message(void)
{
(void) nh_doprev_message();
return ECMD_OK;
}
/* Count down by decrementing multi */
static int
timed_occupation(void)
{
(*timed_occ_fn)();
if (gm.multi > 0)
gm.multi--;
return gm.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(void)
{
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(int (*fn)(void), const char *txt, cmdcount_nht xtime)
{
if (xtime) {
go.occupation = timed_occupation;
timed_occ_fn = fn;
} else
go.occupation = fn;
go.occtxt = txt;
go.occtime = 0;
return;
}
/*
void
cmdq_print(int q)
{
struct _cmd_queue *cq = gc.command_queue[q];
char buf[QBUFSZ];
pline("CQ:%i", q);
while (cq) {
switch (cq->typ) {
case CMDQ_KEY: pline("(key:%s)", key2txt(cq->key, buf)); break;
case CMDQ_EXTCMD: pline("(extcmd:#%s)", cq->ec_entry->ef_txt); break;
case CMDQ_DIR: pline("(dir:%i,%i,%i)", cq->dirx, cq->diry, cq->dirz); break;
case CMDQ_USER_INPUT: pline1("(userinput)"); break;
case CMDQ_INT: pline("(int:%i)", cq->intval); break;
default: pline("(ERROR:%i)",cq->typ); break;
}
cq = cq->next;
}
}
*/
/* add extended command function to the command queue */
void
cmdq_add_ec(int q, int (*fn)(void))
{
struct _cmd_queue *tmp = (struct _cmd_queue *) alloc(sizeof *tmp);
struct _cmd_queue *cq = gc.command_queue[q];
tmp->typ = CMDQ_EXTCMD;
tmp->ec_entry = ext_func_tab_from_func(fn);
tmp->next = NULL;
while (cq && cq->next)
cq = cq->next;
if (cq)
cq->next = tmp;
else
gc.command_queue[q] = tmp;
}
/* add a key to the command queue */
void
cmdq_add_key(int q, char key)
{
struct _cmd_queue *tmp = (struct _cmd_queue *) alloc(sizeof *tmp);
struct _cmd_queue *cq = gc.command_queue[q];
tmp->typ = CMDQ_KEY;
tmp->key = key;
tmp->next = NULL;
while (cq && cq->next)
cq = cq->next;
if (cq)
cq->next = tmp;
else
gc.command_queue[q] = tmp;
}
/* add a direction to the command queue */
void
cmdq_add_dir(int q, schar dx, schar dy, schar dz)
{
struct _cmd_queue *tmp = (struct _cmd_queue *) alloc(sizeof *tmp);
struct _cmd_queue *cq = gc.command_queue[q];
tmp->typ = CMDQ_DIR;
tmp->dirx = dx;
tmp->diry = dy;
tmp->dirz = dz;
tmp->next = NULL;
while (cq && cq->next)
cq = cq->next;
if (cq)
cq->next = tmp;
else
gc.command_queue[q] = tmp;
}
/* add placeholder to the command queue, allows user input there */
void
cmdq_add_userinput(int q)
{
struct _cmd_queue *tmp = (struct _cmd_queue *) alloc(sizeof *tmp);
struct _cmd_queue *cq = gc.command_queue[q];
tmp->typ = CMDQ_USER_INPUT;
tmp->next = NULL;
while (cq && cq->next)
cq = cq->next;
if (cq)
cq->next = tmp;
else
gc.command_queue[q] = tmp;
}
/* add integer to the command queue */
void
cmdq_add_int(int q, int val)
{
struct _cmd_queue *tmp = (struct _cmd_queue *) alloc(sizeof *tmp);
struct _cmd_queue *cq = gc.command_queue[q];
tmp->typ = CMDQ_INT;
tmp->intval = val;
tmp->next = NULL;
while (cq && cq->next)
cq = cq->next;
if (cq)
cq->next = tmp;
else
gc.command_queue[q] = tmp;
}
/* shift the last entry in command queue to first */
void
cmdq_shift(int q)
{
struct _cmd_queue *tmp = NULL;
struct _cmd_queue *cq = gc.command_queue[q];
while (cq && cq->next && cq->next->next)
cq = cq->next;
if (cq)
tmp = cq->next;
if (tmp) {
tmp->next = gc.command_queue[q];
gc.command_queue[q] = tmp;
cq->next = NULL;
}
}
struct _cmd_queue *
cmdq_reverse(struct _cmd_queue *head)
{
struct _cmd_queue *prev = NULL, *curr = head, *next;
while (curr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
struct _cmd_queue *
cmdq_copy(int q)
{
struct _cmd_queue *tmp = NULL;
struct _cmd_queue *cq = gc.command_queue[q];
while (cq) {
struct _cmd_queue *tmp2 = (struct _cmd_queue *) alloc(sizeof *tmp2);
*tmp2 = *cq;
tmp2->next = tmp;
tmp = tmp2;
cq = cq->next;
}
tmp = cmdq_reverse(tmp);
return tmp;
}
/* pop off the topmost command from the command queue.
* caller is responsible for freeing the returned _cmd_queue.
*/
struct _cmd_queue *
cmdq_pop(void)
{
int q = (gi.in_doagain) ? CQ_REPEAT : CQ_CANNED;
struct _cmd_queue *tmp = gc.command_queue[q];
if (tmp) {
gc.command_queue[q] = tmp->next;
tmp->next = NULL;
}
return tmp;
}
/* get the top entry without popping it */
struct _cmd_queue *
cmdq_peek(int q)
{
return gc.command_queue[q];
}
/* clear all commands from the command queue */
void
cmdq_clear(int q)
{
struct _cmd_queue *tmp = gc.command_queue[q];
struct _cmd_queue *tmp2;
while (tmp) {
tmp2 = tmp->next;
free(tmp);
tmp = tmp2;
}
gc.command_queue[q] = NULL;
}
char
pgetchar(void) /* courtesy of [email protected] */
{
register int ch = '\0';
if (iflags.debug_fuzzer)
return randomkey();
ch = nhgetch();
return (char) ch;
}
/* '#' or whatever has been bound to doextcmd() in its place */
char
extcmd_initiator(void)
{
return gc.Cmd.extcmd_char;
}
static boolean
can_do_extcmd(const struct ext_func_tab *extcmd)
{
int ecflags = extcmd->flags;
if (gl.luacore && nhcb_counts[NHCB_CMD_BEFORE]) {
lua_getglobal(gl.luacore, "nh_callback_run");
lua_pushstring(gl.luacore, nhcb_name[NHCB_CMD_BEFORE]);
lua_pushstring(gl.luacore, extcmd->ef_txt);
nhl_pcall(gl.luacore, 2, 1);
if (!lua_toboolean(gl.luacore, -1))
return FALSE;
}
if (!wizard && (ecflags & WIZMODECMD)) {
pline(unavailcmd, extcmd->ef_txt);
return FALSE;
} else if (u.uburied && !(ecflags & IFBURIED)) {
You_cant("do that while you are buried!");
return FALSE;
} else if (iflags.debug_fuzzer && (ecflags & NOFUZZERCMD)) {
return FALSE;
}
return TRUE;
}
/* here after # - now read a full-word command */
int
doextcmd(void)
{
int idx, retval;
int (*func)(void);
/* keep repeating until we don't run help or quit */
do {
idx = get_ext_cmd();
if (idx < 0)
return ECMD_OK; /* quit */
func = extcmdlist[idx].ef_funct;
if (!can_do_extcmd(&extcmdlist[idx]))
return ECMD_OK;
if (iflags.menu_requested && !accept_menu_prefix(&extcmdlist[idx])) {
pline("'%s' prefix has no effect for the %s command.",
visctrl(cmd_from_func(do_reqmenu)),
extcmdlist[idx].ef_txt);
iflags.menu_requested = FALSE;
}
/* tell rhack() what command is actually executing */
ge.ext_tlist = &extcmdlist[idx];
retval = (*func)();
} while (func == doextlist);
return retval;
}
/* format extended command flags for display */
static char *
doc_extcmd_flagstr(
winid menuwin,
const struct ext_func_tab *efp) /* if Null, add a footnote to the menu */
{
static char Abuf[10]; /* 5 would suffice: {'[','m','A',']','\0'} */
int clr = 0;
/* note: tag shown for menu prefix is 'm' even if m-prefix action
has been bound to some other key */
if (!efp) {
char qbuf[QBUFSZ];
anything any = cg.zeroany;
add_menu(menuwin, &nul_glyphinfo, &any, 0, 0, ATR_NONE, clr,
"[A] Command autocompletes", MENU_ITEMFLAGS_NONE);
Sprintf(qbuf, "[m] Command accepts '%s' prefix",
visctrl(cmd_from_func(do_reqmenu)));
add_menu(menuwin, &nul_glyphinfo, &any, 0, 0, ATR_NONE, clr, qbuf,
MENU_ITEMFLAGS_NONE);
return (char *) 0;
} else {
boolean mprefix = accept_menu_prefix(efp),
autocomplete = (efp->flags & AUTOCOMPLETE) != 0;
char *p = Abuf;
/* "" or "[m]" or "[A]" or "[mA]" */
if (mprefix || autocomplete) {
*p++ = '[';
if (mprefix)
*p++ = 'm';
if (autocomplete)
*p++ = 'A';
*p++ = ']';
}
*p = '\0';
return Abuf;
}
}
/* here after #? - now list all full-word commands and provide
some navigation capability through the long list */
int
doextlist(void)
{
register const struct ext_func_tab *efp = (struct ext_func_tab *) 0;
char buf[BUFSZ], searchbuf[BUFSZ], descbuf[BUFSZ], promptbuf[QBUFSZ];
const char *cmd_desc;
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 *const headings[] = { "Extended commands",
"Debugging Extended Commands" };
int clr = 0;
searchbuf[0] = '\0';
menuwin = create_nhwindow(NHW_MENU);
while (redisplay) {
redisplay = FALSE;
any = cg.zeroany;
start_menu(menuwin, MENU_BEHAVE_STANDARD);
add_menu(menuwin, &nul_glyphinfo, &any, 0, 0, ATR_NONE, clr,
"Extended Commands List",
MENU_ITEMFLAGS_NONE);
add_menu(menuwin, &nul_glyphinfo, &any, 0, 0, ATR_NONE, clr,
"", MENU_ITEMFLAGS_NONE);
Sprintf(buf, "Switch to %s commands that don't autocomplete",
menumode ? "including" : "excluding");
any.a_int = 1;
add_menu(menuwin, &nul_glyphinfo, &any, 'a', 0, ATR_NONE, clr, buf,
MENU_ITEMFLAGS_NONE);
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, &nul_glyphinfo, &any, ':', 's', ATR_NONE,
clr, "Search extended commands",
MENU_ITEMFLAGS_NONE);
} else {
Strcpy(buf, "Switch back from 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, &nul_glyphinfo, &any, 's', ':', ATR_NONE,
clr, buf, MENU_ITEMFLAGS_NONE);
}
if (wizard) {
any.a_int = 4;
add_menu(menuwin, &nul_glyphinfo, &any, 'z', 0, ATR_NONE, clr,
onelist ? "Switch to showing debugging commands in separate section"
: "Switch to showing all alphabetically, including debugging commands",
MENU_ITEMFLAGS_NONE);
}
any = cg.zeroany;
add_menu(menuwin, &nul_glyphinfo, &any, 0, 0, ATR_NONE, clr,
"", MENU_ITEMFLAGS_NONE);
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 | INTERNALCMD)) != 0)
continue;
/* if hiding non-autocomplete commands, skip such */
if (menumode == 1 && (efp->flags & AUTOCOMPLETE) == 0)
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;
/* command descripton might get modified on the fly */
cmd_desc = efp->ef_desc;
/* suppress part of the descripton for #genocided if it
doesn't apply during the current game */
if (!wizard && !discover
&& (efp->flags & GENERALCMD) != 0 /* minor optimization */
&& strstri(cmd_desc, "extinct"))
cmd_desc = strsubst(strcpy(descbuf, cmd_desc),
" been genocided or become extinct",
" been genocided");
/* if searching, skip this command if it doesn't match */
if (*searchbuf
/* first try case-insensitive substring match */
&& !strstri(efp->ef_txt, searchbuf)
&& !strstri(cmd_desc, searchbuf)
/* wildcard support; most interfaces use case-insensitive
pmatch rather than regexp for menu searching */
&& !pmatchi(searchbuf, efp->ef_txt)
&& !pmatchi(searchbuf, cmd_desc))
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, &nul_glyphinfo, &any, 0, 0,
iflags.menu_headings, clr, buf,
MENU_ITEMFLAGS_NONE);
menushown[pass] = 1;
}
/* longest ef_txt at present is "wizrumorcheck" (13 chars);
2nd field will be " " or " [A]" or " [m]" or "[mA]" */
Sprintf(buf, " %-14s %4s %s", efp->ef_txt,
doc_extcmd_flagstr(menuwin, efp), cmd_desc);
add_menu(menuwin, &nul_glyphinfo, &any, 0, 0, ATR_NONE,
clr, buf, MENU_ITEMFLAGS_NONE);
++n;
}
if (n)
add_menu(menuwin, &nul_glyphinfo, &any, 0, 0, ATR_NONE,
clr, "", MENU_ITEMFLAGS_NONE);
}
if (*searchbuf && !n)
add_menu(menuwin, &nul_glyphinfo, &any, 0, 0, ATR_NONE,
clr, "no matches", MENU_ITEMFLAGS_NONE);
else
(void) doc_extcmd_flagstr(menuwin, (struct ext_func_tab *) 0);
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 ECMD_OK;
}
#if defined(TTY_GRAPHICS) || defined(CURSES_GRAPHICS)
#define MAX_EXT_CMD 200 /* Change if we ever have more ext cmds */
DISABLE_WARNING_FORMAT_NONLITERAL
/*
* 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(void)
{
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;
int clr = 0;
ret = 0;
cbuf[0] = '\0';
biggest = 0;
while (!ret) {
i = n = 0;
any = cg.zeroany;
/* populate choices */
for (efp = extcmdlist; efp->ef_txt; efp++) {
if ((efp->flags & (CMD_NOT_AVAILABLE|INTERNALCMD))
|| !(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, MENU_BEHAVE_STANDARD);
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, &nul_glyphinfo, &any, any.a_char,
0, ATR_NONE, clr, buf, MENU_ITEMFLAGS_NONE);
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, &nul_glyphinfo, &any, any.a_char, 0,
ATR_NONE, clr, buf, MENU_ITEMFLAGS_NONE);
}
Snprintf(prompt, sizeof(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;
}
RESTORE_WARNING_FORMAT_NONLITERAL
#endif /* TTY_GRAPHICS */
/* #monster command - use special monster ability while polymorphed */
int
domonability(void)
{
struct permonst *uptr = gy.youmonst.data;
boolean might_hide = (is_hider(uptr) || hides_under(uptr));
char c = '\0';
if (might_hide && webmaker(uptr)) {
c = yn_function("Hide [h] or spin a web [s]?", "hsq", 'q', TRUE);
if (c == 'q' || c == '\033')
return ECMD_OK;
}
if (can_breathe(uptr))
return dobreathe();
else if (attacktype(uptr, AT_SPIT))
return dospit();
else if (uptr->mlet == S_NYMPH)
return doremove();
else if (attacktype(uptr, AT_GAZE))
return dogaze();
else if (is_were(uptr))
return dosummon();
else if (c ? c == 'h' : might_hide)
return dohide();
else if (c ? c == 's' : webmaker(uptr))
return dospinweb();
else if (is_mind_flayer(uptr))
return domindblast();
else if (u.umonnum == PM_GREMLIN) {
if (IS_FOUNTAIN(levl[u.ux][u.uy].typ)) {
if (split_mon(&gy.youmonst, (struct monst *) 0))
dryup(u.ux, u.uy, TRUE);
} else
There("is no fountain here.");
} else if (is_unicorn(uptr)) {
use_unicorn_horn((struct obj **) 0);
return ECMD_TIME;
} else if (uptr->msound == MS_SHRIEK) {
You("shriek.");
if (u.uburied)
pline("Unfortunately sound does not carry well through rock.");
else
aggravate();
} else if (is_vampire(uptr) || is_vampshifter(&gy.youmonst)) {
return dopoly();
} else if (u.usteed && can_breathe(u.usteed->data)) {
(void) pet_ranged_attk(u.usteed);
return ECMD_TIME;
} 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 ECMD_OK;
}
int
enter_explore_mode(void)
{
if (discover) {
You("are already in explore mode.");
} else {
const char *oldmode = !wizard ? "normal game" : "debug mode";
#ifdef SYSCF
#if defined(UNIX)
if (!sysopt.explorers || !sysopt.explorers[0]
|| !check_user_string(sysopt.explorers)) {
if (!wizard) {
You("cannot access explore mode.");
return ECMD_OK;
} else {
pline(
"Note: normally you wouldn't be allowed into explore mode.");
/* keep going */
}
}
#endif
#endif
pline("Beware! From explore mode there will be no return to %s,",
oldmode);
if (paranoid_query(ParanoidQuit,
"Do you want to enter explore mode?")) {