Skip to content
This repository has been archived by the owner on May 4, 2023. It is now read-only.

Feature/check container when adding item to player #42

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
15 changes: 6 additions & 9 deletions data/scripts/talkactions/god/create_item.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,16 @@ function createItem.onSay(player, words, param)
end
end

local result = player:addItem(itemType:getId(), count)
if result then
local item = Game.createItem(itemType:getId(), 1)
local returnValue = player:addItemEx(item)
if returnValue == RETURNVALUE_NOERROR then
if not itemType:isStackable() then
if type(result) == "table" then
for _, item in ipairs(result) do
item:decay()
end
else
result:decay()
end
item:decay()
end
player:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN)
return
end
player:sendCancelMessage(returnValue)
return false
end

Expand Down
14 changes: 3 additions & 11 deletions src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9408,8 +9408,7 @@ int LuaScriptInterface::luaPlayerAddItem(lua_State* L)

int LuaScriptInterface::luaPlayerAddItemEx(lua_State* L)
{
// player:addItemEx(item[, canDropOnMap = false[, index = INDEX_WHEREEVER[, flags = 0]]])
// player:addItemEx(item[, canDropOnMap = true[, slot = CONST_SLOT_WHEREEVER]])
// player:addItemEx(item[, canDropOnMap, slot = CONST_SLOT_WHEREEVER]])
Item* item = getUserdata<Item>(L, 2);
if (!item) {
reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND));
Expand All @@ -9430,15 +9429,8 @@ int LuaScriptInterface::luaPlayerAddItemEx(lua_State* L)
}

bool canDropOnMap = getBoolean(L, 3, false);
ReturnValue returnValue;
if (canDropOnMap) {
slots_t slot = getNumber<slots_t>(L, 4, CONST_SLOT_WHEREEVER);
returnValue = g_game().internalPlayerAddItem(player, item, true, slot);
} else {
int32_t index = getNumber<int32_t>(L, 4, INDEX_WHEREEVER);
uint32_t flags = getNumber<uint32_t>(L, 5, 0);
returnValue = g_game().internalAddItem(player, item, index, flags);
}
slots_t slot = getNumber<slots_t>(L, 4, CONST_SLOT_WHEREEVER);
ReturnValue returnValue = g_game().internalPlayerAddItem(player, item, canDropOnMap, slot);

