summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoruntodesu <kirill@untode.su>2025-09-12 15:09:01 +0500
committeruntodesu <kirill@untode.su>2025-09-12 15:09:01 +0500
commitf210a86c1406ccc6dfd6f14181dd7a1274ee0de4 (patch)
tree58bf44927afe33609dc3a531b0d183f5504d8485
parent73cbcdd6e8c849e32abbf9757e603e6a6654e870 (diff)
downloadvoxelius-f210a86c1406ccc6dfd6f14181dd7a1274ee0de4.tar.bz2
voxelius-f210a86c1406ccc6dfd6f14181dd7a1274ee0de4.zip
Random ticking? In my game?! Hell yeah!
-rw-r--r--game/client/io/glfw.hh4
-rw-r--r--game/server/game.cc7
-rw-r--r--game/server/world/CMakeLists.txt2
-rw-r--r--game/server/world/random_tick.cc40
-rw-r--r--game/server/world/random_tick.hh14
-rw-r--r--game/shared/entity/grounded.hh6
-rw-r--r--game/shared/game.hh4
-rw-r--r--game/shared/game_items.hh4
-rw-r--r--game/shared/game_voxels.cc13
-rw-r--r--game/shared/game_voxels.hh4
-rw-r--r--game/shared/world/chunk_aabb.hh4
-rw-r--r--game/shared/world/ray_dda.hh4
12 files changed, 76 insertions, 30 deletions
diff --git a/game/client/io/glfw.hh b/game/client/io/glfw.hh
index bbd767a..7697d97 100644
--- a/game/client/io/glfw.hh
+++ b/game/client/io/glfw.hh
@@ -1,5 +1,3 @@
-#ifndef CLIENTFW
-#define CLIENTFW 1
#pragma once
namespace io
@@ -36,5 +34,3 @@ struct GlfwScrollEvent final {
float dy;
};
} // namespace io
-
-#endif // CLIENTFW
diff --git a/game/server/game.cc b/game/server/game.cc
index 8624670..f9802ae 100644
--- a/game/server/game.cc
+++ b/game/server/game.cc
@@ -28,6 +28,7 @@
#include "shared/protocol.hh"
#include "shared/splash.hh"
+#include "server/world/random_tick.hh"
#include "server/world/universe.hh"
#include "server/world/unloader.hh"
#include "server/world/worldgen.hh"
@@ -69,6 +70,8 @@ void server_game::init(void)
world::unloader::init();
world::universe::init();
+
+ world::random_tick::init();
}
void server_game::init_late(void)
@@ -128,6 +131,10 @@ void server_game::fixed_update(void)
entity::Transform::fixed_update(dimension.second);
entity::Gravity::fixed_update(dimension.second);
entity::Stasis::fixed_update(dimension.second);
+
+ for(auto [entity, component] : dimension.second->chunks.view<world::ChunkComponent>().each()) {
+ world::random_tick::tick(component.cpos, component.chunk);
+ }
}
}
diff --git a/game/server/world/CMakeLists.txt b/game/server/world/CMakeLists.txt
index e8fd4be..58a2216 100644
--- a/game/server/world/CMakeLists.txt
+++ b/game/server/world/CMakeLists.txt
@@ -2,6 +2,8 @@ target_sources(vserver PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/inhabited.hh"
"${CMAKE_CURRENT_LIST_DIR}/overworld.cc"
"${CMAKE_CURRENT_LIST_DIR}/overworld.hh"
+ "${CMAKE_CURRENT_LIST_DIR}/random_tick.cc"
+ "${CMAKE_CURRENT_LIST_DIR}/random_tick.hh"
"${CMAKE_CURRENT_LIST_DIR}/universe.cc"
"${CMAKE_CURRENT_LIST_DIR}/universe.hh"
"${CMAKE_CURRENT_LIST_DIR}/unloader.cc"
diff --git a/game/server/world/random_tick.cc b/game/server/world/random_tick.cc
new file mode 100644
index 0000000..c5fa47c
--- /dev/null
+++ b/game/server/world/random_tick.cc
@@ -0,0 +1,40 @@
+#include "server/pch.hh"
+
+#include "server/world/random_tick.hh"
+
+#include "core/config/number.hh"
+
+#include "core/io/config_map.hh"
+
+#include "shared/world/chunk.hh"
+#include "shared/world/dimension.hh"
+#include "shared/world/voxel.hh"
+
+#include "shared/coord.hh"
+
+#include "server/globals.hh"
+
+static config::Int random_tick_speed(2, 1, 1000);
+static std::mt19937_64 random_source;
+
+void world::random_tick::init(void)
+{
+ globals::server_config.add_value("world.random_tick_speed", random_tick_speed);
+
+ random_source.seed(std::random_device {}());
+}
+
+void world::random_tick::tick(const chunk_pos& cpos, Chunk* chunk)
+{
+ assert(chunk);
+
+ for(int i = 0; i < random_tick_speed.get_value(); ++i) {
+ auto voxel_index = random_source() % CHUNK_VOLUME;
+ auto lpos = coord::to_local(voxel_index);
+ auto vpos = coord::to_voxel(cpos, lpos);
+
+ if(auto voxel = chunk->get_voxel(lpos)) {
+ voxel->on_tick(chunk->get_dimension(), vpos);
+ }
+ }
+}
diff --git a/game/server/world/random_tick.hh b/game/server/world/random_tick.hh
new file mode 100644
index 0000000..4ef1691
--- /dev/null
+++ b/game/server/world/random_tick.hh
@@ -0,0 +1,14 @@
+#pragma once
+
+#include "shared/types.hh"
+
+namespace world
+{
+class Chunk;
+} // namespace world
+
+namespace world::random_tick
+{
+void init(void);
+void tick(const chunk_pos& cpos, Chunk* chunk);
+} // namespace world::random_tick
diff --git a/game/shared/entity/grounded.hh b/game/shared/entity/grounded.hh
index 940cebe..34a0f9e 100644
--- a/game/shared/entity/grounded.hh
+++ b/game/shared/entity/grounded.hh
@@ -1,8 +1,6 @@
-#ifndef SHARED_ENTITY_GROUNDED
-#define SHARED_ENTITY_GROUNDED 1
#pragma once
-#include "shared/world/voxel_registry.hh"
+#include "shared/world/voxel.hh"
namespace entity
{
@@ -12,5 +10,3 @@ struct Grounded final {
world::VoxelMaterial surface;
};
} // namespace entity
-
-#endif // SHARED_ENTITY_GROUNDED
diff --git a/game/shared/game.hh b/game/shared/game.hh
index 21286e8..0dfbadb 100644
--- a/game/shared/game.hh
+++ b/game/shared/game.hh
@@ -1,5 +1,3 @@
-#ifndef SHARED_GAME
-#define SHARED_GAME 1
#pragma once
namespace shared_game
@@ -7,5 +5,3 @@ namespace shared_game
void init(int argc, char** argv);
void shutdown(void);
} // namespace shared_game
-
-#endif // SHARED_GAME
diff --git a/game/shared/game_items.hh b/game/shared/game_items.hh
index 88952fc..6f8eac9 100644
--- a/game/shared/game_items.hh
+++ b/game/shared/game_items.hh
@@ -1,5 +1,3 @@
-#ifndef SHARED_GAME_ITEMS
-#define SHARED_GAME_ITEMS 1
#pragma once
namespace world
@@ -24,5 +22,3 @@ namespace game_items
{
void populate(void);
} // namespace game_items
-
-#endif // SHARED_GAME_ITEMS
diff --git a/game/shared/game_voxels.cc b/game/shared/game_voxels.cc
index f262c23..51cf873 100644
--- a/game/shared/game_voxels.cc
+++ b/game/shared/game_voxels.cc
@@ -54,11 +54,21 @@ static void dirt_tick(world::Dimension* dimension, const voxel_pos& vpos)
}
if(grass_found && air_above) {
- // Replace itself with the grass voxel
dimension->set_voxel(game_voxels::grass, vpos);
}
}
+static void grass_tick(world::Dimension* dimension, const voxel_pos& vpos)
+{
+ auto above_vpos = vpos + voxel_pos(0, 1, 0);
+ auto above_voxel = dimension->get_voxel(above_vpos);
+
+ if(above_voxel && !above_voxel->is_surface_material<world::VMAT_GLASS>()) {
+ // Decay into dirt if something is blocking airflow
+ dimension->set_voxel(game_voxels::dirt, vpos);
+ }
+}
+
void game_voxels::populate(void)
{
auto stone_builder = world::VoxelBuilder("stone");
@@ -92,6 +102,7 @@ void game_voxels::populate(void)
grass_builder.add_face_texture(world::VFACE_TOP, "textures/voxel/grass_01.png");
grass_builder.add_face_texture(world::VFACE_TOP, "textures/voxel/grass_02.png");
grass_builder.set_surface_material(world::VMAT_GRASS);
+ grass_builder.set_on_tick(&grass_tick);
grass = world::voxel_registry::register_voxel(grass_builder);
auto vtest_builder = world::VoxelBuilder("vtest");
diff --git a/game/shared/game_voxels.hh b/game/shared/game_voxels.hh
index 68e599e..2211619 100644
--- a/game/shared/game_voxels.hh
+++ b/game/shared/game_voxels.hh
@@ -1,5 +1,3 @@
-#ifndef SHARED_GAME_VOXELS
-#define SHARED_GAME_VOXELS 1
#pragma once
namespace world
@@ -26,5 +24,3 @@ namespace game_voxels
{
void populate(void);
} // namespace game_voxels
-
-#endif // SHARED_GAME_VOXELS
diff --git a/game/shared/world/chunk_aabb.hh b/game/shared/world/chunk_aabb.hh
index 3a2d26f..d926b55 100644
--- a/game/shared/world/chunk_aabb.hh
+++ b/game/shared/world/chunk_aabb.hh
@@ -1,5 +1,3 @@
-#ifndef SHARED_CHUNK_AABB
-#define SHARED_CHUNK_AABB 1
#pragma once
#include "core/math/aabb.hh"
@@ -10,5 +8,3 @@ namespace world
{
using ChunkAABB = math::AABB<chunk_pos::value_type>;
} // namespace world
-
-#endif // SHARED_CHUNK_AABB
diff --git a/game/shared/world/ray_dda.hh b/game/shared/world/ray_dda.hh
index 3071be2..110e3d4 100644
--- a/game/shared/world/ray_dda.hh
+++ b/game/shared/world/ray_dda.hh
@@ -1,5 +1,3 @@
-#ifndef SHARED_RAY_DDA
-#define SHARED_RAY_DDA 1
#pragma once
#include "shared/types.hh"
@@ -38,5 +36,3 @@ public:
voxel_pos vpos;
};
} // namespace world
-
-#endif // SHARED_RAY_DDA