forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item_action.cpp
373 lines (316 loc) · 11.2 KB
/
item_action.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
#include "item_action.h"
#include <algorithm>
#include <iterator>
#include <list>
#include <memory>
#include <set>
#include <tuple>
#include <unordered_set>
#include <utility>
#include "avatar.h"
#include "calendar.h"
#include "catacharset.h"
#include "clone_ptr.h"
#include "cursesdef.h"
#include "debug.h"
#include "game.h"
#include "input.h"
#include "inventory.h"
#include "item.h"
#include "item_contents.h"
#include "item_factory.h"
#include "itype.h"
#include "iuse.h"
#include "json.h"
#include "output.h"
#include "player.h"
#include "ret_val.h"
#include "string_formatter.h"
#include "translations.h"
#include "type_id.h"
#include "ui.h"
class Character;
static const std::string errstring( "ERROR" );
static const bionic_id bio_tools( "bio_tools" );
static const bionic_id bio_claws( "bio_claws" );
static const bionic_id bio_claws_weapon( "bio_claws_weapon" );
struct tripoint;
static item_action nullaction;
static char key_bound_to( const input_context &ctxt, const item_action_id &act )
{
auto keys = ctxt.keys_bound_to( act );
return keys.empty() ? '\0' : keys[0];
}
class actmenu_cb : public uilist_callback
{
private:
const action_map am;
public:
actmenu_cb( const action_map &acm ) : am( acm ) { }
~actmenu_cb() override = default;
bool key( const input_context &ctxt, const input_event &event, int /*idx*/,
uilist * /*menu*/ ) override {
const std::string &action = ctxt.input_to_action( event );
// Don't write a message if unknown command was sent
// Only when an inexistent tool was selected
const auto itemless_action = am.find( action );
if( itemless_action != am.end() ) {
popup( _( "You do not have an item that can perform this action." ) );
return true;
}
return false;
}
};
item_action_generator::item_action_generator() = default;
item_action_generator::~item_action_generator() = default;
// Get use methods of this item and its contents
bool item::item_has_uses_recursive() const
{
if( !type->use_methods.empty() ) {
return true;
}
return contents.item_has_uses_recursive();
}
bool item_contents::item_has_uses_recursive() const
{
for( const item &it : items ) {
if( it.item_has_uses_recursive() ) {
return true;
}
}
return false;
}
item_action_map item_action_generator::map_actions_to_items( player &p ) const
{
return map_actions_to_items( p, std::vector<item *>() );
}
item_action_map item_action_generator::map_actions_to_items( player &p,
const std::vector<item *> &pseudos ) const
{
std::set< item_action_id > unmapped_actions;
for( auto &ia_ptr : item_actions ) { // Get ids of wanted actions
unmapped_actions.insert( ia_ptr.first );
}
item_action_map candidates;
std::vector< item * > items = p.inv_dump();
items.reserve( items.size() + pseudos.size() );
items.insert( items.end(), pseudos.begin(), pseudos.end() );
std::unordered_set< item_action_id > to_remove;
for( item *i : items ) {
if( !i->item_has_uses_recursive() ) {
continue;
}
for( const item_action_id &use : unmapped_actions ) {
// Actually used item can be different from the "outside item"
// For example, sheathed knife
item *actual_item = i->get_usable_item( use );
if( actual_item == nullptr ) {
continue;
}
const use_function *func = actual_item->get_use( use );
if( !( func && func->get_actor_ptr() &&
func->get_actor_ptr()->can_use( p, *actual_item, false, p.pos() ).success() ) ) {
continue;
}
if( !actual_item->ammo_sufficient() ) {
continue;
}
// Don't try to remove 'irremovable' toolmods
if( actual_item->is_toolmod() && use == item_action_id( "TOOLMOD_ATTACH" ) &&
actual_item->has_flag( "IRREMOVABLE" ) ) {
continue;
}
// Add to usable items if it needs less charges per use or has less charges
auto found = candidates.find( use );
bool better = false;
if( found == candidates.end() ) {
better = true;
} else {
if( actual_item->ammo_required() > found->second->ammo_required() ) {
continue; // Other item consumes less charges
}
if( found->second->ammo_remaining() > actual_item->ammo_remaining() ) {
better = true; // Items with less charges preferred
}
}
if( better ) {
candidates[use] = i;
if( actual_item->ammo_required() == 0 ) {
to_remove.insert( use );
}
}
}
for( const item_action_id &r : to_remove ) {
unmapped_actions.erase( r );
}
}
return candidates;
}
std::string item_action_generator::get_action_name( const item_action_id &id ) const
{
const auto &act = get_action( id );
if( !act.name.empty() ) {
return act.name.translated();
}
return id;
}
bool item_action_generator::action_exists( const item_action_id &id ) const
{
return item_actions.find( id ) != item_actions.end();
}
const item_action &item_action_generator::get_action( const item_action_id &id ) const
{
const auto &iter = item_actions.find( id );
if( iter != item_actions.end() ) {
return iter->second;
}
debugmsg( "Couldn't find item action named %s", id.c_str() );
return nullaction;
}
void item_action_generator::load_item_action( const JsonObject &jo )
{
item_action ia;
ia.id = jo.get_string( "id" );
if( !jo.read( "name", ia.name ) || ia.name.empty() ) {
ia.name = no_translation( ia.id );
}
item_actions[ia.id] = ia;
}
void item_action_generator::check_consistency() const
{
for( const auto &elem : item_actions ) {
const auto &action = elem.second;
if( !item_controller->has_iuse( action.id ) ) {
debugmsg( "Item action \"%s\" isn't known to the game. Check item action definitions in JSON.",
action.id.c_str() );
}
}
}
void item_action_generator::reset()
{
item_actions.clear();
}
void game::item_action_menu()
{
const auto &gen = item_action_generator::generator();
const action_map &item_actions = gen.get_item_action_map();
// HACK: A bit of a hack for now. If more pseudos get implemented, this should be un-hacked
std::vector<item *> pseudos;
item toolset( "toolset", calendar::turn );
if( u.has_active_bionic( bio_tools ) ) {
pseudos.push_back( &toolset );
}
item bio_claws_item( static_cast<std::string>( bio_claws_weapon ), calendar::turn );
if( u.has_active_bionic( bio_claws ) ) {
pseudos.push_back( &bio_claws_item );
}
item_action_map iactions = gen.map_actions_to_items( u, pseudos );
if( iactions.empty() ) {
popup( _( "You don't have any items with registered uses" ) );
}
uilist kmenu;
kmenu.text = _( "Execute which action?" );
kmenu.input_category = "ITEM_ACTIONS";
input_context ctxt( "ITEM_ACTIONS" );
for( const auto &id : item_actions ) {
ctxt.register_action( id.first, id.second.name );
kmenu.additional_actions.emplace_back( id.first, id.second.name );
}
actmenu_cb callback( item_actions );
kmenu.callback = &callback;
int num = 0;
const auto assigned_action = [&iactions]( const item_action_id & action ) {
return iactions.find( action ) != iactions.end();
};
std::vector<std::tuple<item_action_id, std::string, std::string>> menu_items;
// Sorts menu items by action.
using Iter = decltype( menu_items )::iterator;
const auto sort_menu = []( Iter from, Iter to ) {
std::sort( from, to, []( const std::tuple<item_action_id, std::string, std::string> &lhs,
const std::tuple<item_action_id, std::string, std::string> &rhs ) {
return std::get<1>( lhs ).compare( std::get<1>( rhs ) ) < 0;
} );
};
// Add mapped actions to the menu vector.
std::transform( iactions.begin(), iactions.end(), std::back_inserter( menu_items ),
[]( const std::pair<item_action_id, item *> &elem ) {
std::string ss = elem.second->display_name();
if( elem.second->ammo_required() ) {
ss += string_format( " (%d/%d)", elem.second->ammo_required(), elem.second->ammo_remaining() );
}
const auto method = elem.second->get_use( elem.first );
if( method ) {
return std::make_tuple( method->get_type(), method->get_name(), ss );
} else {
return std::make_tuple( errstring, std::string( "NO USE FUNCTION" ), ss );
}
} );
// Sort mapped actions.
sort_menu( menu_items.begin(), menu_items.end() );
// Add unmapped but binded actions to the menu vector.
for( const auto &elem : item_actions ) {
if( key_bound_to( ctxt, elem.first ) != '\0' && !assigned_action( elem.first ) ) {
menu_items.emplace_back( elem.first, gen.get_action_name( elem.first ), "-" );
}
}
// Sort unmapped actions.
auto iter = menu_items.begin();
std::advance( iter, iactions.size() );
sort_menu( iter, menu_items.end() );
// Determine max lengths, to print the menu nicely.
std::pair<int, int> max_len;
for( const auto &elem : menu_items ) {
max_len.first = std::max( max_len.first, utf8_width( std::get<1>( elem ), true ) );
max_len.second = std::max( max_len.second, utf8_width( std::get<2>( elem ), true ) );
}
// Fill the menu.
for( const auto &elem : menu_items ) {
std::string ss;
ss += std::get<1>( elem );
ss += std::string( max_len.first - utf8_width( std::get<1>( elem ), true ), ' ' );
ss += std::string( 4, ' ' );
ss += std::get<2>( elem );
ss += std::string( max_len.second - utf8_width( std::get<2>( elem ), true ), ' ' );
const char bind = key_bound_to( ctxt, std::get<0>( elem ) );
const bool enabled = assigned_action( std::get<0>( elem ) );
kmenu.addentry( num, enabled, bind, ss );
num++;
}
kmenu.query();
if( kmenu.ret < 0 || kmenu.ret >= static_cast<int>( iactions.size() ) ) {
return;
}
const item_action_id action = std::get<0>( menu_items[kmenu.ret] );
item *it = iactions[action];
u.invoke_item( it, action );
u.inv.restack( u );
u.inv.unsort();
}
std::string use_function::get_type() const
{
if( actor ) {
return actor->type;
} else {
return errstring;
}
}
ret_val<bool> iuse_actor::can_use( const Character &, const item &, bool, const tripoint & ) const
{
return ret_val<bool>::make_success();
}
bool iuse_actor::is_valid() const
{
return item_action_generator::generator().action_exists( type );
}
std::string iuse_actor::get_name() const
{
return item_action_generator::generator().get_action_name( type );
}
std::string use_function::get_name() const
{
if( actor ) {
return actor->get_name();
} else {
return errstring;
}
}