Fixed some bugs in harvester.

This commit is contained in:
douwe
2025-08-22 00:13:32 +02:00
parent 0c8da1d472
commit 0381ea6873
4 changed files with 28 additions and 15 deletions

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@@ -18,7 +18,7 @@
#include "Creeps/Harvester.hpp" #include "Creeps/Harvester.hpp"
bool isNearTo(const Screeps::RoomPosition &pos1, const Screeps::RoomPosition &pos2); bool isNearTo(const Screeps::RoomPosition &pos1, const Screeps::RoomPosition &pos2, int dist);
void DouwcoHivemind::HarvesterRole::loop() void DouwcoHivemind::HarvesterRole::loop()
{ {
@@ -50,7 +50,7 @@ void DouwcoHivemind::HarvesterRole::harvestSource()
auto source = getSourceTarget(); auto source = getSourceTarget();
if (!source) if (!source)
return; return;
if (isNearTo(creep.pos(), source->pos())) if (isNearTo(creep.pos(), source->pos(), 1))
{ {
int resp = creep.harvest(*source); int resp = creep.harvest(*source);
} }
@@ -124,22 +124,28 @@ void DouwcoHivemind::HarvesterRole::depositEnergy()
if (!structure) if (!structure)
return; return;
if (isNearTo(creep.pos(), structure->pos()))
{
int resp;
if (structure->structureType() == Screeps::STRUCTURE_CONTROLLER) if (structure->structureType() == Screeps::STRUCTURE_CONTROLLER)
{
if (isNearTo(creep.pos(), structure->pos(), 3))
{ {
auto controller = dynamic_cast<Screeps::StructureController *>(structure.get()); auto controller = dynamic_cast<Screeps::StructureController *>(structure.get());
if (!controller) if (!controller)
return; return;
resp = creep.upgradeController(*controller); int resp = creep.upgradeController(*controller);
}
else
resp = creep.transfer(*structure, Screeps::RESOURCE_ENERGY);
} }
else else
creep.moveTo(*structure); creep.moveTo(*structure);
} }
else
{
if (isNearTo(creep.pos(), structure->pos(), 1))
{
int resp = creep.transfer(*structure, Screeps::RESOURCE_ENERGY);
}
else
creep.moveTo(*structure);
}
}
std::unique_ptr<Screeps::Structure> DouwcoHivemind::HarvesterRole::getDepositTarget() std::unique_ptr<Screeps::Structure> DouwcoHivemind::HarvesterRole::getDepositTarget()
{ {
@@ -162,7 +168,11 @@ std::unique_ptr<Screeps::Structure> DouwcoHivemind::HarvesterRole::getDepositTar
// Check if the structure can receive energy to harvest // Check if the structure can receive energy to harvest
int energyCapacity; int energyCapacity;
auto structureType = structure->structureType(); auto structureType = structure->structureType();
if (structureType == Screeps::STRUCTURE_SPAWN) if (structureType == Screeps::STRUCTURE_CONTROLLER)
{
energyCapacity = 1;
}
else if (structureType == Screeps::STRUCTURE_SPAWN)
{ {
auto spawn = dynamic_cast<Screeps::StructureSpawn *>(structure.get()); auto spawn = dynamic_cast<Screeps::StructureSpawn *>(structure.get());
energyCapacity = spawn->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value(); energyCapacity = spawn->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
@@ -240,9 +250,12 @@ std::unique_ptr<Screeps::RoomObject> DouwcoHivemind::HarvesterRole::getRoomObjec
return std::move(roomObj); return std::move(roomObj);
} }
bool isNearTo(const Screeps::RoomPosition &pos1, const Screeps::RoomPosition &pos2) bool isNearTo(const Screeps::RoomPosition &pos1, const Screeps::RoomPosition &pos2, int dist)
{ {
int dx = pos1.x() - pos2.x(); int dx = pos1.x() - pos2.x();
int dy = pos1.y() - pos2.y(); int dy = pos1.y() - pos2.y();
return dx * dx < 2 && dy * dy < 2 && pos1.roomName() == pos2.roomName(); int dist2 = dist * dist;
return dx * dx <= dist2 &&
dy * dy <= dist2 &&
pos1.roomName() == pos2.roomName();
} }

View File

@@ -22,6 +22,7 @@ extern "C" void loop()
spawn.process(); spawn.process();
} }
JS::console.log(std::string("\n\n\n\n\n\n\n\n\n"));
JS::console.log(std::string("Processing tick:\t") + std::to_string(Screeps::Game.time())); JS::console.log(std::string("Processing tick:\t") + std::to_string(Screeps::Game.time()));
DouwcoHivemind::Engine engine; DouwcoHivemind::Engine engine;
@@ -29,7 +30,6 @@ extern "C" void loop()
JS::console.log("Used CPU:\t" + std::to_string(Screeps::Game.cpuGetUsed())); JS::console.log("Used CPU:\t" + std::to_string(Screeps::Game.cpuGetUsed()));
JS::console.log("Bucket:\t" + std::to_string(static_cast<int>(Screeps::Game.cpu()["bucket"]))); JS::console.log("Bucket:\t" + std::to_string(static_cast<int>(Screeps::Game.cpu()["bucket"])));
JS::console.log(std::string("\n\n\n"));
} }
EMSCRIPTEN_BINDINGS(loop) EMSCRIPTEN_BINDINGS(loop)