Skip to content

Commit

Permalink
Mongroup: multiply nested packsize by parent packsize + unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dseguin committed Jul 17, 2022
1 parent f7ff0bb commit 5c86e98
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
15 changes: 15 additions & 0 deletions data/mods/TEST_DATA/monstergroups.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,20 @@
{ "monster": "mon_test_CBM", "weight": 5 },
{ "monster": "mon_test_bovine", "weight": 5 }
]
},
{
"name": "test_nested_packsize",
"type": "monstergroup",
"monsters": [ { "monster": "mon_test_CBM", "pack_size": [ 2, 4 ] } ]
},
{
"name": "test_top_level_packsize",
"type": "monstergroup",
"monsters": [ { "group": "test_nested_packsize", "pack_size": [ 4, 6 ] } ]
},
{
"name": "test_top_level_no_packsize",
"type": "monstergroup",
"monsters": [ { "group": "test_nested_packsize" } ]
}
]
15 changes: 7 additions & 8 deletions src/mongroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,20 +204,19 @@ MonsterGroupResult MonsterGroupManager::GetResultFromGroup(
if( found_in_subgroup ) {
//If spawned from a subgroup, we've already obtained that data
spawn_details = std::move( tmp_grp );
if( !!quantity ) {
( *quantity ) = tmp_qty;
}
int ps_mult = it->pack_maximum > 1 ? rng( it->pack_minimum, it->pack_maximum ) : 1;
spawn_details.pack_size *= ps_mult;
} else {
if( it->pack_maximum > 1 ) {
spawn_details = MonsterGroupResult( it->name, rng( it->pack_minimum, it->pack_maximum ), it->data );
} else {
spawn_details = MonsterGroupResult( it->name, 1, 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 * spawn_details.pack_size );
}
}
//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 * spawn_details.pack_size );
}
monster_found = true;
//Otherwise, subtract the frequency from spawn result for the next loop around
Expand Down
28 changes: 28 additions & 0 deletions tests/mongroup_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,31 @@ TEST_CASE( "Nested monster groups spawn chance", "[mongroup]" )
Approx( static_cast<float>( std::get<2>( res.second ) ) / iters ).epsilon( 0.5 ) );
}
}

TEST_CASE( "Nested monster group pack size", "[mongroup]" )
{
const int iters = 100;
calendar::turn += 1_turns;

SECTION( "Nested group pack size used as-is" ) {
mongroup_id mg( "test_top_level_no_packsize" );
for( int i = 0; i < iters; i++ ) {
bool found = false;
MonsterGroupResult res = MonsterGroupManager::GetResultFromGroup( mg, nullptr, &found );
REQUIRE( found );
// pack_size == [2, 4]
CHECK( res.pack_size == Approx( 3 ).margin( 1 ) );
}
}

SECTION( "Nested group pack size multiplied by top level pack size" ) {
mongroup_id mg( "test_top_level_packsize" );
for( int i = 0; i < iters; i++ ) {
bool found = false;
MonsterGroupResult res = MonsterGroupManager::GetResultFromGroup( mg, nullptr, &found );
REQUIRE( found );
// pack_size == [8, 24]
CHECK( res.pack_size == Approx( 16 ).margin( 8 ) );
}
}
}

0 comments on commit 5c86e98

Please sign in to comment.