Improved walking system.
This commit is contained in:
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