forked from angband/angband
/
cmd3.c
797 lines (653 loc) · 16.6 KB
/
cmd3.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
/*
* File: cmd3.c
* Purpose: Miscellaneous queries
*
* Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
*
* This work is free software; you can redistribute it and/or modify it
* under the terms of either:
*
* a) the GNU General Public License as published by the Free Software
* Foundation, version 2, or
*
* b) the "Angband licence":
* This software may be copied and distributed for educational, research,
* and not for profit purposes provided that this copyright and statement
* are included in all such copies. Other copyrights may also apply.
*/
#include "angband.h"
#include "button.h"
#include "cave.h"
#include "cmds.h"
#include "monster/mon-lore.h"
#include "monster/monster.h"
#include "object/inventory.h"
#include "object/tvalsval.h"
#include "object/object.h"
#include "squelch.h"
#include "ui-menu.h"
#include "target.h"
/*
* Pick the context menu options appropiate for the item
* bits (1<<(n-1)):
* 1 - item can be examined
* 2 - item can be wielded
* 3 - item can be taken off
* 4 - item can be inscribed
* 5 - item can be uninscribed
* 6 - item can be dropped
* 7 - item can be picked up
* 8 - item can be used (name,key are specific to item type)
* 9 - item(book) can be browsed
* 10 - item(book) can be studdied
* 11 - item can be used to refill light source
*/
int object_context_bits(const object_type *o_ptr, const int slot)
{
int bits = 0;
if (o_ptr) {
bits |= 1;
} else {
return bits;
}
if ((slot > INVEN_WIELD) && obj_can_takeoff(o_ptr)) {
bits |= 4;
} else
if ((slot <= INVEN_WIELD) && obj_can_wear(o_ptr)) {
bits |= 2;
}
if (obj_has_inscrip(o_ptr)) {
bits |= 16;
} else
{
bits |= 8;
}
if (slot >= 0) {
bits |= 32;
} else
{
bits |= 64;
}
if (obj_is_useable(o_ptr)) {
bits |= 128;
}
if (obj_can_browse(o_ptr)) {
bits |= 256;
if (obj_can_cast_from(o_ptr)) {
bits |= 128;
}
if (obj_can_study(o_ptr)) {
bits |= 512;
}
}
if (obj_can_refill(o_ptr)) {
bits |= 1024;
}
return bits;
}
/*
* Display inventory
*/
int context_menu_object(const object_type *o_ptr, const int slot);
void do_cmd_inven(void)
{
int item;
int ret = 3;
int diff = weight_remaining();
/* Hack -- Start in "inventory" mode */
p_ptr->command_wrk = (USE_INVEN);
/* Hack -- show empty slots */
item_tester_full = TRUE;
/* Loop this menu until an object context menu says differently */
while (ret == 3) {
/* Save screen */
screen_save();
/* Prompt for a command */
prt(format("(Inventory) Burden %d.%d lb (%d.%d lb %s). Select Item: ",
p_ptr->total_weight / 10, p_ptr->total_weight % 10,
abs(diff) / 10, abs(diff) % 10,
(diff < 0 ? "overweight" : "remaining")),
0, 0);
/* Get an item to use a context command on (Display the inventory) */
if (get_item(&item, NULL, NULL, CMD_NULL, USE_EQUIP|USE_INVEN|USE_FLOOR|IS_HARMLESS)) {
object_type *o_ptr;
/* Load screen */
screen_load();
/* Track the object kind */
track_object(item);
o_ptr = object_from_item_idx(item);
if (o_ptr && o_ptr->kind) {
while ((ret = context_menu_object(o_ptr, item)) == 2);
}
} else {
/* Load screen */
screen_load();
ret = -1;
}
}
/* Hack -- hide empty slots */
item_tester_full = FALSE;
}
/*
* Display equipment
*/
void do_cmd_equip(void)
{
int item;
int ret = 3;
/* Hack -- Start in "inventory" mode */
p_ptr->command_wrk = (USE_EQUIP);
/* Hack -- show empty slots */
item_tester_full = TRUE;
/* Loop this menu until an object context menu says differently */
while (ret == 3) {
/* Save screen */
screen_save();
/* Get an item to use a context command on (Display the inventory) */
if (get_item(&item, "Select Item:", NULL, CMD_NULL, USE_EQUIP|USE_INVEN|USE_FLOOR|IS_HARMLESS)) {
object_type *o_ptr;
/* Load screen */
screen_load();
/* Track the object kind */
track_object(item);
o_ptr = object_from_item_idx(item);
if (o_ptr && o_ptr->kind) {
while ((ret = context_menu_object(o_ptr, item)) == 2);
}
} else {
/* Load screen */
screen_load();
ret = -1;
}
}
/* Hack -- hide empty slots */
item_tester_full = FALSE;
}
enum
{
IGNORE_THIS_ITEM,
UNIGNORE_THIS_ITEM,
IGNORE_THIS_FLAVOR,
UNIGNORE_THIS_FLAVOR,
IGNORE_THIS_QUALITY
};
void textui_cmd_destroy_menu(int item)
{
object_type *o_ptr;
char out_val[160];
menu_type *m;
region r;
int selected;
o_ptr = object_from_item_idx(item);
if (!(o_ptr->kind))
return;
m = menu_dynamic_new();
m->selections = lower_case;
/* Basic ignore option */
if (!o_ptr->ignore) {
menu_dynamic_add(m, "This item only", IGNORE_THIS_ITEM);
} else {
menu_dynamic_add(m, "Unignore this item", UNIGNORE_THIS_ITEM);
}
/* Flavour-aware squelch */
if (squelch_tval(o_ptr->tval) &&
(!o_ptr->artifact || !object_flavor_is_aware(o_ptr))) {
bool squelched = kind_is_squelched_aware(o_ptr->kind) ||
kind_is_squelched_unaware(o_ptr->kind);
char tmp[70];
object_desc(tmp, sizeof(tmp), o_ptr, ODESC_BASE | ODESC_PLURAL);
if (!squelched) {
strnfmt(out_val, sizeof out_val, "All %s", tmp);
menu_dynamic_add(m, out_val, IGNORE_THIS_FLAVOR);
} else {
strnfmt(out_val, sizeof out_val, "Unignore all %s", tmp);
menu_dynamic_add(m, out_val, UNIGNORE_THIS_FLAVOR);
}
}
/* Quality squelching */
if (object_was_sensed(o_ptr) || object_was_worn(o_ptr) ||
object_is_known_not_artifact(o_ptr)) {
byte value = squelch_level_of(o_ptr);
int type = squelch_type_of(o_ptr);
if (object_is_jewelry(o_ptr) &&
squelch_level_of(o_ptr) != SQUELCH_BAD)
value = SQUELCH_MAX;
if (value != SQUELCH_MAX && type != TYPE_MAX) {
strnfmt(out_val, sizeof out_val, "All %s %s",
quality_values[value].name, quality_choices[type].name);
menu_dynamic_add(m, out_val, IGNORE_THIS_QUALITY);
}
}
/* work out display region */
r.width = menu_dynamic_longest_entry(m) + 3 + 2; /* +3 for tag, 2 for pad */
r.col = 80 - r.width;
r.row = 1;
r.page_rows = m->count;
screen_save();
menu_layout(m, &r);
region_erase_bordered(&r);
prt("(Enter to select, ESC) Ignore:", 0, 0);
selected = menu_dynamic_select(m);
screen_load();
if (selected == IGNORE_THIS_ITEM) {
cmd_insert(CMD_DESTROY);
cmd_set_arg_item(cmd_get_top(), 0, item);
} else if (selected == UNIGNORE_THIS_ITEM) {
o_ptr->ignore = FALSE;
} else if (selected == IGNORE_THIS_FLAVOR) {
object_squelch_flavor_of(o_ptr);
} else if (selected == UNIGNORE_THIS_FLAVOR) {
kind_squelch_clear(o_ptr->kind);
} else if (selected == IGNORE_THIS_QUALITY) {
byte value = squelch_level_of(o_ptr);
int type = squelch_type_of(o_ptr);
squelch_level[type] = value;
}
p_ptr->notice |= PN_SQUELCH;
menu_dynamic_free(m);
}
void textui_cmd_destroy(void)
{
int item;
/* Get an item */
const char *q = "Ignore which item? ";
const char *s = "You have nothing to ignore.";
if (!get_item(&item, q, s, CMD_DESTROY, USE_INVEN | USE_EQUIP | USE_FLOOR))
return;
textui_cmd_destroy_menu(item);
}
void textui_cmd_toggle_ignore(void)
{
p_ptr->unignoring = !p_ptr->unignoring;
p_ptr->notice |= PN_SQUELCH;
do_cmd_redraw();
}
/* Examine an object */
void textui_obj_examine(void)
{
char header[120];
textblock *tb;
region area = { 0, 0, 0, 0 };
object_type *o_ptr;
int item;
/* Select item */
if (!get_item(&item, "Examine which item?", "You have nothing to examine.",
CMD_NULL, (USE_EQUIP | USE_INVEN | USE_FLOOR | IS_HARMLESS)))
return;
/* Track object for object recall */
track_object(item);
/* Display info */
o_ptr = object_from_item_idx(item);
tb = object_info(o_ptr, OINFO_NONE);
object_desc(header, sizeof(header), o_ptr,
ODESC_PREFIX | ODESC_FULL | ODESC_CAPITAL);
textui_textblock_show(tb, area, header);
textblock_free(tb);
}
/*
* Target command
*/
void do_cmd_target(void)
{
if (target_set_interactive(TARGET_KILL, -1, -1))
msg("Target Selected.");
else
msg("Target Aborted.");
}
void do_cmd_target_closest(void)
{
target_set_closest(TARGET_KILL);
}
/*
* Look command
*/
void do_cmd_look(void)
{
/* Look around */
if (target_set_interactive(TARGET_LOOK, -1, -1))
{
msg("Target Selected.");
}
}
/*
* Allow the player to examine other sectors on the map
*/
void do_cmd_locate(void)
{
int dir, y1, x1, y2, x2;
char tmp_val[80];
char out_val[160];
/* Start at current panel */
y1 = Term->offset_y;
x1 = Term->offset_x;
/* Show panels until done */
while (1)
{
/* Get the current panel */
y2 = Term->offset_y;
x2 = Term->offset_x;
/* Describe the location */
if ((y2 == y1) && (x2 == x1))
{
tmp_val[0] = '\0';
}
else
{
strnfmt(tmp_val, sizeof(tmp_val), "%s%s of",
((y2 < y1) ? " north" : (y2 > y1) ? " south" : ""),
((x2 < x1) ? " west" : (x2 > x1) ? " east" : ""));
}
/* Prepare to ask which way to look */
strnfmt(out_val, sizeof(out_val),
"Map sector [%d,%d], which is%s your sector. Direction?",
(y2 / PANEL_HGT), (x2 / PANEL_WID), tmp_val);
/* More detail */
if (OPT(center_player))
{
strnfmt(out_val, sizeof(out_val),
"Map sector [%d(%02d),%d(%02d)], which is%s your sector. Direction?",
(y2 / PANEL_HGT), (y2 % PANEL_HGT),
(x2 / PANEL_WID), (x2 % PANEL_WID), tmp_val);
}
/* Assume no direction */
dir = 0;
/* Get a direction */
while (!dir)
{
struct keypress command;
/* Get a command (or Cancel) */
if (!get_com(out_val, &command)) break;
/* Extract direction */
dir = target_dir(command);
/* Error */
if (!dir) bell("Illegal direction for locate!");
}
/* No direction */
if (!dir) break;
/* Apply the motion */
change_panel(dir);
/* Handle stuff */
handle_stuff(p_ptr);
}
/* Verify panel */
verify_panel();
}
static int cmp_mexp(const void *a, const void *b)
{
u16b ia = *(const u16b *)a;
u16b ib = *(const u16b *)b;
if (r_info[ia].mexp < r_info[ib].mexp)
return -1;
if (r_info[ia].mexp > r_info[ib].mexp)
return 1;
return (a < b ? -1 : (a > b ? 1 : 0));
}
static int cmp_level(const void *a, const void *b)
{
u16b ia = *(const u16b *)a;
u16b ib = *(const u16b *)b;
if (r_info[ia].level < r_info[ib].level)
return -1;
if (r_info[ia].level > r_info[ib].level)
return 1;
return cmp_mexp(a, b);
}
static int cmp_tkill(const void *a, const void *b)
{
u16b ia = *(const u16b *)a;
u16b ib = *(const u16b *)b;
if (l_list[ia].tkills < l_list[ib].tkills)
return -1;
if (l_list[ia].tkills > l_list[ib].tkills)
return 1;
return cmp_level(a, b);
}
static int cmp_pkill(const void *a, const void *b)
{
u16b ia = *(const u16b *)a;
u16b ib = *(const u16b *)b;
if (l_list[ia].pkills < l_list[ib].pkills)
return -1;
if (l_list[ia].pkills > l_list[ib].pkills)
return 1;
return cmp_tkill(a, b);
}
int cmp_monsters(const void *a, const void *b)
{
return cmp_level(a, b);
}
/*
* Search the monster, item, and feature types to find the
* meaning for the given symbol.
*
* Note: We currently search items first, then features, then
* monsters, and we return the first hit for a symbol.
* This is to prevent mimics and lurkers from matching
* a symbol instead of the item or feature it is mimicking.
*
* Todo: concatenate all matches into buf. This will be much
* easier once we can loop through item tvals instead of items
* (see note below.)
*
* Todo: Should this take the user's pref files into account?
*/
static void lookup_symbol(struct keypress sym, char *buf, size_t max)
{
int i;
monster_base *race;
/* Look through items */
/* Note: We currently look through all items, and grab the tval when we find a match.
It would make more sense to loop through tvals, but then we need to associate
a display character with each tval. */
for (i = 1; i < z_info->k_max; i++) {
if (char_matches_key(k_info[i].d_char, sym.code)) {
strnfmt(buf, max, "%c - %s.", (char)sym.code, tval_find_name(k_info[i].tval));
return;
}
}
/* Look through features */
/* Note: We need a better way of doing this. Currently '#' matches secret door,
and '^' matches trap door (instead of the more generic "trap"). */
for (i = 1; i < z_info->f_max; i++) {
if (char_matches_key(f_info[i].d_char, sym.code)) {
strnfmt(buf, max, "%c - %s.", (char)sym.code, f_info[i].name);
return;
}
}
/* Look through monster templates */
for (race = rb_info; race; race = race->next){
if (char_matches_key(race->d_char, sym.code)) {
strnfmt(buf, max, "%c - %s.", (char)sym.code, race->text);
return;
}
}
/* No matches */
strnfmt(buf, max, "%c - %s.", (char)sym.code, "Unknown Symbol");
return;
}
/*
* Identify a character, allow recall of monsters
*
* Several "special" responses recall "multiple" monsters:
* ^A (all monsters)
* ^U (all unique monsters)
* ^N (all non-unique monsters)
*
* The responses may be sorted in several ways, see below.
*
* Note that the player ghosts are ignored, since they do not exist.
*/
void do_cmd_query_symbol(void)
{
int i, n, r_idx;
char buf[128];
struct keypress sym;
struct keypress query;
bool all = FALSE;
bool uniq = FALSE;
bool norm = FALSE;
bool recall = FALSE;
u16b *who;
const monster_race *r_ptr;
const monster_lore *l_ptr;
/* Get a character, or abort */
if (!get_com("Enter character to be identified, or control+[ANU]: ", &sym))
return;
/* Describe */
if (sym.code == KTRL('A'))
{
all = TRUE;
my_strcpy(buf, "Full monster list.", sizeof(buf));
}
else if (sym.code == KTRL('U'))
{
all = uniq = TRUE;
my_strcpy(buf, "Unique monster list.", sizeof(buf));
}
else if (sym.code == KTRL('N'))
{
all = norm = TRUE;
my_strcpy(buf, "Non-unique monster list.", sizeof(buf));
}
else
{
lookup_symbol(sym, buf, sizeof(buf));
}
/* Display the result */
prt(buf, 0, 0);
/* Allocate the "who" array */
who = C_ZNEW(z_info->r_max, u16b);
/* Collect matching monsters */
for (n = 0, i = 1; i < z_info->r_max - 1; i++)
{
monster_race *r_ptr = &r_info[i];
monster_lore *l_ptr = &l_list[i];
/* Nothing to recall */
if (!OPT(cheat_know) && !l_ptr->sights) continue;
/* Require non-unique monsters if needed */
if (norm && rf_has(r_ptr->flags, RF_UNIQUE)) continue;
/* Require unique monsters if needed */
if (uniq && !rf_has(r_ptr->flags, RF_UNIQUE)) continue;
/* Collect "appropriate" monsters */
if (all || char_matches_key(r_ptr->d_char, sym.code)) who[n++] = i;
}
/* Nothing to recall */
if (!n)
{
/* XXX XXX Free the "who" array */
FREE(who);
return;
}
/* Buttons */
button_add("[y]", 'y');
button_add("[k]", 'k');
/* Don't collide with the repeat button */
button_add("[n]", 'q');
redraw_stuff(p_ptr);
/* Prompt */
put_str("Recall details? (y/k/n): ", 0, 40);
/* Query */
query = inkey();
/* Restore */
prt(buf, 0, 0);
/* Buttons */
button_kill('y');
button_kill('k');
button_kill('q');
redraw_stuff(p_ptr);
/* Interpret the response */
if (query.code == 'k')
{
/* Sort by kills (and level) */
sort(who, n, sizeof(*who), cmp_pkill);
}
else if (query.code == 'y' || query.code == 'p')
{
/* Sort by level; accept 'p' as legacy */
sort(who, n, sizeof(*who), cmp_level);
}
else
{
/* Any unsupported response is "nope, no history please" */
/* XXX XXX Free the "who" array */
FREE(who);
return;
}
/* Start at the end */
i = n - 1;
/* Button */
button_add("[r]", 'r');
button_add("[-]", '-');
button_add("[+]", '+');
redraw_stuff(p_ptr);
/* Scan the monster memory */
while (1)
{
/* Extract a race */
r_idx = who[i];
r_ptr = &r_info[r_idx];
l_ptr = &l_list[r_idx];
/* Hack -- Auto-recall */
monster_race_track(r_idx);
/* Hack -- Handle stuff */
handle_stuff(p_ptr);
/* Hack -- Begin the prompt */
roff_top(r_ptr);
/* Hack -- Complete the prompt */
Term_addstr(-1, TERM_WHITE, " [(r)ecall, ESC]");
/* Interact */
while (1)
{
/* Recall */
if (recall)
{
/* Save screen */
screen_save();
/* Recall on screen */
screen_roff(r_ptr, l_ptr);
/* Hack -- Complete the prompt (again) */
Term_addstr(-1, TERM_WHITE, " [(r)ecall, ESC]");
}
/* Command */
query = inkey();
/* Unrecall */
if (recall)
{
/* Load screen */
screen_load();
}
/* Normal commands */
if (query.code != 'r') break;
/* Toggle recall */
recall = !recall;
}
/* Stop scanning */
if (query.code == ESCAPE) break;
/* Move to "prev" monster */
if (query.code == '-')
{
if (++i == n)
i = 0;
}
/* Move to "next" monster */
else
{
if (i-- == 0)
i = n - 1;
}
}
/* Button */
button_kill('r');
button_kill('-');
button_kill('+');
redraw_stuff(p_ptr);
/* Re-display the identity */
prt(buf, 0, 0);
/* Free the "who" array */
FREE(who);
}
/* Centers the map on the player */
void do_cmd_center_map(void)
{
center_panel();
}