Skip to content

Commit

Permalink
[objects] add: Inventory::size
Browse files Browse the repository at this point in the history
  • Loading branch information
jd28 committed Nov 26, 2024
1 parent d298346 commit 0535553
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 6 deletions.
4 changes: 4 additions & 0 deletions docs/fake/rollnw/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,10 @@ def remove_item(self, item: "Item") -> bool:
"""Removes an item from an inventory."""
pass

def size(self) -> int:
"""Gets number of items in the inventory"""
pass


class ClassEntry:
"""Class level data
Expand Down
5 changes: 5 additions & 0 deletions lib/nw/objects/Inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ void Inventory::set_growable(bool value)
growable_ = value;
}

size_t Inventory::size() const noexcept
{
return items.size();
}

std::pair<uint16_t, uint16_t> Inventory::slot_to_xy(InventorySlot slot) const noexcept
{
uint16_t x = static_cast<uint16_t>(slot.col);
Expand Down
3 changes: 3 additions & 0 deletions lib/nw/objects/Inventory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ struct Inventory {
/// Sets the inventory to allow adding pages dynamically (for stores)
void set_growable(bool value);

/// Gets number of items in the inventory
size_t size() const noexcept;

/// Converts coordinates from our page, row, col to bioware x,y cooardinates
std::pair<uint16_t, uint16_t> slot_to_xy(InventorySlot slot) const noexcept;

Expand Down
3 changes: 2 additions & 1 deletion rollnw-py/wrapper_objects_components.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ void init_component_inventory(py::module& m)
}
return pylist;
})
.def("remove_item", &nw::Inventory::remove_item);
.def("remove_item", &nw::Inventory::remove_item)
.def("size", &nw::Inventory::size);
}

void init_component_levelhistory(py::module& m)
Expand Down
1 change: 1 addition & 0 deletions rollnw-stubs/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,7 @@ class Inventory:
def debug(self) -> str: ...
def items(self) -> List["Item"]: ...
def remove_item(self, item: "Item") -> bool: ...
def size(self) -> int: ...


class InventoryItem:
Expand Down
6 changes: 3 additions & 3 deletions tests/objects_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ TEST(Player, Inventory)
auto pl = nwk::objects().load_player("CDKEY", "daeris1");
EXPECT_TRUE(pl);

EXPECT_EQ(pl->inventory.items.size(), 4);
EXPECT_EQ(pl->inventory.size(), 4);
auto slot1 = pl->inventory.find_slot(1, 1);
auto [x1, y1] = pl->inventory.slot_to_xy(slot1);
EXPECT_EQ(x1, 1);
Expand All @@ -93,11 +93,11 @@ TEST(Player, Inventory)
auto it = pl->inventory.items[0].item.as<nw::Item*>();
EXPECT_TRUE(pl->inventory.remove_item(it));
EXPECT_FALSE(pl->inventory.has_item(it));
EXPECT_EQ(pl->inventory.items.size(), 3);
EXPECT_EQ(pl->inventory.size(), 3);
EXPECT_TRUE(pl->inventory.can_add_item(it));
EXPECT_TRUE(pl->inventory.add_item(it));
EXPECT_TRUE(pl->inventory.has_item(it));
EXPECT_EQ(pl->inventory.items.size(), 4);
EXPECT_EQ(pl->inventory.size(), 4);
}

TEST(Player, PerPartColor)
Expand Down
4 changes: 2 additions & 2 deletions tests/objects_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ TEST(Store, JsonDeserialize)
EXPECT_EQ(ent->common.resref, "storethief002");
EXPECT_TRUE(ent->blackmarket);
EXPECT_EQ(ent->blackmarket_markdown, 25);
EXPECT_GT(ent->inventory.weapons.items.size(), 0);
EXPECT_GT(ent->inventory.weapons.size(), 0);
EXPECT_EQ(ent->inventory.weapons.items[0].item.as<nw::Resref>(), "nw_wswdg001");
}

Expand Down Expand Up @@ -73,7 +73,7 @@ TEST(Store, GffDeserialize)
EXPECT_EQ(ent->common.resref, "storethief002");
EXPECT_TRUE(ent->blackmarket);
EXPECT_EQ(ent->blackmarket_markdown, 25);
EXPECT_TRUE(ent->inventory.weapons.items.size() > 0);
EXPECT_TRUE(ent->inventory.weapons.size() > 0);
EXPECT_EQ(ent->inventory.weapons.items[0].item.as<nw::Item*>()->common.resref, "nw_wswdg001");
}

Expand Down

0 comments on commit 0535553

Please sign in to comment.