Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool Game::removeMoney(Cylinder* cylinder, uint64_t money, uint32_t flags /*= 0*/)
- {
- if (cylinder == nullptr) {
- return false;
- }
- if (money <= 0) {
- return true;
- }
- std::vector<Container*> containers;
- typedef std::multimap<uint64_t, Item*, std::less<uint64_t>> MoneyMap;
- typedef MoneyMap::value_type moneymap_pair;
- MoneyMap moneyMap;
- uint64_t moneyCount = 0;
- for (int32_t i = cylinder->getFirstIndex(), j = cylinder->getLastIndex(); i < j; ++i) {
- Thing* thing = cylinder->getThing(i);
- if (!thing) {
- continue;
- }
- Item* item = thing->getItem();
- if (!item) {
- continue;
- }
- Container* container = item->getContainer();
- if (container) {
- containers.push_back(container);
- } else if (item->getWorth() != 0) {
- moneyCount += item->getWorth();
- moneyMap.insert(moneymap_pair(item->getWorth(), item));
- }
- }
- size_t i = 0;
- while (i < containers.size()) {
- Container* container = containers[i++];
- for (Item* item : container->getItemList()) {
- Container* tmpContainer = item->getContainer();
- if (tmpContainer) {
- containers.push_back(tmpContainer);
- } else if (item->getWorth() != 0) {
- moneyCount += item->getWorth();
- moneyMap.insert(moneymap_pair(item->getWorth(), item));
- }
- }
- }
- /*not enough money*/
- if (moneyCount < money) {
- return false;
- }
- for (MoneyMap::const_iterator mit = moneyMap.begin(), mend = moneyMap.end(); mit != mend && money > 0; ++mit) {
- Item* item = mit->second;
- internalRemoveItem(item);
- if (mit->first > money) {
- /* Remove a monetary value from an item*/
- uint64_t remaind = item->getWorth() - money;
- addMoney(cylinder, remaind, flags);
- money = 0;
- } else {
- money -= mit->first;
- }
- }
- return money == 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment