Added custom move behaviour to creeps

This commit is contained in:
douwe
2025-08-23 18:15:27 +02:00
parent 6ee67eac47
commit 98b123ee0d
21 changed files with 240 additions and 108 deletions

View File

@@ -0,0 +1,52 @@
#ifndef DOUWCO_HIVEMIND_CREEP_HPP
#define DOUWCO_HIVEMIND_CREEP_HPP
#include <Screeps/Creep.hpp>
#include <Screeps/ReturnTypes.hpp>
#include "Tools/JsonTool.hpp"
#include "Tools/PathTool.hpp"
namespace DouwcoHivemind
{
enum CreepRole
{
UNEMPLOYED,
HARVESTER
};
class Creep
{
public:
CreepRole role;
std::string target_id;
std::vector<Screeps::PathStep> path;
protected:
Screeps::Creep creep;
JSON memory;
public:
Creep(Screeps::Creep crp) : creep(crp),
memory(crp.memory())
{
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();
path = memory.contains("path") ? unflattenPathSteps(jsonToVector<int>(memory["path"])) : std::vector<Screeps::PathStep>();
}
virtual ~Creep()
{
memory["target_id"] = target_id;
memory["path"] = vectorToJson(flattenPathSteps(path));
creep.setMemory(memory);
}
virtual void loop() {}
bool isNearTo(const Screeps::RoomPosition &pos, int dist);
protected:
void moveToTarget(int dist = 1);
std::unique_ptr<Screeps::RoomObject> getRoomObjectTarget();
bool isNearTo(const Screeps::RoomPosition &pos1, const Screeps::RoomPosition &pos2, int dist);
};
}
#endif // DOUWCO_HIVEMIND_CREEP_HPP

View File

@@ -0,0 +1,42 @@
#ifndef DOUWCO_HIVEMIND_HARVESTER_HPP
#define DOUWCO_HIVEMIND_HARVESTER_HPP
#include <Screeps/Creep.hpp>
#include "Creeps/Creep.hpp"
namespace DouwcoHivemind
{
class HarvesterRole : public Creep
{
private:
bool harvesting;
int taskCounter;
public:
HarvesterRole(Screeps::Creep crp) : Creep(crp)
{
harvesting = memory.contains("harvesting") ? static_cast<bool>(memory["harvesting"]) : false;
taskCounter = memory.contains("taskCounter") ? static_cast<int>(memory["taskCounter"]) : 0;
}
~HarvesterRole() override
{
memory["harvesting"] = harvesting;
memory["taskCounter"] = taskCounter;
}
void loop() override;
private:
void harvestSource();
std::unique_ptr<Screeps::Source> getSourceTarget();
void searchSource();
void depositEnergy();
std::unique_ptr<Screeps::Structure> getDepositTarget();
void searchDeposit();
};
}
#endif // DOUWCO_HIVEMIND_HARVESTER_HPP

View File

@@ -0,0 +1,73 @@
#ifndef DOUWCO_HIVEMIND_ENGINE_HPP
#define DOUWCO_HIVEMIND_ENGINE_HPP
#include <vector>
#include <Screeps/JS.hpp>
#include <Screeps/Game.hpp>
#include <Screeps/Creep.hpp>
#include "Creeps/Creep.hpp"
#include "Creeps/Harvester.hpp"
#include "Structures/Structure.hpp"
#include "Structures/Spawn.hpp"
namespace DouwcoHivemind
{
class Creep;
class Structure;
class Engine
{
private:
std::vector<std::unique_ptr<Creep>> creeps;
std::vector<std::unique_ptr<Structure>> structures;
public:
Engine()
{
ReadOutCreeps();
ReadOutStructures();
}
~Engine() {}
void loop()
{
for (auto &creep : creeps)
creep->loop();
for (auto &structure : structures)
structure->loop();
}
private:
void ReadOutCreeps()
{
auto src_creeps = Screeps::Game.creeps();
for (auto &creep : src_creeps)
{
CreepRole role = creep.second.memory()["role"];
switch (role)
{
case CreepRole::HARVESTER:
creeps.push_back(std::make_unique<HarvesterRole>(creep.second));
break;
case CreepRole::UNEMPLOYED:
default:
EM_ASM({console.log('Undefined role for creep' + $0)}, creep.first.c_str());
break;
}
}
}
void ReadOutStructures()
{
auto spawns = Screeps::Game.spawns();
for (auto &spawn : spawns)
{
structures.push_back(std::make_unique<Spawn>(spawn.second));
}
}
};
}
#endif // DOUWCO_HIVEMIND_ENGINE_HPP

