Improved miners and workers to work together.

This commit is contained in:
2026-07-01 00:24:25 +02:00
parent ceed83358b
commit 1e9b14f21c
6 changed files with 98 additions and 42 deletions

View File

@@ -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<Screeps::Source> getSourceTarget();
std::unique_ptr<Screeps::StructureContainer> getContainerTarget();
void searchSource();
void searchContainer();
};
}

View File

@@ -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<Screeps::Source *>(roomObjs[0].release());
auto roomObjs = creep.pos().findClosestByRange(Screeps::FIND_SOURCES, 2);
auto source = static_cast<Screeps::Source *>(roomObjs.release());
target_id = source->id();
} else {
moveToTarget(0);

View File

@@ -24,9 +24,15 @@ DouwcoHivemind::Worker::Worker(Screeps::Creep crp) : CreepBase(crp) {
harvesting = memory.contains("harvesting")
? static_cast<bool>(memory["harvesting"])
: false;
foundContainer = memory.contains("foundContainer")
? static_cast<bool>(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<std::string>(containersJSON);
target_id = containerID;
auto containerRoomObject = getRoomObjectTarget();
auto container = static_cast<Screeps::StructureContainer *>(
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<Screeps::StructureContainer>
DouwcoHivemind::Worker::getContainerTarget() {
auto roomObj = getRoomObjectTarget();
if (!roomObj) {
searchContainer();
return nullptr;
}
// Check if found roomobject is an actual source
auto container = std::unique_ptr<Screeps::StructureContainer>(
dynamic_cast<Screeps::StructureContainer *>(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<Screeps::Source> DouwcoHivemind::Worker::getSourceTarget() {
auto roomObj = getRoomObjectTarget();
if (!roomObj) {
@@ -102,29 +134,42 @@ std::unique_ptr<Screeps::Source> 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<Screeps::Source *>(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<Screeps::Source *>(sourceObject.get());
if (source->energy() == 0)
int fullestContents = 0;
Screeps::StructureContainer *selectedContainer;
for (auto &structureObject : structureObjects) {
auto structure =
dynamic_cast<Screeps::Structure *>(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<Screeps::StructureContainer *>(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();
}
}

View File

@@ -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;
}

View File

@@ -64,7 +64,8 @@ void DouwcoHivemind::Spawn::loop() {
std::vector<std::string> 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 {