added the maintainer and changed some naming.

This commit is contained in:
douwe
2025-08-26 16:34:49 +02:00
parent 0c693354e6
commit eea9e7d69f
24 changed files with 328 additions and 164 deletions

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@@ -0,0 +1,22 @@
#ifndef DOUWCO_HIVEMIND_BUILDER_HPP
#define DOUWCO_HIVEMIND_BUILDER_HPP
#include "Creeps/Worker.hpp"
namespace DouwcoHivemind
{
class Builder : public Worker
{
public:
Builder(Screeps::Creep creep) : Worker(creep) {}
protected:
void depositEnergy() override;
private:
std::unique_ptr<Screeps::ConstructionSite> getConstructionSiteTarget();
void searchConstructionSite();
};
}
#endif // DOUWCO_HIVEMIND_BUILDER_HPP

View File

@@ -1,5 +1,5 @@
#ifndef DOUWCO_HIVEMIND_CREEP_HPP #ifndef DOUWCO_HIVEMIND_CREEPBASE_HPP
#define DOUWCO_HIVEMIND_CREEP_HPP #define DOUWCO_HIVEMIND_CREEPBASE_HPP
#include <Screeps/Creep.hpp> #include <Screeps/Creep.hpp>
@@ -14,12 +14,13 @@ namespace DouwcoHivemind
enum CreepRole enum CreepRole
{ {
UNEMPLOYED, UNEMPLOYED,
HARVESTER_SUPPLIER, SUPPLIER,
HARVESTER_UPGRADER, UPGRADER,
HARVESTER_BUILDER BUILDER,
MAINTAINER
}; };
class Creep class CreepBase
{ {
public: public:
CreepRole role; CreepRole role;
@@ -30,15 +31,19 @@ namespace DouwcoHivemind
JSON memory; JSON memory;
public: public:
Creep(Screeps::Creep crp); CreepBase(Screeps::Creep crp);
virtual ~Creep(); virtual ~CreepBase();
virtual void loop() {} virtual void loop() {}
bool isNearTo(const Screeps::RoomPosition &pos, int dist); bool isNearTo(const Screeps::RoomPosition &pos, int dist);
protected: protected:
void moveToTarget(int dist = 1); void moveToTarget(int dist = 1);
std::unique_ptr<Screeps::RoomObject> getRoomObjectTarget(); std::unique_ptr<Screeps::RoomObject> getRoomObjectTarget();
}; };
} }
#endif // DOUWCO_HIVEMIND_CREEP_HPP #endif // DOUWCO_HIVEMIND_CREEPBASE_HPP

View File

@@ -1,22 +0,0 @@
#ifndef DOUWCO_HIVEMIND_HARVESTER_BUILDER_HPP
#define DOUWCO_HIVEMIND_HARVESTER_BUILDER_HPP
#include "Creeps/Harvester.hpp"
namespace DouwcoHivemind
{
class HarvesterBuilder : public Harvester
{
public:
HarvesterBuilder(Screeps::Creep creep) : Harvester(creep) {}
protected:
void depositEnergy() override;
private:
std::unique_ptr<Screeps::ConstructionSite> getConstructionSiteTarget();
void searchConstructionSite();
};
}
#endif // DOUWCO_HIVEMIND_HARVESTER_BUILDER_HPP

View File

@@ -1,22 +0,0 @@
#ifndef DOUWCO_HIVEMIND_HARVESTER_SUPPLIER_HPP
#define DOUWCO_HIVEMIND_HARVESTER_SUPPLIER_HPP
#include "Creeps/Harvester.hpp"
namespace DouwcoHivemind
{
class HarvesterSupplier : public Harvester
{
public:
HarvesterSupplier(Screeps::Creep creep) : Harvester(creep) {}
protected:
void depositEnergy() override;
private:
std::unique_ptr<Screeps::Structure> getEnergyStructureTarget();
void searchEnergyStructure();
};
}
#endif // DOUWCO_HIVEMIND_HARVESTER_SUPPLIER_HPP

View File