View File

@@ -0,0 +1,24 @@
#ifndef DOUWCO_HIVEMIND_SPAWN_HPP
#define DOUWCO_HIVEMIND_SPAWN_HPP
#include <Screeps/StructureSpawn.hpp>
#include "Structures/Structure.hpp"
namespace DouwcoHivemind
{
class Spawn : public Structure
{
private:
Screeps::StructureSpawn spawn;
public:
Spawn(Screeps::StructureSpawn spwn) : spawn(spwn),
Structure() {}
~Spawn() {}
void loop() override;
};
}
#endif // DOUWCO_HIVEMIND_SPAWN_HPP

View File

@@ -0,0 +1,15 @@
#ifndef DOUWCO_HIVEMIND_STRUCTURE_HPP
#define DOUWCO_HIVEMIND_STRUCTURE_HPP
#include <Screeps/Structure.hpp>
namespace DouwcoHivemind
{
class Structure
{
public:
virtual void loop(){}
};
}
#endif // DOUWCO_HIVEMIND_STRUCTURE_HPP

View File

@@ -0,0 +1,33 @@
#ifndef DOUWCO_HIVEMIND_JSON_TOOLS_HPP
#define DOUWCO_HIVEMIND_JSON_TOOLS_HPP
#include <nlohmann/json.hpp>
#include <vector>
namespace DouwcoHivemind
{
template <typename T>
std::vector<T> jsonToVector(const nlohmann::json &json)
{
std::vector<T> vector;
for (const auto &item : json)
{
vector.emplace_back(item.get<T>());
}
return vector;
};
template <typename T>
nlohmann::json vectorToJson(const std::vector<T> &vector)
{
nlohmann::json json;
for (const auto &item : vector)
{
json.emplace_back(item);
}
return json;
};
}
#endif // DOUWCO_HIVEMIND_JSON_TOOLS_HPP

View File

@@ -0,0 +1,35 @@
#ifndef DOUWCO_HIVEMIND_PATH_TOOL_HPP
#define DOUWCO_HIVEMIND_PATH_TOOL_HPP
#include <vector>
#include <Screeps/ReturnTypes.hpp>
namespace DouwcoHivemind
{
static std::vector<int> flattenPathSteps(const std::vector<Screeps::PathStep> &pathSteps)
{
std::vector<int> flattened;
for (const auto &step : pathSteps)
{
flattened.push_back(step.x);
flattened.push_back(step.y);
flattened.push_back(step.dx);
flattened.push_back(step.dy);
flattened.push_back(step.direction);
}
return flattened;
}
static std::vector<Screeps::PathStep> unflattenPathSteps(const std::vector<int> &flattened)
{
std::vector<Screeps::PathStep> pathSteps;
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]));
}
return pathSteps;
}
} // namespace Screeps
#endif // DOUWCO_HIVEMIND_PATH_TOOL_HPP

View File

