diff options
| author | Ben Boeckel <MathStuf@gmail.com> | 2009-02-09 04:29:48 -0500 |
|---|---|---|
| committer | Ben Boeckel <MathStuf@gmail.com> | 2009-02-09 04:29:48 -0500 |
| commit | 92cdc34ddc1bc6b05a6b2522074299b739c17fa8 (patch) | |
| tree | a8c3a896d531f4a50ce2d113b5813f94eeca52ef /sigencore/Player.cpp | |
| parent | 08a2e6554045e2984d4d1b35149c1ad8ae57b185 (diff) | |
| download | sigen-92cdc34ddc1bc6b05a6b2522074299b739c17fa8.tar.gz sigen-92cdc34ddc1bc6b05a6b2522074299b739c17fa8.tar.xz sigen-92cdc34ddc1bc6b05a6b2522074299b739c17fa8.zip | |
Add item handling in Player
Diffstat (limited to 'sigencore/Player.cpp')
| -rw-r--r-- | sigencore/Player.cpp | 79 |
1 files changed, 77 insertions, 2 deletions
diff --git a/sigencore/Player.cpp b/sigencore/Player.cpp index b289018a..4b2a3eaf 100644 --- a/sigencore/Player.cpp +++ b/sigencore/Player.cpp @@ -19,6 +19,8 @@ #include "Player.h" // Sigscript includes +#include "../sigscript/ItemWrapper.h" +#include "../sigscript/ItemTypeWrapper.h" #include "../sigscript/RulesWrapper.h" #include "../sigscript/SigmodWrapper.h" @@ -57,9 +59,82 @@ void Sigencore::Player::exitArena() Client::exitArena(); } -void Sigencore::Player::giveMoney(const int amount) +int Sigencore::Player::giveItems(Sigscript::ItemWrapper* item, const int count, const bool allOrNothing) { - m_money = qMax(m_sigmod->rules()->maxMoney(), m_money + amount); + const int id = item->id(); + const QString name = item->name(); + const int typeId = item->type()->id(); + const QString typeName = item->type()->name(); + const int addWeight = count * item->weight(); + int end = 0; + if (count < 0) + { + int minWeight = 0; + int minWeightType = 0; + int minWeightTotal = 0; + valueOfType(QString("weight-item-minimum-%1").arg(name), &minWeight); + valueOfType(QString("weight-item-type-minimum-%1").arg(typeName), &minWeightType); + valueOfType("weight-item-total-minimum", &minWeightTotal); + const int diffWeight = (itemWeight(id) + addWeight) - minWeight; + const int diffWeightType = (itemTypeWeight(typeId) + addWeight) - minWeightType; + const int diffWeightTotal = (itemWeight() + addWeight) - minWeightTotal; + int minCount = 0; + int minCountType = 0; + int minCountTotal = 0; + valueOfType(QString("count-item-minimum-%1").arg(name), &minCount); + valueOfType(QString("count-item-type-minimum-%1").arg(typeName), &minCountType); + valueOfType("count-item-total-minimum", &minCountTotal); + const int diffCount = qMax(0, itemCount(id) + count) - minCount; + const int diffCountType = qMax(0, itemTypeCount(typeId) + count) - minCountType; + const int diffCountTotal = qMax(0, itemCount(-1) + count) - minCountTotal; + if ((0 < diffWeight) && (0 < diffWeightType) && (0 < diffWeightTotal) && (0 < diffCount) && (0 < diffCountType) && (0 < diffCountTotal)) + end = count; + else if (!allOrNothing) + { + const int weightUnderflow = qAbs(qMin(diffWeight, qMin(diffWeightType, diffWeightTotal))); + const int countUnderflow = qMax(diffCount, qMax(diffCountType, diffCountTotal)); + end = count + qMax((weightUnderflow / item->weight()) - !!(weightUnderflow % item->weight()), countUnderflow); + } + } + else if (0 < count) + { + int maxWeight = INT_MAX; + int maxWeightType = item->type()->maxWeight(); + maxWeightType = (maxWeightType == -1) ? INT_MAX : maxWeightType; + int maxWeightTotal = m_sigmod->rules()->maxTotalWeight(); + maxWeightTotal = (maxWeightTotal == -1) ? INT_MAX : maxWeightTotal; + valueOfType(QString("weight-item-maximum-%1").arg(name), &maxWeight); + valueOfType(QString("weight-item-type-maximum-%1").arg(typeName), &maxWeightType); + valueOfType("weight-item-total-maximum", &maxWeightTotal); + const int diffWeight = maxWeight - (itemWeight(id) + addWeight); + const int diffWeightType = maxWeight - (itemTypeWeight(typeId) + addWeight); + const int diffWeightTotal = maxWeight - (itemWeight() + addWeight); + int maxCount = item->type()->player(); + int maxCountType = item->type()->player(); + maxCountType = maxCountType ? maxCountType : INT_MAX; + int maxCountTotal = INT_MAX; + valueOfType(QString("count-item-maximum-%1").arg(name), &maxCount); + valueOfType(QString("count-item-type-maximum-%1").arg(typeName), &maxCountType); + valueOfType("count-item-total-maximum", &maxCountTotal); + const int diffCount = maxCount - (itemCount(id) + count); + const int diffCountType = maxCountType - (itemTypeCount(typeId) + count); + const int diffCountTotal = maxCountTotal - (itemCount(-1) + count); + if ((0 < diffWeight) && (0 < diffWeightType) && (0 < diffWeightTotal) && (0 < diffCount) && (0 < diffCountType) && (0 < diffCountTotal)) + end = count; + else if (!allOrNothing) + { + const int weightOverflow = qAbs(qMin(diffWeight, qMin(diffWeightType, diffWeightTotal))); + const int countOverflow = qMax(diffCount, qMax(diffCountType, diffCountTotal)); + end = count - qMax((weightOverflow / item->weight()) - !!(countOverflow % item->weight()), countOverflow); + } + } + end = qBound(qMin(0, count), end, qMax(count, 0)); + if (end) + { + m_items[item] += end; + emit(itemsGiven(item, end)); + } + return count - end; } bool Sigencore::Player::giveMoney(const int amount, const bool allOrNothing) |
