Created a base engine class and let it handle all creeps.

This commit is contained in:
douwe
2025-08-21 14:13:32 +02:00
parent c9ce09ba1a
commit 1e9e696b1e
16 changed files with 221 additions and 91 deletions

15
.gitignore vendored
View File

@@ -1,14 +1 @@
.vscode
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
CMakeUserPresets.json
build

100
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,100 @@
{
"files.associations": {
"__hash_table": "cpp",
"__string": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"locale": "cpp",
"utility": "cpp",
"mutex": "cpp",
"shared_mutex": "cpp",
"stop_token": "cpp",
"cassert": "cpp",
"vector": "cpp",
"optional": "cpp",
"*.inc": "cpp",
"thread": "cpp",
"atomic": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__functional_03": "cpp",
"__mutex_base": "cpp",
"__nullptr": "cpp",
"__split_buffer": "cpp",
"__tree": "cpp",
"any": "cpp",
"array": "cpp",
"bit": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"compare": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"exception": "cpp",
"filesystem": "cpp",
"forward_list": "cpp",
"iterator": "cpp",
"list": "cpp",
"map": "cpp",
"memory_resource": "cpp",
"regex": "cpp",
"set": "cpp",
"string": "cpp",
"type_traits": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"fstream": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"memory": "cpp",
"new": "cpp",
"numeric": "cpp",
"ostream": "cpp",
"queue": "cpp",
"random": "cpp",
"ratio": "cpp",
"span": "cpp",
"sstream": "cpp",
"stack": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string_view": "cpp",
"strstream": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"typeinfo": "cpp",
"valarray": "cpp",
"variant": "cpp",
"cerrno": "cpp",
"cfloat": "cpp",
"climits": "cpp",
"concepts": "cpp",
"format": "cpp",
"numbers": "cpp",
"semaphore": "cpp",
"cfenv": "cpp",
"cinttypes": "cpp"
}
}

22
app.js

File diff suppressed because one or more lines are too long

BIN
app.wasm

Binary file not shown.

2
dist/app_loader.js vendored

File diff suppressed because one or more lines are too long

BIN
dist/app_module.wasm vendored

Binary file not shown.

View File

@@ -3,8 +3,7 @@
namespace DouwcoHivemind{
// Roles
const int ROLE_HARVESTER = 0;
enum Roles{ UNEMPLOYED, HARVESTER };
}

21
include/Creeps/Creep.hpp Normal file
View File

@@ -0,0 +1,21 @@
#ifndef DOUWCO_HIVEMIND_CREEP_HPP
#define DOUWCO_HIVEMIND_CREEP_HPP
#include <Screeps/Creep.hpp>
namespace DouwcoHivemind
{
class Creep
{
protected:
Screeps::Creep creep;
JSON memory;
public:
Creep(Screeps::Creep crp) : creep(crp), memory(crp.memory()){}
~Creep(){ creep.setMemory(memory); }
virtual void loop(){}
};
}
#endif // DOUWCO_HIVEMIND_CREEP_HPP

View File

