-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
temp_crafting_inventory
- lightweight alternative to the `inven…
…tory` for crafting queries
- Loading branch information
Showing
5 changed files
with
163 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
#include "temp_crafting_inventory.h" | ||
|
||
temp_crafting_inventory::temp_crafting_inventory( const read_only_visitable &v ) | ||
{ | ||
add_all_ref( v ); | ||
} | ||
|
||
size_t temp_crafting_inventory::size() const | ||
{ | ||
return items.size(); | ||
} | ||
|
||
void temp_crafting_inventory::clear() | ||
{ | ||
items.clear(); | ||
temp_owned_items.clear(); | ||
} | ||
|
||
void temp_crafting_inventory::add_item_ref( item &item ) | ||
{ | ||
items.insert( &item ); | ||
} | ||
|
||
item &temp_crafting_inventory::add_item_copy( const item &item ) | ||
{ | ||
const auto iter = temp_owned_items.insert( item ); | ||
items.insert( &( *iter ) ); | ||
return *iter; | ||
} | ||
|
||
void temp_crafting_inventory::add_all_ref( const read_only_visitable &v ) | ||
{ | ||
v.visit_items( [&]( item * it, item * ) { | ||
add_item_ref( *it ); | ||
return VisitResponse::SKIP; | ||
} ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#pragma once | ||
#ifndef CATA_SRC_TEMP_CRAFTING_INVENTORY_H | ||
#define CATA_SRC_TEMP_CRAFTING_INVENTORY_H | ||
|
||
#include "colony.h" | ||
#include "item.h" | ||
#include "visitable.h" | ||
|
||
/** | ||
* A transient add-only list of item references and temporary pseudo-items, that implements `read_only_visitable` | ||
* and thus can answer crafting queries (see crafting.cpp and requirement_data::can_make_with_inventory). | ||
*/ | ||
class temp_crafting_inventory : public read_only_visitable | ||
{ | ||
public: | ||
temp_crafting_inventory() = default; | ||
temp_crafting_inventory( const read_only_visitable & ); | ||
|
||
size_t size() const; | ||
|
||
void clear(); | ||
|
||
/** | ||
* Adds item reference to this container. | ||
* @note container doesn't own the added reference, meaning added item should outlive this container. | ||
*/ | ||
void add_item_ref( item &item ); | ||
|
||
/** | ||
* Adds all (top-level) items from the given visitable. | ||
* @note container doesn't own the added references, meaning added items should outlive this container. | ||
*/ | ||
void add_all_ref( const read_only_visitable &v ); | ||
|
||
/** | ||
* Adds item copy to this container. | ||
* Container will own a copy of the given item. | ||
* @return reference to the added item within the container | ||
*/ | ||
item &add_item_copy( const item &item ); | ||
|
||
// inherited from visitable | ||
VisitResponse visit_items( const std::function<VisitResponse( item *, item * )> &func ) const | ||
override; | ||
|
||
private: | ||
// list of all items in this container | ||
cata::colony<item *> items; | ||
// copies of "owned" items added by `add_item_copy` | ||
cata::colony<item> temp_owned_items; | ||
}; | ||
|
||
#endif // CATA_SRC_TEMP_CRAFTING_INVENTORY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "catch/catch.hpp" | ||
|
||
#include "temp_crafting_inventory.h" | ||
#include "../src/temp_crafting_inventory.h" | ||
|
||
TEST_CASE( "temp_crafting_inv_test_amount", "[crafting][inventory]" ) | ||
{ | ||
temp_crafting_inventory inv; | ||
CHECK( inv.size() == 0 ); | ||
|
||
item gum( "test_gum", calendar::turn_zero, item::default_charges_tag{} ); | ||
|
||
inv.add_item_ref( gum ); | ||
CHECK( inv.size() == 1 ); | ||
|
||
CHECK( inv.amount_of( itype_id( "test_gum" ) ) == 1 ); | ||
CHECK( inv.has_amount( itype_id( "test_gum" ), 1 ) ); | ||
CHECK( inv.has_charges( itype_id( "test_gum" ), 10 ) ); | ||
CHECK_FALSE( inv.has_charges( itype_id( "test_gum" ), 11 ) ); | ||
|
||
inv.clear(); | ||
CHECK( inv.size() == 0 ); | ||
} | ||
|
||
TEST_CASE( "temp_crafting_inv_test_quality", "[crafting][inventory]" ) | ||
{ | ||
temp_crafting_inventory inv; | ||
inv.add_item_copy( item( "test_halligan" ) ); | ||
|
||
CHECK( inv.has_quality( quality_id( "HAMMER" ), 1 ) ); | ||
CHECK( inv.has_quality( quality_id( "HAMMER" ), 2 ) ); | ||
CHECK_FALSE( inv.has_quality( quality_id( "HAMMER" ), 3 ) ); | ||
CHECK( inv.has_quality( quality_id( "DIG" ), 1 ) ); | ||
CHECK_FALSE( inv.has_quality( quality_id( "AXE" ) ) ); | ||
|
||
inv.add_item_copy( item( "test_fire_ax" ) ); | ||
CHECK( inv.has_quality( quality_id( "AXE" ) ) ); | ||
|
||
CHECK( inv.max_quality( quality_id( "PRY" ) ) == 4 ); | ||
} |