Skip to content

Commit

Permalink
Less repeatedly spawning item groups when checking consistency (#72064)
Browse files Browse the repository at this point in the history
* Less repeatedly spawning item groups when checking consistency

* Further tweaks
  • Loading branch information
SurFlurer authored Mar 2, 2024
1 parent 7ee6c6a commit 669a9fc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/item_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2482,7 +2482,7 @@ void Item_factory::check_definitions() const
}
}
for( const auto &elem : m_template_groups ) {
elem.second->check_consistency();
elem.second->check_consistency( true );
inp_mngr.pump_events();
}
}
Expand Down
31 changes: 20 additions & 11 deletions src/item_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ item Item_spawn_data::create_single( const time_point &birthday ) const
return create_single( birthday, rec );
}

void Item_spawn_data::check_consistency() const
void Item_spawn_data::check_consistency( bool actually_spawn ) const
{
if( on_overflow != overflow_behaviour::none && !container_item ) {
debugmsg( "item group %s specifies overflow behaviour but not container", context() );
}
// Spawn ourselves with all possible items being definitely spawned, so as
// to verify e.g. that if a container item was specified it can actually
// contain what was wanted.
ItemList dummy_list;
dummy_list.reserve( 20 );
create( dummy_list, calendar::turn_zero, spawn_flags::maximized );
if( actually_spawn ) {
ItemList dummy_list;
dummy_list.reserve( 20 );
create( dummy_list, calendar::turn_zero, spawn_flags::maximized );
}
}

void Item_spawn_data::relic_generator::load( const JsonObject &jo )
Expand Down Expand Up @@ -376,7 +378,7 @@ void Single_item_creator::finalize( const itype_id &container_ex )
}
}

void Single_item_creator::check_consistency() const
void Single_item_creator::check_consistency( bool actually_spawn ) const
{
if( type == S_ITEM ) {
if( !item::type_is_defined( itype_id( id ) ) ) {
Expand All @@ -394,7 +396,10 @@ void Single_item_creator::check_consistency() const
if( modifier ) {
modifier->check_consistency( context() );
}
Item_spawn_data::check_consistency();
// If this is to create a group and there's no container to restrain it, no need to actually create it,
// since groups are processed separately.
Item_spawn_data::check_consistency( ( type == S_ITEM_GROUP &&
!container_item ) ? false : actually_spawn );
}

bool Single_item_creator::remove_item( const itype_id &itemid )
Expand Down Expand Up @@ -713,10 +718,10 @@ void Item_modifier::modify( item &new_item, const std::string &context ) const
void Item_modifier::check_consistency( const std::string &context ) const
{
if( ammo != nullptr ) {
ammo->check_consistency();
ammo->check_consistency( true );
}
if( container != nullptr ) {
container->check_consistency();
container->check_consistency( true );
}
if( with_ammo < 0 || with_ammo > 100 ) {
debugmsg( "in %s: Item modifier's ammo chance %d is out of range", context, with_ammo );
Expand Down Expand Up @@ -886,12 +891,16 @@ item Item_group::create_single( const time_point &birthday, RecursionList &rec )
return item( null_item_id, birthday );
}

void Item_group::check_consistency() const
void Item_group::check_consistency( bool actually_spawn ) const
{
// if type is collection, then spawning itself automatically spawnes all entries,
// thus no need to spawn them individually.
for( const auto &elem : items ) {
elem->check_consistency();
// Assuming every entry needs to be spawned.
elem->check_consistency( true );
}
Item_spawn_data::check_consistency();
// If this group has no container, no need to spawn it as a whole as every entry has been spawned.
Item_spawn_data::check_consistency( container_item ? actually_spawn : false );
}

int Item_spawn_data::get_probability( bool skip_event_check ) const
Expand Down
6 changes: 3 additions & 3 deletions src/item_group.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class Item_spawn_data
* Check item / spawn settings for consistency. Includes
* checking for valid item types and valid settings.
*/
virtual void check_consistency() const;
virtual void check_consistency( bool actually_spawn ) const;
/**
* For item blacklisted, remove the given item from this and
* all linked groups.
Expand Down Expand Up @@ -334,7 +334,7 @@ class Single_item_creator : public Item_spawn_data
void finalize( const itype_id &container = itype_id::NULL_ID() ) override;
item create_single( const time_point &birthday, RecursionList &rec ) const override;
item create_single_without_container( const time_point &birthday, RecursionList &rec ) const;
void check_consistency() const override;
void check_consistency( bool actually_spawn ) const override;
bool remove_item( const itype_id &itemid ) override;
void replace_items( const std::unordered_map<itype_id, itype_id> &replacements ) override;

Expand Down Expand Up @@ -383,7 +383,7 @@ class Item_group : public Item_spawn_data
std::size_t create( ItemList &list, const time_point &birthday, RecursionList &rec,
spawn_flags ) const override;
item create_single( const time_point &birthday, RecursionList &rec ) const override;
void check_consistency() const override;
void check_consistency( bool actually_spawn ) const override;
bool remove_item( const itype_id &itemid ) override;
void replace_items( const std::unordered_map<itype_id, itype_id> &replacements ) override;
bool has_item( const itype_id &itemid ) const override;
Expand Down

0 comments on commit 669a9fc

Please sign in to comment.