Skip to content

Commit

Permalink
Add container:getItems() function and option stash moving items to su…
Browse files Browse the repository at this point in the history
…pply stash and depot (#169)

Part of code by @nekiro (https://github.com/nekiro)
otland/forgottenserver#3160

container:getItems() function
New method which will return all container items, optional bool recursive to search in sub containers of container.
Usage:
```
local items = container:getItems(true)
for _, item in ipairs(items) do
	print(item:getName())
end
```

My part of code:
- New stash option.
- Stow an container to your Stash(or right click container->Stow Item), the stowable items go your stash, and non-stacked items go to the selected depot chest(default is 4, config.lua configurable).
  • Loading branch information
omeranha authored Feb 28, 2022
1 parent caf0a97 commit 844530b
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config.lua.dist
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ onlyPremiumAccount = false
weatherRain = false
thunderEffect = false
allConsoleLog = false
-- stashMoving = true, stow an container inside your stash
-- the non-stackable items will be moved to the selected depot chest(I - XVIII).
stashMoving = false
depotChest = 4

-- Stamina in Trainers
staminaTrainer = false
Expand Down
2 changes: 2 additions & 0 deletions src/config/config_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ enum booleanConfig_t {
SORT_LOOT_BY_CHANCE,
TOGLE_SAVE_INTERVAL,
TOGLE_SAVE_INTERVAL_CLEAN_MAP,
STASH_MOVING,
TOGLE_IMBUEMENT_SHRINE_STORAGE,

LAST_BOOLEAN_CONFIG
Expand Down Expand Up @@ -154,6 +155,7 @@ enum integerConfig_t {
SAVE_INTERVAL_TIME,
MAX_ALLOWED_ON_A_DUMMY,
FREE_QUEST_STAGE,
DEPOTCHEST,

LAST_INTEGER_CONFIG
};
Expand Down
2 changes: 2 additions & 0 deletions src/config/configmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ bool ConfigManager::load()
boolean[CLASSIC_EQUIPMENT_SLOTS] = getGlobalBoolean(L, "classicEquipmentSlots", false);
boolean[CLASSIC_ATTACK_SPEED] = getGlobalBoolean(L, "classicAttackSpeed", false);
boolean[SCRIPTS_CONSOLE_LOGS] = getGlobalBoolean(L, "showScriptsLogInConsole", true);
boolean[STASH_MOVING] = getGlobalBoolean(L, "stashMoving", false);
boolean[ALLOW_BLOCK_SPAWN] = getGlobalBoolean(L, "allowBlockSpawn", true);
boolean[REMOVE_WEAPON_AMMO] = getGlobalBoolean(L, "removeWeaponAmmunition", true);
boolean[REMOVE_WEAPON_CHARGES] = getGlobalBoolean(L, "removeWeaponCharges", true);
Expand Down Expand Up @@ -246,6 +247,7 @@ bool ConfigManager::load()
integer[SAVE_INTERVAL_TIME] = getGlobalNumber(L, "saveIntervalTime", 1);
integer[MAX_ALLOWED_ON_A_DUMMY] = getGlobalNumber(L, "maxAllowedOnADummy", 1);
integer[FREE_QUEST_STAGE] = getGlobalNumber(L, "freeQuestStage", 1);
integer[DEPOTCHEST] = getGlobalNumber(L, "depotChest", 4);

floating[RATE_HEALTH_REGEN] = getGlobalFloat(L, "rateHealthRegen", 1.0);
floating[RATE_HEALTH_REGEN_SPEED] = getGlobalFloat(L, "rateHealthRegenSpeed", 1.0);
Expand Down
13 changes: 13 additions & 0 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3462,6 +3462,10 @@ void Player::stashContainer(StashContainerList itemDict)
}

retString << "Stowed " << totalStowed << " object" << (totalStowed > 1 ? "s." : ".");
if (moved) {
retString << " Moved " << movedItems << " object" << (movedItems > 1 ? "s." : ".");
movedItems = 0;
}
sendTextMessage(MESSAGE_STATUS, retString.str());
}

Expand Down Expand Up @@ -5593,6 +5597,15 @@ void Player::stowItem(Item* item, uint32_t count, bool allItems) {
}
} else if (item->getContainer()) {
itemDict = item->getContainer()->getStowableItems();
for (Item* containerItem : item->getContainer()->getItems()) {
uint32_t depotChest = g_configManager().getNumber(DEPOTCHEST);
bool validDepot = depotChest > 0 && depotChest < 19;
if (g_configManager().getBoolean(STASH_MOVING) && containerItem && !containerItem->isStackable() && validDepot) {
g_game.internalMoveItem(containerItem->getParent(), getDepotChest(depotChest, true), INDEX_WHEREEVER, containerItem, containerItem->getItemCount(), nullptr);
movedItems++;
moved = true;
}
}
} else {
itemDict.push_back(std::pair<Item*, uint32_t>(item, count));
}
Expand Down
2 changes: 2 additions & 0 deletions src/creatures/players/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -2078,6 +2078,7 @@ class Player final : public Creature, public Cylinder
uint16_t staminaXpBoost = 100;
int16_t lastDepotId = -1;
StashItemList stashItems; // [ClientID] = amount
uint32_t movedItems = 0;

