#include "Room.hpp" #include "Creep.hpp" #include "JSON.hpp" #include "StructureController.hpp" #include "StructureStorage.hpp" #include namespace Screeps { Room::Room(JS::Value value) : Object(std::move(value)) { } std::optional Room::controller() const { if (value()["controller"].isUndefined()) return std::nullopt; return StructureController(value()["controller"]); } int Room::energyAvailable() const { return value()["energyAvailable"].as(); } int Room::energyCapacityAvailable() const { return value()["energyCapacityAvailable"].as(); } JSON Room::memory() const { return JS::toJSON(value()["memory"]); } void Room::setMemory(const JSON& memory) { value().set("memory", JS::fromJSON(memory)); } std::string Room::name() const { return value()["name"].as(); } std::optional Room::storage() const { if (value()["storage"].isUndefined()) return std::nullopt; return StructureStorage(value()["storage"]); } std::vector> Room::find(int type, std::function predicate) const { std::vector> list; std::vector objects = JS::jsArrayToVector(value().call("find", type)); if (predicate) objects.erase(std::remove_if(objects.begin(), objects.end(), [predicate](const JS::Value& value) { return !predicate(value); }), objects.end()); for (auto& object : objects) { if (auto roomObject = createRoomObject(object)) list.push_back(std::move(roomObject)); else JS::console.log(std::string("failed to create RoomObject from "), JS::getGlobal("JSON").call("stringify", object)); } return list; } int Room::findExitTo(const std::string& room) { return value().call("findExitTo", room); } } // namespace Screeps