forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
advanced_inv_area.cpp
490 lines (460 loc) · 16.4 KB
/
advanced_inv_area.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#include <cstddef>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <set>
#include <unordered_map>
#include <utility>
#include "advanced_inv_area.h"
#include "advanced_inv_listitem.h"
#include "avatar.h"
#include "cata_assert.h"
#include "character.h"
#include "enums.h"
#include "field.h"
#include "field_type.h"
#include "game_constants.h"
#include "inventory.h"
#include "item.h"
#include "map.h"
#include "map_selector.h"
#include "mapdata.h"
#include "optional.h"
#include "pimpl.h"
#include "translations.h"
#include "trap.h"
#include "type_id.h"
#include "uistate.h"
#include "units.h"
#include "veh_type.h"
#include "vehicle.h"
#include "vehicle_selector.h"
#include "vpart_position.h"
int advanced_inv_area::get_item_count() const
{
Character &player_character = get_player_character();
if( id == AIM_INVENTORY ) {
return player_character.inv->size();
} else if( id == AIM_WORN ) {
return player_character.worn.size();
} else if( id == AIM_ALL ) {
return 0;
} else if( id == AIM_DRAGGED ) {
return can_store_in_vehicle() ? veh->get_items( vstor ).size() : 0;
} else {
return get_map().i_at( pos ).size();
}
}
advanced_inv_area::advanced_inv_area( aim_location id, const point &h, tripoint off,
const std::string &name, const std::string &shortname,
std::string minimapname, std::string actionname,
aim_location relative_location ) :
id( id ), hscreen( h ),
off( off ), name( name ), shortname( shortname ),
vstor( -1 ), volume( 0_ml ),
weight( 0_gram ), minimapname( minimapname ), actionname( actionname ),
relative_location( relative_location )
{
}
void advanced_inv_area::init()
{
avatar &player_character = get_avatar();
pos = player_character.pos() + off;
veh = nullptr;
vstor = -1;
// must update in main function
volume = 0_ml;
volume_veh = 0_ml;
// must update in main function
weight = 0_gram;
weight_veh = 0_gram;
map &here = get_map();
switch( id ) {
case AIM_INVENTORY:
case AIM_WORN:
canputitemsloc = true;
break;
case AIM_DRAGGED:
if( player_character.get_grab_type() != object_type::VEHICLE ) {
canputitemsloc = false;
desc[0] = _( "Not dragging any vehicle!" );
break;
}
// offset for dragged vehicles is not statically initialized, so get it
off = player_character.grab_point;
// Reset position because offset changed
pos = player_character.pos() + off;
if( const cata::optional<vpart_reference> vp = here.veh_at( pos ).part_with_feature( "CARGO",
false ) ) {
veh = &vp->vehicle();
vstor = vp->part_index();
} else {
veh = nullptr;
vstor = -1;
}
if( vstor >= 0 ) {
desc[0] = veh->name;
canputitemsloc = true;
max_size = MAX_ITEM_IN_VEHICLE_STORAGE;
} else {
veh = nullptr;
canputitemsloc = false;
desc[0] = _( "No dragged vehicle!" );
}
break;
case AIM_CONTAINER:
// set container position based on location
set_container_position();
// location always valid, actual check is done in canputitems()
// and depends on selected item in pane (if it is valid container)
canputitemsloc = true;
if( !get_container() ) {
desc[0] = _( "Invalid container!" );
}
break;
case AIM_ALL:
desc[0] = _( "All 9 squares" );
canputitemsloc = true;
break;
case AIM_SOUTHWEST:
case AIM_SOUTH:
case AIM_SOUTHEAST:
case AIM_WEST:
case AIM_CENTER:
case AIM_EAST:
case AIM_NORTHWEST:
case AIM_NORTH:
case AIM_NORTHEAST: {
const cata::optional<vpart_reference> vp =
here.veh_at( pos ).part_with_feature( "CARGO", false );
if( vp ) {
veh = &vp->vehicle();
vstor = vp->part_index();
} else {
veh = nullptr;
vstor = -1;
}
canputitemsloc = can_store_in_vehicle() || here.can_put_items_ter_furn( pos );
max_size = MAX_ITEM_IN_SQUARE;
if( can_store_in_vehicle() ) {
std::string part_name = vp->info().name();
desc[1] = vp->get_label().value_or( part_name );
}
// get graffiti or terrain name
desc[0] = here.has_graffiti_at( pos ) ?
here.graffiti_at( pos ) : here.name( pos );
}
default:
break;
}
/* assemble a list of interesting traits of the target square */
// fields? with a special case for fire
bool danger_field = false;
const field &tmpfld = here.field_at( pos );
for( const auto &fld : tmpfld ) {
const field_entry &cur = fld.second;
if( fld.first.obj().has_fire ) {
flags.append( _( " <color_white_red>FIRE</color>" ) );
} else {
if( cur.is_dangerous() ) {
danger_field = true;
}
}
}
if( danger_field ) {
flags.append( _( " DANGER" ) );
}
// trap?
const trap &tr = here.tr_at( pos );
if( tr.can_see( pos, player_character ) && !tr.is_benign() ) {
flags.append( _( " TRAP" ) );
}
// water?
if( here.has_flag_ter( ter_furn_flag::TFLAG_SHALLOW_WATER, pos ) ||
here.has_flag_ter( ter_furn_flag::TFLAG_DEEP_WATER, pos ) ) {
flags.append( _( " WATER" ) );
}
// remove leading space
if( !flags.empty() && flags[0] == ' ' ) {
flags.erase( 0, 1 );
}
}
units::volume advanced_inv_area::free_volume( bool in_vehicle ) const
{
// should be a specific location instead
cata_assert( id != AIM_ALL );
if( id == AIM_INVENTORY || id == AIM_WORN ) {
return get_player_character().free_space();
}
return in_vehicle ? veh->free_volume( vstor ) : get_map().free_volume( pos );
}
bool advanced_inv_area::is_same( const advanced_inv_area &other ) const
{
// All locations (sans the below) are compared by the coordinates,
// e.g. dragged vehicle (to the south) and AIM_SOUTH are the same.
if( id != AIM_INVENTORY && other.id != AIM_INVENTORY &&
id != AIM_WORN && other.id != AIM_WORN &&
id != AIM_CONTAINER && other.id != AIM_CONTAINER ) {
// have a vehicle?... ...do the cargo index and pos match?... ...at least pos?
return veh == other.veh ? pos == other.pos && vstor == other.vstor : pos == other.pos;
}
// ...is the id?
return id == other.id;
}
bool advanced_inv_area::canputitems( const advanced_inv_listitem *advitem )
{
bool canputitems = false;
bool from_vehicle = false;
switch( id ) {
case AIM_CONTAINER: {
item_location it;
if( advitem != nullptr ) {
it = advitem->items.front();
from_vehicle = advitem->from_vehicle;
}
if( get_container( from_vehicle ) ) {
it = get_container( from_vehicle );
}
if( it ) {
canputitems = it->is_watertight_container();
}
break;
}
default:
canputitems = canputitemsloc;
break;
}
return canputitems;
}
item_location advanced_inv_area::get_container( bool in_vehicle )
{
item_location container;
Character &player_character = get_player_character();
if( uistate.adv_inv_container_location != -1 ) {
// try to find valid container in the area
if( uistate.adv_inv_container_location == AIM_INVENTORY ) {
const invslice &stacks = player_character.inv->slice();
// check index first
if( stacks.size() > static_cast<size_t>( uistate.adv_inv_container_index ) ) {
auto &it = stacks[uistate.adv_inv_container_index]->front();
if( is_container_valid( &it ) ) {
container = item_location( player_character, &it );
}
}
// try entire area
if( !container ) {
for( size_t x = 0; x < stacks.size(); ++x ) {
item &it = stacks[x]->front();
if( is_container_valid( &it ) ) {
container = item_location( player_character, &it );
uistate.adv_inv_container_index = x;
break;
}
}
}
} else if( uistate.adv_inv_container_location == AIM_WORN ) {
std::list<item> &worn = player_character.worn;
size_t idx = static_cast<size_t>( uistate.adv_inv_container_index );
if( worn.size() > idx ) {
auto iter = worn.begin();
std::advance( iter, idx );
if( is_container_valid( &*iter ) ) {
container = item_location( player_character, &*iter );
}
}
// no need to reinvent the wheel
if( !container ) {
auto iter = worn.begin();
for( size_t i = 0; i < worn.size(); ++i, ++iter ) {
if( is_container_valid( &*iter ) ) {
container = item_location( player_character, &*iter );
uistate.adv_inv_container_index = i;
break;
}
}
}
} else {
map &here = get_map();
bool is_in_vehicle = veh &&
( uistate.adv_inv_container_in_vehicle || ( can_store_in_vehicle() && in_vehicle ) );
const itemstack &stacks = is_in_vehicle ?
i_stacked( veh->get_items( vstor ) ) :
i_stacked( here.i_at( pos ) );
// check index first
if( stacks.size() > static_cast<size_t>( uistate.adv_inv_container_index ) ) {
item *it = stacks[uistate.adv_inv_container_index].front();
if( is_container_valid( it ) ) {
if( is_in_vehicle ) {
container = item_location( vehicle_cursor( *veh, vstor ), it );
} else {
container = item_location( map_cursor( pos ), it );
}
}
}
// try entire area
if( !container ) {
for( size_t x = 0; x < stacks.size(); ++x ) {
item *it = stacks[x].front();
if( is_container_valid( it ) ) {
if( is_in_vehicle ) {
container = item_location( vehicle_cursor( *veh, vstor ), it );
} else {
container = item_location( map_cursor( pos ), it );
}
uistate.adv_inv_container_index = x;
break;
}
}
}
}
// no valid container in the area, resetting container
if( !container ) {
set_container( nullptr );
desc[0] = _( "Invalid container" );
}
}
return container;
}
void advanced_inv_area::set_container( const advanced_inv_listitem *advitem )
{
if( advitem != nullptr ) {
const item_location &it = advitem->items.front();
uistate.adv_inv_container_location = advitem->area;
uistate.adv_inv_container_in_vehicle = advitem->from_vehicle;
uistate.adv_inv_container_index = advitem->idx;
uistate.adv_inv_container_type = it->typeId();
uistate.adv_inv_container_content_type = !it->is_container_empty() ? it->legacy_front().typeId() :
itype_id::NULL_ID();
set_container_position();
} else {
uistate.adv_inv_container_location = -1;
uistate.adv_inv_container_index = 0;
uistate.adv_inv_container_in_vehicle = false;
uistate.adv_inv_container_type = itype_id::NULL_ID();
uistate.adv_inv_container_content_type = itype_id::NULL_ID();
}
}
bool advanced_inv_area::is_container_valid( const item *it ) const
{
if( it != nullptr ) {
if( it->typeId() == uistate.adv_inv_container_type ) {
if( it->is_container_empty() ) {
if( uistate.adv_inv_container_content_type.is_null() ) {
return true;
}
} else {
if( it->legacy_front().typeId() == uistate.adv_inv_container_content_type ) {
return true;
}
}
}
}
return false;
}
static tripoint aim_vector( aim_location id )
{
switch( id ) {
case AIM_SOUTHWEST:
return tripoint_south_west;
case AIM_SOUTH:
return tripoint_south;
case AIM_SOUTHEAST:
return tripoint_south_east;
case AIM_WEST:
return tripoint_west;
case AIM_EAST:
return tripoint_east;
case AIM_NORTHWEST:
return tripoint_north_west;
case AIM_NORTH:
return tripoint_north;
case AIM_NORTHEAST:
return tripoint_north_east;
default:
return tripoint_zero;
}
}
void advanced_inv_area::set_container_position()
{
avatar &player_character = get_avatar();
// update the offset of the container based on location
if( uistate.adv_inv_container_location == AIM_DRAGGED ) {
off = player_character.grab_point;
} else {
off = aim_vector( static_cast<aim_location>( uistate.adv_inv_container_location ) );
}
// update the absolute position
pos = player_character.pos() + off;
// update vehicle information
if( const cata::optional<vpart_reference> vp = get_map().veh_at( pos ).part_with_feature( "CARGO",
false ) ) {
veh = &vp->vehicle();
vstor = vp->part_index();
} else {
veh = nullptr;
vstor = -1;
}
}
aim_location advanced_inv_area::offset_to_location() const
{
static aim_location loc_array[3][3] = {
{AIM_NORTHWEST, AIM_NORTH, AIM_NORTHEAST},
{AIM_WEST, AIM_CENTER, AIM_EAST},
{AIM_SOUTHWEST, AIM_SOUTH, AIM_SOUTHEAST}
};
return loc_array[off.y + 1][off.x + 1];
}
bool advanced_inv_area::can_store_in_vehicle() const
{
// disallow for non-valid vehicle locations
if( id > AIM_DRAGGED || id < AIM_SOUTHWEST ) {
return false;
}
// Prevent AIM access to activated dishwasher, washing machine, or autoclave.
if( veh != nullptr && vstor >= 0 ) {
return !veh->part( vstor ).is_cleaner_on();
} else {
return false;
}
}
template <typename T>
advanced_inv_area::itemstack advanced_inv_area::i_stacked( T items )
{
//create a new container for our stacked items
advanced_inv_area::itemstack stacks;
// used to recall indices we stored `itype_id' item at in itemstack
std::unordered_map<itype_id, std::set<int>> cache;
// iterate through and create stacks
for( auto &elem : items ) {
const auto id = elem.typeId();
auto iter = cache.find( id );
bool got_stacked = false;
// cache entry exists
if( iter != cache.end() ) {
// check to see if it stacks with each item in a stack, not just front()
for( auto &idx : iter->second ) {
for( auto &it : stacks[idx] ) {
if( ( got_stacked = it->display_stacked_with( elem ) ) ) {
stacks[idx].push_back( &elem );
break;
}
}
if( got_stacked ) {
break;
}
}
}
if( !got_stacked ) {
cache[id].insert( stacks.size() );
stacks.push_back( { &elem } );
}
}
return stacks;
}
// instantiate the template
template
advanced_inv_area::itemstack advanced_inv_area::i_stacked<vehicle_stack>( vehicle_stack items );
template
advanced_inv_area::itemstack advanced_inv_area::i_stacked<map_stack>( map_stack items );