Decently-well working, doing some profiling currently.

This commit is contained in:
2025-03-29 14:31:57 -04:00
parent 5cd84258d8
commit 60dbd6d725
7 changed files with 94 additions and 259 deletions

View File

@@ -20,8 +20,8 @@ set(CMAKE_CXX_STANDARD 23)
if (UNIX)
# TODO: Enable ALL optimization flags for RELEASE builds.
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -floop-nest-optimize -funroll-loops")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -floop-nest-optimize -funroll-loops")
endif()
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

View File

@@ -222,24 +222,37 @@ namespace CaveGame::Client {
void LocalWorld::RenderTile(const TileID& t_id, int wx, int wy, int tx, int ty)
{
using namespace CaveGame::Core;
static Tile stone = Tiles()["stone"];
static TileID air = Tiles()["air"].numeric_id;
static TileID void_tile = Tiles()["void"].numeric_id;
static TileID cobblestone = Tiles()["cobblestone"].numeric_id;
static TileID stone_id = stone.numeric_id;
static TileID oak_plank = Tiles()["oak-plank"].numeric_id;
static TileID stone_brick = Tiles()["stone-brick"].numeric_id;
// TODO: Migrate custom tile render code to an override Draw method in tile classes?
// TODO: See class Tile::Draw to see why this is halted.
if (t_id == Tiles()["air"].numeric_id || t_id == Tiles()["void"].numeric_id)
if (t_id == air || t_id == void_tile)
return;
Color4 t_color;
Core::Tile t_data = Tiles()[t_id];
const Core::Tile& t_data = Tiles()[t_id];
if (t_id == Tiles()["cobblestone"].numeric_id) {
if (t_id == cobblestone) {
float val = generator.Perlin(wx, wy, 4, 4, 0.25f, 1.5f);
unsigned int rand = generator.ColorMap(Tiles()["stone"].pallet->size(), wx, wy);
unsigned int rand = generator.ColorMap(stone.pallet->size(), wx, wy);
t_color = t_data.pallet->operator[](rand);
if (val > 0.40f || val < -0.40f) {
@@ -255,7 +268,7 @@ namespace CaveGame::Client {
//Core::Tiles::Cobblestone.color_pallet
} else if (t_id == Tiles()["oak-plank"].numeric_id) {
} else if (t_id == oak_plank) {
// TODO: Make each plank have a slightly different base color.
@@ -351,7 +364,6 @@ namespace CaveGame::Client {
TileID t_id;
Core::Tile t_data;
Color4 t_color;
JGL::J2D::Begin(destination, true);
for (int x = 0; x < Core::Chunk::ChunkSize; x++)
@@ -484,7 +496,7 @@ namespace CaveGame::Client {
/*std::vector<Particle>::iterator it;
for (it = particles.begin(); it != particles.end();)
{
{aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
if (it->life <= 0)
it = particles.erase(it);
//else

View File

@@ -20,7 +20,6 @@ namespace CaveGame::Core {
using ColorPallet = std::vector<Color4>;
using TileTiccFunc = std::function<void(const Tile& data, ITileMap* world, int x, int y)>;
void SandGravTiccFunc(const Tile& data, ITileMap* world, int x, int y);
void LiquidSettleTiccFunc(const Tile& data, ITileMap* world, int x, int y);
void GrassRandomTiccFunc(const Tile& data, ITileMap* world, int x, int y);

View File

@@ -186,7 +186,7 @@ namespace CaveGame::Core {
float tile_ticc_counter;
// Ticcs per second.
float tile_ticc_frequency = 48.f;
float tile_ticc_frequency = 24.f;
bool save_in_progress = false;

View File

@@ -6,42 +6,7 @@
namespace CaveGame::Core {
/*void Tile::DecayTo(ITileMap *world, TileState state, int x, int y, TileID TDecaysTo) {
world->SetTile(x, y, TDecaysTo);
}
bool Tile::ShouldSpread(ITileMap *world, int x, int y, TileID spreads_to) const {
if (world->GetTile(x, y) == spreads_to)
{
if (world->HasAdjacentOrDiagonalAirBlock(x, y))
return true;
}
return false;
}
LiquidTile::LiquidTile(): Tile() {}
LiquidTile::LiquidTile(TileID id, const std::string &name, const Color4 &color, const std::vector<Color4> &pallet): Tile(id,name, color,pallet) {
does_random_ticc = true;
}
LiquidTile::LiquidTile(TileID numeric, const std::string &name, Color4 color): Tile(numeric, name, color) {
does_random_ticc = true;
}
SoilTile::SoilTile(): Tile() {}
SoilTile::SoilTile(TileID id, const std::string &name, const Color4 &color, const std::vector<Color4> &pallet,
TileID decays_target): Tile(id,name, color,pallet) {
decays_to = decays_target;
does_random_ticc = true;
}
SoilTile::SoilTile(TileID numeric, const std::string &name, Color4 color, TileID decays_target): Tile(numeric, name, color) {
decays_to = decays_target;
does_random_ticc = true;
}
/*
bool Tile::ShouldSuffocate(ITileMap *world, int x, int y) const {
if (!world->HasAdjacentOrDiagonalAirBlock(x, y))
return true;
@@ -108,17 +73,6 @@ namespace CaveGame::Core {
DecayTo(world, state, x, y, decays_to);
}
GrassyTile::GrassyTile(): SoilTile() {}
GrassyTile::GrassyTile(TileID id, const std::string &name, const Color4 &color, const std::vector<Color4> &pallet,
TileID decays_target): SoilTile(id,name, color,pallet, decays_target) { }
GrassyTile::GrassyTile(TileID numeric, const std::string &name, Color4 color, TileID decays_target): SoilTile(numeric, name, color, decays_target) { }
GrassyTile::GrassyTile(TileID id, const std::string &name, const Color4 &color, const std::vector<Color4> &pallet): SoilTile(id,name, color,pallet, TileID::DIRT) { }
GrassyTile::GrassyTile(TileID numeric, const std::string &name, Color4 color): SoilTile(numeric, name, color, TileID::DIRT) { }
void GrassyTile::RandomTicc(ITileMap* world, TileState state, int x, int y)
{
if (DecayCheck(world, state, x, y, decays_to))
@@ -134,16 +88,6 @@ namespace CaveGame::Core {
}
}
MossyTile::MossyTile(): SoilTile()
{}
MossyTile::MossyTile(TileID id, const std::string& name, const Color4& color, TileID decays_target): SoilTile(id, name, color, decays_target) { }
MossyTile::MossyTile(TileID id, const std::string &name, const Color4 &color, const std::vector<Color4> &pallet): SoilTile(id,name, color,pallet, TileID::STONE) { }
MossyTile::MossyTile(TileID numeric, const std::string &name, Color4 color): SoilTile(numeric, name, color, TileID::STONE) { }
void MossyTile::RandomTicc(ITileMap* world, TileState state, int x, int y)
{
if (DecayCheck(world, state, x, y, decays_to))
@@ -154,137 +98,7 @@ namespace CaveGame::Core {
}
GravityTile::GravityTile(): Tile()
{
//does_forced_ticc = true;
}
GravityTile::GravityTile(TileID id, const std::string& name, const Color4& color): Tile(id, name, color)
{
//does_forced_ticc = true;
}
GravityTile::GravityTile(TileID id, const std::string& name, const Color4& color, const std::vector<Color4>& pallet):Tile(id, name, color, pallet)
{
//does_forced_ticc = true;
}
void GravityTile::ForcedTicc(ITileMap *world, TileState state, int x, int y) {
TileID below = world->GetTile(x, y + 1);
if (below == TileID::AIR || below == TileID::WATER)
world->SwapTile(x, y, x, y + 1);
// TODO Change this to check specifically for gravity tile.
else if (below == TileID::SAND && world->GetTile(x, y + 2) == TileID::SAND) {
bool rng_roll = rand() % 2 == 0;
if (world->GetTile(rng_roll ? (x + 1) : (x - 1), y) == TileID::AIR)
world->SwapTile(x, y, rng_roll ? (x + 1) : (x - 1), y);
}
}
VineTile::VineTile(): Tile()
{
//does_forced_ticc = true;
}
VineTile::VineTile(TileID id, const std::string& name, const Color4& color): Tile(id, name, color)
{
does_random_ticc = true;
}
VineTile::VineTile(TileID id, const std::string& name, const Color4& color, const std::vector<Color4>& pallet):Tile(id, name, color, pallet)
{
does_random_ticc = true;
}
void VineTile::ForcedTicc(ITileMap* world, TileState state, int x, int y)
{
if (DecayCheck(world, state, x, y, TileID::AIR))
return;
}
void VineTile::RandomTicc(ITileMap* world, TileState state, int x, int y)
{
if (DecayCheck(world, state, x,y, TileID::AIR))
return;
if (SpreadCheck(world, state, x, y, TileID::AIR))
return;
}
bool VineTile::DecayCheck(ITileMap* world, TileState state, int x, int y, TileID decays_to)
{
TileID above = world->GetTile(x, y-1);
if (above != TileID::VINE && above != TileID::GRASS)
{
DecayTo(world, state, x, y, TileID::AIR);
return true;
}
return false;
}
bool VineTile::ShouldSpread(ITileMap* world, int x, int y, TileID spreads_to) const
{
if (world->GetTile(x, y) == spreads_to)
{
return true;
}
return false;
}
bool VineTile::SpreadCheck(ITileMap* world, TileState state, int x, int y, TileID spreads_to)
{
if (ShouldSpread(world, x, y + 1, spreads_to))
{
if (world->GetTile(x+1, y+1) == TileID::AIR && world->GetTile(x-1, y+1) == TileID::AIR)
{
world->SetTile(x, y+1, numeric_id);
return true;
}
}
return false;
}
Tile *GetByName(const std::string &name) {
// TODO: Optimize with additional mapping!!
for(auto& tile : registered_tiles)
if (tile->MnemonicID() == name)
return tile;
throw std::runtime_error("Invalid mnemonic ID!");
}
Tile::Tile() {}
Tile::Tile(TileID id, const std::string &name, const Color4 &color): numeric_id(id), mnemonic_id(name), base_color(color),
has_color_pallet(false) {
RegisterTile(this);
}
Tile::Tile(TileID id, const std::string &name, const Color4 &color, const std::vector<Color4> &pallet): numeric_id(id), mnemonic_id(name), base_color(color), color_pallet(pallet),
has_color_pallet(true) {
RegisterTile(this);
}
TileID Tile::NumericID() const { return numeric_id; }
std::string Tile::MnemonicID() const { return mnemonic_id; }
std::string Tile::Name() const {
// Use the mnemonic ID if no display name is set.
if (display_name.empty())
return mnemonic_id;
return display_name;
}
WaterTile::WaterTile() : LiquidTile() {}
WaterTile::WaterTile(TileID id, const std::string &name, const Color4 &color) : LiquidTile(id, name, color) {}*/
*/
void SandGravTiccFunc(const Tile &data, ITileMap *world, int x, int y) {
static TileID air = Tiles()["air"].numeric_id;
@@ -309,71 +123,96 @@ namespace CaveGame::Core {
}
bool ShouldSpread(const Tile& data, ITileMap* world, int x, int y, TileID spreads_to)
{
if (world->GetTile(x, y) == spreads_to)
{
if (world->HasAdjacentOrDiagonalAirBlock(x, y))
return true;
}
return false;
}
bool SpreadCheck(const Tile& data, ITileMap* world, int x, int y, TileID spreads_to)
{
if (ShouldSpread(data, world, x, y, spreads_to))
{
world->SetTile(x, y, data.numeric_id);
return true;
}
return false;
}
void GrassForcedTiccFunc(const Tile &data, ITileMap *world, int x, int y) {
}
void GrassRandomTiccFunc(const Tile &data, ITileMap *world, int x, int y) {
static TileID air = Tiles()["air"].numeric_id;
static TileID vine = Tiles()["vine"].numeric_id;
static TileID dirt = Tiles()["dirt"].numeric_id;
// Decay check
if (!world->HasAdjacentOrDiagonalAirBlock(x, y)) {
world->SetTile(x, y, dirt);
return;
}
// Spread check
if (SpreadCheck(data, world, x+1, y, dirt)) return;
if (SpreadCheck(data, world, x-1, y, dirt)) return;
if (SpreadCheck(data, world, x, y+1, dirt)) return;
if (SpreadCheck(data, world, x, y-1, dirt)) return;
// Vine growth check
if (world->GetTile(x, y+1) == air)
{
world->SetTile(x, y+1, vine);
return;
}
}
void MossForcedTiccFunc(const Tile &data, ITileMap *world, int x, int y) {
}
void VineRandomTiccFunc(const Tile &data, ITileMap *world, int x, int y) {
static TileID air = Tiles()["air"].numeric_id;
if (DecayCheck(world, x,y, air))
return;
if (SpreadCheck(world, x, y, air))
return;
void DecayTo(ITileMap *world, int x, int y, TileID TDecaysTo) {
world->SetTile(x, y, TDecaysTo);
}
void VineForcedTiccFunc(const Tile &data, ITileMap *world, int x, int y) {
static TileID air = Tiles()["air"].numeric_id;
if (DecayCheck(world, x, y, TileID::AIR))
return;
}
bool DecayCheck(ITileMap* world, int x, int y, TileID decays_to) {
bool VineDecayCheck(ITileMap* world, int x, int y, TileID decays_to) {
static TileID vine = Tiles()["vine"].numeric_id;
static TileID grass = Tiles()["grass"].numeric_id;
static TileID air = Tiles()["air"].numeric_id;
TileID above = world->GetTile(x, y-1);
if (above != vine && above != grass)
{
DecayTo(world, x, y, air);
if (above != vine && above != grass) {
world->SetTile(x, y, air);
return true;
}
return false;
}
bool VineTile::ShouldSpread(ITileMap* world, int x, int y, TileID spreads_to) const
{
if (world->GetTile(x, y) == spreads_to)
{
return true;
}
return false;
}
void VineRandomTiccFunc(const Tile &data, ITileMap *world, int x, int y) {
static TileID air = Tiles()["air"].numeric_id;
bool VineTile::SpreadCheck(ITileMap* world, TileState state, int x, int y, TileID spreads_to)
{
if (ShouldSpread(world, x, y + 1, spreads_to))
{
if (world->GetTile(x+1, y+1) == TileID::AIR && world->GetTile(x-1, y+1) == TileID::AIR)
{
world->SetTile(x, y+1, numeric_id);
return true;
if (VineDecayCheck(world, x,y, air))
return;
if (world->GetTile(x, y + 1) == air) {
if (world->GetTile(x+1, y+1) == air && world->GetTile(x-1, y+1) == air) {
world->SetTile(x, y+1, data.numeric_id);
return;
}
}
return false;
}
void VineForcedTiccFunc(const Tile &data, ITileMap *world, int x, int y) {
static TileID air = Tiles()["air"].numeric_id;
if (VineDecayCheck(world, x, y, air))
return;
}
}

View File

@@ -136,7 +136,7 @@ namespace CaveGame::Core
std::string ftfkey = "forced-ticc-func";
std::string rtfkey = "random-ticc-func";
// Retrieve and evaluate the force ticc function ID. Assign to tile if valid.
tile.does_forced_ticc = false;
@@ -154,7 +154,9 @@ namespace CaveGame::Core
}
// Retrieve and evaluate the random ticc function ID. Assign to tile if valid.
std::string rtfkey = "random-ticc-func";
tile.does_random_ticc = false;
if (tile_entry.contains(rtfkey)
&& tile_entry[rtfkey].type == json::value_type::string
&& !tile_entry[rtfkey].string.value().empty())

View File

@@ -116,24 +116,6 @@ namespace CaveGame::Core {
std::unordered_map<Vector2i, Chunk*> World::GetChunkList() { return loaded_chunks; }
void World::SetTileUpdateFlag(int x, int y, bool flag) {
/*
float chunkX = Math::Floor((float)x / Chunk::ChunkSize);
float chunkY = Math::Floor((float)y / Chunk::ChunkSize);
float tileX = Math::Mod(x, Chunk::ChunkSize);
float tileY = Math::Mod(y, Chunk::ChunkSize);
if (tileX < 0)
tileX = Chunk::ChunkSize + tileX;
if (tileY < 0)
tileY = Chunk::ChunkSize + tileY;
const Vector2 coords = Vector2(chunkX, chunkY);
if (HasChunkAtCell(coords))
loaded_chunks.at(coords)->SetTileUpdateFlag(tileX, tileY, flag);*/
const Vector2i chunkCoords = GetChunkCoordinatesAtCell(x, y);
Vector2i tileCoords = GetTileCoordinatesAtCell(x, y);
@@ -158,7 +140,6 @@ namespace CaveGame::Core {
Vector2 coords = Vector2(chunkX, chunkY);
if (HasChunkAtCell(coords))
return loaded_chunks.at(coords)->GetTileUpdateFlag(tileX, tileY);
@@ -444,8 +425,10 @@ namespace CaveGame::Core {
// TODO make sure this value is restored when the chunks load from disk.
if (chunk->tiles_with_random_tick_count == 0)
return;
if (chunk->tiles_with_random_tick_count < 0)
std::cout << "The number of tiles with random tick in the chunk is negative?" << std::endl;
if (chunk->tiles_with_random_tick_count < 0) {
chunk->tiles_with_random_tick_count = 0;
}
//std::cout << "The number of tiles with random tick in the chunk is negative?" << std::endl;
Tile tile;