forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pickup.cpp
427 lines (385 loc) · 15 KB
/
pickup.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
#include "pickup.h"
#include <algorithm>
#include <cstddef>
#include <functional>
#include <iosfwd>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "activity_actor_definitions.h"
#include "auto_pickup.h"
#include "cata_utility.h"
#include "catacharset.h"
#include "character.h"
#include "colony.h"
#include "color.h"
#include "cursesdef.h"
#include "debug.h"
#include "enums.h"
#include "game.h"
#include "input.h"
#include "item.h"
#include "item_location.h"
#include "item_search.h"
#include "item_stack.h"
#include "line.h"
#include "map.h"
#include "map_selector.h"
#include "mapdata.h"
#include "messages.h"
#include "optional.h"
#include "options.h"
#include "output.h"
#include "panels.h"
#include "player_activity.h"
#include "point.h"
#include "popup.h"
#include "ret_val.h"
#include "sdltiles.h"
#include "string_formatter.h"
#include "string_input_popup.h"
#include "translations.h"
#include "type_id.h"
#include "ui.h"
#include "ui_manager.h"
#include "units.h"
#include "units_utility.h"
#include "vehicle.h"
#include "vehicle_selector.h"
#include "vpart_position.h"
using ItemCount = std::pair<item, int>;
using PickupMap = std::map<std::string, ItemCount>;
static const zone_type_id zone_type_NO_AUTO_PICKUP( "NO_AUTO_PICKUP" );
//helper function for Pickup::autopickup
static void show_pickup_message( const PickupMap &mapPickup )
{
for( const auto &entry : mapPickup ) {
if( entry.second.first.invlet != 0 ) {
add_msg( _( "You pick up: %d %s [%c]" ), entry.second.second,
entry.second.first.display_name( entry.second.second ), entry.second.first.invlet );
} else if( entry.second.first.count_by_charges() ) {
add_msg( _( "You pick up: %s" ), entry.second.first.display_name( entry.second.second ) );
} else {
add_msg( _( "You pick up: %d %s" ), entry.second.second,
entry.second.first.display_name( entry.second.second ) );
}
}
}
struct pickup_count {
bool pick = false;
//count is 0 if the whole stack is being picked up, nonzero otherwise.
int count = 0;
};
enum pickup_answer : int {
CANCEL = -1,
SPILL,
STASH,
NUM_ANSWERS
};
static pickup_answer handle_problematic_pickup( const item &it, const std::string &explain )
{
Character &u = get_player_character();
uilist amenu;
amenu.text = explain;
if( it.is_bucket_nonempty() ) {
amenu.addentry( SPILL, u.can_stash( it ), 's', _( "Spill contents of %s, then pick up %s" ),
it.tname(), it.display_name() );
}
amenu.query();
int choice = amenu.ret;
if( choice <= CANCEL || choice >= NUM_ANSWERS ) {
return CANCEL;
}
return static_cast<pickup_answer>( choice );
}
bool Pickup::query_thief()
{
Character &u = get_player_character();
const bool force_uc = get_option<bool>( "FORCE_CAPITAL_YN" );
const auto &allow_key = force_uc ? input_context::disallow_lower_case_or_non_modified_letters
: input_context::allow_all_keys;
std::string answer = query_popup()
.preferred_keyboard_mode( keyboard_mode::keycode )
.allow_cancel( false )
.context( "YES_NO_ALWAYS_NEVER" )
.message( "%s", force_uc && !is_keycode_mode_supported()
? _( "Picking up this item will be considered stealing, continue? (Case sensitive)" )
: _( "Picking up this item will be considered stealing, continue?" ) )
.option( "YES", allow_key ) // yes, steal all items in this location that is selected
.option( "NO", allow_key ) // no, pick up only what is free
.option( "ALWAYS", allow_key ) // Yes, steal all items and stop asking me this question
.option( "NEVER", allow_key ) // no, only grab free item and never ask me again
.cursor( 1 ) // default to the second option `NO`
.query()
.action; // retrieve the input action
if( answer == "YES" ) {
u.set_value( "THIEF_MODE", "THIEF_STEAL" );
u.set_value( "THIEF_MODE_KEEP", "NO" );
return true;
} else if( answer == "NO" ) {
u.set_value( "THIEF_MODE", "THIEF_HONEST" );
u.set_value( "THIEF_MODE_KEEP", "NO" );
return false;
} else if( answer == "ALWAYS" ) {
u.set_value( "THIEF_MODE", "THIEF_STEAL" );
u.set_value( "THIEF_MODE_KEEP", "YES" );
return true;
} else if( answer == "NEVER" ) {
u.set_value( "THIEF_MODE", "THIEF_HONEST" );
u.set_value( "THIEF_MODE_KEEP", "YES" );
return false;
} else {
// error
debugmsg( "Not a valid option [ %s ]", answer );
}
return false;
}
// Returns false if pickup caused a prompt and the player selected to cancel pickup
static bool pick_one_up( item_location &loc, int quantity, bool &got_water, PickupMap &mapPickup,
bool autopickup, bool &stash_successful )
{
Character &player_character = get_player_character();
int moves_taken = loc.obtain_cost( player_character, quantity );
bool picked_up = false;
pickup_answer option = CANCEL;
// We already checked in do_pickup if this was a nullptr
// Make copies so the original remains untouched if we bail out
item_location newloc = loc;
//original item reference
item &it = *newloc.get_item();
//new item (copy)
item newit = it;
if( !newit.is_owned_by( player_character, true ) ) {
// Has the player given input on if stealing is ok?
if( player_character.get_value( "THIEF_MODE" ) == "THIEF_ASK" ) {
Pickup::query_thief();
}
if( player_character.get_value( "THIEF_MODE" ) == "THIEF_HONEST" ) {
return true; // Since we are honest, return no problem before picking up
}
}
if( newit.invlet != '\0' &&
player_character.invlet_to_item( newit.invlet ) != nullptr ) {
// Existing invlet is not re-usable, remove it and let the code in player.cpp/inventory.cpp
// add a new invlet, otherwise keep the (usable) invlet.
newit.invlet = '\0';
}
// Handle charges, quantity == 0 means move all
if( quantity != 0 && newit.count_by_charges() ) {
if( newit.charges > quantity ) {
newit.charges = quantity;
}
}
bool did_prompt = false;
if( newit.is_frozen_liquid() ) {
if( !( got_water = !player_character.crush_frozen_liquid( newloc ) ) ) {
option = STASH;
}
} else if( newit.made_of_from_type( phase_id::LIQUID ) && !newit.is_frozen_liquid() ) {
got_water = true;
} else if( !player_character.can_pickWeight_partial( newit, false ) ||
!player_character.can_stash_partial( newit, false ) ) {
option = CANCEL;
stash_successful = false;
} else if( newit.is_bucket_nonempty() ) {
if( !autopickup ) {
const std::string &explain = string_format( _( "Can't stash %s while it's not empty" ),
newit.display_name() );
option = handle_problematic_pickup( newit, explain );
did_prompt = true;
} else {
option = CANCEL;
}
} else {
option = STASH;
}
switch( option ) {
case NUM_ANSWERS:
// Some other option
break;
case CANCEL:
picked_up = false;
break;
case SPILL:
if( newit.is_container_empty() ) {
debugmsg( "Tried to spill contents from an empty container" );
break;
}
//using original item, possibly modifying it
picked_up = it.spill_contents( player_character );
if( !picked_up ) {
break;
} else {
const char invlet = newit.invlet;
newit = it;
newit.invlet = invlet;
}
// Intentional fallthrough
case STASH: {
item_location added_it = player_character.i_add( newit, true, nullptr, &it,
/*allow_drop=*/false, /*allow_wield=*/false, false );
if( added_it == item_location::nowhere ) {
// failed to add, fill pockets if it's a stack
if( newit.count_by_charges() ) {
int remaining_charges = newit.charges;
item_location carried_item = player_character.get_wielded_item();
if( carried_item && !carried_item->has_pocket_type( item_pocket::pocket_type::MAGAZINE ) &&
carried_item->can_contain_partial( newit ) ) {
int used_charges = carried_item->fill_with( newit, remaining_charges, false, false, false );
remaining_charges -= used_charges;
}
player_character.worn.pickup_stash( newit, remaining_charges, false );
newit.charges -= remaining_charges;
newit.on_pickup( player_character );
if( newit.charges != 0 ) {
auto &entry = mapPickup[newit.tname()];
entry.second += newit.charges;
entry.first = newit;
picked_up = true;
}
}
} else if( &*added_it == &it ) {
// merged to the original stack, restore original charges
it.charges -= newit.charges;
} else {
// successfully added
auto &entry = mapPickup[newit.tname()];
entry.second += newit.count();
entry.first = newit;
picked_up = true;
}
break;
}
}
if( picked_up ) {
contents_change_handler handler;
handler.unseal_pocket_containing( loc );
item &orig_it = *loc.get_item();
// Subtract moved charges instead of assigning leftover charges,
// since the total charges of the original item may have changed
// due to merging.
if( orig_it.charges > newit.charges ) {
orig_it.charges -= newit.charges;
} else {
loc.remove_item();
}
player_character.moves -= moves_taken;
player_character.flag_encumbrance();
player_character.invalidate_weight_carried_cache();
}
return picked_up || !did_prompt;
}
bool Pickup::do_pickup( std::vector<item_location> &targets, std::vector<int> &quantities,
bool autopickup, bool &stash_successful )
{
bool got_water = false;
Character &player_character = get_player_character();
bool weight_is_okay = ( player_character.weight_carried() <= player_character.weight_capacity() );
// Map of items picked up so we can output them all at the end and
// merge dropping items with the same name.
PickupMap mapPickup;
bool problem = false;
while( !problem && player_character.moves >= 0 && !targets.empty() ) {
item_location target = std::move( targets.back() );
int quantity = quantities.back();
// Whether we pick the item up or not, we're done trying to do so,
// so remove it from the list.
targets.pop_back();
quantities.pop_back();
if( !target ) {
debugmsg( "lost target item of ACT_PICKUP" );
continue;
}
problem = !pick_one_up( target, quantity, got_water, mapPickup, autopickup, stash_successful );
}
if( !mapPickup.empty() ) {
show_pickup_message( mapPickup );
}
if( got_water ) {
add_msg( m_info, _( "Spilt liquid cannot be picked back up. Try mopping it instead." ) );
}
if( weight_is_okay && player_character.weight_carried() > player_character.weight_capacity() ) {
add_msg( m_bad, _( "You're overburdened!" ) );
}
player_character.recoil = MAX_RECOIL;
return !problem;
}
// Auto pickup items at given location
void Pickup::autopickup( const tripoint &p )
{
map &local = get_map();
if( local.i_at( p ).empty() && !get_option<bool>( "AUTO_PICKUP_ADJACENT" ) ) {
return;
}
// which items are we grabbing?
std::vector<item_stack::iterator> here;
const map_stack mapitems = local.i_at( p );
for( item_stack::iterator it = mapitems.begin(); it != mapitems.end(); ++it ) {
here.push_back( it );
}
Character &player = get_player_character();
// Recursively pick up adjacent items if that option is on.
if( get_option<bool>( "AUTO_PICKUP_ADJACENT" ) && player.pos() == p ) {
//Autopickup adjacent
direction adjacentDir[8] = {
direction::NORTH, direction::NORTHEAST, direction::EAST,
direction::SOUTHEAST, direction::SOUTH, direction::SOUTHWEST,
direction::WEST, direction::NORTHWEST
};
for( direction &elem : adjacentDir ) {
tripoint apos = tripoint( displace_XY( elem ), 0 );
apos += p;
autopickup( apos );
}
}
// Bail out if this square cannot be auto-picked-up
if( g->check_zone( zone_type_NO_AUTO_PICKUP, p ) ||
local.has_flag( ter_furn_flag::TFLAG_SEALED, p ) ) {
return;
}
drop_locations selected_items = auto_pickup::select_items( here, p );
if( selected_items.empty() ) {
return;
}
// At this point we've selected our items, register an activity to pick them up.
std::vector<int> quantities;
std::vector<item_location> target_items;
for( drop_location selected : selected_items ) {
item *it = selected.first.get_item();
target_items.push_back( selected.first );
quantities.push_back( it->count_by_charges() ? it->charges : 0 );
}
pickup_activity_actor actor = pickup_activity_actor( target_items, quantities, player.pos(), true );
player.assign_activity( player_activity( actor ) );
// Auto pickup will need to auto resume since there can be several of them on the stack.
player.activity.auto_resume = true;
}
int Pickup::cost_to_move_item( const Character &who, const item &it )
{
// Do not involve inventory capacity, it's not like you put it in backpack
int ret = 50;
if( who.is_armed() ) {
// No free hand? That will cost you extra
ret += 20;
}
const int delta_weight = units::to_gram( it.weight() - who.weight_capacity() );
// Is it too heavy? It'll take 10 moves per kg over limit
ret += std::max( 0, delta_weight / 100 );
// Keep it sane - it's not a long activity
return std::min( 400, ret );
}
std::vector<Pickup::pickup_rect> Pickup::pickup_rect::list;
Pickup::pickup_rect *Pickup::pickup_rect::find_by_coordinate( const point &p )
{
for( pickup_rect &rect : pickup_rect::list ) {
if( rect.contains( p ) ) {
return ▭
}
}
return nullptr;
}