Skip to content

Commit

Permalink
Better error messages
Browse files Browse the repository at this point in the history
Improve context for consistency check errors in item groups.

Improve errors for undefined item (group) types in mapgen.
  • Loading branch information
jbytheway committed Oct 4, 2019
1 parent e89e89b commit 81206d2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/item_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ void Item_factory::check_definitions() const
}
}
for( const auto &elem : m_template_groups ) {
elem.second->check_consistency();
elem.second->check_consistency( elem.first );
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/item_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,23 @@ Item_spawn_data::ItemList Single_item_creator::create( const time_point &birthda
return result;
}

void Single_item_creator::check_consistency() const
void Single_item_creator::check_consistency( const std::string &context ) const
{
if( type == S_ITEM ) {
if( !item::type_is_defined( id ) ) {
debugmsg( "item id %s is unknown", id.c_str() );
debugmsg( "item id %s is unknown (in %s)", id, context );
}
} else if( type == S_ITEM_GROUP ) {
if( !item_group::group_is_defined( id ) ) {
debugmsg( "item group id %s is unknown", id.c_str() );
debugmsg( "item group id %s is unknown (in %s)", id, context );
}
} else if( type == S_NONE ) {
// this is okay, it will be ignored
} else {
debugmsg( "Unknown type of Single_item_creator: %d", static_cast<int>( type ) );
}
if( modifier ) {
modifier->check_consistency();
modifier->check_consistency( context );
}
}

Expand Down Expand Up @@ -301,13 +301,13 @@ void Item_modifier::modify( item &new_item ) const
}
}

void Item_modifier::check_consistency() const
void Item_modifier::check_consistency( const std::string &context ) const
{
if( ammo != nullptr ) {
ammo->check_consistency();
ammo->check_consistency( "ammo of " + context );
}
if( container != nullptr ) {
container->check_consistency();
container->check_consistency( "container of " + context );
}
if( with_ammo < 0 || with_ammo > 100 ) {
debugmsg( "Item modifier's ammo chance %d is out of range", with_ammo );
Expand Down Expand Up @@ -432,10 +432,10 @@ 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( const std::string &context ) const
{
for( const auto &elem : items ) {
( elem )->check_consistency();
( elem )->check_consistency( "item in " + context );
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/item_group.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,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 = 0;
virtual void check_consistency( const std::string &context ) const = 0;
/**
* For item blacklisted, remove the given item from this and
* all linked groups.
Expand Down Expand Up @@ -186,7 +186,7 @@ class Item_modifier
Item_modifier( Item_modifier && ) = default;

void modify( item &new_item ) const;
void check_consistency() const;
void check_consistency( const std::string &context ) const;
bool remove_item( const Item_tag &itemid );

// Currently these always have the same chance as the item group it's part of, but
Expand Down Expand Up @@ -233,7 +233,7 @@ class Single_item_creator : public Item_spawn_data

ItemList create( const time_point &birthday, RecursionList &rec ) const override;
item create_single( const time_point &birthday, RecursionList &rec ) const override;
void check_consistency() const override;
void check_consistency( const std::string &context ) const override;
bool remove_item( const Item_tag &itemid ) override;
bool has_item( const Item_tag &itemid ) const override;
std::set<const itype *> every_item() const override;
Expand Down Expand Up @@ -277,7 +277,7 @@ class Item_group : public Item_spawn_data

ItemList create( const time_point &birthday, RecursionList &rec ) const override;
item create_single( const time_point &birthday, RecursionList &rec ) const override;
void check_consistency() const override;
void check_consistency( const std::string &context ) const override;
bool remove_item( const Item_tag &itemid ) override;
bool has_item( const Item_tag &itemid ) const override;
std::set<const itype *> every_item() const override;
Expand Down
6 changes: 3 additions & 3 deletions src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ class jmapgen_liquid_item : public jmapgen_piece
, liquid( jsi.get_string( "liquid" ) )
, chance( jsi, "chance", 1, 1 ) {
if( !item::type_is_defined( itype_id( liquid ) ) ) {
set_mapgen_defer( jsi, "liquid", "no such item type" );
set_mapgen_defer( jsi, "liquid", "no such item type '" + liquid + "'" );
}
}
void apply( mapgendata &dat, const jmapgen_int &x, const jmapgen_int &y ) const override {
Expand Down Expand Up @@ -1045,7 +1045,7 @@ class jmapgen_item_group : public jmapgen_piece
group_id( jsi.get_string( "item" ) )
, chance( jsi, "chance", 1, 1 ) {
if( !item_group::group_is_defined( group_id ) ) {
set_mapgen_defer( jsi, "item", "no such item type" );
set_mapgen_defer( jsi, "item", "no such item group '" + group_id + "'" );
}
repeat = jmapgen_int( jsi, "repeat", 1, 1 );
}
Expand Down Expand Up @@ -1075,7 +1075,7 @@ class jmapgen_loot : public jmapgen_piece
set_mapgen_defer( jsi, "group", "no such item group" );
}
if( !name.empty() && !item::type_is_defined( name ) ) {
set_mapgen_defer( jsi, "item", "no such item type" );
set_mapgen_defer( jsi, "item", "no such item type '" + name + "'" );
}

// All the probabilities are 100 because we do the roll in @ref apply.
Expand Down

0 comments on commit 81206d2

Please sign in to comment.