1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-04-27 12:22:45 +02:00

Fix for random dwellings

This commit is contained in:
nordsoft 2023-09-09 21:17:21 +02:00
parent 7aef40dfb6
commit cc3864a0fa
3 changed files with 18 additions and 5 deletions
lib

@ -286,7 +286,7 @@ std::pair<Obj,int> CGameState::pickObject (CGObjectInstance *obj)
//if level set to range //if level set to range
if(auto * info = dynamic_cast<CCreGenLeveledInfo *>(dwl->info)) if(auto * info = dynamic_cast<CCreGenLeveledInfo *>(dwl->info))
{ {
level = getRandomGenerator().nextInt(info->minLevel, info->maxLevel); level = getRandomGenerator().nextInt(info->minLevel, info->maxLevel) - 1;
} }
else // fixed level else // fixed level
{ {
@ -297,7 +297,20 @@ std::pair<Obj,int> CGameState::pickObject (CGObjectInstance *obj)
dwl->info = nullptr; dwl->info = nullptr;
std::pair<Obj, int> result(Obj::NO_OBJ, -1); std::pair<Obj, int> result(Obj::NO_OBJ, -1);
CreatureID cid = (*VLC->townh)[faction]->town->creatures[level][0]; CreatureID cid;
if((*VLC->townh)[faction]->town)
cid = (*VLC->townh)[faction]->town->creatures[level][0];
else
{
//neutral faction
std::vector<CCreature*> possibleCreatures;
std::copy_if(VLC->creh->objects.begin(), VLC->creh->objects.end(), std::back_inserter(possibleCreatures), [faction](const CCreature * c)
{
return c->getFaction() == faction;
});
assert(!possibleCreatures.empty());
cid = (*RandomGeneratorUtil::nextItem(possibleCreatures, getRandomGenerator()))->getId();
}
//NOTE: this will pick last dwelling with this creature (Mantis #900) //NOTE: this will pick last dwelling with this creature (Mantis #900)
//check for block map equality is better but more complex solution //check for block map equality is better but more complex solution

@ -42,7 +42,7 @@ public:
class DLL_LINKAGE CCreGenLeveledInfo : public virtual CSpecObjInfo class DLL_LINKAGE CCreGenLeveledInfo : public virtual CSpecObjInfo
{ {
public: public:
ui8 minLevel = 0; ui8 minLevel = 1;
ui8 maxLevel = 7; //minimal and maximal level of creature in dwelling: <1, 7> ui8 maxLevel = 7; //minimal and maximal level of creature in dwelling: <1, 7>
void serializeJson(JsonSerializeFormat & handler) override; void serializeJson(JsonSerializeFormat & handler) override;

@ -1273,8 +1273,8 @@ CGObjectInstance * CMapLoaderH3M::readDwellingRandom(const int3 & mapPosition, s
//216 and 218 //216 and 218
if(auto * lvlSpec = dynamic_cast<CCreGenLeveledInfo *>(spec)) if(auto * lvlSpec = dynamic_cast<CCreGenLeveledInfo *>(spec))
{ {
lvlSpec->minLevel = std::max(reader->readUInt8(), static_cast<ui8>(1)); lvlSpec->minLevel = std::max(reader->readUInt8(), static_cast<ui8>(0)) + 1;
lvlSpec->maxLevel = std::min(reader->readUInt8(), static_cast<ui8>(7)); lvlSpec->maxLevel = std::min(reader->readUInt8(), static_cast<ui8>(6)) + 1;
} }
object->info = spec; object->info = spec;
return object; return object;