From 3fad4634e95c6e37686fc3e40aa0e7cb8ed7b023 Mon Sep 17 00:00:00 2001 From: Kirill Kirilenko Date: Sat, 5 Jun 2021 17:41:06 +0300 Subject: [PATCH] Add Deposit, Flag, Resource, StructureExtractor, StructureLink classes. Completed StructureController, StructureExtension, StructureRampart, StructureRoad, StructureSpawn classes. --- include/Screeps/Creep.hpp | 5 +- include/Screeps/Deposit.hpp | 26 +++++++ include/Screeps/Flag.hpp | 33 +++++++++ include/Screeps/JS.hpp | 27 +++++++- include/Screeps/Object.hpp | 2 + include/Screeps/Resource.hpp | 22 ++++++ include/Screeps/RoomObject.hpp | 2 +- include/Screeps/Store.hpp | 2 + include/Screeps/StructureController.hpp | 43 ++++++++++++ include/Screeps/StructureExtension.hpp | 4 ++ include/Screeps/StructureExtractor.hpp | 18 +++++ include/Screeps/StructureLink.hpp | 24 +++++++ include/Screeps/StructureRampart.hpp | 6 ++ include/Screeps/StructureRoad.hpp | 2 + include/Screeps/StructureSpawn.hpp | 41 +++++++++-- src/Creep.cpp | 9 ++- src/Deposit.cpp | 34 +++++++++ src/Flag.cpp | 65 ++++++++++++++++++ src/JS.cpp | 33 --------- src/Resource.cpp | 24 +++++++ src/RoomObject.cpp | 15 ++++ src/StructureController.cpp | 91 +++++++++++++++++++++++++ src/StructureExtension.cpp | 7 ++ src/StructureExtractor.cpp | 14 ++++ src/StructureLink.cpp | 29 ++++++++ src/StructureRampart.cpp | 15 ++++ src/StructureRoad.cpp | 5 ++ src/StructureSpawn.cpp | 69 +++++++++++++++++++ 28 files changed, 624 insertions(+), 43 deletions(-) create mode 100644 include/Screeps/Deposit.hpp create mode 100644 include/Screeps/Flag.hpp create mode 100644 include/Screeps/Resource.hpp create mode 100644 include/Screeps/StructureExtractor.hpp create mode 100644 include/Screeps/StructureLink.hpp create mode 100644 src/Deposit.cpp create mode 100644 src/Flag.cpp create mode 100644 src/Resource.cpp create mode 100644 src/StructureExtractor.cpp create mode 100644 src/StructureLink.cpp diff --git a/include/Screeps/Creep.hpp b/include/Screeps/Creep.hpp index 5f04ae3..89ec4a7 100644 --- a/include/Screeps/Creep.hpp +++ b/include/Screeps/Creep.hpp @@ -9,6 +9,7 @@ namespace Screeps { class ConstructionSite; +class Resource; class Source; class Store; class Structure; @@ -53,7 +54,7 @@ public: int ticksToLive() const; - int attack(const Object& target); + int attack(const RoomObject& target); int attackController(const StructureController& target); @@ -86,7 +87,7 @@ public: int notifyWhenAttacked(bool enabled); - // int pickup(const Resource& target); + int pickup(const Resource& target); int pull(const Creep& target); diff --git a/include/Screeps/Deposit.hpp b/include/Screeps/Deposit.hpp new file mode 100644 index 0000000..b930ec9 --- /dev/null +++ b/include/Screeps/Deposit.hpp @@ -0,0 +1,26 @@ +#ifndef SCREEPS_DEPOSIT_HPP +#define SCREEPS_DEPOSIT_HPP + +#include "RoomObject.hpp" + +namespace Screeps { + +class Deposit : public RoomObject +{ +public: + explicit Deposit(JS::Value value); + + int cooldown() const; + + std::string depositType() const; + + std::string id() const; + + int lastCooldown() const; + + int ticksToDecay() const; +}; + +} // namespace Screeps + +#endif // SCREEPS_DEPOSIT_HPP diff --git a/include/Screeps/Flag.hpp b/include/Screeps/Flag.hpp new file mode 100644 index 0000000..9dfdd19 --- /dev/null +++ b/include/Screeps/Flag.hpp @@ -0,0 +1,33 @@ +#ifndef SCREEPS_FLAG_HPP +#define SCREEPS_FLAG_HPP + +#include "RoomObject.hpp" + +namespace Screeps { + +class Flag : public RoomObject +{ +public: + explicit Flag(JS::Value value); + + int color() const; + + JSON memory() const; + void setMemory(const JSON&); + + std::string name() const; + + int secondaryColor() const; + + void remove(); + + int setColor(int color, std::optional secondaryColor); + + int setPosition(int x, int y); + int setPosition(const RoomPosition& pos); + int setPosition(const RoomObject& pos); +}; + +} // namespace Screeps + +#endif // SCREEPS_FLAG_HPP diff --git a/include/Screeps/JS.hpp b/include/Screeps/JS.hpp index b577d16..ce79c24 100644 --- a/include/Screeps/JS.hpp +++ b/include/Screeps/JS.hpp @@ -5,6 +5,7 @@ #include #include +#include #include using JSON = nlohmann::json; @@ -25,8 +26,30 @@ Value getConstant(const std::string& name); bool isInstanceOf(const Value& val, const char* name); -std::vector jsArrayToVector(const Value& array); -std::map jsObjectToMap(const Value& object); +template +std::vector jsArrayToVector(const Value& array) +{ + std::vector result; + int size = array["length"].as(); + result.reserve(size); + for (int i = 0; i < size; ++i) + result.emplace_back(array[i].as()); + return result; +} + +template +std::map jsObjectToMap(const Value& object) +{ + std::map result; + Value keys = gObject.call("keys", object); + int size = keys["length"].as(); + for (int i = 0; i < size; ++i) + { + auto key = keys[i].as(); + result.emplace_hint(result.end(), key, object[key].as()); + } + return result; +} template Value vectorToJSArray(const std::vector& vector) diff --git a/include/Screeps/Object.hpp b/include/Screeps/Object.hpp index 4d15eb9..aae0c65 100644 --- a/include/Screeps/Object.hpp +++ b/include/Screeps/Object.hpp @@ -8,6 +8,8 @@ namespace Screeps { class Object { public: + using Error = int; + JS::Value& value(); const JS::Value& value() const; void setValue(JS::Value value); diff --git a/include/Screeps/Resource.hpp b/include/Screeps/Resource.hpp new file mode 100644 index 0000000..58750ee --- /dev/null +++ b/include/Screeps/Resource.hpp @@ -0,0 +1,22 @@ +#ifndef SCREEPS_RESOURCE_HPP +#define SCREEPS_RESOURCE_HPP + +#include "RoomObject.hpp" + +namespace Screeps { + +class Resource : public RoomObject +{ +public: + explicit Resource(JS::Value value); + + int amount() const; + + std::string id() const; + + std::string resourceType() const; +}; + +} // namespace Screeps + +#endif // SCREEPS_RESOURCE_HPP diff --git a/include/Screeps/RoomObject.hpp b/include/Screeps/RoomObject.hpp index 15184e1..66cb546 100644 --- a/include/Screeps/RoomObject.hpp +++ b/include/Screeps/RoomObject.hpp @@ -12,7 +12,7 @@ class Room; class RoomObject : public Object { public: - RoomObject(JS::Value value); + explicit RoomObject(JS::Value value); virtual ~RoomObject(); diff --git a/include/Screeps/Store.hpp b/include/Screeps/Store.hpp index 8d2f01d..e4587a7 100644 --- a/include/Screeps/Store.hpp +++ b/include/Screeps/Store.hpp @@ -3,6 +3,8 @@ #include "Object.hpp" +#include + namespace Screeps { class Store : public Object diff --git a/include/Screeps/StructureController.hpp b/include/Screeps/StructureController.hpp index 434fb5b..858f2d4 100644 --- a/include/Screeps/StructureController.hpp +++ b/include/Screeps/StructureController.hpp @@ -3,12 +3,55 @@ #include "OwnedStructure.hpp" +#include +#include + namespace Screeps { class StructureController : public OwnedStructure { public: + struct Reservation + { + std::string username; + int ticksToEnd; + }; + + struct Sign + { + std::string username; + std::string text; + int time; + std::int64_t datetime; // ms since epoch (UTC) + }; + explicit StructureController(JS::Value value); + + bool isPowerEnabled() const; + + int level() const; + + int progress() const; + + int progressTotal() const; + + std::optional reservation() const; + + std::optional safeMode() const; + + int safeModeAvailable() const; + + std::optional safeModeCooldown() const; + + std::optional sign() const; + + int ticksToDowngrade() const; + + int upgradeBlocked() const; + + int activateSafeMode(); + + int unclaim(); }; } // namespace Screeps diff --git a/include/Screeps/StructureExtension.hpp b/include/Screeps/StructureExtension.hpp index 4dfdbd1..a11a8e2 100644 --- a/include/Screeps/StructureExtension.hpp +++ b/include/Screeps/StructureExtension.hpp @@ -5,10 +5,14 @@ namespace Screeps { +class Store; + class StructureExtension : public Structure { public: explicit StructureExtension(JS::Value value); + + Store store() const; }; } // namespace Screeps diff --git a/include/Screeps/StructureExtractor.hpp b/include/Screeps/StructureExtractor.hpp new file mode 100644 index 0000000..3e82d80 --- /dev/null +++ b/include/Screeps/StructureExtractor.hpp @@ -0,0 +1,18 @@ +#ifndef SCREEPS_STRUCTURE_EXTRACTOR_HPP +#define SCREEPS_STRUCTURE_EXTRACTOR_HPP + +#include "OwnedStructure.hpp" + +namespace Screeps { + +class StructureExtractor : public OwnedStructure +{ +public: + explicit StructureExtractor(JS::Value value); + + int cooldown() const; +}; + +} // namespace Screeps + +#endif // SCREEPS_STRUCTURE_EXTRACTOR_HPP diff --git a/include/Screeps/StructureLink.hpp b/include/Screeps/StructureLink.hpp new file mode 100644 index 0000000..a11622b --- /dev/null +++ b/include/Screeps/StructureLink.hpp @@ -0,0 +1,24 @@ +#ifndef SCREEPS_STRUCTURE_LINK_HPP +#define SCREEPS_STRUCTURE_LINK_HPP + +#include "OwnedStructure.hpp" + +namespace Screeps { + +class Store; + +class StructureLink : public OwnedStructure +{ +public: + explicit StructureLink(JS::Value value); + + int cooldown() const; + + Store store() const; + + int transferEnergy(const StructureLink& target, std::optional amount = std::nullopt); +}; + +} // namespace Screeps + +#endif // SCREEPS_STRUCTURE_LINK_HPP diff --git a/include/Screeps/StructureRampart.hpp b/include/Screeps/StructureRampart.hpp index 2199516..e79d5b9 100644 --- a/include/Screeps/StructureRampart.hpp +++ b/include/Screeps/StructureRampart.hpp @@ -9,6 +9,12 @@ class StructureRampart : public Structure { public: explicit StructureRampart(JS::Value value); + + bool isPublic() const; + + int ticksToDecay() const; + + int setPublic(bool isPublic); }; } // namespace Screeps diff --git a/include/Screeps/StructureRoad.hpp b/include/Screeps/StructureRoad.hpp index 51f9dd3..1bf3045 100644 --- a/include/Screeps/StructureRoad.hpp +++ b/include/Screeps/StructureRoad.hpp @@ -9,6 +9,8 @@ class StructureRoad : public Structure { public: explicit StructureRoad(JS::Value value); + + int ticksToDecay() const; }; } // namespace Screeps diff --git a/include/Screeps/StructureSpawn.hpp b/include/Screeps/StructureSpawn.hpp index 0c4973d..ef7b734 100644 --- a/include/Screeps/StructureSpawn.hpp +++ b/include/Screeps/StructureSpawn.hpp @@ -1,26 +1,59 @@ -#ifndef SPAWN_HPP -#define SPAWN_HPP +#ifndef SCREEPS_STRUCTURE_SPAWN_HPP +#define SCREEPS_STRUCTURE_SPAWN_HPP #include "OwnedStructure.hpp" +#include + namespace Screeps { +class Creep; class Store; class StructureSpawn : public OwnedStructure { public: + class Spawning; + explicit StructureSpawn(JS::Value obj); - Store store() const; + JSON memory() const; + void setMemory(const JSON&); std::string name() const; + std::optional spawning() const; + + Store store() const; int spawnCreep(const std::vector& body, const std::string& name); int spawnCreep(const std::vector& body, const std::string& name, const JSON& options); + + int recycleCreep(const Creep& target); + + int renewCreep(const Creep& target); +}; + +class StructureSpawn::Spawning : public Object +{ +public: + explicit Spawning(JS::Value value); + + std::vector directions() const; + + std::string name() const; + + int needTime() const; + + int remainingTime() const; + + StructureSpawn spawn(); + + int cancel(); + + int setDirections(std::vector directions); }; } // namespace Screeps -#endif // SPAWN_HPP +#endif // SCREEPS_STRUCTURE_SPAWN_HPP diff --git a/src/Creep.cpp b/src/Creep.cpp index 5ca5100..d568e56 100644 --- a/src/Creep.cpp +++ b/src/Creep.cpp @@ -7,6 +7,8 @@ #include "Store.hpp" #include "StructureController.hpp" +#include + namespace Screeps { Creep::Creep(JS::Value creep) : RoomObject(std::move(creep)) @@ -93,7 +95,7 @@ int Creep::ticksToLive() const return value()["ticksToLive"].as(); } -int Creep::attack(const Object& target) +int Creep::attack(const RoomObject& target) { return value().call("attack", target.value()); } @@ -190,6 +192,11 @@ int Creep::notifyWhenAttacked(bool enabled) return value().call("notifyWhenAttacked", enabled); } +int Creep::pickup(const Resource& target) +{ + return value().call("pickup", target.value()); +} + int Creep::pull(const Creep& target) { return value().call("pull", target.value()); diff --git a/src/Deposit.cpp b/src/Deposit.cpp new file mode 100644 index 0000000..5f71555 --- /dev/null +++ b/src/Deposit.cpp @@ -0,0 +1,34 @@ +#include "Deposit.hpp" + +namespace Screeps { + +Deposit::Deposit(JS::Value value) : RoomObject(std::move(value)) +{ +} + +int Deposit::cooldown() const +{ + return value()["cooldown"].as(); +} + +std::string Deposit::depositType() const +{ + return value()["depositType"].as(); +} + +std::string Deposit::id() const +{ + return value()["id"].as(); +} + +int Deposit::lastCooldown() const +{ + return value()["lastCooldown"].as(); +} + +int Deposit::ticksToDecay() const +{ + return value()["ticksToDecay"].as(); +} + +} // namespace Screeps diff --git a/src/Flag.cpp b/src/Flag.cpp new file mode 100644 index 0000000..fa03790 --- /dev/null +++ b/src/Flag.cpp @@ -0,0 +1,65 @@ +#include "Flag.hpp" + +#include "JSON.hpp" +#include "RoomPosition.hpp" + +namespace Screeps { + +Flag::Flag(JS::Value value) : RoomObject(std::move(value)) +{ +} + +int Flag::color() const +{ + return value()["color"].as(); +} + +JSON Flag::memory() const +{ + return JS::toJSON(value()["memory"]); +} + +void Flag::setMemory(const JSON& memory) +{ + value().set("memory", JS::fromJSON(memory)); +} + +std::string Flag::name() const +{ + return value()["name"].as(); +} + +int Flag::secondaryColor() const +{ + return value()["secondaryColor"].as(); +} + +void Flag::remove() +{ + value().call("remove"); +} + +int Flag::setColor(int color, std::optional secondaryColor) +{ + if (secondaryColor) + return value().call("setColor", color, *secondaryColor); + else + return value().call("setColor", color); +} + +int Flag::setPosition(int x, int y) +{ + return value().call("setPosition", x, y); +} + +int Flag::setPosition(const RoomPosition& pos) +{ + return value().call("setPosition", pos.value()); +} + +int Flag::setPosition(const RoomObject& pos) +{ + return value().call("setPosition", pos.value()); +} + +} // namespace Screeps diff --git a/src/JS.cpp b/src/JS.cpp index c73f3a6..93b161d 100644 --- a/src/JS.cpp +++ b/src/JS.cpp @@ -2,10 +2,6 @@ #include "JSON.hpp" -#include - -#include - namespace JS { Value getGlobal(char const* name) @@ -28,35 +24,6 @@ bool isInstanceOf(const Value& value, const char* name) return value.instanceof (JS::Value::global(name)); } -std::vector jsArrayToVector(const Value& array) -{ - std::vector result; - - int size = array["length"].as(); - result.reserve(size); - - for (int i = 0; i < size; ++i) - result.emplace_back(array[i]); - - return result; -} - -std::map jsObjectToMap(const Value& object) -{ - std::map result; - - Value keys = gObject.call("keys", object); - int size = keys["length"].as(); - - for (int i = 0; i < size; ++i) - { - auto key = keys[i].as(); - result.emplace_hint(result.end(), key, object[key]); - } - - return result; -} - Value fromJSONArray(const JSON& array) { Value value = Value::array(); diff --git a/src/Resource.cpp b/src/Resource.cpp new file mode 100644 index 0000000..29dcea0 --- /dev/null +++ b/src/Resource.cpp @@ -0,0 +1,24 @@ +#include "Resource.hpp" + +namespace Screeps { + +Resource::Resource(JS::Value value) : RoomObject(std::move(value)) +{ +} + +int Resource::amount() const +{ + return value()["amount"].as(); +} + +std::string Resource::id() const +{ + return value()["id"].as(); +} + +std::string Resource::resourceType() const +{ + return value()["resourceType"].as(); +} + +} // namespace Screeps diff --git a/src/RoomObject.cpp b/src/RoomObject.cpp index a82a624..d58d64a 100644 --- a/src/RoomObject.cpp +++ b/src/RoomObject.cpp @@ -2,7 +2,10 @@ #include "ConstructionSite.hpp" #include "Creep.hpp" +#include "Deposit.hpp" #include "Effect.hpp" +#include "Flag.hpp" +#include "Resource.hpp" #include "Room.hpp" #include "RoomPosition.hpp" #include "Ruin.hpp" @@ -10,6 +13,8 @@ #include "StructureContainer.hpp" #include "StructureController.hpp" #include "StructureExtension.hpp" +#include "StructureExtractor.hpp" +#include "StructureLink.hpp" #include "StructureRampart.hpp" #include "StructureRoad.hpp" #include "StructureSpawn.hpp" @@ -76,6 +81,8 @@ std::unique_ptr createRoomObject(JS::Value object) return std::make_unique(std::move(object)); else if (is("StructureTower")) return std::make_unique(std::move(object)); + else if (is("StructureLink")) + return std::make_unique(std::move(object)); else if (is("StructureSpawn")) return std::make_unique(std::move(object)); else if (is("Ruin")) @@ -84,6 +91,14 @@ std::unique_ptr createRoomObject(JS::Value object) return std::make_unique(std::move(object)); else if (is("StructureStorage")) return std::make_unique(std::move(object)); + else if (is("StructureExtractor")) + return std::make_unique(std::move(object)); + else if (is("Deposit")) + return std::make_unique(std::move(object)); + else if (is("Flag")) + return std::make_unique(std::move(object)); + else if (is("Resource")) + return std::make_unique(std::move(object)); else return nullptr; } diff --git a/src/StructureController.cpp b/src/StructureController.cpp index 9187968..d9aefea 100644 --- a/src/StructureController.cpp +++ b/src/StructureController.cpp @@ -6,4 +6,95 @@ StructureController::StructureController(JS::Value value) : OwnedStructure(std:: { } +bool StructureController::isPowerEnabled() const +{ + return value()["isPowerEnabled"].as(); +} + +int StructureController::level() const +{ + return value()["level"].as(); +} + +int StructureController::progress() const +{ + return value()["progress"].as(); +} + +int StructureController::progressTotal() const +{ + return value()["progressTotal"].as(); +} + +std::optional StructureController::reservation() const +{ + auto obj = value()["reservation"]; + if (obj.isUndefined()) + return std::nullopt; + else + { + Reservation result; + result.username = obj["username"].as(); + result.ticksToEnd = obj["ticksToEnd"].as(); + return result; + } +} + +std::optional StructureController::safeMode() const +{ + auto result = value()["safeMode"]; + if (result.isUndefined()) + return std::nullopt; + return result.as(); +} + +int StructureController::safeModeAvailable() const +{ + return value()["safeModeAvailable"].as(); +} + +std::optional StructureController::safeModeCooldown() const +{ + auto result = value()["safeModeCooldown"]; + if (result.isUndefined()) + return std::nullopt; + return result.as(); +} + +std::optional StructureController::sign() const +{ + auto obj = value()["sign"]; + if (obj.isUndefined()) + return std::nullopt; + else + { + Sign result; + result.username = obj["username"].as(); + result.text = obj["text"].as(); + result.time = obj["time"].as(); + result.datetime = obj["datetime"].call("valueOf"); + return result; + } +} + +int StructureController::ticksToDowngrade() const +{ + return value()["ticksToDowngrade"].as(); +} + +int StructureController::upgradeBlocked() const +{ + return value()["upgradeBlocked"].as(); +} + +int StructureController::activateSafeMode() +{ + return value().call("activateSafeMode"); +} + +int StructureController::unclaim() +{ + return value().call("unclaim"); +} + } // namespace Screeps diff --git a/src/StructureExtension.cpp b/src/StructureExtension.cpp index a84d435..e6b643c 100644 --- a/src/StructureExtension.cpp +++ b/src/StructureExtension.cpp @@ -1,9 +1,16 @@ #include "StructureExtension.hpp" +#include "Store.hpp" + namespace Screeps { StructureExtension::StructureExtension(JS::Value value) : Structure(std::move(value)) { } +Store StructureExtension::store() const +{ + return Store(value()["store"]); +} + } // namespace Screeps diff --git a/src/StructureExtractor.cpp b/src/StructureExtractor.cpp new file mode 100644 index 0000000..a871ddc --- /dev/null +++ b/src/StructureExtractor.cpp @@ -0,0 +1,14 @@ +#include "StructureExtractor.hpp" + +namespace Screeps { + +StructureExtractor::StructureExtractor(JS::Value value) : OwnedStructure(std::move(value)) +{ +} + +int StructureExtractor::cooldown() const +{ + return value()["cooldown"].as(); +} + +} // namespace Screeps diff --git a/src/StructureLink.cpp b/src/StructureLink.cpp new file mode 100644 index 0000000..422c018 --- /dev/null +++ b/src/StructureLink.cpp @@ -0,0 +1,29 @@ +#include "StructureLink.hpp" + +#include "Store.hpp" + +namespace Screeps { + +StructureLink::StructureLink(JS::Value value) : OwnedStructure(std::move(value)) +{ +} + +int StructureLink::cooldown() const +{ + return value()["cooldown"].as(); +} + +Store StructureLink::store() const +{ + return Store(value()["store"]); +} + +int StructureLink::transferEnergy(const StructureLink& target, std::optional amount) +{ + if (amount) + return value().call("transferEnergy", target.value(), *amount); + else + return value().call("transferEnergy", target.value()); +} + +} // namespace Screeps diff --git a/src/StructureRampart.cpp b/src/StructureRampart.cpp index bdc8bcb..1d1f40c 100644 --- a/src/StructureRampart.cpp +++ b/src/StructureRampart.cpp @@ -6,4 +6,19 @@ StructureRampart::StructureRampart(JS::Value value) : Structure(std::move(value) { } +bool StructureRampart::isPublic() const +{ + return value()["isPublic"].as(); +} + +int StructureRampart::ticksToDecay() const +{ + return value()["ticksToDecay"].as(); +} + +int StructureRampart::setPublic(bool isPublic) +{ + return value().call("setPublic", isPublic); +} + } // namespace Screeps diff --git a/src/StructureRoad.cpp b/src/StructureRoad.cpp index 38f7230..26606f3 100644 --- a/src/StructureRoad.cpp +++ b/src/StructureRoad.cpp @@ -6,4 +6,9 @@ StructureRoad::StructureRoad(JS::Value value) : Structure(std::move(value)) { } +int StructureRoad::ticksToDecay() const +{ + return value()["ticksToDecay"].as(); +} + } // namespace Screeps diff --git a/src/StructureSpawn.cpp b/src/StructureSpawn.cpp index 0c3ec79..0d4a3e0 100644 --- a/src/StructureSpawn.cpp +++ b/src/StructureSpawn.cpp @@ -1,5 +1,6 @@ #include "StructureSpawn.hpp" +#include "Creep.hpp" #include "JSON.hpp" #include "Store.hpp" @@ -9,6 +10,25 @@ StructureSpawn::StructureSpawn(JS::Value obj) : OwnedStructure(std::move(obj)) { } +JSON StructureSpawn::memory() const +{ + return JS::toJSON(value()["memory"]); +} + +void StructureSpawn::setMemory(const JSON& memory) +{ + value().set("memory", JS::fromJSON(memory)); +} + +std::optional StructureSpawn::spawning() const +{ + auto obj = value()["spawning"]; + if (obj.isUndefined()) + return std::nullopt; + else + return Spawning(std::move(obj)); +} + Store StructureSpawn::store() const { return Store(value()["store"]); @@ -35,4 +55,53 @@ int StructureSpawn::spawnCreep(const std::vector& body, return value().call("spawnCreep", JS::vectorToJSArray(body), name, JS::fromJSON(options)); } +int StructureSpawn::recycleCreep(const Creep& target) +{ + return value().call("recycleCreep", target.value()); +} + +int StructureSpawn::renewCreep(const Creep& target) +{ + return value().call("renewCreep", target.value()); +} + +StructureSpawn::Spawning::Spawning(JS::Value value) : Object(std::move(value)) +{ +} + +std::vector StructureSpawn::Spawning::directions() const +{ + return JS::jsArrayToVector(value()["directions"]); +} + +std::string StructureSpawn::Spawning::name() const +{ + return value()["name"].as(); +} + +int StructureSpawn::Spawning::needTime() const +{ + return value()["needTime"].as(); +} + +int StructureSpawn::Spawning::remainingTime() const +{ + return value()["remainingTime"].as(); +} + +StructureSpawn StructureSpawn::Spawning::spawn() +{ + return StructureSpawn(value()["spawn"]); +} + +int StructureSpawn::Spawning::cancel() +{ + return value().call("cancel"); +} + +int StructureSpawn::Spawning::setDirections(std::vector directions) +{ + return value().call("setDirections", JS::vectorToJSArray(directions)); +} + } // namespace Screeps