// Bestiary
bool charmExpansion = false;
Expand Down Expand Up @@ -2148,6 +2149,7 @@ class Player final : public Creature, public Cylinder
bool supplyStash = false; // Menu option 'stow, stow container ...'
bool marketMenu = false; // Menu option 'show in market'
bool exerciseTraining = false;
bool moved = false;

static uint32_t playerAutoID;

Expand Down
15 changes: 15 additions & 0 deletions src/items/containers/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,21 @@ Thing* Container::getThing(size_t index) const
return getItemByIndex(index);
}

ItemVector Container::getItems(bool recursive /*= false*/) const
{
ItemVector containerItems;
if (recursive) {
for (ContainerIterator it = iterator(); it.hasNext(); it.advance()) {
containerItems.push_back(*it);
}
} else {
for (Item* item : itemlist) {
containerItems.push_back(item);
}
}
return containerItems;
}

void Container::postAddNotification(Thing* thing, const Cylinder* oldParent, int32_t index, CylinderLink_t)
{
Cylinder* topParent = getTopParent();
Expand Down
3 changes: 3 additions & 0 deletions src/items/containers/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "items/cylinder.h"
#include "items/item.h"
#include "items/tile.h"

class Container;
class DepotChest;
Expand Down Expand Up @@ -160,6 +161,8 @@ class Container : public Item, public Cylinder
uint32_t getItemTypeCount(uint16_t itemId, int32_t subType = -1) const override final;
std::map<uint32_t, uint32_t>& getAllItemTypeCount(std::map<uint32_t, uint32_t>& countMap) const override final;
Thing* getThing(size_t index) const override final;

ItemVector getItems(bool recursive = false) const;

void postAddNotification(Thing* thing, const Cylinder* oldParent, int32_t index, CylinderLink_t link = LINK_OWNER) override;
void postRemoveNotification(Thing* thing, const Cylinder* newParent, int32_t index, CylinderLink_t link = LINK_OWNER) override;
Expand Down
23 changes: 23 additions & 0 deletions src/lua/functions/items/container_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,29 @@ int ContainerFunctions::luaContainerGetContentDescription(lua_State* L) {
return 1;
}

int ContainerFunctions::luaContainerGetItems(lua_State* L) {
// container:getItems([recursive = false])
const Container* container = getUserdata<Container>(L, 1);
if (!container) {
lua_pushnil(L);
return 1;
}

bool recursive = getBoolean(L, 2, false);
std::vector<Item*> items = container->getItems(recursive);

lua_createtable(L, static_cast<int>(items.size()), 0);

int index = 0;
for (Item* item : items) {
index++;
pushUserdata(L, item);
setItemMetatable(L, -1, item);
lua_rawseti(L, -2, index);
}
return 1;
}

int ContainerFunctions::luaContainerRegisterReward(lua_State* L) {
// container:registerReward()
Container* container = getUserdata<Container>(L, 1);
Expand Down
2 changes: 2 additions & 0 deletions src/lua/functions/items/container_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ContainerFunctions final : LuaScriptInterface {
registerMethod(L, "Container", "getCapacity", ContainerFunctions::luaContainerGetCapacity);
registerMethod(L, "Container", "getEmptySlots", ContainerFunctions::luaContainerGetEmptySlots);
registerMethod(L, "Container", "getContentDescription", ContainerFunctions::luaContainerGetContentDescription);
registerMethod(L, "Container", "getItems", ContainerFunctions::luaContainerGetItems);
registerMethod(L, "Container", "getItemHoldingCount", ContainerFunctions::luaContainerGetItemHoldingCount);
registerMethod(L, "Container", "getItemCountById", ContainerFunctions::luaContainerGetItemCountById);

Expand All @@ -54,6 +55,7 @@ class ContainerFunctions final : LuaScriptInterface {
static int luaContainerGetEmptySlots(lua_State* L);

static int luaContainerGetContentDescription(lua_State* L);
static int luaContainerGetItems(lua_State* L);
static int luaContainerGetItemHoldingCount(lua_State* L);
static int luaContainerGetItemCountById(lua_State* L);

Expand Down

1 comment on commit 844530b

@Aerwix
Copy link
Contributor

@Aerwix Aerwix commented on 844530b Feb 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non stackable items are going to Depot?
image

Please sign in to comment.