Skip to content

Commit

Permalink
Improve a level gen crash error message
Browse files Browse the repository at this point in the history
  • Loading branch information
miki151 committed May 8, 2024
1 parent 23c0b03 commit 20206f9
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 17 deletions.
4 changes: 2 additions & 2 deletions collective_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "view_object.h"
#include "level.h"

CollectiveBuilder::CollectiveBuilder(const CollectiveConfig& cfg, TribeId t)
: config(cfg), tribe(t) {
CollectiveBuilder::CollectiveBuilder(const CollectiveConfig& cfg, TribeId t, string enemyId)
: enemyId(enemyId), config(cfg), tribe(t) {
}

CollectiveBuilder& CollectiveBuilder::setLevel(Level* l) {
Expand Down
4 changes: 3 additions & 1 deletion collective_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ContentFactory;

class CollectiveBuilder {
public:
CollectiveBuilder(const CollectiveConfig&, TribeId);
CollectiveBuilder(const CollectiveConfig&, TribeId, string enemyId);
CollectiveBuilder& setModel(Model*);
CollectiveBuilder& setLevel(Level*);
CollectiveBuilder& addCreature(Creature*, EnumSet<MinionTrait>);
Expand All @@ -32,6 +32,8 @@ class CollectiveBuilder {
bool hasCreatures() const;
optional<CollectiveName> generateName() const;

string enemyId;

private:
optional<CollectiveName> getCollectiveName();
Model* model = nullptr;
Expand Down
4 changes: 2 additions & 2 deletions game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ void Game::spawnKeeper(AvatarInfo avatarInfo, vector<string> introText) {
CHECK(level->landCreature(StairKey::keeperSpawn(), keeperRef)) << "Couldn't place keeper on level.";
model->addCreature(std::move(avatarInfo.playerCreature));
auto& keeperInfo = avatarInfo.creatureInfo;
auto builder = CollectiveBuilder(CollectiveConfig::keeper(
auto builder = CollectiveBuilder(CollectiveConfig::keeper(
TimeInterval(keeperInfo.immigrantInterval), keeperInfo.maxPopulation, keeperInfo.populationString,
keeperInfo.prisoners, ConquerCondition::KILL_LEADER, keeperInfo.requireQuartersForExp),
keeperRef->getTribeId())
keeperRef->getTribeId(), "keeper collective")
.setModel(model)
.addCreature(keeperRef, keeperInfo.minionTraits);
if (avatarInfo.chosenBaseName)
Expand Down
7 changes: 1 addition & 6 deletions level_maker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1612,13 +1612,8 @@ class PlaceCollective : public LevelMaker {
virtual void make(LevelBuilder* builder, Rectangle area) override {
auto territory = builder->toGlobalCoordinates(area.getAllSquares()
.filter([&](Vec2 pos) { return predicate.apply(builder, pos); }));
auto getName = [&] {
if (auto name = collective->generateName())
return name->full;
return "noname"_s;
};
if (!collective->hasCentralPoint()) {
CHECK(!territory.empty()) << "Tried to place " << getName() << " on an empty territory";
CHECK(!territory.empty()) << "Tried to place " << collective->enemyId << " on an empty territory";
collective->setCentralPoint(Vec2::getCenterOfWeight(territory));
}
collective->addArea(territory);
Expand Down
6 changes: 3 additions & 3 deletions model_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ ModelBuilder::LevelMakerMethod ModelBuilder::getMaker(LevelType type) {
return [this] (RandomGen& random, SettlementInfo info, Vec2, int difficulty) {
auto& biomeInfo = contentFactory->biomeInfo.at(BiomeId("OUTBACK"));
auto natives = enemyFactory->get(EnemyId("NATIVE_VILLAGE"));
natives.settlement.collective = new CollectiveBuilder(natives.config, natives.settlement.tribe);
natives.settlement.collective = new CollectiveBuilder(natives.config, natives.settlement.tribe, "native village");
return LevelMaker::topLevel(random, {info, natives.settlement}, 100, difficulty, none, none, biomeInfo, ResourceCounts{},
*contentFactory);
};
Expand Down Expand Up @@ -286,7 +286,7 @@ SettlementInfo& ModelBuilder::processLevelConnection(Model* model, EnemyInfo& en
if (auto extra = enemyInfo.getReferenceMaybe<LevelConnection::ExtraEnemy>()) {
for (auto& enemy : extra->enemyInfo) {
enemy.settlement.collective =
new CollectiveBuilder(enemy.config, enemy.settlement.tribe);
new CollectiveBuilder(enemy.config, enemy.settlement.tribe, !!enemy.id ? enemy.id->data() : "unknown");
extraEnemies.push_back(enemy);
}
}
Expand Down Expand Up @@ -318,7 +318,7 @@ PModel ModelBuilder::tryModel(int width, int difficulty, vector<EnemyInfo> enemy
vector<SettlementInfo> topLevelSettlements;
vector<EnemyInfo> extraEnemies;
for (auto& elem : enemyInfo) {
elem.settlement.collective = new CollectiveBuilder(elem.config, elem.settlement.tribe);
elem.settlement.collective = new CollectiveBuilder(elem.config, elem.settlement.tribe, !!elem.id ? elem.id->data() : "unknown");
topLevelSettlements.push_back(processLevelConnection(model.get(), elem, extraEnemies, 0, true, difficulty));
}
append(enemyInfo, extraEnemies);
Expand Down
6 changes: 3 additions & 3 deletions zlevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int getZLevelCombatExp(int depth) {
static EnemyInfo getEnemy(EnemyId id, ContentFactory* contentFactory) {
auto enemy = EnemyFactory(Random, contentFactory->getCreatures().getNameGenerator(), contentFactory->enemies,
contentFactory->buildingInfo, {}).get(id);
enemy.settlement.collective = new CollectiveBuilder(enemy.config, enemy.settlement.tribe);
enemy.settlement.collective = new CollectiveBuilder(enemy.config, enemy.settlement.tribe, id.data());
return enemy;
}

Expand Down Expand Up @@ -92,8 +92,8 @@ LevelMakerResult getUpLevel(RandomGen& random, ContentFactory* contentFactory,
if (enemyInfo.first.contains(depth) && random.chance(enemyInfo.second.probability))
for (int it : Range(Random.get(enemyInfo.second.count))) {
enemies.push_back(getEnemy(enemyInfo.second.id, contentFactory));
enemies.back().settlement.collective = new CollectiveBuilder(enemies.back().config,
enemies.back().settlement.tribe);
enemies.back().settlement.collective = new CollectiveBuilder(enemies.back().config,
enemies.back().settlement.tribe, enemyInfo.second.id.data());
}
auto res = chooseResourceCounts(random, contentFactory->resources, -depth);
auto maker = LevelMaker::upLevel(pos, biomeInfo, enemies.transform([](auto e) {return e.settlement; }), res);
Expand Down

1 comment on commit 20206f9

@KA101
Copy link

@KA101 KA101 commented on 20206f9 May 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this helped. 👍

Please sign in to comment.