From beaa8bf69d387dad690f07657d34834955283eb9 Mon Sep 17 00:00:00 2001 From: Mark Langsdorf Date: Fri, 4 Dec 2020 11:42:20 -0600 Subject: [PATCH] level_cache: move the level_cache into its own C++ file In preparation for making the vehicle elements level_cache into a proper cache with dirty bits, move the entire thing into another file. --- src/level_cache.cpp | 26 ++++++++++ src/level_cache.h | 124 ++++++++++++++++++++++++++++++++++++++++++++ src/map.cpp | 21 -------- src/map.h | 75 +-------------------------- 4 files changed, 151 insertions(+), 95 deletions(-) create mode 100644 src/level_cache.cpp create mode 100644 src/level_cache.h diff --git a/src/level_cache.cpp b/src/level_cache.cpp new file mode 100644 index 0000000000000..5d0420b941f11 --- /dev/null +++ b/src/level_cache.cpp @@ -0,0 +1,26 @@ +#include "level_cache.h" + +#include + +level_cache::level_cache() +{ + const int map_dimensions = MAPSIZE_X * MAPSIZE_Y; + transparency_cache_dirty.set(); + outside_cache_dirty = true; + floor_cache_dirty = false; + constexpr four_quadrants four_zeros( 0.0f ); + std::fill_n( &lm[0][0], map_dimensions, four_zeros ); + std::fill_n( &sm[0][0], map_dimensions, 0.0f ); + std::fill_n( &light_source_buffer[0][0], map_dimensions, 0.0f ); + std::fill_n( &outside_cache[0][0], map_dimensions, false ); + std::fill_n( &floor_cache[0][0], map_dimensions, false ); + std::fill_n( &transparency_cache[0][0], map_dimensions, 0.0f ); + std::fill_n( &vision_transparency_cache[0][0], map_dimensions, 0.0f ); + std::fill_n( &seen_cache[0][0], map_dimensions, 0.0f ); + std::fill_n( &camera_cache[0][0], map_dimensions, 0.0f ); + std::fill_n( &visibility_cache[0][0], map_dimensions, lit_level::DARK ); + veh_in_active_range = false; + std::fill_n( &veh_exists_at[0][0], map_dimensions, false ); +} + + diff --git a/src/level_cache.h b/src/level_cache.h new file mode 100644 index 0000000000000..d8b00619e06c3 --- /dev/null +++ b/src/level_cache.h @@ -0,0 +1,124 @@ +#pragma once +#ifndef CATA_SRC_LEVEL_CACHE_H +#define CATA_SRC_LEVEL_CACHE_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cata_utility.h" +#include "coordinates.h" +#include "game_constants.h" +#include "lightmap.h" +#include "line.h" +#include "lru_cache.h" +#include "point.h" +#include "reachability_cache.h" +#include "shadowcasting.h" +#include "string_id.h" +#include "type_id.h" +#include "units_fwd.h" +#include "value_ptr.h" + +class vehicle; +struct pathfinding_cache; +struct pathfinding_settings; + +struct level_cache { + public: + // Zeros all relevant values + level_cache(); + level_cache( const level_cache &other ) = default; + + std::bitset transparency_cache_dirty; + bool outside_cache_dirty = false; + bool floor_cache_dirty = false; + bool seen_cache_dirty = false; + // This is a single value indicating that the entire level is floored. + bool no_floor_gaps = false; + + four_quadrants lm[MAPSIZE_X][MAPSIZE_Y]; + float sm[MAPSIZE_X][MAPSIZE_Y]; + // To prevent redundant ray casting into neighbors: precalculate bulk light source positions. + // This is only valid for the duration of generate_lightmap + float light_source_buffer[MAPSIZE_X][MAPSIZE_Y]; + + // if false, means tile is under the roof ("inside"), true means tile is "outside" + // "inside" tiles are protected from sun, rain, etc. (see "INDOORS" flag) + bool outside_cache[MAPSIZE_X][MAPSIZE_Y]; + + // true when vehicle below has "ROOF" or "OPAQUE" part, furniture below has "SUN_ROOF_ABOVE" + // or terrain doesn't have "NO_FLOOR" flag + // false otherwise + // i.e. true == has floor + bool floor_cache[MAPSIZE_X][MAPSIZE_Y]; + + // stores cached transparency of the tiles + // units: "transparency" (see LIGHT_TRANSPARENCY_OPEN_AIR) + float transparency_cache[MAPSIZE_X][MAPSIZE_Y]; + + // materialized (transparency_cache[i][j] > LIGHT_TRANSPARENCY_SOLID) + // doesn't consider fields (i.e. if tile is covered in thick smoke, it's still + // considered transparent for the purpuses of this cache) + // true, if tile is not opaque + std::array, MAPSIZE_X> transparent_cache_wo_fields; + + // stores "adjusted transparency" of the tiles + // initial values derived from transparency_cache, uses same units + // examples of adjustment: changed transparency on player's tile and special case for crouching + float vision_transparency_cache[MAPSIZE_X][MAPSIZE_Y]; + + // stores "visibility" of the tiles to the player + // values range from 1 (fully visible to player) to 0 (not visible) + float seen_cache[MAPSIZE_X][MAPSIZE_Y]; + + // same as `seen_cache` (same units) but contains values for cameras and mirrors + // effective "visibility_cache" is calculated as "max(seen_cache, camera_cache)" + float camera_cache[MAPSIZE_X][MAPSIZE_Y]; + + // reachability caches + // Note: indirection here is introduced, because caches are quite large: + // at least (MAPSIZE_X * MAPSIZE_Y) * 4 bytes (≈69,696 bytes) each + // so having them directly as part of the level_cache interferes with + // CPU cache coherency of level_cache + cata::value_ptrr_hor_cache = + cata::make_value(); + cata::value_ptr r_up_cache = + cata::make_value(); + + // stores resulting apparent brightness to player, calculated by map::apparent_light_at + lit_level visibility_cache[MAPSIZE_X][MAPSIZE_Y]; + std::bitset map_memory_seen_cache; + std::bitset field_cache; + + std::set vehicle_list; + std::set zone_vehicles; + + /* + bool get_veh_in_active_range() const; + bool get_veh_exists_at( const point &pt ) const; + std::pair get_veh_cached_parts( const tripoint &pt ) const; + + void set_veh_in_active_range( bool is_active ); + void set_veh_exists_at( const point &pt, bool exists_at ); + void set_veh_cached_parts( const tripoint &pt, vehicle *veh, int part_num ); + + private: + bool veh_cache_dirty = true; + */ + bool veh_in_active_range = false; + bool veh_exists_at[MAPSIZE_X][MAPSIZE_Y]; + std::map< tripoint, std::pair > veh_cached_parts; +}; +#endif // CATA_SRC_LEVEL_CACHE_H diff --git a/src/map.cpp b/src/map.cpp index d76543d6e1fd2..a0c344edb2e21 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -8643,27 +8643,6 @@ const level_cache &map::access_cache( int zlev ) const return nullcache; } -level_cache::level_cache() -{ - const int map_dimensions = MAPSIZE_X * MAPSIZE_Y; - transparency_cache_dirty.set(); - outside_cache_dirty = true; - floor_cache_dirty = false; - constexpr four_quadrants four_zeros( 0.0f ); - std::fill_n( &lm[0][0], map_dimensions, four_zeros ); - std::fill_n( &sm[0][0], map_dimensions, 0.0f ); - std::fill_n( &light_source_buffer[0][0], map_dimensions, 0.0f ); - std::fill_n( &outside_cache[0][0], map_dimensions, false ); - std::fill_n( &floor_cache[0][0], map_dimensions, false ); - std::fill_n( &transparency_cache[0][0], map_dimensions, 0.0f ); - std::fill_n( &vision_transparency_cache[0][0], map_dimensions, 0.0f ); - std::fill_n( &seen_cache[0][0], map_dimensions, 0.0f ); - std::fill_n( &camera_cache[0][0], map_dimensions, 0.0f ); - std::fill_n( &visibility_cache[0][0], map_dimensions, lit_level::DARK ); - veh_in_active_range = false; - std::fill_n( &veh_exists_at[0][0], map_dimensions, false ); -} - pathfinding_cache::pathfinding_cache() { dirty = true; diff --git a/src/map.h b/src/map.h index 02b1c3ceb8828..a42ba85c30468 100644 --- a/src/map.h +++ b/src/map.h @@ -26,6 +26,7 @@ #include "game_constants.h" #include "item.h" #include "item_stack.h" +#include "level_cache.h" #include "lightmap.h" #include "line.h" #include "lru_cache.h" @@ -158,80 +159,6 @@ struct bash_params { bool bashing_from_above = false; }; -struct level_cache { - // Zeros all relevant values - level_cache(); - level_cache( const level_cache &other ) = default; - - std::bitset transparency_cache_dirty; - bool outside_cache_dirty = false; - bool floor_cache_dirty = false; - bool seen_cache_dirty = false; - // This is a single value indicating that the entire level is floored. - bool no_floor_gaps = false; - - four_quadrants lm[MAPSIZE_X][MAPSIZE_Y]; - float sm[MAPSIZE_X][MAPSIZE_Y]; - // To prevent redundant ray casting into neighbors: precalculate bulk light source positions. - // This is only valid for the duration of generate_lightmap - float light_source_buffer[MAPSIZE_X][MAPSIZE_Y]; - - // if false, means tile is under the roof ("inside"), true means tile is "outside" - // "inside" tiles are protected from sun, rain, etc. (see "INDOORS" flag) - bool outside_cache[MAPSIZE_X][MAPSIZE_Y]; - - // true when vehicle below has "ROOF" or "OPAQUE" part, furniture below has "SUN_ROOF_ABOVE" - // or terrain doesn't have "NO_FLOOR" flag - // false otherwise - // i.e. true == has floor - bool floor_cache[MAPSIZE_X][MAPSIZE_Y]; - - // stores cached transparency of the tiles - // units: "transparency" (see LIGHT_TRANSPARENCY_OPEN_AIR) - float transparency_cache[MAPSIZE_X][MAPSIZE_Y]; - - // materialized (transparency_cache[i][j] > LIGHT_TRANSPARENCY_SOLID) - // doesn't consider fields (i.e. if tile is covered in thick smoke, it's still - // considered transparent for the purpuses of this cache) - // true, if tile is not opaque - std::array, MAPSIZE_X> transparent_cache_wo_fields; - - // stores "adjusted transparency" of the tiles - // initial values derived from transparency_cache, uses same units - // examples of adjustment: changed transparency on player's tile and special case for crouching - float vision_transparency_cache[MAPSIZE_X][MAPSIZE_Y]; - - // stores "visibility" of the tiles to the player - // values range from 1 (fully visible to player) to 0 (not visible) - float seen_cache[MAPSIZE_X][MAPSIZE_Y]; - - // same as `seen_cache` (same units) but contains values for cameras and mirrors - // effective "visibility_cache" is calculated as "max(seen_cache, camera_cache)" - float camera_cache[MAPSIZE_X][MAPSIZE_Y]; - - // reachability caches - // Note: indirection here is introduced, because caches are quite large: - // at least (MAPSIZE_X * MAPSIZE_Y) * 4 bytes (≈69,696 bytes) each - // so having them directly as part of the level_cache interferes with - // CPU cache coherency of level_cache - cata::value_ptrr_hor_cache = - cata::make_value(); - cata::value_ptr r_up_cache = - cata::make_value(); - - // stores resulting apparent brightness to player, calculated by map::apparent_light_at - lit_level visibility_cache[MAPSIZE_X][MAPSIZE_Y]; - std::bitset map_memory_seen_cache; - std::bitset field_cache; - - bool veh_in_active_range = false; - bool veh_exists_at[MAPSIZE_X][MAPSIZE_Y]; - std::map< tripoint, std::pair > veh_cached_parts; - std::set vehicle_list; - std::set zone_vehicles; - -}; - /** * Manage and cache data about a part of the map. *