143 lines
3.8 KiB
C++
143 lines
3.8 KiB
C++
#include <algorithm>
|
|
|
|
#include <Screeps/Game.hpp>
|
|
#include <Screeps/Room.hpp>
|
|
#include <Screeps/RoomPosition.hpp>
|
|
|
|
#include "Creeps/CreepBase.hpp"
|
|
|
|
#include "Tools/JsonTool.hpp"
|
|
#include "Tools/PathTool.hpp"
|
|
#include "Tools/MeasureTool.hpp"
|
|
|
|
DouwcoHivemind::CreepBase::CreepBase(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::CreepBase::~CreepBase()
|
|
{
|
|
memory["target_id"] = target_id;
|
|
creep.setMemory(memory);
|
|
}
|
|
|
|
void DouwcoHivemind::CreepBase::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();
|
|
|
|
Screeps::Room::PathStep step;
|
|
step.x = pathStepData[0];
|
|
step.y = pathStepData[1];
|
|
step.dx = pathStepData[2];
|
|
step.dy = pathStepData[3];
|
|
step.direction = 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::CreepBase::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::CreepBase::isNearTo(const Screeps::RoomPosition &pos, int dist)
|
|
{
|
|
return DouwcoHivemind::isNearTo(creep.pos(), pos, dist);
|
|
} |