-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
item_category.cpp
159 lines (134 loc) · 3.78 KB
/
item_category.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
#include "item_category.h"
#include <set>
#include "generic_factory.h"
#include "item.h"
#include "json.h"
namespace
{
generic_factory<item_category> item_category_factory( "item_category" );
} // namespace
template<>
const item_category &string_id<item_category>::obj() const
{
return item_category_factory.obj( *this );
}
template<>
bool string_id<item_category>::is_valid() const
{
return item_category_factory.is_valid( *this );
}
void zone_priority_data::deserialize( const JsonObject &jo )
{
load( jo );
}
void zone_priority_data::load( const JsonObject &jo )
{
mandatory( jo, was_loaded, "id", id );
optional( jo, was_loaded, "flags", flags );
optional( jo, was_loaded, "filthy", filthy, false );
}
const std::vector<item_category> &item_category::get_all()
{
return item_category_factory.get_all();
}
void item_category::load_item_cat( const JsonObject &jo, const std::string &src )
{
item_category_factory.load( jo, src );
}
void item_category::reset()
{
item_category_factory.reset();
}
void item_category::load( const JsonObject &jo, const std::string_view )
{
mandatory( jo, was_loaded, "id", id );
mandatory( jo, was_loaded, "name_header", name_header_ );
name_noun_.make_plural();
mandatory( jo, was_loaded, "name_noun", name_noun_ );
mandatory( jo, was_loaded, "sort_rank", sort_rank_ );
optional( jo, was_loaded, "priority_zones", zone_priority_ );
optional( jo, was_loaded, "zone", zone_, std::nullopt );
float spawn_rate = 1.0f;
optional( jo, was_loaded, "spawn_rate", spawn_rate, 1.0f );
set_spawn_rate( spawn_rate );
}
bool item_category::operator<( const item_category &rhs ) const
{
if( sort_rank_ != rhs.sort_rank_ ) {
return sort_rank_ < rhs.sort_rank_;
}
if( name_header_.translated_ne( rhs.name_header_ ) ) {
return name_header_.translated_lt( rhs.name_header_ );
}
return id < rhs.id;
}
bool item_category::operator==( const item_category &rhs ) const
{
return sort_rank_ == rhs.sort_rank_ && name_header_.translated_eq( rhs.name_header_ ) &&
id == rhs.id;
}
bool item_category::operator!=( const item_category &rhs ) const
{
return !operator==( rhs );
}
std::string item_category::name_header() const
{
return name_header_.translated();
}
std::string item_category::name_noun( const int count ) const
{
return name_noun_.translated( count );
}
item_category_id item_category::get_id() const
{
return id;
}
std::optional<zone_type_id> item_category::zone() const
{
return zone_;
}
std::optional<zone_type_id> item_category::priority_zone( const item &it ) const
{
for( const zone_priority_data &zone_dat : zone_priority_ ) {
if( zone_dat.filthy ) {
if( it.is_filthy() ) {
return zone_dat.id;
} else {
continue;
}
}
if( it.has_any_flag( zone_dat.flags ) ) {
return zone_dat.id;
}
}
return std::nullopt;
}
int item_category::sort_rank() const
{
return sort_rank_;
}
void item_category::set_spawn_rate( const float &rate ) const
{
item_category_spawn_rates::get_item_category_spawn_rates().set_spawn_rate( id, rate );
}
float item_category::get_spawn_rate() const
{
return item_category_spawn_rates::get_item_category_spawn_rates().get_spawn_rate( id );
}
void item_category_spawn_rates::set_spawn_rate( const item_category_id &id, const float &rate )
{
auto it = spawn_rates.find( id );
if( it != spawn_rates.end() ) {
it->second = rate;
} else {
spawn_rates.insert( std::make_pair( id, rate ) );
}
}
float item_category_spawn_rates::get_spawn_rate( const item_category_id &id )
{
auto it = spawn_rates.find( id );
if( it != spawn_rates.end() ) {
return it->second;
}
return 1.0f;
}