@@ -0,0 +1,22 @@
#ifndef DOUWCO_HIVEMIND_MAINTAINER_HPP
#define DOUWCO_HIVEMIND_MAINTAINER_HPP
#include "Creeps/Worker.hpp"
namespace DouwcoHivemind
{
class Maintainer : public Worker
{
public:
Maintainer(Screeps::Creep creep) : Worker(creep) {}
protected:
void depositEnergy() override;
private:
std::unique_ptr<Screeps::Structure> getDamagedStructureTarget();
void searchDamagedStructure();
};
}
#endif // DOUWCO_HIVEMIND_MAINTAINER_HPP

View File

@@ -0,0 +1,22 @@
#ifndef DOUWCO_HIVEMIND_SUPPLIER_HPP
#define DOUWCO_HIVEMIND_SUPPLIER_HPP
#include "Creeps/Worker.hpp"
namespace DouwcoHivemind
{
class Supplier : public Worker
{
public:
Supplier(Screeps::Creep creep) : Worker(creep) {}
protected:
void depositEnergy() override;
private:
std::unique_ptr<Screeps::Structure> getEnergyStructureTarget();
void searchEnergyStructure();
};
}
#endif // DOUWCO_HIVEMIND_SUPPLIER_HPP

View File

@@ -1,14 +1,14 @@
#ifndef DOUWCO_HIVEMIND_HARVESTER_UPGRADER_HPP #ifndef DOUWCO_HIVEMIND_HARVESTER_UPGRADER_HPP
#define DOUWCO_HIVEMIND_HARVESTER_UPGRADER_HPP #define DOUWCO_HIVEMIND_HARVESTER_UPGRADER_HPP
#include "Creeps/Harvester.hpp" #include "Creeps/Worker.hpp"
namespace DouwcoHivemind namespace DouwcoHivemind
{ {
class HarvesterUpgrader : public Harvester class Upgrader : public Worker
{ {
public: public:
HarvesterUpgrader(Screeps::Creep creep) : Harvester(creep) {} Upgrader(Screeps::Creep creep) : Worker(creep) {}
protected: protected:
void depositEnergy() override; void depositEnergy() override;

View File

@@ -1,18 +1,18 @@
#ifndef DOUWCO_HIVEMIND_HARVESTER_HPP #ifndef DOUWCO_HIVEMIND_HARVESTER_HPP
#define DOUWCO_HIVEMIND_HARVESTER_HPP #define DOUWCO_HIVEMIND_HARVESTER_HPP
#include "Creeps/Creep.hpp" #include "Creeps/CreepBase.hpp"
namespace DouwcoHivemind namespace DouwcoHivemind
{ {
class Harvester : public Creep class Worker : public CreepBase
{ {
private: private:
bool harvesting; bool harvesting;
public: public:
Harvester(Screeps::Creep crp); Worker(Screeps::Creep crp);
~Harvester() override; ~Worker() override;
void loop() override; void loop() override;

View File

@@ -3,16 +3,16 @@
#include <vector> #include <vector>
#include "Creeps/Creep.hpp" #include "Creeps/CreepBase.hpp"
#include "Structures/Structure.hpp" #include "Structures/StructureBase.hpp"
namespace DouwcoHivemind namespace DouwcoHivemind
{ {
class Engine class Engine
{ {
private: private:
std::vector<std::unique_ptr<Creep>> creeps; std::vector<std::unique_ptr<CreepBase>> creeps;
std::vector<std::unique_ptr<Structure>> structures; std::vector<std::unique_ptr<StructureBase>> structures;
public: public:
Engine(); Engine();
@@ -21,6 +21,7 @@ namespace DouwcoHivemind
private: private:
void ReadOutCreeps(); void ReadOutCreeps();
void ReadOutStructures(); void ReadOutStructures();
void clearDeadCreepMemory();
}; };
} }

View File

@@ -1,15 +0,0 @@
#ifndef DOUWCO_HIVEMIND_ROOMS_HPP
#define DOUWCO_HIVEMIND_ROOMS_HPP
namespace DouwcoHivemind
{
class Room
{
public:
Room();
void loop();
};
}
#endif // DOUWCO_HIVEMIND_ROOMS_HPP

View File

@@ -3,18 +3,18 @@
#include <Screeps/StructureSpawn.hpp> #include <Screeps/StructureSpawn.hpp>
#include "Structures/Structure.hpp" #include "Structures/StructureBase.hpp"
namespace DouwcoHivemind namespace DouwcoHivemind
{ {
class Spawn : public Structure class Spawn : public StructureBase
{ {
private: private:
Screeps::StructureSpawn spawn; Screeps::StructureSpawn spawn;
public: public:
Spawn(Screeps::StructureSpawn spwn) : spawn(spwn), Spawn(Screeps::StructureSpawn spwn) : spawn(spwn),
Structure() {} StructureBase() {}
~Spawn() {} ~Spawn() {}
void loop() override; void loop() override;

View File

@@ -5,7 +5,7 @@
namespace DouwcoHivemind namespace DouwcoHivemind
{ {
class Structure class StructureBase
{ {
public: public:
virtual void loop(){} virtual void loop(){}

View File

@@ -2,11 +2,11 @@
#define DOUWCO_HIVEMIND_PATH_TOOL_HPP #define DOUWCO_HIVEMIND_PATH_TOOL_HPP
#include <vector> #include <vector>
#include <Screeps/ReturnTypes.hpp> #include <Screeps/Room.hpp>
namespace DouwcoHivemind namespace DouwcoHivemind
{ {
static std::vector<int> flattenPathSteps(const std::vector<Screeps::PathStep> &pathSteps) static std::vector<int> flattenPathSteps(const std::vector<Screeps::Room::PathStep> &pathSteps)
{ {
std::vector<int> flattened; std::vector<int> flattened;
for (const auto &step : pathSteps) for (const auto &step : pathSteps)
@@ -20,12 +20,18 @@ namespace DouwcoHivemind
return flattened; return flattened;
} }
static std::vector<Screeps::PathStep> unflattenPathSteps(const std::vector<int> &flattened) static std::vector<Screeps::Room::PathStep> unflattenPathSteps(const std::vector<int> &flattened)
{ {
std::vector<Screeps::PathStep> pathSteps; std::vector<Screeps::Room::PathStep> pathSteps;
for (size_t i = 0; i < flattened.size(); i += 5) for (size_t i = 0; i < flattened.size(); i += 5)
{ {
pathSteps.emplace_back(Screeps::PathStep(flattened[i], flattened[i + 1], flattened[i + 2], flattened[i + 3], flattened[i + 4])); Screeps::Room::PathStep step;
step.x = flattened[i];
step.y = flattened[i+1];
step.dx = flattened[i+2];
step.dy = flattened[i+3];
step.direction = flattened[i+4];
pathSteps.emplace_back(step);
} }
return pathSteps; return pathSteps;
} }

View File

@@ -18,9 +18,9 @@
#include <Screeps/ConstructionSite.hpp> #include <Screeps/ConstructionSite.hpp>
#include "Creeps/HarvesterBuilder.hpp" #include "Creeps/Builder.hpp"
void DouwcoHivemind::HarvesterBuilder::depositEnergy() void DouwcoHivemind::Builder::depositEnergy()
{ {
auto constructionSite = getConstructionSiteTarget(); auto constructionSite = getConstructionSiteTarget();
if (!constructionSite) if (!constructionSite)
@@ -36,7 +36,7 @@ void DouwcoHivemind::HarvesterBuilder::depositEnergy()
} }
} }
std::unique_ptr<Screeps::ConstructionSite> DouwcoHivemind::HarvesterBuilder::getConstructionSiteTarget() std::unique_ptr<Screeps::ConstructionSite> DouwcoHivemind::Builder::getConstructionSiteTarget()
{ {
auto roomObj = getRoomObjectTarget(); auto roomObj = getRoomObjectTarget();
if (!roomObj) if (!roomObj)
@@ -55,7 +55,7 @@ std::unique_ptr<Screeps::ConstructionSite> DouwcoHivemind::HarvesterBuilder::get
return std::move(constructionSite); return std::move(constructionSite);
} }
void DouwcoHivemind::HarvesterBuilder::searchConstructionSite() void DouwcoHivemind::Builder::searchConstructionSite()
{ {
int leastProgressLeft = INT16_MAX; int leastProgressLeft = INT16_MAX;
Screeps::ConstructionSite *selectedConstructionSite; Screeps::ConstructionSite *selectedConstructionSite;

View File

@@ -3,28 +3,27 @@
#include <Screeps/Game.hpp> #include <Screeps/Game.hpp>
#include <Screeps/Room.hpp> #include <Screeps/Room.hpp>
#include <Screeps/RoomPosition.hpp> #include <Screeps/RoomPosition.hpp>
#include <Screeps/ReturnTypes.hpp>
#include "Creeps/Creep.hpp" #include "Creeps/CreepBase.hpp"
#include "Tools/JsonTool.hpp" #include "Tools/JsonTool.hpp"
#include "Tools/PathTool.hpp" #include "Tools/PathTool.hpp"
#include "Tools/MeasureTool.hpp" #include "Tools/MeasureTool.hpp"
DouwcoHivemind::Creep::Creep(Screeps::Creep crp) : creep(crp), DouwcoHivemind::CreepBase::CreepBase(Screeps::Creep crp) : creep(crp),
memory(crp.memory()) memory(crp.memory())
{ {
role = memory.contains("role") ? static_cast<CreepRole>(memory["role"]) : CreepRole::UNEMPLOYED; role = memory.contains("role") ? static_cast<CreepRole>(memory["role"]) : CreepRole::UNEMPLOYED;
target_id = memory.contains("target_id") ? static_cast<std::string>(memory["target_id"]) : std::string(); target_id = memory.contains("target_id") ? static_cast<std::string>(memory["target_id"]) : std::string();
} }
DouwcoHivemind::Creep::~Creep() DouwcoHivemind::CreepBase::~CreepBase()
{ {
memory["target_id"] = target_id; memory["target_id"] = target_id;
creep.setMemory(memory); creep.setMemory(memory);
} }
void DouwcoHivemind::Creep::moveToTarget(int dist) void DouwcoHivemind::CreepBase::moveToTarget(int dist)
{ {
// Is move required? // Is move required?
auto target = getRoomObjectTarget(); auto target = getRoomObjectTarget();
@@ -83,7 +82,12 @@ void DouwcoHivemind::Creep::moveToTarget(int dist)
int x = creep.pos().x(); int x = creep.pos().x();
int y = creep.pos().y(); int y = creep.pos().y();
auto step = Screeps::PathStep(pathStepData[0], pathStepData[1], pathStepData[2], pathStepData[3], pathStepData[4]); Screeps::Room::PathStep step;
step.x = pathStepData[0];
step.y = pathStepData[1];
step.dx = pathStepData[2];
step.dy = pathStepData[3];
step.direction = pathStepData[4];
if (memory.contains("last_pos")) if (memory.contains("last_pos"))
{ {
@@ -121,7 +125,7 @@ void DouwcoHivemind::Creep::moveToTarget(int dist)
} }
} }
std::unique_ptr<Screeps::RoomObject> DouwcoHivemind::Creep::getRoomObjectTarget() std::unique_ptr<Screeps::RoomObject> DouwcoHivemind::CreepBase::getRoomObjectTarget()
{ {
// Check if game can find target // Check if game can find target
auto roomObj = Screeps::Game.getObjectById(target_id); auto roomObj = Screeps::Game.getObjectById(target_id);
@@ -133,7 +137,7 @@ std::unique_ptr<Screeps::RoomObject> DouwcoHivemind::Creep::getRoomObjectTarget(
return std::move(roomObj); return std::move(roomObj);
} }
bool DouwcoHivemind::Creep::isNearTo(const Screeps::RoomPosition &pos, int dist) bool DouwcoHivemind::CreepBase::isNearTo(const Screeps::RoomPosition &pos, int dist)
{ {
return DouwcoHivemind::isNearTo(creep.pos(), pos, dist); return DouwcoHivemind::isNearTo(creep.pos(), pos, dist);
} }

View File

@@ -1,10 +1,12 @@
#include <Screeps/Game.hpp> #include <Screeps/Game.hpp>
#include <Screeps/Memory.hpp>
#include "Engine.hpp" #include "Engine.hpp"
#include "Creeps/HarvesterSupplier.hpp" #include "Creeps/Supplier.hpp"
#include "Creeps/HarvesterUpgrader.hpp" #include "Creeps/Upgrader.hpp"
#include "Creeps/HarvesterBuilder.hpp" #include "Creeps/Builder.hpp"
#include "Creeps/Maintainer.hpp"
#include "Structures/Spawn.hpp" #include "Structures/Spawn.hpp"
@@ -23,6 +25,11 @@ void DouwcoHivemind::Engine::loop()
JS::console.log(std::string("Iterating over structures")); JS::console.log(std::string("Iterating over structures"));
for (auto &structure : structures) for (auto &structure : structures)
structure->loop(); structure->loop();
if (Screeps::Game.time() % 1000 == 0)
{
clearDeadCreepMemory();
}
} }
void DouwcoHivemind::Engine::ReadOutCreeps() void DouwcoHivemind::Engine::ReadOutCreeps()
@@ -32,12 +39,14 @@ void DouwcoHivemind::Engine::ReadOutCreeps()
for (auto &creep : src_creeps) for (auto &creep : src_creeps)
{ {
CreepRole role = creep.second.memory()["role"]; CreepRole role = creep.second.memory()["role"];
if (role == CreepRole::HARVESTER_SUPPLIER) if (role == CreepRole::SUPPLIER)
creeps.push_back(std::make_unique<HarvesterSupplier>(creep.second)); creeps.push_back(std::make_unique<Supplier>(creep.second));
else if (role == CreepRole::HARVESTER_UPGRADER) else if (role == CreepRole::UPGRADER)
creeps.push_back(std::make_unique<HarvesterUpgrader>(creep.second)); creeps.push_back(std::make_unique<Upgrader>(creep.second));
else if (role == CreepRole::HARVESTER_BUILDER) else if (role == CreepRole::BUILDER)
creeps.push_back(std::make_unique<HarvesterBuilder>(creep.second)); creeps.push_back(std::make_unique<Builder>(creep.second));
else if (role == CreepRole::MAINTAINER)
creeps.push_back(std::make_unique<Maintainer>(creep.second));
} }
} }
@@ -50,3 +59,30 @@ void DouwcoHivemind::Engine::ReadOutStructures()
structures.push_back(std::make_unique<Spawn>(spawn.second)); structures.push_back(std::make_unique<Spawn>(spawn.second));
} }
} }
void DouwcoHivemind::Engine::clearDeadCreepMemory()
{
auto creepMemory = Screeps::Memory["creeps"];
auto creepsMap = Screeps::Game.creeps();
int iterator = 0;
for (auto [name, creep] : creepMemory.items())
{
// avoid cpu overload
iterator++;
if (iterator == 100)
break;
bool containsname = false;
for (auto creepObject : creepsMap)
{
if (creepObject.first == name)
{
containsname = true;
break;
}
}
if (!containsname)
creepMemory.erase(name);
}
Screeps::Memory.set("creeps", creepMemory);
}

View File

@@ -0,0 +1,89 @@
#include <vector>
#include <optional>
#include <emscripten.h>
#include <Screeps/Game.hpp>
#include <Screeps/Creep.hpp>
#include <Screeps/Source.hpp>
#include <Screeps/Room.hpp>
#include <Screeps/RoomPosition.hpp>
#include <Screeps/RoomObject.hpp>
#include <Screeps/Structure.hpp>
#include <Screeps/StructureController.hpp>
#include <Screeps/StructureSpawn.hpp>
#include <Screeps/StructureExtension.hpp>
#include <Screeps/StructureTower.hpp>
#include <Screeps/Store.hpp>
#include "Creeps/Maintainer.hpp"
void DouwcoHivemind::Maintainer::depositEnergy()
{
auto structure = getDamagedStructureTarget();
if (!structure)
return;
if (isNearTo(structure->pos(), 1))
{
int resp = creep.repair(*structure);
}
else
{
moveToTarget();
}
}
std::unique_ptr<Screeps::Structure> DouwcoHivemind::Maintainer::getDamagedStructureTarget()
{
auto roomObj = getRoomObjectTarget();
if (!roomObj)
{
searchDamagedStructure();
return nullptr;
}
auto structure = std::unique_ptr<Screeps::Structure>(dynamic_cast<Screeps::Structure *>(roomObj.release()));
if (!structure)
{
searchDamagedStructure();
return nullptr;
}
// Check if the structure is still broken
int damage = structure->hitsMax() - structure->hits();
if (damage == 0)
{
searchDamagedStructure();
return nullptr;
}
return std::move(structure);
}
void DouwcoHivemind::Maintainer::searchDamagedStructure()
{
int lowestMaintaince = INT16_MAX;
Screeps::Structure *selectedStructure;
auto structures = creep.room().find(Screeps::FIND_STRUCTURES);
for (auto &structureObject : structures)
{
auto structure = dynamic_cast<Screeps::Structure *>(structureObject.get());
if (!structure)
continue;
int maintance = structure->hits();
auto structureType = structure->structureType();
if (structureType == Screeps::STRUCTURE_CONTROLLER)
continue;
if (maintance < lowestMaintaince)
{
lowestMaintaince = maintance;
selectedStructure = structure;
}
}
if (selectedStructure)
target_id = selectedStructure->id();
else
target_id.clear();
}

View File

@@ -1,7 +1,7 @@
#include <Screeps/Game.hpp> #include <Screeps/Game.hpp>
#include <Screeps/Room.hpp> #include <Screeps/Room.hpp>
#include "Creeps/Creep.hpp" #include "Creeps/CreepBase.hpp"
#include "Structures/Spawn.hpp" #include "Structures/Spawn.hpp"
void DouwcoHivemind::Spawn::loop() void DouwcoHivemind::Spawn::loop()
@@ -13,38 +13,49 @@ void DouwcoHivemind::Spawn::loop()
int energyAvailable = spawn.room().energyAvailable(); int energyAvailable = spawn.room().energyAvailable();
int energyCapacityAvailable = spawn.room().energyCapacityAvailable(); int energyCapacityAvailable = spawn.room().energyCapacityAvailable();
int required_upgraders = 4; int required_upgraders = 1;
int required_suppliers = energyCapacityAvailable/200; int required_suppliers = 1;
int required_builders = 4; int required_maintainers = 1;
int required_builders = spawn.room().find(Screeps::FIND_MY_CONSTRUCTION_SITES).size() == 0 ? 0 : 1;
for (auto &creep : Screeps::Game.creeps()) for (auto &creep : Screeps::Game.creeps())
{ {
CreepRole role = creep.second.memory()["role"]; CreepRole role = creep.second.memory()["role"];
if (role == CreepRole::HARVESTER_SUPPLIER) if (role == CreepRole::SUPPLIER)
required_suppliers--; required_suppliers--;
else if (role == CreepRole::HARVESTER_UPGRADER) else if (role == CreepRole::UPGRADER)
required_upgraders--; required_upgraders--;
else if (role == CreepRole::HARVESTER_BUILDER) else if (role == CreepRole::MAINTAINER)
required_maintainers--;
else if (role == CreepRole::BUILDER)
required_builders--; required_builders--;
} }
if(energyAvailable < energyCapacityAvailable && required_suppliers == 0) return; if (energyAvailable < energyCapacityAvailable && required_suppliers < 2)
return;
std::string name; std::string name;
JSON opts; JSON opts;
if (required_suppliers > 0){ if (required_suppliers > 0)
opts["memory"]["role"] = CreepRole::HARVESTER_SUPPLIER; {
name = "Susi Harviston\n"; opts["memory"]["role"] = CreepRole::SUPPLIER;
name = "Supplier: ";
} }
else if (required_upgraders > 0){ else if (required_upgraders > 0)
opts["memory"]["role"] = CreepRole::HARVESTER_UPGRADER; {
name = "Upperheim Harviston\n"; opts["memory"]["role"] = CreepRole::UPGRADER;
name = "Upgrader: ";
}
else if (required_builders > 0)
{
opts["memory"]["role"] = CreepRole::BUILDER;
name = "Builder: ";
}
else if (required_maintainers > 0)
{
opts["memory"]["role"] = CreepRole::MAINTAINER;
name = "Maintainer: ";
} }
else if (required_builders > 0){
opts["memory"]["role"] = CreepRole::HARVESTER_BUILDER;
name = "Bob Harviston\n";
}
else else
return; return;

View File

@@ -13,12 +13,11 @@
#include <Screeps/StructureSpawn.hpp> #include <Screeps/StructureSpawn.hpp>
#include <Screeps/StructureExtension.hpp> #include <Screeps/StructureExtension.hpp>
#include <Screeps/StructureTower.hpp> #include <Screeps/StructureTower.hpp>
#include <Screeps/Constants.hpp>
#include <Screeps/Store.hpp> #include <Screeps/Store.hpp>
#include "Creeps/HarvesterSupplier.hpp" #include "Creeps/Supplier.hpp"
void DouwcoHivemind::HarvesterSupplier::depositEnergy() void DouwcoHivemind::Supplier::depositEnergy()
{ {
auto structure = getEnergyStructureTarget(); auto structure = getEnergyStructureTarget();
if (!structure) if (!structure)
@@ -34,7 +33,7 @@ void DouwcoHivemind::HarvesterSupplier::depositEnergy()
} }
} }
std::unique_ptr<Screeps::Structure> DouwcoHivemind::HarvesterSupplier::getEnergyStructureTarget() std::unique_ptr<Screeps::Structure> DouwcoHivemind::Supplier::getEnergyStructureTarget()
{ {
auto roomObj = getRoomObjectTarget(); auto roomObj = getRoomObjectTarget();
if (!roomObj) if (!roomObj)
@@ -63,6 +62,11 @@ std::unique_ptr<Screeps::Structure> DouwcoHivemind::HarvesterSupplier::getEnergy
auto extension = dynamic_cast<Screeps::StructureExtension *>(structure.get()); auto extension = dynamic_cast<Screeps::StructureExtension *>(structure.get());
energyCapacity = extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value(); energyCapacity = extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
} }
else if (structureType == Screeps::STRUCTURE_TOWER)
{
auto extension = dynamic_cast<Screeps::StructureTower *>(structure.get());
energyCapacity = extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
}
if (energyCapacity == 0) if (energyCapacity == 0)
{ {
@@ -73,7 +77,7 @@ std::unique_ptr<Screeps::Structure> DouwcoHivemind::HarvesterSupplier::getEnergy
return std::move(structure); return std::move(structure);
} }
void DouwcoHivemind::HarvesterSupplier::searchEnergyStructure() void DouwcoHivemind::Supplier::searchEnergyStructure()
{ {
int mostEnergyNeeded = 0; int mostEnergyNeeded = 0;
Screeps::Structure *selectedStructure; Screeps::Structure *selectedStructure;
@@ -97,6 +101,11 @@ void DouwcoHivemind::HarvesterSupplier::searchEnergyStructure()
auto extension = dynamic_cast<Screeps::StructureExtension *>(structure); auto extension = dynamic_cast<Screeps::StructureExtension *>(structure);
energyRequired = extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value(); energyRequired = extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
} }
else if (structureType == Screeps::STRUCTURE_TOWER)
{
auto extension = dynamic_cast<Screeps::StructureTower *>(structure);
energyRequired = extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
}
if (energyRequired > mostEnergyNeeded) if (energyRequired > mostEnergyNeeded)
{ {

View File

@@ -16,9 +16,9 @@
#include <Screeps/Constants.hpp> #include <Screeps/Constants.hpp>
#include <Screeps/Store.hpp> #include <Screeps/Store.hpp>
#include "Creeps/HarvesterUpgrader.hpp" #include "Creeps/Upgrader.hpp"
void DouwcoHivemind::HarvesterUpgrader::depositEnergy() void DouwcoHivemind::Upgrader::depositEnergy()
{ {
auto controller = creep.room().controller().value(); auto controller = creep.room().controller().value();
target_id = controller.id(); target_id = controller.id();

View File

@@ -16,19 +16,19 @@
#include <Screeps/Constants.hpp> #include <Screeps/Constants.hpp>
#include <Screeps/Store.hpp> #include <Screeps/Store.hpp>
#include "Creeps/Harvester.hpp" #include "Creeps/Worker.hpp"
DouwcoHivemind::Harvester::Harvester(Screeps::Creep crp) : Creep(crp) DouwcoHivemind::Worker::Worker(Screeps::Creep crp) : CreepBase(crp)
{ {
harvesting = memory.contains("harvesting") ? static_cast<bool>(memory["harvesting"]) : false; harvesting = memory.contains("harvesting") ? static_cast<bool>(memory["harvesting"]) : false;
} }
DouwcoHivemind::Harvester::~Harvester() DouwcoHivemind::Worker::~Worker()
{ {
memory["harvesting"] = harvesting; memory["harvesting"] = harvesting;
} }
void DouwcoHivemind::Harvester::loop() void DouwcoHivemind::Worker::loop()
{ {
if (harvesting) if (harvesting)
{ {
@@ -50,7 +50,7 @@ void DouwcoHivemind::Harvester::loop()
} }
} }
void DouwcoHivemind::Harvester::harvestSource() void DouwcoHivemind::Worker::harvestSource()
{ {
auto source = getSourceTarget(); auto source = getSourceTarget();
if (!source) if (!source)
@@ -66,7 +66,7 @@ void DouwcoHivemind::Harvester::harvestSource()
} }
} }
std::unique_ptr<Screeps::Source> DouwcoHivemind::Harvester::getSourceTarget() std::unique_ptr<Screeps::Source> DouwcoHivemind::Worker::getSourceTarget()
{ {
auto roomObj = getRoomObjectTarget(); auto roomObj = getRoomObjectTarget();
if (!roomObj) if (!roomObj)
@@ -94,7 +94,7 @@ std::unique_ptr<Screeps::Source> DouwcoHivemind::Harvester::getSourceTarget()
return std::move(source); return std::move(source);
} }
void DouwcoHivemind::Harvester::searchSource() void DouwcoHivemind::Worker::searchSource()
{ {
target_id.clear(); target_id.clear();
@@ -102,26 +102,22 @@ void DouwcoHivemind::Harvester::searchSource()
if (sources.size() == 0) if (sources.size() == 0)
return; return;
Screeps::Source *selectedSource; int x = creep.pos().x();
int maxEnergy = 0; int y = creep.pos().y();
for (auto &sourceObj : sources)
{
auto source = dynamic_cast<Screeps::Source *>(sourceObj.get());
if (!source)
continue;
auto sourceEnergy = source->energy(); int closestDistance = INT16_MAX;
if (sourceEnergy > maxEnergy) Screeps::Source *selectedSource;
{ for(auto &sourceObject : sources){
maxEnergy = sourceEnergy; auto source =dynamic_cast<Screeps::Source *>(sourceObject.get());
if(source->energy()==0) 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; selectedSource = source;
} }
} }
if(!selectedSource) return;
if (!selectedSource)
{
// EM_ASM({console.log($0 + ': No sources with energy found!')}, creep.name().c_str());
return;
}
target_id = selectedSource->id(); target_id = selectedSource->id();
} }