[untested] Implemented Miner role. Not in spawner yet.

This commit is contained in:
douwe
2025-12-19 09:19:32 +01:00
parent 1596283841
commit 656cca578e
6 changed files with 94 additions and 11 deletions

View File

@@ -0,0 +1,23 @@
#ifndef DOUWCO_HIVEMIND_MINER_HPP
#define DOUWCO_HIVEMIND_MINER_HPP
#include <Creeps/CreepBase.hpp>
namespace DouwcoHivemind
{
class Miner : public CreepBase
{
private:
bool requestedContainer = false;
public:
Miner(Screeps::Creep crp);
~Miner() override;
void loop() override;
private:
bool mineSource();
std::unique_ptr<Screeps::Source> getSourceTarget();
};
}
#endif // DOUWCO_HIVEMIND_MINER_HPP

View File

@@ -1,5 +1,5 @@
#ifndef DOUWCO_HIVEMIND_HARVESTER_HPP
#define DOUWCO_HIVEMIND_HARVESTER_HPP
#ifndef DOUWCO_HIVEMIND_WORKER_HPP
#define DOUWCO_HIVEMIND_WORKER_HPP
#include "Creeps/CreepBase.hpp"
@@ -26,4 +26,4 @@ namespace DouwcoHivemind
};
}
#endif // DOUWCO_HIVEMIND_HARVESTER_HPP
#endif // DOUWCO_HIVEMIND_WORKER_HPP

View File

@@ -0,0 +1,67 @@
#include <emscripten.h>
#include <Screeps/Constants.hpp>
#include <Screeps/Source.hpp>
#include <Screeps/Room.hpp>
#include <Screeps/RoomPosition.hpp>
#include "Creeps/Miner.hpp"
DouwcoHivemind::Miner::Miner(Screeps::Creep crp) : CreepBase(crp)
{
requestedContainer = memory.contains("requestedContainer") ? static_cast<bool>(memory["requestedContainer"]) : false;
}
DouwcoHivemind::Miner::~Miner()
{
memory["requestedContainer"] = requestedContainer;
}
void DouwcoHivemind::Miner::loop()
{
if (mineSource() && !requestedContainer)
{
creep.room().createConstructionSite(creep.pos(), Screeps::STRUCTURE_CONTAINER);
requestedContainer = true;
}
}
bool DouwcoHivemind::Miner::mineSource()
{
auto source = getSourceTarget();
if (!source){
EM_ASM({console.log($0 + ': Miner doesn\'t have valid source target')}, creep.name().c_str());
return false;
}
if (isNearTo(source->pos(), 1))
{
int resp = creep.harvest(*source);
return true;
}
else
{
moveToTarget();
return false;
}
}
std::unique_ptr<Screeps::Source> DouwcoHivemind::Miner::getSourceTarget()
{
auto roomObj = getRoomObjectTarget();
if (!roomObj)
{
// todo: request source from room
return nullptr;
}
// Check if found roomobject is an actual source
auto source = std::unique_ptr<Screeps::Source>(dynamic_cast<Screeps::Source *>(roomObj.release()));
if (!source)
{
// EM_ASM({console.log($0 + ': Can\'t cast target to Source')}, creep.name().c_str());
// todo: request source from room
return nullptr;
}
return std::move(source);
}