Building Room based placement system.

This commit is contained in:
2026-06-18 01:12:09 +02:00
parent 2332978ab7
commit fe039cbdcf
4 changed files with 167 additions and 87 deletions

View File

@@ -4,25 +4,32 @@
#include <vector>
#include "Creeps/CreepBase.hpp"
#include "Room.hpp"
#include "Structures/StructureBase.hpp"
namespace DouwcoHivemind
{
class Engine
{
private:
std::vector<std::unique_ptr<CreepBase>> creeps;
std::vector<std::unique_ptr<StructureBase>> structures;
namespace DouwcoHivemind {
class Engine {
private:
std::vector<std::unique_ptr<Room>> rooms;
std::vector<std::unique_ptr<CreepBase>> creeps;
std::vector<std::unique_ptr<StructureBase>> structures;
public:
Engine();
void loop();
public:
// Readout data from screeps API into C++ vectors
Engine();
// Loop over all rooms, screeps and structures
void loop();
private:
void ReadOutCreeps();
void ReadOutStructures();
void clearDeadCreepMemory();
};
private:
// Readout player owned rooms
void ReadOutRooms();
// Readout player owned screeps and assign their roles
void ReadOutCreeps();
// Readout player owned buildings based on their structure type
void ReadOutStructures();
// Clear dead screep memory
void clearDeadCreepMemory();
};
}
#endif // DOUWCO_HIVEMIND_ENGINE_HPP

View File

@@ -0,0 +1,31 @@
#ifndef DOUWCO_HIVEMIND_ROOM_HPP
#define DOUWCO_HIVEMIND_ROOM_HPP
#include <Screeps/Room.hpp>
namespace DouwcoHivemind {
class Room {
protected:
enum ProgressState {
PLANNED,
IN_PROGRESS,
FINISHED
};
Screeps::Room room;
JSON memory;
ProgressState _sourceContainerState;
public:
Room(Screeps::Room rm);
~Room();
void loop();
protected:
bool assignConstructionSites();
};
} // namespace DouwcoHivemind
#endif // DOUWCO_HIVEMIND_ROOM_HPP

View File

@@ -3,86 +3,88 @@
#include "Engine.hpp"
#include "Creeps/Supplier.hpp"
#include "Creeps/Upgrader.hpp"
#include "Creeps/Builder.hpp"
#include "Creeps/Maintainer.hpp"
#include "Creeps/Supplier.hpp"
#include "Creeps/Upgrader.hpp"
#include "Structures/Spawn.hpp"
DouwcoHivemind::Engine::Engine()
{
ReadOutCreeps();
ReadOutStructures();
DouwcoHivemind::Engine::Engine() {
ReadOutRooms();
ReadOutCreeps();
ReadOutStructures();
}
void DouwcoHivemind::Engine::loop()
{
JS::console.log(std::string("Iterating over creeps"));
for (auto &creep : creeps)
creep->loop();
void DouwcoHivemind::Engine::loop() {
JS::console.log(std::string("Iterating over rooms"));
for (auto &room : rooms)
room->loop();
JS::console.log(std::string("Iterating over structures"));
for (auto &structure : structures)
structure->loop();
JS::console.log(std::string("Iterating over creeps"));
for (auto &creep : creeps)
creep->loop();
if (Screeps::Game.time() % 1000 == 0)
{
clearDeadCreepMemory();
JS::console.log(std::string("Iterating over structures"));
for (auto &structure : structures)
structure->loop();
if (Screeps::Game.time() % 100 == 0) {
clearDeadCreepMemory();
}
}
void DouwcoHivemind::Engine::ReadOutRooms() {
JS::console.log(std::string("Reading out owned rooms"));
auto src_rooms = Screeps::Game.rooms();
for (auto &room : src_rooms) {
rooms.push_back(std::make_unique<Room>(room.second));
}
}
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;
}
}
}
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);
if (!containsname)
creepMemory.erase(name);
}
Screeps::Memory.set("creeps", creepMemory);
}

View File

@@ -0,0 +1,40 @@
#include "Room.hpp"
#include "Screeps/Constants.hpp"
#include "Screeps/Room.hpp"
#include "Screeps/RoomObject.hpp"
#include "Screeps/RoomPosition.hpp"
DouwcoHivemind::Room::Room(Screeps::Room rm) : room(rm), memory(rm.memory()) {
_sourceContainerState =
memory.contains("_sourceContainerState")
? static_cast<ProgressState>(memory["_sourceContainerState"])
: ProgressState::PLANNED;
}
DouwcoHivemind::Room::~Room() {
memory["_sourceContainerState"] = _sourceContainerState;
}
void DouwcoHivemind::Room::loop() {
if (assignConstructionSites())
return;
}
bool DouwcoHivemind::Room::assignConstructionSites() {
if (_sourceContainerState == PLANNED) {
auto sources = room.find(Screeps::FIND_SOURCES);
for (auto &source : sources) {
int x = source->pos().x();
int y = source->pos().y();
for (int dx = -1; dx < 2; dx++) {
for (int dy = -1; dy < 2; dy++) {
if(dx == 0 && dy == 0) continue;
auto pos = room.getPositionAt(x + dx, y + dy);
}
}
}
} else if (_sourceContainerState == IN_PROGRESS) {
}
return false;
}