Building Room based placement system.
This commit is contained in:
@@ -4,23 +4,30 @@
|
||||
#include <vector>
|
||||
|
||||
#include "Creeps/CreepBase.hpp"
|
||||
#include "Room.hpp"
|
||||
#include "Structures/StructureBase.hpp"
|
||||
|
||||
namespace DouwcoHivemind
|
||||
{
|
||||
class Engine
|
||||
{
|
||||
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:
|
||||
// Readout data from screeps API into C++ vectors
|
||||
Engine();
|
||||
// Loop over all rooms, screeps and structures
|
||||
void loop();
|
||||
|
||||
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();
|
||||
};
|
||||
}
|
||||
|
||||
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,21 +3,24 @@
|
||||
|
||||
#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()
|
||||
{
|
||||
DouwcoHivemind::Engine::Engine() {
|
||||
ReadOutRooms();
|
||||
ReadOutCreeps();
|
||||
ReadOutStructures();
|
||||
}
|
||||
|
||||
void DouwcoHivemind::Engine::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 creeps"));
|
||||
for (auto &creep : creeps)
|
||||
creep->loop();
|
||||
@@ -26,18 +29,23 @@ void DouwcoHivemind::Engine::loop()
|
||||
for (auto &structure : structures)
|
||||
structure->loop();
|
||||
|
||||
if (Screeps::Game.time() % 1000 == 0)
|
||||
{
|
||||
if (Screeps::Game.time() % 100 == 0) {
|
||||
clearDeadCreepMemory();
|
||||
}
|
||||
}
|
||||
|
||||
void DouwcoHivemind::Engine::ReadOutCreeps()
|
||||
{
|
||||
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)
|
||||
{
|
||||
for (auto &creep : src_creeps) {
|
||||
CreepRole role = creep.second.memory()["role"];
|
||||
if (role == CreepRole::SUPPLIER)
|
||||
creeps.push_back(std::make_unique<Supplier>(creep.second));
|
||||
@@ -50,33 +58,27 @@ void DouwcoHivemind::Engine::ReadOutCreeps()
|
||||
}
|
||||
}
|
||||
|
||||
void DouwcoHivemind::Engine::ReadOutStructures()
|
||||
{
|
||||
void DouwcoHivemind::Engine::ReadOutStructures() {
|
||||
JS::console.log(std::string("Reading out structures"));
|
||||
auto spawns = Screeps::Game.spawns();
|
||||
for (auto &spawn : spawns)
|
||||
{
|
||||
for (auto &spawn : spawns) {
|
||||
structures.push_back(std::make_unique<Spawn>(spawn.second));
|
||||
}
|
||||
}
|
||||
|
||||
void DouwcoHivemind::Engine::clearDeadCreepMemory()
|
||||
{
|
||||
void DouwcoHivemind::Engine::clearDeadCreepMemory() {
|
||||
auto creepMemory = Screeps::Memory["creeps"];
|
||||
auto creepsMap = Screeps::Game.creeps();
|
||||
int iterator = 0;
|
||||
for (auto [name, creep] : creepMemory.items())
|
||||
{
|
||||
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)
|
||||
{
|
||||
for (auto creepObject : creepsMap) {
|
||||
if (creepObject.first == name) {
|
||||
containsname = true;
|
||||
break;
|
||||
}
|
||||
|
||||
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