diff options
-rw-r--r-- | engines/wage/combat.cpp | 4 | ||||
-rw-r--r-- | engines/wage/randomhat.cpp | 24 | ||||
-rw-r--r-- | engines/wage/randomhat.h | 8 |
3 files changed, 32 insertions, 4 deletions
diff --git a/engines/wage/combat.cpp b/engines/wage/combat.cpp index 8f3e5154d5..9bb1a54d1a 100644 --- a/engines/wage/combat.cpp +++ b/engines/wage/combat.cpp @@ -107,7 +107,7 @@ void WageEngine::performCombatAction(Chr *npc, Chr *player) { if (npc->_context._frozen) return; - RandomHat hat; + RandomHat hat(_rnd); bool winning = (npc->_context._statVariables[PHYS_HIT_CUR] > player->_context._statVariables[PHYS_HIT_CUR]); int validMoves = getValidMoveDirections(npc); @@ -166,6 +166,8 @@ void WageEngine::performCombatAction(Chr *npc, Chr *player) { case kTokOffer: performOffer(npc, player); break; + case kTokNone: + break; default: { int cnt = 0; diff --git a/engines/wage/randomhat.cpp b/engines/wage/randomhat.cpp index f908e0b1a8..9371140398 100644 --- a/engines/wage/randomhat.cpp +++ b/engines/wage/randomhat.cpp @@ -45,6 +45,8 @@ * */ +#include "common/random.h" + #include "common/hashmap.h" #include "wage/randomhat.h" @@ -54,8 +56,28 @@ void RandomHat::addTokens(int type, int count) { _tokens.setVal(type, _tokens.getVal(type, 0) + count); } +int RandomHat::countTokens() { + int count = 0; + for (Common::HashMap<int, int>::const_iterator it = _tokens.begin(); it != _tokens.end(); ++it) + count += it->_value; + + return count; +} + int RandomHat::drawToken() { - return 0; + int total = countTokens(); + if (total > 0) { + int random = _rnd->getRandomNumber(total - 1); + int count = 0; + for (Common::HashMap<int, int>::iterator it = _tokens.begin(); it != _tokens.end(); ++it) { + if (random >= count && random < count + it->_value) { + it->_value--; + return it->_key; + } + count += it->_value; + } + } + return kTokNone; } } // End of namespace Wage diff --git a/engines/wage/randomhat.h b/engines/wage/randomhat.h index f54b86bbeb..254cd2ae8d 100644 --- a/engines/wage/randomhat.h +++ b/engines/wage/randomhat.h @@ -54,18 +54,22 @@ enum { kTokWeapons = -400, kTokMagic = -300, kTokRun = -200, - kTokOffer = -100 + kTokOffer = -100, + kTokNone = -100000 }; class RandomHat { public: - RandomHat() {} + RandomHat(Common::RandomSource *rnd) : _rnd(rnd) {} void addTokens(int type, int count); int drawToken(); private: + Common::RandomSource *_rnd; Common::HashMap<int, int> _tokens; + + int countTokens(); }; } // End of namespace Wage |