@@ -0,0 +1,64 @@
#include <algorithm>
#include <Screeps/Game.hpp>
#include <Screeps/Room.hpp>
#include <Screeps/RoomPosition.hpp>
#include "Creeps/Creep.hpp"
void DouwcoHivemind::Creep::moveToTarget(int dist)
{
auto target = getRoomObjectTarget();
if (isNearTo(target->pos(), dist))
return;
if (path.size() == 0){
path = creep.room().findPath(creep.pos(), target->pos());
std::reverse(path.begin(), path.end());
}
JS::console.log(std::string("creep pos: [") +
std::to_string(creep.pos().x()) +
std::string(",") +
std::to_string(creep.pos().y()) +
std::string("]"));
auto step = path.back();
path.pop_back();
if (creep.pos().x() == step.x - step.dx && creep.pos().y() == step.y - step.dy)
{
int resp = creep.move(step.direction);
if(resp != Screeps::ERR_INVALID_ARGS) return;
}
path.clear();
}
std::unique_ptr<Screeps::RoomObject> DouwcoHivemind::Creep::getRoomObjectTarget()
{
// Check if game can find target
auto roomObj = Screeps::Game.getObjectById(target_id);
if (!roomObj)
{
JS::console.log(creep.name() + ": Game can\'t find target id");
return nullptr;
}
return std::move(roomObj);
}
bool DouwcoHivemind::Creep::isNearTo(const Screeps::RoomPosition &pos, int dist)
{
return isNearTo(creep.pos(), pos, dist);
}
bool DouwcoHivemind::Creep::isNearTo(const Screeps::RoomPosition &pos1, const Screeps::RoomPosition &pos2, int dist)
{
int dx = pos1.x() - pos2.x();
int dy = pos1.y() - pos2.y();
int dist2 = dist * dist;
return dx * dx <= dist2 &&
dy * dy <= dist2 &&
pos1.roomName() == pos2.roomName();
}

View File

