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"
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()
{
@@ -50,7 +50,7 @@ void DouwcoHivemind::HarvesterRole::harvestSource()
auto source = getSourceTarget();
if (!source)
return;
if (isNearTo(creep.pos(), source->pos()))
if (isNearTo(creep.pos(), source->pos(), 1))
{
int resp = creep.harvest(*source);
}
@@ -124,21 +124,27 @@ void DouwcoHivemind::HarvesterRole::depositEnergy()
if (!structure)
return;
if (isNearTo(creep.pos(), structure->pos()))
if (structure->structureType() == Screeps::STRUCTURE_CONTROLLER)
{
int resp;
if (structure->structureType() == Screeps::STRUCTURE_CONTROLLER)
if (isNearTo(creep.pos(), structure->pos(), 3))
{
auto controller = dynamic_cast<Screeps::StructureController *>(structure.get());
if (!controller)
return;
resp = creep.upgradeController(*controller);
int resp = creep.upgradeController(*controller);
}
else
resp = creep.transfer(*structure, Screeps::RESOURCE_ENERGY);
creep.moveTo(*structure);
}
else
creep.moveTo(*structure);
{
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()
@@ -162,7 +168,11 @@ std::unique_ptr<Screeps::Structure> DouwcoHivemind::HarvesterRole::getDepositTar
// Check if the structure can receive energy to harvest
int energyCapacity;
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());
energyCapacity = spawn->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
@@ -216,8 +226,8 @@ void DouwcoHivemind::HarvesterRole::searchDeposit()
selectedStructure = structure;
}
}
if(selectedStructure)
if (selectedStructure)
memory["target"] = selectedStructure->id();
else
memory["target"].clear();
@@ -240,9 +250,12 @@ std::unique_ptr<Screeps::RoomObject> DouwcoHivemind::HarvesterRole::getRoomObjec
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 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();
}
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()));
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("Bucket:\t" + std::to_string(static_cast<int>(Screeps::Game.cpu()["bucket"])));
JS::console.log(std::string("\n\n\n"));
}
EMSCRIPTEN_BINDINGS(loop)