Skip to content
Draft
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
62 changes: 62 additions & 0 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2659,6 +2659,68 @@ std::shared_ptr<Item> Game::findItemOfType(const std::shared_ptr<Cylinder> &cyli
return nullptr;
}

bool Game::validRemoveMoney(const std::shared_ptr<Cylinder> &cylinder, uint64_t money, uint32_t flags /*= 0*/, bool useBalance /*= false*/) {
if (cylinder == nullptr) {
g_logger().error("[{}] cylinder is nullptr", __FUNCTION__);
return false;
}
if (money == 0) {
return true;
}

std::vector<std::shared_ptr<Container>> containers;
std::multimap<uint32_t, std::shared_ptr<Item>> moneyMap;
uint64_t moneyCount = 0;
for (size_t i = cylinder->getFirstIndex(), j = cylinder->getLastIndex(); i < j; ++i) {
const std::shared_ptr<Thing> &thing = cylinder->getThing(i);
if (!thing) {
continue;
}
const auto &item = thing->getItem();
if (!item) {
continue;
}
const std::shared_ptr<Container> &container = item->getContainer();
if (container) {
containers.push_back(container);
} else {
const uint32_t worth = item->getWorth();
if (worth != 0) {
moneyCount += worth;
moneyMap.emplace(worth, item);
}
}
}
size_t i = 0;
while (i < containers.size()) {
const std::shared_ptr<Container> &container = containers[i++];
for (const std::shared_ptr<Item> &item : container->getItemList()) {
const std::shared_ptr<Container> &tmpContainer = item->getContainer();
if (tmpContainer) {
containers.push_back(tmpContainer);
} else {
const uint32_t worth = item->getWorth();
if (worth != 0) {
moneyCount += worth;
moneyMap.emplace(worth, item);
}
}
}
}

const auto &player = useBalance ? std::dynamic_pointer_cast<Player>(cylinder) : nullptr;
uint64_t balance = 0;
if (useBalance && player) {
balance = player->getBankBalance();
}

if (moneyCount + balance < money) {
return false;
}

return true;
}

bool Game::removeMoney(const std::shared_ptr<Cylinder> &cylinder, uint64_t money, uint32_t flags /*= 0*/, bool useBalance /*= false*/) {
if (cylinder == nullptr) {
g_logger().error("[{}] cylinder is nullptr", __FUNCTION__);
Expand Down
2 changes: 2 additions & 0 deletions src/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ class Game {

void createLuaItemsOnMap();

bool validRemoveMoney(const std::shared_ptr<Cylinder> &cylinder, uint64_t money, uint32_t flags = 0, bool useBank = false);

bool removeMoney(const std::shared_ptr<Cylinder> &cylinder, uint64_t money, uint32_t flags = 0, bool useBank = false);

void addMoney(const std::shared_ptr<Cylinder> &cylinder, uint64_t money, uint32_t flags = 0);
Expand Down
9 changes: 9 additions & 0 deletions src/lua/functions/creatures/npc/npc_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,15 @@ int NpcFunctions::luaNpcSellItem(lua_State* L) {
}
}

const auto totalCost = amount * pricePerUnit;

if (!g_game().validRemoveMoney(player, totalCost, 0, true)) {
Lua::pushBoolean(L, false);
player->sendCancelMessage(RETURNVALUE_NOTENOUGHROOM);
g_logger().error("[NpcFunctions::luaNpcSellItem (validRemoveMoney)] - Player {} possibly tried to abuse a bug buying large amounts of {} on shop for npc {}", player->getName(), itemId, npc->getName());
return 1;
}

const auto &[_, itemsPurchased, backpacksPurchased] = g_game().createItem(player, itemId, amount, subType, actionId, ignoreCap, inBackpacks ? ITEM_SHOPPING_BAG : 0);

std::stringstream ss;
Expand Down