feat: add core library and implement device and room components
Signed-off-by: szdytom <szdytom@qq.com>
This commit is contained in:
parent
f8a2b0c9eb
commit
b6b585b700
@ -16,3 +16,6 @@ add_subdirectory(util)
|
||||
|
||||
# Add tilemap library and examples
|
||||
add_subdirectory(tilemap)
|
||||
|
||||
# Add server core library
|
||||
add_subdirectory(core)
|
||||
|
10
core/CMakeLists.txt
Normal file
10
core/CMakeLists.txt
Normal file
@ -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)
|
0
core/include/instructed.h
Normal file
0
core/include/instructed.h
Normal file
68
core/include/istd_core/device.h
Normal file
68
core/include/istd_core/device.h
Normal file
@ -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 <cstdint>
|
||||
#include <entt/entt.hpp>
|
||||
#include <string_view>
|
||||
|
||||
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<Item, DeviceBuilder *> builders_;
|
||||
};
|
||||
|
||||
} // namespace istd
|
||||
|
||||
#endif
|
82
core/include/istd_core/item.h
Normal file
82
core/include/istd_core/item.h
Normal file
@ -0,0 +1,82 @@
|
||||
#ifndef ISTD_CORE_ITEM_H
|
||||
#define ISTD_CORE_ITEM_H
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
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<std::uint32_t>(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
|
26
core/include/istd_core/room.h
Normal file
26
core/include/istd_core/room.h
Normal file
@ -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 <entt/entt.hpp>
|
||||
#include <tuple>
|
||||
|
||||
namespace istd {
|
||||
|
||||
// A Room <-> a chunk in tilemap
|
||||
class Room {
|
||||
std::uint8_t chunk_x_, chunk_y_;
|
||||
SmallMap<std::tuple<std::uint8_t, std::uint8_t>, 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
|
40
core/include/istd_core/unit.h
Normal file
40
core/include/istd_core/unit.h
Normal file
@ -0,0 +1,40 @@
|
||||
#ifndef ISTD_CORE_UNIT_H
|
||||
#define ISTD_CORE_UNIT_H
|
||||
|
||||
#include "entt/entt.hpp"
|
||||
#include "istd_util/vec2.h"
|
||||
#include <vector>
|
||||
|
||||
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<entt::entity> devices;
|
||||
};
|
||||
|
||||
} // namespace istd
|
||||
|
||||
#endif
|
20
core/include/istd_core/world.h
Normal file
20
core/include/istd_core/world.h
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef ISTD_CORE_WORLD_H
|
||||
#define ISTD_CORE_WORLD_H
|
||||
|
||||
#include "istd_core/room.h"
|
||||
#include "tilemap/tilemap.h"
|
||||
#include <vector>
|
||||
|
||||
namespace istd {
|
||||
|
||||
struct World {
|
||||
TileMap tilemap;
|
||||
std::vector<std::vector<Room>> rooms;
|
||||
entt::registry registry;
|
||||
|
||||
World(std::uint8_t size);
|
||||
};
|
||||
|
||||
} // namespace istd
|
||||
|
||||
#endif
|
36
core/src/device.cpp
Normal file
36
core/src/device.cpp
Normal file
@ -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
|
1
core/src/room.cpp
Normal file
1
core/src/room.cpp
Normal file
@ -0,0 +1 @@
|
||||
#include "istd_core/room.h"
|
Loading…
x
Reference in New Issue
Block a user