载入中...
搜索中...
未找到
Logistics.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4#include "common/Module.h"
5#include "common/Result.h"
6#include "common/Time.h"
7
8#include <cstdint>
9#include <string>
10#include <string_view>
11#include <vector>
12
13namespace eve::logistics {
14
16enum class Dir : std::uint8_t { East = 0, South = 1, West = 2, North = 3 };
17
19enum class CellKind : std::uint8_t { Empty = 0, Belt = 1, Machine = 2, Source = 3, Sink = 4 };
20
22struct Recipe {
23 std::string id;
24 int inputType = 0;
25 int inputCount = 1;
26 int outputType = 0;
27 int outputCount = 1;
28 float durationSec = 1.0f;
29};
30
42
44struct ItemState {
45 float x = 0.f;
46 float y = 0.f;
47 int type = 0;
48 int cellX = 0;
49 int cellY = 0;
50 float progress = 0.f;
51 int dir = 0;
52};
53
56 int id = -1;
57 int originX = 0;
58 int originY = 0;
59 int width = 1;
60 int height = 1;
61 int recipeIndex = -1;
62 int inputStored = 0;
63 int outputStored = 0;
64 float craftProgress = 0.f;
65 bool crafting = false;
66 int layer = 0;
67};
68
83public:
88 ~Logistics() override;
89
101 [[nodiscard]] Result<void> configure(int width, int height, int layers, float cellSize, float originX,
102 float originY);
103
105 void clear();
106
108 [[nodiscard]] Result<int> registerRecipe(std::string id, int inputType, int inputCount, int outputType,
109 int outputCount, float durationSec);
110
112 [[nodiscard]] Result<void> placeBelt(int cx, int cy, int dir, int layer = 0);
113
115 [[nodiscard]] Result<void> clearCell(int cx, int cy, int layer = 0);
116
122 [[nodiscard]] Result<int> placeMachine(int cx, int cy, int width, int height, std::string_view recipeId,
123 int layer = 0);
124
126 [[nodiscard]] Result<void> removeMachine(int machineId);
127
129 [[nodiscard]] Result<void> placeSource(int cx, int cy, int itemType, float itemsPerSec, int layer = 0);
130
132 [[nodiscard]] Result<void> placeSink(int cx, int cy, int layer = 0);
133
135 [[nodiscard]] Result<int> spawnItem(int cx, int cy, int itemType, int layer = 0);
136
142 [[nodiscard]] Result<StepReport> advance(const SimulationStep& step);
143
148 [[nodiscard]] Result<StepReport> advance(float dt);
149
151 [[nodiscard]] int width() const noexcept { return width_; }
153 [[nodiscard]] int height() const noexcept { return height_; }
155 [[nodiscard]] int layers() const noexcept { return layers_; }
157 [[nodiscard]] float cellSize() const noexcept { return cellSize_; }
159 [[nodiscard]] float originX() const noexcept { return originX_; }
161 [[nodiscard]] float originY() const noexcept { return originY_; }
163 [[nodiscard]] SimulationTick currentTick() const noexcept { return tick_; }
164
166 [[nodiscard]] int cellKind(int cx, int cy, int layer = 0) const noexcept;
168 [[nodiscard]] int cellDir(int cx, int cy, int layer = 0) const noexcept;
170 [[nodiscard]] int cellMachine(int cx, int cy, int layer = 0) const noexcept;
171
173 [[nodiscard]] int itemCount() const noexcept { return static_cast<int>(itemType_.size()); }
175 [[nodiscard]] Result<ItemState> itemAt(int index) const;
180 [[nodiscard]] Result<std::vector<float>> packItemSimFloats() const;
185 [[nodiscard]] Result<std::vector<float>> packItemWorldFloats() const;
186
188 [[nodiscard]] int machineSlotCount() const noexcept { return static_cast<int>(machines_.size()); }
190 [[nodiscard]] Result<MachineState> machineAt(int index) const;
192 [[nodiscard]] std::string machineOwnerId(int index) const;
193
195 [[nodiscard]] int sunkTotal() const noexcept { return sunkTotal_; }
197 [[nodiscard]] int craftsTotal() const noexcept { return craftsTotal_; }
198
200 void setBeltSpeed(float cellsPerSec) noexcept { beltSpeed_ = cellsPerSec > 0.f ? cellsPerSec : beltSpeed_; }
202 [[nodiscard]] float beltSpeed() const noexcept { return beltSpeed_; }
204 void setBufferCapacity(int capacity) noexcept { bufferCapacity_ = capacity > 0 ? capacity : bufferCapacity_; }
206 [[nodiscard]] int bufferCapacity() const noexcept { return bufferCapacity_; }
207
208private:
209 struct Cell {
210 CellKind kind = CellKind::Empty;
211 Dir dir = Dir::East;
212 int machineSlot = -1;
213 int itemType = 0;
214 float spawnRate = 0.f;
215 float spawnAcc = 0.f;
216 };
217
218 struct Machine {
219 bool alive = false;
220 int id = -1;
221 int originX = 0;
222 int originY = 0;
223 int width = 1;
224 int height = 1;
225 int layer = 0;
226 int recipeIndex = -1;
227 int inputStored = 0;
228 int outputStored = 0;
229 float craftProgress = 0.f;
230 bool crafting = false;
231 std::string ownerId;
232 };
233
234 struct Item {
235 int cellX = 0;
236 int cellY = 0;
237 int layer = 0;
238 int type = 0;
239 float progress = 0.f;
240 Dir dir = Dir::East;
241 };
242
243 [[nodiscard]] bool inBounds(int cx, int cy, int layer) const noexcept;
244 [[nodiscard]] std::size_t cellIndex(int cx, int cy, int layer) const noexcept;
245 static void dirDelta(Dir dir, int& dx, int& dy) noexcept;
246 [[nodiscard]] ItemState makeItemState(const Item& item) const noexcept;
247 [[nodiscard]] Result<void> ensureConfigured() const;
248 void destroyItemAt(std::size_t index);
249 [[nodiscard]] int findItemAt(int cx, int cy, int layer) const noexcept;
250 void stepBelts(float dt, StepReport& report);
251 void stepSources(float dt, StepReport& report);
252 void stepSinks(StepReport& report);
253 void stepMachines(float dt, StepReport& report);
254 void tryPullInputs();
255 void tryPushOutputs();
256
257 int width_ = 0;
258 int height_ = 0;
259 int layers_ = 0;
260 float cellSize_ = 16.f;
261 float originX_ = 0.f;
262 float originY_ = 0.f;
263 float beltSpeed_ = 4.f;
264 int bufferCapacity_ = 8;
265 int nextMachineId_ = 1;
266 int sunkTotal_ = 0;
267 int craftsTotal_ = 0;
268 SimulationTick tick_ = SimulationTick::zero();
269
270 std::vector<Cell> cells_;
271 std::vector<Machine> machines_;
272 std::vector<Recipe> recipes_;
273 // Item SoA (dense).
274 std::vector<int> itemType_;
275 std::vector<int> itemCellX_;
276 std::vector<int> itemCellY_;
277 std::vector<int> itemLayer_;
278 std::vector<float> itemProgress_;
279 std::vector<std::uint8_t> itemDir_;
280 std::vector<std::int32_t> occupancy_;
281};
282
283} // namespace eve::logistics
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
std::uint32_t alive
std::uint32_t capacity
std::uint32_t height
std::uint32_t width
glm::dvec3 dx
double dt
TileLayer * layer
Move-only, checked operation results for the common layer.
std::string kind
float dy
Definition Shadow.cpp:151
Common time values and an injectable deterministic simulation clock.
float step
Definition TreeMesh.cpp:451
V3 dir
Definition TreeMesh.cpp:151
uint32_t index
Signed, fixed-resolution simulation duration in nanoseconds.
Definition Time.h:43
EVENGINE_API_FOUNDATION public API.
Definition Module.h:48
Move-only operation result carrying either a value or Status.
Definition Result.h:176
Deterministic 2D factory logistics: belts, discrete items, machine buffers, crafts.
Definition Logistics.h:82
int machineSlotCount() const noexcept
Live machine count (includes holes after remove; use machineAt).
Definition Logistics.h:188
int height() const noexcept
Grid height in cells.
Definition Logistics.h:153
float beltSpeed() const noexcept
Current belt speed.
Definition Logistics.h:202
~Logistics() override
Destroy logistics storage.
int bufferCapacity() const noexcept
Current buffer capacity.
Definition Logistics.h:206
Logistics()
Construct an empty unconfigured logistics world.
SimulationTick currentTick() const noexcept
Last accepted simulation tick.
Definition Logistics.h:163
float originX() const noexcept
World origin X.
Definition Logistics.h:159
float cellSize() const noexcept
World units per cell.
Definition Logistics.h:157
int sunkTotal() const noexcept
Items destroyed by sinks since configure/clear.
Definition Logistics.h:195
float originY() const noexcept
World origin Y.
Definition Logistics.h:161
int layers() const noexcept
Configured floor count.
Definition Logistics.h:155
int craftsTotal() const noexcept
Crafts completed since configure/clear.
Definition Logistics.h:197
void setBeltSpeed(float cellsPerSec) noexcept
Default belt speed in cells per second.
Definition Logistics.h:200
void setBufferCapacity(int capacity) noexcept
Max items stored in a machine input or output buffer.
Definition Logistics.h:204
int width() const noexcept
Grid width in cells.
Definition Logistics.h:151
Dir
Orthographic belt / port facing on the 2D factory grid (Y grows south).
Definition Logistics.h:16
CellKind
Occupancy kind stored per grid cell.
Definition Logistics.h:19
detail::StrongUint64< detail::SimulationTickTag > SimulationTick
Deterministic simulation time step; it is not wall-clock time.
Definition Time.h:31
One deterministic fixed-step emitted by SimulationClock.
Definition Time.h:158
Read-only item snapshot for script / GPU packing.
Definition Logistics.h:44
float x
World X (cell center + progress along belt).
Definition Logistics.h:45
Read-only machine snapshot.
Definition Logistics.h:55
Compact recipe: consume inputCount of inputType, emit outputCount of outputType.
Definition Logistics.h:22
One completed fixed logistics step.
Definition Logistics.h:32