Skip to content

Commit

Permalink
level_cache: move the level_cache into its own C++ file
Browse files Browse the repository at this point in the history
In preparation for making the vehicle elements level_cache into
a proper cache with dirty bits, move the entire thing into
another file.
  • Loading branch information
mlangsdorf committed Dec 6, 2020
1 parent 7a263ba commit beaa8bf
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 95 deletions.
26 changes: 26 additions & 0 deletions src/level_cache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "level_cache.h"

#include <cstdlib>

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 );
}


124 changes: 124 additions & 0 deletions src/level_cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#pragma once
#ifndef CATA_SRC_LEVEL_CACHE_H
#define CATA_SRC_LEVEL_CACHE_H

#include <array>
#include <bitset>
#include <climits>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#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<MAPSIZE *MAPSIZE> 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<std::bitset<MAPSIZE_Y>, 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_ptr<reachability_cache_horizontal>r_hor_cache =
cata::make_value<reachability_cache_horizontal>();
cata::value_ptr<reachability_cache_vertical> r_up_cache =
cata::make_value<reachability_cache_vertical>();

// stores resulting apparent brightness to player, calculated by map::apparent_light_at
lit_level visibility_cache[MAPSIZE_X][MAPSIZE_Y];
std::bitset<MAPSIZE_X *MAPSIZE_Y> map_memory_seen_cache;
std::bitset<MAPSIZE *MAPSIZE> field_cache;

std::set<vehicle *> vehicle_list;
std::set<vehicle *> zone_vehicles;

/*
bool get_veh_in_active_range() const;
bool get_veh_exists_at( const point &pt ) const;
std::pair<vehicle *, int> 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<vehicle *, int> > veh_cached_parts;
};
#endif // CATA_SRC_LEVEL_CACHE_H
21 changes: 0 additions & 21 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
75 changes: 1 addition & 74 deletions src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<MAPSIZE *MAPSIZE> 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<std::bitset<MAPSIZE_Y>, 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_ptr<reachability_cache_horizontal>r_hor_cache =
cata::make_value<reachability_cache_horizontal>();
cata::value_ptr<reachability_cache_vertical> r_up_cache =
cata::make_value<reachability_cache_vertical>();

// stores resulting apparent brightness to player, calculated by map::apparent_light_at
lit_level visibility_cache[MAPSIZE_X][MAPSIZE_Y];
std::bitset<MAPSIZE_X *MAPSIZE_Y> map_memory_seen_cache;
std::bitset<MAPSIZE *MAPSIZE> field_cache;

bool veh_in_active_range = false;
bool veh_exists_at[MAPSIZE_X][MAPSIZE_Y];
std::map< tripoint, std::pair<vehicle *, int> > veh_cached_parts;
std::set<vehicle *> vehicle_list;
std::set<vehicle *> zone_vehicles;

};

/**
* Manage and cache data about a part of the map.
*
Expand Down

0 comments on commit beaa8bf

Please sign in to comment.