* Gets a random mapblock within the given constraints. * @param maxsize The maximum size of the mapblock (10 or 20 or 999 - don't care). * @param type Whether this must be a block of a certain type. * @param force Whether to enforce the max size. * @return Pointer to the mapblock. */
| 121 | * @return Pointer to the mapblock. |
| 122 | */ |
| 123 | MapBlock* RuleTerrain::getRandomMapBlock(int maxsize, MapBlockType type, bool force) |
| 124 | { |
| 125 | std::vector<MapBlock*> compliantMapBlocks; |
| 126 | |
| 127 | for (std::vector<MapBlock*>::const_iterator i = _mapBlocks.begin(); i != _mapBlocks.end(); ++i) |
| 128 | { |
| 129 | if (((force && (*i)->getSizeX() == maxsize) || |
| 130 | (!force && (*i)->getSizeX() <= maxsize)) && |
| 131 | ((*i)->getType() == type || (*i)->getSubType() == type)) |
| 132 | { |
| 133 | for (int j = 0; j != (*i)->getRemainingUses(); ++j) |
| 134 | { |
| 135 | compliantMapBlocks.push_back((*i)); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (compliantMapBlocks.empty()) return 0; |
| 141 | |
| 142 | size_t n = RNG::generate(0, compliantMapBlocks.size() - 1); |
| 143 | |
| 144 | if (type == MT_DEFAULT) |
| 145 | compliantMapBlocks[n]->markUsed(); |
| 146 | |
| 147 | return compliantMapBlocks[n]; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Gets a mapblock with a given name. |
no test coverage detected