Skip to content

Commit

Permalink
add BasicInventory
Browse files Browse the repository at this point in the history
  • Loading branch information
jube committed Feb 13, 2025
1 parent b572926 commit 814c08b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
32 changes: 18 additions & 14 deletions code/bits/InventoryState.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,36 @@

namespace akgr {

namespace {

void add_item_to(const DataReference<ItemData>& data, std::vector<InventoryItemState>& items, int32_t count)
{
if (auto iterator = std::find_if(items.begin(), items.end(), [&data](const InventoryItemState& state) { return data.id == state.data.id; }); iterator != items.end()) {
iterator->count += count;
} else {
items.push_back({ data, count });
}
}
/*
* BasicInventoryState
*/

void BasicInventoryState::add_item(const DataReference<ItemData>& data, int32_t count)
{
if (auto iterator = std::find_if(items.begin(), items.end(), [&data](const InventoryItemState& state) { return data.id == state.data.id; }); iterator != items.end()) {
iterator->count += count;
} else {
items.push_back({ data, count });
}
}

void InventoryState::add_item(const DataReference<ItemData>& data, int32_t count)
/*
* InventoryState
*/

void InventoryState::add_regular_item(const DataReference<ItemData>& data, int32_t count)
{
add_item_to(data, items, count);
regular.add_item(data, count);
}

void InventoryState::add_quest_item(const DataReference<ItemData>& data, int32_t count)
{
add_item_to(data, quest_items, count);
quest.add_item(data, count);
}

void InventoryState::transfer_to_quest_items(const DataReference<ItemData>& data, int32_t count)
{
if (auto iterator = std::find_if(items.begin(), items.end(), [&data](const InventoryItemState& state) { return data.id == state.data.id; }); iterator != items.end()) {
if (auto iterator = std::find_if(quest.items.begin(), quest.items.end(), [&data](const InventoryItemState& state) { return data.id == state.data.id; }); iterator != quest.items.end()) {
assert(iterator->count >= count);
iterator->count -= count;
add_quest_item(data, count);
Expand Down
17 changes: 14 additions & 3 deletions code/bits/InventoryState.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,29 @@ namespace akgr {
return ar | state.data | state.count;
}

struct InventoryState {
struct BasicInventoryState {
std::vector<InventoryItemState> items;
std::vector<InventoryItemState> quest_items;

void add_item(const DataReference<ItemData>& data, int32_t count = 1);
};

template<typename Archive>
Archive& operator|(Archive& ar, gf::MaybeConst<BasicInventoryState, Archive>& state) {
return ar | state.items;
}

struct InventoryState {
BasicInventoryState regular;
BasicInventoryState quest;

void add_regular_item(const DataReference<ItemData>& data, int32_t count = 1);
void add_quest_item(const DataReference<ItemData>& data, int32_t count = 1);
void transfer_to_quest_items(const DataReference<ItemData>& data, int32_t count = 1);
};

template<typename Archive>
Archive& operator|(Archive& ar, gf::MaybeConst<InventoryState, Archive>& state) {
return ar | state.items | state.quest_items;
return ar | state.regular | state.quest;
}

}
Expand Down
4 changes: 2 additions & 2 deletions code/bits/WorldModel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ namespace akgr {
for (auto& item : state.items) {
if (item.status.test(ItemStatus::Picked)) {
need_update = true;
state.hero.inventory.add_item(item.data);
state.hero.inventory.add_regular_item(item.data);
check_quest_farm(item.data->label.tag);
}
}
Expand Down Expand Up @@ -324,7 +324,7 @@ namespace akgr {
auto& quest_state = std::get<FarmQuestState>(quest.features);
quest_state.amount = 0;

for (auto& item : state.hero.inventory.items) {
for (auto& item : state.hero.inventory.regular.items) {
if (quest_data.item->label.id == item.data->label.id) {
quest_state.amount += item.count;

Expand Down

0 comments on commit 814c08b

Please sign in to comment.