* Generates an int representing the index of the soldier's look, when passed the maximum index value. * @param numLooks The maximum index. * @return The index of the soldier's look. */
| 125 | * @return The index of the soldier's look. |
| 126 | */ |
| 127 | size_t SoldierNamePool::genLook(size_t numLooks) |
| 128 | { |
| 129 | int look = 0; |
| 130 | const int minimumChance = 2; // minimum chance of a look being selected if it isn't enumerated. This ensures that looks MUST be zeroed to not appear. |
| 131 | |
| 132 | while (_lookWeights.size() < numLooks) |
| 133 | { |
| 134 | _lookWeights.push_back(minimumChance); |
| 135 | _totalWeight += minimumChance; |
| 136 | } |
| 137 | while (_lookWeights.size() > numLooks) |
| 138 | { |
| 139 | _totalWeight -= _lookWeights.back(); |
| 140 | _lookWeights.pop_back(); |
| 141 | } |
| 142 | |
| 143 | int random = RNG::generate(0, _totalWeight); |
| 144 | for (std::vector<int>::iterator i = _lookWeights.begin(); i != _lookWeights.end(); ++i) |
| 145 | { |
| 146 | if (random <= *i) |
| 147 | { |
| 148 | return look; |
| 149 | } |
| 150 | random -= *i; |
| 151 | ++look; |
| 152 | } |
| 153 | |
| 154 | return RNG::generate(0, numLooks - 1); |
| 155 | } |
| 156 | |
| 157 | } |