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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
|
/**
* @file
* @brief Functions for inventory related commands.
**/
#include "AppHdr.h"
#include "invent.h"
#include <algorithm> // any_of
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <sstream>
#include "artefact.h"
#include "colour.h"
#include "command.h"
#include "describe.h"
#include "env.h"
#include "evoke.h"
#include "god-item.h"
#include "god-passive.h"
#include "initfile.h"
#include "item-prop.h"
#include "items.h"
#include "item-use.h"
#include "item-prop.h"
#include "item-status-flag-type.h"
#include "known-items.h"
#include "libutil.h"
#include "macro.h"
#include "message.h"
#include "options.h"
#include "output.h"
#include "prompt.h"
#include "religion.h"
#include "rltiles/tiledef-dngn.h"
#include "rltiles/tiledef-icons.h"
#include "rltiles/tiledef-main.h"
#include "showsymb.h"
#include "state.h"
#include "stringutil.h"
#include "tag-version.h"
#include "terrain.h"
#include "throw.h"
#include "tilepick.h"
#include "tile-env.h"
///////////////////////////////////////////////////////////////////////////////
// Inventory menu shenanigans
static void _get_inv_items_to_show(vector<const item_def*> &v,
int selector, int excluded_slot = -1);
InvTitle::InvTitle(Menu *mn, const string &title, invtitle_annotator tfn)
: MenuEntry(title, MEL_TITLE)
{
m = mn;
titlefn = tfn;
}
string InvTitle::get_text() const
{
return titlefn ? titlefn(m, MenuEntry::get_text())
: MenuEntry::get_text();
}
InvEntry::InvEntry(const item_def &i)
: MenuEntry("", MEL_ITEM), item(&i)
{
indent_no_hotkeys = true;
// This gets the inventory coloring rules to apply by default:
tag = "inventory";
// Data is an inherited void *. When using InvEntry in menus
// use the const item in this class whenever possible
data = const_cast<item_def *>(item);
if (in_inventory(i) && i.base_type != OBJ_GOLD)
{
// We need to do this in order to get the 'wielded' annotation.
// We then toss out the first four characters, which look
// like this: "a - ". Ow. FIXME.
text = i.name(DESC_INVENTORY_EQUIP, false).substr(4);
}
else
text = i.name(DESC_A, false);
if (i.base_type != OBJ_GOLD && in_inventory(i))
add_hotkey(i.slot);
else
add_hotkey(' '); // dummy hotkey
add_class_hotkeys(i);
quantity = i.quantity;
}
int InvEntry::highlight_colour(bool temp) const
{
return menu_colour(get_text(), item_prefix(*item, temp), tag, false);
}
const string &InvEntry::get_basename() const
{
if (basename.empty())
basename = item->name(DESC_BASENAME);
return basename;
}
const string &InvEntry::get_qualname() const
{
if (qualname.empty())
qualname = item->name(DESC_QUALNAME);
return qualname;
}
const string &InvEntry::get_fullname() const
{
return text;
}
const string &InvEntry::get_dbname() const
{
if (dbname.empty())
dbname = item->name(DESC_DBNAME);
return dbname;
}
bool InvEntry::is_cursed() const
{
return item->cursed();
}
bool InvEntry::is_glowing() const
{
return !item->is_identified()
&& (get_equip_desc(*item)
|| (is_artefact(*item)
&& (item->base_type == OBJ_WEAPONS
|| item->base_type == OBJ_ARMOUR
|| item->base_type == OBJ_BOOKS)));
}
bool InvEntry::is_ego() const
{
return item->is_identified() && !is_artefact(*item)
&& item->brand != 0
&& (item->base_type == OBJ_WEAPONS
|| item->base_type == OBJ_MISSILES
|| item->base_type == OBJ_ARMOUR);
}
bool InvEntry::is_art() const
{
return item->is_identified() && is_artefact(*item);
}
bool InvEntry::is_equipped() const
{
if (item->link == -1 || item->pos != ITEM_IN_INVENTORY)
return false;
return item_is_equipped(*item);
}
void InvEntry::select(int qty)
{
if (item && item->quantity < qty)
qty = item->quantity;
MenuEntry::select(qty);
}
string InvEntry::get_filter_text() const
{
return item_prefix(*item, false) + " " + get_text();
}
string InvEntry::_get_text_preface() const
{
ostringstream tstr;
const bool nosel = hotkeys_count() == 0;
if (nosel && tag != "pickup")
return MenuEntry::_get_text_preface();
const char key = nosel ? ' ' : static_cast<char>(hotkeys[0]);
tstr << ' ' << key << ' ';
if (nosel)
tstr << ' '; // pickup only
else if (!selected_qty)
tstr << '-';
else if (selected_qty < quantity)
tstr << '#';
else
tstr << '+';
tstr << ' ';
if (InvEntry::show_glyph)
tstr << "(" << glyph_to_tagstr(get_item_glyph(*item)) << ")" << " ";
if (InvEntry::show_coordinates && in_bounds(item->pos))
{
const coord_def relpos = item->pos - you.pos();
tstr << "(" << relpos.x << ", " << -relpos.y << ")" << " ";
}
return tstr.str();
}
void get_class_hotkeys(const int type, vector<char> &glyphs)
{
switch (type)
{
case OBJ_GOLD:
glyphs.push_back('$');
break;
case OBJ_MISSILES:
glyphs.push_back('(');
break;
case OBJ_WEAPONS:
glyphs.push_back(')');
break;
case OBJ_ARMOUR:
glyphs.push_back('[');
break;
case OBJ_WANDS:
glyphs.push_back('/');
break;
case OBJ_BOOKS:
glyphs.push_back(':');
break;
case OBJ_SCROLLS:
glyphs.push_back('?');
break;
case OBJ_JEWELLERY:
glyphs.push_back('"');
glyphs.push_back('=');
break;
case OBJ_POTIONS:
glyphs.push_back('!');
break;
case OBJ_STAVES:
glyphs.push_back('|');
break;
#if TAG_MAJOR_VERSION == 34
case OBJ_RODS:
glyphs.push_back('\\');
break;
#endif
case OBJ_TALISMANS:
glyphs.push_back('%');
break;
case OBJ_MISCELLANY:
glyphs.push_back('}');
break;
default:
break;
}
}
void InvEntry::add_class_hotkeys(const item_def &i)
{
const int type = i.base_type;
if (type == OBJ_JEWELLERY)
{
add_hotkey(i.sub_type >= AMU_FIRST_AMULET ? '"' : '=');
return;
}
vector<char> glyphs;
get_class_hotkeys(type, glyphs);
for (char gly : glyphs)
add_hotkey(gly);
}
bool InvEntry::show_glyph = false;
void InvEntry::set_show_glyph(bool doshow)
{
show_glyph = doshow;
}
bool InvEntry::show_coordinates = false;
void InvEntry::set_show_coordinates(bool doshow)
{
show_coordinates = doshow;
}
InvMenu::InvMenu(int mflags)
: Menu((mflags & MF_NOSELECT) ? mflags : (mflags | MF_ARROWS_SELECT),
"inventory"),
type(menu_type::invlist), pre_select(nullptr),
title_annotate(nullptr), cur_osel(0)
{
menu_action = ACT_EXAMINE; // default
if (!Options.single_column_item_menus)
set_flags(get_flags() | MF_USE_TWO_COLUMNS);
}
void InvMenu::set_type(menu_type t)
{
type = t;
menu_action = t == menu_type::describe ? ACT_EXAMINE : ACT_EXECUTE;
}
void InvMenu::set_title_annotator(invtitle_annotator afn)
{
title_annotate = afn;
}
void InvMenu::set_title(MenuEntry *t, bool first)
{
Menu::set_title(t, first);
}
void InvMenu::set_preselect(const vector<SelItem> *pre)
{
pre_select = pre;
}
string slot_description()
{
return make_stringf("%d/%d gear slots", inv_count(INVENT_GEAR), MAX_GEAR);
}
void InvMenu::set_title(const string &s)
{
if ((flags & MF_PAGED_INVENTORY))
{
string str;
switch (cur_osel)
{
case 0: str = "Gear: " + slot_description(); break;
case 1: str = "Potions: "; break;
case 2: str = "Scrolls: "; break;
case 3: str = "Evocable Items: "; break;
}
str += " (Left/Right to switch category)";
set_title(new InvTitle(this, str, title_annotate));
return;
}
set_title(new InvTitle(this, s.empty() ? "Inventory: " + slot_description()
: s,
title_annotate));
}
bool InvMenu::skip_process_command(int keyin)
{
switch (keyin)
{
case '?':
case '!':
// item type shortcuts
return true;
}
return Menu::skip_process_command(keyin);
}
int InvMenu::pre_process(int key)
{
if (key == ';'
&& you.last_unequip != -1
&& (type == menu_type::drop || type == menu_type::invlist))
{
key = index_to_letter(you.last_unequip);
}
return key;
}
const static int modes[] =
{
OSEL_GEAR,
OBJ_POTIONS,
OBJ_SCROLLS,
OSEL_EVOKABLE_ALL,
};
void InvMenu::cycle_page(int dir)
{
do
{
int new_osel = cur_osel;
new_osel += dir;
if (new_osel < 0)
new_osel = ARRAYSZ(modes) - 1;
if (new_osel >= static_cast<int>(ARRAYSZ(modes)))
new_osel = 0;
set_page(new_osel);
}
// If this page is empty, go to the next one.
// XXX: (Theoretically, this could cause an infinite loop, but other code
// should already prevent opening a menu when you have no items.)
while (items.empty());
}
void InvMenu::set_page(int page)
{
ASSERT(page >= 0 && page < static_cast<int>(ARRAYSZ(modes)));
if (!(flags & MF_PAGED_INVENTORY))
return;
const int old_osel = cur_osel;
cur_osel = page;
// Save selected items from our current page
get_selected(&sel);
offscreen_sel[old_osel] = get_selitems();
deselect_all();
// Clear old entries and load new ones based on the new page
clear();
load_inv_items(modes[cur_osel]);
update_more();
reset();
update_menu(true);
// If the player has selected items on this new page previously, restore
// those selections.
for (SelItem& sel_item : offscreen_sel[cur_osel])
{
for (size_t i = 0; i < items.size(); ++i)
{
InvEntry *inv = dynamic_cast<InvEntry*>(items[i]);
if (!inv)
continue;
if (inv->item->link == sel_item.item->link)
{
select_index(i, sel_item.quantity);
break;
}
}
}
get_selected(&sel);
update_title();
}
bool InvMenu::process_command(command_type cmd)
{
if (flags & MF_PAGED_INVENTORY)
{
if (cmd == CMD_MENU_ACCEPT_SELECTION)
{
get_selected(&sel);
return false;
}
else if (cmd == CMD_MENU_EXIT)
{
// Must clear offscreen selection or exiting the menu will still act
// upon those items.
for (size_t i = 0; i < ARRAYSZ(offscreen_sel); ++i)
offscreen_sel[i].clear();
sel.clear();
lastch = CK_ESCAPE; // XX is this correct?
return is_set(MF_UNCANCEL) && !crawl_state.seen_hups;
}
else if (cmd == CMD_MENU_LEFT)
{
cycle_page(-1);
return true;
}
else if (cmd == CMD_MENU_RIGHT)
{
cycle_page(1);
return true;
}
}
return Menu::process_command(cmd);
}
void InvMenu::select_index(int index, int qty)
{
// XXX: Apply drop_filter to all pages at once.
if (flags & MF_PAGED_INVENTORY && qty == MENU_SELECT_ALL)
{
int start_osel = cur_osel;
do
{
Menu::select_index(index, qty);
cycle_page(1);
} while (cur_osel != start_osel);
}
else
Menu::select_index(index, qty);
}
string InvMenu::get_select_count_string(int) const
{
if (flags & MF_PAGED_INVENTORY)
{
vector<SelItem> all_sel = get_selitems(true);
if (all_sel.empty())
return "";
return make_stringf(" %d item%s", (int)all_sel.size(),
all_sel.size() > 1 ? "s" : "");
}
return Menu::get_select_count_string(0);
}
bool InvMenu::examine_index(int i)
{
const bool do_actions = type == menu_type::describe;
// not entirely sure if the bounds check is necessary
auto ie = (i >= 0 && i < static_cast<int>(items.size()))
? dynamic_cast<InvEntry *>(items[i])
: nullptr;
// superclass behavior: do nothing unless on_examine is defined, in which
// case call on_examine.
if (!ie || on_examine)
return Menu::examine_index(i);
else if (type == menu_type::pickup)
{
// item is a floor item.
auto desc_tgt = const_cast<item_def*>(ie->item);
ASSERT(desc_tgt);
return describe_item(*desc_tgt, nullptr, do_actions);
}
else if (ie->hotkeys.size())
{
// default behavior: examine inv item. You must override or use on_examine
// if your items come from somewhere else, or this will cause crashes!
const int invidx = ie->item->link;
ASSERT(you.inv[invidx].defined());
return describe_item(you.inv[invidx], nullptr, do_actions);
}
// nothing to describe, ignore
return true;
}
static bool _has_temp_unwearable_armour()
{
for (const auto &item : you.inv)
{
if (item.defined() && item.base_type == OBJ_ARMOUR
&& can_equip_item(item) && !can_equip_item(item, true))
{
return true;
}
}
return false;
}
/**
* What message should the player be given when they look for items matching
* the given selector and don't find any?
*
* @param selector The given type of object_selector.
* @return A message such as "You aren't carrying any weapons."
* "Your armour is currently melded into you.", etc.
*/
string no_selectables_message(int item_selector)
{
switch (item_selector)
{
case OSEL_ANY:
return "You aren't carrying anything.";
case OSEL_WIELD:
case OBJ_WEAPONS:
return "You aren't carrying any weapons.";
case OSEL_BLESSABLE_WEAPON:
return "You aren't carrying any weapons that can be blessed.";
case OBJ_ARMOUR:
{
if (_has_temp_unwearable_armour())
return "You aren't carrying any currently wearable armour.";
else
return "You aren't carrying any wearable armour.";
}
case OSEL_UNIDENT:
return "You don't currently have any unidentified items.";
case OSEL_ENCHANTABLE_ARMOUR:
return "You aren't carrying any armour which can be enchanted further.";
case OBJ_CORPSES:
return "You don't have any corpses.";
case OBJ_POTIONS:
return "You aren't carrying any potions.";
case OBJ_SCROLLS:
return "You aren't carrying any scrolls.";
case OBJ_BOOKS:
return "You don't have any books.";
case OBJ_WANDS:
return "You aren't carrying any wands.";
case OBJ_JEWELLERY:
return "You aren't carrying any pieces of jewellery.";
case OSEL_AMULET:
return "You aren't carrying any amulets.";
case OSEL_JEWELLERY_OR_TALISMAN:
return "You aren't carrying any jewellery or talismans.";
case OSEL_LAUNCHING:
return "You aren't carrying any items that might be thrown or fired.";
case OSEL_EVOKABLE:
if (you.get_mutation_level(MUT_NO_ARTIFICE)) // iffy
return "You cannot evoke magical items.";
return "You aren't carrying any items that you can evoke.";
case OSEL_CURSED_WORN:
return "None of your equipped items are cursed.";
case OSEL_WORN_ARMOUR:
return "You aren't wearing any pieces of armour.";
case OSEL_WORN_JEWELLERY_OR_TALISMAN:
return "You aren't wearing any rings, amulets, or talismans.";
case OSEL_WORN_EQUIPABLE:
return "You aren't wearing anything.";
case OSEL_EQUIPABLE:
return "You aren't carrying anything that can be equipped.";
case OSEL_BRANDABLE_WEAPON:
return "You aren't carrying any weapons that can be branded.";
case OSEL_ENCHANTABLE_WEAPON:
return "You aren't carrying any weapons that can be enchanted.";
case OSEL_ARTEFACT_WEAPON:
return "You aren't carrying any artefact melee weapons.";
case OSEL_CURSABLE:
return "You aren't wearing any cursable items.";
case OSEL_UNCURSED_WORN_RINGS:
return "You aren't wearing any uncursed rings.";
case OSEL_QUIVER_ACTION:
return "You don't have any quiverable items.";
}
return "You aren't carrying any such object.";
}
void InvMenu::load_inv_items(int item_selector, int excluded_slot,
function<MenuEntry* (MenuEntry*)> procfn)
{
vector<const item_def *> tobeshown;
_get_inv_items_to_show(tobeshown, item_selector, excluded_slot);
load_items(tobeshown, procfn, 'a', true, true);
if (!item_count())
set_title(no_selectables_message(item_selector));
else
set_title("");
}
bool get_tiles_for_item(const item_def &item, vector<tile_def>& tileset, bool show_background)
{
tileidx_t idx = tileidx_item(item);
if (!idx)
return false;
if (in_inventory(item))
{
const equipment_slot eq = item_equip_slot(item);
if (eq != SLOT_UNUSED)
{
if (item.cursed())
tileset.emplace_back(TILE_ITEM_SLOT_EQUIP_CURSED);
else
tileset.emplace_back(TILE_ITEM_SLOT_EQUIP);
}
else if (item.cursed())
tileset.emplace_back(TILE_ITEM_SLOT_CURSED);
if (testbits(item.flags, ISFLAG_CHAOTIC))
tileset.emplace_back(TILE_MODIFIER_CHAOTIC);
tileidx_t base_item = tileidx_known_base_item(idx);
if (base_item)
tileset.emplace_back(base_item);
tileset.emplace_back(idx);
if (item_is_melded(item))
tileset.emplace_back(TILEI_MESH);
}
else
{
// Do we want to display the floor type or is that too distracting?
const coord_def c = item.held_by_monster()
? item.holding_monster()->pos()
: item.pos;
tileidx_t ch = 0;
if (c != coord_def() && show_background && item.link != ITEM_IN_SHOP)
{
ch = tileidx_feature(c);
tileset.emplace_back(ch);
}
tileidx_t base_item = tileidx_known_base_item(idx);
if (base_item)
tileset.emplace_back(base_item);
tileset.emplace_back(idx);
// Add overlays for parchments, based on the schools of their spell.
if (item.base_type == OBJ_BOOKS && item.sub_type == BOOK_PARCHMENT)
{
spell_type spell = static_cast<spell_type>(item.plus);
const tileidx_t school1 = tileidx_parchment_overlay(spell, 0);
const tileidx_t school2 = tileidx_parchment_overlay(spell, 1);
if (school1 > 0)
tileset.emplace_back(school1);
if (school2 > 0)
tileset.emplace_back(school2);
}
if (ch != 0)
{
// Needs to be displayed so as to not give away mimics in shallow water.
if (ch == TILE_DNGN_SHALLOW_WATER)
tileset.emplace_back(TILEI_MASK_SHALLOW_WATER);
else if (ch == TILE_DNGN_SHALLOW_WATER_MURKY)
tileset.emplace_back(TILEI_MASK_SHALLOW_WATER_MURKY);
}
}
if (item.base_type == OBJ_WEAPONS || item.base_type == OBJ_MISSILES
|| item.base_type == OBJ_ARMOUR || item.base_type == OBJ_STAVES
#if TAG_MAJOR_VERSION == 34
|| item.base_type == OBJ_RODS
#endif
)
{
tileidx_t brand = tileidx_known_brand(item);
if (brand)
tileset.emplace_back(brand);
}
return true;
}
#ifdef USE_TILE
bool InvEntry::get_tiles(vector<tile_def>& tileset) const
{
if (!Options.tile_menu_icons)
return false;
// Runes + gems + orb of zot have a special uncollected tile
if (quantity <= 0 && !item_is_collectible(*item))
return false;
return get_tiles_for_item(*item, tileset, show_background);
}
#else
bool InvEntry::get_tiles(vector<tile_def>& /*tileset*/) const { return false; }
#endif
bool InvMenu::is_selectable(int index) const
{
if (type == menu_type::drop)
{
InvEntry *item = dynamic_cast<InvEntry*>(items[index]);
if (item->is_cursed() && item->is_equipped())
return false;
string text = item->get_text();
if (text.find("!*") != string::npos || text.find("!d") != string::npos)
return false;
}
return Menu::is_selectable(index);
}
template <const string &(InvEntry::*method)() const>
static int compare_item_str(const InvEntry *a, const InvEntry *b)
{
return (a->*method)().compare((b->*method)());
}
// Would call this just compare_item, but MSVC mistakenly thinks the next
// one is a specialization rather than an overload.
template <typename T, T (*proc)(const InvEntry *a)>
static int compare_item_fn(const InvEntry *a, const InvEntry *b)
{
return int(proc(a)) - int(proc(b));
}
template <typename T, T (InvEntry::*method)() const>
static int compare_item(const InvEntry *a, const InvEntry *b)
{
return int((a->*method)()) - int((b->*method)());
}
template <typename T, T (InvEntry::*method)() const>
static int compare_item_rev(const InvEntry *a, const InvEntry *b)
{
return int((b->*method)()) - int((a->*method)());
}
template <item_sort_fn cmp>
static int compare_reverse(const InvEntry *a, const InvEntry *b)
{
return -cmp(a, b);
}
// We need C++11 already!
// Some prototypes to prevent warnings; we can't make these static because
// they're used as template parameters.
int sort_item_qty(const InvEntry *a);
int sort_item_slot(const InvEntry *a);
bool sort_item_identified(const InvEntry *a);
bool sort_item_charged(const InvEntry *a);
int sort_item_consumable_usefulness(const InvEntry *a);
int sort_item_qty(const InvEntry *a)
{
return a->quantity;
}
int sort_item_slot(const InvEntry *a)
{
return isalpha(a->item->slot) ? letter_to_index(a->item->slot) : 0;
}
bool sort_item_identified(const InvEntry *a)
{
return !a->item->is_identified();
}
bool sort_item_charged(const InvEntry *a)
{
return !is_xp_evoker(*a->item)
|| evoker_charges(a->item->sub_type) <= 0;
}
int sort_item_consumable_usefulness(const InvEntry *a)
{
if (inventory_category_for(*a->item) != INVENT_CONSUMABLE)
return 100;
if (is_useless_item(*a->item))
return 100;
if (is_emergency_item(*a->item))
return 0;
else if (is_good_item(*a->item))
return 1;
else if (is_dangerous_item(*a->item))
return 3;
else
return 2;
}
static bool _compare_invmenu_items(const InvEntry *a, const InvEntry *b,
const item_sort_comparators *cmps)
{
for (const auto &comparator : *cmps)
{
const int cmp = comparator.compare(a, b);
if (cmp)
return cmp < 0;
}
return a->item->link < b->item->link;
}
struct menu_entry_comparator
{
const menu_sort_condition *cond;
menu_entry_comparator(const menu_sort_condition *c)
: cond(c)
{
}
bool operator () (const MenuEntry* a, const MenuEntry* b) const
{
const InvEntry *ia = dynamic_cast<const InvEntry *>(a);
const InvEntry *ib = dynamic_cast<const InvEntry *>(b);
return _compare_invmenu_items(ia, ib, &cond->cmp);
}
};
void init_item_sort_comparators(item_sort_comparators &list, const string &set)
{
static struct
{
const string cname;
item_sort_fn cmp;
} cmp_map[] =
{
{ "basename", compare_item_str<&InvEntry::get_basename> },
{ "qualname", compare_item_str<&InvEntry::get_qualname> },
{ "fullname", compare_item_str<&InvEntry::get_fullname> },
{ "dbname", compare_item_str<&InvEntry::get_dbname> },
{ "curse", compare_item<bool, &InvEntry::is_cursed> },
{ "glowing", compare_item_rev<bool, &InvEntry::is_glowing> },
{ "ego", compare_item_rev<bool, &InvEntry::is_ego> },
{ "art", compare_item_rev<bool, &InvEntry::is_art> },
{ "equipped", compare_item_rev<bool, &InvEntry::is_equipped> },
{ "identified",compare_item_fn<bool, sort_item_identified> },
{ "charged", compare_item_fn<bool, sort_item_charged>},
{ "qty", compare_item_fn<int, sort_item_qty> },
{ "slot", compare_item_fn<int, sort_item_slot> },
{ "usefulness", compare_item_fn<int, sort_item_consumable_usefulness> },
};
list.clear();
for (string s : split_string(",", set))
{
if (s.empty())
continue;
const bool negated = s[0] == '>';
if (s[0] == '<' || s[0] == '>')
s = s.substr(1);
for (const auto &ci : cmp_map)
if (ci.cname == s)
{
list.emplace_back(ci.cmp, negated);
break;
}
}
if (list.empty())
list.emplace_back(compare_item_str<&InvEntry::get_fullname>);
}
const menu_sort_condition *InvMenu::find_menu_sort_condition() const
{
for (int i = Options.sort_menus.size() - 1; i >= 0; --i)
if (Options.sort_menus[i].matches(type))
return &Options.sort_menus[i];
return nullptr;
}
void InvMenu::sort_menu(vector<InvEntry*> &invitems,
const menu_sort_condition *cond)
{
if (!cond || cond->sort == -1 || (int) invitems.size() < cond->sort)
return;
sort(invitems.begin(), invitems.end(), menu_entry_comparator(cond));
}
FixedVector<int, NUM_OBJECT_CLASSES> inv_order(
OBJ_WEAPONS,
OBJ_MISSILES,
OBJ_ARMOUR,
OBJ_STAVES,
OBJ_GIZMOS,
#if TAG_MAJOR_VERSION == 34
OBJ_RODS,
#endif
OBJ_JEWELLERY,
OBJ_TALISMANS,
OBJ_WANDS,
OBJ_SCROLLS,
OBJ_POTIONS,
OBJ_MISCELLANY,
OBJ_BAUBLES,
#if TAG_MAJOR_VERSION == 34
OBJ_FOOD,
#endif
// These five can't actually be in your inventory.
OBJ_CORPSES,
OBJ_BOOKS,
OBJ_RUNES,
OBJ_GEMS,
OBJ_ORBS,
OBJ_GOLD);
menu_letter InvMenu::load_items(const vector<item_def>& mitems,
function<MenuEntry* (MenuEntry*)> procfn,
menu_letter ckey, bool sort, bool subkeys)
{
vector<const item_def*> xlatitems;
for (const item_def &item : mitems)
xlatitems.push_back(&item);
return load_items(xlatitems, procfn, ckey, sort, subkeys);
}
menu_letter InvMenu::load_items(const vector<const item_def*> &mitems,
function<MenuEntry* (MenuEntry*)> procfn,
menu_letter ckey, bool sort, bool subkeys)
{
subkeys |= is_set(MF_MULTISELECT); // XXX Can the caller do this?
FixedVector< int, NUM_OBJECT_CLASSES > inv_class(0);
for (const item_def * const mitem : mitems)
inv_class[mitem->base_type]++;
vector<InvEntry*> items_in_class;
const menu_sort_condition *cond = nullptr;
if (sort)
cond = find_menu_sort_condition();
string select_all;
if (subkeys)
{
// Mention the class selection shortcuts.
if (is_set(MF_SECONDARY_SCROLL))
select_all = "go to first";
else if (is_set(MF_MULTISELECT))
select_all = "select all";
else
select_all = "select first";
}
for (int obj = 0; obj < NUM_OBJECT_CLASSES; ++obj)
{
int i = inv_order[obj];
if (!inv_class[i])
continue;
string subtitle = item_class_name(i);
if (subkeys)
{
vector<char> glyphs;
get_class_hotkeys(i, glyphs);
if (!glyphs.empty())
{
// longest string
const string str = "Magical Staves ";
subtitle += string(strwidth(str) - strwidth(subtitle),
' ');
subtitle += "("+select_all+" with <w>";
for (char gly : glyphs)
subtitle += gly;
subtitle += "</w><blue>)";
}
}
add_entry(new MenuEntry(subtitle, MEL_SUBTITLE));
items_in_class.clear();
InvEntry *forced_first = nullptr;
for (const item_def * const mitem : mitems)
{
if (mitem->base_type != i)
continue;
InvEntry * const ie = new InvEntry(*mitem);
if (!subkeys)
ie->hotkeys.resize(1);
if (mitem->sub_type == get_max_subtype(mitem->base_type))
forced_first = ie;
else
items_in_class.push_back(ie);
}
sort_menu(items_in_class, cond);
if (forced_first)
items_in_class.insert(items_in_class.begin(),forced_first);
for (InvEntry *ie : items_in_class)
{
if (tag == "pickup")
{
if (ie->item && item_is_stationary(*ie->item))
ie->tag = "nopickup";
else
ie->tag = "pickup";
}
if (get_flags() & MF_NOSELECT)
ie->hotkeys.clear();
// If there's no hotkey, provide one.
else if (ie->hotkeys[0] == ' ')
{
if (ie->tag == "nopickup")
ie->hotkeys.clear();
else
ie->hotkeys[0] = ckey++;
}
do_preselect(ie);
add_entry(procfn ? procfn(ie) : ie);
}
}
return ckey;
}
void InvMenu::do_preselect(InvEntry *ie)
{
if (!pre_select || pre_select->empty())
return;
for (const SelItem &presel : *pre_select)
if (ie->item && ie->item == presel.item)
{
ie->selected_qty = presel.quantity;
break;
}
}
vector<SelItem> InvMenu::get_selitems(bool include_offscreen) const
{
vector<SelItem> selected_items;
for (MenuEntry *me : sel)
{
InvEntry *inv = dynamic_cast<InvEntry*>(me);
selected_items.emplace_back(inv->item->link, inv->selected_qty, inv->item);
}
if (include_offscreen)
{
for (int i = 0; i < static_cast<int>(ARRAYSZ(offscreen_sel)); ++i)
{
if (cur_osel == i)
continue;
selected_items.insert(selected_items.end(),
offscreen_sel[i].begin(),
offscreen_sel[i].end());
}
}
return selected_items;
}
string InvMenu::help_key() const
{
return type == menu_type::drop || type == menu_type::pickup ? "pick-up"
: "";
}
int InvMenu::getkey() const
{
auto mkey = lastch;
if (is_set(MF_ARROWS_SELECT) && mkey == CK_ENTER
|| mkey == CK_MOUSE_B1)
{
return mkey;
}
if (type == menu_type::know && mkey == 0) // ??
return mkey;
// this is sort of a mess. It seems to be converting a lot of keys to ' '
// so that invprompt_flag::escape_only can work right, but it almost
// certainly has other effects. Needless to say, it makes modifying key
// handling in specific menus pretty annoying, but I don't dare touch it
// right now.
if (!isaalnum(mkey) && mkey != '$' && mkey != '-' && mkey != '?'
&& mkey != '*' && !key_is_escape(mkey) && mkey != '\\'
&& mkey != ',')
{
mkey = ' ';
}
return mkey;
}
//////////////////////////////////////////////////////////////////////////////
bool in_inventory(const item_def &i)
{
return i.pos == ITEM_IN_INVENTORY;
}
const char *item_class_name(int type, bool terse)
{
if (terse)
{
switch (type)
{
case OBJ_STAVES: return "magical staff";
case OBJ_MISCELLANY: return "misc";
default: return base_type_string((object_class_type) type);
}
}
else
{
switch (type)
{
case OBJ_GOLD: return "Gold";
case OBJ_WEAPONS: return "Hand Weapons";
case OBJ_MISSILES: return "Missiles";
case OBJ_ARMOUR: return "Armour";
case OBJ_WANDS: return "Wands";
#if TAG_MAJOR_VERSION == 34
case OBJ_FOOD: return "Comestibles";
#endif
case OBJ_SCROLLS: return "Scrolls";
case OBJ_JEWELLERY: return "Jewellery";
case OBJ_POTIONS: return "Potions";
case OBJ_BOOKS: return "Books";
case OBJ_STAVES: return "Magical Staves";
#if TAG_MAJOR_VERSION == 34
case OBJ_RODS: return "Rods";
#endif
case OBJ_ORBS: return "Orbs of Power";
case OBJ_MISCELLANY: return "Miscellaneous";
case OBJ_CORPSES: return "Carrion";
case OBJ_RUNES: return "Runes of Zot";
case OBJ_GEMS: return "Ancient Gems";
case OBJ_TALISMANS: return "Talismans";
case OBJ_GIZMOS: return "Gizmo";
case OBJ_BAUBLES: return "Baubles";
}
}
return "";
}
const char* equip_slot_name(equipment_slot type, bool terse)
{
switch (type)
{
case SLOT_WEAPON: return "Weapon";
case SLOT_CLOAK: return "Cloak";
case SLOT_HELMET: return "Helmet";
case SLOT_GLOVES: return "Gloves";
case SLOT_BOOTS: return "Boots";
case SLOT_WEAPON_OR_OFFHAND:
case SLOT_OFFHAND: return "Offhand";
case SLOT_BODY_ARMOUR: return terse ? "Armour" : "Body Armour";
case SLOT_BARDING: return "Barding";
case SLOT_RING: return "Ring";
case SLOT_AMULET: return "Amulet";
case SLOT_GIZMO: return "Gizmo";
case SLOT_HAUNTED_AUX: return "Armour";
default: return "";
}
}
vector<SelItem> select_items(const vector<const item_def*> &items,
const char *title, bool noselect,
menu_type mtype)
{
vector<SelItem> selected;
if (!items.empty())
{
InvMenu menu;
menu.set_type(mtype);
menu.set_title(title);
if (mtype == menu_type::pickup)
menu.set_tag("pickup");
menu.load_items(items);
int new_flags = noselect ? MF_NOSELECT
: MF_MULTISELECT | MF_ALLOW_FILTER;
if (mtype == menu_type::sel_one)
{
new_flags |= MF_SINGLESELECT;
new_flags &= ~MF_MULTISELECT;
}
if (!!(new_flags & MF_MULTISELECT))
new_flags |= MF_SELECT_QTY;
new_flags |= MF_ALLOW_FORMATTING | MF_ARROWS_SELECT;
if (!Options.single_column_item_menus)
new_flags |= menu.get_flags() & MF_USE_TWO_COLUMNS;
menu.set_flags(new_flags);
menu.show();
selected = menu.get_selitems(true);
}
return selected;
}
bool item_is_selected(const item_def &i, int selector)
{
const object_class_type itype = i.base_type;
if (selector == OSEL_ANY || selector == itype
&& itype != OBJ_ARMOUR)
{
return true;
}
switch (selector)
{
case OBJ_ARMOUR:
return itype == OBJ_ARMOUR && can_equip_item(i, true);
case OSEL_WORN_ARMOUR:
return itype == OBJ_ARMOUR && item_is_equipped(i);
case OSEL_UNIDENT:
return !i.is_identified() && itype != OBJ_BOOKS;
case OBJ_MISSILES:
return itype == OBJ_MISSILES || itype == OBJ_WEAPONS;
case OSEL_LAUNCHING:
return itype == OBJ_MISSILES && is_throwable(&you, i)
|| itype == OBJ_WEAPONS && is_range_weapon(i)
&& item_is_equipped(i);
case OBJ_WEAPONS:
case OSEL_WIELD:
return item_is_wieldable(i);
case OSEL_ARTEFACT_WEAPON:
return is_melee_weapon(i) && is_artefact(i);
case OSEL_EVOKABLE:
return item_ever_evokable(i);
case OSEL_ENCHANTABLE_ARMOUR:
return is_enchantable_armour(i, true);
case OSEL_CURSED_WORN:
return i.cursed() && item_is_equipped(i);
case OSEL_BRANDABLE_WEAPON:
return is_brandable_weapon(i, true);
case OSEL_ENCHANTABLE_WEAPON:
return is_enchantable_weapon(i, true);
case OSEL_BLESSABLE_WEAPON:
return is_brandable_weapon(i, you_worship(GOD_SHINING_ONE)
|| you_worship(GOD_KIKUBAAQUDGHA), true);
case OSEL_CURSABLE:
return item_is_equipped(i) && item_is_cursable(i);
case OSEL_UNCURSED_WORN_RINGS:
return !i.cursed() && item_is_equipped(i) && itype == OBJ_JEWELLERY
&& !jewellery_is_amulet(i);
case OSEL_QUIVER_ACTION:
if (in_inventory(i))
{
auto a = quiver::slot_to_action(i.link);
// lots of things can be activated via the quiver, but don't have
// a targeter -- ignore these.
// However, we do want to allow selecting ammo/launchers under
// confusion...
// XX should the primary weapon be allowed here?
return a && a->is_valid()
&& (a->is_targeted()
|| you.confused() && item_is_selected(i, OSEL_LAUNCHING));
}
return false;
case OSEL_WORN_JEWELLERY_OR_TALISMAN:
return item_is_equipped(i) && item_is_selected(i, OSEL_JEWELLERY_OR_TALISMAN);
case OSEL_AMULET:
return itype == OBJ_JEWELLERY && jewellery_is_amulet(i);
case OSEL_WORN_EQUIPABLE:
if (!item_is_equipped(i))
return false;
// fallthrough
case OSEL_EQUIPABLE:
return item_is_selected(i, OBJ_ARMOUR)
|| item_is_selected(i, OSEL_WIELD)
|| item_is_selected(i, OBJ_JEWELLERY)
|| item_is_selected(i, OBJ_TALISMANS);
case OSEL_MARKED_ITEMS:
return i.flags & ISFLAG_MARKED_FOR_MENU;
case OSEL_JEWELLERY_OR_TALISMAN:
return i.base_type == OBJ_JEWELLERY || i.base_type == OBJ_TALISMANS;
case OSEL_GEAR:
return inventory_category_for(i) == INVENT_GEAR
|| i.base_type == OBJ_MISSILES;
case OSEL_EVOKABLE_ALL:
return i.base_type == OBJ_WANDS
|| i.base_type == OBJ_MISCELLANY
|| i.base_type == OBJ_BAUBLES;
default:
return false;
}
}
static void _get_inv_items_to_show(vector<const item_def*> &v,
int selector, int excluded_slot)
{
for (const auto &item : you.inv)
{
if (item.defined()
&& item.link != excluded_slot
&& item_is_selected(item, selector))
{
v.push_back(&item);
}
}
}
/**
* Does the player have any items of the given type?
*
* @param selector A object_selector.
* @param excluded_slot An item slot to ignore.
* @param inspect_floor If true, also check the floor where the player is
* standing.
*
* @return Whether there are any items matching the given
* selector in the player's inventory.
*/
bool any_items_of_type(int selector, int excluded_slot, bool inspect_floor)
{
bool ret = any_of(begin(you.inv), end(you.inv),
[=] (const item_def &item) -> bool
{
return item.defined() && item.link != excluded_slot
&& item_is_selected(item, selector);
});
if (!ret && inspect_floor)
{
auto item_floor = item_list_on_square(you.visible_igrd(you.pos()));
ret = any_of(begin(item_floor), end(item_floor),
[=] (const item_def* item) -> bool
{
return item->defined()
&& item_is_selected(*item, selector);
});
}
return ret;
}
// Use title = nullptr for stock Inventory title
// type = menu_type::drop allows the multidrop toggle
static int _invent_select(const char *title = nullptr,
menu_type type = menu_type::invlist,
int item_selector = OSEL_ANY,
int excluded_slot = -1,
int flags = MF_NOSELECT,
invtitle_annotator titlefn = nullptr,
vector<SelItem> *items = nullptr,
vector<text_pattern> *filter = nullptr,
Menu::selitem_tfn selitemfn = nullptr,
const vector<SelItem> *pre_select = nullptr)
{
InvMenu menu(flags | MF_ALLOW_FORMATTING | MF_INIT_HOVER);
menu.set_preselect(pre_select);
menu.set_title_annotator(titlefn);
menu.f_selitem = selitemfn;
if (filter)
menu.set_select_filter(*filter);
menu.load_inv_items(item_selector, excluded_slot);
menu.set_type(type);
// Don't override title if there are no items.
if (title && menu.item_count())
menu.set_title(title);
// Cycle through all pages to properly apply pre-selections immediately.
if (flags & MF_PAGED_INVENTORY)
{
for (int i = 0; i < 4; ++i)
menu.set_page(i);
// And then jump to the first non-empty page.
menu.cycle_page(1);
}
menu.show(true);
if (items)
*items = menu.get_selitems(true);
return menu.getkey();
}
void display_inventory()
{
if (inv_count() < 1)
{
canned_msg(MSG_NOTHING_CARRIED);
return;
}
int flags = MF_SINGLESELECT | MF_ALLOW_FORMATTING | MF_SECONDARY_SCROLL;
if (Options.show_paged_inventory)
flags |= MF_PAGED_INVENTORY;
else
flags |= MF_SELECT_BY_CATEGORY;
InvMenu menu(flags);
menu.load_inv_items(Options.show_paged_inventory ? OSEL_GEAR : OSEL_ANY, -1);
menu.set_type(menu_type::describe);
// Jump to the first non-empty page.
if ((flags & MF_PAGED_INVENTORY) && menu.item_count(false) == 0)
menu.cycle_page(1);
menu.show(true);
if (!crawl_state.doing_prev_cmd_again)
{
redraw_screen();
update_screen();
}
}
static string _drop_menu_titlefn(const Menu*, const string &)
{
return "Drop what? (Left/Right to switch category) " + slot_description() + " (_ for help)";
}
/**
* Prompt the player to select zero or more items to drop.
* TODO: deduplicate/merge with prompt_invent_item().
*
* @param Items already selected to drop.
* @return The total set of items the player's chosen to drop.
*/
vector<SelItem> prompt_drop_items(const vector<SelItem> &preselected_items)
{
vector<SelItem> items;
// multi-select some items to drop
_invent_select("",
menu_type::drop,
OSEL_GEAR,
-1,
MF_MULTISELECT | MF_ALLOW_FILTER | MF_SELECT_QTY | MF_PAGED_INVENTORY,
_drop_menu_titlefn,
&items,
&Options.drop_filter,
nullptr,
&preselected_items);
return items;
}
static bool item_matches_digit_inscription(item_def &item, char digit, operation_types oper)
{
const string& r(item.inscription);
const char iletter = static_cast<char>(oper);
for (unsigned int j = 0; j + 2 < r.size(); ++j)
if (r[j] == '@' && (r[j+1] == iletter || r[j+1] == '*') && r[j+2] == digit)
return true;
return false;
}
item_def *digit_inscription_to_item(char digit, operation_types oper)
{
for (int i = 0; i < ENDOFPACK; ++i)
if (you.inv[i].defined()
&& item_matches_digit_inscription(you.inv[i], digit, oper))
{
return &you.inv[i];
}
for (stack_iterator si(you.pos(), true); si; ++si)
if (item_matches_digit_inscription(*si, digit, oper))
return &*si;
return nullptr;
}
operation_types generalize_oper(operation_types oper)
{
switch (oper)
{
case OPER_EQUIP:
case OPER_WIELD:
case OPER_WEAR:
case OPER_PUTON:
return OPER_EQUIP;
case OPER_UNEQUIP:
case OPER_REMOVE:
case OPER_TAKEOFF:
return OPER_UNEQUIP;
default:
return OPER_NONE;
}
}
static bool _has_warning_inscription(const item_def& item,
operation_types oper)
{
const char iletter = static_cast<char>(oper);
const string& r(item.inscription);
for (unsigned int i = 0; i + 1 < r.size(); ++i)
{
if (r[i] == '!')
{
if (r[i+1] == iletter || r[i+1] == '*')
return true;
else if (oper == OPER_ZAP && r[i+1] == 'z') // for the 0.3.4. keys
return true;
else if (oper == OPER_EVOKE
&& (r[i+1] == 'V' || toalower(r[i+1]) == 'z'))
{
return true;
}
else if (oper == OPER_UNEQUIP)
{
if (item.base_type == OBJ_JEWELLERY && r[i+1] == 'R')
return true;
else if (item.base_type == OBJ_ARMOUR && r[i+1] == 'T')
return true;
else if (is_weapon(item) && r[i+i] == 'w')
return true;
}
else if (oper == OPER_EQUIP)
{
if (item.base_type == OBJ_JEWELLERY && r[i+1] == 'P')
return true;
else if (item.base_type == OBJ_ARMOUR && r[i+1] == 'W')
return true;
else if (is_weapon(item) && r[i+i] == 'w')
return true;
}
}
}
return false;
}
// Returns whether the player either confirmed the removal, or did not need to
// be prompted.
bool maybe_warn_about_removing(const item_def& item)
{
string prompt;
bool penance = false;
if (!needs_handle_warning(item, OPER_UNEQUIP, penance))
return true;
if (item.base_type == OBJ_WEAPONS || item.base_type == OBJ_STAVES)
prompt += "Really unwield ";
else if (item.base_type == OBJ_ARMOUR)
prompt += "Really take off ";
else
prompt += "Really remove ";
// now ask
if (item.cursed())
prompt += "and destroy ";
prompt += item.name(DESC_INVENTORY);
prompt += "?";
if (penance)
prompt += " This could place you under penance!";
return yesno(prompt.c_str(), false, 'n');
}
static string _operation_verb(operation_types oper)
{
switch (oper)
{
case OPER_WIELD: return "wield";
case OPER_QUAFF: return "quaff";
case OPER_DROP: return "drop";
case OPER_TAKEOFF: return "take off";
case OPER_WEAR: return "wear";
case OPER_PUTON: return "put on";
case OPER_REMOVE: return "remove";
case OPER_READ: return "read";
case OPER_MEMORISE: return "memorise from";
case OPER_ZAP: return "zap";
case OPER_FIRE: return "fire";
case OPER_EVOKE: return "evoke";
case OPER_QUIVER: return "quiver";
case OPER_EQUIP: return "equip";
case OPER_UNEQUIP: return "unequip";
case OPER_ANY:
default:
return "choose";
}
}
static bool _is_known_no_tele_item(const item_def &item)
{
if (!item.is_identified() || !is_artefact(item))
return false;
return artefact_property(item, ARTP_PREVENT_TELEPORTATION);
}
bool needs_notele_warning(const item_def &item, operation_types oper)
{
return (oper == OPER_EQUIP)
&& (_is_known_no_tele_item(item) && you.duration[DUR_TELEPORT]);
}
bool needs_handle_warning(const item_def &item, operation_types oper,
bool &penance, bool check_inscriptions)
{
if (check_inscriptions && _has_warning_inscription(item, oper))
return true;
// Curses first. Warn if something would take off (i.e. destroy) the cursed item.
if (item.cursed()
&& (oper == OPER_WIELD && is_weapon(item)
|| oper == OPER_PUTON || oper == OPER_WEAR
|| oper == OPER_TAKEOFF || oper == OPER_REMOVE))
{
return true;
}
if ((oper == OPER_EVOKE || oper == OPER_PUTON)
&& god_hates_item(item))
{
penance = true;
return true;
}
if (needs_notele_warning(item, oper))
return true;
if (oper == OPER_ATTACK && god_hates_item(item))
{
penance = true;
return true;
}
if ((oper == OPER_EQUIP || oper == OPER_UNEQUIP))
{
if (item.is_type(OBJ_JEWELLERY, AMU_FAITH)
&& faith_has_penalty())
{
return true;
}
if (is_weapon(item) && get_weapon_brand(item) == SPWPN_DISTORTION
&& !have_passive(passive_t::safe_distortion))
{
return true;
}
if (is_artefact(item) && (artefact_property(item, ARTP_CONTAM)
|| artefact_property(item, ARTP_DRAIN)
|| artefact_property(item, ARTP_FRAGILE)
|| artefact_property(item, ARTP_BANE)))
{
return true;
}
}
return false;
}
// If there are warning inscriptions associated with performing this operation
// on this item, prompt the user for confirmation. Return true if all prompts
// are OK'd.
bool check_warning_inscriptions(const item_def& item,
operation_types oper)
{
bool penance = false;
if (item.defined()
&& needs_handle_warning(item, oper, penance))
{
if (oper == OPER_UNEQUIP)
{
// Don't ask if it will fail anyway.
if (item.cursed())
return true;
}
// XXX: duplicates a check in delay.cc:_finish_delay()
string prompt = "Really " + _operation_verb(oper) + " ";
prompt += (in_inventory(item) ? item.name(DESC_INVENTORY)
: item.name(DESC_A));
if (needs_notele_warning(item, oper))
prompt += " while about to teleport";
prompt += "?";
if (god_despises_item(item, you.religion))
prompt += " You'd be excommunicated if you did!";
else if (penance)
prompt += " This could place you under penance!";
return yesno(prompt.c_str(), false, 'n');
}
return true;
}
/**
* Prompts the user for an item.
*
* Prompts the user for an item, and returns the inventory slot to the caller
* (which if must_exist is true (the default) will be an assigned item), with
* a positive quantity.
*
*
* @param prompt The question to ask the user.
* @param mtype The menu type.
* @param type_expect The object_class_type or object_selector for
* items to be listed.
* @param oper The operation_type that will be used on the result.
* Modifies some checks, including applicability of
* warning inscriptions.
* @param flags See comments on invent_prompt_flags.
* @param other_valid_char A character that, if not '\0', will cause
* PROMPT_GOT_SPECIAL to be returned when pressed.
*
* @return the inventory slot of an item or one of the following special values
* - PROMPT_ABORT: if the player hits escape.
* - PROMPT_GOT_SPECIAL: if the player hits the "other_valid_char".
* - PROMPT_NOTHING: if there are no matching items.
*/
int prompt_invent_item(const char *prompt,
menu_type mtype, int type_expect,
operation_types oper,
invent_prompt_flags flags,
const char other_valid_char)
{
const bool do_warning = !(flags & invprompt_flag::no_warning);
const bool allow_list_known = !(flags & invprompt_flag::hide_known);
const bool must_exist = !(flags & invprompt_flag::unthings_ok);
const bool auto_list = !(flags & invprompt_flag::manual_list);
const bool allow_easy_quit = !(flags & invprompt_flag::escape_only);
if (!any_items_of_type(type_expect) && type_expect != OSEL_WIELD
&& type_expect != OSEL_QUIVER_ACTION)
{
mprf(MSGCH_PROMPT, "%s",
no_selectables_message(type_expect).c_str());
return PROMPT_NOTHING;
}
int keyin = 0;
int ret = PROMPT_ABORT;
bool need_redraw = false;
bool need_prompt = true;
bool need_getch = true;
if (auto_list)
{
need_prompt = false;
need_getch = false;
keyin = '?';
}
// ugh, why is this done manually
while (true)
{
if (need_redraw && !crawl_state.doing_prev_cmd_again)
{
redraw_screen();
clear_messages();
}
if (need_prompt)
mprf(MSGCH_PROMPT, "%s (<w>?</w> for menu, <w>Esc</w> to quit)", prompt);
else
flush_prev_message();
if (need_getch)
keyin = get_ch();
need_redraw = false;
need_prompt = true;
need_getch = true;
// Note: We handle any "special" character first, so that
// it can be used to override the others.
if (other_valid_char != 0 && keyin == other_valid_char)
{
ret = PROMPT_GOT_SPECIAL;
break;
}
// Item chosen by menu.
item_def* chosen = nullptr;
if (keyin == '?' || keyin == '*')
{
// The "view inventory listing" mode.
vector< SelItem > items;
const auto last_keyin = keyin;
int mflags = MF_SINGLESELECT | MF_ANYPRINTABLE | MF_SECONDARY_SCROLL;
if (other_valid_char == '-')
mflags |= MF_SPECIAL_MINUS;
while (true)
{
keyin = _invent_select(prompt,
mtype, type_expect, -1, mflags, nullptr, &items);
if (allow_list_known && keyin == '\\')
{
check_item_knowledge();
continue;
}
break;
}
// a continue at this point has need_prompt = need_getch = true
// let `?` toggle the menu altogether. If this is a non-autolist,
// return to the prompt, otherwise we will pass escape to the
// abort handling below. (TODO: is this the right behavior?)
// TODO: some versions of _invent_select, such as wield, never
// return '?'. Is this a problem?
if (keyin == '?' || key_is_escape(keyin) && !auto_list)
continue;
else if (keyin == '*')
{
// let `*` act as a toggle. This is a slightly wacky
// implementation in that '?' as a toggle does something
// entirely different...
// need_prompt = view_all_prompt;
need_prompt = need_getch = false;
if (last_keyin == '*')
keyin = '?';
else
keyin = '*';
continue;
}
else if ((keyin == CK_ENTER || keyin == CK_MOUSE_B1) && items.size() > 0)
{
// hacky, but lets the inscription checks below trip
// TODO: this code should not rely on keyin, it breaks cmd
// bindings
keyin = items[0].item->slot;
}
else if (other_valid_char != 0 && keyin == other_valid_char)
{
// need to handle overrides...ugly code duplication
ret = PROMPT_GOT_SPECIAL;
break;
}
// Mark the item chosen by the menu, one way or the other.
if (items.size() > 0)
chosen = &you.inv[items[0].item->link];
}
if (isadigit(keyin))
{
// scan for our item
item_def *item = digit_inscription_to_item(keyin, oper);
if (item && in_inventory(*item))
{
ret = item->link;
if (!do_warning || check_warning_inscriptions(*item, oper))
break;
}
}
else if (key_is_escape(keyin) || allow_easy_quit && keyin == ' ')
{
ret = PROMPT_ABORT;
break;
}
else if (allow_list_known && keyin == '\\')
{
check_item_knowledge();
keyin = '?';
need_getch = false;
}
else if (isaalpha(keyin))
{
if (chosen)
ret = chosen->link;
else
ret = letter_to_index(keyin);
if (must_exist && !you.inv[ret].defined())
mpr("You don't have any such object.");
else if (must_exist && !item_is_selected(you.inv[ret], type_expect))
mpr("That's the wrong kind of item!");
else if (!do_warning || check_warning_inscriptions(you.inv[ret], oper))
break;
}
else if (keyin == ';')
{
ret = you.last_unequip;
break;
}
else if (!isspace(keyin))
{
// We've got a character we don't understand...
canned_msg(MSG_HUH);
}
else
{
// We're going to loop back up, so don't draw another prompt.
need_prompt = false;
}
}
return ret;
}
bool prompt_failed(int retval)
{
if (retval != PROMPT_ABORT && retval != PROMPT_NOTHING)
return false;
if (retval == PROMPT_ABORT)
canned_msg(MSG_OK);
crawl_state.cancel_cmd_repeat();
return true;
}
bool item_is_wieldable(const item_def &item)
{
return is_weapon(item) && !you.has_mutation(MUT_NO_GRASPING);
}
/**
* What xp-charged evocable items is the player currently devoting XP to, if
* any?
*
* @param[out] evokers A vector, to be filled with a list of the elemental
* evokers that the player is currently charging.
* (Only one of a type is charged at a time.)
*/
void list_charging_evokers(FixedVector<item_def*, NUM_MISCELLANY> &evokers)
{
for (auto &item : you.inv)
{
// can't charge non-evokers, or evokers that are full
if (!is_xp_evoker(item) || evoker_debt(item.sub_type) == 0)
continue;
evokers[item.sub_type] = &item;
}
}
void identify_inventory()
{
for (auto &item : you.inv)
if (item.defined())
identify_item(item);
}
|