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
|
/**
* @file
* @brief Showing the level map (X and background).
**/
#include "AppHdr.h"
#include "viewmap.h"
#include <algorithm>
#include "branch.h"
#include "colour.h"
#include "command.h"
#include "coord.h"
#include "coordit.h"
#include "dgn-overview.h"
#include "directn.h"
#include "env.h"
#include "files.h"
#include "format.h"
#include "fprop.h"
#include "libutil.h"
#include "macro.h"
#include "map-knowledge.h"
#include "message.h"
#include "options.h"
#include "output.h"
#include "showsymb.h"
#include "state.h"
#include "stringutil.h"
#include "terrain.h"
#include "tileview.h"
#include "tiles-build-specific.h"
#include "travel.h"
#include "ui.h"
#include "unicode.h"
#include "view.h"
#include "viewchar.h"
#include "viewgeom.h"
#ifdef USE_TILE
#endif
#ifndef USE_TILE_LOCAL
/**
* Get a console colour for representing the travel possibility at the given
* position based on the last travel update. Used when drawing the console map.
*
* @param p The position.
* @returns An unsigned int value for the colour.
*/
static unsigned _get_travel_colour(const coord_def& p)
{
#ifdef WIZARD
if (you.wizard && testbits(env.pgrid(p), FPROP_HIGHLIGHT))
return LIGHTGREEN;
#endif
if (is_waypoint(p))
return LIGHTGREEN;
if (is_stair_exclusion(p))
return Options.tc_excluded;
const unsigned no_travel_col
= feat_is_traversable(env.grid(p)) ? Options.tc_forbidden
: Options.tc_dangerous;
const short dist = travel_point_distance[p.x][p.y];
return dist > 0? Options.tc_reachable :
dist == PD_EXCLUDED ? Options.tc_excluded :
dist == PD_EXCLUDED_RADIUS ? Options.tc_exclude_circle :
dist < 0? no_travel_col :
Options.tc_disconnected;
}
#endif
bool travel_colour_override(const coord_def& p)
{
if (is_waypoint(p) || is_stair_exclusion(p)
|| travel_point_distance[p.x][p.y] == PD_EXCLUDED)
{
return true;
}
#ifdef WIZARD
if (you.wizard && testbits(env.pgrid(p), FPROP_HIGHLIGHT))
return true;
#endif
const map_cell& cell = env.map_knowledge(p);
show_class cls = get_cell_show_class(cell);
if (cls == SH_FEATURE)
{
switch (cell.feat())
{
case DNGN_FLOOR:
case DNGN_LAVA:
case DNGN_DEEP_WATER:
case DNGN_SHALLOW_WATER:
return true;
default:
return false;
}
}
else
return false;
}
static bool _is_player_defined_feature(char32_t feature)
{
return feature == 'E' || feature == 'F' || feature == 'W';
}
// Determines if the given feature is present at (x, y) in _feat_ coordinates.
// If you have map coords, add (1, 1) to get grid coords.
// Use one of
// 1. '<' and '>' to look for stairs
// 2. '\t' or '\\' for shops, portals.
// 3. '^' for traps
// 4. '_' for altars
// 5. Anything else will look for the exact same character in the level map.
bool is_feature(char32_t feature, const coord_def& where)
{
if (!env.map_knowledge(where).known() && !you.see_cell(where) && !_is_player_defined_feature(feature))
return false;
dungeon_feature_type grid = env.map_knowledge(where).feat();
switch (feature)
{
case 'E':
return is_exclude_root(where);
case 'F':
case 'W':
return is_waypoint(where);
case 'I':
return is_stash(where);
case '_':
return feat_is_altar(grid) || grid == DNGN_UNKNOWN_ALTAR;
case '\t':
case '\\':
return feat_is_gate(grid) || grid == DNGN_ENTER_SHOP
|| grid == DNGN_UNKNOWN_PORTAL;
case '<':
// DNGN_UNKNOWN_ALTAR doesn't need to be excluded here, because
// feat_is_altar doesn't include it in the first place.
return feat_stair_direction(grid) == CMD_GO_UPSTAIRS
&& !feat_is_altar(grid)
&& grid != DNGN_ENTER_SHOP
&& grid != DNGN_TRANSPORTER;
case '>':
return feat_stair_direction(grid) == CMD_GO_DOWNSTAIRS
&& !feat_is_altar(grid)
&& grid != DNGN_ENTER_SHOP
&& grid != DNGN_TRANSPORTER;
case '^':
return feat_is_trap(grid);
default:
return get_cell_glyph(where).ch == feature;
}
}
static bool _is_feature_fudged(char32_t glyph, const coord_def& where)
{
if (!env.map_knowledge(where).known() && !_is_player_defined_feature(glyph))
return false;
if (is_feature(glyph, where))
return true;
if (glyph == '<')
{
return env.grid(where) == DNGN_EXIT_ABYSS
|| env.grid(where) == DNGN_EXIT_PANDEMONIUM;
}
else if (glyph == '>')
{
return env.grid(where) == DNGN_TRANSIT_PANDEMONIUM
|| env.grid(where) == DNGN_TRANSPORTER;
}
return false;
}
vector<coord_def> search_path_around_point(coord_def centre)
{
vector<coord_def> points;
int cx = centre.x, cy = centre.y;
// Find the first occurrence of given glyph, spiralling around (x,y)
int maxradius = GXM > GYM ? GXM : GYM;
for (int radius = 1; radius < maxradius; ++radius)
for (int axis = -2; axis < 2; ++axis)
{
int rad = radius - (axis < 0);
for (int var = -rad; var <= rad; ++var)
{
int dx = radius, dy = var;
if (axis % 2)
dx = -dx;
if (axis < 0)
{
int temp = dx;
dx = dy;
dy = temp;
}
const auto x = cx + dx, y = cy + dy;
if (in_bounds(x, y))
points.emplace_back(x, y);
}
}
return points;
}
static int _find_feature(const vector<coord_def>& features,
char32_t feature,
int ignore_count,
coord_def &out_pos,
bool forward)
{
int firstx = -1, firsty = -1, firstmatch = -1;
int matchcount = 0;
for (coord_def coord : features)
{
if (_is_feature_fudged(feature, coord))
{
++matchcount;
if (forward? !ignore_count-- : --ignore_count == 1)
{
out_pos = coord;
// We want to cursor to (x,y)
return matchcount;
}
else if (!forward || firstx == -1)
{
firstx = coord.x;
firsty = coord.y;
firstmatch = matchcount;
}
}
}
// We found something, but ignored it because of an ignorecount
if (firstx != -1)
{
out_pos = coord_def(firstx, firsty);
return firstmatch;
}
return 0;
}
#ifndef USE_TILE_LOCAL
static int _get_number_of_lines_levelmap()
{
return get_number_of_lines() - 1;
}
static void _draw_level_map(int start_x, int start_y, bool travel_mode,
bool on_level, ui::Region region)
{
region.width = min(region.width, GXM);
region.height = min(region.height, GYM);
const coord_def extents(region.width, region.height);
crawl_view_buffer vbuf(extents);
screen_cell_t *cell = vbuf;
cursor_control cs(false);
for (int screen_y = 0; screen_y < region.height; screen_y++)
for (int screen_x = 0; screen_x < region.width; screen_x++)
{
coord_def c(start_x + screen_x, start_y + screen_y);
if (!map_bounds(c))
{
cell->colour = DARKGREY;
cell->glyph = 0;
}
else
{
cglyph_t g = get_cell_glyph(c, false, -1);
cell->glyph = g.ch;
cell->colour = g.col;
const show_class show = get_cell_show_class(env.map_knowledge(c));
if (show == SH_NOTHING && is_explore_horizon(c))
{
const feature_def& fd = get_feature_def(DNGN_EXPLORE_HORIZON);
cell->glyph = fd.symbol();
cell->colour = fd.colour();
}
if (travel_mode && travel_colour_override(c))
cell->colour = _get_travel_colour(c);
// If we've a waypoint on the current square,
// show the waypoint number.
const uint8_t waypoint_char = is_waypoint(c);
if (waypoint_char)
cell->glyph = waypoint_char;
if (c == you.pos() && !crawl_state.arena_suspended && on_level)
{
// [dshaligram] Draw the @ symbol on the
// level-map. It's no longer saved into the
// env.map_knowledge, so we need to draw it
// directly.
cell->colour = WHITE;
cell->glyph = mons_char(you.symbol);
}
if (Options.show_travel_trail && travel_trail_index(c) >= 0)
{
const feature_def& fd = get_feature_def(DNGN_TRAVEL_TRAIL);
// Don't overwrite the player's symbol
if (fd.symbol() && c != you.pos())
cell->glyph = fd.symbol();
if (fd.colour() != COLOUR_UNDEF)
cell->colour = fd.colour();
cell->colour |= COLFLAG_REVERSE;
}
}
cell++;
}
puttext(region.x + 1, region.y + 1, vbuf);
}
#endif // !USE_TILE_LOCAL
static void _reset_travel_colours(vector<coord_def> &features, bool on_level)
{
// We now need to redo travel colours.
features.clear();
if (on_level)
fill_travel_point_distance(you.pos(), &features);
else
{
travel_pathfind tp;
tp.set_feature_vector(&features);
tp.get_features();
}
}
// Sort glyphs within a group, for the feature list.
static bool _comp_glyphs(const cglyph_t& g1, const cglyph_t& g2)
{
return g1.ch < g2.ch || g1.ch == g2.ch && g1.col < g2.col;
}
#ifndef USE_TILE_LOCAL
static cglyph_t _get_feat_glyph(const coord_def& gc);
#endif
class feature_list
{
enum group
{
G_UP, G_DOWN, G_PORTAL, G_OTHER, G_NONE, NUM_GROUPS = G_NONE
};
vector<cglyph_t> data[NUM_GROUPS];
static group feat_dir(dungeon_feature_type feat)
{
switch (feat_stair_direction(feat))
{
case CMD_GO_UPSTAIRS:
return G_UP;
case CMD_GO_DOWNSTAIRS:
return G_DOWN;
default:
return G_NONE;
}
}
group get_group(const coord_def& gc)
{
dungeon_feature_type feat = env.map_knowledge(gc).feat();
if (feat_is_staircase(feat) || feat_is_escape_hatch(feat))
return feat_dir(feat);
if (feat == DNGN_TRAP_SHAFT)
return G_DOWN;
if (feat_is_altar(feat) || feat == DNGN_ENTER_SHOP)
return G_OTHER;
if (get_feature_dchar(feat) == DCHAR_ARCH)
return G_PORTAL;
return G_NONE;
}
void maybe_add(const coord_def& gc)
{
#ifndef USE_TILE_LOCAL
if (!env.map_knowledge(gc).known())
return;
group grp = get_group(gc);
if (grp != G_NONE)
data[grp].push_back(_get_feat_glyph(gc));
#else
UNUSED(gc);
#endif
}
public:
void init()
{
for (vector<cglyph_t> &groupdata : data)
groupdata.clear();
for (rectangle_iterator ri(0); ri; ++ri)
maybe_add(*ri);
for (vector<cglyph_t> &groupdata : data)
sort(begin(groupdata), end(groupdata), _comp_glyphs);
}
formatted_string format() const
{
formatted_string s;
for (const vector<cglyph_t> &groupdata : data)
for (cglyph_t gly : groupdata)
s.add_glyph(gly);
return s;
}
};
#ifndef USE_TILE_LOCAL
static void _draw_title(const coord_def& cpos, const feature_list& feats, const int columns)
{
const formatted_string help =
formatted_string::parse_string("(Press <w>?</w> for help)");
const int helplen = help.width();
if (columns < helplen)
return;
const formatted_string title = feats.format();
const int titlelen = title.width();
if (columns < titlelen)
return;
string pstr = "";
#ifdef WIZARD
if (you.wizard)
{
char buf[10];
snprintf(buf, sizeof(buf), " (%d, %d)", cpos.x, cpos.y);
pstr = string(buf);
}
#endif // WIZARD
cgotoxy(1, 1);
textcolour(WHITE);
cprintf("%s", chop_string(
uppercase_first(level_id::current().describe(true, true))
+ pstr, columns - helplen).c_str());
cgotoxy(max(1, (columns - titlelen) / 2), 1);
title.display();
textcolour(LIGHTGREY);
cgotoxy(max(1, columns - helplen + 1), 1);
help.display();
}
#endif
static level_pos _stair_dest(const coord_def& p, command_type dir)
{
if (!in_bounds(p))
return level_pos();
if (feat_stair_direction(env.map_knowledge(p).feat()) != dir)
return level_pos();
LevelInfo *linf = travel_cache.find_level_info(level_id::current());
if (!linf)
return level_pos();
const stair_info *sinf = linf->get_stair(p);
if (!sinf)
return level_pos();
return sinf->destination;
}
static void _unforget_map()
{
ASSERT(env.map_forgotten);
MapKnowledge &old(*env.map_forgotten);
for (rectangle_iterator ri(0); ri; ++ri)
if (!env.map_knowledge(*ri).seen() && old(*ri).seen())
{
// Don't overwrite known squares, nor magic-mapped with
// magic-mapped data -- what was forgotten is less up to date.
env.map_knowledge(*ri) = old(*ri);
env.map_seen.set(*ri);
#ifdef USE_TILE
tiles.update_minimap(*ri);
#endif
}
}
static void _forget_map(bool wizard_forget = false)
{
for (rectangle_iterator ri(0); ri; ++ri)
{
auto& flags = env.map_knowledge(*ri).flags;
// don't touch squares we can currently see
if (flags & MAP_VISIBLE_FLAG)
continue;
if (wizard_forget)
{
env.map_knowledge(*ri).clear();
#ifdef USE_TILE
tile_forget_map(*ri);
#endif
if (monster *m = monster_at(*ri))
m->flags &= ~(MF_WAS_IN_VIEW | MF_SEEN | MF_SENSED);
}
else if (flags & MAP_SEEN_FLAG)
{
// squares we've seen in the past, pretend we've mapped instead
flags |= MAP_MAGIC_MAPPED_FLAG;
flags &= ~MAP_SEEN_FLAG;
}
env.map_seen.set(*ri, false);
#ifdef USE_TILE
tiles.update_minimap(*ri);
#endif
}
}
map_control_state process_map_command(command_type cmd, const map_control_state &state);
static coord_def _recentre_map_target(const level_id level,
const level_id original)
{
if (level == original)
return you.pos();
const auto bounds = known_map_bounds();
return (bounds.first + bounds.second + 1) / 2;
}
#ifndef USE_TILE_LOCAL
static map_view_state _get_view_state(coord_def cur_ul, const map_control_state& state)
{
// TODO: why should local tiles use different logic?
// also, these might make more sense as member functions of UIMapView.
// recalculate cursor position, and scroll if necessary
map_view_state view;
const int num_lines = _get_number_of_lines_levelmap();
view.start.x = cur_ul.x;
view.start.y = cur_ul.y;
// Scroll when needed to keep MARGIN rows/cols to the left or right of the
// cursor -- as long as there is map to scroll to.
const int MARGIN = 3;
// careful with off-by-one errors in these calcs
view.cursor = state.lpos.pos - view.start + coord_def(1,1);
const auto bounds = known_map_bounds();
const coord_def map_min = state.lpos.pos - bounds.first;
const coord_def map_max = bounds.second - state.lpos.pos;
const coord_def ul_margin(max(0, min(MARGIN, map_min.x)),
max(0, min(MARGIN, map_min.y)));
const coord_def lr_margin(max(0, min(MARGIN, map_max.x)),
max(0, min(MARGIN, map_max.y)));
// First check upper-left margins
// In principle x doesn't need to scroll; a level can't be larger than 80
// columns. But let's make the logic general.
coord_def shift = coord_def(min(0, view.cursor.x - ul_margin.x - 1),
min(0, view.cursor.y - ul_margin.y - 1));
// If we didn't enforce any margins on the upper-left, check lower-right
if (shift.x >= 0)
shift.x = max(0, view.cursor.x - get_number_of_cols() + lr_margin.x);
if (shift.y >= 0)
shift.y = max(0, view.cursor.y - num_lines + lr_margin.y);
view.start += shift;
// now calculate the final cursor position relative to any view shifting
// that happened to enforce margins
view.cursor = state.lpos.pos - view.start + coord_def(1,1);
return view;
}
static map_view_state _init_view_state(const map_control_state& state)
{
// initial placement calculations for the map viewport window.
// This code is a bit inscrutable but it seems to do some heuristics to
// try to make partially explored levels look reasonable in console map
// view, mostly to do with placement in the x direction.
const int num_lines = _get_number_of_lines_levelmap();
const int half_screen = (num_lines - 1) / 2;
const auto bounds = known_map_bounds();
const auto min_x = bounds.first.x;
const auto min_y = bounds.first.y;
const auto max_x = bounds.second.x;
const auto max_y = bounds.second.y;
const auto map_lines = max_y - min_y + 1;
coord_def ul((min_x + max_x + 1) / 2 - 40, 0);
auto screen_y = state.lpos.pos.y;
// If close to top of known map, put min_y on top
// else if close to bottom of known map, put max_y on bottom.
//
// The num_lines comparisons are done to keep things neat, by
// keeping things at the top of the screen. By shifting an
// additional one in the num_lines > map_lines case, we can
// keep the top line clear... which makes things look a whole
// lot better for small maps.
if (num_lines > map_lines)
screen_y = min_y + half_screen - 1;
else if (num_lines == map_lines || screen_y - half_screen < min_y)
screen_y = min_y + half_screen;
else if (screen_y + half_screen > max_y)
screen_y = max_y - half_screen;
ul.y = screen_y - half_screen;
// Finally, adjust the upper left corner for the view state so that it is
// appropriately scrolled relative to the cursor.
return _get_view_state(ul, state);
}
#endif
class UIMapView : public ui::Widget
{
public:
UIMapView(level_pos& lp, levelview_excursion& le, bool travel_mode,
bool allow_offlevel)
: m_reentry(false)
{
m_state.lpos = lp;
m_state.features = &m_features;
m_state.feats = &m_feats;
m_state.excursion = ≤
m_state.allow_offlevel = allow_offlevel;
m_state.travel_mode = travel_mode;
m_state.original = level_id::current();
m_state.map_alive = true;
m_state.redraw_map = true;
m_state.search_anchor = coord_def(-1, -1);
m_state.chose = false;
m_state.on_level = true;
goto_level();
}
~UIMapView() {}
void _render() override
{
#ifdef USE_TILE
tiles.load_dungeon(m_state.lpos.pos);
#endif
#ifdef USE_TILE_LOCAL
display_message_window();
tiles.render_current_regions();
glmanager->reset_transform();
#endif
#ifndef USE_TILE_LOCAL
const auto view = _get_view_state(view_ul, m_state);
view_ul = view.start;
_draw_title(m_state.lpos.pos, *m_state.feats, m_region.width);
const ui::Region map_region = {0, 1, m_region.width, m_region.height - 1};
_draw_level_map(view_ul.x, view_ul.y, m_state.travel_mode,
m_state.on_level, map_region);
// the `+ 1` here is for the map overview line
ui::show_cursor_at(view.cursor.x, view.cursor.y + 1);
#endif
}
void _allocate_region() override
{
// Note: Tile versions just center on the current cursor
// location. It silently ignores everything else going
// on in this function. --Enne
#ifdef USE_TILE_LOCAL
if (first_run)
{
tiles.update_tabs();
first_run = false;
}
#endif
_expose();
}
bool on_event(const ui::Event& ev) override
{
if (ev.type() == ui::Event::Type::KeyDown)
{
auto key = static_cast<const ui::KeyEvent&>(ev).key();
#ifndef USE_TILE_LOCAL
key = unmangle_direction_keys(key, KMC_LEVELMAP);
#endif
command_type cmd = key_to_command(key, KMC_LEVELMAP);
if (cmd < CMD_MIN_OVERMAP || cmd > CMD_MAX_OVERMAP)
cmd = CMD_NO_CMD;
process_command(cmd);
if (m_state.redraw_map)
_expose();
return true;
}
#ifdef USE_TILE_LOCAL
if (ev.type() == ui::Event::Type::MouseMove
|| ev.type() == ui::Event::Type::MouseDown
|| ev.type() == ui::Event::Type::MouseUp)
{
auto wm_event = to_wm_event(static_cast<const ui::MouseEvent&>(ev));
int k = tiles.handle_mouse(wm_event);
// XX this really seems like it shouldn't be here, maybe should be
// in tilereg stuff?
// in any case, CK_MOUSE_CLICK *should* be what only tilereg-dgn.cc
// could returns, if the player clicked in the dungeon region.
if (k == CK_MOUSE_CLICK && ev.type() == ui::Event::Type::MouseDown)
{
if (tiles.get_cursor() == m_state.lpos.pos)
{
process_command(CMD_MAP_GOTO_TARGET);
return true;
}
else
{
m_state.lpos.pos
= tiles.get_cursor().clamped(known_map_bounds());
}
}
_expose();
return true;
}
#endif
return false;
}
void process_command(command_type cmd)
{
auto ret = process_map_command(cmd, m_state);
// reentry happens if during the process, something else called in to
// set_lpos. E.g. this can happen via a describe popup. If this
// happened, then it set m_state and we don't want to overwrite it.
// TODO some refactoring to make this cleaner
if (!check_and_reset_reentry())
m_state = std::move(ret);
if (!m_state.map_alive)
return;
if (m_state.lpos.id != level_id::current())
goto_level();
m_state.lpos.pos = m_state.lpos.pos.clamped(known_map_bounds());
}
void set_lpos(level_pos dest)
{
map_control_state state = m_state;
state.map_alive = true;
state.chose = false;
if (!dest.id.is_valid()
|| dest.id != level_id::current() && !state.allow_offlevel)
{
return;
}
los_changed();
state.lpos = dest;
m_state = state;
if (m_state.lpos.id != level_id::current())
goto_level();
m_state.lpos.pos = m_state.lpos.pos.clamped(known_map_bounds());
m_reentry = true;
}
void goto_level()
{
if (m_state.lpos.id != level_id::current())
m_state.excursion->go_to(m_state.lpos.id);
m_state.on_level = (level_id::current() == m_state.original);
// Vector to track all state.features we can travel to, in
// order of distance.
if (m_state.travel_mode)
_reset_travel_colours(*m_state.features, m_state.on_level);
m_state.feats->init();
m_state.search_index = 0;
m_state.search_anchor.reset();
// This happens when CMD_MAP_PREV_LEVEL etc. assign dest to lpos,
// with a position == (-1, -1).
if (!map_bounds(m_state.lpos.pos))
{
m_state.lpos.pos =
_recentre_map_target(m_state.lpos.id, m_state.original);
m_state.lpos.id = level_id::current();
}
#ifndef USE_TILE_LOCAL
auto s = _init_view_state(m_state);
view_ul = s.start;
#endif
_expose();
}
bool is_alive() const
{
return m_state.map_alive;
}
bool chose() const
{
return m_state.chose;
}
level_pos lpos() const
{
return m_state.lpos;
}
bool check_and_reset_reentry()
{
const bool ret = m_reentry;
m_reentry = false;
return ret;
}
#ifdef USE_TILE_LOCAL
// This function should not be called while a map command is processing
void start_far_viewing(coord_def viewed_location)
{
m_far_view = true;
update_far_viewing(viewed_location);
}
void stop_far_viewing()
{
m_far_view = false;
}
bool is_far_viewing()
{
return m_far_view;
}
// This function should not be called while a map command is processing
void update_far_viewing(coord_def viewed_location)
{
m_state.lpos.pos = viewed_location.clamped(known_map_bounds());
}
#endif
private:
map_control_state m_state;
// Vector to track all features we can travel to, in order of distance.
vector<coord_def> m_features;
// List of all interesting features for display in the (console) title.
feature_list m_feats;
bool m_reentry;
#ifndef USE_TILE_LOCAL
coord_def view_ul;
#endif
#ifdef USE_TILE_LOCAL
bool first_run = true;
bool m_far_view = false;
#endif
};
static shared_ptr<UIMapView> map_view = nullptr;
// show_map() now centers the known map along x or y. This prevents
// the player from getting "artificial" location clues by using the
// map to see how close to the end they are. They'll need to explore
// to get that. This function is still a mess, though. -- bwr
bool show_map(level_pos &lpos, bool travel_mode, bool allow_offlevel)
{
if (map_view)
{
ASSERT(map_view->is_alive());
// handle reentry -- just attempt to set the position. Ignore both
// the travel_mode and the allow_offlevel params -- inherit from the
// running map.
map_view->set_lpos(lpos);
return false;
}
else
{
#ifdef USE_TILE_LOCAL
mouse_control mc(MOUSE_MODE_NORMAL);
tiles.do_map_display();
#endif
#ifdef USE_TILE
ui::cutoff_point ui_cutoff_point;
#endif
#ifdef USE_TILE_WEB
tiles_ui_control ui(UI_VIEW_MAP);
#endif
if (!lpos.id.is_valid() || !allow_offlevel)
lpos.id = level_id::current();
levelview_excursion le(travel_mode);
map_view = make_shared<UIMapView>(lpos, le, travel_mode, allow_offlevel);
#ifdef USE_TILE_LOCAL
unwind_bool inhibit_rendering(ui::should_render_current_regions, false);
#else
cursor_control cc(!Options.use_fake_cursor);
#endif
ui::push_layout(map_view, KMC_LEVELMAP);
while (map_view->is_alive() && !crawl_state.seen_hups)
ui::pump_events();
ui::pop_layout();
#ifdef USE_TILE_LOCAL
tiles.set_map_display(false);
#endif
#ifdef USE_TILE
tiles.place_cursor(CURSOR_MAP, NO_CURSOR);
#endif
lpos = map_view->lpos();
const bool result = map_view->chose();
map_view = nullptr;
return result;
}
}
level_pos map_follow_stairs(bool up, const coord_def &pos)
{
level_pos dest = _stair_dest(pos, up ? CMD_GO_UPSTAIRS : CMD_GO_DOWNSTAIRS);
if (!dest.id.is_valid())
{
dest.id = up ? find_up_level(level_id::current())
: find_down_level(level_id::current());
dest.pos = coord_def(-1, -1);
}
if (dest.id.is_valid() && dest.id != level_id::current()
&& you.level_visited(dest.id))
{
return dest;
}
else
return level_pos();
}
void process_map_command(command_type cmd)
{
if (map_view)
map_view->process_command(cmd);
}
#ifdef USE_TILE_LOCAL
void start_far_viewing(coord_def viewed_location)
{
if (map_view)
map_view->start_far_viewing(viewed_location);
}
void stop_far_viewing()
{
if (map_view)
map_view->stop_far_viewing();
}
bool is_far_viewing()
{
return map_view && map_view->is_far_viewing();
}
void update_far_viewing(coord_def viewed_location)
{
if (map_view)
map_view->update_far_viewing(viewed_location);
}
#endif
map_control_state process_map_command(command_type cmd, const map_control_state& prev_state)
{
// the map needs a cursor, but we need to hide it here
cursor_control cc(false);
map_control_state state = prev_state;
state.map_alive = true;
state.chose = false;
const auto block_step = Options.level_map_cursor_step;
switch (cmd)
{
case CMD_MAP_HELP:
show_levelmap_help();
break;
case CMD_MAP_CLEAR_MAP:
clear_map_or_travel_trail();
break;
#ifdef WIZARD
case CMD_MAP_WIZARD_FORGET:
{
// this doesn't seem useful outside of debugging and may
// be buggy in unexpected ways, so wizmode-only. (Though
// it doesn't leak information or anything.)
if (!you.wizard)
break;
if (env.map_forgotten)
_unforget_map();
MapKnowledge *old = new MapKnowledge(env.map_knowledge);
// completely wipe out map
_forget_map(true);
env.map_forgotten.reset(old);
mpr("Level map wiped.");
break;
}
#endif
case CMD_MAP_FORGET:
{
// Merge it with already forgotten data first.
if (env.map_forgotten)
_unforget_map();
MapKnowledge *old = new MapKnowledge(env.map_knowledge);
_forget_map();
env.map_forgotten.reset(old);
mpr("Level map cleared.");
}
break;
case CMD_MAP_UNFORGET:
if (env.map_forgotten)
{
_unforget_map();
env.map_forgotten.reset();
mpr("Remembered map restored.");
}
else
mpr("No remembered map.");
break;
case CMD_MAP_ADD_WAYPOINT:
travel_cache.add_waypoint(state.lpos.pos.x, state.lpos.pos.y);
// We need to do this all over again so that the user can jump
// to the waypoint he just created.
_reset_travel_colours(*state.features, state.on_level);
state.feats->init();
break;
// Cycle the radius of an exclude.
case CMD_MAP_EXCLUDE_AREA:
if (!is_map_persistent())
break;
cycle_exclude_radius(state.lpos.pos);
_reset_travel_colours(*state.features, state.on_level);
state.feats->init();
break;
case CMD_MAP_CLEAR_EXCLUDES:
clear_excludes();
_reset_travel_colours(*state.features, state.on_level);
state.feats->init();
break;
case CMD_MAP_EXCLUDE_RADIUS:
set_exclude(state.lpos.pos, getchm() - '0');
_reset_travel_colours(*state.features, state.on_level);
state.feats->init();
break;
case CMD_MAP_MOVE_DOWN_LEFT:
state.lpos.pos += coord_def(-1, 1);
break;
case CMD_MAP_MOVE_DOWN:
state.lpos.pos += coord_def(0, 1);
break;
case CMD_MAP_MOVE_UP_RIGHT:
state.lpos.pos += coord_def(1, -1);
break;
case CMD_MAP_MOVE_UP:
state.lpos.pos += coord_def(0, -1);
break;
case CMD_MAP_MOVE_UP_LEFT:
state.lpos.pos += coord_def(-1, -1);
break;
case CMD_MAP_MOVE_LEFT:
state.lpos.pos += coord_def(-1, 0);
break;
case CMD_MAP_MOVE_DOWN_RIGHT:
state.lpos.pos += coord_def(1, 1);
break;
case CMD_MAP_MOVE_RIGHT:
state.lpos.pos += coord_def(1, 0);
break;
case CMD_MAP_PREV_LEVEL:
case CMD_MAP_NEXT_LEVEL:
{
if (!state.allow_offlevel)
break;
const level_pos dest = map_follow_stairs(
cmd == CMD_MAP_PREV_LEVEL, state.lpos.pos);
if (dest.id.is_valid())
{
state.lpos = dest;
los_changed();
}
break;
}
case CMD_MAP_GOTO_LEVEL:
if (!state.allow_offlevel)
break;
{
string name;
#ifdef USE_TILE_WEB
tiles_ui_control msgwin(UI_NORMAL);
#endif
const level_pos pos
= prompt_translevel_target(TPF_DEFAULT_OPTIONS, name);
if (pos.id.depth < 1
|| pos.id.depth > brdepth[pos.id.branch]
|| !you.level_visited(pos.id))
{
canned_msg(MSG_OK);
state.redraw_map = true;
break;
}
state.lpos = pos;
}
break;
case CMD_MAP_JUMP_DOWN_LEFT:
state.lpos.pos += coord_def(-block_step, block_step);
break;
case CMD_MAP_JUMP_DOWN:
state.lpos.pos += coord_def(0, block_step);
break;
case CMD_MAP_JUMP_UP_RIGHT:
state.lpos.pos += coord_def(block_step, -block_step);
break;
case CMD_MAP_JUMP_UP:
state.lpos.pos += coord_def(0, -block_step);
break;
case CMD_MAP_JUMP_UP_LEFT:
state.lpos.pos += coord_def(-block_step, -block_step);
break;
case CMD_MAP_JUMP_LEFT:
state.lpos.pos += coord_def(-block_step, 0);
break;
case CMD_MAP_JUMP_DOWN_RIGHT:
state.lpos.pos += coord_def(block_step, block_step);
break;
case CMD_MAP_JUMP_RIGHT:
state.lpos.pos += coord_def(block_step, 0);
break;
case CMD_MAP_SCROLL_DOWN:
state.lpos.pos += coord_def(0, 20);
break;
case CMD_MAP_SCROLL_UP:
state.lpos.pos += coord_def(0, -20);
break;
case CMD_MAP_FIND_YOU:
if (state.on_level)
state.lpos.pos += you.pos() - state.lpos.pos;
break;
#ifdef USE_TILE
case CMD_MAP_ZOOM_IN:
case CMD_MAP_ZOOM_OUT:
tiles.zoom_dungeon(cmd == CMD_MAP_ZOOM_IN);
break;
#endif
case CMD_MAP_FIND_UPSTAIR:
case CMD_MAP_FIND_DOWNSTAIR:
case CMD_MAP_FIND_PORTAL:
case CMD_MAP_FIND_TRAP:
case CMD_MAP_FIND_ALTAR:
case CMD_MAP_FIND_EXCLUDED:
case CMD_MAP_FIND_WAYPOINT:
case CMD_MAP_FIND_STASH:
case CMD_MAP_FIND_STASH_REVERSE:
{
bool forward = (cmd != CMD_MAP_FIND_STASH_REVERSE);
char32_t getty;
switch (cmd)
{
case CMD_MAP_FIND_UPSTAIR:
getty = '<';
break;
case CMD_MAP_FIND_DOWNSTAIR:
getty = '>';
break;
case CMD_MAP_FIND_PORTAL:
getty = '\t';
break;
case CMD_MAP_FIND_TRAP:
getty = '^';
break;
case CMD_MAP_FIND_ALTAR:
getty = '_';
break;
case CMD_MAP_FIND_EXCLUDED:
getty = 'E';
break;
case CMD_MAP_FIND_WAYPOINT:
getty = 'W';
break;
default:
case CMD_MAP_FIND_STASH:
case CMD_MAP_FIND_STASH_REVERSE:
getty = 'I';
break;
}
if (state.search_anchor.zero())
state.search_anchor = state.lpos.pos;
if (state.travel_mode && !_is_player_defined_feature(getty))
{
state.search_index = _find_feature(*state.features, getty,
state.search_index,
state.lpos.pos,
forward);
}
else
{
const auto search_path =
search_path_around_point(state.search_anchor);
state.search_index = _find_feature(*state.features, getty,
state.search_index,
state.lpos.pos,
true);
}
break;
}
case CMD_MAP_GOTO_TARGET:
if (state.travel_mode && state.on_level && state.lpos.pos == you.pos())
{
if (you.travel_x > 0 && you.travel_y > 0)
{
if (you.travel_z == level_id::current())
state.lpos.pos = coord_def(you.travel_x, you.travel_y);
else if (state.allow_offlevel && you.travel_z.is_valid()
&& can_travel_to(you.travel_z)
&& you.level_visited(you.travel_z))
{
// previous travel target is offlevel
state.lpos = level_pos(you.travel_z,
coord_def(you.travel_x, you.travel_y));
los_changed();
}
}
}
else
{
state.chose = true;
state.map_alive = false;
}
break;
case CMD_MAP_ANNOTATE_LEVEL:
state.excursion->go_to(state.original);
redraw_screen();
update_screen();
state.excursion->go_to(state.lpos.id);
if (!is_map_persistent())
mpr("You can't annotate this level.");
else
{
#ifdef USE_TILE_WEB
tiles_ui_control msgwin(UI_NORMAL);
#endif
annotate_level(state.lpos.id);
}
state.redraw_map = true;
break;
case CMD_MAP_EXPLORE:
if (state.on_level)
{
travel_pathfind tp;
tp.set_floodseed(you.pos(), true);
coord_def whereto = tp.pathfind(Options.explore_greedy
? RMODE_EXPLORE_GREEDY
: RMODE_EXPLORE);
_reset_travel_colours(*state.features, state.on_level);
if (!whereto.zero())
state.lpos.pos = whereto;
}
break;
#ifdef WIZARD
case CMD_MAP_WIZARD_TELEPORT:
if (!you.wizard || !state.on_level || !in_bounds(state.lpos.pos))
break;
if (cell_is_solid(state.lpos.pos))
you.wizmode_teleported_into_rock = true;
you.move_to(state.lpos.pos, MV_INTERNAL);
state.map_alive = false;
break;
#endif
case CMD_MAP_EXIT_MAP:
state.lpos = level_pos();
state.map_alive = false;
break;
#ifdef USE_TILE_LOCAL
case CMD_NEXT_CMD:
break; // allow mouse clicks to move cursor without leaving map mode
#endif
case CMD_MAP_DESCRIBE:
if (map_bounds(state.lpos.pos) && env.map_knowledge(state.lpos.pos).known())
{
if (full_describe_square(state.lpos.pos, false))
{
state.map_alive = false;
state.chose = false; // don't go to the location
}
// n.b. it's possible for the describe popup to trigger a reentrant
// call and overwrite state
state.redraw_map = true;
}
break;
default:
if (!state.travel_mode)
state.redraw_map = false;
break;
}
return state;
}
bool emphasise(const coord_def& where)
{
return is_unknown_stair(where) && !player_in_branch(BRANCH_VESTIBULE)
|| is_unknown_transporter(where);
}
#ifndef USE_TILE_LOCAL
// Get glyph for feature list; here because it's so similar
// to get_map_col.
// Except that that function doesn't exist...
static cglyph_t _get_feat_glyph(const coord_def& gc)
{
// XXX: it's unclear whether we want to display all features
// or just those not obscured by remembered/detected stuff.
dungeon_feature_type feat = env.map_knowledge(gc).feat();
const bool terrain_seen = env.map_knowledge(gc).seen();
const feature_def &fdef = get_feature_def(feat);
cglyph_t g;
g.ch = terrain_seen ? fdef.symbol() : fdef.magic_symbol();
unsigned col;
if (travel_colour_override(gc))
col = _get_travel_colour(gc);
else if (emphasise(gc))
{
g.ch = fdef.magic_symbol();
col = fdef.seen_em_colour();
}
else
col = fdef.seen_colour();
g.col = real_colour(col, gc);
return g;
}
#endif
|