forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_memory.cpp
411 lines (342 loc) · 12.5 KB
/
map_memory.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "map_memory.h"
#include "coordinate_conversions.h"
#include "cuboid_rectangle.h"
#include "debug.h"
#include "filesystem.h"
#include "fstream_utils.h"
#include "game.h"
#include "line.h"
#include "translations.h"
#include "map.h"
const memorized_terrain_tile mm_submap::default_tile { "", 0, 0 };
const int mm_submap::default_symbol = 0;
#define MM_SIZE (MAPSIZE * 2)
#define dbg(x) DebugLog((x),DC::MapMem)
static std::string find_legacy_mm_file()
{
return g->get_player_base_save_path() + ".mm";
}
static std::string find_mm_dir()
{
return string_format( "%s.mm1", g->get_player_base_save_path() );
}
static std::string find_region_path( const std::string &dirname, const tripoint &p )
{
return string_format( "%s/%d.%d.%d.mmr", dirname, p.x, p.y, p.z );
}
/**
* Helper class for converting global sm coord into
* global mm_region coord + sm coord within the region.
*/
struct reg_coord_pair {
tripoint reg;
point sm_loc;
reg_coord_pair( const tripoint &p ) : sm_loc( p.xy() ) {
reg = tripoint( sm_to_mmr_remain( sm_loc.x, sm_loc.y ), p.z );
}
};
mm_submap::mm_submap() = default;
mm_region::mm_region() : submaps {{ nullptr }} {}
bool mm_region::is_empty() const
{
for( const auto &itt : submaps ) {
for( const shared_ptr_fast<mm_submap> &it : itt ) {
if( !it->is_empty() ) {
return false;
}
}
}
return true;
}
map_memory::coord_pair::coord_pair( const tripoint &p ) : loc( p.xy() )
{
sm = tripoint( ms_to_sm_remain( loc.x, loc.y ), p.z );
}
map_memory::map_memory()
{
clear_cache();
}
const memorized_terrain_tile &map_memory::get_tile( const tripoint &pos )
{
coord_pair p( pos );
const mm_submap &sm = get_submap( p.sm );
return sm.tile( p.loc );
}
bool map_memory::has_memory_for_autodrive( const tripoint &pos )
{
// HACK: Map memory is not supposed to be used by ingame mechanics.
// It's just a graphical overlay, it memorizes tileset tiles and text symbols.
// Problem is, many cars' headlights won't cover every ground tile in front of them at night,
// and these dark tiles would be considered as possible obstacles.
// To work around it, we check for whether map memory has any data associated with the tile
// and then assume it's up to date, which works in 99% cases.
// Oh, and we don't want to use get_tile() and get_symbol() to avoid looking up the mm_submap twice.
coord_pair p( pos );
shared_ptr_fast<mm_submap> sm = fetch_submap( p.sm );
return sm->tile( p.loc ) != mm_submap::default_tile ||
sm->symbol( p.loc ) != mm_submap::default_symbol;
}
void map_memory::memorize_tile( const tripoint &pos, const std::string &ter,
const int subtile, const int rotation )
{
coord_pair p( pos );
mm_submap &sm = get_submap( p.sm );
sm.set_tile( p.loc, memorized_terrain_tile{ ter, subtile, rotation } );
}
int map_memory::get_symbol( const tripoint &pos )
{
coord_pair p( pos );
const mm_submap &sm = get_submap( p.sm );
return sm.symbol( p.loc );
}
void map_memory::memorize_symbol( const tripoint &pos, const int symbol )
{
coord_pair p( pos );
mm_submap &sm = get_submap( p.sm );
sm.set_symbol( p.loc, symbol );
}
void map_memory::clear_memorized_tile( const tripoint &pos )
{
coord_pair p( pos );
mm_submap &sm = get_submap( p.sm );
sm.set_symbol( p.loc, mm_submap::default_symbol );
sm.set_tile( p.loc, mm_submap::default_tile );
}
bool map_memory::prepare_region( const tripoint &p1, const tripoint &p2 )
{
assert( p1.z == p2.z );
assert( p1.x <= p2.x && p1.y <= p2.y );
tripoint sm_p1 = coord_pair( p1 ).sm - point_south_east;
tripoint sm_p2 = coord_pair( p2 ).sm + point_south_east;
tripoint sm_pos = sm_p1;
point sm_size = sm_p2.xy() - sm_p1.xy();
bool z_levels = get_map().has_zlevels();
if( sm_pos.z == cache_pos.z || z_levels ) {
inclusive_rectangle<point> rect( cache_pos.xy(), cache_pos.xy() + cache_size );
if( rect.contains( sm_p1.xy() ) && rect.contains( sm_p2.xy() ) ) {
return false;
}
}
dbg( DL::Info ) << "Preparing memory map for area: pos: " << sm_pos << " size: " << sm_size;
int minz = z_levels ? -OVERMAP_DEPTH : p1.z;
int maxz = z_levels ? OVERMAP_HEIGHT : p1.z;
cache_pos = sm_pos;
cache_size = sm_size;
cached.clear();
cached.reserve( cache_size.x * cache_size.y * ( maxz - minz + 1 ) );
for( int z = minz; z <= maxz; z++ ) {
for( int dy = 0; dy < cache_size.y; dy++ ) {
for( int dx = 0; dx < cache_size.x; dx++ ) {
cached.push_back( fetch_submap( tripoint( cache_pos.xy(), z ) + point( dx, dy ) ) );
}
}
}
return true;
}
shared_ptr_fast<mm_submap> map_memory::fetch_submap( const tripoint &sm_pos )
{
shared_ptr_fast<mm_submap> sm = find_submap( sm_pos );
if( sm ) {
return sm;
}
sm = load_submap( sm_pos );
if( sm ) {
return sm;
}
return allocate_submap( sm_pos );
}
shared_ptr_fast<mm_submap> map_memory::allocate_submap( const tripoint &sm_pos )
{
// Since all save/load operations are done on regions of submaps,
// we need to allocate the whole region at once.
shared_ptr_fast<mm_submap> ret;
tripoint reg = reg_coord_pair( sm_pos ).reg;
dbg( DL::Info ) << "Allocated mm_region " << reg << " [" << mmr_to_sm_copy( reg ) << "]";
for( size_t y = 0; y < MM_REG_SIZE; y++ ) {
for( size_t x = 0; x < MM_REG_SIZE; x++ ) {
tripoint pos = mmr_to_sm_copy( reg ) + tripoint( x, y, 0 );
shared_ptr_fast<mm_submap> sm = make_shared_fast<mm_submap>();
if( pos == sm_pos ) {
ret = sm;
}
submaps.insert( std::make_pair( pos, sm ) );
}
}
return ret;
}
shared_ptr_fast<mm_submap> map_memory::find_submap( const tripoint &sm_pos )
{
auto sm = submaps.find( sm_pos );
if( sm == submaps.end() ) {
return nullptr;
} else {
return sm->second;
}
}
//FIXME: This is to fix old (mid 2022) saves. It can be removed at some point.
static void temp_remove_open_air( shared_ptr_fast<mm_submap> sm )
{
if( sm->is_empty() ) {
return;
}
for( int x = 0; x < SEEX; x++ ) {
for( int y = 0; y < SEEY; y++ ) {
const memorized_terrain_tile &t = sm->tile( {x, y} );
if( !t.tile.empty() && t.tile == "t_open_air" ) {
sm->set_tile( {x, y}, mm_submap::default_tile );
}
}
}
}
shared_ptr_fast<mm_submap> map_memory::load_submap( const tripoint &sm_pos )
{
if( test_mode ) {
return nullptr;
}
const std::string dirname = find_mm_dir();
reg_coord_pair p( sm_pos );
const std::string path = find_region_path( dirname, p.reg );
if( !dir_exist( dirname ) ) {
// Old saves don't have [plname].mm1 folder
return nullptr;
}
mm_region mmr;
const auto loader = [&]( JsonIn & jsin ) {
mmr.deserialize( jsin );
};
try {
if( !read_from_file_optional_json( path, loader ) ) {
// Region not found
return nullptr;
}
} catch( const std::exception &err ) {
debugmsg( "Failed to load memory map region (%d,%d,%d): %s",
p.reg.x, p.reg.y, p.reg.z, err.what() );
return nullptr;
}
dbg( DL::Info ) << "Loaded mm_region " << p.reg << " [" << mmr_to_sm_copy( p.reg ) << "]";
shared_ptr_fast<mm_submap> ret;
for( size_t y = 0; y < MM_REG_SIZE; y++ ) {
for( size_t x = 0; x < MM_REG_SIZE; x++ ) {
tripoint pos = mmr_to_sm_copy( p.reg ) + tripoint( x, y, 0 );
shared_ptr_fast<mm_submap> &sm = mmr.submaps[x][y];
if( pos == sm_pos ) {
ret = sm;
}
temp_remove_open_air( mmr.submaps[x][y] );
submaps.insert( std::make_pair( pos, sm ) );
}
}
return ret;
}
mm_submap &map_memory::get_submap( const tripoint &sm_pos )
{
// First, try fetching from cache.
// If it's not in cache (or cache is absent), go the long way.
if( cache_pos != tripoint_min ) {
int zoffset = get_map().has_zlevels()
? ( sm_pos.z + OVERMAP_DEPTH ) * cache_size.y * cache_size.x
: 0;
const point idx = ( sm_pos - cache_pos ).xy();
if( idx.x > 0 && idx.y > 0 && idx.x < cache_size.x && idx.y < cache_size.y ) {
return *cached[idx.y * cache_size.x + idx.x + zoffset];
}
}
return *fetch_submap( sm_pos );
}
void map_memory::load( const tripoint &pos )
{
const std::string dirname = find_mm_dir();
clear_cache();
if( !dir_exist( dirname ) ) {
// Old saves have [plname].mm file and no [plname].mm1 folder
const std::string legacy_file = find_legacy_mm_file();
if( file_exist( legacy_file ) ) {
try {
read_from_file_optional_json( legacy_file, [&]( JsonIn & jsin ) {
this->load_legacy( jsin );
} );
} catch( const std::exception &err ) {
debugmsg( "Failed to load legacy memory map file: %s", err.what() );
}
}
return;
}
coord_pair p( pos );
tripoint start = p.sm - tripoint( MM_SIZE / 2, MM_SIZE / 2, 0 );
dbg( DL::Info ) << "[LOAD] Loading memory map around " << p.sm << ". Loading submaps within "
<< start << "->" << start + tripoint( MM_SIZE, MM_SIZE, 0 );
for( int dy = 0; dy < MM_SIZE; dy++ ) {
for( int dx = 0; dx < MM_SIZE; dx++ ) {
fetch_submap( start + tripoint( dx, dy, 0 ) );
}
}
dbg( DL::Info ) << "[LOAD] Done.";
}
bool map_memory::save( const tripoint &pos )
{
tripoint sm_center = coord_pair( pos ).sm;
const std::string dirname = find_mm_dir();
assure_dir_exist( dirname );
clear_cache();
dbg( DL::Info ) << "N submaps before save: " << submaps.size();
// Since mm_submaps are always allocated in regions,
// we are certain that each region will be filled.
std::map<tripoint, mm_region> regions;
for( auto &it : submaps ) {
const reg_coord_pair p( it.first );
regions[p.reg].submaps[p.sm_loc.x][p.sm_loc.y] = it.second;
}
submaps.clear();
constexpr point MM_HSIZE_P = point( MM_SIZE / 2, MM_SIZE / 2 );
half_open_rectangle<point> rect_keep( sm_center.xy() - MM_HSIZE_P, sm_center.xy() + MM_HSIZE_P );
dbg( DL::Info ) << "[SAVE] Saving memory map around " << sm_center << ". Keeping submaps within "
<< rect_keep.p_min << "->" << rect_keep.p_max;
bool result = true;
for( auto &it : regions ) {
const tripoint ®p = it.first;
mm_region ® = it.second;
if( !reg.is_empty() ) {
const std::string path = find_region_path( dirname, regp );
const std::string descr = string_format(
_( "memory map region for (%d,%d,%d)" ),
regp.x, regp.y, regp.z
);
const auto writer = [&]( std::ostream & fout ) -> void {
fout << serialize_wrapper( [&]( JsonOut & jsout )
{
reg.serialize( jsout );
} );
};
const bool res = write_to_file( path, writer, descr.c_str() );
result = result & res;
}
tripoint regp_sm = mmr_to_sm_copy( regp );
half_open_rectangle<point> rect_reg(
regp_sm.xy(),
regp_sm.xy() + point( MM_REG_SIZE, MM_REG_SIZE )
);
if( rect_reg.overlaps( rect_keep ) ) {
dbg( DL::Info ) << "Keeping mm_region " << regp << " [" << regp_sm << "]";
// Put submaps back
for( size_t y = 0; y < MM_REG_SIZE; y++ ) {
for( size_t x = 0; x < MM_REG_SIZE; x++ ) {
tripoint p = regp_sm + tripoint( x, y, 0 );
shared_ptr_fast<mm_submap> &sm = reg.submaps[x][y];
submaps.insert( std::make_pair( p, sm ) );
}
}
} else {
dbg( DL::Info ) << "Dropping mm_region " << regp << " [" << regp_sm << "]";
}
}
dbg( DL::Info ) << "[SAVE] Done.";
dbg( DL::Info ) << "N submaps after save: " << submaps.size();
return result;
}
void map_memory::clear_cache()
{
cached.clear();
cache_pos = tripoint_min;
cache_size = point_zero;
}