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
|
/**
* @file
* @brief Code related to travel exclusions.
**/
#include "AppHdr.h"
#include "exclude.h"
#include <algorithm>
#include <sstream>
#include "cloud.h"
#include "coord.h"
#include "coordit.h"
#include "dgn-overview.h"
#include "english.h"
#include "env.h"
#include "hints.h"
#include "libutil.h"
#include "mon-util.h"
#include "options.h"
#include "player-notices.h"
#include "stringutil.h"
#include "tags.h"
#include "terrain.h"
#include "tiles-build-specific.h"
#include "travel.h"
#include "view.h"
// defined in dgn-overview.cc
extern set<pair<string, level_id> > auto_unique_annotations;
static bool _mon_needs_auto_exclude(const monster* mon, bool sleepy = false)
{
// These include the base monster's name in their name, but we don't
// want things in the auto_exclude option to match them.
if (mons_class_is_remnant(mon->type)
|| mons_class_is_test(mon->type)) // don't autoexclude test statues/spawners
{
return false;
}
// Please don't autoexclude your friends. It'll hurt their feelings.
if (mon->wont_attack())
return false;
if (mon->is_stationary())
return !sleepy;
// Auto exclusion only makes sense if the monster is still asleep.
return mon->asleep();
}
// Check whether a given monster is listed in the auto_exclude option.
static bool _need_auto_exclude(const monster* mon, bool sleepy = false)
{
// This only works if the name is lowercased.
string name = mon->name(DESC_BASENAME, mon->is_stationary()
&& testbits(mon->flags, MF_SEEN));
lowercase(name);
for (const text_pattern &pat : Options.auto_exclude)
{
if (pat.matches(name)
&& _mon_needs_auto_exclude(mon, sleepy)
&& (mon->attitude == ATT_HOSTILE))
{
return true;
}
}
return false;
}
// Nightstalker reduces LOS, so reducing the maximum exclusion radius
// only makes sense. This is only possible because it's a permanent
// mutation; other sources of LOS reduction should not have this effect.
// Similarly, Barachim's LOS increase.
static int _get_full_exclusion_radius()
{
// XXX: dedup with update_vision_range()!
return LOS_DEFAULT_RANGE - you.get_mutation_level(MUT_NIGHTSTALKER)
+ (you.species == SP_BARACHI ? 1 : 0);
}
/**
* Adds auto-exclusions for any monsters in LOS that need them.
*/
void add_auto_excludes()
{
if (!is_map_persistent() || !map_bounds(you.pos()))
return;
vector<monster*> mons;
for (radius_iterator ri(you.pos(), LOS_DEFAULT); ri; ++ri)
{
monster *mon = monster_at(*ri);
if (!mon || mon->is_summoned())
continue;
// Something of a speed hack, but some vaults have a TON of plants.
if (mon->is_firewood())
continue;
if (_need_auto_exclude(mon) && !is_exclude_root(*ri))
{
int radius = _get_full_exclusion_radius();
// Sting and Harpoon Shot's minimum ranges, respectively.
if (mon->type == MONS_SCRUB_NETTLE)
radius = min(radius, 4);
else if (mon->type == MONS_STARFLOWER)
radius = min(radius, 6);
set_exclude(*ri, radius, true);
mons.emplace_back(mon);
}
}
if (mons.empty())
return;
mprf(MSGCH_WARN, "Marking area around %s as unsafe for travelling.",
describe_monsters_condensed(mons).c_str());
learned_something_new(HINT_AUTO_EXCLUSION);
}
travel_exclude::travel_exclude(const coord_def &p, int r,
bool autoexcl, string dsc, bool vaultexcl)
: pos(p), radius(r),
uptodate(false), autoex(autoexcl), desc(dsc), vault(vaultexcl)
{
const monster_info *mi = env.map_knowledge(p).monsterinfo();
if (mi)
{
// Don't exclude past glass for stationary monsters.
if (mi->is_stationary())
los = los_def(p, opc_fully_no_trans, circle_def(r, C_SQUARE));
else
los = los_def(p, opc_excl, circle_def(r, C_SQUARE));
}
else
los = los_def(p, opc_excl, circle_def(r, C_SQUARE));
set_los();
}
// For exclude_map[p] = foo;
travel_exclude::travel_exclude()
: pos(-1, -1), radius(-1),
los(coord_def(-1, -1), opc_excl, circle_def(-1, C_SQUARE)),
uptodate(false), autoex(false), desc(""), vault(false)
{
}
extern exclude_set curr_excludes; // in travel.cc
void travel_exclude::set_los()
{
uptodate = true;
if (radius > 1)
{
// Radius might have been changed, and this is cheap.
los.set_bounds(circle_def(radius, C_SQUARE));
los.update();
}
}
bool travel_exclude::affects(const coord_def& p) const
{
if (!uptodate)
{
mprf(MSGCH_ERROR, "exclusion not up-to-date: e (%d,%d) p (%d,%d)",
pos.x, pos.y, p.x, p.y);
}
if (radius == 0)
return p == pos;
else if (radius == 1)
return (p - pos).rdist() <= 1;
else
return los.see_cell(p);
}
bool travel_exclude::in_bounds(const coord_def &p) const
{
return radius == 0 && p == pos
|| radius == 1 && (p - pos).rdist() <= 1
|| los.in_bounds(p);
}
/////////////////////////////////////////////////////////////////////////
exclude_set::exclude_set()
{
}
void exclude_set::clear()
{
exclude_roots.clear();
exclude_points.clear();
}
void exclude_set::erase(const coord_def &p)
{
auto it = exclude_roots.find(p);
if (it == exclude_roots.end())
return;
travel_exclude old_ex = it->second;
exclude_roots.erase(it);
recompute_excluded_points();
}
void exclude_set::add_exclude(travel_exclude &ex)
{
add_exclude_points(ex);
exclude_roots[ex.pos] = ex;
}
void exclude_set::add_exclude(const coord_def &p, int radius,
bool autoexcl, string desc,
bool vaultexcl)
{
travel_exclude ex(p, radius, autoexcl, desc, vaultexcl);
add_exclude(ex);
}
void exclude_set::add_exclude_points(travel_exclude& ex)
{
if (ex.radius == 0)
{
exclude_points.insert(ex.pos);
return;
}
if (!ex.uptodate)
ex.set_los();
else
ex.los.update();
for (radius_iterator ri(ex.pos, ex.radius, C_SQUARE); ri; ++ri)
if (ex.affects(*ri))
exclude_points.insert(*ri);
}
void exclude_set::update_excluded_points(bool recompute_los)
{
for (iterator it = exclude_roots.begin(); it != exclude_roots.end(); ++it)
{
travel_exclude &ex = it->second;
if (!ex.uptodate)
{
recompute_excluded_points(recompute_los);
return;
}
}
}
void exclude_set::recompute_excluded_points(bool recompute_los)
{
exclude_points.clear();
for (iterator it = exclude_roots.begin(); it != exclude_roots.end(); ++it)
{
travel_exclude &ex = it->second;
if (recompute_los)
ex.set_los();
add_exclude_points(ex);
}
}
bool exclude_set::is_excluded(const coord_def &p) const
{
return exclude_points.count(p);
}
bool exclude_set::is_exclude_root(const coord_def &p) const
{
return exclude_roots.count(p);
}
travel_exclude* exclude_set::get_exclude_root(const coord_def &p)
{
return map_find(exclude_roots, p);
}
size_t exclude_set::size() const
{
return exclude_roots.size();
}
bool exclude_set::empty() const
{
return exclude_roots.empty();
}
exclude_set::const_iterator exclude_set::begin() const
{
return exclude_roots.begin();
}
exclude_set::const_iterator exclude_set::end() const
{
return exclude_roots.end();
}
exclude_set::iterator exclude_set::begin()
{
return exclude_roots.begin();
}
exclude_set::iterator exclude_set::end()
{
return exclude_roots.end();
}
/////////////////////////////////////////////////////////////////////////
static void _mark_excludes_non_updated(const coord_def &p)
{
for (auto &entry : curr_excludes)
{
travel_exclude &ex = entry.second;
ex.uptodate = ex.uptodate && !ex.in_bounds(p);
}
}
void init_exclusion_los()
{
curr_excludes.recompute_excluded_points(true);
}
static bool _is_gate_that_spreads_exclusion(coord_def c,
dungeon_feature_type type)
{
if (!map_bounds(c))
return false;
if (env.grid(c) != type)
return false;
travel_exclude* exc = curr_excludes.get_exclude_root(c);
if (!exc || exc->radius != 0)
return false;
string prop = env.markers.property_at(c, MAT_ANY, "connected_exclude");
if (!prop.empty())
return false;
return true;
}
static void _exclude_gate(const coord_def& p, bool del = false)
{
set<coord_def> all_doors;
find_connected_identical(p, all_doors, true);
for (const auto& dc : all_doors)
{
if (del)
del_exclude(dc);
else
set_exclude(dc, 0);
}
}
static void _update_gate_exclusions(coord_def c)
{
const dungeon_feature_type feat = env.map_knowledge(c).feat();
if (!feat_is_door(feat))
return;
travel_exclude* exc = curr_excludes.get_exclude_root(c);
if (exc)
return;
string prop = env.markers.property_at(c, MAT_ANY, "connected_exclude");
if (!prop.empty())
return;
if (_is_gate_that_spreads_exclusion(c + coord_def(1, 0), feat)
|| _is_gate_that_spreads_exclusion(c + coord_def(-1, 0), feat)
|| _is_gate_that_spreads_exclusion(c + coord_def(0, 1), feat)
|| _is_gate_that_spreads_exclusion(c + coord_def(0, -1), feat))
{
_exclude_gate(c);
}
}
/*
* Update exclusions' LOS to reflect changes within their range.
* "changed" is a list of coordinates that have been changed.
* Only exclusions that might have one of the changed points
* in view are updated.
*/
void update_exclusion_los(vector<coord_def> changed)
{
if (changed.empty())
return;
for (coord_def c : changed)
{
_mark_excludes_non_updated(c);
_update_gate_exclusions(c);
}
curr_excludes.update_excluded_points(true);
}
bool is_excluded(const coord_def &p, const exclude_set &exc)
{
return exc.is_excluded(p);
}
bool is_exclude_root(const coord_def &p)
{
return curr_excludes.get_exclude_root(p);
}
int get_exclusion_radius(const coord_def &p)
{
if (travel_exclude *exc = curr_excludes.get_exclude_root(p))
{
if (!exc->radius)
return 1;
else
return exc->radius;
}
return 0;
}
string get_exclusion_desc(const coord_def &p)
{
if (travel_exclude *exc = curr_excludes.get_exclude_root(p))
return exc->desc;
return "";
}
#ifdef USE_TILE
// update Gmap for squares surrounding exclude centre
static void _tile_exclude_gmap_update(const coord_def &p)
{
for (int x = -8; x <= 8; x++)
for (int y = -8; y <= 8; y++)
{
const coord_def pc(p.x + x, p.y + y);
tiles.update_minimap(pc);
}
}
#endif
static void _exclude_update()
{
set_level_exclusion_annotation(curr_excludes.get_exclusion_desc());
travel_cache.update_excludes();
}
static void _exclude_update(const coord_def &p)
{
#ifdef USE_TILE
_tile_exclude_gmap_update(p);
#else
UNUSED(p);
#endif
_exclude_update();
}
// Catch up exclude updates from set_exclude with defer_updates=true.
//
// Warning: For tiles, this assumes all changed exclude centres
// are still there, so this won't work as is for
// del_exclude.
void deferred_exclude_update()
{
_exclude_update();
#ifdef USE_TILE
for (const auto &entry : curr_excludes)
_tile_exclude_gmap_update(entry.second.pos);
#endif
}
void clear_excludes()
{
#ifdef USE_TILE
// Tiles needs to update the minimap for each exclusion that is removed,
// but the exclusions need to be removed first. Therefore, make a copy
// of the current set of exclusions and iterate through the copy below.
exclude_set excludes = curr_excludes;
#endif
curr_excludes.clear();
clear_level_exclusion_annotation();
#ifdef USE_TILE
for (const auto &entry : excludes)
_tile_exclude_gmap_update(entry.second.pos);
#endif
_exclude_update();
}
// Cycles the radius of an exclusion, including "off" state;
// may start at 0 < radius < LOS_RADIUS, but won't cycle there.
void cycle_exclude_radius(const coord_def &p)
{
if (travel_exclude *exc = curr_excludes.get_exclude_root(p))
{
if (feat_is_door(env.grid(p)) && env.map_knowledge(p).known())
{
_exclude_gate(p, exc->radius == 0);
return;
}
if (exc->radius > 0)
set_exclude(p, 0);
else
del_exclude(p);
}
else
set_exclude(p, _get_full_exclusion_radius());
}
// Remove a possible exclude.
void del_exclude(const coord_def &p)
{
curr_excludes.erase(p);
_exclude_update(p);
}
// Set or update an exclude.
void set_exclude(const coord_def &p, int radius, bool autoexcl, bool vaultexcl,
bool defer_updates)
{
if (!is_map_persistent())
return;
if (!in_bounds(p))
return;
if (travel_exclude *exc = curr_excludes.get_exclude_root(p))
{
if (exc->desc.empty() && defer_updates)
{
if (cloud_struct* cloud = cloud_at(p))
exc->desc = cloud->cloud_name(true) + " cloud";
}
else if (exc->radius == radius)
return;
exc->radius = radius;
exc->uptodate = false;
curr_excludes.recompute_excluded_points();
}
else
{
string desc = "";
if (!defer_updates)
{
// Don't list a monster in the exclusion annotation if the
// exclusion was triggered by e.g. the flamethrowers' lua check.
const map_cell& cell = env.map_knowledge(p);
if (cell.monster() != MONS_NO_MONSTER)
{
desc = mons_type_name(cell.monster(), DESC_PLAIN);
if (cell.detected_monster())
desc += " (detected)";
}
else
{
const dungeon_feature_type feat = cell.feat();
desc = feat_type_name(feat);
}
}
else if (cloud_struct* cloud = cloud_at(p))
desc = cloud->cloud_name(true) + " cloud";
curr_excludes.add_exclude(p, radius, autoexcl, desc, vaultexcl);
}
if (!defer_updates)
_exclude_update(p);
}
// If a cell that was placed automatically no longer contains the original
// monster (or it is invisible), or if the player is no longer vulnerable to a
// damaging cloud, then remove the exclusion.
void maybe_remove_autoexclusion(const coord_def &p)
{
if (travel_exclude *exc = curr_excludes.get_exclude_root(p))
{
if (!exc->autoex)
return;
const monster* m = monster_at(p);
// We don't want to remove excluded clouds, check exc desc
// XXX: This conditional is a mess.
string desc = exc->desc;
bool cloudy_exc = ends_with(desc, "cloud");
if ((!m || !you.can_see(*m)
|| !_need_auto_exclude(m)
|| mons_type_name(m->type, DESC_PLAIN) != desc)
&& !cloudy_exc)
{
del_exclude(p);
}
else if (cloudy_exc && you.cloud_immune())
del_exclude(p);
}
}
// Lists all exclusions on the current level.
string exclude_set::get_exclusion_desc()
{
vector<string> desc;
int count_other = 0;
for (auto &entry : exclude_roots)
{
travel_exclude &ex = entry.second;
// Don't count cloud exclusions.
if (strstr(ex.desc.c_str(), "cloud"))
continue;
// Don't duplicate if there's already an annotation from unique monsters.
auto ma = auto_unique_annotations.find(make_pair(ex.desc,
level_id::current()));
if (ma != auto_unique_annotations.end())
continue;
if (!ex.desc.empty())
desc.push_back(ex.desc);
else
count_other++;
}
if (desc.size() > 1)
{
// Combine identical descriptions.
sort(desc.begin(), desc.end());
vector<string> help = desc;
desc.clear();
string old_desc = "";
int count = 1;
for (unsigned int i = 0; i < help.size(); ++i)
{
string tmp = help[i];
if (i == 0)
old_desc = tmp;
else
{
if (strcmp(tmp.c_str(), old_desc.c_str()) == 0)
count++;
else
{
if (count == 1)
desc.push_back(old_desc);
else
{
desc.push_back(make_stringf("%d %s",
count, pluralise(old_desc).c_str()));
count = 1;
}
old_desc = tmp;
}
}
}
if (count == 1)
desc.push_back(old_desc);
else
{
desc.push_back(make_stringf("%d %s",
count, pluralise(old_desc).c_str()));
}
}
if (count_other > 0)
{
desc.push_back(make_stringf("%d %sexclusion%s",
count_other, desc.empty() ? "" : "more ",
count_other > 1 ? "s" : ""));
}
else if (desc.empty())
return "";
string desc_str = "";
if (desc.size() > 1 || count_other == 0)
{
desc_str += "exclusion";
if (desc.size() > 1)
desc_str += "s";
desc_str += ": ";
}
return desc_str + comma_separated_line(desc.begin(), desc.end(),
" and ", ", ");
}
void marshallExcludes(writer& outf, const exclude_set& excludes)
{
marshallShort(outf, excludes.size());
if (excludes.size())
{
for (const auto &entry : excludes)
{
const travel_exclude &ex = entry.second;
marshallCoord(outf, ex.pos);
marshallShort(outf, ex.radius);
marshallBoolean(outf, ex.autoex);
marshallString(outf, ex.desc);
// XXX: marshall travel_exclude::vault?
}
}
}
void unmarshallExcludes(reader& inf, int minorVersion, exclude_set &excludes)
{
UNUSED(minorVersion);
excludes.clear();
int nexcludes = unmarshallShort(inf);
if (nexcludes)
{
for (int i = 0; i < nexcludes; ++i)
{
const coord_def c = unmarshallCoord(inf);
const int radius = unmarshallShort(inf);
const bool autoexcl = unmarshallBoolean(inf);
const string desc = unmarshallString(inf);
excludes.add_exclude(c, radius, autoexcl, desc);
}
}
}
|