Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failed crafts with 0 volume #35554

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3481,7 +3481,14 @@ void activity_handlers::craft_do_turn( player_activity *act, player *p )
}
}
} else if( craft->item_counter >= craft->get_next_failure_point() ) {
craft->handle_craft_failure( *p );
bool destroy = craft->handle_craft_failure( *p );
// If the craft needs to be destroyed, do it and stop crafting.
if( destroy ) {
p->add_msg_player_or_npc( _( "There is nothing left of the %s to craft from." ),
_( "There is nothing left of the %s <npcname> was crafting." ), craft->tname() );
act->targets.front().remove_item();
p->cancel_activity();
}
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/crafting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,15 +986,15 @@ static void destroy_random_component( item &craft, const player &crafter )
_( "<npcname> messes up and destroys the %s" ), destroyed.tname() );
}

void item::handle_craft_failure( player &crafter )
bool item::handle_craft_failure( player &crafter )
{
if( !is_craft() ) {
debugmsg( "handle_craft_failure() called on non-craft '%s.' Aborting.", tname() );
return;
return false;
}

const double success_roll = crafter.crafting_success_roll( get_making() );

const int starting_components = this->components.size();
// Destroy at most 75% of the components, always a chance of losing 1 though
const size_t max_destroyed = std::max<size_t>( 1, components.size() * 3 / 4 );
for( size_t i = 0; i < max_destroyed; i++ ) {
Expand All @@ -1008,6 +1008,10 @@ void item::handle_craft_failure( player &crafter )
}
destroy_random_component( *this, crafter );
}
if( starting_components > 0 && this->components.empty() ) {
// The craft had components and all of them were destroyed.
return true;
}

// Minimum 25% progress lost, average 35%. Falls off exponentially
// Loss is scaled by the success roll
Expand All @@ -1024,6 +1028,7 @@ void item::handle_craft_failure( player &crafter )
if( !crafter.can_continue_craft( *this ) ) {
crafter.cancel_activity();
}
return false;
}

requirement_data item::get_continue_reqs() const
Expand Down
3 changes: 2 additions & 1 deletion src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -1992,8 +1992,9 @@ class item : public visitable<item>
* Handle failure during crafting.
* Destroy components, lose progress, and set a new failure point.
* @param crafter the crafting player.
* @return whether the craft being worked on should be entirely destroyed
*/
void handle_craft_failure( player &crafter );
bool handle_craft_failure( player &crafter );

/**
* Returns requirement data representing what is needed to resume work on an in progress craft.
Expand Down