5#include <simplesquirrel/simplesquirrel.hpp>
12void Logistics::expose(ssq::Table& table) {
13 auto cls =
table.addClass(
name, Logistics::create,
false);
16 auto item =
table.addClass<ItemState>(
"LogisticsItemState", ssq::Class::Ctor<ItemState()>());
25 auto machine =
table.addClass<MachineState>(
"LogisticsMachineState", ssq::Class::Ctor<MachineState()>());
39void Logistics::expose(ssq::Class&
cls) {
49 const std::string
id =
recipe.get<std::string>(
"id");
50 const int inputType =
recipe.get<
int>(
"inputType");
51 const int inputCount =
recipe.get<
int>(
"inputCount");
52 const int outputType =
recipe.get<
int>(
"outputType");
53 const int outputCount =
recipe.get<
int>(
"outputCount");
54 const float durationSec =
recipe.get<
float>(
"durationSec");
56 vm, self->registerRecipe(
id, inputType, inputCount, outputType, outputCount, durationSec),
57 [](
int index) { return Value(index); });
58 }
catch (
const std::exception&
error) {
61 [](
int index) { return Value(index); });
70 cls.addFunc(
"placeMachine",
73 [](
int id) { return Value(id); });
75 cls.addFunc(
"removeMachine", [
vm](
Logistics* self,
int machineId) {
89 return Value(Value::Object{{
"tick", Value(std::to_string(report.tick.value()))},
90 {
"elapsedNanoseconds", Value(report.elapsed.nanoseconds())},
91 {
"itemsMoved", Value(report.itemsMoved)},
92 {
"craftsStarted", Value(report.craftsStarted)},
93 {
"craftsFinished", Value(report.craftsFinished)},
94 {
"itemsSpawned", Value(report.itemsSpawned)},
95 {
"itemsSunk", Value(report.itemsSunk)},
96 {
"jammedMoves", Value(report.jammedMoves)}});
99 cls.addFunc(
"getWidth", [](Logistics* self) {
return self->width(); });
100 cls.addFunc(
"getHeight", [](Logistics* self) {
return self->height(); });
101 cls.addFunc(
"getLayers", [](Logistics* self) {
return self->layers(); });
102 cls.addFunc(
"getCellSize", [](Logistics* self) {
return self->cellSize(); });
103 cls.addFunc(
"getOriginX", [](Logistics* self) {
return self->originX(); });
104 cls.addFunc(
"getOriginY", [](Logistics* self) {
return self->originY(); });
105 cls.addFunc(
"getCurrentTick",
106 [](Logistics* self) {
return static_cast<std::int64_t
>(self->currentTick().value()); });
107 cls.addFunc(
"cellKind", [](Logistics* self,
int cx,
int cy,
int layer) {
return self->cellKind(
cx,
cy,
layer); });
108 cls.addFunc(
"cellDir", [](Logistics* self,
int cx,
int cy,
int layer) {
return self->cellDir(
cx,
cy,
layer); });
109 cls.addFunc(
"cellMachine",
110 [](Logistics* self,
int cx,
int cy,
int layer) {
return self->cellMachine(
cx,
cy,
layer); });
111 cls.addFunc(
"getItemCount", [](Logistics* self) {
return self->itemCount(); });
112 cls.addFunc(
"getItem", [](Logistics* self,
int index) {
115 state.ignore(
"getItem out of range");
118 return std::move(state).takeValue();
120 cls.addFunc(
"packItemSimFloats", [
vm](Logistics* self) {
123 arr.reserve(floats.size());
124 for (float f : floats) arr.emplace_back(f);
125 return Value(std::move(arr));
128 cls.addFunc(
"packItemWorldFloats", [
vm](Logistics* self) {
131 arr.reserve(floats.size());
132 for (float f : floats) arr.emplace_back(f);
133 return Value(std::move(arr));
136 cls.addFunc(
"fillItemWorldFloats", [](Logistics* self, ssq::Array xs, ssq::Array ys, ssq::Array types) {
137 const int n = self->itemCount();
138 const std::size_t
count = std::min({xs.size(), ys.size(), types.size(),
static_cast<std::size_t
>(
n)});
139 for (std::size_t i = 0; i <
count; ++i) {
140 auto state = self->itemAt(
static_cast<int>(i));
142 state.ignore(
"fillItemWorldFloats skipped missing item");
145 const ItemState& snap =
state.value();
148 types.set(i,
static_cast<float>(snap.type));
150 return static_cast<int>(
count);
152 cls.addFunc(
"getMachineSlotCount", [](Logistics* self) {
return self->machineSlotCount(); });
153 cls.addFunc(
"getMachine", [](Logistics* self,
int index) {
156 state.ignore(
"getMachine empty slot");
157 return MachineState{};
159 return std::move(state).takeValue();
161 cls.addFunc(
"machineOwnerId", [](Logistics* self,
int index) {
return self->machineOwnerId(
index); });
162 cls.addFunc(
"getSunkTotal", [](Logistics* self) {
return self->sunkTotal(); });
163 cls.addFunc(
"getCraftsTotal", [](Logistics* self) {
return self->craftsTotal(); });
164 cls.addFunc(
"setBeltSpeed", [](Logistics* self,
float speed) { self->setBeltSpeed(speed); });
165 cls.addFunc(
"getBeltSpeed", [](Logistics* self) {
return self->beltSpeed(); });
166 cls.addFunc(
"setBufferCapacity", [](Logistics* self,
int capacity) { self->setBufferCapacity(
capacity); });
167 cls.addFunc(
"getBufferCapacity", [](Logistics* self) {
return self->bufferCapacity(); });
struct SQVM * HSQUIRRELVM
ScriptMotionSinkStore< ScriptMotionFloatSink > floats
The single Squirrel projection for common Result, Status and Value.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
virtual std::string getName() const =0
Returns the name.
static Result failure(Status status)
Construct a failed result from a structured status.
int height() const noexcept
Grid height in cells.
Logistics()
Construct an empty unconfigured logistics world.
float originX() const noexcept
World origin X.
float cellSize() const noexcept
World units per cell.
float originY() const noexcept
World origin Y.
int layers() const noexcept
Configured floor count.
void clear()
Clear every cell, item, machine, recipe and reset the simulation clock.
int width() const noexcept
Grid width in cells.
std::unordered_map< std::string, SkillDefinition > & table()
ssq::Table projectResult(HSQUIRRELVM vm, Result< void > &&result)
Consume and project a void native Result using the common schema.
float x
World X (cell center + progress along belt).