Added custom move behaviour to creeps
This commit is contained in:
52
douwco_hivemind/include/Creeps/Creep.hpp
Normal file
52
douwco_hivemind/include/Creeps/Creep.hpp
Normal 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
|
||||
42
douwco_hivemind/include/Creeps/Harvester.hpp
Normal file
42
douwco_hivemind/include/Creeps/Harvester.hpp
Normal 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
|
||||
73
douwco_hivemind/include/Engine.hpp
Normal file
73
douwco_hivemind/include/Engine.hpp
Normal 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
|
||||
24
douwco_hivemind/include/Structures/Spawn.hpp
Normal file
24
douwco_hivemind/include/Structures/Spawn.hpp
Normal 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
|
||||
15
douwco_hivemind/include/Structures/Structure.hpp
Normal file
15
douwco_hivemind/include/Structures/Structure.hpp
Normal 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
|
||||
33
douwco_hivemind/include/Tools/JsonTool.hpp
Normal file
33
douwco_hivemind/include/Tools/JsonTool.hpp
Normal 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
|
||||
35
douwco_hivemind/include/Tools/PathTool.hpp
Normal file
35
douwco_hivemind/include/Tools/PathTool.hpp
Normal 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
|
||||
Reference in New Issue
Block a user