forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapbuffer.cpp
302 lines (259 loc) · 9.67 KB
/
mapbuffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "mapbuffer.h"
#include <algorithm>
#include <exception>
#include <functional>
#include <set>
#include <sstream>
#include <utility>
#include <vector>
#include "cata_utility.h"
#include "coordinate_conversions.h"
#include "debug.h"
#include "distribution_grid.h"
#include "filesystem.h"
#include "fstream_utils.h"
#include "game.h"
#include "game_constants.h"
#include "json.h"
#include "map.h"
#include "output.h"
#include "popup.h"
#include "string_formatter.h"
#include "submap.h"
#include "translations.h"
#include "ui_manager.h"
static std::string find_quad_path( const std::string &dirname, const tripoint &om_addr )
{
return string_format( "%s/%d.%d.%d.map", dirname, om_addr.x, om_addr.y, om_addr.z );
}
static std::string find_dirname( const tripoint &om_addr )
{
const tripoint segment_addr = omt_to_seg_copy( om_addr );
return string_format( "%s/maps/%d.%d.%d", g->get_world_base_save_path(), segment_addr.x,
segment_addr.y, segment_addr.z );
}
mapbuffer MAPBUFFER;
mapbuffer::mapbuffer() = default;
mapbuffer::~mapbuffer() = default;
void mapbuffer::clear()
{
submaps.clear();
}
bool mapbuffer::add_submap( const tripoint &p, std::unique_ptr<submap> &sm )
{
if( submaps.count( p ) ) {
return false;
}
submaps[p] = std::move( sm );
return true;
}
bool mapbuffer::add_submap( const tripoint &p, submap *sm )
{
// FIXME: get rid of this overload and make submap ownership semantics sane.
std::unique_ptr<submap> temp( sm );
bool result = add_submap( p, temp );
if( !result ) {
// NOLINTNEXTLINE( bugprone-unused-return-value )
temp.release();
}
return result;
}
void mapbuffer::remove_submap( tripoint addr )
{
auto m_target = submaps.find( addr );
if( m_target == submaps.end() ) {
debugmsg( "Tried to remove non-existing submap %s", addr.to_string() );
return;
}
submaps.erase( m_target );
}
submap *mapbuffer::lookup_submap( const tripoint &p )
{
const auto iter = submaps.find( p );
if( iter == submaps.end() ) {
try {
return unserialize_submaps( p );
} catch( const std::exception &err ) {
debugmsg( "Failed to load submap %s: %s", p.to_string(), err.what() );
}
return nullptr;
}
return iter->second.get();
}
void mapbuffer::save( bool delete_after_save )
{
assure_dir_exist( g->get_world_base_save_path() + "/maps" );
int num_saved_submaps = 0;
int num_total_submaps = submaps.size();
map &here = get_map();
const tripoint map_origin = sm_to_omt_copy( here.get_abs_sub() );
const bool map_has_zlevels = g != nullptr && here.has_zlevels();
static_popup popup;
// A set of already-saved submaps, in global overmap coordinates.
std::set<tripoint> saved_submaps;
std::list<tripoint> submaps_to_delete;
static constexpr std::chrono::milliseconds update_interval( 500 );
auto last_update = std::chrono::steady_clock::now();
for( auto &elem : submaps ) {
auto now = std::chrono::steady_clock::now();
if( last_update + update_interval < now ) {
popup.message( _( "Please wait as the map saves [%d/%d]" ),
num_saved_submaps, num_total_submaps );
ui_manager::redraw();
refresh_display();
inp_mngr.pump_events();
last_update = now;
}
// Whatever the coordinates of the current submap are,
// we're saving a 2x2 quad of submaps at a time.
// Submaps are generated in quads, so we know if we have one member of a quad,
// we have the rest of it, if that assumption is broken we have REAL problems.
const tripoint om_addr = sm_to_omt_copy( elem.first );
if( saved_submaps.count( om_addr ) != 0 ) {
// Already handled this one.
continue;
}
saved_submaps.insert( om_addr );
// A segment is a chunk of 32x32 submap quads.
// We're breaking them into subdirectories so there aren't too many files per directory.
// Might want to make a set for this one too so it's only checked once per save().
const std::string dirname = find_dirname( om_addr );
const std::string quad_path = find_quad_path( dirname, om_addr );
// delete_on_save deletes everything, otherwise delete submaps
// outside the current map.
const bool zlev_del = !map_has_zlevels && om_addr.z != g->get_levz();
save_quad( dirname, quad_path, om_addr, submaps_to_delete,
delete_after_save || zlev_del ||
om_addr.x < map_origin.x || om_addr.y < map_origin.y ||
om_addr.x > map_origin.x + HALF_MAPSIZE ||
om_addr.y > map_origin.y + HALF_MAPSIZE );
num_saved_submaps += 4;
}
for( auto &elem : submaps_to_delete ) {
remove_submap( elem );
}
get_distribution_grid_tracker().on_saved();
}
void mapbuffer::save_quad( const std::string &dirname, const std::string &filename,
const tripoint &om_addr, std::list<tripoint> &submaps_to_delete,
bool delete_after_save )
{
std::vector<point> offsets;
std::vector<tripoint> submap_addrs;
offsets.push_back( point_zero );
offsets.push_back( point_south );
offsets.push_back( point_east );
offsets.push_back( point_south_east );
bool all_uniform = true;
for( auto &offsets_offset : offsets ) {
tripoint submap_addr = omt_to_sm_copy( om_addr );
submap_addr.x += offsets_offset.x;
submap_addr.y += offsets_offset.y;
submap_addrs.push_back( submap_addr );
submap *sm = submaps[submap_addr].get();
if( sm != nullptr && !sm->is_uniform ) {
all_uniform = false;
}
}
if( all_uniform ) {
// Nothing to save - this quad will be regenerated faster than it would be re-read
if( delete_after_save ) {
for( auto &submap_addr : submap_addrs ) {
if( submaps.count( submap_addr ) > 0 && submaps[submap_addr] != nullptr ) {
submaps_to_delete.push_back( submap_addr );
}
}
}
return;
}
if( disable_mapgen ) {
return;
}
// Don't create the directory if it would be empty
assure_dir_exist( dirname );
write_to_file( filename, [&]( std::ostream & fout ) {
JsonOut jsout( fout );
jsout.start_array();
for( auto &submap_addr : submap_addrs ) {
if( submaps.count( submap_addr ) == 0 ) {
continue;
}
submap *sm = submaps[submap_addr].get();
if( sm == nullptr ) {
continue;
}
jsout.start_object();
jsout.member( "version", savegame_version );
jsout.member( "coordinates" );
jsout.start_array();
jsout.write( submap_addr.x );
jsout.write( submap_addr.y );
jsout.write( submap_addr.z );
jsout.end_array();
sm->store( jsout );
jsout.end_object();
if( delete_after_save ) {
submaps_to_delete.push_back( submap_addr );
}
}
jsout.end_array();
} );
}
// We're reading in way too many entities here to mess around with creating sub-objects and
// seeking around in them, so we're using the json streaming API.
submap *mapbuffer::unserialize_submaps( const tripoint &p )
{
// Map the tripoint to the submap quad that stores it.
const tripoint om_addr = sm_to_omt_copy( p );
const std::string dirname = find_dirname( om_addr );
std::string quad_path = find_quad_path( dirname, om_addr );
if( !file_exist( quad_path ) ) {
// Fix for old saves where the path was generated using std::stringstream, which
// did format the number using the current locale. That formatting may insert
// thousands separators, so the resulting path is "map/1,234.7.8.map" instead
// of "map/1234.7.8.map".
std::ostringstream buffer;
buffer << dirname << "/" << om_addr.x << "." << om_addr.y << "." << om_addr.z << ".map";
if( file_exist( buffer.str() ) ) {
quad_path = buffer.str();
}
}
using namespace std::placeholders;
if( !read_from_file_optional_json( quad_path, std::bind( &mapbuffer::deserialize, this, _1 ) ) ) {
// If it doesn't exist, trigger generating it.
return nullptr;
}
if( submaps.count( p ) == 0 ) {
debugmsg( "file %s did not contain the expected submap %d,%d,%d",
quad_path, p.x, p.y, p.z );
return nullptr;
}
return submaps[ p ].get();
}
void mapbuffer::deserialize( JsonIn &jsin )
{
jsin.start_array();
while( !jsin.end_array() ) {
std::unique_ptr<submap> sm = std::make_unique<submap>();
tripoint submap_coordinates;
jsin.start_object();
int version = 0;
while( !jsin.end_object() ) {
std::string submap_member_name = jsin.get_member_name();
if( submap_member_name == "version" ) {
version = jsin.get_int();
} else if( submap_member_name == "coordinates" ) {
jsin.start_array();
tripoint loc{ jsin.get_int(), jsin.get_int(), jsin.get_int() };
jsin.end_array();
submap_coordinates = loc;
} else {
sm->load( jsin, submap_member_name, version );
}
}
if( !add_submap( submap_coordinates, sm ) ) {
debugmsg( "submap %d,%d,%d was already loaded", submap_coordinates.x, submap_coordinates.y,
submap_coordinates.z );
}
}
}