Files
screeps/douwco_hivemind/src/Engine.cpp

89 lines
2.3 KiB
C++

#include <Screeps/Game.hpp>
#include <Screeps/Memory.hpp>
#include "Engine.hpp"
#include "Creeps/Supplier.hpp"
#include "Creeps/Upgrader.hpp"
#include "Creeps/Builder.hpp"
#include "Creeps/Maintainer.hpp"
#include "Structures/Spawn.hpp"
DouwcoHivemind::Engine::Engine()
{
ReadOutCreeps();
ReadOutStructures();
}
void DouwcoHivemind::Engine::loop()
{
JS::console.log(std::string("Iterating over creeps"));
for (auto &creep : creeps)
creep->loop();
JS::console.log(std::string("Iterating over structures"));
for (auto &structure : structures)
structure->loop();
if (Screeps::Game.time() % 1000 == 0)
{
clearDeadCreepMemory();
}
}
void DouwcoHivemind::Engine::ReadOutCreeps()
{
JS::console.log(std::string("Reading out creeps"));
auto src_creeps = Screeps::Game.creeps();
for (auto &creep : src_creeps)
{
CreepRole role = creep.second.memory()["role"];
if (role == CreepRole::SUPPLIER)
creeps.push_back(std::make_unique<Supplier>(creep.second));
else if (role == CreepRole::UPGRADER)
creeps.push_back(std::make_unique<Upgrader>(creep.second));
else if (role == CreepRole::BUILDER)
creeps.push_back(std::make_unique<Builder>(creep.second));
else if (role == CreepRole::MAINTAINER)
creeps.push_back(std::make_unique<Maintainer>(creep.second));
}
}
void DouwcoHivemind::Engine::ReadOutStructures()
{
JS::console.log(std::string("Reading out structures"));
auto spawns = Screeps::Game.spawns();
for (auto &spawn : spawns)
{
structures.push_back(std::make_unique<Spawn>(spawn.second));
}
}
void DouwcoHivemind::Engine::clearDeadCreepMemory()
{
auto creepMemory = Screeps::Memory["creeps"];
auto creepsMap = Screeps::Game.creeps();
int iterator = 0;
for (auto [name, creep] : creepMemory.items())
{
// avoid cpu overload
iterator++;
if (iterator == 100)
break;
bool containsname = false;
for (auto creepObject : creepsMap)
{
if (creepObject.first == name)
{
containsname = true;
break;
}
}
if (!containsname)
creepMemory.erase(name);
}
Screeps::Memory.set("creeps", creepMemory);
}