Improved walking system.

This commit is contained in:
douwe
2025-08-24 12:08:22 +02:00
parent 98b123ee0d
commit cc0de10471
26 changed files with 858 additions and 154 deletions

View File

@@ -2,9 +2,12 @@
#define DOUWCO_HIVEMIND_CREEP_HPP
#include <Screeps/Creep.hpp>
#include <Screeps/ReturnTypes.hpp>
#include "Tools/JsonTool.hpp"
#include "Tools/PathTool.hpp"
namespace Screeps{
class RoomPosition;
class RoomObject;
class PathStep;
}
namespace DouwcoHivemind
{
@@ -13,39 +16,26 @@ namespace DouwcoHivemind
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);
}
Creep(Screeps::Creep crp);
virtual ~Creep();
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);
};
}

View File

@@ -1,10 +1,13 @@
#ifndef DOUWCO_HIVEMIND_HARVESTER_HPP
#define DOUWCO_HIVEMIND_HARVESTER_HPP
#include <Screeps/Creep.hpp>
#include "Creeps/Creep.hpp"
namespace {
class Creep;
}
namespace DouwcoHivemind
{
class HarvesterRole : public Creep
@@ -14,17 +17,8 @@ namespace DouwcoHivemind
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;
}
HarvesterRole(Screeps::Creep crp);
~HarvesterRole() override;
void loop() override;

View File

@@ -2,20 +2,12 @@
#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:
@@ -23,50 +15,12 @@ namespace DouwcoHivemind
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();
}
Engine();
void 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));
}
}
void ReadOutCreeps();
void ReadOutStructures();
};
}

View File

@@ -0,0 +1,15 @@
#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

@@ -0,0 +1,19 @@
#ifndef DOUWCO_HIVEMIND_MEASURE_TOOL_HPP
#define DOUWCO_HIVEMIND_MEASURE_TOOL_HPP
#include <Screeps/RoomPosition.hpp>
namespace DouwcoHivemind
{
static bool 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();
}
} // namespace Screeps
#endif // DOUWCO_HIVEMIND_MEASURE_TOOL_HPP

View File

@@ -6,7 +6,6 @@
namespace DouwcoHivemind
{
static std::vector<int> flattenPathSteps(const std::vector<Screeps::PathStep> &pathSteps)
{
std::vector<int> flattened;