forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
harvest.cpp
237 lines (205 loc) · 6.79 KB
/
harvest.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
#include "harvest.h"
#include <algorithm>
#include <cmath>
#include <iterator>
#include <string>
#include "assign.h"
#include "debug.h"
#include "item.h"
#include "item_group.h"
#include "json.h"
#include "output.h"
#include "string_formatter.h"
#include "string_id.h"
#include "text_snippets.h"
// TODO: Make a generic factory
static std::map<harvest_id, harvest_list> harvest_all;
/** @relates string_id */
template<>
const harvest_list &string_id<harvest_list>::obj() const
{
const auto found = harvest_all.find( *this );
if( found == harvest_all.end() ) {
debugmsg( "Tried to get invalid harvest list: %s", c_str() );
static const harvest_list null_list{};
return null_list;
}
return found->second;
}
/** @relates string_id */
template<>
bool string_id<harvest_list>::is_valid() const
{
return harvest_all.count( *this ) > 0;
}
harvest_list::harvest_list() : id_( harvest_id::NULL_ID() ) {}
const harvest_id &harvest_list::id() const
{
return id_;
}
std::string harvest_list::message() const
{
return SNIPPET.expand( message_.translated() );
}
bool harvest_list::is_null() const
{
return id_ == harvest_id::NULL_ID();
}
bool harvest_list::has_entry_type( std::string type ) const
{
for( const harvest_entry &entry : entries() ) {
if( entry.type == type ) {
return true;
}
}
return false;
}
harvest_entry harvest_entry::load( const JsonObject &jo, const std::string &src )
{
const bool strict = is_strict_enabled( src );
harvest_entry ret;
assign( jo, "drop", ret.drop, strict );
assign( jo, "base_num", ret.base_num, strict, -1000.0f );
assign( jo, "scale_num", ret.scale_num, strict, -1000.0f );
assign( jo, "max", ret.max, strict, 1 );
assign( jo, "type", ret.type, strict );
assign( jo, "mass_ratio", ret.mass_ratio, strict, 0.00f );
assign( jo, "flags", ret.flags );
assign( jo, "faults", ret.faults );
return ret;
}
const harvest_id &harvest_list::load( const JsonObject &jo, const std::string &src,
const std::string &force_id )
{
harvest_list ret;
if( jo.has_string( "id" ) ) {
ret.id_ = harvest_id( jo.get_string( "id" ) );
} else if( !force_id.empty() ) {
ret.id_ = harvest_id( force_id );
} else {
jo.throw_error( "id was not specified for harvest" );
}
jo.read( "message", ret.message_ );
for( const JsonObject current_entry : jo.get_array( "entries" ) ) {
ret.entries_.push_back( harvest_entry::load( current_entry, src ) );
}
auto &new_entry = harvest_all[ ret.id_ ];
new_entry = ret;
return new_entry.id();
}
void harvest_list::finalize()
{
std::transform( entries_.begin(), entries_.end(), std::inserter( names_, names_.begin() ),
[]( const harvest_entry & entry ) {
return itype_id( entry.drop ).is_valid() ?
item::nname( itype_id( entry.drop ) ) : "";
} );
}
void harvest_list::finalize_all()
{
for( auto &pr : harvest_all ) {
pr.second.finalize();
}
}
void harvest_list::reset()
{
harvest_all.clear();
}
const std::map<harvest_id, harvest_list> &harvest_list::all()
{
return harvest_all;
}
void harvest_list::check_consistency()
{
for( const auto &pr : harvest_all ) {
const auto &hl = pr.second;
const std::string hl_id = hl.id().c_str();
auto error_func = [&]( const harvest_entry & entry ) {
std::string errorlist;
bool item_valid = true;
if( ( entry.type == "bionic" || entry.type == "bionic_group" ) && entry.mass_ratio != 0.0f ) {
errorlist += string_format( "\"mass_ratio\" != 0.0, but type %s", entry.type.c_str() );
}
if( !( itype_id( entry.drop ).is_valid() || ( entry.type == "bionic_group" &&
item_group::group_is_defined( item_group_id( entry.drop ) ) ) ) ) {
item_valid = false;
errorlist += entry.drop;
}
// non butchery harvests need to be excluded
if( hl_id.substr( 0, 14 ) != "harvest_inline" ) {
if( entry.type == "null" ) {
if( !item_valid ) {
errorlist += ", ";
}
errorlist += "null type";
} else if( !( entry.type == "flesh" || entry.type == "bone" || entry.type == "skin" ||
entry.type == "offal" || entry.type == "bionic" || entry.type == "bionic_group" ) ) {
if( !item_valid ) {
errorlist += ", ";
}
errorlist += entry.type;
}
}
return errorlist;
};
const std::string errors = enumerate_as_string( hl.entries_.begin(), hl.entries_.end(),
error_func );
if( !errors.empty() ) {
debugmsg( "Harvest list %s has invalid entry: %s", hl_id, errors );
}
}
}
std::list<harvest_entry>::const_iterator harvest_list::begin() const
{
return entries().begin();
}
std::list<harvest_entry>::const_iterator harvest_list::end() const
{
return entries().end();
}
std::list<harvest_entry>::const_reverse_iterator harvest_list::rbegin() const
{
return entries().rbegin();
}
std::list<harvest_entry>::const_reverse_iterator harvest_list::rend() const
{
return entries().rend();
}
std::string harvest_list::describe( int at_skill ) const
{
if( empty() ) {
return "";
}
return enumerate_as_string( entries().begin(), entries().end(),
[at_skill]( const harvest_entry & en ) {
float min_f = en.base_num.first;
float max_f = en.base_num.second;
if( at_skill >= 0 ) {
min_f += en.scale_num.first * at_skill;
max_f += en.scale_num.second * at_skill;
} else {
max_f = en.max;
}
// TODO: Avoid repetition here by making a common harvest drop function
int max_drops = std::min<int>( en.max, std::round( std::max( 0.0f, max_f ) ) );
int min_drops = std::max<int>( 0.0f, std::round( std::min( min_f, max_f ) ) );
if( max_drops <= 0 ) {
return std::string();
}
std::string ss;
ss += "<bold>" + item::nname( itype_id( en.drop ), max_drops ) + "</bold>";
// If the number is unspecified, just list the type
if( max_drops >= 1000 && min_drops <= 0 ) {
return ss;
}
ss += ": ";
if( min_drops == max_drops ) {
ss += string_format( "<stat>%d</stat>", min_drops );
} else if( max_drops < 1000 ) {
ss += string_format( "<stat>%d-%d</stat>", min_drops, max_drops );
} else {
ss += string_format( "<stat>%d+</stat>", min_drops );
}
return ss;
} );
}