-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
material.cpp
317 lines (264 loc) · 7.72 KB
/
material.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
#include "material.h"
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <set>
#include "assign.h"
#include "damage.h" // damage_type
#include "debug.h"
#include "generic_factory.h"
#include "item.h"
#include "json.h"
#include "translations.h"
#include "player.h"
#include "field.h"
namespace
{
generic_factory<material_type> material_data( "material", "ident" );
} // namespace
/** @relates string_id */
template<>
bool string_id<material_type>::is_valid() const
{
return material_data.is_valid( *this );
}
/** @relates string_id */
template<>
const material_type &string_id<material_type>::obj() const
{
return material_data.obj( *this );
}
material_type::material_type() :
id( material_id::NULL_ID() ),
_bash_dmg_verb( translate_marker( "damages" ) ),
_cut_dmg_verb( translate_marker( "damages" ) )
{
_dmg_adj = { translate_marker( "lightly damaged" ), translate_marker( "damaged" ), translate_marker( "very damaged" ), translate_marker( "thoroughly damaged" ) };
}
static mat_burn_data load_mat_burn_data( const JsonObject &jsobj )
{
mat_burn_data bd;
assign( jsobj, "immune", bd.immune );
assign( jsobj, "volume_per_turn", bd.volume_per_turn );
jsobj.read( "fuel", bd.fuel );
jsobj.read( "smoke", bd.smoke );
jsobj.read( "burn", bd.burn );
return bd;
}
void material_type::load( const JsonObject &jsobj, const std::string & )
{
mandatory( jsobj, was_loaded, "name", _name );
mandatory( jsobj, was_loaded, "bash_resist", _bash_resist );
mandatory( jsobj, was_loaded, "cut_resist", _cut_resist );
mandatory( jsobj, was_loaded, "acid_resist", _acid_resist );
mandatory( jsobj, was_loaded, "elec_resist", _elec_resist );
mandatory( jsobj, was_loaded, "fire_resist", _fire_resist );
mandatory( jsobj, was_loaded, "chip_resist", _chip_resist );
mandatory( jsobj, was_loaded, "density", _density );
optional( jsobj, was_loaded, "specific_heat_liquid", _specific_heat_liquid );
optional( jsobj, was_loaded, "specific_heat_solid", _specific_heat_solid );
optional( jsobj, was_loaded, "latent_heat", _latent_heat );
optional( jsobj, was_loaded, "freeze_point", _freeze_point );
assign( jsobj, "salvaged_into", _salvaged_into );
optional( jsobj, was_loaded, "repaired_with", _repaired_with, "null" );
optional( jsobj, was_loaded, "edible", _edible, false );
optional( jsobj, was_loaded, "rotting", _rotting, false );
optional( jsobj, was_loaded, "soft", _soft, false );
optional( jsobj, was_loaded, "reinforces", _reinforces, false );
for( JsonArray pair : jsobj.get_array( "vitamins" ) ) {
_vitamins.emplace( vitamin_id( pair.get_string( 0 ) ), pair.get_float( 1 ) );
}
mandatory( jsobj, was_loaded, "bash_dmg_verb", _bash_dmg_verb );
mandatory( jsobj, was_loaded, "cut_dmg_verb", _cut_dmg_verb );
mandatory( jsobj, was_loaded, "dmg_adj", _dmg_adj, string_reader() );
if( jsobj.has_array( "burn_data" ) ) {
for( JsonObject brn : jsobj.get_array( "burn_data" ) ) {
_burn_data.emplace_back( load_mat_burn_data( brn ) );
}
} else {
// If not specified, supply default
mat_burn_data mbd;
if( _fire_resist <= 0 ) {
mbd.burn = 1;
}
_burn_data.emplace_back( mbd );
}
for( JsonArray pair : jsobj.get_array( "burn_products" ) ) {
_burn_products.emplace_back( pair.get_string( 0 ), static_cast< float >( pair.get_float( 1 ) ) );
}
optional( jsobj, was_loaded, "compact_accepts", _compact_accepts,
auto_flags_reader<material_id>() );
optional( jsobj, was_loaded, "compacts_into", _compacts_into, string_reader() );
}
void material_type::check() const
{
if( name().empty() ) {
debugmsg( "material %s has no name.", id.c_str() );
}
if( _dmg_adj.size() < 4 ) {
debugmsg( "material %s specifies insufficient damaged adjectives.", id.c_str() );
}
if( _salvaged_into && ( !item::type_is_defined( *_salvaged_into ) || *_salvaged_into == "null" ) ) {
debugmsg( "invalid \"salvaged_into\" %s for %s.", _salvaged_into->c_str(), id.c_str() );
}
if( !item::type_is_defined( _repaired_with ) ) {
debugmsg( "invalid \"repaired_with\" %s for %s.", _repaired_with.c_str(), id.c_str() );
}
for( auto &ca : _compact_accepts ) {
if( !ca.is_valid() ) {
debugmsg( "invalid \"compact_accepts\" %s for %s.", ca.c_str(), id.c_str() );
}
}
for( auto &ci : _compacts_into ) {
if( !item::type_is_defined( ci ) || !item( ci, 0 ).only_made_of( std::set<material_id> { id } ) ) {
debugmsg( "invalid \"compacts_into\" %s for %s.", ci.c_str(), id.c_str() );
}
}
}
material_id material_type::ident() const
{
return id;
}
std::string material_type::name() const
{
return _( _name );
}
cata::optional<itype_id> material_type::salvaged_into() const
{
return _salvaged_into;
}
itype_id material_type::repaired_with() const
{
return _repaired_with;
}
int material_type::bash_resist() const
{
return _bash_resist;
}
int material_type::cut_resist() const
{
return _cut_resist;
}
std::string material_type::bash_dmg_verb() const
{
return _( _bash_dmg_verb );
}
std::string material_type::cut_dmg_verb() const
{
return _( _cut_dmg_verb );
}
std::string material_type::dmg_adj( int damage ) const
{
if( damage <= 0 ) {
// not damaged (+/- reinforced)
return std::string();
}
// apply bounds checking
return _( _dmg_adj[std::min( static_cast<size_t>( damage ), _dmg_adj.size() ) - 1] );
}
int material_type::acid_resist() const
{
return _acid_resist;
}
int material_type::elec_resist() const
{
return _elec_resist;
}
int material_type::fire_resist() const
{
return _fire_resist;
}
int material_type::chip_resist() const
{
return _chip_resist;
}
float material_type::specific_heat_liquid() const
{
return _specific_heat_liquid;
}
float material_type::specific_heat_solid() const
{
return _specific_heat_solid;
}
float material_type::latent_heat() const
{
return _latent_heat;
}
int material_type::freeze_point() const
{
return _freeze_point;
}
int material_type::density() const
{
return _density;
}
bool material_type::edible() const
{
return _edible;
}
bool material_type::rotting() const
{
return _rotting;
}
bool material_type::soft() const
{
return _soft;
}
bool material_type::reinforces() const
{
return _reinforces;
}
const mat_burn_data &material_type::burn_data( size_t intensity ) const
{
return _burn_data[ std::min<size_t>( intensity, fd_fire.obj().get_max_intensity() ) - 1 ];
}
const mat_burn_products &material_type::burn_products() const
{
return _burn_products;
}
const material_id_list &material_type::compact_accepts() const
{
return _compact_accepts;
}
const mat_compacts_into &material_type::compacts_into() const
{
return _compacts_into;
}
void materials::load( const JsonObject &jo, const std::string &src )
{
material_data.load( jo, src );
}
void materials::check()
{
material_data.check();
}
void materials::reset()
{
material_data.reset();
}
material_list materials::get_all()
{
return material_data.get_all();
}
material_list materials::get_compactable()
{
material_list all = get_all();
material_list compactable;
std::copy_if( all.begin(), all.end(),
std::back_inserter( compactable ), []( const material_type & mt ) {
return !mt.compacts_into().empty();
} );
return compactable;
}
std::set<material_id> materials::get_rotting()
{
material_list all = get_all();
std::set<material_id> rotting;
for( const material_type &m : all ) {
if( m.rotting() ) {
rotting.emplace( m.ident() );
}
}
return rotting;
}