1 #pragma once 2 #ifndef CATA_SRC_MAP_H 3 #define CATA_SRC_MAP_H 4 5 #include <array> 6 #include <bitset> 7 #include <climits> 8 #include <cstddef> 9 #include <cstdint> 10 #include <functional> 11 #include <iosfwd> 12 #include <list> 13 #include <map> 14 #include <memory> 15 #include <new> 16 #include <set> 17 #include <tuple> 18 #include <utility> 19 #include <vector> 20 21 #include "calendar.h" 22 #include "cata_assert.h" 23 #include "cata_utility.h" 24 #include "colony.h" 25 #include "coordinate_conversions.h" 26 #include "coordinates.h" 27 #include "enums.h" 28 #include "game_constants.h" 29 #include "item.h" 30 #include "item_stack.h" 31 #include "level_cache.h" 32 #include "lightmap.h" 33 #include "line.h" 34 #include "lru_cache.h" 35 #include "map_selector.h" 36 #include "mapdata.h" 37 #include "optional.h" 38 #include "point.h" 39 #include "reachability_cache.h" 40 #include "rng.h" 41 #include "type_id.h" 42 #include "units.h" 43 #include "value_ptr.h" 44 45 struct scent_block; 46 47 namespace catacurses 48 { 49 class window; 50 } // namespace catacurses 51 class Character; 52 class Creature; 53 class basecamp; 54 class character_id; 55 class computer; 56 class field; 57 class field_entry; 58 class item_location; 59 class mapgendata; 60 class monster; 61 class optional_vpart_position; 62 class player; 63 class relic_procgen_data; 64 class submap; 65 class vehicle; 66 class zone_data; 67 struct fragment_cloud; 68 struct maptile; 69 struct partial_con; 70 struct spawn_data; 71 struct trap; 72 template<typename Tripoint> 73 class tripoint_range; 74 75 enum class special_item_type : int; 76 class npc_template; 77 class tileray; 78 class vpart_reference; 79 struct MonsterGroupResult; 80 struct mongroup; 81 struct projectile; 82 struct veh_collision; 83 84 struct wrapped_vehicle { 85 tripoint pos; 86 vehicle *v; 87 }; 88 89 using VehicleList = std::vector<wrapped_vehicle>; 90 class map; 91 92 enum ter_bitflags : int; 93 struct pathfinding_cache; 94 struct pathfinding_settings; 95 template<typename T> 96 struct weighted_int_list; 97 struct field_proc_data; 98 99 using relic_procgen_id = string_id<relic_procgen_data>; 100 101 class map_stack : public item_stack 102 { 103 private: 104 tripoint location; 105 map *myorigin; 106 public: map_stack(cata::colony<item> * newstack,tripoint newloc,map * neworigin)107 map_stack( cata::colony<item> *newstack, tripoint newloc, map *neworigin ) : 108 item_stack( newstack ), location( newloc ), myorigin( neworigin ) {} 109 void insert( const item &newitem ) override; 110 iterator erase( const_iterator it ) override; count_limit()111 int count_limit() const override { 112 return MAX_ITEM_IN_SQUARE; 113 } 114 units::volume max_volume() const override; 115 }; 116 117 struct visibility_variables { 118 // Is this struct initialized for current z-level 119 bool variables_set = false; 120 bool u_sight_impaired = false; 121 bool u_is_boomered = false; 122 // Cached values for map visibility calculations 123 int g_light_level = 0; 124 int u_clairvoyance = 0; 125 float vision_threshold = 0.0f; 126 cata::optional<field_type_id> clairvoyance_field; 127 }; 128 129 struct bash_params { 130 // Initial strength 131 int strength = 0; 132 // Make a sound? 133 bool silent = false; 134 // Essentially infinite bash strength + some 135 bool destroy = false; 136 // Do we want to bash floor if no furn/wall exists? 137 bool bash_floor = false; 138 /** 139 * Value from 0.0 to 1.0 that affects interpolation between str_min and str_max 140 * At 0.0, the bash is against str_min of targeted objects 141 * This is required for proper "piercing" bashing, so that one strong hit 142 * can destroy a wall and a floor under it rather than only one at a time. 143 */ 144 float roll = 0.0f; 145 // Was anything hit? 146 bool did_bash = false; 147 // Was anything destroyed? 148 bool success = false; 149 // Did we bash furniture, terrain or vehicle 150 bool bashed_solid = false; 151 /* 152 * Are we bashing this location from above? 153 * Used in determining what sort of terrain the location will turn into, 154 * since if we bashed from above and destroyed it, it probably shouldn't 155 * have a roof either. 156 */ 157 bool bashing_from_above = false; 158 }; 159 160 /** 161 * Manage and cache data about a part of the map. 162 * 163 * Despite the name, this class isn't actually responsible for managing the map as a whole. For that function, 164 * see \ref mapbuffer. Instead, this class loads a part of the mapbuffer into a cache, and adds certain temporary 165 * information such as lighting calculations to it. 166 * 167 * To understand the following descriptions better, you should also read \ref map_management 168 * 169 * The map coordinates always start at (0, 0) for the top-left and end at (map_width-1, map_height-1) for the bottom-right. 170 * 171 * The actual map data is stored in `submap` instances. These instances are managed by `mapbuffer`. 172 * References to the currently active submaps are stored in `map::grid`: 173 * 0 1 2 174 * 3 4 5 175 * 6 7 8 176 * In this example, the top-right submap would be at `grid[2]`. 177 * 178 * When the player moves between submaps, the whole map is shifted, so that if the player moves one submap to the right, 179 * (0, 0) now points to a tile one submap to the right from before 180 */ 181 class map 182 { 183 friend class editmap; 184 friend std::list<item> map_cursor::remove_items_with( const std::function<bool( const item & )> &, 185 int ); 186 187 //FIXME some field processor use private methods 188 friend void field_processor_fd_fire( const tripoint &, field_entry &, field_proc_data & ); 189 friend void field_processor_spread_gas( const tripoint &, field_entry &, field_proc_data & ); 190 friend void field_processor_wandering_field( const tripoint &, field_entry &, field_proc_data & ); 191 friend void field_processor_fd_fire_vent( const tripoint &, field_entry &, field_proc_data & ); 192 friend void field_processor_fd_flame_burst( const tripoint &, field_entry &, field_proc_data & ); 193 friend void field_processor_fd_bees( const tripoint &, field_entry &, field_proc_data & ); 194 friend void field_processor_fd_incendiary( const tripoint &, field_entry &, field_proc_data & ); 195 196 // for testing 197 friend void clear_fields( int zlevel ); 198 199 public: 200 // Constructors & Initialization 201 explicit map( int mapsize = MAPSIZE, bool zlev = true ); map(bool zlev)202 explicit map( bool zlev ) : map( MAPSIZE, zlev ) { } 203 virtual ~map(); 204 205 map &operator=( map && ) = default; 206 207 /** 208 * Tinymaps will ocassionally need to skip npc rotation in map::rotate 209 * Here's a little trigger for them to opt out. We won't be doing that here, though 210 */ skip_npc_rotation()211 virtual bool skip_npc_rotation() const { 212 return false; 213 } 214 215 /** 216 * Sets a dirty flag on the a given cache. 217 * 218 * If this isn't set, it's just assumed that 219 * the cache hasn't changed and 220 * doesn't need to be updated. 221 */ 222 /*@{*/ set_transparency_cache_dirty(const int zlev)223 void set_transparency_cache_dirty( const int zlev ) { 224 if( inbounds_z( zlev ) ) { 225 get_cache( zlev ).transparency_cache_dirty.set(); 226 get_cache( zlev ).r_hor_cache->invalidate(); 227 get_cache( zlev ).r_up_cache->invalidate(); 228 } 229 } 230 231 // more granular version of the transparency cache invalidation 232 // preferred over map::set_transparency_cache_dirty( const int zlev ) 233 // p is in local coords ("ms") 234 // @param field denotes if change comes from the field 235 // fields are not considered for some caches, such as reachability_caches 236 // so passing field=true allows to skip rebuilding of such caches 237 void set_transparency_cache_dirty( const tripoint &p, bool field = false ) { 238 if( inbounds( p ) ) { 239 const tripoint smp = ms_to_sm_copy( p ); 240 get_cache( smp.z ).transparency_cache_dirty.set( smp.x * MAPSIZE + smp.y ); 241 if( !field ) { 242 get_cache( smp.z ).r_hor_cache->invalidate( p.xy() ); 243 get_cache( smp.z ).r_up_cache->invalidate( p.xy() ); 244 } 245 } 246 } 247 set_seen_cache_dirty(const tripoint & change_location)248 void set_seen_cache_dirty( const tripoint &change_location ) { 249 if( inbounds( change_location ) ) { 250 level_cache &cache = get_cache( change_location.z ); 251 if( cache.seen_cache_dirty ) { 252 return; 253 } 254 if( cache.seen_cache[change_location.x][change_location.y] != 0.0 || 255 cache.camera_cache[change_location.x][change_location.y] != 0.0 ) { 256 cache.seen_cache_dirty = true; 257 } 258 } 259 } 260 261 // invalidates seen cache for the whole zlevel unconditionally set_seen_cache_dirty(const int zlevel)262 void set_seen_cache_dirty( const int zlevel ) { 263 if( inbounds_z( zlevel ) ) { 264 level_cache &cache = get_cache( zlevel ); 265 cache.seen_cache_dirty = true; 266 } 267 } 268 set_outside_cache_dirty(const int zlev)269 void set_outside_cache_dirty( const int zlev ) { 270 if( inbounds_z( zlev ) ) { 271 get_cache( zlev ).outside_cache_dirty = true; 272 } 273 } 274 set_floor_cache_dirty(const int zlev)275 void set_floor_cache_dirty( const int zlev ) { 276 if( inbounds_z( zlev ) ) { 277 get_cache( zlev ).floor_cache_dirty = true; 278 } 279 } 280 281 void set_pathfinding_cache_dirty( int zlev ); 282 /*@}*/ 283 set_memory_seen_cache_dirty(const tripoint & p)284 void set_memory_seen_cache_dirty( const tripoint &p ) { 285 const int offset = p.x + p.y * MAPSIZE_Y; 286 if( offset >= 0 && offset < MAPSIZE_X * MAPSIZE_Y ) { 287 get_cache( p.z ).map_memory_seen_cache.reset( offset ); 288 } 289 } 290 invalidate_map_cache(const int zlev)291 void invalidate_map_cache( const int zlev ) { 292 if( inbounds_z( zlev ) ) { 293 level_cache &ch = get_cache( zlev ); 294 ch.floor_cache_dirty = true; 295 ch.seen_cache_dirty = true; 296 ch.outside_cache_dirty = true; 297 set_transparency_cache_dirty( zlev ); 298 } 299 } 300 check_seen_cache(const tripoint & p)301 bool check_seen_cache( const tripoint &p ) const { 302 std::bitset<MAPSIZE_X *MAPSIZE_Y> &memory_seen_cache = 303 get_cache( p.z ).map_memory_seen_cache; 304 return !memory_seen_cache[ static_cast<size_t>( p.x + p.y * MAPSIZE_Y ) ]; 305 } check_and_set_seen_cache(const tripoint & p)306 bool check_and_set_seen_cache( const tripoint &p ) const { 307 std::bitset<MAPSIZE_X *MAPSIZE_Y> &memory_seen_cache = 308 get_cache( p.z ).map_memory_seen_cache; 309 if( !memory_seen_cache[ static_cast<size_t>( p.x + p.y * MAPSIZE_Y ) ] ) { 310 memory_seen_cache.set( static_cast<size_t>( p.x + p.y * MAPSIZE_Y ) ); 311 return true; 312 } 313 return false; 314 } 315 316 /** 317 * A pre-filter for bresenham LOS. 318 * true, if there might be is a potential bresenham path between two points. 319 * false, if such path definitely not possible. 320 */ 321 bool has_potential_los( const tripoint &from, const tripoint &to, 322 bool bounds_check = true ) const { 323 if( bounds_check && ( !inbounds( from ) || !inbounds( to ) ) ) { 324 return false; 325 } 326 if( from.z == to.z ) { 327 level_cache &cache = get_cache( from.z ); 328 return cache.r_hor_cache->has_potential_los( from.xy(), to.xy(), cache ) && 329 cache.r_hor_cache->has_potential_los( to.xy(), from.xy(), cache ) ; 330 } 331 tripoint upper, lower; 332 std::tie( upper, lower ) = from.z > to.z ? std::make_pair( from, to ) : std::make_pair( to, from ); 333 // z-bounds depend on the invariant that both points are inbounds and their z are different 334 return get_cache( lower.z ).r_up_cache->has_potential_los( 335 lower.xy(), upper.xy(), get_cache( lower.z ), get_cache( lower.z + 1 ) ); 336 } 337 338 int reachability_cache_value( const tripoint &p, bool vertical_cache, 339 reachability_cache_quadrant quadrant ) const; 340 341 /** 342 * Callback invoked when a vehicle has moved. 343 */ 344 void on_vehicle_moved( int smz ); 345 346 struct apparent_light_info { 347 bool obstructed; 348 float apparent_light; 349 }; 350 /** Helper function for light calculation; exposed here for map editor 351 */ 352 static apparent_light_info apparent_light_helper( const level_cache &map_cache, 353 const tripoint &p ); 354 /** Determine the visible light level for a tile, based on light_at 355 * for the tile, vision distance, etc 356 * 357 * @param p The tile on this map to draw. 358 * @param cache Currently cached visibility parameters 359 */ 360 lit_level apparent_light_at( const tripoint &p, const visibility_variables &cache ) const; 361 visibility_type get_visibility( lit_level ll, 362 const visibility_variables &cache ) const; 363 364 bool apply_vision_effects( const catacurses::window &w, visibility_type vis ) const; 365 366 // See field.cpp 367 std::tuple<maptile, maptile, maptile> get_wind_blockers( const int &winddirection, 368 const tripoint &pos ); 369 370 /** Draw a visible part of the map into `w`. 371 * 372 * This method uses `g->u.posx()/posy()` for visibility calculations, so it can 373 * not be used for anything but the player's viewport. Likewise, only 374 * `g->m` and maps with equivalent coordinates can be used, as other maps 375 * would have coordinate systems incompatible with `g->u.posx()` 376 * 377 * @param w Window we are drawing in 378 * @param center The coordinate of the center of the viewport, this can 379 * be different from the player coordinate. 380 */ 381 void draw( const catacurses::window &w, const tripoint ¢er ); 382 383 /** Draw the map tile at the given coordinate. Called by `map::draw()`. 384 * 385 * @param w The window we are drawing in 386 * @param u The player 387 * @param p The tile on this map to draw. 388 * @param invert Invert colors if this flag is true 389 * @param show_items Draw items in tile if this flag is true see `center` in `map::draw()` 390 */ 391 void drawsq( const catacurses::window &w, player &u, const tripoint &p, 392 bool invert, bool show_items, 393 const tripoint &view_center, 394 bool low_light = false, bool bright_light = false, 395 bool inorder = false ) const; 396 void drawsq( const catacurses::window &w, player &u, const tripoint &p, 397 bool invert = false, bool show_items = true ) const; 398 399 /** 400 * Add currently loaded submaps (in @ref grid) to the @ref mapbuffer. 401 * They will than be stored by that class and can be loaded from that class. 402 * This can be called several times, the mapbuffer takes care of adding 403 * the same submap several times. It should only be called after the map has 404 * been loaded. 405 * Submaps that have been loaded from the mapbuffer (and not generated) are 406 * already stored in the mapbuffer. 407 * TODO: determine if this is really needed? Submaps are already in the mapbuffer 408 * if they have been loaded from disc and the are added by map::generate, too. 409 * So when do they not appear in the mapbuffer? 410 */ 411 void save(); 412 /** 413 * Load submaps into @ref grid. This might create new submaps if 414 * the @ref mapbuffer can not deliver the requested submap (as it does 415 * not exist on disc). 416 * This must be called before the map can be used at all! 417 * @param w global coordinates of the submap at grid[0]. This 418 * is in submap coordinates. 419 * @param update_vehicles If true, add vehicles to the vehicle cache. 420 */ 421 void load( const tripoint_abs_sm &w, bool update_vehicles ); 422 /** 423 * Shift the map along the vector s. 424 * This is like loading the map with coordinates derived from the current 425 * position of the map (@ref abs_sub) plus the shift vector. 426 * Note: the map must have been loaded before this can be called. 427 */ 428 void shift( const point &s ); 429 /** 430 * Moves the map vertically to (not by!) newz. 431 * Does not actually shift anything, only forces cache updates. 432 * In the future, it will either actually shift the map or it will get removed 433 * after 3D migration is complete. 434 */ 435 void vertical_shift( int newz ); 436 437 void clear_spawns(); 438 void clear_traps(); 439 440 maptile maptile_at( const tripoint &p ) const; 441 maptile maptile_at( const tripoint &p ); 442 private: 443 // Versions of the above that don't do bounds checks 444 maptile maptile_at_internal( const tripoint &p ) const; 445 maptile maptile_at_internal( const tripoint &p ); 446 std::pair<tripoint, maptile> maptile_has_bounds( const tripoint &p, bool bounds_checked ); 447 std::array<std::pair<tripoint, maptile>, 8> get_neighbors( const tripoint &p ); 448 void spread_gas( field_entry &cur, const tripoint &p, int percent_spread, 449 const time_duration &outdoor_age_speedup, scent_block &sblk, 450 const oter_id &om_ter ); 451 void create_hot_air( const tripoint &p, int intensity ); 452 bool gas_can_spread_to( field_entry &cur, const maptile &dst ); 453 void gas_spread_to( field_entry &cur, maptile &dst, const tripoint &p ); 454 int burn_body_part( player &u, field_entry &cur, const bodypart_id &bp, int scale ); 455 public: 456 457 // Movement and LOS 458 /** 459 * Calculate the cost to move past the tile at p. 460 * 461 * The move cost is determined by various obstacles, such 462 * as terrain, vehicles and furniture. 463 * 464 * @note Movement costs for players and zombies both use this function. 465 * 466 * @return The return value is interpreted as follows: 467 * Move Cost | Meaning 468 * --------- | ------- 469 * 0 | Impassable. Use `passable`/`impassable` to check for this. 470 * n > 0 | x*n turns to move past this 471 */ 472 int move_cost( const tripoint &p, const vehicle *ignored_vehicle = nullptr ) const; 473 int move_cost( const point &p, const vehicle *ignored_vehicle = nullptr ) const { 474 return move_cost( tripoint( p, abs_sub.z ), ignored_vehicle ); 475 } 476 bool impassable( const tripoint &p ) const; impassable(const point & p)477 bool impassable( const point &p ) const { 478 return !passable( p ); 479 } 480 bool passable( const tripoint &p ) const; passable(const point & p)481 bool passable( const point &p ) const { 482 return passable( tripoint( p, abs_sub.z ) ); 483 } 484 bool is_wall_adjacent( const tripoint ¢er ) const; 485 486 /** 487 * Similar behavior to `move_cost()`, but ignores vehicles. 488 */ 489 int move_cost_ter_furn( const tripoint &p ) const; move_cost_ter_furn(const point & p)490 int move_cost_ter_furn( const point &p ) const { 491 return move_cost_ter_furn( tripoint( p, abs_sub.z ) ); 492 } 493 bool impassable_ter_furn( const tripoint &p ) const; 494 bool passable_ter_furn( const tripoint &p ) const; 495 496 /** 497 * Cost to move out of one tile and into the next. 498 * 499 * @return The cost in turns to move out of tripoint `from` and into `to` 500 */ 501 int combined_movecost( const tripoint &from, const tripoint &to, 502 const vehicle *ignored_vehicle = nullptr, 503 int modifier = 0, bool flying = false, bool via_ramp = false ) const; 504 505 /** 506 * Returns true if a creature could walk from `from` to `to` in one step. 507 * That is, if the tiles are adjacent and either on the same z-level or connected 508 * by stairs or (in case of flying monsters) open air with no floors. 509 */ 510 bool valid_move( const tripoint &from, const tripoint &to, 511 bool bash = false, bool flying = false, bool via_ramp = false ) const; 512 513 /** 514 * Size of map objects at `p` for purposes of ranged combat. 515 * Size is in percentage of tile: if 1.0, all attacks going through tile 516 * should hit map objects on it, if 0.0 there is nothing to be hit (air/water). 517 */ 518 double ranged_target_size( const tripoint &p ) const; 519 520 // Sees: 521 /** 522 * Returns whether `F` sees `T` with a view range of `range`. 523 */ 524 bool sees( const tripoint &F, const tripoint &T, int range ) const; 525 private: 526 /** 527 * Don't expose the slope adjust outside map functions. 528 * 529 * @param F Thing doing the seeing 530 * @param T Thing being seen 531 * @param range Vision range of F 532 * @param bresenham_slope Indicates the start offset of Bresenham line used to connect 533 * the two points, and may subsequently be used to form a path between them. 534 * Set to zero if the function returns false. 535 **/ 536 bool sees( const tripoint &F, const tripoint &T, int range, int &bresenham_slope ) const; 537 public: 538 /** 539 * Returns coverage of target in relation to the observer. Target is loc2, observer is loc1. 540 * First tile from the target is an obstacle, which has the coverage value. 541 * If there's no obstacle adjacent to the target - no coverage. 542 */ 543 int obstacle_coverage( const tripoint &loc1, const tripoint &loc2 ) const; 544 /** 545 * Returns coverage value of the tile. 546 */ 547 int coverage( const tripoint &p ) const; 548 /** 549 * Check whether there's a direct line of sight between `F` and 550 * `T` with the additional movecost restraints. 551 * 552 * Checks two things: 553 * 1. The `sees()` algorithm between `F` and `T` 554 * 2. That moving over the line of sight would have a move_cost between 555 * `cost_min` and `cost_max`. 556 */ 557 bool clear_path( const tripoint &f, const tripoint &t, int range, 558 int cost_min, int cost_max ) const; 559 560 /** 561 * Populates a vector of points that are reachable within a number of steps from a 562 * point. It could be generalized to take advantage of z levels, but would need some 563 * additional code to detect whether a valid transition was on a tile. 564 * 565 * Does the following: 566 * 1. Checks if a point is reachable using a flood fill and if it is, adds it to a vector. 567 * 568 */ 569 void reachable_flood_steps( std::vector<tripoint> &reachable_pts, const tripoint &f, 570 int range, 571 int cost_min, int cost_max ) const; 572 573 /** 574 * Iteratively tries Bresenham lines with different biases 575 * until it finds a clear line or decides there isn't one. 576 * returns the line found, which may be the straight line, but blocked. 577 */ 578 std::vector<tripoint> find_clear_path( const tripoint &source, const tripoint &destination ) const; 579 580 /** 581 * Check whether the player can access the items located @p. Certain furniture/terrain 582 * may prevent that (e.g. a locked safe). 583 */ 584 bool accessible_items( const tripoint &t ) const; 585 /** 586 * Calculate next search points surrounding the current position. 587 * Points closer to the target come first. 588 * This method leads to straighter lines and prevents weird looking movements away from the target. 589 */ 590 std::vector<tripoint> get_dir_circle( const tripoint &f, const tripoint &t ) const; 591 592 /** 593 * Calculate the best path using A* 594 * 595 * @param f The source location from which to path. 596 * @param t The destination to which to path. 597 * @param settings Structure describing pathfinding parameters. 598 * @param pre_closed Never path through those points. They can still be the source or the destination. 599 */ 600 std::vector<tripoint> route( const tripoint &f, const tripoint &t, 601 const pathfinding_settings &settings, 602 const std::set<tripoint> &pre_closed = {{ }} ) const; 603 604 // Vehicles: Common to 2D and 3D 605 VehicleList get_vehicles(); 606 void add_vehicle_to_cache( vehicle * ); 607 void clear_vehicle_point_from_cache( vehicle *veh, const tripoint &pt ); 608 // clears all vehicle level caches 609 void clear_vehicle_level_caches(); 610 // clears and build vehicle level caches 611 void rebuild_vehicle_level_caches(); 612 void clear_vehicle_list( int zlev ); 613 void update_vehicle_list( const submap *to, int zlev ); 614 //Returns true if vehicle zones are dirty and need to be recached 615 bool check_vehicle_zones( int zlev ); 616 std::vector<zone_data *> get_vehicle_zones( int zlev ); 617 void register_vehicle_zone( vehicle *, int zlev ); 618 bool deregister_vehicle_zone( zone_data &zone ); 619 620 // Removes vehicle from map and returns it in unique_ptr 621 std::unique_ptr<vehicle> detach_vehicle( vehicle *veh ); 622 void destroy_vehicle( vehicle *veh ); 623 // Vehicle movement 624 void vehmove(); 625 // Selects a vehicle to move, returns false if no moving vehicles 626 bool vehproceed( VehicleList &vehicle_list ); 627 628 // Vehicles 629 VehicleList get_vehicles( const tripoint &start, const tripoint &end ); 630 /** 631 * Checks if tile is occupied by vehicle and by which part. 632 * 633 * @param p Tile to check for vehicle 634 */ 635 optional_vpart_position veh_at( const tripoint &p ) const; 636 vehicle *veh_at_internal( const tripoint &p, int &part_num ); 637 const vehicle *veh_at_internal( const tripoint &p, int &part_num ) const; 638 // Put player on vehicle at x,y 639 void board_vehicle( const tripoint &p, Character *pl ); 640 // Remove given passenger from given vehicle part. 641 // If dead_passenger, then null passenger is acceptable. 642 void unboard_vehicle( const vpart_reference &, Character *passenger, 643 bool dead_passenger = false ); 644 // Remove passenger from vehicle at p. 645 void unboard_vehicle( const tripoint &p, bool dead_passenger = false ); 646 // Change vehicle coordinates and move vehicle's driver along. 647 // WARNING: not checking collisions! 648 // optionally: include a list of parts to displace instead of the entire vehicle 649 bool displace_vehicle( vehicle &veh, const tripoint &dp, bool adjust_pos = true, 650 const std::set<int> &parts_to_move = {} ); 651 // make sure a vehicle that is split across z-levels is properly supported 652 // calls displace_vehicle() and shouldn't be called from displace_vehicle 653 void level_vehicle( vehicle &veh ); 654 // move water under wheels. true if moved 655 bool displace_water( const tripoint &dp ); 656 657 // Returns the wheel area of the vehicle multiplied by traction of the surface 658 // When ignore_movement_modifiers is set to true, it returns the area of the wheels touching the ground 659 // TODO: Remove the ugly sinking vehicle hack 660 float vehicle_wheel_traction( const vehicle &veh, bool ignore_movement_modifiers = false ) const; 661 662 // Executes vehicle-vehicle collision based on vehicle::collision results 663 // Returns impulse of the executed collision 664 // If vector contains collisions with vehicles other than veh2, they will be ignored 665 float vehicle_vehicle_collision( vehicle &veh, vehicle &veh2, 666 const std::vector<veh_collision> &collisions ); 667 // Throws vehicle passengers about the vehicle, possibly out of it 668 // Returns change in vehicle orientation due to lost control 669 units::angle shake_vehicle( vehicle &veh, int velocity_before, const units::angle &direction ); 670 671 // Actually moves the vehicle 672 // Unlike displace_vehicle, this one handles collisions 673 vehicle *move_vehicle( vehicle &veh, const tripoint &dp, const tileray &facing ); 674 675 // Furniture 676 void set( const tripoint &p, const ter_id &new_terrain, const furn_id &new_furniture ); set(const point & p,const ter_id & new_terrain,const furn_id & new_furniture)677 void set( const point &p, const ter_id &new_terrain, const furn_id &new_furniture ) { 678 furn_set( p, new_furniture ); 679 ter_set( p, new_terrain ); 680 } 681 std::string name( const tripoint &p ); name(const point & p)682 std::string name( const point &p ) { 683 return name( tripoint( p, abs_sub.z ) ); 684 } 685 std::string disp_name( const tripoint &p ); 686 /** 687 * Returns the name of the obstacle at p that might be blocking movement/projectiles/etc. 688 * Note that this only accounts for vehicles, terrain, and furniture. 689 */ 690 std::string obstacle_name( const tripoint &p ); 691 bool has_furn( const tripoint &p ) const; has_furn(const point & p)692 bool has_furn( const point &p ) const { 693 return has_furn( tripoint( p, abs_sub.z ) ); 694 } 695 furn_id furn( const tripoint &p ) const; furn(const point & p)696 furn_id furn( const point &p ) const { 697 return furn( tripoint( p, abs_sub.z ) ); 698 } 699 /** 700 * furn_reset should be true if new_furniture is being set to f_null 701 * when the player is grab-moving furniture 702 */ 703 void furn_set( const tripoint &p, const furn_id &new_furniture, bool furn_reset = false ); furn_set(const point & p,const furn_id & new_furniture)704 void furn_set( const point &p, const furn_id &new_furniture ) { 705 furn_set( tripoint( p, abs_sub.z ), new_furniture ); 706 } 707 std::string furnname( const tripoint &p ); furnname(const point & p)708 std::string furnname( const point &p ) { 709 return furnname( tripoint( p, abs_sub.z ) ); 710 } 711 bool can_move_furniture( const tripoint &pos, player *p = nullptr ); 712 713 // Terrain 714 ter_id ter( const tripoint &p ) const; ter(const point & p)715 ter_id ter( const point &p ) const { 716 return ter( tripoint( p, abs_sub.z ) ); 717 } 718 719 // Return a bitfield of the adjacent tiles which connect to the given 720 // connect_group. From least-significant bit the order is south, east, 721 // west, north (because that's what cata_tiles expects). 722 // Based on a combination of visibility and memory, not simply the true 723 // terrain. Additional overrides can be passed in to override terrain 724 // at specific positions. This is used to display terrain overview in 725 // the map editor. 726 uint8_t get_known_connections( const tripoint &p, int connect_group, 727 const std::map<tripoint, ter_id> &override = {} ) const; 728 // as above, but for furniture 729 uint8_t get_known_connections_f( const tripoint &p, int connect_group, 730 const std::map<tripoint, furn_id> &override = {} ) const; 731 /** 732 * Returns the full harvest list, for spawning. 733 */ 734 const harvest_id &get_harvest( const tripoint &p ) const; 735 /** 736 * Returns names of the items that would be dropped. 737 */ 738 const std::set<std::string> &get_harvest_names( const tripoint &p ) const; 739 ter_id get_ter_transforms_into( const tripoint &p ) const; 740 741 bool ter_set( const tripoint &p, const ter_id &new_terrain ); ter_set(const point & p,const ter_id & new_terrain)742 bool ter_set( const point &p, const ter_id &new_terrain ) { 743 return ter_set( tripoint( p, abs_sub.z ), new_terrain ); 744 } 745 746 std::string tername( const tripoint &p ) const; tername(const point & p)747 std::string tername( const point &p ) const { 748 return tername( tripoint( p, abs_sub.z ) ); 749 } 750 751 // Check for terrain/furniture/field that provide a 752 // "fire" item to be used for example when crafting or when 753 // a iuse function needs fire. 754 bool has_nearby_fire( const tripoint &p, int radius = 1 ); 755 /** 756 * Check whether a table/workbench/vehicle kitchen or other flat 757 * surface is nearby that could be used for crafting or eating. 758 */ 759 bool has_nearby_table( const tripoint &p, int radius = 1 ); 760 /** 761 * Check whether a chair or vehicle seat is nearby. 762 */ 763 bool has_nearby_chair( const tripoint &p, int radius = 1 ); 764 /** 765 * Checks whether a specific terrain is nearby. 766 */ 767 bool has_nearby_ter( const tripoint &p, const ter_id &type, int radius = 1 ); 768 /** 769 * Check if creature can see some items at p. Includes: 770 * - check for items at this location (has_items(p)) 771 * - check for SEALED flag (sealed furniture/terrain makes 772 * items not visible under any circumstances). 773 * - check for CONTAINER flag (makes items only visible when 774 * the creature is at p or at an adjacent square). 775 */ 776 bool sees_some_items( const tripoint &p, const Creature &who ) const; 777 bool sees_some_items( const tripoint &p, const tripoint &from ) const; 778 /** 779 * Check if the creature could see items at p if there were 780 * any items. This is similar to @ref sees_some_items, but it 781 * does not check that there are actually any items. 782 */ 783 bool could_see_items( const tripoint &p, const Creature &who ) const; 784 bool could_see_items( const tripoint &p, const tripoint &from ) const; 785 /** 786 * Checks for existence of items. Faster than i_at(p).empty 787 */ 788 bool has_items( const tripoint &p ) const; 789 790 /** 791 * Calls the examine function of furniture or terrain at given tile, for given character. 792 * Will only examine terrain if furniture had @ref iexamine::none as the examine function. 793 */ 794 void examine( Character &p, const tripoint &pos ); 795 796 /** 797 * Returns true if point at pos is harvestable right now, with no extra tools. 798 */ 799 bool is_harvestable( const tripoint &pos ) const; 800 801 // Flags 802 // Words relevant to terrain (sharp, etc) 803 std::string features( const tripoint &p ); features(const point & p)804 std::string features( const point &p ) { 805 return features( tripoint( p, abs_sub.z ) ); 806 } 807 // Checks terrain, furniture and vehicles 808 bool has_flag( const std::string &flag, const tripoint &p ) const; has_flag(const std::string & flag,const point & p)809 bool has_flag( const std::string &flag, const point &p ) const { 810 return has_flag( flag, tripoint( p, abs_sub.z ) ); 811 } 812 // True if items can be dropped in this tile 813 bool can_put_items( const tripoint &p ) const; can_put_items(const point & p)814 bool can_put_items( const point &p ) const { 815 return can_put_items( tripoint( p, abs_sub.z ) ); 816 } 817 // True if items can be placed in this tile 818 bool can_put_items_ter_furn( const tripoint &p ) const; can_put_items_ter_furn(const point & p)819 bool can_put_items_ter_furn( const point &p ) const { 820 return can_put_items_ter_furn( tripoint( p, abs_sub.z ) ); 821 } 822 // Checks terrain 823 bool has_flag_ter( const std::string &flag, const tripoint &p ) const; has_flag_ter(const std::string & flag,const point & p)824 bool has_flag_ter( const std::string &flag, const point &p ) const { 825 return has_flag_ter( flag, tripoint( p, abs_sub.z ) ); 826 } 827 // Checks furniture 828 bool has_flag_furn( const std::string &flag, const tripoint &p ) const; has_flag_furn(const std::string & flag,const point & p)829 bool has_flag_furn( const std::string &flag, const point &p ) const { 830 return has_flag_furn( flag, tripoint( p, abs_sub.z ) ); 831 } 832 // Checks terrain or furniture 833 bool has_flag_ter_or_furn( const std::string &flag, const tripoint &p ) const; has_flag_ter_or_furn(const std::string & flag,const point & p)834 bool has_flag_ter_or_furn( const std::string &flag, const point &p ) const { 835 return has_flag_ter_or_furn( flag, tripoint( p, abs_sub.z ) ); 836 } 837 // Fast "oh hai it's update_scent/lightmap/draw/monmove/self/etc again, what about this one" flag checking 838 // Checks terrain, furniture and vehicles 839 bool has_flag( ter_bitflags flag, const tripoint &p ) const; has_flag(ter_bitflags flag,const point & p)840 bool has_flag( ter_bitflags flag, const point &p ) const { 841 return has_flag( flag, tripoint( p, abs_sub.z ) ); 842 } 843 // Checks terrain 844 bool has_flag_ter( ter_bitflags flag, const tripoint &p ) const; has_flag_ter(ter_bitflags flag,const point & p)845 bool has_flag_ter( ter_bitflags flag, const point &p ) const { 846 return has_flag_ter( flag, tripoint( p, abs_sub.z ) ); 847 } 848 // Checks furniture 849 bool has_flag_furn( ter_bitflags flag, const tripoint &p ) const; has_flag_furn(ter_bitflags flag,const point & p)850 bool has_flag_furn( ter_bitflags flag, const point &p ) const { 851 return has_flag_furn( flag, tripoint( p, abs_sub.z ) ); 852 } 853 // Checks terrain or furniture 854 bool has_flag_ter_or_furn( ter_bitflags flag, const tripoint &p ) const; has_flag_ter_or_furn(ter_bitflags flag,const point & p)855 bool has_flag_ter_or_furn( ter_bitflags flag, const point &p ) const { 856 return has_flag_ter_or_furn( flag, tripoint( p, abs_sub.z ) ); 857 } 858 859 // Bashable 860 /** Returns true if there is a bashable vehicle part or the furn/terrain is bashable at p */ 861 bool is_bashable( const tripoint &p, bool allow_floor = false ) const; is_bashable(const point & p)862 bool is_bashable( const point &p ) const { 863 return is_bashable( tripoint( p, abs_sub.z ) ); 864 } 865 /** Returns true if the terrain at p is bashable */ 866 bool is_bashable_ter( const tripoint &p, bool allow_floor = false ) const; is_bashable_ter(const point & p)867 bool is_bashable_ter( const point &p ) const { 868 return is_bashable_ter( tripoint( p, abs_sub.z ) ); 869 } 870 /** Returns true if the furniture at p is bashable */ 871 bool is_bashable_furn( const tripoint &p ) const; is_bashable_furn(const point & p)872 bool is_bashable_furn( const point &p ) const { 873 return is_bashable_furn( tripoint( p, abs_sub.z ) ); 874 } 875 /** Returns true if the furniture or terrain at p is bashable */ 876 bool is_bashable_ter_furn( const tripoint &p, bool allow_floor = false ) const; is_bashable_ter_furn(const point & p)877 bool is_bashable_ter_furn( const point &p ) const { 878 return is_bashable_ter_furn( tripoint( p, abs_sub.z ) ); 879 } 880 /** Returns max_str of the furniture or terrain at p */ 881 int bash_strength( const tripoint &p, bool allow_floor = false ) const; bash_strength(const point & p)882 int bash_strength( const point &p ) const { 883 return bash_strength( tripoint( p, abs_sub.z ) ); 884 } 885 /** Returns min_str of the furniture or terrain at p */ 886 int bash_resistance( const tripoint &p, bool allow_floor = false ) const; bash_resistance(const point & p)887 int bash_resistance( const point &p ) const { 888 return bash_resistance( tripoint( p, abs_sub.z ) ); 889 } 890 /** Returns a success rating from -1 to 10 for a given tile based on a set strength, used for AI movement planning 891 * Values roughly correspond to 10% increment chances of success on a given bash, rounded down. -1 means the square is not bashable */ 892 int bash_rating( int str, const tripoint &p, bool allow_floor = false ) const; bash_rating(const int str,const point & p)893 int bash_rating( const int str, const point &p ) const { 894 return bash_rating( str, tripoint( p, abs_sub.z ) ); 895 } 896 897 // Rubble 898 /** Generates rubble at the given location, if overwrite is true it just writes on top of what currently exists 899 * floor_type is only used if there is a non-bashable wall at the location or with overwrite = true */ 900 void make_rubble( const tripoint &p, const furn_id &rubble_type, bool items, 901 const ter_id &floor_type, bool overwrite = false ); make_rubble(const tripoint & p,const furn_id & rubble_type,bool items)902 void make_rubble( const tripoint &p, const furn_id &rubble_type, bool items ) { 903 make_rubble( p, rubble_type, items, t_dirt, false ); 904 } make_rubble(const tripoint & p)905 void make_rubble( const tripoint &p ) { 906 make_rubble( p, f_rubble, false, t_dirt, false ); 907 } 908 909 bool is_outside( const tripoint &p ) const; is_outside(const point & p)910 bool is_outside( const point &p ) const { 911 return is_outside( tripoint( p, abs_sub.z ) ); 912 } 913 /** 914 * Returns whether or not the terrain at the given location can be dived into 915 * (by monsters that can swim or are aquatic or non-breathing). 916 * @param p The coordinate to look at. 917 * @return true if the terrain can be dived into; false if not. 918 */ 919 bool is_divable( const tripoint &p ) const; is_divable(const point & p)920 bool is_divable( const point &p ) const { 921 return is_divable( tripoint( p, abs_sub.z ) ); 922 } 923 bool is_water_shallow_current( const tripoint &p ) const; is_water_shallow_current(const point & p)924 bool is_water_shallow_current( const point &p ) const { 925 return is_water_shallow_current( tripoint( p, abs_sub.z ) ); 926 } 927 928 /** Check if the last terrain is wall in direction NORTH, SOUTH, WEST or EAST 929 * @param no_furn if true, the function will stop and return false 930 * if it encounters a furniture 931 * @param p starting coordinates of check 932 * @param max ending coordinates of check 933 * @param dir Direction of check 934 * @return true if from x to xmax or y to ymax depending on direction 935 * all terrain is floor and the last terrain is a wall */ 936 bool is_last_ter_wall( bool no_furn, const point &p, 937 const point &max, direction dir ) const; 938 939 /** 940 * Checks if there are any tinder flagged items on the tile. 941 * @param p tile to check 942 */ 943 bool tinder_at( const tripoint &p ); 944 945 /** 946 * Checks if there are any flammable items on the tile. 947 * @param p tile to check 948 * @param threshold Fuel threshold (lower means worse fuels are accepted). 949 */ 950 bool flammable_items_at( const tripoint &p, int threshold = 0 ); 951 /** Returns true if there is a flammable item or field or the furn/terrain is flammable at p */ 952 bool is_flammable( const tripoint &p ); 953 point random_outdoor_tile(); 954 // mapgen 955 956 void draw_line_ter( const ter_id &type, const point &p1, const point &p2 ); 957 void draw_line_furn( const furn_id &type, const point &p1, const point &p2 ); 958 void draw_fill_background( const ter_id &type ); 959 void draw_fill_background( ter_id( *f )() ); 960 void draw_fill_background( const weighted_int_list<ter_id> &f ); 961 962 void draw_square_ter( const ter_id &type, const point &p1, const point &p2 ); 963 void draw_square_furn( const furn_id &type, const point &p1, const point &p2 ); 964 void draw_square_ter( ter_id( *f )(), const point &p1, const point &p2 ); 965 void draw_square_ter( const weighted_int_list<ter_id> &f, const point &p1, 966 const point &p2 ); 967 void draw_rough_circle_ter( const ter_id &type, const point &p, int rad ); 968 void draw_rough_circle_furn( const furn_id &type, const point &p, int rad ); 969 void draw_circle_ter( const ter_id &type, const rl_vec2d &p, double rad ); 970 void draw_circle_ter( const ter_id &type, const point &p, int rad ); 971 void draw_circle_furn( const furn_id &type, const point &p, int rad ); 972 973 void add_corpse( const tripoint &p ); 974 975 // Terrain changing functions 976 // Change all instances of $from->$to 977 void translate( const ter_id &from, const ter_id &to ); 978 // Change all instances $from->$to within this radius, optionally limited to locations in the same submap. 979 // Optionally toggles instances $from->$to & $to->$from 980 void translate_radius( const ter_id &from, const ter_id &to, float radi, const tripoint &p, 981 bool same_submap = false, bool toggle_between = false ); 982 bool close_door( const tripoint &p, bool inside, bool check_only ); 983 bool open_door( const tripoint &p, bool inside, bool check_only = false ); 984 // Destruction 985 /** bash a square for a set number of times at set power. Does not destroy */ 986 void batter( const tripoint &p, int power, int tries = 1, bool silent = false ); 987 /** Keeps bashing a square until it can't be bashed anymore */ 988 void destroy( const tripoint &p, bool silent = false ); 989 /** Keeps bashing a square until there is no more furniture */ 990 void destroy_furn( const tripoint &p, bool silent = false ); 991 void crush( const tripoint &p ); 992 void shoot( const tripoint &p, projectile &proj, bool hit_items ); 993 /** Checks if a square should collapse, returns the X for the one_in(X) collapse chance */ 994 int collapse_check( const tripoint &p ); 995 /** Causes a collapse at p, such as from destroying a wall */ 996 void collapse_at( const tripoint &p, bool silent, bool was_supporting = false, 997 bool destroy_pos = true ); 998 /** Tries to smash the items at the given tripoint. Used by the explosion code */ 999 void smash_items( const tripoint &p, int power, const std::string &cause_message ); 1000 /** 1001 * Returns a pair where first is whether anything was smashed and second is if it was destroyed. 1002 * 1003 * @param p Where to bash 1004 * @param str How hard to bash 1005 * @param silent Don't produce any sound 1006 * @param destroy Destroys some otherwise unbashable tiles 1007 * @param bash_floor Allow bashing the floor and the tile that supports it 1008 * @param bashing_vehicle Vehicle that should NOT be bashed (because it is doing the bashing) 1009 */ 1010 bash_params bash( const tripoint &p, int str, bool silent = false, 1011 bool destroy = false, bool bash_floor = false, 1012 const vehicle *bashing_vehicle = nullptr ); 1013 1014 // Effects of attacks/items 1015 bool hit_with_acid( const tripoint &p ); 1016 bool hit_with_fire( const tripoint &p ); 1017 1018 /** 1019 * Returns true if there is furniture for which filter returns true in a 1 tile radius of p. 1020 * Pass return_true<furn_t> to detect all adjacent furniture. 1021 * @param p the location to check at 1022 * @param filter what to filter the furniture by. 1023 */ 1024 bool has_adjacent_furniture_with( const tripoint &p, 1025 const std::function<bool( const furn_t & )> &filter ); 1026 /** 1027 * Remove moppable fields/items at this location 1028 * @param p the location 1029 * @return true if anything moppable was there, false otherwise. 1030 */ 1031 bool mop_spills( const tripoint &p ); 1032 /** 1033 * Moved here from weather.cpp for speed. Decays fire, washable fields and scent. 1034 * Washable fields are decayed only by 1/3 of the amount fire is. 1035 */ 1036 void decay_fields_and_scent( const time_duration &amount ); 1037 1038 // Signs 1039 std::string get_signage( const tripoint &p ) const; 1040 void set_signage( const tripoint &p, const std::string &message ) const; 1041 void delete_signage( const tripoint &p ) const; 1042 1043 // Radiation 1044 int get_radiation( const tripoint &p ) const; 1045 void set_radiation( const tripoint &p, int value ); set_radiation(const point & p,const int value)1046 void set_radiation( const point &p, const int value ) { 1047 set_radiation( tripoint( p, abs_sub.z ), value ); 1048 } 1049 1050 /** Increment the radiation in the given tile by the given delta 1051 * (decrement it if delta is negative) 1052 */ 1053 void adjust_radiation( const tripoint &p, int delta ); adjust_radiation(const point & p,const int delta)1054 void adjust_radiation( const point &p, const int delta ) { 1055 adjust_radiation( tripoint( p, abs_sub.z ), delta ); 1056 } 1057 1058 // Temperature 1059 // Temperature for submap 1060 int get_temperature( const tripoint &p ) const; 1061 // Set temperature for all four submap quadrants 1062 void set_temperature( const tripoint &p, int temperature ); set_temperature(const point & p,int new_temperature)1063 void set_temperature( const point &p, int new_temperature ) { 1064 set_temperature( tripoint( p, abs_sub.z ), new_temperature ); 1065 } 1066 1067 // Returns points for all submaps with inconsistent state relative to 1068 // the list in map. Used in tests. 1069 std::vector<tripoint> check_submap_active_item_consistency(); 1070 // Accessor that returns a wrapped reference to an item stack for safe modification. 1071 map_stack i_at( const tripoint &p ); i_at(const point & p)1072 map_stack i_at( const point &p ) { 1073 return i_at( tripoint( p, abs_sub.z ) ); 1074 } 1075 item water_from( const tripoint &p ); 1076 void i_clear( const tripoint &p ); i_clear(const point & p)1077 void i_clear( const point &p ) { 1078 i_clear( tripoint( p, abs_sub.z ) ); 1079 } 1080 // i_rem() methods that return values act like container::erase(), 1081 // returning an iterator to the next item after removal. 1082 map_stack::iterator i_rem( const tripoint &p, const map_stack::const_iterator &it ); i_rem(const point & location,const map_stack::const_iterator & it)1083 map_stack::iterator i_rem( const point &location, const map_stack::const_iterator &it ) { 1084 return i_rem( tripoint( location, abs_sub.z ), it ); 1085 } 1086 void i_rem( const tripoint &p, item *it ); i_rem(const point & p,item * it)1087 void i_rem( const point &p, item *it ) { 1088 i_rem( tripoint( p, abs_sub.z ), it ); 1089 } 1090 void spawn_artifact( const tripoint &p, const relic_procgen_id &id ); 1091 void spawn_item( const tripoint &p, const itype_id &type_id, 1092 unsigned quantity = 1, int charges = 0, 1093 const time_point &birthday = calendar::start_of_cataclysm, int damlevel = 0, 1094 const std::set<flag_id> &flags = {} ); 1095 void spawn_item( const point &p, const itype_id &type_id, 1096 unsigned quantity = 1, int charges = 0, 1097 const time_point &birthday = calendar::start_of_cataclysm, int damlevel = 0, 1098 const std::set<flag_id> &flags = {} ) { 1099 spawn_item( tripoint( p, abs_sub.z ), type_id, quantity, charges, birthday, damlevel, flags ); 1100 } 1101 1102 // FIXME: remove these overloads and require spawn_item to take an 1103 // itype_id 1104 void spawn_item( const tripoint &p, const std::string &type_id, 1105 unsigned quantity = 1, int charges = 0, 1106 const time_point &birthday = calendar::start_of_cataclysm, int damlevel = 0, 1107 const std::set<flag_id> &flags = {} ) { 1108 spawn_item( p, itype_id( type_id ), quantity, charges, birthday, damlevel, flags ); 1109 } 1110 void spawn_item( const point &p, const std::string &type_id, 1111 unsigned quantity = 1, int charges = 0, 1112 const time_point &birthday = calendar::start_of_cataclysm, int damlevel = 0, 1113 const std::set<flag_id> &flags = {} ) { 1114 spawn_item( tripoint( p, abs_sub.z ), type_id, quantity, charges, birthday, damlevel, flags ); 1115 } 1116 units::volume max_volume( const tripoint &p ); 1117 units::volume free_volume( const tripoint &p ); 1118 units::volume stored_volume( const tripoint &p ); 1119 1120 /** 1121 * Adds an item to map tile or stacks charges 1122 * @param pos Where to add item 1123 * @param obj Item to add 1124 * @param overflow if destination is full attempt to drop on adjacent tiles 1125 * @return reference to dropped (and possibly stacked) item or null item on failure 1126 * @warning function is relatively expensive and meant for user initiated actions, not mapgen 1127 */ 1128 item &add_item_or_charges( const tripoint &pos, item obj, bool overflow = true ); 1129 item &add_item_or_charges( const point &p, const item &obj, bool overflow = true ) { 1130 return add_item_or_charges( tripoint( p, abs_sub.z ), obj, overflow ); 1131 } 1132 1133 /** 1134 * Place an item on the map, despite the parameter name, this is not necessarily a new item. 1135 * WARNING: does -not- check volume or stack charges. player functions (drop etc) should use 1136 * map::add_item_or_charges 1137 * 1138 * @returns The item that got added, or nulitem. 1139 */ 1140 item &add_item( const tripoint &p, item new_item ); add_item(const point & p,const item & new_item)1141 void add_item( const point &p, const item &new_item ) { 1142 add_item( tripoint( p, abs_sub.z ), new_item ); 1143 } 1144 1145 /** 1146 * Update an item's active status, for example when adding 1147 * hot or perishable liquid to a container. 1148 */ 1149 void make_active( item_location &loc ); 1150 1151 /** 1152 * Update luminosity before and after item's transformation 1153 */ 1154 void update_lum( item_location &loc, bool add ); 1155 1156 /** 1157 * @name Consume items on the map 1158 * 1159 * The functions here consume accessible items / item charges on the map or in vehicles 1160 * around the player (whose positions is given as origin). 1161 * They return a list of copies of the consumed items (with the actually consumed charges 1162 * in it). 1163 * The quantity / amount parameter will be reduced by the number of items/charges removed. 1164 * If all required items could be removed from the map, the quantity/amount will be 0, 1165 * otherwise it will contain a positive value and the remaining items must be gathered from 1166 * somewhere else. 1167 */ 1168 /*@{*/ 1169 std::list<item> use_amount_square( const tripoint &p, const itype_id &type, 1170 int &quantity, const std::function<bool( const item & )> &filter = return_true<item> ); 1171 std::list<item> use_amount( const tripoint &origin, int range, const itype_id &type, 1172 int &quantity, const std::function<bool( const item & )> &filter = return_true<item> ); 1173 std::list<item> use_charges( const tripoint &origin, int range, const itype_id &type, 1174 int &quantity, const std::function<bool( const item & )> &filter = return_true<item>, 1175 basecamp *bcp = nullptr ); 1176 /*@}*/ 1177 std::list<std::pair<tripoint, item *> > get_rc_items( const tripoint &p = { -1, -1, -1 } ); 1178 1179 /** 1180 * Place items from item group in the rectangle f - t. Several items may be spawned 1181 * on different places. Several items may spawn at once (at one place) when the item group says 1182 * so (uses @ref item_group::items_from which may return several items at once). 1183 * @param gp id of item group to spawn from 1184 * @param chance Chance for more items. A chance of 100 creates 1 item all the time, otherwise 1185 * it's the chance that more items will be created (place items until the random roll with that 1186 * chance fails). The chance is used for the first item as well, so it may not spawn an item at 1187 * all. Values <= 0 or > 100 are invalid. 1188 * @param p1 One corner of rectangle in which to spawn items 1189 * @param p2 Second corner of rectangle in which to spawn items 1190 * @param ongrass If false the items won't spawn on flat terrain (grass, floor, ...). 1191 * @param turn The birthday that the created items shall have. 1192 * @param magazine percentage chance item will contain the default magazine 1193 * @param ammo percentage chance item will be filled with default ammo 1194 * @return vector containing all placed items 1195 */ 1196 std::vector<item *> place_items( 1197 const item_group_id &group_id, int chance, const tripoint &p1, const tripoint &p2, 1198 bool ongrass, const time_point &turn, int magazine = 0, int ammo = 0 ); 1199 std::vector<item *> place_items( 1200 const item_group_id &group_id, int chance, const point &p1, const point &p2, 1201 bool ongrass, const time_point &turn, int magazine = 0, int ammo = 0 ) { 1202 return place_items( group_id, chance, tripoint( p1, abs_sub.z ), 1203 tripoint( p2, abs_sub.z ), ongrass, turn, magazine, ammo ); 1204 } 1205 /** 1206 * Place items from an item group at p. Places as much items as the item group says. 1207 * (Most item groups are distributions and will only create one item.) 1208 * @param gp item group to spawn 1209 * @param p Destination of items 1210 * @param turn The birthday that the created items shall have. 1211 * @return Vector of pointers to placed items (can be empty, but no nulls). 1212 */ 1213 std::vector<item *> put_items_from_loc( 1214 const item_group_id &group_id, const tripoint &p, 1215 const time_point &turn = calendar::start_of_cataclysm ); 1216 1217 // Places a list of items, or nothing if the list is empty. 1218 std::vector<item *> spawn_items( const tripoint &p, const std::vector<item> &new_items ); spawn_items(const point & p,const std::vector<item> & new_items)1219 void spawn_items( const point &p, const std::vector<item> &new_items ) { 1220 spawn_items( tripoint( p, abs_sub.z ), new_items ); 1221 } 1222 1223 void create_anomaly( const tripoint &p, artifact_natural_property prop, bool create_rubble = true ); 1224 void create_anomaly( const point &cp, artifact_natural_property prop, bool create_rubble = true ) { 1225 create_anomaly( tripoint( cp, abs_sub.z ), prop, create_rubble ); 1226 } 1227 1228 // Partial construction functions 1229 void partial_con_set( const tripoint &p, const partial_con &con ); 1230 void partial_con_remove( const tripoint &p ); 1231 partial_con *partial_con_at( const tripoint &p ); 1232 // Traps 1233 void trap_set( const tripoint &p, const trap_id &type ); 1234 1235 const trap &tr_at( const tripoint &p ) const; 1236 /// See @ref trap::can_see, which is called for the trap here. 1237 bool can_see_trap_at( const tripoint &p, const Character &c ) const; 1238 1239 void remove_trap( const tripoint &p ); 1240 const std::vector<tripoint> &get_furn_field_locations() const; 1241 const std::vector<tripoint> &get_ter_field_locations() const; 1242 const std::vector<tripoint> &trap_locations( const trap_id &type ) const; 1243 1244 /** 1245 * Handles activating a trap. It includes checks for avoiding the trap 1246 * (which also makes it visible). 1247 * This functions assumes the character is either on top of the trap, 1248 * or adjacent to it. 1249 */ 1250 void maybe_trigger_trap( const tripoint &pos, Creature &c, bool may_avoid ); 1251 1252 // Spawns byproducts from items destroyed in fire. 1253 void create_burnproducts( const tripoint &p, const item &fuel, const units::mass &burned_mass ); 1254 // See fields.cpp 1255 void process_fields(); 1256 void process_fields_in_submap( submap *current_submap, const tripoint &submap_pos ); 1257 /** 1258 * Apply field effects to the creature when it's on a square with fields. 1259 */ 1260 void creature_in_field( Creature &critter ); 1261 /** 1262 * Apply trap effects to the creature, similar to @ref creature_in_field. 1263 * If there is no trap at the creatures location, nothing is done. 1264 * If the creature can avoid the trap, nothing is done as well. 1265 * Otherwise the trap is triggered. 1266 * @param critter Creature that just got trapped 1267 * @param may_avoid If true, the creature tries to avoid the trap 1268 * (@ref Creature::avoid_trap). If false, the trap is always triggered. 1269 */ 1270 void creature_on_trap( Creature &critter, bool may_avoid = true ); 1271 // Field functions 1272 /** 1273 * Get the fields that are here. This is for querying and looking at it only, 1274 * one can not change the fields. 1275 * @param p The local map coordinates, if out of bounds, returns an empty field. 1276 */ 1277 const field &field_at( const tripoint &p ) const; 1278 /** 1279 * Gets fields that are here. Both for querying and edition. 1280 */ 1281 field &field_at( const tripoint &p ); 1282 /** 1283 * Get the age of a field entry (@ref field_entry::age), if there is no 1284 * field of that type, returns `-1_turns`. 1285 */ 1286 time_duration get_field_age( const tripoint &p, const field_type_id &type ) const; 1287 /** 1288 * Get the intensity of a field entry (@ref field_entry::intensity), 1289 * if there is no field of that type, returns 0. 1290 */ 1291 int get_field_intensity( const tripoint &p, const field_type_id &type ) const; 1292 /** 1293 * Increment/decrement age of field entry at point. 1294 * @return resulting age or `-1_turns` if not present (does *not* create a new field). 1295 */ 1296 time_duration mod_field_age( const tripoint &p, const field_type_id &type, 1297 const time_duration &offset ); 1298 /** 1299 * Increment/decrement intensity of field entry at point, creating if not present, 1300 * removing if intensity becomes 0. 1301 * @return resulting intensity, or 0 for not present (either removed or not created at all). 1302 */ 1303 int mod_field_intensity( const tripoint &p, const field_type_id &type, int offset ); 1304 /** 1305 * Set age of field entry at point. 1306 * @param p Location of field 1307 * @param type ID of field 1308 * @param age New age of specified field 1309 * @param isoffset If true, the given age value is added to the existing value, 1310 * if false, the existing age is ignored and overridden. 1311 * @return resulting age or `-1_turns` if not present (does *not* create a new field). 1312 */ 1313 time_duration set_field_age( const tripoint &p, const field_type_id &type, 1314 const time_duration &age, bool isoffset = false ); 1315 /** 1316 * Set intensity of field entry at point, creating if not present, 1317 * removing if intensity becomes 0. 1318 * @param p Location of field 1319 * @param type ID of field 1320 * @param new_intensity New intensity of field 1321 * @param isoffset If true, the given new_intensity value is added to the existing value, 1322 * if false, the existing intensity is ignored and overridden. 1323 * @return resulting intensity, or 0 for not present (either removed or not created at all). 1324 */ 1325 int set_field_intensity( const tripoint &p, const field_type_id &type, int new_intensity, 1326 bool isoffset = false ); 1327 1328 // returns true, if there **might** be a field at `p` 1329 // if false, there's no fields at `p` 1330 bool has_field_at( const tripoint &p, bool check_bounds = true ) const; 1331 /** 1332 * Get field of specific type at point. 1333 * @return NULL if there is no such field entry at that place. 1334 */ 1335 field_entry *get_field( const tripoint &p, const field_type_id &type ) const; 1336 bool dangerous_field_at( const tripoint &p ); 1337 /** 1338 * Add field entry at point, or set intensity if present 1339 * @return false if the field could not be created (out of bounds), otherwise true. 1340 */ 1341 bool add_field( const tripoint &p, const field_type_id &type_id, int intensity = INT_MAX, 1342 const time_duration &age = 0_turns, bool hit_player = true ); 1343 /** 1344 * Remove field entry at xy, ignored if the field entry is not present. 1345 */ 1346 void remove_field( const tripoint &p, const field_type_id &field_to_remove ); 1347 1348 private: 1349 // Is called when field intensity is changed. 1350 // Invalidates relevan map caches, such as transparency cache. 1351 void on_field_modified( const tripoint &p, const field_type &fd_type ); 1352 1353 public: 1354 1355 // Splatters of various kind 1356 void add_splatter( const field_type_id &type, const tripoint &where, int intensity = 1 ); 1357 void add_splatter_trail( const field_type_id &type, const tripoint &from, const tripoint &to ); 1358 void add_splash( const field_type_id &type, const tripoint ¢er, int radius, int intensity ); 1359 1360 void propagate_field( const tripoint ¢er, const field_type_id &type, 1361 int amount, int max_intensity = 0 ); 1362 1363 /** 1364 * Runs one cycle of emission @ref src which **may** result in propagation of fields 1365 * @param pos Location of emission 1366 * @param src Id of object producing the emission 1367 * @param mul Multiplies the chance and possibly qty (if `chance*mul > 100`) of the emission 1368 */ 1369 void emit_field( const tripoint &pos, const emit_id &src, float mul = 1.0f ); 1370 1371 // Scent propagation helpers 1372 /** 1373 * Build the map of scent-resistant tiles. 1374 * Should be way faster than if done in `game.cpp` using public map functions. 1375 */ 1376 void scent_blockers( std::array<std::array<bool, MAPSIZE_X>, MAPSIZE_Y> &blocks_scent, 1377 std::array<std::array<bool, MAPSIZE_X>, MAPSIZE_Y> &reduces_scent, 1378 const point &min, const point &max ); 1379 1380 // Computers 1381 computer *computer_at( const tripoint &p ); 1382 computer *add_computer( const tripoint &p, const std::string &name, int security ); 1383 1384 // Camps 1385 void add_camp( const tripoint_abs_omt &omt_pos, const std::string &name ); 1386 void remove_submap_camp( const tripoint & ); 1387 basecamp hoist_submap_camp( const tripoint &p ); 1388 bool point_within_camp( const tripoint &point_check ) const; 1389 // Graffiti 1390 bool has_graffiti_at( const tripoint &p ) const; 1391 const std::string &graffiti_at( const tripoint &p ) const; 1392 void set_graffiti( const tripoint &p, const std::string &contents ); 1393 void delete_graffiti( const tripoint &p ); 1394 1395 // Climbing 1396 /** 1397 * Checks 3x3 block centered on p for terrain to climb. 1398 * @return Difficulty of climbing check from point p. 1399 */ 1400 int climb_difficulty( const tripoint &p ) const; 1401 1402 // Support (of weight, structures etc.) 1403 private: 1404 // Tiles whose ability to support things was removed in the last turn 1405 std::set<tripoint> support_cache_dirty; 1406 // Checks if the tile is supported and adds it to support_cache_dirty if it isn't 1407 void support_dirty( const tripoint &p ); 1408 public: 1409 1410 // Returns true if terrain at p has NO flag TFLAG_NO_FLOOR, 1411 // if we're not in z-levels mode or if we're at lowest level 1412 bool has_floor( const tripoint &p ) const; 1413 /** Does this tile support vehicles and furniture above it */ 1414 bool supports_above( const tripoint &p ) const; 1415 bool has_floor_or_support( const tripoint &p ) const; 1416 1417 /** 1418 * Handles map objects of given type (not creatures) falling down. 1419 * Returns true if anything changed. 1420 */ 1421 /*@{*/ 1422 void drop_everything( const tripoint &p ); 1423 void drop_furniture( const tripoint &p ); 1424 void drop_items( const tripoint &p ); 1425 void drop_vehicle( const tripoint &p ); 1426 void drop_fields( const tripoint &p ); 1427 /*@}*/ 1428 1429 /** 1430 * Invoked @ref drop_everything on cached dirty tiles. 1431 */ 1432 void process_falling(); 1433 1434 bool is_cornerfloor( const tripoint &p ) const; 1435 1436 // mapgen.cpp functions 1437 void generate( const tripoint &p, const time_point &when ); 1438 void place_spawns( const mongroup_id &group, int chance, 1439 const point &p1, const point &p2, float density, 1440 bool individual = false, bool friendly = false, const std::string &name = "NONE", 1441 int mission_id = -1 ); 1442 void place_gas_pump( const point &p, int charges, const std::string &fuel_type ); place_gas_pump(const point & p,int charges)1443 void place_gas_pump( const point &p, int charges ) { 1444 place_gas_pump( p, charges, one_in( 4 ) ? "diesel" : "gasoline" ); 1445 } 1446 // 6 liters at 250 ml per charge 1447 void place_toilet( const point &p, int charges = 6 * 4 ); 1448 void place_vending( const point &p, const item_group_id &type, bool reinforced = false ); 1449 // places an NPC, if static NPCs are enabled or if force is true 1450 character_id place_npc( const point &p, const string_id<npc_template> &type ); 1451 void apply_faction_ownership( const point &p1, const point &p2, const faction_id &id ); 1452 void add_spawn( const mtype_id &type, int count, const tripoint &p, 1453 bool friendly = false, int faction_id = -1, int mission_id = -1, 1454 const std::string &name = "NONE" ) const; 1455 void add_spawn( const mtype_id &type, int count, const tripoint &p, bool friendly, 1456 int faction_id, int mission_id, const std::string &name, 1457 const spawn_data &data ) const; 1458 void add_spawn( const MonsterGroupResult &spawn_details, const tripoint &p ) const; 1459 void do_vehicle_caching( int z ); 1460 // Note: in 3D mode, will actually build caches on ALL z-levels 1461 void build_map_cache( int zlev, bool skip_lightmap = false ); 1462 // Unlike the other caches, this populates a supplied cache instead of an internal cache. 1463 void build_obstacle_cache( const tripoint &start, const tripoint &end, 1464 fragment_cloud( &obstacle_cache )[MAPSIZE_X][MAPSIZE_Y] ); 1465 1466 vehicle *add_vehicle( const vgroup_id &type, const tripoint &p, const units::angle &dir, 1467 int init_veh_fuel = -1, int init_veh_status = -1, 1468 bool merge_wrecks = true ); 1469 vehicle *add_vehicle( const vgroup_id &type, const point &p, const units::angle &dir, 1470 int init_veh_fuel = -1, int init_veh_status = -1, 1471 bool merge_wrecks = true ); 1472 vehicle *add_vehicle( const vproto_id &type, const tripoint &p, const units::angle &dir, 1473 int init_veh_fuel = -1, int init_veh_status = -1, 1474 bool merge_wrecks = true ); 1475 vehicle *add_vehicle( const vproto_id &type, const point &p, const units::angle &dir, 1476 int init_veh_fuel = -1, int init_veh_status = -1, 1477 bool merge_wrecks = true ); 1478 // Light/transparency 1479 float light_transparency( const tripoint &p ) const; 1480 // Assumes 0,0 is light map center 1481 lit_level light_at( const tripoint &p ) const; 1482 // Raw values for tilesets 1483 float ambient_light_at( const tripoint &p ) const; 1484 /** 1485 * Returns whether the tile at `p` is transparent(you can look past it). 1486 */ 1487 bool is_transparent( const tripoint &p ) const; 1488 // End of light/transparency 1489 1490 /** 1491 * Whether the player character (g->u) can see the given square (local map coordinates). 1492 * This only checks the transparency of the path to the target, the light level is not 1493 * checked. 1494 * @param t Target point to look at 1495 * @param max_range All squares that are further away than this are invisible. 1496 * Ignored if smaller than 0. 1497 */ 1498 bool pl_sees( const tripoint &t, int max_range ) const; 1499 /** 1500 * Uses the map cache to tell if the player could see the given square. 1501 * pl_sees implies pl_line_of_sight 1502 * Used for infrared. 1503 */ 1504 bool pl_line_of_sight( const tripoint &t, int max_range ) const; 1505 std::set<vehicle *> dirty_vehicle_list; 1506 1507 /** return @ref abs_sub */ 1508 tripoint get_abs_sub() const; 1509 /** 1510 * Translates local (to this map) coordinates of a square to global absolute coordinates. 1511 * Coordinates is in the system that is used by the ter/furn/i_at functions. 1512 * Output is in the same scale, but in global system. 1513 */ 1514 tripoint getabs( const tripoint &p ) const; getabs(const point & p)1515 point getabs( const point &p ) const { 1516 return getabs( tripoint( p, abs_sub.z ) ).xy(); 1517 } 1518 /** 1519 * Inverse of @ref getabs 1520 */ 1521 tripoint getlocal( const tripoint &p ) const; getlocal(const point & p)1522 point getlocal( const point &p ) const { 1523 return getlocal( tripoint( p, abs_sub.z ) ).xy(); 1524 } 1525 virtual bool inbounds( const tripoint &p ) const; inbounds(const point & p)1526 bool inbounds( const point &p ) const { 1527 return inbounds( tripoint( p, 0 ) ); 1528 } 1529 inbounds_z(const int z)1530 bool inbounds_z( const int z ) const { 1531 return z >= -OVERMAP_DEPTH && z <= OVERMAP_HEIGHT; 1532 } 1533 1534 /** Clips the coordinates of p to fit the map bounds */ 1535 void clip_to_bounds( tripoint &p ) const; 1536 void clip_to_bounds( int &x, int &y ) const; 1537 void clip_to_bounds( int &x, int &y, int &z ) const; 1538 getmapsize()1539 int getmapsize() const { 1540 return my_MAPSIZE; 1541 } has_zlevels()1542 bool has_zlevels() const { 1543 return zlevels; 1544 } 1545 1546 // Not protected/private for mapgen_functions.cpp access 1547 // Rotates the current map 90*turns degrees clockwise 1548 // Useful for houses, shops, etc 1549 // @param turns number of 90 clockwise turns to make 1550 // @param setpos_safe if true, being used outside of mapgen and can use setpos to 1551 // set NPC positions. if false, cannot use setpos 1552 void rotate( int turns, bool setpos_safe = false ); 1553 1554 // Monster spawning: 1555 public: 1556 /** 1557 * Spawn monsters from submap spawn points and from the overmap. 1558 * @param ignore_sight If true, monsters may spawn in the view of the player 1559 * character (useful when the whole map has been loaded instead, e.g. 1560 * when starting a new game, or after teleportation or after moving vertically). 1561 * If false, monsters are not spawned in view of player character. 1562 */ 1563 void spawn_monsters( bool ignore_sight ); 1564 1565 /** 1566 * Checks to see if the item that is rotting away generates a creature when it does. 1567 * @param item item that is spawning creatures 1568 * @param p The point on this map where the item is and creature will be 1569 */ 1570 void rotten_item_spawn( const item &item, const tripoint &p ); 1571 private: 1572 // Helper #1 - spawns monsters on one submap 1573 void spawn_monsters_submap( const tripoint &gp, bool ignore_sight ); 1574 // Helper #2 - spawns monsters on one submap and from one group on this submap 1575 void spawn_monsters_submap_group( const tripoint &gp, mongroup &group, bool ignore_sight ); 1576 1577 protected: 1578 void saven( const tripoint &grid ); 1579 void loadn( const tripoint &grid, bool update_vehicles, bool _actualize = true ); 1580 void loadn( const point &grid, bool update_vehicles, bool _actualize = true ) { 1581 if( zlevels ) { 1582 for( int gridz = -OVERMAP_DEPTH; gridz <= OVERMAP_HEIGHT; gridz++ ) { 1583 loadn( tripoint( grid, gridz ), update_vehicles, _actualize ); 1584 } 1585 1586 // Note: we want it in a separate loop! It is a post-load cleanup 1587 // Since we're adding roofs, we want it to go up (from lowest to highest) 1588 for( int gridz = -OVERMAP_DEPTH; gridz <= OVERMAP_HEIGHT; gridz++ ) { 1589 add_roofs( tripoint( grid, gridz ) ); 1590 } 1591 } else { 1592 loadn( tripoint( grid, abs_sub.z ), update_vehicles, _actualize ); 1593 } 1594 } 1595 /** 1596 * Fast forward a submap that has just been loading into this map. 1597 * This is used to rot and remove rotten items, grow plants, fill funnels etc. 1598 */ 1599 void actualize( const tripoint &grid ); 1600 /** 1601 * Hacks in missing roofs. Should be removed when 3D mapgen is done. 1602 */ 1603 void add_roofs( const tripoint &grid ); 1604 /** 1605 * Try to fill funnel based items here. Simulates rain from @p since till now. 1606 * @param p The location in this map where to fill funnels. 1607 */ 1608 void fill_funnels( const tripoint &p, const time_point &since ); 1609 /** 1610 * Try to grow a harvestable plant to the next stage(s). 1611 */ 1612 void grow_plant( const tripoint &p ); 1613 /** 1614 * Try to grow fruits on static plants (not planted by the player) 1615 * @param p Place to restock 1616 * @param time_since_last_actualize Time since this function has been 1617 * called the last time. 1618 */ 1619 void restock_fruits( const tripoint &p, const time_duration &time_since_last_actualize ); 1620 /** 1621 * Produce sap on tapped maple trees 1622 * @param p Location of tapped tree 1623 * @param time_since_last_actualize Time since this function has been 1624 * called the last time. 1625 */ 1626 void produce_sap( const tripoint &p, const time_duration &time_since_last_actualize ); 1627 /** 1628 * Radiation-related plant (and fungus?) death. 1629 */ 1630 void rad_scorch( const tripoint &p, const time_duration &time_since_last_actualize ); 1631 void decay_cosmetic_fields( const tripoint &p, const time_duration &time_since_last_actualize ); 1632 1633 void player_in_field( player &u ); 1634 void monster_in_field( monster &z ); 1635 /** 1636 * As part of the map shifting, this shifts the trap locations stored in @ref traplocs. 1637 * @param shift The amount shifting in submap, the same as go into @ref shift. 1638 */ 1639 void shift_traps( const tripoint &shift ); 1640 1641 void copy_grid( const tripoint &to, const tripoint &from ); 1642 void draw_map( mapgendata &dat ); 1643 1644 void draw_lab( mapgendata &dat ); 1645 void draw_temple( const mapgendata &dat ); 1646 void draw_mine( mapgendata &dat ); 1647 void draw_spiral( const mapgendata &dat ); 1648 void draw_anthill( const mapgendata &dat ); 1649 void draw_slimepit( const mapgendata &dat ); 1650 void draw_spider_pit( const mapgendata &dat ); 1651 void draw_triffid( const mapgendata &dat ); 1652 void draw_connections( const mapgendata &dat ); 1653 1654 // Builds a transparency cache and returns true if the cache was invalidated. 1655 // Used to determine if seen cache should be rebuilt. 1656 bool build_transparency_cache( int zlev ); 1657 bool build_vision_transparency_cache( int zlev ); 1658 // fills lm with sunlight. pzlev is current player's zlevel 1659 void build_sunlight_cache( int pzlev ); 1660 public: 1661 void build_outside_cache( int zlev ); 1662 // Get a bitmap indicating which layers are potentially visible from the target layer. 1663 std::bitset<OVERMAP_LAYERS> get_inter_level_visibility( int origin_zlevel )const ; 1664 // Builds a floor cache and returns true if the cache was invalidated. 1665 // Used to determine if seen cache should be rebuilt. 1666 bool build_floor_cache( int zlev ); 1667 // We want this visible in `game`, because we want it built earlier in the turn than the rest 1668 void build_floor_caches(); 1669 1670 protected: 1671 void generate_lightmap( int zlev ); 1672 void build_seen_cache( const tripoint &origin, int target_z ); 1673 void apply_character_light( Character &p ); 1674 1675 int my_MAPSIZE; 1676 bool zlevels; 1677 1678 /** 1679 * Absolute coordinates of first submap (get_submap_at(0,0)) 1680 * This is in submap coordinates (see overmapbuffer for explanation). 1681 * It is set upon: 1682 * - loading submap at grid[0], 1683 * - generating submaps (@ref generate) 1684 * - shifting the map with @ref shift 1685 */ 1686 tripoint abs_sub; 1687 /** 1688 * Sets @ref abs_sub, see there. Uses the same coordinate system as @ref abs_sub. 1689 */ 1690 void set_abs_sub( const tripoint &p ); 1691 1692 private: 1693 field &get_field( const tripoint &p ); 1694 1695 /** 1696 * Get the submap pointer with given index in @ref grid, the index must be valid! 1697 */ 1698 submap *getsubmap( size_t grididx ) const; 1699 /** 1700 * Get the submap pointer containing the specified position within the reality bubble. 1701 * (p) must be a valid coordinate, check with @ref inbounds. 1702 */ unsafe_get_submap_at(const tripoint & p)1703 inline submap *unsafe_get_submap_at( const tripoint &p ) const { 1704 cata_assert( inbounds( p ) ); 1705 return get_submap_at_grid( { p.x / SEEX, p.y / SEEY, p.z } ); 1706 } 1707 submap *get_submap_at( const tripoint &p ) const; get_submap_at(const point & p)1708 submap *get_submap_at( const point &p ) const { 1709 return get_submap_at( tripoint( p, abs_sub.z ) ); 1710 } 1711 /** 1712 * Get the submap pointer containing the specified position within the reality bubble. 1713 * The same as other get_submap_at, (p) must be valid (@ref inbounds). 1714 * Also writes the position within the submap to offset_p 1715 */ unsafe_get_submap_at(const tripoint & p,point & offset_p)1716 submap *unsafe_get_submap_at( const tripoint &p, point &offset_p ) const { 1717 offset_p.x = p.x % SEEX; 1718 offset_p.y = p.y % SEEY; 1719 return unsafe_get_submap_at( p ); 1720 } get_submap_at(const tripoint & p,point & offset_p)1721 submap *get_submap_at( const tripoint &p, point &offset_p ) const { 1722 offset_p.x = p.x % SEEX; 1723 offset_p.y = p.y % SEEY; 1724 return get_submap_at( p ); 1725 } get_submap_at(const point & p,point & offset_p)1726 submap *get_submap_at( const point &p, point &offset_p ) const { 1727 return get_submap_at( { p, abs_sub.z }, offset_p ); 1728 } 1729 /** 1730 * Get submap pointer in the grid at given grid coordinates. Grid coordinates must 1731 * be valid: 0 <= x < my_MAPSIZE, same for y. 1732 * z must be between -OVERMAP_DEPTH and OVERMAP_HEIGHT 1733 */ get_submap_at_grid(const point & gridp)1734 submap *get_submap_at_grid( const point &gridp ) const { 1735 return getsubmap( get_nonant( gridp ) ); 1736 } 1737 submap *get_submap_at_grid( const tripoint &gridp ) const; 1738 protected: 1739 /** 1740 * Get the index of a submap pointer in the grid given by grid coordinates. The grid 1741 * coordinates must be valid: 0 <= x < my_MAPSIZE, same for y. 1742 * Version with z-levels checks for z between -OVERMAP_DEPTH and OVERMAP_HEIGHT 1743 */ 1744 size_t get_nonant( const tripoint &gridp ) const; get_nonant(const point & gridp)1745 size_t get_nonant( const point &gridp ) const { 1746 return get_nonant( { gridp, abs_sub.z } ); 1747 } 1748 /** 1749 * Set the submap pointer in @ref grid at the give index. This is the inverse of 1750 * @ref getsubmap, any existing pointer is overwritten. The index must be valid. 1751 * The given submap pointer must not be null. 1752 */ 1753 void setsubmap( size_t grididx, submap *smap ); 1754 private: 1755 /** Caclulate the greatest populated zlevel in the loaded submaps and save in the level cache. 1756 * fills the map::max_populated_zlev and returns it 1757 * @return max_populated_zlev value 1758 */ 1759 int calc_max_populated_zlev(); 1760 /** 1761 * Conditionally invalidates max_pupulated_zlev cache if the submap uniformity change occurs above current 1762 * max_pupulated_zlev value 1763 * @param zlev zlevel where uniformity change occurred 1764 */ 1765 void invalidate_max_populated_zlev( int zlev ); 1766 1767 /** 1768 * Internal versions of public functions to avoid checking same variables multiple times. 1769 * They lack safety checks, because their callers already do those. 1770 */ 1771 int move_cost_internal( const furn_t &furniture, const ter_t &terrain, const field &field, 1772 const vehicle *veh, int vpart ) const; 1773 int bash_rating_internal( int str, const furn_t &furniture, 1774 const ter_t &terrain, bool allow_floor, 1775 const vehicle *veh, int part ) const; 1776 1777 /** 1778 * Internal version of the drawsq. Keeps a cached maptile for less re-getting. 1779 * Returns false if it has drawn all it should, true if `draw_from_above` should be called after. 1780 */ 1781 bool draw_maptile( const catacurses::window &w, const player &u, const tripoint &p, 1782 const maptile &tile, 1783 bool invert, bool show_items, 1784 const tripoint &view_center, 1785 bool low_light, bool bright_light, bool inorder ) const; 1786 bool draw_maptile_from_memory( const catacurses::window &w, const tripoint &p, 1787 const tripoint &view_center, 1788 bool move_cursor = true ) const; 1789 /** 1790 * Draws the tile as seen from above. 1791 */ 1792 void draw_from_above( const catacurses::window &w, const player &u, const tripoint &p, 1793 const maptile &tile, bool invert, 1794 const tripoint &view_center, 1795 bool low_light, bool bright_light, bool inorder ) const; 1796 1797 int determine_wall_corner( const tripoint &p ) const; 1798 // apply a circular light pattern immediately, however it's best to use... 1799 void apply_light_source( const tripoint &p, float luminance ); 1800 // ...this, which will apply the light after at the end of generate_lightmap, and prevent redundant 1801 // light rays from causing massive slowdowns, if there's a huge amount of light. 1802 void add_light_source( const tripoint &p, float luminance ); 1803 // Handle just cardinal directions and 45 deg angles. 1804 void apply_directional_light( const tripoint &p, int direction, float luminance ); 1805 void apply_light_arc( const tripoint &p, const units::angle &angle, float luminance, 1806 const units::angle &wideangle = 30_degrees ); 1807 void apply_light_ray( bool lit[MAPSIZE_X][MAPSIZE_Y], 1808 const tripoint &s, const tripoint &e, float luminance ); 1809 void add_light_from_items( const tripoint &p, const item_stack::iterator &begin, 1810 const item_stack::iterator &end ); 1811 std::unique_ptr<vehicle> add_vehicle_to_map( std::unique_ptr<vehicle> veh, bool merge_wrecks ); 1812 1813 // Internal methods used to bash just the selected features 1814 // Information on what to bash/what was bashed is read from/written to the bash_params struct 1815 void bash_ter_furn( const tripoint &p, bash_params ¶ms ); 1816 void bash_items( const tripoint &p, bash_params ¶ms ); 1817 void bash_vehicle( const tripoint &p, bash_params ¶ms ); 1818 void bash_field( const tripoint &p, bash_params ¶ms ); 1819 1820 // Gets the roof type of the tile at p 1821 // Second argument refers to whether we have to get a roof (we're over an unpassable tile) 1822 // or can just return air because we bashed down an entire floor tile 1823 ter_id get_roof( const tripoint &p, bool allow_air ); 1824 1825 public: 1826 void process_items(); 1827 private: 1828 // Iterates over every item on the map, passing each item to the provided function. 1829 void process_items_in_submap( submap ¤t_submap, const tripoint &gridp ); 1830 void process_items_in_vehicles( submap ¤t_submap ); 1831 void process_items_in_vehicle( vehicle &cur_veh, submap ¤t_submap ); 1832 1833 /** Enum used by functors in `function_over` to control execution. */ 1834 enum iteration_state { 1835 ITER_CONTINUE = 0, // Keep iterating 1836 ITER_SKIP_SUBMAP, // Skip the rest of this submap 1837 ITER_SKIP_ZLEVEL, // Skip the rest of this z-level 1838 ITER_FINISH // End iteration 1839 }; 1840 /** 1841 * Runs a functor over given submaps 1842 * over submaps in the area, getting next submap only when the current one "runs out" rather than every time. 1843 * gp in the functor is Grid (like `get_submap_at_grid`) coordinate of the submap, 1844 * Will silently clip the area to map bounds. 1845 * @param start Starting point for function 1846 * @param end End point for function 1847 * @param fun Function to run 1848 */ 1849 /*@{*/ 1850 template<typename Functor> 1851 void function_over( const tripoint &start, const tripoint &end, Functor fun ) const; 1852 /*@}*/ 1853 1854 /** 1855 * The list of currently loaded submaps. The size of this should not be changed. 1856 * After calling @ref load or @ref generate, it should only contain non-null pointers. 1857 * Use @ref getsubmap or @ref setsubmap to access it. 1858 */ 1859 std::vector<submap *> grid; 1860 /** 1861 * This vector contains an entry for each trap type, it has therefore the same size 1862 * as the traplist vector. Each entry contains a list of all point on the map that 1863 * contain a trap of that type. The first entry however is always empty as it denotes the 1864 * tr_null trap. 1865 */ 1866 std::vector< std::vector<tripoint> > traplocs; 1867 /** 1868 * Vector of tripoints containing active field-emitting furniture 1869 */ 1870 std::vector<tripoint> field_furn_locs; 1871 /** 1872 * Vector of tripoints containing active field-emitting terrain 1873 */ 1874 std::vector<tripoint> field_ter_locs; 1875 /** 1876 * Holds caches for visibility, light, transparency and vehicles 1877 */ 1878 std::array< std::unique_ptr<level_cache>, OVERMAP_LAYERS > caches; 1879 1880 mutable std::array< std::unique_ptr<pathfinding_cache>, OVERMAP_LAYERS > pathfinding_caches; 1881 /** 1882 * Set of submaps that contain active items in absolute coordinates. 1883 */ 1884 std::set<tripoint> submaps_with_active_items; 1885 1886 /** 1887 * Cache of coordinate pairs recently checked for visibility. 1888 */ 1889 mutable lru_cache<point, char> skew_vision_cache; 1890 1891 // Note: no bounds check get_cache(int zlev)1892 level_cache &get_cache( int zlev ) const { 1893 return *caches[zlev + OVERMAP_DEPTH]; 1894 } 1895 1896 pathfinding_cache &get_pathfinding_cache( int zlev ) const; 1897 1898 visibility_variables visibility_variables_cache; 1899 1900 // caches the highest zlevel above which all zlevels are uniform 1901 // !value || value->first != map::abs_sub means cache is invalid 1902 cata::optional<std::pair<tripoint, int>> max_populated_zlev = cata::nullopt; 1903 1904 public: get_cache_ref(int zlev)1905 const level_cache &get_cache_ref( int zlev ) const { 1906 return *caches[zlev + OVERMAP_DEPTH]; 1907 } 1908 1909 const pathfinding_cache &get_pathfinding_cache_ref( int zlev ) const; 1910 1911 void update_pathfinding_cache( int zlev ) const; 1912 1913 void update_visibility_cache( int zlev ); 1914 const visibility_variables &get_visibility_variables_cache() const; 1915 1916 void update_submap_active_item_status( const tripoint &p ); 1917 1918 // Just exposed for unit test introspection. get_submaps_with_active_items()1919 const std::set<tripoint> &get_submaps_with_active_items() const { 1920 return submaps_with_active_items; 1921 } 1922 // Clips the area to map bounds 1923 tripoint_range<tripoint> points_in_rectangle( 1924 const tripoint &from, const tripoint &to ) const; 1925 tripoint_range<tripoint> points_in_radius( 1926 const tripoint ¢er, size_t radius, size_t radiusz = 0 ) const; 1927 /** 1928 * Yields a range of all points that are contained in the map and have the z-level of 1929 * this map (@ref abs_sub). 1930 */ 1931 tripoint_range<tripoint> points_on_zlevel() const; 1932 /// Same as above, but uses the specific z-level. If the given z-level is invalid, it 1933 /// returns an empty range. 1934 tripoint_range<tripoint> points_on_zlevel( int z ) const; 1935 1936 std::list<item_location> get_active_items_in_radius( const tripoint ¢er, int radius ) const; 1937 std::list<item_location> get_active_items_in_radius( const tripoint ¢er, int radius, 1938 special_item_type type ) const; 1939 1940 /**returns positions of furnitures with matching flag in the specified radius*/ 1941 std::list<tripoint> find_furnitures_with_flag_in_radius( const tripoint ¢er, size_t radius, 1942 const std::string &flag, 1943 size_t radiusz = 0 ); 1944 /**returns creatures in specified radius*/ 1945 std::list<Creature *> get_creatures_in_radius( const tripoint ¢er, size_t radius, 1946 size_t radiusz = 0 ); 1947 1948 level_cache &access_cache( int zlev ); 1949 const level_cache &access_cache( int zlev ) const; 1950 bool dont_draw_lower_floor( const tripoint &p ); 1951 1952 bool has_haulable_items( const tripoint &pos ); 1953 std::vector<item_location> get_haulable_items( const tripoint &pos ); 1954 }; 1955 1956 map &get_map(); 1957 1958 template<int SIZE, int MULTIPLIER> 1959 void shift_bitset_cache( std::bitset<SIZE *SIZE> &cache, const point &s ); 1960 1961 bool ter_furn_has_flag( const ter_t &ter, const furn_t &furn, ter_bitflags flag ); 1962 class tinymap : public map 1963 { 1964 friend class editmap; 1965 public: 1966 explicit tinymap( int mapsize = 2, bool zlevels = false ); 1967 bool inbounds( const tripoint &p ) const override; 1968 1969 /** Sometimes you need to generate and rotate a tinymap without touching npcs */ skip_npc_rotation()1970 bool skip_npc_rotation() const override { 1971 return no_rotate_npcs; 1972 } 1973 bool no_rotate_npcs = false; 1974 }; 1975 1976 class fake_map : public tinymap 1977 { 1978 private: 1979 std::vector<std::unique_ptr<submap>> temp_submaps_; 1980 public: 1981 fake_map( const furn_id &fur_type, const ter_id &ter_type, const trap_id &trap_type, 1982 int fake_map_z ); 1983 ~fake_map() override; 1984 }; 1985 #endif // CATA_SRC_MAP_H 1986