From b6b585b700bf4f20a1319cd717f206c74f0530dd Mon Sep 17 00:00:00 2001 From: szdytom Date: Tue, 5 Aug 2025 13:16:57 +0800 Subject: [PATCH] feat: add core library and implement device and room components Signed-off-by: szdytom --- CMakeLists.txt | 3 ++ core/CMakeLists.txt | 10 ++++ core/include/instructed.h | 0 core/include/istd_core/device.h | 68 +++++++++++++++++++++++++++ core/include/istd_core/item.h | 82 +++++++++++++++++++++++++++++++++ core/include/istd_core/room.h | 26 +++++++++++ core/include/istd_core/unit.h | 40 ++++++++++++++++ core/include/istd_core/world.h | 20 ++++++++ core/src/device.cpp | 36 +++++++++++++++ core/src/room.cpp | 1 + 10 files changed, 286 insertions(+) create mode 100644 core/CMakeLists.txt create mode 100644 core/include/instructed.h create mode 100644 core/include/istd_core/device.h create mode 100644 core/include/istd_core/item.h create mode 100644 core/include/istd_core/room.h create mode 100644 core/include/istd_core/unit.h create mode 100644 core/include/istd_core/world.h create mode 100644 core/src/device.cpp create mode 100644 core/src/room.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c4981cb..738fd54 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,3 +16,6 @@ add_subdirectory(util) # Add tilemap library and examples add_subdirectory(tilemap) + +# Add server core library +add_subdirectory(core) diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt new file mode 100644 index 0000000..d3ea2fa --- /dev/null +++ b/core/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.27) + +set(ISTD_CORE_SRC + src/room.cpp +) + +add_library(istd_core STATIC ${ISTD_CORE_SRC}) +target_include_directories(istd_core PUBLIC include) +target_link_libraries(istd_core PUBLIC istd_util istd_tilemap EnTT) +target_compile_features(istd_core PUBLIC cxx_std_23) diff --git a/core/include/instructed.h b/core/include/instructed.h new file mode 100644 index 0000000..e69de29 diff --git a/core/include/istd_core/device.h b/core/include/istd_core/device.h new file mode 100644 index 0000000..c8371cd --- /dev/null +++ b/core/include/istd_core/device.h @@ -0,0 +1,68 @@ +#ifndef ISTD_CORE_DEVICE_H +#define ISTD_CORE_DEVICE_H + +#include "entt/entity/fwd.hpp" +#include "istd_core/item.h" +#include "istd_core/world.h" +#include "istd_util/small_map.h" +#include +#include +#include + +namespace istd { + +using ItemPort = std::int16_t; +using DeviceId = std::uint8_t; + +// device's register +class RegSetStrategy { +public: + virtual bool read( + World &world, entt::entity entity, std::uint8_t reg_id, + std::uint32_t &value + ) const noexcept; + + virtual bool write( + World &world, entt::entity entity, std::uint8_t reg_id, + std::uint32_t value + ) const noexcept; +}; + +struct DevicePrototype { + std::string_view name; + RegSetStrategy *reg_set_strategy; // life cycle: static + std::uint32_t mass; + ItemPort input_n, output_n; // number of input/output ports +}; + +struct DevicePrototypeComponent { + DevicePrototype *prototype; // life cycle: static +}; + +struct DeviceIdComponent { + entt::entity unit; // the unit this device belongs to + std::uint8_t device_id; // unique ID within the unit +}; + +struct DeviceBuilder { + virtual entt::entity build( + World &world, entt::entity unit, DeviceId device_id + ) const + = 0; +}; + +struct DeviceBuilderRegistry { + static DeviceBuilderRegistry &instance(); + + void register_builder(Item item, DeviceBuilder *builder); + entt::entity build( + World &world, Item item, entt::entity unit, DeviceId device_id + ) const; + +private: + SmallMap builders_; +}; + +} // namespace istd + +#endif diff --git a/core/include/istd_core/item.h b/core/include/istd_core/item.h new file mode 100644 index 0000000..3613fbd --- /dev/null +++ b/core/include/istd_core/item.h @@ -0,0 +1,82 @@ +#ifndef ISTD_CORE_ITEM_H +#define ISTD_CORE_ITEM_H + +#include + +namespace istd { + +consteval std::uint32_t id_string(const char *str) { + std::uint32_t id = 0; + for (int i = 0; i < 4; ++i) { + if (str[i] == '\0') { + break; + } + id |= static_cast(str[i]) << (i * 8); + } + return id; +} + +enum class Item : std::uint32_t { + Null = 0, + + // Materials + // Raw + Sand = id_string("sand"), + Rock = id_string("rock"), + Ice = id_string("ice"), + Salt = id_string("salt"), + Algae = id_string("alga"), + Coal = id_string("coal"), + Hematite = id_string("Fe+O"), + Titanomagnetite = id_string("TiFe"), + Gibbsite = id_string("Al+O"), + + // Fundamentals + Concrete = id_string("ccrt"), + Plastic = id_string("pltc"), + Ferrum = id_string("Fe"), + Titanium = id_string("Ti"), + Aluminium = id_string("Al"), + Silicon = id_string("Si"), + Glass = id_string("glas"), + Lithium = id_string("Li"), + Ammonia = id_string("NH3"), + Hydrocarbon = id_string("C=C"), + Tritium = id_string("T"), + Explosive = id_string("expl"), + + // Structures + Core = id_string("core"), + + // Walls + ConcreteWall = id_string("WL-C"), + AlloyWall = id_string("WL-A"), + + // Extractors + LaserDrill = id_string("DR-L"), + AlloyDrill = id_string("DR-A"), + ExplodeDrill = id_string("DR-E"), + LakePump = id_string("PP-W"), + OceanPump = id_string("PP-S"), + AlgaeCollector = id_string("ALC"), + + // Power systems + SolarPanel = id_string("SP"), + SteamGenerator = id_string("SG"), + DifferentialGenerator = id_string("DG"), + WaveGenerator = id_string("WG"), + RTG = id_string("RTG"), + FusionReactor = id_string("FR"), + Battery = id_string("BATT"), + + // Turrets + LaserTurret = id_string("LSLS"), + + // Devices + BasicVehicleChassis = id_string("VC-B"), + PoweredVehicleChassis = id_string("VC-P"), +}; + +} // namespace istd + +#endif diff --git a/core/include/istd_core/room.h b/core/include/istd_core/room.h new file mode 100644 index 0000000..1079211 --- /dev/null +++ b/core/include/istd_core/room.h @@ -0,0 +1,26 @@ +#ifndef ISTD_CORE_ROOM_H +#define ISTD_CORE_ROOM_H + +#include "istd_util/small_map.h" +#include "tilemap/chunk.h" +#include +#include + +namespace istd { + +// A Room <-> a chunk in tilemap +class Room { + std::uint8_t chunk_x_, chunk_y_; + SmallMap, entt::entity> structures_; + +public: + Room(std::uint8_t chunk_x, std::uint8_t chunk_y); + + TilePos tilepos_of(std::uint8_t local_x, std::uint8_t local_y) const { + return {chunk_x_, chunk_y_, local_x, local_y}; + } +}; + +} // namespace istd + +#endif \ No newline at end of file diff --git a/core/include/istd_core/unit.h b/core/include/istd_core/unit.h new file mode 100644 index 0000000..7d090da --- /dev/null +++ b/core/include/istd_core/unit.h @@ -0,0 +1,40 @@ +#ifndef ISTD_CORE_UNIT_H +#define ISTD_CORE_UNIT_H + +#include "entt/entt.hpp" +#include "istd_util/vec2.h" +#include + +namespace istd { + +// A Unit is an entity in the world that can move without aligning to the +// tilemap grid, i.e. have floating-point coordinates. + +/** + * @brief Component to unit identification. + */ +struct UnitIdComponent { + std::uint8_t room_x, room_y; + std::uint8_t unit_id; +}; + +/** + * @brief Component for unit's position and velocity. + */ +struct KinematicsComponent { + Vec2 position; + Vec2 velocity; +}; + +/** + * @brief Component for unit's device stack. + * + * Contains a list of devices (entities) that the unit has. + */ +struct DeviceStackComponent { + std::vector devices; +}; + +} // namespace istd + +#endif diff --git a/core/include/istd_core/world.h b/core/include/istd_core/world.h new file mode 100644 index 0000000..3ace3d4 --- /dev/null +++ b/core/include/istd_core/world.h @@ -0,0 +1,20 @@ +#ifndef ISTD_CORE_WORLD_H +#define ISTD_CORE_WORLD_H + +#include "istd_core/room.h" +#include "tilemap/tilemap.h" +#include + +namespace istd { + +struct World { + TileMap tilemap; + std::vector> rooms; + entt::registry registry; + + World(std::uint8_t size); +}; + +} // namespace istd + +#endif diff --git a/core/src/device.cpp b/core/src/device.cpp new file mode 100644 index 0000000..6e1c5cb --- /dev/null +++ b/core/src/device.cpp @@ -0,0 +1,36 @@ +#include "istd_core/device.h" +#include "istd_core/world.h" + +namespace istd { + +bool RegSetStrategy::read( + World &, entt::entity, std::uint8_t, std::uint32_t & +) const noexcept { + return false; +} + +bool RegSetStrategy::write( + World &, entt::entity, std::uint8_t, std::uint32_t +) const noexcept { + return false; +} + +DeviceBuilderRegistry &DeviceBuilderRegistry::instance() { + static DeviceBuilderRegistry registry; + return registry; +} + +void DeviceBuilderRegistry::register_builder( + Item item, DeviceBuilder *builder +) { + builders_.insert(item, builder); +} + +entt::entity DeviceBuilderRegistry::build( + World &world, Item item, entt::entity unit, DeviceId device_id +) const { + auto builder = builders_[item]; + return builder->build(world, unit, device_id); +} + +} // namespace istd diff --git a/core/src/room.cpp b/core/src/room.cpp new file mode 100644 index 0000000..19e73b8 --- /dev/null +++ b/core/src/room.cpp @@ -0,0 +1 @@ +#include "istd_core/room.h"