forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ammo.cpp
83 lines (68 loc) · 1.91 KB
/
ammo.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
#include "ammo.h"
#include <string>
#include <unordered_map>
#include <utility>
#include "debug.h"
#include "item.h"
#include "json.h"
#include "translations.h"
#include "type_id.h"
namespace
{
using ammo_map_t = std::unordered_map<ammotype, ammunition_type>;
ammo_map_t &all_ammunition_types()
{
static ammo_map_t the_map;
return the_map;
}
} //namespace
ammunition_type::ammunition_type() : name_( no_translation( "null" ) )
{
}
void ammunition_type::load_ammunition_type( const JsonObject &jsobj )
{
ammunition_type &res = all_ammunition_types()[ ammotype( jsobj.get_string( "id" ) ) ];
jsobj.read( "name", res.name_ );
jsobj.read( "default", res.default_ammotype_, true );
}
/** @relates string_id */
template<>
bool string_id<ammunition_type>::is_valid() const
{
return all_ammunition_types().count( *this ) > 0;
}
/** @relates string_id */
template<>
const ammunition_type &string_id<ammunition_type>::obj() const
{
const auto &the_map = all_ammunition_types();
const auto it = the_map.find( *this );
if( it != the_map.end() ) {
return it->second;
}
debugmsg( "Tried to get invalid ammunition: %s", c_str() );
static const ammunition_type null_ammunition;
return null_ammunition;
}
void ammunition_type::reset()
{
all_ammunition_types().clear();
}
void ammunition_type::check_consistency()
{
for( const auto &ammo : all_ammunition_types() ) {
const auto &id = ammo.first;
const auto &at = ammo.second.default_ammotype_;
// TODO: these ammo types should probably not have default ammo at all.
if( at.str() == "components" || at.str() == "thrown" ) {
continue;
}
if( !at.is_empty() && !item::type_is_defined( at ) ) {
debugmsg( "ammo type %s has invalid default ammo %s", id.c_str(), at.c_str() );
}
}
}
std::string ammunition_type::name() const
{
return name_.translated();
}