forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem_action.cpp
302 lines (258 loc) · 9.01 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
#include "action.h"
#include "output.h"
#include "options.h"
#include "path_info.h"
#include "debug.h"
#include "game.h"
#include "options.h"
#include "messages.h"
#include "inventory.h"
#include "item_factory.h"
#include "item_action.h"
#include "iuse_actor.h"
#include "translations.h"
#include "input.h"
#include "itype.h"
#include "ui.h"
#include <istream>
#include <sstream>
#include <fstream>
#include <iterator>
static item_action nullaction;
static const std::string errstring("ERROR");
int clamp(int value, int low, int high)
{
return (value < low) ? low : ( (value > high) ? high : value );
}
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 uimenu_callback {
private:
input_context ctxt;
const action_map am;
public:
actmenu_cb( const action_map &acm ) : ctxt("ITEM_ACTIONS"), am( acm ) {
ctxt.register_action("HELP_KEYBINDINGS");
ctxt.register_action("QUIT");
for( const auto &id : am ) {
ctxt.register_action( id.first, id.second.name );
}
}
~actmenu_cb() { }
bool key(int ch, int /*num*/, uimenu * /*menu*/) override {
input_event wrap = input_event( ch, CATA_INPUT_KEYBOARD );
const std::string action = ctxt.input_to_action( wrap );
if( action == "HELP_KEYBINDINGS" ) {
ctxt.display_help();
return true;
}
// Don't write a message if unknown command was sent
// Only when an inexistent tool was selected
auto itemless_action = am.find( action );
if( itemless_action != am.end() ) {
popup( _("You do not have an item that can perform this action.") );
}
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_has_uses_recursive( const item &it )
{
if( !it.type->use_methods.empty() ) {
return true;
}
for( const auto &elem : it.contents ) {
if( item_has_uses_recursive( elem ) ) {
return true;
}
}
return false;
}
item_action_map item_action_generator::map_actions_to_items( player &p ) const
{
std::set< item_action_id > unmapped_actions;
for( auto &p : item_actions ) { // Get ids of wanted actions
unmapped_actions.insert( p.first );
}
item_action_map candidates;
std::vector< item* > items = p.inv_dump();
std::unordered_set< item_action_id > to_remove;
for( item *i : items ) {
if( !item_has_uses_recursive( *i ) ) {
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;
}
it_tool *tool = dynamic_cast<it_tool*>( actual_item->type );
const use_function *ufunc = actual_item->get_use( use );
// Can't just test for charges_per_use > charges, because charges can be -1
if( ufunc == nullptr ||
( ufunc->get_actor_ptr() != nullptr &&
!ufunc->get_actor_ptr()->can_use( &p, actual_item, false, p.pos3() ) ) ||
( tool != nullptr && tool->charges_per_use > 0 &&
tool->charges_per_use > actual_item->charges ) ) {
continue;
}
// Add to usable items if it needs less charges per use or has less charges
auto found = candidates.find( use );
int would_use_charges = tool == nullptr ? 0 : tool->charges_per_use;
bool better = false;
if( found == candidates.end() ) {
better = true;
} else {
it_tool *other = dynamic_cast<it_tool*>(found->second->type);
if( other == nullptr || would_use_charges > other->charges_per_use ) {
continue; // Other item consumes less charges
}
if( found->second->charges > actual_item->charges ) {
better = true; // Items with less charges preferred
}
}
if( better ) {
candidates[use] = i;
if( would_use_charges == 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.c_str());
}
return id;
}
std::string item_action_generator::get_action_name( const iuse_actor *actor ) const
{
if( actor == nullptr ) {
debugmsg( "Tried to get name of a null iuse_actor" );
return errstring;
}
const iuse_transform *trans_actor = nullptr;
trans_actor = dynamic_cast<const iuse_transform *>( actor );
if ( trans_actor != nullptr && !trans_actor->menu_option_text.empty()) {
return _(trans_actor->menu_option_text.c_str());
}
return get_action_name( actor->type );
}
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(JsonObject &jo)
{
item_action ia;
ia.id = jo.get_string( "id" );
ia.name = jo.get_string( "name", "" );
if( !ia.name.empty() ) {
ia.name = _( ia.name.c_str() );
} else {
ia.name = ia.id;
}
item_actions[ia.id] = ia;
}
void game::item_action_menu()
{
const auto &gen = item_action_generator::generator();
const action_map &item_actions = gen.get_item_action_map();
item_action_map iactions = gen.map_actions_to_items( u );
if( iactions.empty() ) {
popup( _("You don't have any items with registered uses") );
}
uimenu kmenu;
kmenu.text = _( "Execute which action?" );
input_context ctxt("ITEM_ACTIONS");
actmenu_cb callback( item_actions );
kmenu.callback = &callback;
int num = 0;
for( auto &p : iactions ) {
it_tool *tool = dynamic_cast<it_tool*>( p.second->type );
int would_use_charges = tool == nullptr ? 0 : tool->charges_per_use;
std::stringstream ss;
ss << _( gen.get_action_name( p.first ).c_str() ) << " [" << p.second->display_name();
if( would_use_charges > 0 ) {
ss << " (" << would_use_charges << '/' << p.second->charges << ')';
}
ss << "]";
char bind = key_bound_to( ctxt, p.first );
kmenu.addentry( num, true, bind, ss.str() );
num++;
}
std::set< item_action_id > itemless;
for( auto &p : item_actions ) {
if( iactions.find( p.first ) == iactions.end() ) {
char bind = key_bound_to( ctxt, p.first );
kmenu.addentry( num, false, bind, _( gen.get_action_name( p.first ).c_str() ) );
num++;
}
}
kmenu.addentry( num, true, key_bound_to( ctxt, "QUIT" ), _("Cancel") );
kmenu.query();
if( kmenu.ret < 0 || kmenu.ret >= (int)iactions.size() ) {
return;
}
draw_ter();
auto iter = iactions.begin();
std::advance( iter, kmenu.ret );
if( u.invoke_item( iter->second, iter->first ) ) {
// Need to remove item
u.i_rem( iter->second );
}
u.inv.restack( &u );
u.inv.unsort();
}
std::string use_function::get_type_name() const
{
switch( function_type ) {
case USE_FUNCTION_CPP:
return item_controller->inverse_get_iuse( this );
case USE_FUNCTION_ACTOR_PTR:
return get_actor_ptr()->type;
case USE_FUNCTION_LUA:
debugmsg( "Tried to get type name of a lua function (not implemented yet)" );
return errstring;
case USE_FUNCTION_NONE:
return errstring;
default:
debugmsg( "Tried to get type name of a badly typed iuse_function." );
return errstring;
}
}
std::string use_function::get_name() const
{
switch( function_type ) {
case USE_FUNCTION_CPP:
return item_action_generator::generator().get_action_name( get_type_name() );
case USE_FUNCTION_ACTOR_PTR:
return item_action_generator::generator().get_action_name( get_actor_ptr());
case USE_FUNCTION_LUA:
return "Lua";
case USE_FUNCTION_NONE:
return "None";
default:
debugmsg( "Tried to get type name of a badly typed iuse_function." );
return errstring;
}
}