forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itype.cpp
234 lines (202 loc) · 5.01 KB
/
itype.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
#include "itype.h"
#include <cstdlib>
#include "debug.h"
#include "item.h"
#include "player.h"
#include "ret_val.h"
#include "translations.h"
#include "relic.h"
#include "skill.h"
struct tripoint;
std::string gunmod_location::name() const
{
// Yes, currently the name is just the translated id.
return _( _id );
}
namespace io
{
template<>
std::string enum_to_string<condition_type>( condition_type data )
{
switch( data ) {
case condition_type::FLAG:
return "FLAG";
case condition_type::COMPONENT_ID:
return "COMPONENT_ID";
case condition_type::num_condition_types:
break;
}
debugmsg( "Invalid condition_type" );
abort();
}
} // namespace io
itype::itype()
{
melee.fill( 0 );
}
itype::~itype() = default;
int itype::damage_min() const
{
return count_by_charges() ? 0 : damage_min_;
}
int itype::damage_max() const
{
return count_by_charges() ? 0 : damage_max_;
}
std::string itype::get_item_type_string() const
{
if( tool ) {
return "TOOL";
} else if( comestible ) {
return "FOOD";
} else if( container ) {
return "CONTAINER";
} else if( armor ) {
return "ARMOR";
} else if( book ) {
return "BOOK";
} else if( gun ) {
return "GUN";
} else if( bionic ) {
return "BIONIC";
} else if( ammo ) {
return "AMMO";
}
return "misc";
}
std::string itype::nname( unsigned int quantity ) const
{
// Always use singular form for liquids.
// (Maybe gases too? There are no gases at the moment)
if( phase == LIQUID ) {
quantity = 1;
}
return name.translated( quantity );
}
const itype_id &itype::get_id() const
{
return id;
}
bool itype::count_by_charges() const
{
return stackable_ || ammo || comestible;
}
int itype::charges_default() const
{
if( tool ) {
return tool->def_charges;
} else if( comestible ) {
return comestible->def_charges;
} else if( ammo ) {
return ammo->def_charges;
}
return count_by_charges() ? 1 : 0;
}
int itype::charges_to_use() const
{
if( tool ) {
return static_cast<int>( tool->charges_per_use );
}
return 1;
}
int itype::charge_factor() const
{
return tool ? tool->charge_factor : 1;
}
int itype::maximum_charges() const
{
if( tool ) {
return tool->max_charges;
}
return 0;
}
int itype::charges_per_volume( const units::volume &vol ) const
{
if( volume == 0_ml ) {
// TODO: items should not have 0 volume at all!
return item::INFINITE_CHARGES;
}
return ( count_by_charges() ? stack_size : 1 ) * vol / volume;
}
// Members of iuse struct, which is slowly morphing into a class.
bool itype::has_use() const
{
return !use_methods.empty();
}
bool itype::has_flag( const std::string &flag ) const
{
return item_tags.count( flag );
}
bool itype::has_flag( const flag_str_id &flag ) const
{
return item_tags.count( flag.str() );
}
const itype::FlagsSetType &itype::get_flags() const
{
return item_tags;
}
bool itype::can_use( const std::string &iuse_name ) const
{
return get_use( iuse_name ) != nullptr;
}
const use_function *itype::get_use( const std::string &iuse_name ) const
{
const auto iter = use_methods.find( iuse_name );
return iter != use_methods.end() ? &iter->second : nullptr;
}
void itype::tick( player &p, item &it, const tripoint &pos ) const
{
// Maybe should move charge decrementing here?
for( auto &method : use_methods ) {
method.second.call( p, it, true, pos );
}
}
int itype::invoke( player &p, item &it, const tripoint &pos ) const
{
if( !has_use() ) {
return 0;
}
return invoke( p, it, pos, use_methods.begin()->first );
}
int itype::invoke( player &p, item &it, const tripoint &pos, const std::string &iuse_name ) const
{
const use_function *use = get_use( iuse_name );
if( use == nullptr ) {
debugmsg( "Tried to invoke %s on a %s, which doesn't have this use_function",
iuse_name, nname( 1 ) );
return 0;
}
const auto ret = use->can_call( p, it, false, pos );
if( !ret.success() ) {
p.add_msg_if_player( m_info, ret.str() );
return 0;
}
// used for grenades and such, to increase kill count
// invoke is called a first time with transform, when the explosive item is activated
// then a second time with draw explosion
// the player responsible of the explosion is the one that activated the object
if( iuse_name == "transform" ) {
it.activated_by = p.get_safe_reference();
}
return use->call( p, it, false, pos );
}
std::string gun_type_type::name() const
{
return pgettext( "gun_type_type", name_.c_str() );
}
bool itype::can_have_charges() const
{
if( count_by_charges() ) {
return true;
}
if( tool && tool->max_charges > 0 ) {
return true;
}
if( gun && gun->clip > 0 ) {
return true;
}
if( has_flag( "CAN_HAVE_CHARGES" ) ) {
return true;
}
return false;
}