forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
submap.cpp
336 lines (293 loc) · 9.01 KB
/
submap.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
#include "submap.h"
#include <algorithm>
#include <array>
#include <iterator>
#include <memory>
#include <utility>
#include "basecamp.h"
#include "int_id.h"
#include "mapdata.h"
#include "tileray.h"
#include "trap.h"
#include "vehicle.h"
template<int sx, int sy>
void maptile_soa<sx, sy>::swap_soa_tile( point p1, point p2 )
{
std::swap( ter[p1.x][p1.y], ter[p2.x][p2.y] );
std::swap( frn[p1.x][p1.y], frn[p2.x][p2.y] );
std::swap( lum[p1.x][p1.y], lum[p2.x][p2.y] );
std::swap( itm[p1.x][p1.y], itm[p2.x][p2.y] );
std::swap( fld[p1.x][p1.y], fld[p2.x][p2.y] );
std::swap( trp[p1.x][p1.y], trp[p2.x][p2.y] );
std::swap( rad[p1.x][p1.y], rad[p2.x][p2.y] );
}
template<int sx, int sy>
void maptile_soa<sx, sy>::swap_soa_tile( point p, maptile_soa<1, 1> &other )
{
std::swap( ter[p.x][p.y], **other.ter );
std::swap( frn[p.x][p.y], **other.frn );
std::swap( lum[p.x][p.y], **other.lum );
std::swap( itm[p.x][p.y], **other.itm );
std::swap( fld[p.x][p.y], **other.fld );
std::swap( trp[p.x][p.y], **other.trp );
std::swap( rad[p.x][p.y], **other.rad );
}
submap::submap()
{
std::uninitialized_fill_n( &ter[0][0], elements, t_null );
std::uninitialized_fill_n( &frn[0][0], elements, f_null );
std::uninitialized_fill_n( &lum[0][0], elements, 0 );
std::uninitialized_fill_n( &trp[0][0], elements, tr_null );
std::uninitialized_fill_n( &rad[0][0], elements, 0 );
is_uniform = false;
}
submap::submap( submap && ) = default;
submap::~submap() = default;
submap &submap::operator=( submap && ) = default;
void submap::update_lum_rem( point p, const item &i )
{
is_uniform = false;
if( !i.is_emissive() ) {
return;
} else if( lum[p.x][p.y] && lum[p.x][p.y] < 255 ) {
lum[p.x][p.y]--;
return;
}
// Have to scan through all items to be sure removing i will actually lower
// the count below 255.
int count = 0;
for( const auto &it : itm[p.x][p.y] ) {
if( it.is_emissive() ) {
count++;
}
}
if( count <= 256 ) {
lum[p.x][p.y] = static_cast<uint8_t>( count - 1 );
}
}
void submap::insert_cosmetic( point p, const std::string &type, const std::string &str )
{
cosmetic_t ins;
ins.pos = p;
ins.type = type;
ins.str = str;
cosmetics.push_back( ins );
}
static const std::string COSMETICS_GRAFFITI( "GRAFFITI" );
static const std::string COSMETICS_SIGNAGE( "SIGNAGE" );
// Handle GCC warning: 'warning: returning reference to temporary'
static const std::string STRING_EMPTY;
struct cosmetic_find_result {
bool result;
int ndx;
};
static cosmetic_find_result make_result( bool b, int ndx )
{
cosmetic_find_result result;
result.result = b;
result.ndx = ndx;
return result;
}
static cosmetic_find_result find_cosmetic(
const std::vector<submap::cosmetic_t> &cosmetics, point p, const std::string &type )
{
for( size_t i = 0; i < cosmetics.size(); ++i ) {
if( cosmetics[i].pos == p && cosmetics[i].type == type ) {
return make_result( true, i );
}
}
return make_result( false, -1 );
}
bool submap::has_graffiti( point p ) const
{
return find_cosmetic( cosmetics, p, COSMETICS_GRAFFITI ).result;
}
const std::string &submap::get_graffiti( point p ) const
{
const auto fresult = find_cosmetic( cosmetics, p, COSMETICS_GRAFFITI );
if( fresult.result ) {
return cosmetics[ fresult.ndx ].str;
}
return STRING_EMPTY;
}
void submap::set_graffiti( point p, const std::string &new_graffiti )
{
is_uniform = false;
// Find signage at p if available
const auto fresult = find_cosmetic( cosmetics, p, COSMETICS_GRAFFITI );
if( fresult.result ) {
cosmetics[ fresult.ndx ].str = new_graffiti;
} else {
insert_cosmetic( p, COSMETICS_GRAFFITI, new_graffiti );
}
}
void submap::delete_graffiti( point p )
{
is_uniform = false;
const auto fresult = find_cosmetic( cosmetics, p, COSMETICS_GRAFFITI );
if( fresult.result ) {
cosmetics[ fresult.ndx ] = cosmetics.back();
cosmetics.pop_back();
}
}
bool submap::has_signage( point p ) const
{
if( frn[p.x][p.y].obj().has_flag( "SIGN" ) ) {
return find_cosmetic( cosmetics, p, COSMETICS_SIGNAGE ).result;
}
return false;
}
std::string submap::get_signage( point p ) const
{
if( frn[p.x][p.y].obj().has_flag( "SIGN" ) ) {
const auto fresult = find_cosmetic( cosmetics, p, COSMETICS_SIGNAGE );
if( fresult.result ) {
return cosmetics[ fresult.ndx ].str;
}
}
return STRING_EMPTY;
}
void submap::set_signage( point p, const std::string &s )
{
is_uniform = false;
// Find signage at p if available
const auto fresult = find_cosmetic( cosmetics, p, COSMETICS_SIGNAGE );
if( fresult.result ) {
cosmetics[ fresult.ndx ].str = s;
} else {
insert_cosmetic( p, COSMETICS_SIGNAGE, s );
}
}
void submap::delete_signage( point p )
{
is_uniform = false;
const auto fresult = find_cosmetic( cosmetics, p, COSMETICS_SIGNAGE );
if( fresult.result ) {
cosmetics[ fresult.ndx ] = cosmetics.back();
cosmetics.pop_back();
}
}
void submap::update_legacy_computer()
{
if( legacy_computer ) {
for( int x = 0; x < SEEX; ++x ) {
for( int y = 0; y < SEEY; ++y ) {
if( ter[x][y] == t_console ) {
computers.emplace( point( x, y ), *legacy_computer );
}
}
}
legacy_computer.reset();
}
}
bool submap::has_computer( point p ) const
{
return computers.find( p ) != computers.end() || ( legacy_computer && ter[p.x][p.y] == t_console );
}
const computer *submap::get_computer( point p ) const
{
// the returned object will not get modified (should not, at least), so we
// don't yet need to update to std::map
const auto it = computers.find( p );
if( it != computers.end() ) {
return &it->second;
}
if( legacy_computer && ter[p.x][p.y] == t_console ) {
return legacy_computer.get();
}
return nullptr;
}
computer *submap::get_computer( point p )
{
// need to update to std::map first so modifications to the returned object
// only affects the exact point p
update_legacy_computer();
const auto it = computers.find( p );
if( it != computers.end() ) {
return &it->second;
}
return nullptr;
}
void submap::set_computer( point p, const computer &c )
{
update_legacy_computer();
const auto it = computers.find( p );
if( it != computers.end() ) {
it->second = c;
} else {
computers.emplace( p, c );
}
}
void submap::delete_computer( point p )
{
update_legacy_computer();
computers.erase( p );
}
bool submap::contains_vehicle( vehicle *veh )
{
const auto match = std::find_if(
begin( vehicles ), end( vehicles ),
[veh]( const std::unique_ptr<vehicle> &v ) {
return v.get() == veh;
} );
return match != vehicles.end();
}
void submap::rotate( int turns )
{
turns = turns % 4;
if( turns == 0 ) {
return;
}
const auto rotate_point = [turns]( point p ) {
return p.rotate( turns, { SEEX, SEEY } );
};
if( turns == 2 ) {
// Swap horizontal stripes.
for( int j = 0, je = SEEY / 2; j < je; ++j ) {
for( int i = j, ie = SEEX - j; i < ie; ++i ) {
swap_soa_tile( { i, j }, rotate_point( { i, j } ) );
}
}
// Swap vertical stripes so that they don't overlap with
// the already swapped horizontals.
for( int i = 0, ie = SEEX / 2; i < ie; ++i ) {
for( int j = i + 1, je = SEEY - i - 1; j < je; ++j ) {
swap_soa_tile( { i, j }, rotate_point( { i, j } ) );
}
}
} else {
maptile_soa<1, 1> tmp;
for( int j = 0, je = SEEY / 2; j < je; ++j ) {
for( int i = j, ie = SEEX - j - 1; i < ie; ++i ) {
auto p = point{ i, j };
swap_soa_tile( p, tmp );
for( int k = 0; k < 4; ++k ) {
p = rotate_point( p );
swap_soa_tile( p, tmp );
}
}
}
}
active_items.rotate_locations( turns, { SEEX, SEEY } );
for( auto &elem : cosmetics ) {
elem.pos = rotate_point( elem.pos );
}
for( auto &elem : spawns ) {
elem.pos = rotate_point( elem.pos );
}
for( auto &elem : vehicles ) {
const auto new_pos = rotate_point( elem->pos );
elem->pos = new_pos;
elem->set_facing( elem->turn_dir + turns * 90_degrees );
}
std::map<point, computer> rot_comp;
for( auto &elem : computers ) {
rot_comp.emplace( rotate_point( elem.first ), elem.second );
}
computers = rot_comp;
std::map<point_sm_ms, cata::poly_serialized<active_tile_data>> rot_active_furn;
for( auto &elem : active_furniture ) {
rot_active_furn.emplace( point_sm_ms( rotate_point( elem.first.raw() ) ), elem.second );
}
active_furniture = rot_active_furn;
}