From 2332978ab7f26f623ae8f3c1a3ecfcd91aa37559 Mon Sep 17 00:00:00 2001 From: Douwe Ravers Date: Wed, 17 Jun 2026 01:04:10 +0200 Subject: [PATCH] Added small movments when waiting on new target preventing blocking a source. --- douwco_hivemind/src/Creeps/Builder.cpp | 5 +++-- douwco_hivemind/src/Creeps/CreepBase.cpp | 8 ++++++-- douwco_hivemind/src/Creeps/Maintainer.cpp | 10 +++++----- douwco_hivemind/src/Creeps/Supplier.cpp | 4 ++-- douwco_hivemind/src/Structures/Spawn.cpp | 3 +-- 5 files changed, 17 insertions(+), 13 deletions(-) diff --git a/douwco_hivemind/src/Creeps/Builder.cpp b/douwco_hivemind/src/Creeps/Builder.cpp index 1bc8f4c..5e806d1 100644 --- a/douwco_hivemind/src/Creeps/Builder.cpp +++ b/douwco_hivemind/src/Creeps/Builder.cpp @@ -19,12 +19,12 @@ #include #include "Creeps/Builder.hpp" +#include "Creeps/CreepBase.hpp" void DouwcoHivemind::Builder::depositEnergy() { auto constructionSite = getConstructionSiteTarget(); - if (!constructionSite) - return; + if (!constructionSite) return; if (isNearTo(constructionSite->pos(), 1)) { @@ -42,6 +42,7 @@ std::unique_ptr DouwcoHivemind::Builder::getConstruct if (!roomObj) { searchConstructionSite(); + if(target_id.empty()) creep.move(rand()%5); return nullptr; } diff --git a/douwco_hivemind/src/Creeps/CreepBase.cpp b/douwco_hivemind/src/Creeps/CreepBase.cpp index eaab95f..95af63e 100644 --- a/douwco_hivemind/src/Creeps/CreepBase.cpp +++ b/douwco_hivemind/src/Creeps/CreepBase.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "Creeps/CreepBase.hpp" @@ -19,6 +20,7 @@ DouwcoHivemind::CreepBase::CreepBase(Screeps::Creep crp) : creep(crp), DouwcoHivemind::CreepBase::~CreepBase() { + memory["role"] = role; memory["target_id"] = target_id; creep.setMemory(memory); } @@ -26,7 +28,7 @@ DouwcoHivemind::CreepBase::~CreepBase() void DouwcoHivemind::CreepBase::moveToTarget(int dist) { // Is move required? - auto target = getRoomObjectTarget(); + auto target = getRoomObjectTarget(); if (isNearTo(target->pos(), dist)) { memory.erase("path"); @@ -127,11 +129,13 @@ void DouwcoHivemind::CreepBase::moveToTarget(int dist) std::unique_ptr DouwcoHivemind::CreepBase::getRoomObjectTarget() { + // Check if any target is present + if(target_id.empty()) return nullptr; // Check if game can find target auto roomObj = Screeps::Game.getObjectById(target_id); if (!roomObj) { - JS::console.log(creep.name() + ": Game can\'t find target id"); + JS::console.log(creep.name() + ": Game can\'t find target id: " + target_id); return nullptr; } return std::move(roomObj); diff --git a/douwco_hivemind/src/Creeps/Maintainer.cpp b/douwco_hivemind/src/Creeps/Maintainer.cpp index 46cea3b..059d377 100644 --- a/douwco_hivemind/src/Creeps/Maintainer.cpp +++ b/douwco_hivemind/src/Creeps/Maintainer.cpp @@ -16,13 +16,12 @@ #include #include "Creeps/Maintainer.hpp" +#include "Creeps/CreepBase.hpp" void DouwcoHivemind::Maintainer::depositEnergy() { auto structure = getDamagedStructureTarget(); - if (!structure) - return; - + if (!structure) return; if (isNearTo(structure->pos(), 1)) { int resp = creep.repair(*structure); @@ -39,6 +38,7 @@ std::unique_ptr DouwcoHivemind::Maintainer::getDamagedStruct if (!roomObj) { searchDamagedStructure(); + if(target_id.empty()) creep.move(rand()%5); return nullptr; } @@ -82,8 +82,8 @@ void DouwcoHivemind::Maintainer::searchDamagedStructure() } } - if (selectedStructure) + if (selectedStructure && selectedStructure->hits() != selectedStructure->hitsMax()) target_id = selectedStructure->id(); - else + else target_id.clear(); } diff --git a/douwco_hivemind/src/Creeps/Supplier.cpp b/douwco_hivemind/src/Creeps/Supplier.cpp index 5742299..ed4c598 100644 --- a/douwco_hivemind/src/Creeps/Supplier.cpp +++ b/douwco_hivemind/src/Creeps/Supplier.cpp @@ -20,8 +20,7 @@ void DouwcoHivemind::Supplier::depositEnergy() { auto structure = getEnergyStructureTarget(); - if (!structure) - return; + if (!structure) return; if (isNearTo(structure->pos(), 1)) { @@ -39,6 +38,7 @@ std::unique_ptr DouwcoHivemind::Supplier::getEnergyStructure if (!roomObj) { searchEnergyStructure(); + if(target_id.empty()) creep.move(rand()%5); return nullptr; } diff --git a/douwco_hivemind/src/Structures/Spawn.cpp b/douwco_hivemind/src/Structures/Spawn.cpp index 6317129..d4d78d6 100644 --- a/douwco_hivemind/src/Structures/Spawn.cpp +++ b/douwco_hivemind/src/Structures/Spawn.cpp @@ -17,7 +17,6 @@ void DouwcoHivemind::Spawn::loop() int required_suppliers = 1; int required_maintainers = spawn.room().find(Screeps::FIND_MY_STRUCTURES).size() <= 2 ? 0 : 1;; int required_builders = spawn.room().find(Screeps::FIND_MY_CONSTRUCTION_SITES).size() == 0 ? 0 : 1; - for (auto &creep : Screeps::Game.creeps()) { CreepRole role = creep.second.memory()["role"]; @@ -32,7 +31,7 @@ void DouwcoHivemind::Spawn::loop() required_builders--; } - if (energyAvailable < energyCapacityAvailable && required_suppliers < 2) + if (energyAvailable < energyCapacityAvailable && 3 < Screeps::Game.creeps().size()) return; std::string name; JSON opts;