From 1e9b14f21c78a3861cc4b35046ea9ea125c17f6d Mon Sep 17 00:00:00 2001 From: Douwe Ravers Date: Wed, 1 Jul 2026 00:24:25 +0200 Subject: [PATCH] Improved miners and workers to work together. --- compile_commands.json | 2 +- douwco_hivemind/include/Creeps/Worker.hpp | 5 + douwco_hivemind/src/Creeps/Miner.cpp | 6 +- douwco_hivemind/src/Creeps/Worker.cpp | 115 +++++++++++++++------- douwco_hivemind/src/Entity/Room.cpp | 9 +- douwco_hivemind/src/Structures/Spawn.cpp | 3 +- 6 files changed, 98 insertions(+), 42 deletions(-) diff --git a/compile_commands.json b/compile_commands.json index 3b72d93..4ef3144 120000 --- a/compile_commands.json +++ b/compile_commands.json @@ -1 +1 @@ -/home/douwe/Projects/screeps/build/compile_commands.json \ No newline at end of file +/mnt/douwe/hdd/Projects/Screeps/build/compile_commands.json \ No newline at end of file diff --git a/douwco_hivemind/include/Creeps/Worker.hpp b/douwco_hivemind/include/Creeps/Worker.hpp index 277b9c0..47b1b9c 100644 --- a/douwco_hivemind/include/Creeps/Worker.hpp +++ b/douwco_hivemind/include/Creeps/Worker.hpp @@ -2,6 +2,7 @@ #define DOUWCO_HIVEMIND_HARVESTER_HPP #include "Creeps/CreepBase.hpp" +#include "Screeps/StructureContainer.hpp" namespace DouwcoHivemind { @@ -9,6 +10,7 @@ namespace DouwcoHivemind { private: bool harvesting; + bool foundContainer; public: Worker(Screeps::Creep crp); @@ -22,8 +24,11 @@ namespace DouwcoHivemind private: void getEnergy(); void harvestSource(); + void collectEnergyFromContainers(); std::unique_ptr getSourceTarget(); + std::unique_ptr getContainerTarget(); void searchSource(); + void searchContainer(); }; } diff --git a/douwco_hivemind/src/Creeps/Miner.cpp b/douwco_hivemind/src/Creeps/Miner.cpp index 12e7e37..0722278 100644 --- a/douwco_hivemind/src/Creeps/Miner.cpp +++ b/douwco_hivemind/src/Creeps/Miner.cpp @@ -30,10 +30,8 @@ void DouwcoHivemind::Miner::loop() { arrivedAtContainer = true; int x = creep.pos().x(); int y = creep.pos().y(); - auto roomObjs = creep.room().lookForAtArea(Screeps::LOOK_SOURCES, y - 1, - x - 1, y + 1, x + 1); - return; // fault is here somewhere - auto source = static_cast(roomObjs[0].release()); + auto roomObjs = creep.pos().findClosestByRange(Screeps::FIND_SOURCES, 2); + auto source = static_cast(roomObjs.release()); target_id = source->id(); } else { moveToTarget(0); diff --git a/douwco_hivemind/src/Creeps/Worker.cpp b/douwco_hivemind/src/Creeps/Worker.cpp index e9003e0..09c4431 100644 --- a/douwco_hivemind/src/Creeps/Worker.cpp +++ b/douwco_hivemind/src/Creeps/Worker.cpp @@ -24,9 +24,15 @@ DouwcoHivemind::Worker::Worker(Screeps::Creep crp) : CreepBase(crp) { harvesting = memory.contains("harvesting") ? static_cast(memory["harvesting"]) : false; + foundContainer = memory.contains("foundContainer") + ? static_cast(memory["foundContainer"]) + : false; } -DouwcoHivemind::Worker::~Worker() { memory["harvesting"] = harvesting; } +DouwcoHivemind::Worker::~Worker() { + memory["harvesting"] = harvesting; + memory["foundContainer"] = foundContainer; +} void DouwcoHivemind::Worker::loop() { if (harvesting) { @@ -45,21 +51,20 @@ void DouwcoHivemind::Worker::loop() { } void DouwcoHivemind::Worker::getEnergy() { - for (auto containersJSON : creep.room().memory()["sourceContainers"]) { - auto containerID = static_cast(containersJSON); - target_id = containerID; - auto containerRoomObject = getRoomObjectTarget(); - auto container = static_cast( - containerRoomObject.release()); - if (0 < container->store().getUsedCapacity(Screeps::RESOURCE_ENERGY)) { - if (isNearTo(container->pos(), 1)) { - int resp = creep.withdraw(*container, Screeps::RESOURCE_ENERGY); - } else { - moveToTarget(); - } - } + if (foundContainer || Screeps::Game.time()%100==0) collectEnergyFromContainers(); + else harvestSource(); +} + +void DouwcoHivemind::Worker::collectEnergyFromContainers() { + auto container = getContainerTarget(); + if (!container) + return; + + if (isNearTo(container->pos(), 1)) { + int resp = creep.withdraw(*container, Screeps::RESOURCE_ENERGY); + } else { + moveToTarget(); } - harvestSource(); } void DouwcoHivemind::Worker::harvestSource() { @@ -74,6 +79,33 @@ void DouwcoHivemind::Worker::harvestSource() { } } +std::unique_ptr +DouwcoHivemind::Worker::getContainerTarget() { + auto roomObj = getRoomObjectTarget(); + if (!roomObj) { + searchContainer(); + return nullptr; + } + + // Check if found roomobject is an actual source + auto container = std::unique_ptr( + dynamic_cast(roomObj.release())); + if (!container) { + // EM_ASM({console.log($0 + ': Can\'t cast target to Source')}, + // creep.name().c_str()); + searchContainer(); + return nullptr; + } + + // Check if the source still has energy to harvest + if (container->store().getUsedCapacity(Screeps::RESOURCE_ENERGY) == 0) { + searchContainer(); + return nullptr; + } + + return std::move(container); +} + std::unique_ptr DouwcoHivemind::Worker::getSourceTarget() { auto roomObj = getRoomObjectTarget(); if (!roomObj) { @@ -102,29 +134,42 @@ std::unique_ptr DouwcoHivemind::Worker::getSourceTarget() { void DouwcoHivemind::Worker::searchSource() { target_id.clear(); + auto sourceObject = creep.pos().findClosestByRange(Screeps::FIND_SOURCES); + if (!sourceObject) + return; + auto source = dynamic_cast(sourceObject.release()); + target_id = source->id(); +} - auto sources = creep.room().find(Screeps::FIND_SOURCES_ACTIVE); - if (sources.size() == 0) +void DouwcoHivemind::Worker::searchContainer() { + target_id.clear(); + + auto structureObjects = creep.room().find(Screeps::FIND_STRUCTURES); + if (structureObjects.size() == 0) return; - int x = creep.pos().x(); - int y = creep.pos().y(); - - int closestDistance = INT16_MAX; - Screeps::Source *selectedSource; - for (auto &sourceObject : sources) { - auto source = dynamic_cast(sourceObject.get()); - if (source->energy() == 0) + int fullestContents = 0; + Screeps::StructureContainer *selectedContainer; + for (auto &structureObject : structureObjects) { + auto structure = + dynamic_cast(structureObject.release()); + if (structure->structureType() != Screeps::STRUCTURE_CONTAINER) continue; - int dx = source->pos().x() - x; - int dy = source->pos().y() - y; - int distance = dx * dx + dy * dy; - if (distance < closestDistance) { - closestDistance = distance; - selectedSource = source; + auto container = dynamic_cast(structure); + int availableEnergy = + container->store().getUsedCapacity(Screeps::RESOURCE_ENERGY).value(); + if (availableEnergy == 0) + continue; + if (fullestContents < availableEnergy) { + fullestContents = availableEnergy; + selectedContainer = container; } } - if (!selectedSource) - return; - target_id = selectedSource->id(); -} + + if (!selectedContainer) { + foundContainer = false; + } else { + foundContainer = true; + target_id = selectedContainer->id(); + } +} \ No newline at end of file diff --git a/douwco_hivemind/src/Entity/Room.cpp b/douwco_hivemind/src/Entity/Room.cpp index 8fd52c7..167986e 100644 --- a/douwco_hivemind/src/Entity/Room.cpp +++ b/douwco_hivemind/src/Entity/Room.cpp @@ -1,6 +1,7 @@ #include "Entity/Room.hpp" #include "Screeps/Constants.hpp" +#include "Screeps/Game.hpp" #include "Screeps/Room.hpp" #include "Screeps/RoomObject.hpp" #include "Screeps/RoomPosition.hpp" @@ -28,6 +29,7 @@ DouwcoHivemind::Room::~Room() { } void DouwcoHivemind::Room::loop() { + if(Screeps::Game.time()%100!=0)return; if (placeContainers()) return; } @@ -36,7 +38,7 @@ bool DouwcoHivemind::Room::placeContainers() { auto sources = room.find(Screeps::FIND_SOURCES); // No containers planned or build, planning now - if (sourceContainers.size() == 0) { + if (sourceContainers.size() != sources.size()) { int controller_x = room.controller().value().pos().x(); int controller_y = room.controller().value().pos().y(); @@ -84,5 +86,10 @@ bool DouwcoHivemind::Room::placeContainers() { } return true; } + // Check if containers still exist + for(int i = 0; i < sourceContainers.size(); i++){ + auto roomObj = Screeps::Game.getObjectById(sourceContainers[i]); + if(!roomObj) sourceContainers.erase(sourceContainers.begin() + i); + } return false; } \ No newline at end of file diff --git a/douwco_hivemind/src/Structures/Spawn.cpp b/douwco_hivemind/src/Structures/Spawn.cpp index c602990..914821a 100644 --- a/douwco_hivemind/src/Structures/Spawn.cpp +++ b/douwco_hivemind/src/Structures/Spawn.cpp @@ -64,7 +64,8 @@ void DouwcoHivemind::Spawn::loop() { std::vector body; if (opts["memory"]["role"] == CreepRole::MINER) { body.push_back("move"); - for (int i = 0; i < (energyAvailable - 50) / 100 && i < 5; i++) { + body.push_back("work"); + for (int i = 0; i < (energyAvailable - 150) / 100 && i < 5; i++) { body.push_back("work"); } } else {