@@ -0,0 +1,236 @@
#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/Constants.hpp>
#include <Screeps/Store.hpp>
#include "Creeps/Harvester.hpp"
void DouwcoHivemind::HarvesterRole::loop()
{
if (harvesting)
{
if (creep.store().getFreeCapacity(Screeps::RESOURCE_ENERGY) == 0)
{
harvesting = false;
target_id.clear();
}
harvestSource();
}
else
{
if (creep.store().getUsedCapacity(Screeps::RESOURCE_ENERGY) == 0)
{
harvesting = true;
target_id.clear();
}
depositEnergy();
}
}
void DouwcoHivemind::HarvesterRole::harvestSource()
{
auto source = getSourceTarget();
if (!source)
return;
if (isNearTo(source->pos(), 1))
{
int resp = creep.harvest(*source);
}
else
{
moveToTarget();
}
}
std::unique_ptr<Screeps::Source> DouwcoHivemind::HarvesterRole::getSourceTarget()
{
auto roomObj = getRoomObjectTarget();
if (!roomObj)
{
searchSource();
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());
searchSource();
return nullptr;
}
// Check if the source still has energy to harvest
if (source->energy() == 0)
{
searchSource();
return nullptr;
}
return std::move(source);
}
void DouwcoHivemind::HarvesterRole::searchSource()
{
target_id.clear();
auto sources = creep.room().find(Screeps::FIND_SOURCES_ACTIVE);
if (sources.size() == 0)
return;
Screeps::Source *selectedSource;
int maxEnergy = 0;
for (auto &sourceObj : sources)
{
auto source = dynamic_cast<Screeps::Source *>(sourceObj.get());
if (!source)
continue;
auto sourceEnergy = source->energy();
if (sourceEnergy > maxEnergy)
{
maxEnergy = sourceEnergy;
selectedSource = source;
}
}
if (!selectedSource)
{
// EM_ASM({console.log($0 + ': No sources with energy found!')}, creep.name().c_str());
return;
}
target_id = selectedSource->id();
}
void DouwcoHivemind::HarvesterRole::depositEnergy()
{
auto structure = getDepositTarget();
if (!structure)
return;
if (structure->structureType() == Screeps::STRUCTURE_CONTROLLER)
{
if (isNearTo(structure->pos(), 3))
{
auto controller = dynamic_cast<Screeps::StructureController *>(structure.get());
if (!controller)
return;
int resp = creep.upgradeController(*controller);
}
else
{
moveToTarget();
}
}
else
{
if (isNearTo(structure->pos(), 1))
{
int resp = creep.transfer(*structure, Screeps::RESOURCE_ENERGY);
}
else
{
moveToTarget();
}
}
}
std::unique_ptr<Screeps::Structure> DouwcoHivemind::HarvesterRole::getDepositTarget()
{
auto roomObj = getRoomObjectTarget();
if (!roomObj)
{
searchDeposit();
return nullptr;
}
// Check if found roomobject is an actual structure
auto structure = std::unique_ptr<Screeps::Structure>(dynamic_cast<Screeps::Structure *>(roomObj.release()));
if (!structure)
{
// EM_ASM({console.log($0 + ': Can\'t cast target to Source')}, creep.name().c_str());
searchDeposit();
return nullptr;
}
// Check if the structure can receive energy to harvest
int energyCapacity;
auto structureType = structure->structureType();
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();
}
else if (structureType == Screeps::STRUCTURE_EXTENSION)
{
auto extension = dynamic_cast<Screeps::StructureExtension *>(structure.get());
energyCapacity = extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
}
if (energyCapacity == 0)
{
searchDeposit();
return nullptr;
}
return std::move(structure);
}
void DouwcoHivemind::HarvesterRole::searchDeposit()
{
int highestEnergyNeed = 0;
Screeps::Structure *selectedStructure;
auto structures = creep.room().find(Screeps::FIND_MY_STRUCTURES);
for (auto &structureObject : structures)
{
auto structure = dynamic_cast<Screeps::Structure *>(structureObject.get());
if (!structure)
continue;
int energyNeed;
auto structureType = structure->structureType();
if (structureType == Screeps::STRUCTURE_SPAWN)
{
auto spawn = dynamic_cast<Screeps::StructureSpawn *>(structure);
energyNeed = spawn->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
}
else if (structureType == Screeps::STRUCTURE_EXTENSION)
{
auto extension = dynamic_cast<Screeps::StructureExtension *>(structure);
energyNeed = extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
}
else if (structureType == Screeps::STRUCTURE_CONTROLLER)
{
energyNeed = 1;
}
if (energyNeed > highestEnergyNeed)
{
highestEnergyNeed = energyNeed;
selectedStructure = structure;
}
}
if (selectedStructure)
target_id = selectedStructure->id();
else
target_id.clear();
}

View File

@@ -0,0 +1,32 @@
#include <Screeps/Context.hpp>
#include <Screeps/Creep.hpp>
#include <Screeps/StructureSpawn.hpp>
#include <emscripten.h>
#include <emscripten/bind.h>
#include <emscripten/val.h>
#include "Engine.hpp"
#include "Structures/Spawn.hpp"
EMSCRIPTEN_KEEPALIVE
extern "C" void loop()
{
Screeps::Context::update();
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;
engine.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"])));
}
EMSCRIPTEN_BINDINGS(loop)
{
emscripten::function("loop", &loop);
}

View File

@@ -0,0 +1,28 @@
#include <nlohmann/json.hpp>
#include <Screeps/JS.hpp>
#include <Screeps/Game.hpp>
#include <Screeps/Room.hpp>
#include <Screeps/StructureSpawn.hpp>
#include <emscripten.h>
#include "Creeps/Creep.hpp"
#include "Structures/Spawn.hpp"
void DouwcoHivemind::Spawn::loop()
{
int creepcount = spawn.room().find(Screeps::FIND_MY_CREEPS).size();
if (creepcount > 10)
{
EM_ASM({ console.log('To much creeps in this room'); });
return;
}
EM_ASM({ console.log('Creating a harvester'); });
JSON opts;
opts["memory"]["role"] = CreepRole::HARVESTER;
int resp = spawn.spawnCreep(
{"work", "carry", "move"},
"harvester" + std::to_string(Screeps::Game.time()),
opts);
}