forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongroup.cpp
602 lines (546 loc) · 22.8 KB
/
mongroup.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
#include "mongroup.h"
#include <algorithm>
#include <string>
#include <utility>
#include "assign.h"
#include "calendar.h"
#include "cata_utility.h"
#include "debug.h"
#include "json.h"
#include "mtype.h"
#include "options.h"
#include "rng.h"
// Frequency: If you don't use the whole 1000 points of frequency for each of
// the monsters, the remaining points will go to the defaultMonster.
// I.e. a group with 1 monster at frequency 500 will have 50% chance to spawn
// the default monster.
// In the same spirit, if you have a total point count of over 1000, the
// default monster will never get picked, and nor will the others past the
// monster that makes the point count go over 1000
std::map<mongroup_id, MonsterGroup> MonsterGroupManager::monsterGroupMap;
MonsterGroupManager::t_string_set MonsterGroupManager::monster_blacklist;
MonsterGroupManager::t_string_set MonsterGroupManager::monster_whitelist;
MonsterGroupManager::t_string_set MonsterGroupManager::monster_categories_blacklist;
MonsterGroupManager::t_string_set MonsterGroupManager::monster_categories_whitelist;
MonsterGroupManager::t_string_set MonsterGroupManager::monster_species_blacklist;
MonsterGroupManager::t_string_set MonsterGroupManager::monster_species_whitelist;
static const mtype_id mon_null( "mon_null" );
static bool monster_whitelist_is_exclusive = false;
/** @relates string_id */
template<>
bool string_id<MonsterGroup>::is_valid() const
{
return MonsterGroupManager::isValidMonsterGroup( *this );
}
/** @relates string_id */
template<>
const MonsterGroup &string_id<MonsterGroup>::obj() const
{
return MonsterGroupManager::GetMonsterGroup( *this );
}
bool mongroup::is_safe() const
{
return type.obj().is_safe;
}
bool mongroup::empty() const
{
return population <= 0 && monsters.empty();
}
void mongroup::clear()
{
population = 0;
monsters.clear();
}
float mongroup::avg_speed() const
{
float avg_speed = 0.0f;
if( monsters.empty() ) {
const MonsterGroup &g = type.obj();
int remaining_frequency = g.freq_total;
for( const MonsterGroupEntry &elem : g.monsters ) {
if( elem.is_group() ) {
// TODO: recursively derive average speed from subgroups
avg_speed += elem.frequency * 100;
} else {
avg_speed += elem.frequency * elem.name.obj().speed;
}
remaining_frequency -= elem.frequency;
}
if( remaining_frequency > 0 ) {
avg_speed += g.defaultMonster.obj().speed * remaining_frequency;
}
avg_speed /= g.freq_total;
} else {
for( const monster &it : monsters ) {
avg_speed += it.type->speed;
}
avg_speed /= monsters.size();
}
return avg_speed;
}
const MonsterGroup &MonsterGroupManager::GetUpgradedMonsterGroup( const mongroup_id &group )
{
const MonsterGroup *groupptr = &group.obj();
if( get_option<float>( "MONSTER_UPGRADE_FACTOR" ) > 0 ) {
const time_duration replace_time = groupptr->monster_group_time *
get_option<float>( "MONSTER_UPGRADE_FACTOR" );
while( groupptr->replace_monster_group &&
calendar::turn - time_point( calendar::start_of_cataclysm ) > replace_time ) {
groupptr = &groupptr->new_monster_group.obj();
}
}
return *groupptr;
}
//Quantity is adjusted directly as a side effect of this function
std::vector<MonsterGroupResult> MonsterGroupManager::GetResultFromGroup(
const mongroup_id &group_name, int *quantity, bool *mon_found, bool from_subgroup )
{
const MonsterGroup &group = GetUpgradedMonsterGroup( group_name );
int spawn_chance = rng( 1, group.event_adjusted_freq_total() );
//Our spawn details specify, by default, a single instance of the default monster
std::vector<MonsterGroupResult> spawn_details;
bool monster_found = false;
// Loop invariant values
const time_point sunset = ::sunset( calendar::turn );
const time_point sunrise = ::sunrise( calendar::turn );
const season_type season = season_of_year( calendar::turn );
std::string opt = get_option<std::string>( "EVENT_SPAWNS" );
const bool can_spawn_events = opt == "monsters" || opt == "both";
// Step through spawn definitions from the monster group until one is found or
for( auto it = group.monsters.begin(); it != group.monsters.end() && !monster_found; ++it ) {
// There's a lot of conditions to work through to see if this spawn definition is valid
bool valid_entry = true;
// If an event was specified for this entry, check if it matches the current holiday
if( it->event != holiday::none && ( !can_spawn_events || it->event != get_holiday_from_time() ) ) {
valid_entry = false;
}
//Insure that the time is not before the spawn first appears or after it stops appearing
valid_entry = valid_entry && ( calendar::start_of_cataclysm + it->starts < calendar::turn );
valid_entry = valid_entry && ( it->lasts_forever() ||
calendar::start_of_cataclysm + it->ends > calendar::turn );
std::vector<std::pair<time_point, time_point> > valid_times_of_day;
bool season_limited = false;
bool season_matched = false;
//Collect the various spawn conditions, and then insure they are met appropriately
for( const std::string &elem : it->conditions ) {
//Collect valid time of day ranges
if( elem == "DAY" || elem == "NIGHT" || elem == "DUSK" || elem == "DAWN" ) {
if( elem == "DAY" ) {
valid_times_of_day.emplace_back( sunrise, sunset );
} else if( elem == "NIGHT" ) {
valid_times_of_day.emplace_back( sunset, sunrise );
} else if( elem == "DUSK" ) {
valid_times_of_day.emplace_back( sunset - 1_hours, sunset + 1_hours );
} else if( elem == "DAWN" ) {
valid_times_of_day.emplace_back( sunrise - 1_hours, sunrise + 1_hours );
}
}
//If we have any seasons listed, we know to limit by season, and if any season matches this season, we are good to spawn
if( elem == "SUMMER" || elem == "WINTER" || elem == "SPRING" || elem == "AUTUMN" ) {
season_limited = true;
if( ( season == SUMMER && elem == "SUMMER" ) ||
( season == WINTER && elem == "WINTER" ) ||
( season == SPRING && elem == "SPRING" ) ||
( season == AUTUMN && elem == "AUTUMN" ) ) {
season_matched = true;
}
}
}
//Make sure the current time of day is within one of the valid time ranges for this spawn
bool is_valid_time_of_day = false;
if( valid_times_of_day.empty() ) {
//Then it can spawn whenever, since no times were defined
is_valid_time_of_day = true;
} else {
//Otherwise, it's valid if it matches any of the times of day
for( auto &elem : valid_times_of_day ) {
if( calendar::turn > elem.first && calendar::turn < elem.second ) {
is_valid_time_of_day = true;
}
}
}
if( !is_valid_time_of_day ) {
valid_entry = false;
}
//If we are limited by season, make sure we matched a season
if( season_limited && !season_matched ) {
valid_entry = false;
}
const int pack_size = it->pack_maximum > 1 ? rng( it->pack_minimum, it->pack_maximum ) : 1;
// Check for monsters within subgroup
bool found_in_subgroup = false;
int tmp_qty = !!quantity ? *quantity : 1;
std::vector<MonsterGroupResult> tmp_grp_list;
if( valid_entry && it->is_group() ) {
for( int i = 0; i < pack_size; i++ ) {
std::vector<MonsterGroupResult> tmp_grp =
GetResultFromGroup( it->group, !!quantity ? &tmp_qty : nullptr, &found_in_subgroup, true );
tmp_grp_list.insert( tmp_grp_list.end(), tmp_grp.begin(), tmp_grp.end() );
}
}
//If the entry was valid, check to see if we actually spawn it
if( valid_entry ) {
//If the monsters frequency is greater than the spawn_chance, select this spawn rule
if( it->frequency >= spawn_chance ) {
if( found_in_subgroup ) {
//If spawned from a subgroup, we've already obtained that data
spawn_details = tmp_grp_list;
} else {
spawn_details.emplace_back( MonsterGroupResult( it->name, pack_size, it->data ) );
//And if a quantity pointer with remaining value was passed, will modify the external value as a side effect
//We will reduce it by the spawn rule's cost multiplier
if( quantity ) {
*quantity -= std::max( 1, it->cost_multiplier * pack_size );
}
}
monster_found = true;
//Otherwise, subtract the frequency from spawn result for the next loop around
} else {
spawn_chance -= it->frequency;
}
}
}
// Force quantity to decrement regardless of whether we found a monster.
if( quantity && !monster_found && !from_subgroup ) {
( *quantity )--;
}
if( mon_found ) {
( *mon_found ) = monster_found;
}
if( !from_subgroup && spawn_details.empty() ) {
spawn_details.emplace_back( MonsterGroupResult( group.defaultMonster, 1, spawn_data() ) );
}
return spawn_details;
}
bool MonsterGroup::IsMonsterInGroup( const mtype_id &mtypeid ) const
{
for( const MonsterGroupEntry &m : monsters ) {
if( m.name == mtypeid ) {
return true;
}
}
return false;
}
int MonsterGroup::event_adjusted_freq_total( holiday event ) const
{
std::string opt = get_option<std::string>( "EVENT_SPAWNS" );
if( opt != "monsters" && opt != "both" ) {
return freq_total;
} else if( event == holiday::num_holiday ) {
event = get_holiday_from_time();
}
if( event == holiday::none ) {
return freq_total;
}
auto iter = event_freq.find( event );
return freq_total + ( iter == event_freq.end() ? 0 : iter->second );
}
bool MonsterGroupManager::IsMonsterInGroup( const mongroup_id &group, const mtype_id &monster )
{
return group.obj().IsMonsterInGroup( monster );
}
const mongroup_id &MonsterGroupManager::Monster2Group( const mtype_id &monster )
{
for( auto &g : monsterGroupMap ) {
if( g.second.IsMonsterInGroup( monster ) ) {
return g.second.name;
}
}
return mongroup_id::NULL_ID();
}
std::vector<mtype_id> MonsterGroupManager::GetMonstersFromGroup( const mongroup_id &group,
bool from_subgroups )
{
const MonsterGroup &g = group.obj();
std::vector<mtype_id> monsters;
std::string opt = get_option<std::string>( "EVENT_SPAWNS" );
const bool can_spawn_events = opt == "monsters" || opt == "both";
for( const MonsterGroupEntry &elem : g.monsters ) {
if( elem.event != holiday::none && ( !can_spawn_events ||
elem.event != get_holiday_from_time() ) ) {
continue;
}
if( elem.is_group() ) {
if( from_subgroups ) {
std::vector<mtype_id> submons = GetMonstersFromGroup( elem.group, from_subgroups );
monsters.reserve( monsters.size() + submons.size() );
monsters.insert( monsters.end(), submons.begin(), submons.end() );
}
} else {
monsters.push_back( elem.name );
}
}
return monsters;
}
bool MonsterGroupManager::isValidMonsterGroup( const mongroup_id &group )
{
return monsterGroupMap.count( group ) > 0;
}
const MonsterGroup &MonsterGroupManager::GetMonsterGroup( const mongroup_id &group )
{
const auto it = monsterGroupMap.find( group );
if( it == monsterGroupMap.end() ) {
debugmsg( "Unable to get the group '%s'", group.c_str() );
// Initialize the group with a null-monster, it's ignored while spawning,
// but it prevents further messages about invalid monster type id
auto &g = monsterGroupMap[group];
g.name = group;
g.defaultMonster = mtype_id::NULL_ID();
return g;
} else {
return it->second;
}
}
void MonsterGroupManager::LoadMonsterBlacklist( const JsonObject &jo )
{
add_array_to_set( monster_blacklist, jo, "monsters" );
add_array_to_set( monster_categories_blacklist, jo, "categories" );
add_array_to_set( monster_species_blacklist, jo, "species" );
}
void MonsterGroupManager::LoadMonsterWhitelist( const JsonObject &jo )
{
if( jo.has_string( "mode" ) && jo.get_string( "mode" ) == "EXCLUSIVE" ) {
monster_whitelist_is_exclusive = true;
}
add_array_to_set( monster_whitelist, jo, "monsters" );
add_array_to_set( monster_categories_whitelist, jo, "categories" );
add_array_to_set( monster_species_whitelist, jo, "species" );
}
bool MonsterGroupManager::monster_is_blacklisted( const mtype_id &m )
{
if( monster_whitelist.count( m.str() ) > 0 ) {
return false;
}
const mtype &mt = m.obj();
for( const auto &elem : monster_categories_whitelist ) {
if( mt.categories.count( elem ) > 0 ) {
return false;
}
}
for( const auto &elem : monster_species_whitelist ) {
if( mt.in_species( species_id( elem ) ) ) {
return false;
}
}
for( const auto &elem : monster_categories_blacklist ) {
if( mt.categories.count( elem ) > 0 ) {
return true;
}
}
for( const auto &elem : monster_species_blacklist ) {
if( mt.in_species( species_id( elem ) ) ) {
return true;
}
}
if( monster_blacklist.count( m.str() ) > 0 ) {
return true;
}
// Return true if the whitelist mode is exclusive and any whitelist is populated.
return monster_whitelist_is_exclusive &&
( !monster_whitelist.empty() || !monster_categories_whitelist.empty() ||
!monster_species_whitelist.empty() );
}
void MonsterGroupManager::FinalizeMonsterGroups()
{
for( const std::string &mtid : monster_whitelist ) {
if( !mtype_id( mtid ).is_valid() ) {
debugmsg( "monster on whitelist %s does not exist", mtid.c_str() );
}
}
for( const std::string &mtid : monster_blacklist ) {
if( !mtype_id( mtid ).is_valid() ) {
debugmsg( "monster on blacklist %s does not exist", mtid.c_str() );
}
}
// Further, remove all blacklisted monsters
for( auto &elem : monsterGroupMap ) {
MonsterGroup &mg = elem.second;
for( FreqDef::iterator c = mg.monsters.begin(); c != mg.monsters.end(); ) {
if( !c->is_group() && MonsterGroupManager::monster_is_blacklisted( c->name ) ) {
c = mg.monsters.erase( c );
} else {
++c;
}
}
if( MonsterGroupManager::monster_is_blacklisted( mg.defaultMonster ) ) {
mg.defaultMonster = mtype_id::NULL_ID();
}
}
}
void MonsterGroupManager::LoadMonsterGroup( const JsonObject &jo )
{
float mon_upgrade_factor = get_option<float>( "MONSTER_UPGRADE_FACTOR" );
MonsterGroup g;
int freq_total = 0;
std::pair<mtype_id, int> max_freq( { mon_null, 0 } );
g.name = mongroup_id( jo.get_string( "name" ) );
bool extending = false; //If already a group with that name, add to it instead of overwriting it
if( monsterGroupMap.count( g.name ) != 0 && !jo.get_bool( "override", false ) ) {
g = monsterGroupMap[g.name];
extending = true;
}
bool explicit_def_null = false;
if( !extending || jo.has_string( "default" ) ) {
g.defaultMonster = mtype_id( jo.get_string( "default", "mon_null" ) );
if( jo.has_string( "default" ) && g.defaultMonster == mon_null ) {
explicit_def_null = true;
}
} else if( extending && !jo.has_string( "default" ) && g.defaultMonster == mon_null ) {
explicit_def_null = true;
}
g.is_animal = jo.get_bool( "is_animal", false );
if( jo.has_array( "monsters" ) ) {
for( JsonObject mon : jo.get_array( "monsters" ) ) {
bool isgroup = false;
std::string id_name;
if( mon.has_string( "group" ) ) {
isgroup = true;
id_name = mon.get_string( "group" );
} else {
id_name = mon.get_string( "monster" );
}
holiday event = mon.get_enum_value<holiday>( "event", holiday::none );
int freq = mon.get_int( "weight", 1 );
if( mon.has_int( "freq" ) ) {
freq = mon.get_int( "freq" );
}
if( freq > max_freq.second ) {
if( !isgroup && event == holiday::none ) {
max_freq = { mtype_id( id_name ), freq };
}
}
if( event == holiday::none ) {
freq_total += freq;
}
int cost = mon.get_int( "cost_multiplier", 1 );
int pack_min = 1;
int pack_max = 1;
if( mon.has_member( "pack_size" ) ) {
JsonArray packarr = mon.get_array( "pack_size" );
pack_min = packarr.next_int();
pack_max = packarr.next_int();
}
static const time_duration tdfactor = 1_hours;
time_duration starts = 0_turns;
time_duration ends = 0_turns;
if( mon.has_member( "starts" ) ) {
assign( mon, "starts", starts, false, tdfactor );
starts *= mon_upgrade_factor > 0 ? mon_upgrade_factor : 1;
}
if( mon.has_member( "ends" ) ) {
assign( mon, "ends", ends, false, tdfactor );
ends *= mon_upgrade_factor > 0 ? mon_upgrade_factor : 1;
}
spawn_data data;
if( mon.has_object( "spawn_data" ) ) {
const JsonObject &sd = mon.get_object( "spawn_data" );
if( sd.has_array( "ammo" ) ) {
const JsonArray &ammos = sd.get_array( "ammo" );
for( const JsonObject adata : ammos ) {
data.ammo.emplace( itype_id( adata.get_string( "ammo_id" ) ), jmapgen_int( adata, "qty" ) );
}
}
}
MonsterGroupEntry new_mon_group = isgroup ?
MonsterGroupEntry( mongroup_id( id_name ), freq, cost,
pack_min, pack_max, data, starts, ends, event ) :
MonsterGroupEntry( mtype_id( id_name ), freq, cost, pack_min,
pack_max, data, starts, ends, event );
if( mon.has_member( "conditions" ) ) {
for( const std::string line : mon.get_array( "conditions" ) ) {
new_mon_group.conditions.push_back( line );
}
}
g.event_freq[event] += freq;
g.monsters.push_back( new_mon_group );
}
// If no default monster specified, use the highest frequency spawn as the default
if( g.defaultMonster == mon_null && !explicit_def_null ) {
g.defaultMonster = max_freq.first;
}
}
g.replace_monster_group = jo.get_bool( "replace_monster_group", false );
g.new_monster_group = mongroup_id( jo.get_string( "new_monster_group_id",
mongroup_id::NULL_ID().str() ) );
assign( jo, "replacement_time", g.monster_group_time, false, 1_days );
g.is_safe = jo.get_bool( "is_safe", false );
g.freq_total = jo.get_int( "freq_total", ( extending ? g.freq_total : 0 ) + freq_total );
if( jo.get_bool( "auto_total", false ) ) { //Fit the max size to the sum of all freqs
int total = 0;
for( MonsterGroupEntry &mon : g.monsters ) {
total += mon.frequency;
}
g.freq_total = total;
}
monsterGroupMap[g.name] = g;
}
bool MonsterGroupManager::is_animal( const mongroup_id &group_name )
{
const MonsterGroup *groupptr = &group_name.obj();
return groupptr->is_animal;
}
void MonsterGroupManager::ClearMonsterGroups()
{
monsterGroupMap.clear();
monster_blacklist.clear();
monster_whitelist.clear();
monster_whitelist_is_exclusive = false;
monster_categories_blacklist.clear();
monster_categories_whitelist.clear();
monster_species_blacklist.clear();
monster_species_whitelist.clear();
}
static void check_group_def( const mongroup_id &g )
{
for( const MonsterGroupEntry &m : g.obj().monsters ) {
if( m.is_group() ) {
if( !m.group.is_valid() ) {
debugmsg( "monster group %s contains unknown subgroup %s", g.c_str(), m.group.c_str() );
} else {
check_group_def( m.group );
}
} else if( !m.name.is_valid() ) {
debugmsg( "monster group %s contains unknown monster %s", g.c_str(), m.name.c_str() );
}
}
}
void MonsterGroupManager::check_group_definitions()
{
for( auto &e : monsterGroupMap ) {
const MonsterGroup &mg = e.second;
for( const MonsterGroupEntry &mge : mg.monsters ) {
if( mge.is_group() ) {
if( !mge.group.is_valid() ) {
debugmsg( "monster group %s contains unknown subgroup %s", mg.name.c_str(), mge.group.c_str() );
} else {
check_group_def( mge.group );
}
} else if( !mge.name.is_valid() ) {
// mon_null should not be valid here
debugmsg( "monster group %s contains unknown monster %s", mg.name.c_str(), mge.name.c_str() );
}
}
}
}
const mtype_id &MonsterGroupManager::GetRandomMonsterFromGroup( const mongroup_id &group_name )
{
const MonsterGroup &group = group_name.obj();
int spawn_chance = rng( 1, group.event_adjusted_freq_total() );
std::string opt = get_option<std::string>( "EVENT_SPAWNS" );
const bool can_spawn_events = opt == "monsters" || opt == "both";
for( const MonsterGroupEntry &monster_type : group.monsters ) {
if( monster_type.event != holiday::none && ( !can_spawn_events ||
monster_type.event != get_holiday_from_time() ) ) {
continue;
}
if( monster_type.frequency >= spawn_chance ) {
if( monster_type.is_group() ) {
return GetRandomMonsterFromGroup( monster_type.group );
}
return monster_type.name;
} else {
spawn_chance -= monster_type.frequency;
}
}
return group.defaultMonster;
}