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