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{
|
namespace DouwcoHivemind{
|
||||||
|
|
||||||
enum Roles{ UNEMPLOYED, HARVESTER };
|
enum CreepRole{ UNEMPLOYED, HARVESTER };
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,17 +2,29 @@
|
|||||||
#define DOUWCO_HIVEMIND_CREEP_HPP
|
#define DOUWCO_HIVEMIND_CREEP_HPP
|
||||||
|
|
||||||
#include <Screeps/Creep.hpp>
|
#include <Screeps/Creep.hpp>
|
||||||
|
#include "Constants.hpp"
|
||||||
|
|
||||||
namespace DouwcoHivemind
|
namespace DouwcoHivemind
|
||||||
{
|
{
|
||||||
class Creep
|
class Creep
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
CreepRole role;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Screeps::Creep creep;
|
Screeps::Creep creep;
|
||||||
JSON memory;
|
JSON memory;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Creep(Screeps::Creep crp) : creep(crp), memory(crp.memory()){}
|
Creep(Screeps::Creep crp) : creep(crp),
|
||||||
~Creep(){ creep.setMemory(memory); }
|
memory(crp.memory())
|
||||||
|
{
|
||||||
|
role = memory.contains("role") ? static_cast<CreepRole>(memory["role"]) : CreepRole::UNEMPLOYED;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~Creep() {
|
||||||
|
creep.setMemory(memory);
|
||||||
|
}
|
||||||
|
|
||||||
virtual void loop() {}
|
virtual void loop() {}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,9 +9,25 @@ namespace DouwcoHivemind
|
|||||||
{
|
{
|
||||||
class HarvesterRole : public Creep
|
class HarvesterRole : public Creep
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
bool harvesting;
|
||||||
|
std::string target_id;
|
||||||
|
int taskCounter;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
HarvesterRole(Screeps::Creep crp) : Creep(crp) {}
|
HarvesterRole(Screeps::Creep crp) : Creep(crp)
|
||||||
~HarvesterRole() {}
|
{
|
||||||
|
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;
|
void loop() override;
|
||||||
|
|
||||||
|
|||||||
@@ -46,13 +46,13 @@ namespace DouwcoHivemind
|
|||||||
auto src_creeps = Screeps::Game.creeps();
|
auto src_creeps = Screeps::Game.creeps();
|
||||||
for (auto &creep : src_creeps)
|
for (auto &creep : src_creeps)
|
||||||
{
|
{
|
||||||
Roles role = creep.second.memory()["role"];
|
CreepRole role = creep.second.memory()["role"];
|
||||||
switch (role)
|
switch (role)
|
||||||
{
|
{
|
||||||
case Roles::HARVESTER:
|
case CreepRole::HARVESTER:
|
||||||
creeps.push_back(std::make_unique<HarvesterRole>(creep.second));
|
creeps.push_back(std::make_unique<HarvesterRole>(creep.second));
|
||||||
break;
|
break;
|
||||||
case Roles::UNEMPLOYED:
|
case CreepRole::UNEMPLOYED:
|
||||||
default:
|
default:
|
||||||
EM_ASM({console.log('Undefined role for creep' + $0)}, creep.first.c_str());
|
EM_ASM({console.log('Undefined role for creep' + $0)}, creep.first.c_str());
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -22,15 +22,12 @@ bool isNearTo(const Screeps::RoomPosition &pos1, const Screeps::RoomPosition &po
|
|||||||
|
|
||||||
void DouwcoHivemind::HarvesterRole::loop()
|
void DouwcoHivemind::HarvesterRole::loop()
|
||||||
{
|
{
|
||||||
if (!memory.contains("harvesting"))
|
if (harvesting)
|
||||||
memory["harvesting"] = false;
|
|
||||||
|
|
||||||
if (memory["harvesting"])
|
|
||||||
{
|
{
|
||||||
if (creep.store().getFreeCapacity(Screeps::RESOURCE_ENERGY) == 0)
|
if (creep.store().getFreeCapacity(Screeps::RESOURCE_ENERGY) == 0)
|
||||||
{
|
{
|
||||||
memory["harvesting"] = false;
|
harvesting = false;
|
||||||
memory["target"].clear();
|
target_id.clear();
|
||||||
}
|
}
|
||||||
harvestSource();
|
harvestSource();
|
||||||
}
|
}
|
||||||
@@ -38,8 +35,8 @@ void DouwcoHivemind::HarvesterRole::loop()
|
|||||||
{
|
{
|
||||||
if (creep.store().getUsedCapacity(Screeps::RESOURCE_ENERGY) == 0)
|
if (creep.store().getUsedCapacity(Screeps::RESOURCE_ENERGY) == 0)
|
||||||
{
|
{
|
||||||
memory["harvesting"] = true;
|
harvesting = true;
|
||||||
memory["target"].clear();
|
target_id.clear();
|
||||||
}
|
}
|
||||||
depositEnergy();
|
depositEnergy();
|
||||||
}
|
}
|
||||||
@@ -50,6 +47,7 @@ void DouwcoHivemind::HarvesterRole::harvestSource()
|
|||||||
auto source = getSourceTarget();
|
auto source = getSourceTarget();
|
||||||
if (!source)
|
if (!source)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (isNearTo(creep.pos(), source->pos(), 1))
|
if (isNearTo(creep.pos(), source->pos(), 1))
|
||||||
{
|
{
|
||||||
int resp = creep.harvest(*source);
|
int resp = creep.harvest(*source);
|
||||||
@@ -88,7 +86,7 @@ std::unique_ptr<Screeps::Source> DouwcoHivemind::HarvesterRole::getSourceTarget(
|
|||||||
|
|
||||||
void DouwcoHivemind::HarvesterRole::searchSource()
|
void DouwcoHivemind::HarvesterRole::searchSource()
|
||||||
{
|
{
|
||||||
memory["target"].clear();
|
target_id.clear();
|
||||||
|
|
||||||
auto sources = creep.room().find(Screeps::FIND_SOURCES_ACTIVE);
|
auto sources = creep.room().find(Screeps::FIND_SOURCES_ACTIVE);
|
||||||
if (sources.size() == 0)
|
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());
|
// EM_ASM({console.log($0 + ': No sources with energy found!')}, creep.name().c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
memory["target"] = selectedSource->id();
|
target_id = selectedSource->id();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DouwcoHivemind::HarvesterRole::depositEnergy()
|
void DouwcoHivemind::HarvesterRole::depositEnergy()
|
||||||
@@ -228,19 +226,15 @@ void DouwcoHivemind::HarvesterRole::searchDeposit()
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (selectedStructure)
|
if (selectedStructure)
|
||||||
memory["target"] = selectedStructure->id();
|
target_id = selectedStructure->id();
|
||||||
else
|
else
|
||||||
memory["target"].clear();
|
target_id.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Screeps::RoomObject> DouwcoHivemind::HarvesterRole::getRoomObjectTarget()
|
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
|
// Check if game can find target
|
||||||
auto roomObj = Screeps::Game.getObjectById(memory["target"]);
|
auto roomObj = Screeps::Game.getObjectById(target_id);
|
||||||
if (!roomObj)
|
if (!roomObj)
|
||||||
{
|
{
|
||||||
JS::console.log(creep.name() + ": Game can\'t find target id");
|
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("\n\n\n\n\n\n\n\n\n"));
|
||||||
JS::console.log(std::string("Processing tick:\t") + std::to_string(Screeps::Game.time()));
|
JS::console.log(std::string("Processing tick:\t") + std::to_string(Screeps::Game.time()));
|
||||||
|
|
||||||
|
{
|
||||||
DouwcoHivemind::Engine engine;
|
DouwcoHivemind::Engine engine;
|
||||||
engine.loop();
|
engine.loop();
|
||||||
|
}
|
||||||
|
|
||||||
JS::console.log("Used CPU:\t" + std::to_string(Screeps::Game.cpuGetUsed()));
|
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"])));
|
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'); });
|
EM_ASM({ console.log('Creating a harvester'); });
|
||||||
|
|
||||||
JSON opts;
|
JSON opts;
|
||||||
opts["memory"]["role"] = Roles::HARVESTER;
|
opts["memory"]["role"] = CreepRole::HARVESTER;
|
||||||
|
|
||||||
int resp = spawn.spawnCreep(
|
int resp = spawn.spawnCreep(
|
||||||
{"work", "carry", "move"},
|
{"work", "carry", "move"},
|
||||||
|
|||||||
Reference in New Issue
Block a user