Building Room based placement system.
This commit is contained in:
@@ -4,25 +4,32 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "Creeps/CreepBase.hpp"
|
#include "Creeps/CreepBase.hpp"
|
||||||
|
#include "Room.hpp"
|
||||||
#include "Structures/StructureBase.hpp"
|
#include "Structures/StructureBase.hpp"
|
||||||
|
|
||||||
namespace DouwcoHivemind
|
namespace DouwcoHivemind {
|
||||||
{
|
class Engine {
|
||||||
class Engine
|
private:
|
||||||
{
|
std::vector<std::unique_ptr<Room>> rooms;
|
||||||
private:
|
std::vector<std::unique_ptr<CreepBase>> creeps;
|
||||||
std::vector<std::unique_ptr<CreepBase>> creeps;
|
std::vector<std::unique_ptr<StructureBase>> structures;
|
||||||
std::vector<std::unique_ptr<StructureBase>> structures;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Engine();
|
// Readout data from screeps API into C++ vectors
|
||||||
void loop();
|
Engine();
|
||||||
|
// Loop over all rooms, screeps and structures
|
||||||
|
void loop();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void ReadOutCreeps();
|
// Readout player owned rooms
|
||||||
void ReadOutStructures();
|
void ReadOutRooms();
|
||||||
void clearDeadCreepMemory();
|
// 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
|
#endif // DOUWCO_HIVEMIND_ENGINE_HPP
|
||||||
31
douwco_hivemind/include/Room.hpp
Normal file
31
douwco_hivemind/include/Room.hpp
Normal 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
|
||||||
@@ -3,86 +3,88 @@
|
|||||||
|
|
||||||
#include "Engine.hpp"
|
#include "Engine.hpp"
|
||||||
|
|
||||||
#include "Creeps/Supplier.hpp"
|
|
||||||
#include "Creeps/Upgrader.hpp"
|
|
||||||
#include "Creeps/Builder.hpp"
|
#include "Creeps/Builder.hpp"
|
||||||
#include "Creeps/Maintainer.hpp"
|
#include "Creeps/Maintainer.hpp"
|
||||||
|
#include "Creeps/Supplier.hpp"
|
||||||
|
#include "Creeps/Upgrader.hpp"
|
||||||
|
|
||||||
#include "Structures/Spawn.hpp"
|
#include "Structures/Spawn.hpp"
|
||||||
|
|
||||||
DouwcoHivemind::Engine::Engine()
|
DouwcoHivemind::Engine::Engine() {
|
||||||
{
|
ReadOutRooms();
|
||||||
ReadOutCreeps();
|
ReadOutCreeps();
|
||||||
ReadOutStructures();
|
ReadOutStructures();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DouwcoHivemind::Engine::loop()
|
void DouwcoHivemind::Engine::loop() {
|
||||||
{
|
JS::console.log(std::string("Iterating over rooms"));
|
||||||
JS::console.log(std::string("Iterating over creeps"));
|
for (auto &room : rooms)
|
||||||
for (auto &creep : creeps)
|
room->loop();
|
||||||
creep->loop();
|
|
||||||
|
|
||||||
JS::console.log(std::string("Iterating over structures"));
|
JS::console.log(std::string("Iterating over creeps"));
|
||||||
for (auto &structure : structures)
|
for (auto &creep : creeps)
|
||||||
structure->loop();
|
creep->loop();
|
||||||
|
|
||||||
if (Screeps::Game.time() % 1000 == 0)
|
JS::console.log(std::string("Iterating over structures"));
|
||||||
{
|
for (auto &structure : structures)
|
||||||
clearDeadCreepMemory();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
if (!containsname)
|
||||||
|
creepMemory.erase(name);
|
||||||
void DouwcoHivemind::Engine::ReadOutCreeps()
|
}
|
||||||
{
|
Screeps::Memory.set("creeps", creepMemory);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|||||||
40
douwco_hivemind/src/Room.cpp
Normal file
40
douwco_hivemind/src/Room.cpp
Normal 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user