Creeps extract memory completly
This commit is contained in:
2
dist/douwco_hivemind_loader.js
vendored
2
dist/douwco_hivemind_loader.js
vendored
File diff suppressed because one or more lines are too long
BIN
dist/douwco_hivemind_module.wasm
vendored
BIN
dist/douwco_hivemind_module.wasm
vendored
Binary file not shown.
@@ -3,7 +3,7 @@
|
||||
|
||||
namespace DouwcoHivemind{
|
||||
|
||||
enum Roles{ UNEMPLOYED, HARVESTER };
|
||||
enum CreepRole{ UNEMPLOYED, HARVESTER };
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -2,19 +2,31 @@
|
||||
#define DOUWCO_HIVEMIND_CREEP_HPP
|
||||
|
||||
#include <Screeps/Creep.hpp>
|
||||
#include "Constants.hpp"
|
||||
|
||||
namespace DouwcoHivemind
|
||||
{
|
||||
class Creep
|
||||
{
|
||||
public:
|
||||
CreepRole role;
|
||||
|
||||
protected:
|
||||
Screeps::Creep creep;
|
||||
JSON memory;
|
||||
public:
|
||||
Creep(Screeps::Creep crp) : creep(crp), memory(crp.memory()){}
|
||||
~Creep(){ creep.setMemory(memory); }
|
||||
|
||||
virtual void loop(){}
|
||||
public:
|
||||
Creep(Screeps::Creep crp) : creep(crp),
|
||||
memory(crp.memory())
|
||||
{
|
||||
role = memory.contains("role") ? static_cast<CreepRole>(memory["role"]) : CreepRole::UNEMPLOYED;
|
||||
}
|
||||
|
||||
virtual ~Creep() {
|
||||
creep.setMemory(memory);
|
||||
}
|
||||
|
||||
virtual void loop() {}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -9,9 +9,25 @@ namespace DouwcoHivemind
|
||||
{
|
||||
class HarvesterRole : public Creep
|
||||
{
|
||||
private:
|
||||
bool harvesting;
|
||||
std::string target_id;
|
||||
int taskCounter;
|
||||
|
||||
public:
|
||||
HarvesterRole(Screeps::Creep crp) : Creep(crp) {}
|
||||
~HarvesterRole() {}
|
||||
HarvesterRole(Screeps::Creep crp) : Creep(crp)
|
||||
{
|
||||
harvesting = memory.contains("harvesting") ? static_cast<bool>(memory["harvesting"]) : false;
|
||||
target_id = memory.contains("target_id") ? static_cast<std::string>(memory["target_id"]) : std::string();
|
||||
taskCounter = memory.contains("taskCounter") ? static_cast<int>(memory["taskCounter"]) : 0;
|
||||
}
|
||||
|
||||
~HarvesterRole() override
|
||||
{
|
||||
memory["harvesting"] = harvesting;
|
||||
memory["target_id"] = target_id;
|
||||
memory["taskCounter"] = taskCounter;
|
||||
}
|
||||
|
||||
void loop() override;
|
||||
|
||||
|
||||
@@ -46,13 +46,13 @@ namespace DouwcoHivemind
|
||||
auto src_creeps = Screeps::Game.creeps();
|
||||
for (auto &creep : src_creeps)
|
||||
{
|
||||
Roles role = creep.second.memory()["role"];
|
||||
CreepRole role = creep.second.memory()["role"];
|
||||
switch (role)
|
||||
{
|
||||
case Roles::HARVESTER:
|
||||
case CreepRole::HARVESTER:
|
||||
creeps.push_back(std::make_unique<HarvesterRole>(creep.second));
|
||||
break;
|
||||
case Roles::UNEMPLOYED:
|
||||
case CreepRole::UNEMPLOYED:
|
||||
default:
|
||||
EM_ASM({console.log('Undefined role for creep' + $0)}, creep.first.c_str());
|
||||
break;
|
||||
|
||||
@@ -22,15 +22,12 @@ bool isNearTo(const Screeps::RoomPosition &pos1, const Screeps::RoomPosition &po
|
||||
|
||||
void DouwcoHivemind::HarvesterRole::loop()
|
||||
{
|
||||
if (!memory.contains("harvesting"))
|
||||
memory["harvesting"] = false;
|
||||
|
||||
if (memory["harvesting"])
|
||||
if (harvesting)
|
||||
{
|
||||
if (creep.store().getFreeCapacity(Screeps::RESOURCE_ENERGY) == 0)
|
||||
{
|
||||
memory["harvesting"] = false;
|
||||
memory["target"].clear();
|
||||
harvesting = false;
|
||||
target_id.clear();
|
||||
}
|
||||
harvestSource();
|
||||
}
|
||||
@@ -38,8 +35,8 @@ void DouwcoHivemind::HarvesterRole::loop()
|
||||
{
|
||||
if (creep.store().getUsedCapacity(Screeps::RESOURCE_ENERGY) == 0)
|
||||
{
|
||||
memory["harvesting"] = true;
|
||||
memory["target"].clear();
|
||||
harvesting = true;
|
||||
target_id.clear();
|
||||
}
|
||||
depositEnergy();
|
||||
}
|
||||
@@ -50,6 +47,7 @@ void DouwcoHivemind::HarvesterRole::harvestSource()
|
||||
auto source = getSourceTarget();
|
||||
if (!source)
|
||||
return;
|
||||
|
||||
if (isNearTo(creep.pos(), source->pos(), 1))
|
||||
{
|
||||
int resp = creep.harvest(*source);
|
||||
@@ -88,7 +86,7 @@ std::unique_ptr<Screeps::Source> DouwcoHivemind::HarvesterRole::getSourceTarget(
|
||||
|
||||
void DouwcoHivemind::HarvesterRole::searchSource()
|
||||
{
|
||||
memory["target"].clear();
|
||||
target_id.clear();
|
||||
|
||||
auto sources = creep.room().find(Screeps::FIND_SOURCES_ACTIVE);
|
||||
if (sources.size() == 0)
|
||||
@@ -115,7 +113,7 @@ void DouwcoHivemind::HarvesterRole::searchSource()
|
||||
// EM_ASM({console.log($0 + ': No sources with energy found!')}, creep.name().c_str());
|
||||
return;
|
||||
}
|
||||
memory["target"] = selectedSource->id();
|
||||
target_id = selectedSource->id();
|
||||
}
|
||||
|
||||
void DouwcoHivemind::HarvesterRole::depositEnergy()
|
||||
@@ -228,19 +226,15 @@ void DouwcoHivemind::HarvesterRole::searchDeposit()
|
||||
}
|
||||
|
||||
if (selectedStructure)
|
||||
memory["target"] = selectedStructure->id();
|
||||
target_id = selectedStructure->id();
|
||||
else
|
||||
memory["target"].clear();
|
||||
target_id.clear();
|
||||
}
|
||||
|
||||
std::unique_ptr<Screeps::RoomObject> DouwcoHivemind::HarvesterRole::getRoomObjectTarget()
|
||||
{
|
||||
// Check if target is still valid
|
||||
if (!memory.contains("target") || memory["target"].empty())
|
||||
return nullptr;
|
||||
|
||||
// Check if game can find target
|
||||
auto roomObj = Screeps::Game.getObjectById(memory["target"]);
|
||||
auto roomObj = Screeps::Game.getObjectById(target_id);
|
||||
if (!roomObj)
|
||||
{
|
||||
JS::console.log(creep.name() + ": Game can\'t find target id");
|
||||
|
||||
@@ -18,8 +18,10 @@ extern "C" void loop()
|
||||
JS::console.log(std::string("\n\n\n\n\n\n\n\n\n"));
|
||||
JS::console.log(std::string("Processing tick:\t") + std::to_string(Screeps::Game.time()));
|
||||
|
||||
DouwcoHivemind::Engine engine;
|
||||
engine.loop();
|
||||
{
|
||||
DouwcoHivemind::Engine engine;
|
||||
engine.loop();
|
||||
}
|
||||
|
||||
JS::console.log("Used CPU:\t" + std::to_string(Screeps::Game.cpuGetUsed()));
|
||||
JS::console.log("Bucket:\t" + std::to_string(static_cast<int>(Screeps::Game.cpu()["bucket"])));
|
||||
|
||||
@@ -19,7 +19,7 @@ void DouwcoHivemind::Spawn::loop()
|
||||
EM_ASM({ console.log('Creating a harvester'); });
|
||||
|
||||
JSON opts;
|
||||
opts["memory"]["role"] = Roles::HARVESTER;
|
||||
opts["memory"]["role"] = CreepRole::HARVESTER;
|
||||
|
||||
int resp = spawn.spawnCreep(
|
||||
{"work", "carry", "move"},
|
||||
|
||||
Reference in New Issue
Block a user