Created a base engine class and let it handle all creeps.
This commit is contained in:
15
.gitignore
vendored
15
.gitignore
vendored
@@ -1,14 +1 @@
|
|||||||
.vscode
|
build
|
||||||
|
|
||||||
CMakeLists.txt.user
|
|
||||||
CMakeCache.txt
|
|
||||||
CMakeFiles
|
|
||||||
CMakeScripts
|
|
||||||
Testing
|
|
||||||
Makefile
|
|
||||||
cmake_install.cmake
|
|
||||||
install_manifest.txt
|
|
||||||
compile_commands.json
|
|
||||||
CTestTestfile.cmake
|
|
||||||
_deps
|
|
||||||
CMakeUserPresets.json
|
|
||||||
100
.vscode/settings.json
vendored
Normal file
100
.vscode/settings.json
vendored
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
||||||
2
dist/app_loader.js
vendored
2
dist/app_loader.js
vendored
File diff suppressed because one or more lines are too long
BIN
dist/app_module.wasm
vendored
BIN
dist/app_module.wasm
vendored
Binary file not shown.
@@ -3,8 +3,7 @@
|
|||||||
|
|
||||||
namespace DouwcoHivemind{
|
namespace DouwcoHivemind{
|
||||||
|
|
||||||
// Roles
|
enum Roles{ UNEMPLOYED, HARVESTER };
|
||||||
const int ROLE_HARVESTER = 0;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
21
include/Creeps/Creep.hpp
Normal file
21
include/Creeps/Creep.hpp
Normal 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
|
||||||
@@ -3,18 +3,17 @@
|
|||||||
|
|
||||||
#include <Screeps/Creep.hpp>
|
#include <Screeps/Creep.hpp>
|
||||||
|
|
||||||
|
#include "Creeps/Creep.hpp"
|
||||||
|
|
||||||
namespace DouwcoHivemind
|
namespace DouwcoHivemind
|
||||||
{
|
{
|
||||||
class HarvesterRole
|
class HarvesterRole : public Creep
|
||||||
{
|
{
|
||||||
private:
|
|
||||||
Screeps::Creep *creep;
|
|
||||||
JSON memory;
|
|
||||||
public:
|
public:
|
||||||
HarvesterRole(Screeps::Creep* creep_pntr);
|
HarvesterRole(Screeps::Creep crp) : Creep(crp){}
|
||||||
~HarvesterRole();
|
~HarvesterRole(){}
|
||||||
|
|
||||||
void process();
|
void loop() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setupMemory();
|
void setupMemory();
|
||||||
58
include/Engine.hpp
Normal file
58
include/Engine.hpp
Normal 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
|
||||||
0
include/Structures/Structure.hpp
Normal file
0
include/Structures/Structure.hpp
Normal file
@@ -9,7 +9,7 @@ cd ..
|
|||||||
|
|
||||||
# Cmake
|
# Cmake
|
||||||
In screeps project run dit om cmake setup in te stellen:
|
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:
|
Run dit om de code te compilen:
|
||||||
cmake --build .
|
cmake --build .
|
||||||
@@ -16,22 +16,11 @@
|
|||||||
#include <Screeps/Constants.hpp>
|
#include <Screeps/Constants.hpp>
|
||||||
#include <Screeps/Store.hpp>
|
#include <Screeps/Store.hpp>
|
||||||
|
|
||||||
#include "creeps/harvester.hpp"
|
#include "Creeps/Harvester.hpp"
|
||||||
|
|
||||||
bool isNearTo(const Screeps::RoomPosition &pos1, const Screeps::RoomPosition &pos2);
|
bool isNearTo(const Screeps::RoomPosition &pos1, const Screeps::RoomPosition &pos2);
|
||||||
|
|
||||||
DouwcoHivemind::HarvesterRole::HarvesterRole(Screeps::Creep *creep_pntr)
|
void DouwcoHivemind::HarvesterRole::loop()
|
||||||
{
|
|
||||||
creep = creep_pntr;
|
|
||||||
memory = creep->memory();
|
|
||||||
}
|
|
||||||
|
|
||||||
DouwcoHivemind::HarvesterRole::~HarvesterRole()
|
|
||||||
{
|
|
||||||
creep->setMemory(memory);
|
|
||||||
}
|
|
||||||
|
|
||||||
void DouwcoHivemind::HarvesterRole::process()
|
|
||||||
{
|
{
|
||||||
setupMemory();
|
setupMemory();
|
||||||
searchTarget();
|
searchTarget();
|
||||||
@@ -60,13 +49,13 @@ void DouwcoHivemind::HarvesterRole::setupMemory()
|
|||||||
|
|
||||||
void DouwcoHivemind::HarvesterRole::searchTarget()
|
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'); });
|
EM_ASM({ console.log('Searching energy deposit'); });
|
||||||
memory["harvesting"] = false;
|
memory["harvesting"] = false;
|
||||||
searchEnergyDeposit();
|
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'); });
|
EM_ASM({ console.log('Searching source'); });
|
||||||
memory["harvesting"] = true;
|
memory["harvesting"] = true;
|
||||||
@@ -76,7 +65,7 @@ void DouwcoHivemind::HarvesterRole::searchTarget()
|
|||||||
|
|
||||||
void DouwcoHivemind::HarvesterRole::searchEnergyDeposit()
|
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;
|
std::vector<std::unique_ptr<Screeps::RoomObject>> filtered;
|
||||||
for (auto &structureObject : structures)
|
for (auto &structureObject : structures)
|
||||||
{
|
{
|
||||||
@@ -97,7 +86,7 @@ void DouwcoHivemind::HarvesterRole::searchEnergyDeposit()
|
|||||||
filtered.emplace_back(std::move(structureObject));
|
filtered.emplace_back(std::move(structureObject));
|
||||||
}
|
}
|
||||||
|
|
||||||
auto closestObj = creep->pos().findClosestByPath(filtered);
|
auto closestObj = creep.pos().findClosestByPath(filtered);
|
||||||
if (closestObj)
|
if (closestObj)
|
||||||
{
|
{
|
||||||
auto closest = dynamic_cast<Screeps::Structure *>(closestObj.get());
|
auto closest = dynamic_cast<Screeps::Structure *>(closestObj.get());
|
||||||
@@ -109,12 +98,12 @@ void DouwcoHivemind::HarvesterRole::searchEnergyDeposit()
|
|||||||
|
|
||||||
void DouwcoHivemind::HarvesterRole::searchSource()
|
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;
|
std::vector<std::unique_ptr<Screeps::RoomObject>> sourceObjects;
|
||||||
for (auto &source : sources)
|
for (auto &source : sources)
|
||||||
sourceObjects.emplace_back(std::move(source));
|
sourceObjects.emplace_back(std::move(source));
|
||||||
|
|
||||||
auto closestObj = creep->pos().findClosestByPath(sourceObjects);
|
auto closestObj = creep.pos().findClosestByPath(sourceObjects);
|
||||||
if (closestObj)
|
if (closestObj)
|
||||||
{
|
{
|
||||||
auto closest = dynamic_cast<Screeps::Source *>(closestObj.get());
|
auto closest = dynamic_cast<Screeps::Source *>(closestObj.get());
|
||||||
@@ -148,10 +137,10 @@ void DouwcoHivemind::HarvesterRole::harvestSource()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isNearTo(creep->pos(), source->pos()))
|
if (isNearTo(creep.pos(), source->pos()))
|
||||||
creep->harvest(*source);
|
creep.harvest(*source);
|
||||||
else
|
else
|
||||||
creep->moveTo(*source);
|
creep.moveTo(*source);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,14 +160,14 @@ void DouwcoHivemind::HarvesterRole::depositEnergy()
|
|||||||
bool searchAgain = !structure;
|
bool searchAgain = !structure;
|
||||||
if (!searchAgain)
|
if (!searchAgain)
|
||||||
{
|
{
|
||||||
if (isNearTo(creep->pos(), structure->pos()))
|
if (isNearTo(creep.pos(), structure->pos()))
|
||||||
{
|
{
|
||||||
if (structure->structureType() == Screeps::STRUCTURE_CONTROLLER)
|
if (structure->structureType() == Screeps::STRUCTURE_CONTROLLER)
|
||||||
{
|
{
|
||||||
Screeps::StructureController *controller = dynamic_cast<Screeps::StructureController *>(structure);
|
Screeps::StructureController *controller = dynamic_cast<Screeps::StructureController *>(structure);
|
||||||
if (!controller)
|
if (!controller)
|
||||||
return;
|
return;
|
||||||
int resp = creep->upgradeController(*controller);
|
int resp = creep.upgradeController(*controller);
|
||||||
if (resp != Screeps::OK)
|
if (resp != Screeps::OK)
|
||||||
searchAgain = true;
|
searchAgain = true;
|
||||||
}
|
}
|
||||||
@@ -186,13 +175,13 @@ void DouwcoHivemind::HarvesterRole::depositEnergy()
|
|||||||
structure->structureType() == Screeps::STRUCTURE_EXTENSION ||
|
structure->structureType() == Screeps::STRUCTURE_EXTENSION ||
|
||||||
structure->structureType() == Screeps::STRUCTURE_TOWER)
|
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)
|
if (resp != Screeps::OK)
|
||||||
searchAgain = true;
|
searchAgain = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
creep->moveTo(*structure);
|
creep.moveTo(*structure);
|
||||||
}
|
}
|
||||||
if (searchAgain)
|
if (searchAgain)
|
||||||
{
|
{
|
||||||
|
|||||||
19
src/loop.cpp
19
src/loop.cpp
@@ -7,14 +7,14 @@
|
|||||||
#include <emscripten/val.h>
|
#include <emscripten/val.h>
|
||||||
|
|
||||||
#include "Constants.hpp"
|
#include "Constants.hpp"
|
||||||
#include "creeps/harvester.hpp"
|
#include "Engine.hpp"
|
||||||
#include "structures/spawn.hpp"
|
#include "Structures/Spawn.hpp"
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE
|
EMSCRIPTEN_KEEPALIVE
|
||||||
extern "C" void loop()
|
extern "C" void loop()
|
||||||
{
|
{
|
||||||
Screeps::Context::update();
|
Screeps::Context::update();
|
||||||
|
EM_ASM({console.log('Starting loop: ')});
|
||||||
// Structures
|
// Structures
|
||||||
for (auto& spawn_entry : Screeps::Game.spawns())
|
for (auto& spawn_entry : Screeps::Game.spawns())
|
||||||
{
|
{
|
||||||
@@ -22,15 +22,10 @@ extern "C" void loop()
|
|||||||
spawn.process();
|
spawn.process();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creeps
|
DouwcoHivemind::Engine engine;
|
||||||
auto creeps = Screeps::Game.creeps();
|
engine.loop();
|
||||||
for (auto& creep : creeps){
|
|
||||||
if (creep.second.memory()["role"] == DouwcoHivemind::ROLE_HARVESTER)
|
EM_ASM({console.log('\n\n\n')});
|
||||||
{
|
|
||||||
auto harvester = DouwcoHivemind::HarvesterRole(&creep.second);
|
|
||||||
harvester.process();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EMSCRIPTEN_BINDINGS(loop)
|
EMSCRIPTEN_BINDINGS(loop)
|
||||||
|
|||||||
@@ -6,19 +6,23 @@
|
|||||||
#include <emscripten.h>
|
#include <emscripten.h>
|
||||||
|
|
||||||
#include "Constants.hpp"
|
#include "Constants.hpp"
|
||||||
#include "structures/spawn.hpp"
|
#include "Structures/Spawn.hpp"
|
||||||
|
|
||||||
void DouwcoHivemind::Spawn::process()
|
void DouwcoHivemind::Spawn::process()
|
||||||
{
|
{
|
||||||
int creepcount = structure->room().find(Screeps::FIND_MY_CREEPS).size();
|
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;
|
JSON opts;
|
||||||
opts["memory"]["role"] = DouwcoHivemind::ROLE_HARVESTER;
|
opts["memory"]["role"] = Roles::HARVESTER;
|
||||||
|
|
||||||
int resp = structure->spawnCreep(
|
int resp = structure->spawnCreep(
|
||||||
{"work", "carry", "move"},
|
{"work", "carry", "move"},
|
||||||
"harvester" + std::to_string(Screeps::Game.time()),
|
"harvester" + std::to_string(Screeps::Game.time()),
|
||||||
opts
|
opts);
|
||||||
);
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user