Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor map memory, remove map memory limit. #247

Merged
merged 26 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0a155f1
Encapsulate map::drawsq() arguments and clarify their meaning
olanti-p Nov 27, 2020
56ced0c
Split map memory into submap-sized chunks
olanti-p Nov 27, 2020
88b8951
Optimize map memory access
olanti-p Nov 27, 2020
bb46b79
Memorize tiles on curses only during map::draw()
olanti-p Nov 27, 2020
f0bb35f
Save/load memorized submaps; migrate old mm file
olanti-p Nov 28, 2020
1c99a74
Remove leftovers from map memory limit
olanti-p Nov 27, 2020
10f7388
Some map memory cleanup
olanti-p Nov 28, 2020
5df0454
Don't save empty mm submaps
olanti-p Nov 28, 2020
542d5bd
Deallocate far-away mm submaps on save; report save failure
olanti-p Nov 28, 2020
1979690
Rework tests for map memory
olanti-p Nov 28, 2020
46ade5c
Don't re-allocate region if old region contains required submaps
olanti-p Nov 28, 2020
4f17cc8
Remove map_memory.h from avatar.h
olanti-p Nov 28, 2020
2acbf20
Rename memorized_submap -> mm_submap
olanti-p Dec 3, 2020
9c06fde
Rename mm_submap::clean -> mm_submap::empty, add comments.
olanti-p Dec 3, 2020
08899f4
Improve drawsq_params interface
olanti-p Dec 3, 2020
ef044ce
Remove map_memory.h from lru_cache.cpp
olanti-p Dec 3, 2020
1dacb75
Add function for rectangle overlapping
olanti-p Dec 3, 2020
4cbd2f8
Save mm_submaps in regions
olanti-p Dec 3, 2020
f1e3b4d
Fix save/load not using avatar's global pos, improve docs.
olanti-p Dec 3, 2020
4d7ec4c
When saving, compress mm_submaps using RLE
olanti-p Dec 3, 2020
edb33d6
Simplify map rendering in ascii mode
olanti-p Dec 4, 2020
a6ef4ff
Memorize off-screen tiles in ascii mode
olanti-p Dec 5, 2020
6caaf9e
Fix copypasted code
olanti-p Dec 5, 2020
43bbc56
Fix tile memory saving code, minor cleanup.
olanti-p Dec 6, 2020
740c10a
Add debug logging for tile memory operations
olanti-p Dec 6, 2020
c7ce984
Purge stray newlines
olanti-p Dec 6, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1095,8 +1095,7 @@ cata::optional<tripoint> choose_adjacent_highlight( const std::string &message,
if( !valid.empty() ) {
hilite_cb = make_shared_fast<game::draw_callback_t>( [&]() {
for( const tripoint &pos : valid ) {
g->m.drawsq( g->w_terrain, g->u, pos,
true, true, g->u.pos() + g->u.view_offset );
g->m.drawsq( g->w_terrain, pos, drawsq_params().highlight( true ) );
}
} );
g->add_draw_callback( hilite_cb );
Expand Down
7 changes: 4 additions & 3 deletions src/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ void draw_bullet_curses( map &m, const tripoint &t, const char bullet, const tri

shared_ptr_fast<game::draw_callback_t> bullet_cb = make_shared_fast<game::draw_callback_t>( [&]() {
if( p != nullptr && p->z == vp.z ) {
m.drawsq( g->w_terrain, g->u, *p, false, true, vp );
m.drawsq( g->w_terrain, *p, drawsq_params().center( vp ) );
}
mvwputch( g->w_terrain, t.xy() - vp.xy() + point( POSX, POSY ), c_red, bullet );
} );
Expand Down Expand Up @@ -618,6 +618,7 @@ namespace
void draw_line_curses( game &g, const tripoint &center, const std::vector<tripoint> &ret,
bool noreveal )
{
drawsq_params params = drawsq_params().highlight( true ).center( center );
for( const tripoint &p : ret ) {
const auto critter = g.critter_at( p, true );

Expand All @@ -634,7 +635,7 @@ void draw_line_curses( game &g, const tripoint &center, const std::vector<tripoi
mvwputch( w, point( k, j ), col, sym );
} else {
// This function reveals tile at p and writes it to the player's memory
g.m.drawsq( g.w_terrain, g.u, p, true, true, center );
g.m.drawsq( g.w_terrain, p, params );
}
}
}
Expand Down Expand Up @@ -672,7 +673,7 @@ namespace
void draw_line_curses( game &g, const std::vector<tripoint> &points )
{
for( const tripoint &p : points ) {
g.m.drawsq( g.w_terrain, g.u, p, true, true );
g.m.drawsq( g.w_terrain, p, drawsq_params().highlight( true ) );
}

const tripoint p = points.empty() ? tripoint {POSX, POSY, 0} :
Expand Down
33 changes: 21 additions & 12 deletions src/avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "iuse.h"
#include "kill_tracker.h"
#include "map.h"
#include "map_memory.h"
#include "martialarts.h"
#include "messages.h"
#include "mission.h"
Expand Down Expand Up @@ -71,8 +72,6 @@
#include "vehicle.h"
#include "vpart_position.h"

extern int memorized_tile_count;

static const activity_id ACT_READ( "ACT_READ" );

static const bionic_id bio_eye_optic( "bio_eye_optic" );
Expand Down Expand Up @@ -121,11 +120,16 @@ static void skim_book_msg( const item &book, avatar &u );

avatar::avatar()
{
player_map_memory = std::make_unique<map_memory>();
show_map_memory = true;
active_mission = nullptr;
grab_type = OBJECT_NONE;
}

avatar::~avatar() = default;
avatar::avatar( avatar && ) = default;
avatar &avatar::operator=( avatar && ) = default;

void avatar::toggle_map_memory()
{
show_map_memory = !show_map_memory;
Expand All @@ -136,40 +140,45 @@ bool avatar::should_show_map_memory()
return show_map_memory;
}

void avatar::serialize_map_memory( JsonOut &jsout ) const
bool avatar::save_map_memory()
{
return player_map_memory->save( g->m.getabs( pos() ) );
}

void avatar::load_map_memory()
{
player_map_memory.store( jsout );
player_map_memory->load( g->m.getabs( pos() ) );
}

void avatar::deserialize_map_memory( JsonIn &jsin )
void avatar::prepare_map_memory_region( const tripoint &p1, const tripoint &p2 )
{
player_map_memory.load( jsin );
player_map_memory->prepare_region( p1, p2 );
}

memorized_terrain_tile avatar::get_memorized_tile( const tripoint &pos ) const
const memorized_terrain_tile &avatar::get_memorized_tile( const tripoint &pos ) const
{
return player_map_memory.get_tile( pos );
return player_map_memory->get_tile( pos );
}

void avatar::memorize_tile( const tripoint &pos, const std::string &ter, const int subtile,
const int rotation )
{
player_map_memory.memorize_tile( memorized_tile_count, pos, ter, subtile, rotation );
player_map_memory->memorize_tile( pos, ter, subtile, rotation );
}

void avatar::memorize_symbol( const tripoint &pos, const int symbol )
{
player_map_memory.memorize_symbol( memorized_tile_count, pos, symbol );
player_map_memory->memorize_symbol( pos, symbol );
}

int avatar::get_memorized_symbol( const tripoint &p ) const
{
return player_map_memory.get_symbol( p );
return player_map_memory->get_symbol( p );
}

void avatar::clear_memorized_tile( const tripoint &pos )
{
player_map_memory.clear_memorized_tile( pos );
player_map_memory->clear_memorized_tile( pos );
}

std::vector<mission *> avatar::get_active_missions() const
Expand Down
20 changes: 12 additions & 8 deletions src/avatar.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "enums.h"
#include "item.h"
#include "magic_teleporter_list.h"
#include "map_memory.h"
#include "player.h"
#include "pldata.h"
#include "point.h"
Expand All @@ -25,6 +24,8 @@ class faction;
class mission;
class monster;
class npc;
class map_memory;
struct memorized_terrain_tile;

namespace debug_menu
{
Expand Down Expand Up @@ -54,13 +55,18 @@ class avatar : public player
{
public:
avatar();
avatar( const avatar & ) = delete;
avatar( avatar && );
~avatar();
avatar &operator=( const avatar & ) = delete;
avatar &operator=( avatar && );

void store( JsonOut &json ) const;
void load( const JsonObject &data );
void serialize( JsonOut &json ) const override;
void deserialize( JsonIn &jsin ) override;
void serialize_map_memory( JsonOut &jsout ) const;
void deserialize_map_memory( JsonIn &jsin );
bool save_map_memory();
void load_map_memory();

// newcharacter.cpp
bool create( character_type type, const std::string &tempname = "" );
Expand All @@ -80,11 +86,12 @@ class avatar : public player

void toggle_map_memory();
bool should_show_map_memory();
void prepare_map_memory_region( const tripoint &p1, const tripoint &p2 );
/** Memorizes a given tile in tiles mode; finalize_tile_memory needs to be called after it */
void memorize_tile( const tripoint &pos, const std::string &ter, int subtile,
int rotation );
/** Returns last stored map tile in given location in tiles mode */
memorized_terrain_tile get_memorized_tile( const tripoint &p ) const;
const memorized_terrain_tile &get_memorized_tile( const tripoint &p ) const;
/** Memorizes a given tile in curses mode; finalize_terrain_memory_curses needs to be called after it */
void memorize_symbol( const tripoint &pos, int symbol );
/** Returns last stored map tile in given location in curses mode */
Expand Down Expand Up @@ -212,11 +219,8 @@ class avatar : public player
}

private:
map_memory player_map_memory;
std::unique_ptr<map_memory> player_map_memory;
bool show_map_memory;
/** Used in max_memorized_tiles to cache memory capacity. **/
mutable time_point current_map_memory_turn = calendar::before_time_starts;
mutable size_t current_map_memory_capacity = 0;

friend class debug_menu::mission_debug;
/**
Expand Down
16 changes: 16 additions & 0 deletions src/cata_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,22 @@ void cata_tiles::draw( const point &dest, const tripoint &center, int width, int

const auto &ch = g->m.access_cache( center.z );

// Map memory should be at least the size of the view range
// so that new tiles can be memorized, and at least the size of the display
// since at farthest zoom displayed area may be bigger than view range.
const point min_mm_reg = point(
std::min( o.x, min_visible_x ),
std::min( o.y, min_visible_y )
);
const point max_mm_reg = point(
std::max( sx + o.x, max_visible_x ),
std::max( sy + o.y, max_visible_y )
);
g->u.prepare_map_memory_region(
g->m.getabs( tripoint( min_mm_reg, center.z ) ),
g->m.getabs( tripoint( max_mm_reg, center.z ) )
);

//set up a default tile for the edges outside the render area
visibility_type offscreen_type = VIS_DARK;
if( cache.u_is_boomered ) {
Expand Down
3 changes: 1 addition & 2 deletions src/construction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -891,8 +891,7 @@ void place_construction( const std::string &desc )

shared_ptr_fast<game::draw_callback_t> draw_valid = make_shared_fast<game::draw_callback_t>( [&]() {
for( auto &elem : valid ) {
g->m.drawsq( g->w_terrain, g->u, elem.first, true, false,
g->u.pos() + g->u.view_offset );
g->m.drawsq( g->w_terrain, elem.first, drawsq_params().highlight( true ).show_items( true ) );
}
} );
g->add_draw_callback( draw_valid );
Expand Down
10 changes: 10 additions & 0 deletions src/coordinate_conversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,13 @@ tripoint omt_to_seg_copy( const tripoint &p )
{
return tripoint( divide( p.x, SEG_SIZE ), divide( p.y, SEG_SIZE ), p.z );
}

point sm_to_mmr_remain( int &x, int &y )
{
return point( divide( x, MM_REG_SIZE, x ), divide( y, MM_REG_SIZE, y ) );
}

tripoint mmr_to_sm_copy( const tripoint &p )
{
return tripoint( p.x * MM_REG_SIZE, p.y * MM_REG_SIZE, p.z );
}
13 changes: 13 additions & 0 deletions src/coordinate_conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
* om.y /= SEG_SIZE
* (with special handling for negative values).
*
* memory map region (mmr): Memory map region is a unit of tile memory saved to a directory.
* Each region contains MM_REG_SIZExMM_REG_SIZE memorized submaps, and is used only for
* saving/loading memorized submaps, see map_memory.cpp.
* Translation from sm to mmr:
* sm.x /= MM_REG_SIZE
* sm.y /= MM_REG_SIZE
* (with special handling for negative values).
*
* overmap terrain (omt): the position of a overmap terrain (oter_id).
* Each overmap contains (OMAPX * OMAPY) overmap terrains.
* Translation from omt to om:
Expand Down Expand Up @@ -190,5 +198,10 @@ inline point ms_to_omt_remain( point &p )
}
// overmap terrain to map segment.
tripoint omt_to_seg_copy( const tripoint &p );
// Submap to memory map region.
point sm_to_mmr_remain( int &x, int &y );
// Memory map region to submap.
// Note: this produces sm coords of top-left corner of the region.
tripoint mmr_to_sm_copy( const tripoint &p );

#endif // CATA_SRC_COORDINATE_CONVERSIONS_H
3 changes: 3 additions & 0 deletions src/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ static std::ostream &operator<<( std::ostream &out, DebugClass cl )
if( cl & D_SDL ) {
out << "SDL ";
}
if( cl & D_MMAP ) {
out << "MMAP ";
}
}
return out;
}
Expand Down
2 changes: 2 additions & 0 deletions src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ enum DebugClass {
D_NPC = 1 << 5,
/** SDL & tiles & anything graphical */
D_SDL = 1 << 6,
/** Related to tile memory (map_memory.cpp) */
D_MMAP = 1 << 7,

DC_ALL = ( 1 << 30 ) - 1
};
Expand Down
12 changes: 6 additions & 6 deletions src/editmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,12 +456,12 @@ void editmap::uber_draw_ter( const catacurses::window &w, map *m )
bool draw_fld=true;
bool draw_veh=true;
*/
bool draw_itm = true;
bool game_map = m == &g->m || w == g->w_terrain;
const int msize = MAPSIZE_X;
if( refresh_mplans ) {
hilights["mplan"].points.clear();
}
drawsq_params params = drawsq_params().center( center );
for( const tripoint &p : tripoint_range( start, end ) ) {
int sym = game_map ? '%' : ' ';
if( p.x >= 0 && p.x < msize && p.y >= 0 && p.y < msize ) {
Expand All @@ -470,7 +470,7 @@ void editmap::uber_draw_ter( const catacurses::window &w, map *m )
if( critter != nullptr ) {
critter->draw( w, center.xy(), false );
} else {
m->drawsq( w, g->u, p, false, draw_itm, center, false, true );
m->drawsq( w, p, params );
}
if( refresh_mplans ) {
monster *mon = dynamic_cast<monster *>( critter );
Expand All @@ -481,7 +481,7 @@ void editmap::uber_draw_ter( const catacurses::window &w, map *m )
}
}
} else {
m->drawsq( w, g->u, p, false, draw_itm, center, false, true );
m->drawsq( w, p, params );
}
} else {
mvwputch( w, p.xy() - start.xy(), c_dark_gray, sym );
Expand Down Expand Up @@ -513,7 +513,7 @@ void editmap::draw_main_ui_overlay()
if( critter != nullptr ) {
critter->draw( g->w_terrain, target, true );
} else {
g->m.drawsq( g->w_terrain, g->u, target, true, true, target );
g->m.drawsq( g->w_terrain, target, drawsq_params().highlight( true ).center( target ) );
}
#ifdef TILES
// give some visual indication of different cursor moving modes
Expand Down Expand Up @@ -676,9 +676,9 @@ void editmap::draw_main_ui_overlay()
#endif
hilights["mapgentgt"].draw( *this, true );
tmpmap.reset_vehicle_cache( target.z );
const tripoint center( SEEX - 1, SEEY - 1, target.z );
drawsq_params params = drawsq_params().center( tripoint( SEEX - 1, SEEY - 1, target.z ) );
for( const tripoint &p : tmpmap.points_on_zlevel() ) {
tmpmap.drawsq( g->w_terrain, g->u, p, false, true, center, false, true );
tmpmap.drawsq( g->w_terrain, p, params );
}
#ifdef TILES
}
Expand Down
14 changes: 4 additions & 10 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2728,9 +2728,7 @@ bool game::load( const save_t &name )
return false;
}

read_from_file_optional_json( playerpath + SAVE_EXTENSION_MAP_MEMORY, [&]( JsonIn & jsin ) {
u.deserialize_map_memory( jsin );
} );
u.load_map_memory();

weather.nextweather = calendar::turn;

Expand Down Expand Up @@ -2936,11 +2934,7 @@ bool game::save_player_data()
const bool saved_data = write_to_file( playerfile + SAVE_EXTENSION, [&]( std::ostream & fout ) {
serialize( fout );
}, _( "player data" ) );
const bool saved_map_memory = write_to_file( playerfile + SAVE_EXTENSION_MAP_MEMORY, [&](
std::ostream & fout ) {
JsonOut jsout( fout );
u.serialize_map_memory( jsout );
}, _( "player map memory" ) );
const bool saved_map_memory = u.save_map_memory();
const bool saved_log = write_to_file( playerfile + SAVE_EXTENSION_LOG, [&](
std::ostream & fout ) {
fout << memorial().dump();
Expand Down Expand Up @@ -5780,7 +5774,7 @@ void game::pickup( const tripoint &p )
{
// Highlight target
shared_ptr_fast<game::draw_callback_t> hilite_cb = make_shared_fast<game::draw_callback_t>( [&]() {
m.drawsq( w_terrain, u, p, true, true, u.pos() + u.view_offset );
m.drawsq( w_terrain, p, drawsq_params().highlight( true ) );
} );
add_draw_callback( hilite_cb );

Expand Down Expand Up @@ -5863,7 +5857,7 @@ void game::draw_look_around_cursor( const tripoint &lp, const visibility_variabl
if( creature != nullptr && u.sees( *creature ) ) {
creature->draw( w_terrain, view_center, true );
} else {
m.drawsq( w_terrain, u, lp, true, true, view_center );
m.drawsq( w_terrain, lp, drawsq_params().highlight( true ).center( view_center ) );
}
} else {
std::string visibility_indicator;
Expand Down
1 change: 0 additions & 1 deletion src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class spell_events;
static const std::string SAVE_MASTER( "master.gsav" );
static const std::string SAVE_ARTIFACTS( "artifacts.gsav" );
static const std::string SAVE_EXTENSION( ".sav" );
static const std::string SAVE_EXTENSION_MAP_MEMORY( ".mm" );
static const std::string SAVE_EXTENSION_LOG( ".log" );
static const std::string SAVE_EXTENSION_WEATHER( ".weather" );
static const std::string SAVE_EXTENSION_SHORTCUTS( ".shortcuts" );
Expand Down
Loading