Improved walking system.
This commit is contained in:
@@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
15
douwco_hivemind/include/Rooms/Room.hpp
Normal file
15
douwco_hivemind/include/Rooms/Room.hpp
Normal 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
|
||||
19
douwco_hivemind/include/Tools/MeasureTool.hpp
Normal file
19
douwco_hivemind/include/Tools/MeasureTool.hpp
Normal 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
|
||||
@@ -6,7 +6,6 @@
|
||||
|
||||
namespace DouwcoHivemind
|
||||
{
|
||||
|
||||
static std::vector<int> flattenPathSteps(const std::vector<Screeps::PathStep> &pathSteps)
|
||||
{
|
||||
std::vector<int> flattened;
|
||||
|
||||
139
douwco_hivemind/src/Creep.cpp
Normal file
139
douwco_hivemind/src/Creep.cpp
Normal file
@@ -0,0 +1,139 @@
|
||||
#include <algorithm>
|
||||
|
||||
#include <Screeps/Game.hpp>
|
||||
#include <Screeps/Room.hpp>
|
||||
#include <Screeps/RoomPosition.hpp>
|
||||
#include <Screeps/ReturnTypes.hpp>
|
||||
|
||||
#include "Creeps/Creep.hpp"
|
||||
|
||||
#include "Tools/JsonTool.hpp"
|
||||
#include "Tools/PathTool.hpp"
|
||||
#include "Tools/MeasureTool.hpp"
|
||||
|
||||
DouwcoHivemind::Creep::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();
|
||||
}
|
||||
|
||||
DouwcoHivemind::Creep::~Creep()
|
||||
{
|
||||
memory["target_id"] = target_id;
|
||||
creep.setMemory(memory);
|
||||
}
|
||||
|
||||
void DouwcoHivemind::Creep::moveToTarget(int dist)
|
||||
{
|
||||
// Is move required?
|
||||
auto target = getRoomObjectTarget();
|
||||
if (isNearTo(target->pos(), dist))
|
||||
{
|
||||
memory.erase("path");
|
||||
return;
|
||||
}
|
||||
|
||||
// Is wating?
|
||||
if (memory.contains("wait") && memory["wait"] > 0)
|
||||
{
|
||||
memory["wait"] = memory["wait"].get<int>() - 1;
|
||||
creep.say("Waiting..");
|
||||
return;
|
||||
}
|
||||
|
||||
// Is there a path to walk?
|
||||
if (!memory.contains("path") || memory["path"].empty())
|
||||
{
|
||||
creep.say("Searching route!");
|
||||
auto target_pos = target->pos();
|
||||
auto path = creep.room().findPath(creep.pos(), target_pos);
|
||||
auto last = path.back();
|
||||
if (last.x - target_pos.x() > dist || last.y - target_pos.y() > dist)
|
||||
{
|
||||
creep.say("No possible path");
|
||||
memory["wait"] = rand() % 20;
|
||||
return;
|
||||
}
|
||||
memory["path"] = vectorToJson(flattenPathSteps(path));
|
||||
}
|
||||
|
||||
// JS::console.log(std::string("creep pos: [") +
|
||||
// std::to_string(creep.pos().x()) +
|
||||
// std::string(",") +
|
||||
// std::to_string(creep.pos().y()) +
|
||||
// std::string("]"));
|
||||
|
||||
// Get step from memory
|
||||
int pathStepData[5] = {0};
|
||||
if (memory["path"].size() > 5)
|
||||
{
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
pathStepData[i] = memory["path"][i];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
memory.erase("path");
|
||||
return;
|
||||
}
|
||||
|
||||
// Is the move of last tick executed?
|
||||
int x = creep.pos().x();
|
||||
int y = creep.pos().y();
|
||||
|
||||
auto step = Screeps::PathStep(pathStepData[0], pathStepData[1], pathStepData[2], pathStepData[3], pathStepData[4]);
|
||||
|
||||
if (memory.contains("last_pos"))
|
||||
{
|
||||
int last_x = memory["last_pos"]["x"];
|
||||
int last_y = memory["last_pos"]["y"];
|
||||
memory.erase("last_pos");
|
||||
if (x == last_x && y == last_y)
|
||||
{
|
||||
creep.say("I'm stuck!");
|
||||
memory["wait"] = rand() % 5;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Is the creep on the place intended by the path?
|
||||
if (!(x == step.x - step.dx && y == step.y - step.dy))
|
||||
{
|
||||
creep.say("I'm lost!");
|
||||
memory["wait"] = rand() % 5;
|
||||
memory.erase("path");
|
||||
return;
|
||||
}
|
||||
|
||||
// Lets move forward
|
||||
int resp = creep.move(step.direction);
|
||||
if (resp == Screeps::OK)
|
||||
{
|
||||
memory["last_pos"]["x"] = x;
|
||||
memory["last_pos"]["y"] = y;
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
memory["path"].erase(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 DouwcoHivemind::isNearTo(creep.pos(), pos, dist);
|
||||
}
|
||||
48
douwco_hivemind/src/Engine.cpp
Normal file
48
douwco_hivemind/src/Engine.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <Screeps/Game.hpp>
|
||||
|
||||
#include "Engine.hpp"
|
||||
|
||||
#include "Creeps/Harvester.hpp"
|
||||
#include "Structures/Spawn.hpp"
|
||||
|
||||
DouwcoHivemind::Engine::Engine()
|
||||
{
|
||||
ReadOutCreeps();
|
||||
ReadOutStructures();
|
||||
}
|
||||
|
||||
void DouwcoHivemind::Engine::loop()
|
||||
{
|
||||
for (auto &creep : creeps)
|
||||
creep->loop();
|
||||
|
||||
for (auto &structure : structures)
|
||||
structure->loop();
|
||||
}
|
||||
|
||||
void DouwcoHivemind::Engine::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:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DouwcoHivemind::Engine::ReadOutStructures()
|
||||
{
|
||||
auto spawns = Screeps::Game.spawns();
|
||||
for (auto &spawn : spawns)
|
||||
{
|
||||
structures.push_back(std::make_unique<Spawn>(spawn.second));
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,18 @@
|
||||
|
||||
#include "Creeps/Harvester.hpp"
|
||||
|
||||
DouwcoHivemind::HarvesterRole::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;
|
||||
}
|
||||
|
||||
DouwcoHivemind::HarvesterRole::~HarvesterRole()
|
||||
{
|
||||
memory["harvesting"] = harvesting;
|
||||
memory["taskCounter"] = taskCounter;
|
||||
}
|
||||
|
||||
void DouwcoHivemind::HarvesterRole::loop()
|
||||
{
|
||||
if (harvesting)
|
||||
@@ -1,9 +1,5 @@
|
||||
#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"
|
||||
@@ -13,16 +9,14 @@ 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"},
|
||||
{"work", "work", "carry", "move"},
|
||||
"harvester" + std::to_string(Screeps::Game.time()),
|
||||
opts);
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
#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();
|
||||
}
|
||||
Reference in New Issue
Block a user