if (returnValue == RETURNVALUE_NOERROR) {
ScriptEnvironment::removeTempItem(item);
Expand Down
45 changes: 24 additions & 21 deletions src/movement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ uint32_t MoveEvents::onPlayerEquip(Player* player, Item* item, slots_t slot, boo
{
MoveEvent* moveEvent = getEvent(item, MOVE_EVENT_EQUIP, slot);
if (!moveEvent) {
return 1;
return player->containerQueryAdd(item, slot);
}
return moveEvent->fireEquip(player, item, slot, isCheck);
}
Expand All @@ -376,7 +376,7 @@ uint32_t MoveEvents::onPlayerDeEquip(Player* player, Item* item, slots_t slot)
{
MoveEvent* moveEvent = getEvent(item, MOVE_EVENT_DEEQUIP, slot);
if (!moveEvent) {
return 1;
return RETURNVALUE_NOERROR;
}
return moveEvent->fireEquip(player, item, slot, false);
}
Expand Down Expand Up @@ -598,26 +598,26 @@ uint32_t MoveEvent::RemoveItemField(Item*, Item*, const Position&)
uint32_t MoveEvent::EquipItem(MoveEvent* moveEvent, Player* player, Item* item, slots_t slot, bool isCheck)
{
if (player->isItemAbilityEnabled(slot)) {
return 1;
return player->containerQueryAdd(item, slot);
}

if (!player->hasFlag(PlayerFlag_IgnoreWeaponCheck) && moveEvent->getWieldInfo() != 0) {
if (player->getLevel() < moveEvent->getReqLevel() || player->getMagicLevel() < moveEvent->getReqMagLv()) {
return 0;
return RETURNVALUE_CANNOTBEDRESSED;
}

if (moveEvent->isPremium() && !player->isPremium()) {
return 0;
return RETURNVALUE_CANNOTBEDRESSED;
}

const VocEquipMap& vocEquipMap = moveEvent->getVocEquipMap();
if (!vocEquipMap.empty() && vocEquipMap.find(player->getVocationId()) == vocEquipMap.end()) {
return 0;
return RETURNVALUE_CANNOTBEDRESSED;
}
}

if (isCheck) {
return 1;
return player->containerQueryAdd(item, slot);
}

const ItemType& it = Item::items[item->getID()];
Expand All @@ -628,7 +628,7 @@ uint32_t MoveEvent::EquipItem(MoveEvent* moveEvent, Player* player, Item* item,
}

if (!it.abilities) {
return 1;
return RETURNVALUE_NOERROR;
}

if (it.abilities->invisible) {
Expand Down Expand Up @@ -712,13 +712,13 @@ uint32_t MoveEvent::EquipItem(MoveEvent* moveEvent, Player* player, Item* item,
player->addScheduledUpdates((PlayerUpdate_Stats | PlayerUpdate_Skills));
}

return 1;
return RETURNVALUE_NOERROR;
}

uint32_t MoveEvent::DeEquipItem(MoveEvent*, Player* player, Item* item, slots_t slot, bool)
{
if (!player->isItemAbilityEnabled(slot)) {
return 1;
return RETURNVALUE_NOERROR;
}

player->setItemAbility(slot, false);
Expand All @@ -729,7 +729,7 @@ uint32_t MoveEvent::DeEquipItem(MoveEvent*, Player* player, Item* item, slots_t
}

if (!it.abilities) {
return 1;
return RETURNVALUE_NOERROR;
}

if (it.abilities->invisible) {
Expand Down Expand Up @@ -793,7 +793,7 @@ uint32_t MoveEvent::DeEquipItem(MoveEvent*, Player* player, Item* item, slots_t
player->addScheduledUpdates((PlayerUpdate_Stats | PlayerUpdate_Skills));
}

return 1;
return RETURNVALUE_NOERROR;
}

bool MoveEvent::loadFunction(const pugi::xml_attribute& attr, bool isScripted)
Expand Down Expand Up @@ -869,16 +869,19 @@ bool MoveEvent::executeStep(Creature* creature, Item* item, const Position& pos)

uint32_t MoveEvent::fireEquip(Player* player, Item* item, slots_t slot, bool isCheck)
{
if (scripted) {
if (!equipFunction || equipFunction(this, player, item, slot, isCheck) == 1) {
if (executeEquip(player, item, slot, isCheck)) {
return 1;
}
}
return 0;
} else {
return equipFunction(this, player, item, slot, isCheck);
uint32_t ret = RETURNVALUE_NOERROR;

if (equipFunction) {
ret = equipFunction(this, player, item, slot, isCheck);
}

if (scripted && ret == RETURNVALUE_NOERROR) {
executeEquip(player, item, slot, isCheck);
} else if (scripted) {
return RETURNVALUE_CANNOTBEDRESSED;
}

return ret;
}

bool MoveEvent::executeEquip(Player* player, Item* item, slots_t slot, bool isCheck)
Expand Down
33 changes: 28 additions & 5 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2470,7 +2470,7 @@ ReturnValue Player::queryAdd(int32_t index, const Thing& thing, uint32_t count,

case CONST_SLOT_BACKPACK: {
if (slotPosition & SLOTP_BACKPACK) {
ret = RETURNVALUE_NOERROR;
ret = containerQueryAdd(item, static_cast<slots_t>(index));
}
break;
}
Expand Down Expand Up @@ -2624,10 +2624,33 @@ ReturnValue Player::queryAdd(int32_t index, const Thing& thing, uint32_t count,
return RETURNVALUE_NOTENOUGHCAPACITY;
}

if (!g_moveEvents().onPlayerEquip(const_cast<Player*>(this), const_cast<Item*>(item), static_cast<slots_t>(index), true)) {
return RETURNVALUE_CANNOTBEDRESSED;
}
return ret;
return static_cast<ReturnValue>(g_moveEvents().onPlayerEquip(const_cast<Player*>(this), const_cast<Item*>(item), static_cast<slots_t>(index), true));
}

ReturnValue Player::containerQueryAdd(const Item* item, slots_t slot) const
{
if (!item) {
return RETURNVALUE_NOTPOSSIBLE;
}

if (slot != CONST_SLOT_BACKPACK && slot > CONST_SLOT_WHEREEVER) {
return RETURNVALUE_NOERROR;
}

bool isBackpack = item->getSlotPosition() & SLOTP_BACKPACK;
Item *bp = getInventoryItem(CONST_SLOT_BACKPACK);
if (!bp && !isBackpack) {
return RETURNVALUE_NOTENOUGHROOM;
}
else if (isBackpack) {
return RETURNVALUE_NOERROR;
}

if (Container *container = bp->getContainer()){
return container->queryAdd(INDEX_WHEREEVER, *item, 1, 0);
}

return RETURNVALUE_NOERROR;
}

ReturnValue Player::queryMaxCount(int32_t index, const Thing& thing, uint32_t count, uint32_t& maxQueryCount,
Expand Down
2 changes: 2 additions & 0 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,8 @@ class Player final : public Creature, public Cylinder
scheduledUpdate = false;
}

ReturnValue containerQueryAdd(const Item* item, slots_t slot) const;

private:
std::vector<Condition*> getMuteConditions() const;

Expand Down