@@ -3,18 +3,17 @@
#include <Screeps/Creep.hpp>
#include "Creeps/Creep.hpp"
namespace DouwcoHivemind
{
class HarvesterRole
class HarvesterRole : public Creep
{
private:
Screeps::Creep *creep;
JSON memory;
public:
HarvesterRole(Screeps::Creep* creep_pntr);
~HarvesterRole();
HarvesterRole(Screeps::Creep crp) : Creep(crp){}
~HarvesterRole(){}
void process();
void loop() override;
private:
void setupMemory();

58
include/Engine.hpp Normal file
View File

@@ -0,0 +1,58 @@
#ifndef DOUWCO_HIVEMIND_ENGINE_HPP
#define DOUWCO_HIVEMIND_ENGINE_HPP
#include <vector>
#include <Screeps/JS.hpp>
#include <Screeps/Game.hpp>
#include <Screeps/Creep.hpp>
#include "Constants.hpp"
#include "Creeps/Harvester.hpp"
namespace DouwcoHivemind
{
class Creep;
class Structure;
class Engine
{
private:
std::vector<std::unique_ptr<Creep>> creeps;
// std::vector<std::unique_ptr<Structure>> structure;
public:
Engine()
{
ReadOutCreeps();
}
~Engine() {}
void loop() {
for(auto &creep : creeps){
creep->loop();
}
}
private:
void ReadOutCreeps()
{
auto src_creeps = Screeps::Game.creeps();
for (auto &creep : src_creeps)
{
Roles role = creep.second.memory()["role"];
switch (role)
{
case Roles::HARVESTER:
creeps.push_back(std::make_unique<HarvesterRole>(creep.second));
break;
case Roles::UNEMPLOYED:
default:
EM_ASM({console.log('Undefined role for creep' + $0)}, creep.first.c_str());
break;
}
}
}
};
}
#endif // DOUWCO_HIVEMIND_ENGINE_HPP

View File

View File

@@ -9,7 +9,7 @@ cd ..
# Cmake
In screeps project run dit om cmake setup in te stellen:
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake .
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake ..
Run dit om de code te compilen:
cmake --build .

View File

@@ -16,22 +16,11 @@
#include <Screeps/Constants.hpp>
#include <Screeps/Store.hpp>
#include "creeps/harvester.hpp"
#include "Creeps/Harvester.hpp"
bool isNearTo(const Screeps::RoomPosition &pos1, const Screeps::RoomPosition &pos2);
DouwcoHivemind::HarvesterRole::HarvesterRole(Screeps::Creep *creep_pntr)
{
creep = creep_pntr;
memory = creep->memory();
}
DouwcoHivemind::HarvesterRole::~HarvesterRole()
{
creep->setMemory(memory);
}
void DouwcoHivemind::HarvesterRole::process()
void DouwcoHivemind::HarvesterRole::loop()
{
setupMemory();
searchTarget();
@@ -60,13 +49,13 @@ void DouwcoHivemind::HarvesterRole::setupMemory()
void DouwcoHivemind::HarvesterRole::searchTarget()
{
if (memory["harvesting"] && creep->store().getFreeCapacity(Screeps::RESOURCE_ENERGY) == 0)
if (memory["harvesting"] && creep.store().getFreeCapacity(Screeps::RESOURCE_ENERGY) == 0)
{
EM_ASM({ console.log('Searching energy deposit'); });
memory["harvesting"] = false;
searchEnergyDeposit();
}
else if (!memory["harvesting"] && creep->store().getUsedCapacity(Screeps::RESOURCE_ENERGY) == 0)
else if (!memory["harvesting"] && creep.store().getUsedCapacity(Screeps::RESOURCE_ENERGY) == 0)
{
EM_ASM({ console.log('Searching source'); });
memory["harvesting"] = true;
@@ -76,7 +65,7 @@ void DouwcoHivemind::HarvesterRole::searchTarget()
void DouwcoHivemind::HarvesterRole::searchEnergyDeposit()
{
auto structures = creep->room().find(Screeps::FIND_MY_STRUCTURES);
auto structures = creep.room().find(Screeps::FIND_MY_STRUCTURES);
std::vector<std::unique_ptr<Screeps::RoomObject>> filtered;
for (auto &structureObject : structures)
{
@@ -97,7 +86,7 @@ void DouwcoHivemind::HarvesterRole::searchEnergyDeposit()
filtered.emplace_back(std::move(structureObject));
}
auto closestObj = creep->pos().findClosestByPath(filtered);
auto closestObj = creep.pos().findClosestByPath(filtered);
if (closestObj)
{
auto closest = dynamic_cast<Screeps::Structure *>(closestObj.get());
@@ -109,12 +98,12 @@ void DouwcoHivemind::HarvesterRole::searchEnergyDeposit()
void DouwcoHivemind::HarvesterRole::searchSource()
{
auto sources = creep->room().find(Screeps::FIND_SOURCES_ACTIVE);
auto sources = creep.room().find(Screeps::FIND_SOURCES_ACTIVE);
std::vector<std::unique_ptr<Screeps::RoomObject>> sourceObjects;
for (auto &source : sources)
sourceObjects.emplace_back(std::move(source));
auto closestObj = creep->pos().findClosestByPath(sourceObjects);
auto closestObj = creep.pos().findClosestByPath(sourceObjects);
if (closestObj)
{
auto closest = dynamic_cast<Screeps::Source *>(closestObj.get());
@@ -148,10 +137,10 @@ void DouwcoHivemind::HarvesterRole::harvestSource()
return;
}
if (isNearTo(creep->pos(), source->pos()))
creep->harvest(*source);
if (isNearTo(creep.pos(), source->pos()))
creep.harvest(*source);
else
creep->moveTo(*source);
creep.moveTo(*source);
}
}
@@ -171,14 +160,14 @@ void DouwcoHivemind::HarvesterRole::depositEnergy()
bool searchAgain = !structure;
if (!searchAgain)
{
if (isNearTo(creep->pos(), structure->pos()))
if (isNearTo(creep.pos(), structure->pos()))
{
if (structure->structureType() == Screeps::STRUCTURE_CONTROLLER)
{
Screeps::StructureController *controller = dynamic_cast<Screeps::StructureController *>(structure);
if (!controller)
return;
int resp = creep->upgradeController(*controller);
int resp = creep.upgradeController(*controller);
if (resp != Screeps::OK)
searchAgain = true;
}
@@ -186,13 +175,13 @@ void DouwcoHivemind::HarvesterRole::depositEnergy()
structure->structureType() == Screeps::STRUCTURE_EXTENSION ||
structure->structureType() == Screeps::STRUCTURE_TOWER)
{
int resp = creep->transfer(*structure, Screeps::RESOURCE_ENERGY);
int resp = creep.transfer(*structure, Screeps::RESOURCE_ENERGY);
if (resp != Screeps::OK)
searchAgain = true;
}
}
else
creep->moveTo(*structure);
creep.moveTo(*structure);
}
if (searchAgain)
{

View File

@@ -7,30 +7,25 @@
#include <emscripten/val.h>
#include "Constants.hpp"
#include "creeps/harvester.hpp"
#include "structures/spawn.hpp"
#include "Engine.hpp"
#include "Structures/Spawn.hpp"
EMSCRIPTEN_KEEPALIVE
extern "C" void loop()
{
Screeps::Context::update();
EM_ASM({console.log('Starting loop: ')});
// Structures
for (auto& spawn_entry : Screeps::Game.spawns())
{
auto spawn = DouwcoHivemind::Spawn(&spawn_entry.second);
spawn.process();
}
DouwcoHivemind::Engine engine;
engine.loop();
// Creeps
auto creeps = Screeps::Game.creeps();
for (auto& creep : creeps){
if (creep.second.memory()["role"] == DouwcoHivemind::ROLE_HARVESTER)
{
auto harvester = DouwcoHivemind::HarvesterRole(&creep.second);
harvester.process();
}
}
EM_ASM({console.log('\n\n\n')});
}
EMSCRIPTEN_BINDINGS(loop)

View File

@@ -6,19 +6,23 @@
#include <emscripten.h>
#include "Constants.hpp"
#include "structures/spawn.hpp"
#include "Structures/Spawn.hpp"
void DouwcoHivemind::Spawn::process()
{
int creepcount = structure->room().find(Screeps::FIND_MY_CREEPS).size();
if (creepcount > 10) return;
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"] = DouwcoHivemind::ROLE_HARVESTER;
opts["memory"]["role"] = Roles::HARVESTER;
int resp = structure->spawnCreep(
{"work", "carry", "move"},
{"work", "carry", "move"},
"harvester" + std::to_string(Screeps::Game.time()),
opts
);
opts);
}