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

Add lightweight crafting inventory to improve NPC construction time #46286

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
3 changes: 1 addition & 2 deletions src/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "game.h"
#include "iexamine.h"
#include "input.h"
#include "inventory.h"
#include "item.h"
#include "map.h"
#include "map_iterator.h"
Expand Down Expand Up @@ -45,8 +46,6 @@ static const std::string flag_GOES_DOWN( "GOES_DOWN" );
static const std::string flag_GOES_UP( "GOES_UP" );
static const std::string flag_SWIMMABLE( "SWIMMABLE" );

class inventory;

static void parse_keymap( std::istream &keymap_txt, std::map<char, action_id> &kmap,
std::set<action_id> &unbound_keymap );

Expand Down
15 changes: 8 additions & 7 deletions src/activity_item_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include "stomach.h"
#include "string_formatter.h"
#include "string_id.h"
#include "temp_crafting_inventory.h"
#include "translations.h"
#include "trap.h"
#include "units.h"
Expand Down Expand Up @@ -899,7 +900,7 @@ static bool are_requirements_nearby( const std::vector<tripoint> &loot_spots,
const bool in_loot_zones, const tripoint &src_loc )
{
zone_manager &mgr = zone_manager::get_manager();
inventory temp_inv;
temp_crafting_inventory temp_inv;
units::volume volume_allowed = p.volume_capacity() - p.volume_carried();
units::mass weight_allowed = p.weight_capacity() - p.weight_carried();
static const auto check_weight_if = []( const activity_id & id ) {
Expand All @@ -919,7 +920,7 @@ static bool are_requirements_nearby( const std::vector<tripoint> &loot_spots,
if( elem->has_quality( qual_WELD ) ) {
found_welder = true;
}
temp_inv += *elem;
temp_inv.add_item_ref( *elem );
}
map &here = get_map();
for( const tripoint &elem : loot_spots ) {
Expand All @@ -935,7 +936,7 @@ static bool are_requirements_nearby( const std::vector<tripoint> &loot_spots,
continue;
}
}
for( const item &elem2 : here.i_at( elem ) ) {
for( item &elem2 : here.i_at( elem ) ) {
if( in_loot_zones && elem2.made_of_from_type( phase_id::LIQUID ) ) {
continue;
}
Expand All @@ -946,15 +947,15 @@ static bool are_requirements_nearby( const std::vector<tripoint> &loot_spots,
continue;
}
}
temp_inv += elem2;
temp_inv.add_item_ref( elem2 );
}
if( !in_loot_zones ) {
if( const cata::optional<vpart_reference> vp = here.veh_at( elem ).part_with_feature( "CARGO",
false ) ) {
vehicle &src_veh = vp->vehicle();
int src_part = vp->part_index();
for( item &it : src_veh.get_items( src_part ) ) {
temp_inv += it;
temp_inv.add_item_ref( it );
}
}
}
Expand All @@ -970,11 +971,11 @@ static bool are_requirements_nearby( const std::vector<tripoint> &loot_spots,
item welder( itype_welder, calendar::turn_zero );
welder.charges = veh_battery;
welder.set_flag( flag_PSEUDO );
temp_inv.add_item( welder );
temp_inv.add_item_copy( welder );
item soldering_iron( itype_soldering_iron, calendar::turn_zero );
soldering_iron.charges = veh_battery;
soldering_iron.set_flag( flag_PSEUDO );
temp_inv.add_item( soldering_iron );
temp_inv.add_item_copy( soldering_iron );
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ballistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static void drop_or_embed_projectile( const dealt_projectile_attack &attack )
if( effects.count( "SHATTER_SELF" ) ) {
// Drop the contents, not the thrown item
add_msg_if_player_sees( pt, _( "The %s shatters!" ), drop_item.tname() );
drop_item.visit_items( [&pt]( const item * it ) {
drop_item.visit_items( [&pt]( const item * it, item * ) {
get_map().add_item_or_charges( pt, *it );
return VisitResponse::NEXT;
} );
Expand Down
2 changes: 1 addition & 1 deletion src/butchery_requirements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void butchery_requirements::check_consistency()
}

std::pair<float, requirement_id> butchery_requirements::get_fastest_requirements(
const inventory &crafting_inv, creature_size size, butcher_type butcher ) const
const read_only_visitable &crafting_inv, creature_size size, butcher_type butcher ) const
{
for( const std::pair<const float, std::map<creature_size, std::map<butcher_type, requirement_id>>>
&riter : requirements ) {
Expand Down
4 changes: 2 additions & 2 deletions src/butchery_requirements.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "string_id.h"
#include "type_id.h"

class inventory;
class read_only_visitable;
class JsonObject;

enum class butcher_type : int;
Expand All @@ -29,7 +29,7 @@ class butchery_requirements

// tries to find the requirement with the highest speed bonus. if it fails it returns cata::nullopt
std::pair<float, requirement_id> get_fastest_requirements(
const inventory &crafting_inv, creature_size size, butcher_type butcher ) const;
const read_only_visitable &crafting_inv, creature_size size, butcher_type butcher ) const;

static void load_butchery_req( const JsonObject &jo, const std::string &src );
void load( const JsonObject &jo, const std::string & );
Expand Down
15 changes: 7 additions & 8 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,6 @@ std::string enum_to_string<blood_type>( blood_type data )

// *INDENT-OFF*
Character::Character() :
visitable<Character>(),
cached_time( calendar::before_time_starts ),
id( -1 ),
next_climate_control_check( calendar::before_time_starts ),
Expand Down Expand Up @@ -2903,7 +2902,7 @@ item *Character::invlet_to_item( const int linvlet )
name;
}
item *invlet_item = nullptr;
visit_items( [&invlet, &invlet_item]( item * it ) {
visit_items( [&invlet, &invlet_item]( item * it, item * ) {
if( it->invlet == invlet ) {
invlet_item = it;
return VisitResponse::ABORT;
Expand Down Expand Up @@ -3373,7 +3372,7 @@ std::vector<item_location> Character::find_reloadables()
{
std::vector<item_location> reloadables;

visit_items( [this, &reloadables]( item * node ) {
visit_items( [this, &reloadables]( item * node, item * ) {
if( !node->is_gun() && !node->is_magazine() ) {
return VisitResponse::NEXT;
}
Expand Down Expand Up @@ -9278,7 +9277,7 @@ void Character::recalculate_enchantment_cache()
// start by resetting the cache to all inventory items
*enchantment_cache = inv->get_active_enchantment_cache( *this );

visit_items( [&]( const item * it ) {
visit_items( [&]( const item * it, item * ) {
for( const enchantment &ench : it->get_enchantments() ) {
if( ench.is_active( *this, *it ) ) {
enchantment_cache->force_add( ench );
Expand Down Expand Up @@ -10810,7 +10809,7 @@ void Character::fall_asleep( const time_duration &duration )

void Character::migrate_items_to_storage( bool disintegrate )
{
inv->visit_items( [&]( const item * it ) {
inv->visit_items( [&]( const item * it, item * ) {
if( disintegrate ) {
if( try_add( *it ) == nullptr ) {
debugmsg( "ERROR: Could not put %s into inventory. Check if the profession has enough space.",
Expand Down Expand Up @@ -11192,7 +11191,7 @@ std::list<item> Character::use_charges( const itype_id &what, int qty, const int

bool has_tool_with_UPS = false;
// Detection of UPS tool
inv.visit_items( [ &what, &qty, &has_tool_with_UPS, &filter]( item * e ) {
inv.visit_items( [ &what, &qty, &has_tool_with_UPS, &filter]( item * e, item * ) {
if( filter( *e ) && e->typeId() == what && e->has_flag( flag_USE_UPS ) ) {
has_tool_with_UPS = true;
return VisitResponse::ABORT;
Expand All @@ -11204,7 +11203,7 @@ std::list<item> Character::use_charges( const itype_id &what, int qty, const int
get_map().use_charges( pos(), radius, what, qty, return_true<item> );
}
if( qty > 0 ) {
visit_items( [this, &what, &qty, &res, &del, &filter]( item * e ) {
visit_items( [this, &what, &qty, &res, &del, &filter]( item * e, item * ) {
if( e->use_charges( what, qty, res, pos(), filter ) ) {
del.push_back( e );
}
Expand Down Expand Up @@ -12854,7 +12853,7 @@ int Character::item_reload_cost( const item &it, const item &ammo, int qty ) con
} else if( ammo.is_ammo_container() ) {
int min_clamp = 0;
// find the first ammo in the container to get its charges
ammo.visit_items( [&min_clamp]( const item * it ) {
ammo.visit_items( [&min_clamp]( const item * it, item * ) {
if( it->is_ammo() ) {
min_clamp = it->charges;
return VisitResponse::ABORT;
Expand Down
26 changes: 20 additions & 6 deletions src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ enum class book_mastery {
MASTERED // can no longer increase skill by reading
};

class Character : public Creature, public visitable<Character>
class Character : public Creature, public visitable
{
public:
Character( const Character & ) = delete;
Expand Down Expand Up @@ -2000,7 +2000,7 @@ class Character : public Creature, public visitable<Character>
std::vector<const item *> all_items_with_flag( const flag_id &flag ) const;

bool has_charges( const itype_id &it, int quantity,
const std::function<bool( const item & )> &filter = return_true<item> ) const;
const std::function<bool( const item & )> &filter = return_true<item> ) const override;

// has_amount works ONLY for quantity.
// has_charges works ONLY for charges.
Expand Down Expand Up @@ -2556,7 +2556,7 @@ class Character : public Creature, public visitable<Character>
* @param obj Object to check for disassembly
* @param inv current crafting inventory
*/
ret_val<bool> can_disassemble( const item &obj, const inventory &inv ) const;
ret_val<bool> can_disassemble( const item &obj, const read_only_visitable &inv ) const;
item_location create_in_progress_disassembly( item_location target );

bool disassemble();
Expand All @@ -2566,11 +2566,11 @@ class Character : public Creature, public visitable<Character>
void complete_disassemble( item_location &target, const recipe &dis );

const requirement_data *select_requirements(
const std::vector<const requirement_data *> &, int batch, const inventory &,
const std::vector<const requirement_data *> &, int batch, const read_only_visitable &,
const std::function<bool( const item & )> &filter ) const;
comp_selection<item_comp>
select_item_component( const std::vector<item_comp> &components,
int batch, inventory &map_inv, bool can_cancel = false,
int batch, read_only_visitable &map_inv, bool can_cancel = false,
const std::function<bool( const item & )> &filter = return_true<item>, bool player_inv = true );
std::list<item> consume_items( const comp_selection<item_comp> &is, int batch,
const std::function<bool( const item & )> &filter = return_true<item> );
Expand All @@ -2581,7 +2581,7 @@ class Character : public Creature, public visitable<Character>
const std::function<bool( const item & )> &filter = return_true<item> );
bool consume_software_container( const itype_id &software_id );
comp_selection<tool_comp>
select_tool_component( const std::vector<tool_comp> &tools, int batch, inventory &map_inv,
select_tool_component( const std::vector<tool_comp> &tools, int batch, read_only_visitable &map_inv,
bool can_cancel = false, bool player_inv = true,
const std::function<int( int )> &charges_required_modifier = []( int i ) {
return i;
Expand Down Expand Up @@ -2680,6 +2680,20 @@ class Character : public Creature, public visitable<Character>
/** Handles the still hard-coded effects. */
void hardcoded_effects( effect &it );

// inherited from visitable
bool has_quality( const quality_id &qual, int level = 1, int qty = 1 ) const override;
int max_quality( const quality_id &qual ) const override;
VisitResponse visit_items( const std::function<VisitResponse( item *, item * )> &func ) const
override;
std::list<item> remove_items_with( const std::function<bool( const item & )> &filter,
int count = INT_MAX ) override;
int charges_of( const itype_id &what, int limit = INT_MAX,
const std::function<bool( const item & )> &filter = return_true<item>,
const std::function<void( int )> &visitor = nullptr ) const override;
int amount_of( const itype_id &what, bool pseudo = true,
int limit = INT_MAX,
const std::function<bool( const item & )> &filter = return_true<item> ) const override;

protected:
Character();
Character( Character && );
Expand Down
7 changes: 4 additions & 3 deletions src/construction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ static std::map<construction_str_id, construction_id> construction_id_map;
// Helper functions, nobody but us needs to call these.
static bool can_construct( const construction_group_str_id &group );
static bool can_construct( const construction &con );
static bool player_can_build( player &p, const inventory &inv,
static bool player_can_build( player &p, const read_only_visitable &inv,
const construction_group_str_id &group );
static bool player_can_see_to_build( player &p, const construction_group_str_id &group );
static void place_construction( const construction_group_str_id &group );
Expand Down Expand Up @@ -837,7 +837,8 @@ construction_id construction_menu( const bool blueprint )
return ret;
}

bool player_can_build( player &p, const inventory &inv, const construction_group_str_id &group )
bool player_can_build( player &p, const read_only_visitable &inv,
const construction_group_str_id &group )
{
// check all with the same group to see if player can build any
std::vector<construction *> cons = constructions_by_group( group );
Expand All @@ -849,7 +850,7 @@ bool player_can_build( player &p, const inventory &inv, const construction_group
return false;
}

bool player_can_build( player &p, const inventory &inv, const construction &con )
bool player_can_build( player &p, const read_only_visitable &inv, const construction &con )
{
if( p.has_trait( trait_DEBUG_HS ) ) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/construction.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void reset_constructions();
construction_id construction_menu( bool blueprint );
void complete_construction( player *p );
bool can_construct( const construction &con, const tripoint &p );
bool player_can_build( player &p, const inventory &inv, const construction &con );
bool player_can_build( player &p, const read_only_visitable &inv, const construction &con );
void check_constructions();
void finalize_constructions();

Expand Down
2 changes: 1 addition & 1 deletion src/consumption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,7 @@ bool Character::can_consume( const item &it ) const
item &Character::get_consumable_from( item &it ) const
{
item *ret = nullptr;
it.visit_items( [&]( item * it ) {
it.visit_items( [&]( item * it, item * ) {
if( can_consume_as_is( *it ) ) {
ret = it;
return VisitResponse::ABORT;
Expand Down
4 changes: 2 additions & 2 deletions src/craft_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ skill_id craft_command::get_skill_id()
}

std::vector<comp_selection<item_comp>> craft_command::check_item_components_missing(
const inventory &map_inv ) const
const read_only_visitable &map_inv ) const
{
std::vector<comp_selection<item_comp>> missing;

Expand Down Expand Up @@ -355,7 +355,7 @@ std::vector<comp_selection<item_comp>> craft_command::check_item_components_miss
}

std::vector<comp_selection<tool_comp>> craft_command::check_tool_components_missing(
const inventory &map_inv ) const
const read_only_visitable &map_inv ) const
{
std::vector<comp_selection<tool_comp>> missing;

Expand Down
6 changes: 3 additions & 3 deletions src/craft_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class Character;
class JsonIn;
class JsonOut;
class inventory;
class read_only_visitable;
class item;
struct item_comp;
struct tool_comp;
Expand Down Expand Up @@ -116,10 +116,10 @@ class craft_command

/** Checks if tools we selected in a previous call to execute() are still available. */
std::vector<comp_selection<item_comp>> check_item_components_missing(
const inventory &map_inv ) const;
const read_only_visitable &map_inv ) const;
/** Checks if items we selected in a previous call to execute() are still available. */
std::vector<comp_selection<tool_comp>> check_tool_components_missing(
const inventory &map_inv ) const;
const read_only_visitable &map_inv ) const;

/** Creates a continue pop up asking to continue crafting and listing the missing components */
bool query_continue( const std::vector<comp_selection<item_comp>> &missing_items,
Expand Down
9 changes: 5 additions & 4 deletions src/crafting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1448,7 +1448,8 @@ bool Character::can_continue_craft( item &craft )
return true;
}
const requirement_data *Character::select_requirements(
const std::vector<const requirement_data *> &alternatives, int batch, const inventory &inv,
const std::vector<const requirement_data *> &alternatives, int batch,
const read_only_visitable &inv,
const std::function<bool( const item & )> &filter ) const
{
cata_assert( !alternatives.empty() );
Expand Down Expand Up @@ -1482,7 +1483,7 @@ const requirement_data *Character::select_requirements(
/* selection of component if a recipe requirement has multiple options (e.g. 'duct tap' or 'welder') */
comp_selection<item_comp> Character::select_item_component( const std::vector<item_comp>
&components,
int batch, inventory &map_inv, bool can_cancel,
int batch, read_only_visitable &map_inv, bool can_cancel,
const std::function<bool( const item & )> &filter, bool player_inv )
{
std::vector<item_comp> player_has;
Expand Down Expand Up @@ -1763,7 +1764,7 @@ bool Character::consume_software_container( const itype_id &software_id )

comp_selection<tool_comp>
Character::select_tool_component( const std::vector<tool_comp> &tools, int batch,
inventory &map_inv, bool can_cancel, bool player_inv,
read_only_visitable &map_inv, bool can_cancel, bool player_inv,
const std::function<int( int )> &charges_required_modifier )
{

Expand Down Expand Up @@ -2032,7 +2033,7 @@ void Character::consume_tools( const std::vector<tool_comp> &tools, int batch )
consume_tools( select_tool_component( tools, batch, map_inv ), batch );
}

ret_val<bool> Character::can_disassemble( const item &obj, const inventory &inv ) const
ret_val<bool> Character::can_disassemble( const item &obj, const read_only_visitable &inv ) const
{
if( !obj.is_disassemblable() ) {
return ret_val<bool>::make_failure( _( "You cannot disassemble this." ) );
Expand Down
2 changes: 1 addition & 1 deletion src/iexamine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3602,7 +3602,7 @@ void iexamine::tree_maple_tapped( player &p, const tripoint &examp )
if( it.will_spill() || it.is_watertight_container() ) {
container = &it;

it.visit_items( [&charges, &has_sap]( const item * it ) {
it.visit_items( [&charges, &has_sap]( const item * it, item * ) {
if( it->typeId() == itype_maple_syrup ) {
has_sap = true;
charges = it->charges;
Expand Down
Loading