Skip to content

Commit

Permalink
Fix furniture tick frequency without resorting to hardcoding furnitur…
Browse files Browse the repository at this point in the history
…e tick frequencies
  • Loading branch information
miki151 committed May 8, 2024
1 parent 20206f9 commit 0c9d2e0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 39 deletions.
67 changes: 29 additions & 38 deletions level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "player_control.h"
#include "portals.h"
#include "effect_type.h"
#include "content_factory.h"

template <class Archive>
void Level::serialize(Archive& ar, const unsigned int version) {
Expand Down Expand Up @@ -81,25 +82,28 @@ void Level::serialize(Archive& ar, const unsigned int version) {
}
}

static Effects::Chance* getChanceTick(FurnitureTickType& t) {
return t.visit<Effects::Chance*>(
[&](auto&) { return nullptr;},
[&](Effect& effect) {
return effect.effect->visit<Effects::Chance*>(
[&](Effects::Chance& c) {
return &c;
},
[&](auto&) { return nullptr;}
);
}
);
}

void Level::updateTickingFurniture() {
for (auto v : squares->getBounds())
for (auto layer : ENUM_ALL(FurnitureLayer))
if (auto f = furniture->getBuilt(layer).getReadonly(v)) {
if (auto t = f->getTickType()) {
double chance = 1.0;
t->visit<void>(
[&](const auto&) {},
[&](const Effect& effect) {
effect.effect->visit(
[&](const Effects::Chance& c) {
chance = c.value;
furniture->getBuilt(layer).getWritable(v)->tickType = FurnitureTickType(std::move(*c.effect));
},
[&](const auto&) {}
);
}
);
tickingFurniture.insert(make_tuple(v, layer, chance));
if (auto chance = getChanceTick(*t))
furniture->getBuilt(layer).getWritable(v)->tickType = FurnitureTickType(std::move(*chance->effect));
tickingFurniture.insert(make_pair(make_pair(v, layer), -1));
}
if ((f->getFire() && f->getFire()->isBurning()) || f->hasBlood())
burningFurniture.insert(make_pair(v, layer));
Expand Down Expand Up @@ -565,7 +569,7 @@ void Level::addTickingSquare(Vec2 pos) {
}

void Level::addTickingFurniture(Vec2 pos, FurnitureLayer layer) {
tickingFurniture.insert(make_tuple(pos, layer, 1.0));
tickingFurniture[make_pair(pos, layer)] = 1.0;
}

void Level::addBurningFurniture(Vec2 pos, FurnitureLayer layer) {
Expand All @@ -576,31 +580,18 @@ void Level::tick() {
PROFILE_BLOCK("Level::tick");
for (Vec2 pos : tickingSquares)
squares->getWritable(pos)->tick(Position(pos, this));
auto& furnitureFactory = getGame()->getContentFactory()->furniture;
for (auto& elem : tickingFurniture)
if (auto f = furniture->getBuilt(std::get<1>(elem)).getWritable(std::get<0>(elem))) {
auto chance = std::get<2>(elem);
if (f->getType() == FurnitureType("UNDERGROUND_WATER"))
chance = 0.002;
if (f->getType() == FurnitureType("ENCHANTED_WATER"))
chance = 0.00002;
if (f->getType() == FurnitureType("MAGMA"))
chance = 0.1;
if (f->getType() == FurnitureType("SWAMPGRASS"))
chance = 0.006;
if (f->getType() == FurnitureType("GHOST_SPAWN"))
chance = 0.002;
if (f->getType() == FurnitureType("CANIF_TREE"))
chance = 0.002;
if (f->getType() == FurnitureType("CHICKEN_COOP"))
chance = 0.05;
if (f->getType() == FurnitureType("PIGSTY"))
chance = 0.05;
if (f->getType() == FurnitureType("COW_PEN"))
chance = 0.05;
if (f->getType() == FurnitureType("HELL_MOUNTAIN"))
chance = 0.0005;
if (auto f = furniture->getBuilt(elem.first.second).getWritable(elem.first.first)) {
auto& chance = elem.second;
if (chance <= 0)
if (auto tickType = furnitureFactory.getData(f->getType()).tickType)
if (auto chanceTick = getChanceTick(*tickType))
chance = chanceTick->value;
if (chance <= 0)
chance = 1;
if (Random.chance(chance))
f->tick(Position(std::get<0>(elem), this), std::get<1>(elem));
f->tick(Position(elem.first.first, this), elem.first.second);
}
for (auto& elem : burningFurniture)
if (auto f = furniture->getBuilt(std::get<1>(elem)).getWritable(std::get<0>(elem)))
Expand Down
2 changes: 1 addition & 1 deletion level.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class Level : public OwnedObject<Level> {
Table<bool> SERIAL(unavailable);
LandingSquares SERIAL(landingSquares);
set<Vec2> SERIAL(tickingSquares);
set<tuple<Vec2, FurnitureLayer, double>> tickingFurniture;
HashMap<pair<Vec2, FurnitureLayer>, double> tickingFurniture;
HashSet<pair<Vec2, FurnitureLayer>> burningFurniture;
void placeCreature(Creature*, Vec2 pos);
void unplaceCreature(Creature*, Vec2 pos);
Expand Down

1 comment on commit 0c9d2e0

@KA101
Copy link

@KA101 KA101 commented on 0c9d2e0 May 8, 2024

Choose a reason for hiding this comment

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

Hey, is this currently available in steam?

Please sign in to comment.