forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trait_group.cpp
308 lines (264 loc) · 8.75 KB
/
trait_group.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
#include "trait_group.h"
#include <algorithm>
#include <cstddef>
#include <map>
#include <string>
#include <type_traits>
#include <utility>
#include "cata_assert.h"
#include "debug.h"
#include "json.h"
#include "mutation.h"
#include "rng.h"
#include "string_formatter.h"
#include "translations.h"
#include "ui.h"
using namespace trait_group;
Trait_list trait_group::traits_from( const Trait_group_tag &gid )
{
return mutation_branch::get_group( gid )->create();
}
bool trait_group::group_contains_trait( const Trait_group_tag &gid, const trait_id &tid )
{
return mutation_branch::get_group( gid )->has_trait( tid );
}
void trait_group::load_trait_group( const JsonObject &jsobj, const Trait_group_tag &gid,
const std::string &subtype )
{
mutation_branch::load_trait_group( jsobj, gid, subtype );
}
// NOTE: This function is largely based on item_group::get_unique_group_id()
static Trait_group_tag get_unique_trait_group_id()
{
// This is just a hint what id to use next. Overflow of it is defined and if the group
// name is already used, we simply go the next id.
static unsigned int next_id = 0;
// Prefixing with a character outside the ASCII range, so it is hopefully unique and
// (if actually printed somewhere) stands out. Theoretically those auto-generated group
// names should not be seen anywhere.
static const std::string unique_prefix = "\u01F7 ";
while( true ) {
const Trait_group_tag new_group( unique_prefix + std::to_string( next_id++ ) );
if( !new_group.is_valid() ) {
return new_group;
}
}
}
Trait_group_tag trait_group::load_trait_group( const JsonValue &value,
const std::string &default_subtype )
{
if( value.test_string() ) {
return Trait_group_tag( value.get_string() );
} else if( value.test_object() ) {
const Trait_group_tag group = get_unique_trait_group_id();
JsonObject jo = value.get_object();
const std::string subtype = jo.get_string( "subtype", default_subtype );
mutation_branch::load_trait_group( jo, group, subtype );
return group;
} else if( value.test_array() ) {
const Trait_group_tag group = get_unique_trait_group_id();
if( default_subtype != "collection" && default_subtype != "distribution" ) {
value.throw_error( "invalid subtype for trait group" );
}
mutation_branch::load_trait_group( value.get_array(), group, default_subtype == "collection" );
return group;
} else {
value.throw_error( "invalid trait group, must be string (group id) or object/array (the group data)" );
return Trait_group_tag{};
}
}
// NOTE: This is largely based on item_group::debug_spawn()
void trait_group::debug_spawn()
{
std::vector<Trait_group_tag> groups = mutation_branch::get_all_group_names();
uilist menu;
menu.text = _( "Test which group?" );
for( size_t i = 0; i < groups.size(); i++ ) {
menu.entries.emplace_back( static_cast<int>( i ), true, -2, groups[i].str() );
}
while( true ) {
menu.query();
const int index = menu.ret;
if( index >= static_cast<int>( groups.size() ) || index < 0 ) {
break;
}
// Spawn traits from the group 100 times
std::map<std::string, int> traitnames;
for( size_t a = 0; a < 100; a++ ) {
const auto traits = traits_from( groups[index] );
for( const trait_and_var &tr : traits ) {
traitnames[tr.name()]++;
}
}
// Invert the map to get sorting!
std::multimap<int, std::string> traitnames2;
for( const auto &e : traitnames ) {
traitnames2.insert( std::pair<int, std::string>( e.second, e.first ) );
}
uilist menu2;
menu2.text = _( "Result of 100 spawns:" );
for( const auto &e : traitnames2 ) {
menu2.entries.emplace_back( static_cast<int>( menu2.entries.size() ), true, -2,
string_format( _( "%d x %s" ), e.first, e.second ) );
}
menu2.query();
}
}
Trait_list Trait_creation_data::create() const
{
RecursionList rec;
auto result = create( rec );
return result;
}
Trait_group::Trait_group( int probability )
: Trait_creation_data( probability ), sum_prob( 0 )
{
}
Single_trait_creator::Single_trait_creator( const trait_id &id, const std::string &var,
int probability )
: Trait_creation_data( probability ), id( id ), variant( var )
{
}
Trait_list Single_trait_creator::create( RecursionList & /* rec */ ) const
{
return Trait_list { { id, variant } };
}
void Single_trait_creator::check_consistency() const
{
if( !id.is_valid() ) {
debugmsg( "trait id %s is unknown", id.c_str() );
}
}
bool Single_trait_creator::remove_trait( const trait_id &tid )
{
// Return true if I would be empty after "removing" the given trait, thus
// signaling to my parent that I should be removed.
return tid == id;
}
bool Single_trait_creator::has_trait( const trait_id &tid ) const
{
return tid == id;
}
Trait_group_creator::Trait_group_creator( const Trait_group_tag &id, int probability )
: Trait_creation_data( probability ), id( id )
{
}
Trait_list Trait_group_creator::create( RecursionList &rec ) const
{
Trait_list result;
if( std::find( rec.begin(), rec.end(), id ) != rec.end() ) {
debugmsg( "recursion in trait creation list %s", id.c_str() );
return result;
}
rec.push_back( id );
if( !id.is_valid() ) {
debugmsg( "unknown trait creation list %s", id.c_str() );
return result;
}
const auto tcd = mutation_branch::get_group( id );
Trait_list tmplist = tcd->create( rec );
rec.pop_back();
result.insert( result.end(), tmplist.begin(), tmplist.end() );
return result;
}
void Trait_group_creator::check_consistency() const
{
if( id.is_valid() ) {
debugmsg( "trait group id %s is unknown", id.c_str() );
}
}
bool Trait_group_creator::remove_trait( const trait_id &tid )
{
return mutation_branch::get_group( id )->remove_trait( tid );
}
bool Trait_group_creator::has_trait( const trait_id &tid ) const
{
return mutation_branch::get_group( id )->has_trait( tid );
}
void Trait_group::add_trait_entry( const trait_id &tid, const std::string &var, int probability )
{
add_entry( std::make_unique<Single_trait_creator>( tid, var, probability ) );
}
void Trait_group::add_group_entry( const Trait_group_tag &gid, int probability )
{
add_entry( std::make_unique<Trait_group_creator>( gid, probability ) );
}
void Trait_group::check_consistency() const
{
for( const auto &creator : creators ) {
creator->check_consistency();
}
}
bool Trait_group::remove_trait( const trait_id &tid )
{
for( auto it = creators.begin(); it != creators.end(); ) {
if( ( *it )->remove_trait( tid ) ) {
sum_prob -= ( *it )->probability;
it = creators.erase( it );
} else {
++it;
}
}
return creators.empty();
}
bool Trait_group::has_trait( const trait_id &tid ) const
{
for( const auto &creator : creators ) {
if( creator->has_trait( tid ) ) {
return true;
}
}
return false;
}
Trait_group_collection::Trait_group_collection( int probability )
: Trait_group( probability )
{
if( probability <= 0 || probability > 100 ) {
debugmsg( "Probability %d out of range", probability );
}
}
Trait_list Trait_group_collection::create( RecursionList &rec ) const
{
Trait_list result;
for( const auto &creator : creators ) {
if( rng( 0, 99 ) >= creator->probability ) {
continue;
}
Trait_list tmp = creator->create( rec );
result.insert( result.end(), tmp.begin(), tmp.end() );
}
return result;
}
void Trait_group_collection::add_entry( std::unique_ptr<Trait_creation_data> ptr )
{
cata_assert( ptr.get() != nullptr );
if( ptr->probability <= 0 ) {
return;
}
ptr->probability = std::min( 100, ptr->probability );
creators.push_back( std::move( ptr ) );
}
void Trait_group_distribution::add_entry( std::unique_ptr<Trait_creation_data> ptr )
{
cata_assert( ptr.get() != nullptr );
if( ptr->probability <= 0 ) {
return;
}
sum_prob += ptr->probability;
creators.push_back( std::move( ptr ) );
}
Trait_list Trait_group_distribution::create( RecursionList &rec ) const
{
Trait_list result;
int p = rng( 0, sum_prob - 1 );
for( const auto &creator : creators ) {
p -= creator->probability;
if( p >= 0 ) {
continue;
}
Trait_list tmp = creator->create( rec );
result.insert( result.end(), tmp.begin(), tmp.end() );
break;
}
return result;
}