From fe039cbdcf84b79ed87dc364460d23e24341eda7 Mon Sep 17 00:00:00 2001 From: Douwe Ravers Date: Thu, 18 Jun 2026 01:12:09 +0200 Subject: [PATCH] Building Room based placement system. --- douwco_hivemind/include/Engine.hpp | 37 +++++--- douwco_hivemind/include/Room.hpp | 31 ++++++ douwco_hivemind/src/Engine.cpp | 146 +++++++++++++++-------------- douwco_hivemind/src/Room.cpp | 40 ++++++++ 4 files changed, 167 insertions(+), 87 deletions(-) create mode 100644 douwco_hivemind/include/Room.hpp create mode 100644 douwco_hivemind/src/Room.cpp diff --git a/douwco_hivemind/include/Engine.hpp b/douwco_hivemind/include/Engine.hpp index a59c437..625ffe9 100644 --- a/douwco_hivemind/include/Engine.hpp +++ b/douwco_hivemind/include/Engine.hpp @@ -4,25 +4,32 @@ #include #include "Creeps/CreepBase.hpp" +#include "Room.hpp" #include "Structures/StructureBase.hpp" -namespace DouwcoHivemind -{ - class Engine - { - private: - std::vector> creeps; - std::vector> structures; +namespace DouwcoHivemind { +class Engine { +private: + std::vector> rooms; + std::vector> creeps; + std::vector> structures; - public: - Engine(); - void loop(); +public: + // Readout data from screeps API into C++ vectors + Engine(); + // Loop over all rooms, screeps and structures + void loop(); - private: - void ReadOutCreeps(); - void ReadOutStructures(); - void clearDeadCreepMemory(); - }; +private: + // Readout player owned rooms + void ReadOutRooms(); + // Readout player owned screeps and assign their roles + void ReadOutCreeps(); + // Readout player owned buildings based on their structure type + void ReadOutStructures(); + // Clear dead screep memory + void clearDeadCreepMemory(); +}; } #endif // DOUWCO_HIVEMIND_ENGINE_HPP \ No newline at end of file diff --git a/douwco_hivemind/include/Room.hpp b/douwco_hivemind/include/Room.hpp new file mode 100644 index 0000000..d83661a --- /dev/null +++ b/douwco_hivemind/include/Room.hpp @@ -0,0 +1,31 @@ +#ifndef DOUWCO_HIVEMIND_ROOM_HPP +#define DOUWCO_HIVEMIND_ROOM_HPP + +#include + +namespace DouwcoHivemind { +class Room { + +protected: + enum ProgressState { + PLANNED, + IN_PROGRESS, + FINISHED + }; + + Screeps::Room room; + JSON memory; + ProgressState _sourceContainerState; + +public: + Room(Screeps::Room rm); + ~Room(); + + void loop(); + +protected: + bool assignConstructionSites(); +}; +} // namespace DouwcoHivemind + +#endif // DOUWCO_HIVEMIND_ROOM_HPP \ No newline at end of file diff --git a/douwco_hivemind/src/Engine.cpp b/douwco_hivemind/src/Engine.cpp index bec200e..4da942e 100644 --- a/douwco_hivemind/src/Engine.cpp +++ b/douwco_hivemind/src/Engine.cpp @@ -3,86 +3,88 @@ #include "Engine.hpp" -#include "Creeps/Supplier.hpp" -#include "Creeps/Upgrader.hpp" #include "Creeps/Builder.hpp" #include "Creeps/Maintainer.hpp" +#include "Creeps/Supplier.hpp" +#include "Creeps/Upgrader.hpp" #include "Structures/Spawn.hpp" -DouwcoHivemind::Engine::Engine() -{ - ReadOutCreeps(); - ReadOutStructures(); +DouwcoHivemind::Engine::Engine() { + ReadOutRooms(); + ReadOutCreeps(); + ReadOutStructures(); } -void DouwcoHivemind::Engine::loop() -{ - JS::console.log(std::string("Iterating over creeps")); - for (auto &creep : creeps) - creep->loop(); +void DouwcoHivemind::Engine::loop() { + JS::console.log(std::string("Iterating over rooms")); + for (auto &room : rooms) + room->loop(); - JS::console.log(std::string("Iterating over structures")); - for (auto &structure : structures) - structure->loop(); + JS::console.log(std::string("Iterating over creeps")); + for (auto &creep : creeps) + creep->loop(); - if (Screeps::Game.time() % 1000 == 0) - { - clearDeadCreepMemory(); + JS::console.log(std::string("Iterating over structures")); + for (auto &structure : structures) + structure->loop(); + + if (Screeps::Game.time() % 100 == 0) { + clearDeadCreepMemory(); + } +} + +void DouwcoHivemind::Engine::ReadOutRooms() { + JS::console.log(std::string("Reading out owned rooms")); + auto src_rooms = Screeps::Game.rooms(); + for (auto &room : src_rooms) { + rooms.push_back(std::make_unique(room.second)); + } +} + +void DouwcoHivemind::Engine::ReadOutCreeps() { + JS::console.log(std::string("Reading out creeps")); + auto src_creeps = Screeps::Game.creeps(); + for (auto &creep : src_creeps) { + CreepRole role = creep.second.memory()["role"]; + if (role == CreepRole::SUPPLIER) + creeps.push_back(std::make_unique(creep.second)); + else if (role == CreepRole::UPGRADER) + creeps.push_back(std::make_unique(creep.second)); + else if (role == CreepRole::BUILDER) + creeps.push_back(std::make_unique(creep.second)); + else if (role == CreepRole::MAINTAINER) + creeps.push_back(std::make_unique(creep.second)); + } +} + +void DouwcoHivemind::Engine::ReadOutStructures() { + JS::console.log(std::string("Reading out structures")); + auto spawns = Screeps::Game.spawns(); + for (auto &spawn : spawns) { + structures.push_back(std::make_unique(spawn.second)); + } +} + +void DouwcoHivemind::Engine::clearDeadCreepMemory() { + auto creepMemory = Screeps::Memory["creeps"]; + auto creepsMap = Screeps::Game.creeps(); + int iterator = 0; + for (auto [name, creep] : creepMemory.items()) { + // avoid cpu overload + iterator++; + if (iterator == 100) + break; + + bool containsname = false; + for (auto creepObject : creepsMap) { + if (creepObject.first == name) { + containsname = true; + break; + } } -} - -void DouwcoHivemind::Engine::ReadOutCreeps() -{ - JS::console.log(std::string("Reading out creeps")); - auto src_creeps = Screeps::Game.creeps(); - for (auto &creep : src_creeps) - { - CreepRole role = creep.second.memory()["role"]; - if (role == CreepRole::SUPPLIER) - creeps.push_back(std::make_unique(creep.second)); - else if (role == CreepRole::UPGRADER) - creeps.push_back(std::make_unique(creep.second)); - else if (role == CreepRole::BUILDER) - creeps.push_back(std::make_unique(creep.second)); - else if (role == CreepRole::MAINTAINER) - creeps.push_back(std::make_unique(creep.second)); - } -} - -void DouwcoHivemind::Engine::ReadOutStructures() -{ - JS::console.log(std::string("Reading out structures")); - auto spawns = Screeps::Game.spawns(); - for (auto &spawn : spawns) - { - structures.push_back(std::make_unique(spawn.second)); - } -} - -void DouwcoHivemind::Engine::clearDeadCreepMemory() -{ - auto creepMemory = Screeps::Memory["creeps"]; - auto creepsMap = Screeps::Game.creeps(); - int iterator = 0; - for (auto [name, creep] : creepMemory.items()) - { - // avoid cpu overload - iterator++; - if (iterator == 100) - break; - - bool containsname = false; - for (auto creepObject : creepsMap) - { - if (creepObject.first == name) - { - containsname = true; - break; - } - } - if (!containsname) - creepMemory.erase(name); - } - Screeps::Memory.set("creeps", creepMemory); + if (!containsname) + creepMemory.erase(name); + } + Screeps::Memory.set("creeps", creepMemory); } diff --git a/douwco_hivemind/src/Room.cpp b/douwco_hivemind/src/Room.cpp new file mode 100644 index 0000000..f7ad21c --- /dev/null +++ b/douwco_hivemind/src/Room.cpp @@ -0,0 +1,40 @@ +#include "Room.hpp" + +#include "Screeps/Constants.hpp" +#include "Screeps/Room.hpp" +#include "Screeps/RoomObject.hpp" +#include "Screeps/RoomPosition.hpp" + +DouwcoHivemind::Room::Room(Screeps::Room rm) : room(rm), memory(rm.memory()) { + _sourceContainerState = + memory.contains("_sourceContainerState") + ? static_cast(memory["_sourceContainerState"]) + : ProgressState::PLANNED; +} + +DouwcoHivemind::Room::~Room() { + memory["_sourceContainerState"] = _sourceContainerState; +} + +void DouwcoHivemind::Room::loop() { + if (assignConstructionSites()) + return; +} + +bool DouwcoHivemind::Room::assignConstructionSites() { + if (_sourceContainerState == PLANNED) { + auto sources = room.find(Screeps::FIND_SOURCES); + for (auto &source : sources) { + int x = source->pos().x(); + int y = source->pos().y(); + for (int dx = -1; dx < 2; dx++) { + for (int dy = -1; dy < 2; dy++) { + if(dx == 0 && dy == 0) continue; + auto pos = room.getPositionAt(x + dx, y + dy); + } + } + } + } else if (_sourceContainerState == IN_PROGRESS) { + } + return false; +} \ No newline at end of file