Implemented auto building of containers.
This commit is contained in:
@@ -2,8 +2,8 @@ cmake_minimum_required(VERSION 3.16)
|
|||||||
|
|
||||||
project(douwco_hivemind CXX)
|
project(douwco_hivemind CXX)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
|
set(CMAKE_CXX_STANDARD_REQUIRED On)
|
||||||
|
|
||||||
# Generate compile commands
|
# Generate compile commands
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|||||||
@@ -2,20 +2,15 @@
|
|||||||
#define DOUWCO_HIVEMIND_ROOM_HPP
|
#define DOUWCO_HIVEMIND_ROOM_HPP
|
||||||
|
|
||||||
#include <Screeps/Room.hpp>
|
#include <Screeps/Room.hpp>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace DouwcoHivemind {
|
namespace DouwcoHivemind {
|
||||||
class Room {
|
class Room {
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
enum ProgressState {
|
|
||||||
PLANNED,
|
|
||||||
IN_PROGRESS,
|
|
||||||
FINISHED
|
|
||||||
};
|
|
||||||
|
|
||||||
Screeps::Room room;
|
Screeps::Room room;
|
||||||
JSON memory;
|
JSON memory;
|
||||||
ProgressState _sourceContainerState;
|
std::vector<std::string> sourceContainers = {};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Room(Screeps::Room rm);
|
Room(Screeps::Room rm);
|
||||||
@@ -24,7 +19,7 @@ public:
|
|||||||
void loop();
|
void loop();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool assignConstructionSites();
|
bool placeContainers();
|
||||||
};
|
};
|
||||||
} // namespace DouwcoHivemind
|
} // namespace DouwcoHivemind
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,6 @@ std::unique_ptr<Screeps::ConstructionSite> DouwcoHivemind::Builder::getConstruct
|
|||||||
if (!roomObj)
|
if (!roomObj)
|
||||||
{
|
{
|
||||||
searchConstructionSite();
|
searchConstructionSite();
|
||||||
if(target_id.empty()) creep.move(rand()%5);
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ std::unique_ptr<Screeps::Structure> DouwcoHivemind::Maintainer::getDamagedStruct
|
|||||||
if (!roomObj)
|
if (!roomObj)
|
||||||
{
|
{
|
||||||
searchDamagedStructure();
|
searchDamagedStructure();
|
||||||
if(target_id.empty()) creep.move(rand()%5);
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ std::unique_ptr<Screeps::Structure> DouwcoHivemind::Supplier::getEnergyStructure
|
|||||||
if (!roomObj)
|
if (!roomObj)
|
||||||
{
|
{
|
||||||
searchEnergyStructure();
|
searchEnergyStructure();
|
||||||
if(target_id.empty()) creep.move(rand()%5);
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include <Screeps/Game.hpp>
|
#include <Screeps/Game.hpp>
|
||||||
#include <Screeps/Memory.hpp>
|
#include <Screeps/Memory.hpp>
|
||||||
|
#include <Screeps/StructureController.hpp>
|
||||||
|
|
||||||
#include "Engine.hpp"
|
#include "Engine.hpp"
|
||||||
|
|
||||||
@@ -38,6 +39,7 @@ void DouwcoHivemind::Engine::ReadOutRooms() {
|
|||||||
JS::console.log(std::string("Reading out owned rooms"));
|
JS::console.log(std::string("Reading out owned rooms"));
|
||||||
auto src_rooms = Screeps::Game.rooms();
|
auto src_rooms = Screeps::Game.rooms();
|
||||||
for (auto &room : src_rooms) {
|
for (auto &room : src_rooms) {
|
||||||
|
if(!room.second.controller().value().my()) continue;
|
||||||
rooms.push_back(std::make_unique<Room>(room.second));
|
rooms.push_back(std::make_unique<Room>(room.second));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,37 +4,85 @@
|
|||||||
#include "Screeps/Room.hpp"
|
#include "Screeps/Room.hpp"
|
||||||
#include "Screeps/RoomObject.hpp"
|
#include "Screeps/RoomObject.hpp"
|
||||||
#include "Screeps/RoomPosition.hpp"
|
#include "Screeps/RoomPosition.hpp"
|
||||||
|
#include "Screeps/RoomTerrain.hpp"
|
||||||
|
#include "Screeps/Structure.hpp"
|
||||||
|
#include "Screeps/StructureContainer.hpp"
|
||||||
|
#include "Screeps/StructureController.hpp"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <memory>
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
DouwcoHivemind::Room::Room(Screeps::Room rm) : room(rm), memory(rm.memory()) {
|
DouwcoHivemind::Room::Room(Screeps::Room rm) : room(rm), memory(rm.memory()) {
|
||||||
_sourceContainerState =
|
if (memory.contains("sourceContainers"))
|
||||||
memory.contains("_sourceContainerState")
|
sourceContainers =
|
||||||
? static_cast<ProgressState>(memory["_sourceContainerState"])
|
static_cast<std::vector<std::string>>(memory["sourceContainers"]);
|
||||||
: ProgressState::PLANNED;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DouwcoHivemind::Room::~Room() {
|
DouwcoHivemind::Room::~Room() {
|
||||||
memory["_sourceContainerState"] = _sourceContainerState;
|
memory["sourceContainers"] = sourceContainers;
|
||||||
|
room.setMemory(memory);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DouwcoHivemind::Room::loop() {
|
void DouwcoHivemind::Room::loop() {
|
||||||
if (assignConstructionSites())
|
if (placeContainers())
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DouwcoHivemind::Room::assignConstructionSites() {
|
bool DouwcoHivemind::Room::placeContainers() {
|
||||||
if (_sourceContainerState == PLANNED) {
|
|
||||||
auto sources = room.find(Screeps::FIND_SOURCES);
|
auto sources = room.find(Screeps::FIND_SOURCES);
|
||||||
|
|
||||||
|
// No containers planned or build, planning now
|
||||||
|
if (sourceContainers.size() == 0) {
|
||||||
|
int controller_x = room.controller().value().pos().x();
|
||||||
|
int controller_y = room.controller().value().pos().y();
|
||||||
|
|
||||||
for (auto &source : sources) {
|
for (auto &source : sources) {
|
||||||
int x = source->pos().x();
|
int source_x = source->pos().x();
|
||||||
int y = source->pos().y();
|
int source_y = source->pos().y();
|
||||||
|
|
||||||
|
int lowest_cost = 1000;
|
||||||
|
int best_x;
|
||||||
|
int best_y;
|
||||||
for (int dx = -1; dx < 2; dx++) {
|
for (int dx = -1; dx < 2; dx++) {
|
||||||
for (int dy = -1; dy < 2; dy++) {
|
for (int dy = -1; dy < 2; dy++) {
|
||||||
if(dx == 0 && dy == 0) continue;
|
if (dx == 0 && dy == 0)
|
||||||
auto pos = room.getPositionAt(x + dx, y + dy);
|
continue;
|
||||||
|
int x = source_x + dx;
|
||||||
|
int y = source_y + dy;
|
||||||
|
int terrainType = room.getTerrain().get(x, y);
|
||||||
|
if (terrainType != Screeps::TERRAIN_MASK_WALL) {
|
||||||
|
int cost = std::abs(x - controller_x) + std::abs(y - controller_y);
|
||||||
|
if (cost < lowest_cost) {
|
||||||
|
lowest_cost = cost;
|
||||||
|
best_x = x;
|
||||||
|
best_y = y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (_sourceContainerState == IN_PROGRESS) {
|
}
|
||||||
|
room.createConstructionSite(best_x, best_y, Screeps::STRUCTURE_CONTAINER);
|
||||||
|
sourceContainers.push_back("under_construction");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// Check if construction container is done.
|
||||||
|
for (auto entry : sourceContainers) {
|
||||||
|
if (entry != "under_construction")
|
||||||
|
continue;
|
||||||
|
auto structures = room.find(Screeps::FIND_STRUCTURES);
|
||||||
|
int index = 0;
|
||||||
|
for (auto &structure : structures) {
|
||||||
|
auto str = dynamic_cast<Screeps::Structure *>(structure.get());
|
||||||
|
if (str->structureType() == Screeps::STRUCTURE_CONTAINER) {
|
||||||
|
sourceContainers[index] = str->id();
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -15,7 +15,7 @@ void DouwcoHivemind::Spawn::loop()
|
|||||||
|
|
||||||
int required_upgraders = 1;
|
int required_upgraders = 1;
|
||||||
int required_suppliers = 1;
|
int required_suppliers = 1;
|
||||||
int required_maintainers = spawn.room().find(Screeps::FIND_MY_STRUCTURES).size() <= 2 ? 0 : 1;;
|
int required_maintainers = spawn.room().find(Screeps::FIND_STRUCTURES).size() <= 2 ? 0 : 1;;
|
||||||
int required_builders = spawn.room().find(Screeps::FIND_MY_CONSTRUCTION_SITES).size() == 0 ? 0 : 1;
|
int required_builders = spawn.room().find(Screeps::FIND_MY_CONSTRUCTION_SITES).size() == 0 ? 0 : 1;
|
||||||
for (auto &creep : Screeps::Game.creeps())
|
for (auto &creep : Screeps::Game.creeps())
|
||||||
{
|
{
|
||||||
@@ -31,8 +31,8 @@ void DouwcoHivemind::Spawn::loop()
|
|||||||
required_builders--;
|
required_builders--;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (energyAvailable < energyCapacityAvailable && 3 < Screeps::Game.creeps().size())
|
// if (energyAvailable < energyCapacityAvailable && 3 < Screeps::Game.creeps().size())
|
||||||
return;
|
// return;
|
||||||
std::string name;
|
std::string name;
|
||||||
JSON opts;
|
JSON opts;
|
||||||
if (required_suppliers > 0)
|
if (required_suppliers > 0)
|
||||||
|
|||||||
Submodule screepsxx updated: 3567060bf5...bf5fd1f69b
Reference in New Issue
Block a user