Skip to content

Commit 314fc26

Browse files
authored
Merge pull request #82275 from sparr/dedupe_crafting
Dedupe various crafting code
2 parents dcb781f + f59a167 commit 314fc26

File tree

9 files changed

+88
-89
lines changed

9 files changed

+88
-89
lines changed

src/butchery.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -875,10 +875,8 @@ bool butchery_drops_harvest( butchery_data bt, Character &you )
875875
// If we're not bleeding the animal we don't care about the blood being wasted
876876
if( action != butcher_type::BLEED ) {
877877
drop_on_map( you, item_drop_reason::deliberate, { obj }, &here, corpse_loc );
878-
} else if( you.is_avatar() ) {
879-
liquid_handler::handle_all_liquid( obj, 1 );
880878
} else {
881-
liquid_handler::handle_npc_liquid( obj, you );
879+
liquid_handler::handle_all_or_npc_liquid( you, obj, 1 );
882880
}
883881
} else if( drop->count_by_charges() ) {
884882
std::vector<item> objs = create_charge_items( drop, roll, entry, &corpse_item, you );

src/character.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3612,6 +3612,9 @@ class Character : public Creature, public visitable
36123612
float crafting_speed_multiplier( const recipe &rec ) const;
36133613
float workbench_crafting_speed_multiplier( const item &craft,
36143614
const std::optional<tripoint_bub_ms> &loc )const;
3615+
float limb_score_crafting_speed_multiplier( const recipe &rec ) const;
3616+
float pain_crafting_speed_multiplier( const recipe &rec ) const;
3617+
float mut_crafting_speed_multiplier( const recipe &rec ) const;
36153618
/** For use with in progress crafts.
36163619
* Workbench multiplier calculation (especially finding lifters nearby)
36173620
* is expensive when numorous items are around.

src/crafting.cpp

Lines changed: 48 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,23 @@ float Character::morale_crafting_speed_multiplier( const recipe &rec ) const
229229
return 1.0f / morale_effect;
230230
}
231231

232+
float Character::limb_score_crafting_speed_multiplier( const recipe &rec ) const
233+
{
234+
return rec.has_flag( flag_NO_MANIP ) ? 1.0f : get_limb_score( limb_score_manip );
235+
}
236+
237+
float Character::pain_crafting_speed_multiplier( const recipe &rec ) const
238+
{
239+
return rec.has_flag( flag_AFFECTED_BY_PAIN ) ? std::max( 0.0f,
240+
1.0f - ( get_perceived_pain() / 100.0f ) ) : 1.0f;
241+
}
242+
243+
float Character::mut_crafting_speed_multiplier( const recipe &rec ) const
244+
{
245+
return rec.has_flag( flag_NO_ENCHANTMENT ) ? 1.0f : 1.0 + enchantment_cache->get_value_multiply(
246+
enchant_vals::mod::CRAFTING_SPEED_MULTIPLIER );
247+
}
248+
232249
template<typename T>
233250
static float lerped_multiplier( const T &value, const T &low, const T &high )
234251
{
@@ -303,14 +320,12 @@ float Character::workbench_crafting_speed_multiplier( const item &craft,
303320
float Character::crafting_speed_multiplier( const recipe &rec ) const
304321
{
305322

306-
const float limb_score = rec.has_flag( flag_NO_MANIP ) ? 1.0f : get_limb_score(
307-
limb_score_manip );
308-
const float pain_multi = rec.has_flag( flag_AFFECTED_BY_PAIN ) ? std::max( 0.0f,
309-
1.0f - ( get_perceived_pain() / 100.0f ) ) : 1.0f;
323+
const float limb_score_multi = limb_score_crafting_speed_multiplier( rec );
324+
const float pain_multi = pain_crafting_speed_multiplier( rec );
310325

311-
float crafting_speed = morale_crafting_speed_multiplier( rec ) *
312-
lighting_craft_speed_multiplier( rec ) *
313-
limb_score * pain_multi;
326+
const float crafting_speed = morale_crafting_speed_multiplier( rec ) *
327+
lighting_craft_speed_multiplier( rec ) *
328+
limb_score_multi * pain_multi;
314329

315330
const float result = enchantment_cache->modify_value( enchant_vals::mod::CRAFTING_SPEED_MULTIPLIER,
316331
crafting_speed );
@@ -333,20 +348,17 @@ float Character::crafting_speed_multiplier( const item &craft,
333348
const recipe &rec = craft.get_making();
334349

335350
const float light_multi = lighting_craft_speed_multiplier( rec );
336-
const float bench_value = ( use_cached_workbench_multiplier ||
337-
cached_workbench_multiplier > 0.0f ) ? cached_workbench_multiplier :
351+
const float bench_multi = rec.has_flag( flag_NO_BENCH ) ?
352+
1.0f :
353+
( use_cached_workbench_multiplier || cached_workbench_multiplier > 0.0f ) ?
354+
cached_workbench_multiplier :
338355
workbench_crafting_speed_multiplier( craft, loc );
339-
const float bench_multi = rec.has_flag( flag_NO_BENCH ) ? 1.0f : bench_value;
340356
const float morale_multi = morale_crafting_speed_multiplier( rec );
341-
const float mut_multi = rec.has_flag( flag_NO_ENCHANTMENT ) ? 1.0f : 1.0 +
342-
enchantment_cache->get_value_multiply(
343-
enchant_vals::mod::CRAFTING_SPEED_MULTIPLIER );
344-
const float limb_score = rec.has_flag( flag_NO_MANIP ) ? 1.0f : get_limb_score(
345-
limb_score_manip );
346-
const float pain_multi = rec.has_flag( flag_AFFECTED_BY_PAIN ) ? std::max( 0.0f,
347-
1.0f - ( get_perceived_pain() / 100.0f ) ) : 1.0f;
348-
349-
const float total_multi = light_multi * bench_multi * morale_multi * mut_multi * limb_score *
357+
const float mut_multi = mut_crafting_speed_multiplier( rec );
358+
const float limb_score_multi = limb_score_crafting_speed_multiplier( rec );
359+
const float pain_multi = pain_crafting_speed_multiplier( rec );
360+
361+
const float total_multi = light_multi * bench_multi * morale_multi * mut_multi * limb_score_multi *
350362
pain_multi;
351363

352364
if( light_multi <= 0.0f ) {
@@ -750,11 +762,7 @@ static item_location set_item_inventory( Character &p, item &newit )
750762
{
751763
item_location ret_val = item_location::nowhere;
752764
if( newit.made_of( phase_id::LIQUID ) ) {
753-
if( p.is_avatar() ) {
754-
liquid_handler::handle_all_liquid( newit, PICKUP_RANGE );
755-
} else {
756-
liquid_handler::handle_npc_liquid( newit, p );
757-
}
765+
liquid_handler::handle_all_or_npc_liquid( p, newit, PICKUP_RANGE );
758766
} else {
759767
p.inv->assign_empty_invlet( newit, p );
760768
// We might not have space for the item
@@ -866,18 +874,21 @@ static item_location place_craft_or_disassembly(
866874
}
867875
}
868876

877+
auto craft_wield = [&craft_in_world, &ch, &craft]() {
878+
if( std::optional<item_location> it_loc = wield_craft( ch, craft ) ) {
879+
craft_in_world = *it_loc;
880+
} else {
881+
// This almost certainly shouldn't happen
882+
put_into_vehicle_or_drop( ch, item_drop_reason::tumbling, {craft} );
883+
}
884+
};
885+
869886
// Crafting without a workbench
870887
if( !target ) {
871888
if( !ch.has_two_arms_lifting() ) {
872889
craft_in_world = set_item_map_or_vehicle( ch, ch.pos_bub(), craft );
873890
} else if( !ch.has_wield_conflicts( craft ) || ch.is_npc() ) {
874-
// NPC always tries wield craft first
875-
if( std::optional<item_location> it_loc = wield_craft( ch, craft ) ) {
876-
craft_in_world = *it_loc;
877-
} else {
878-
// This almost certainly shouldn't happen
879-
put_into_vehicle_or_drop( ch, item_drop_reason::tumbling, {craft} );
880-
}
891+
craft_wield();
881892
} else {
882893
enum option : int {
883894
WIELD_CRAFT = 0,
@@ -902,12 +913,7 @@ static item_location place_craft_or_disassembly(
902913
const option choice = amenu.ret == UILIST_CANCEL ? DROP : static_cast<option>( amenu.ret );
903914
switch( choice ) {
904915
case WIELD_CRAFT: {
905-
if( std::optional<item_location> it_loc = wield_craft( ch, craft ) ) {
906-
craft_in_world = *it_loc;
907-
} else {
908-
// This almost certainly shouldn't happen
909-
put_into_vehicle_or_drop( ch, item_drop_reason::tumbling, {craft} );
910-
}
916+
craft_wield();
911917
break;
912918
}
913919
case DROP_CRAFT: {
@@ -1494,11 +1500,7 @@ static void spawn_items( Character &guy, std::vector<item> &results,
14941500

14951501
newit.set_owner( guy.get_faction()->id );
14961502
if( newit.made_of( phase_id::LIQUID ) ) {
1497-
if( guy.is_avatar() ) {
1498-
liquid_handler::handle_all_liquid( newit, PICKUP_RANGE );
1499-
} else {
1500-
liquid_handler::handle_npc_liquid( newit, guy );
1501-
}
1503+
liquid_handler::handle_all_or_npc_liquid( guy, newit, PICKUP_RANGE );
15021504
} else if( !loc && allow_wield && !guy.has_wield_conflicts( newit ) &&
15031505
guy.can_wield( newit ).success() ) {
15041506
wield_craft( guy, newit );
@@ -2872,18 +2874,7 @@ void Character::complete_disassemble( item_location &target, const recipe &dis )
28722874
if( dis_item.count_by_charges() ) {
28732875
compcount *= activity.position;
28742876
}
2875-
const bool is_liquid = newit.made_of( phase_id::LIQUID );
2876-
// Compress liquids and counted-by-charges items into one item,
2877-
// they are added together on the map anyway and handle_liquid
2878-
// should only be called once to put it all into a container at once.
2879-
if( newit.count_by_charges() || is_liquid ) {
2880-
newit.charges = compcount;
2881-
compcount = 1;
2882-
} else if( !newit.craft_has_charges() && newit.charges > 0 ) {
2883-
// tools that can be unloaded should be created unloaded,
2884-
// tools that can't be unloaded will keep their default charges.
2885-
newit.charges = 0;
2886-
}
2877+
newit.compress_charges_or_liquid( compcount );
28872878

28882879
// If the recipe has a `FULL_MAGAZINE` flag, spawn any magazines full of ammo
28892880
if( newit.is_magazine() && dis.has_flag( flag_FULL_MAGAZINE ) ) {
@@ -2980,11 +2971,7 @@ void Character::complete_disassemble( item_location &target, const recipe &dis )
29802971
}
29812972

29822973
if( act_item.made_of( phase_id::LIQUID ) ) {
2983-
if( is_avatar() ) {
2984-
liquid_handler::handle_all_liquid( act_item, PICKUP_RANGE );
2985-
} else {
2986-
liquid_handler::handle_npc_liquid( act_item, *this );
2987-
}
2974+
liquid_handler::handle_all_or_npc_liquid( *this, act_item, PICKUP_RANGE );
29882975
} else {
29892976
drop_items.push_back( act_item );
29902977
}
@@ -3051,14 +3038,10 @@ void remove_ammo( std::list<item> &dis_items, Character &p )
30513038

30523039
void drop_or_handle( const item &newit, Character &p )
30533040
{
3041+
item tmp( newit );
30543042
if( newit.made_of( phase_id::LIQUID ) ) {
3055-
if( p.is_avatar() ) {
3056-
liquid_handler::handle_all_liquid( newit, PICKUP_RANGE );
3057-
} else {
3058-
liquid_handler::handle_npc_liquid( newit, p );
3059-
}
3043+
liquid_handler::handle_all_or_npc_liquid( p, tmp, PICKUP_RANGE );
30603044
} else {
3061-
item tmp( newit );
30623045
p.i_add_or_drop( tmp );
30633046
}
30643047
}

src/crafting_gui.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@
4444
#include "item_location.h"
4545
#include "itype.h"
4646
#include "localized_comparator.h"
47-
#include "magic_enchantment.h"
4847
#include "options.h"
4948
#include "output.h"
50-
#include "pimpl.h"
5149
#include "point.h"
5250
#include "popup.h"
5351
#include "recipe.h"
@@ -65,13 +63,8 @@
6563
#include "ui_manager.h"
6664
#include "uistate.h"
6765

68-
static const limb_score_id limb_score_manip( "manip" );
69-
70-
static const std::string flag_AFFECTED_BY_PAIN( "AFFECTED_BY_PAIN" );
7166
static const std::string flag_BLIND_EASY( "BLIND_EASY" );
7267
static const std::string flag_BLIND_HARD( "BLIND_HARD" );
73-
static const std::string flag_NO_ENCHANTMENT( "NO_ENCHANTMENT" );
74-
static const std::string flag_NO_MANIP( "NO_MANIP" );
7568

7669
enum TAB_MODE {
7770
NORMAL,
@@ -2370,11 +2363,8 @@ static void draw_hidden_amount( const catacurses::window &w, int amount, int num
23702363
static void draw_can_craft_indicator( const catacurses::window &w, const recipe &rec,
23712364
Character &crafter )
23722365
{
2373-
int limb_modifier = rec.has_flag( flag_NO_MANIP ) ? 100 : crafter.get_limb_score(
2374-
limb_score_manip ) * 100;
2375-
int mut_multi = rec.has_flag( flag_NO_ENCHANTMENT ) ? 100 : ( 1.0 +
2376-
crafter.enchantment_cache->get_value_multiply( enchant_vals::mod::CRAFTING_SPEED_MULTIPLIER ) ) *
2377-
100;
2366+
int limb_modifier = crafter.limb_score_crafting_speed_multiplier( rec ) * 100;
2367+
int mut_multi = crafter.mut_crafting_speed_multiplier( rec ) * 100;
23782368

23792369
std::stringstream modifiers_list;
23802370
if( limb_modifier != 100 ) {
@@ -2398,8 +2388,7 @@ static void draw_can_craft_indicator( const catacurses::window &w, const recipe
23982388
} else if( crafter.crafting_speed_multiplier( rec ) < 1.0f ) {
23992389
int morale_modifier = crafter.morale_crafting_speed_multiplier( rec ) * 100;
24002390
int lighting_modifier = crafter.lighting_craft_speed_multiplier( rec ) * 100;
2401-
int pain_multi = rec.has_flag( flag_AFFECTED_BY_PAIN ) ? 100 * std::max( 0.0f,
2402-
1.0f - ( crafter.get_perceived_pain() / 100.0f ) ) : 100;
2391+
const int pain_multi = crafter.pain_crafting_speed_multiplier( rec ) * 100;
24032392

24042393
if( morale_modifier < 100 ) {
24052394
if( !modifiers_list.str().empty() ) {

src/handle_liquid.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,15 @@ void handle_npc_liquid( item liquid, Character &who )
224224
who.invalidate_weight_carried_cache();
225225
}
226226

227+
void handle_all_or_npc_liquid( Character &p, item &newit, int radius, const item *avoid )
228+
{
229+
if( p.is_avatar() ) {
230+
liquid_handler::handle_all_liquid( newit, radius, avoid );
231+
} else {
232+
liquid_handler::handle_npc_liquid( newit, p );
233+
}
234+
}
235+
227236
bool consume_liquid( item &liquid, const int radius, const item *const avoid )
228237
{
229238
const int original_charges = liquid.charges;

src/handle_liquid.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ void handle_all_liquid( item liquid, int radius, const item *avoid = nullptr );
5252
*/
5353
void handle_npc_liquid( item liquid, Character &who );
5454

55+
/** Call handle_all_liquid for player or handle_npc_liquid for npc */
56+
void handle_all_or_npc_liquid( Character &p, item &newit, int radius = 0,
57+
const item *avoid = nullptr );
58+
5559
/**
5660
* Consume / handle as much of the liquid as possible in varying ways. This function can
5761
* be used when the action can be canceled, which implies the liquid can be put back

src/item.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9127,6 +9127,18 @@ bool item::count_by_charges() const
91279127
return type->count_by_charges();
91289128
}
91299129

9130+
void item::compress_charges_or_liquid( int &compcount )
9131+
{
9132+
if( count_by_charges() || made_of( phase_id::LIQUID ) ) {
9133+
charges = compcount;
9134+
compcount = 1;
9135+
} else if( !craft_has_charges() && charges > 0 ) {
9136+
// tools that can be unloaded should be created unloaded,
9137+
// tools that can't be unloaded will keep their default charges.
9138+
charges = 0;
9139+
}
9140+
}
9141+
91309142
int item::count() const
91319143
{
91329144
return count_by_charges() ? charges : 1;

src/item.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,13 @@ class item : public visitable
10591059
*/
10601060
bool count_by_charges() const;
10611061

1062+
/**
1063+
* Compress liquids and counted-by-charges items into one item.
1064+
* They are added together on the map anyway and handle_liquid
1065+
* should only be called once to put it all into a container at once.
1066+
*/
1067+
void compress_charges_or_liquid( int &compcount );
1068+
10621069
/**
10631070
* If count_by_charges(), returns charges, otherwise 1
10641071
*/

src/vehicle_part.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,7 @@ std::vector<item> vehicle_part::get_salvageable() const
8080
item newit( comp.type, calendar::turn );
8181
if( base.typeId() != comp.type && !newit.has_flag( flag_UNRECOVERABLE ) ) {
8282
int compcount = comp.count;
83-
const bool is_liquid = newit.made_of( phase_id::LIQUID );
84-
if( newit.count_by_charges() || is_liquid ) {
85-
newit.charges = compcount;
86-
compcount = 1;
87-
} else if( !newit.craft_has_charges() && newit.charges > 0 ) {
88-
newit.charges = 0;
89-
}
83+
newit.compress_charges_or_liquid( compcount );
9084
for( ; compcount > 0; compcount-- ) {
9185
tmp.push_back( newit );
9286
}

0 commit comments

Comments
 (0)