Implemented auto building of containers.
This commit is contained in:
@@ -42,7 +42,6 @@ std::unique_ptr<Screeps::ConstructionSite> DouwcoHivemind::Builder::getConstruct
|
||||
if (!roomObj)
|
||||
{
|
||||
searchConstructionSite();
|
||||
if(target_id.empty()) creep.move(rand()%5);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,6 @@ std::unique_ptr<Screeps::Structure> DouwcoHivemind::Maintainer::getDamagedStruct
|
||||
if (!roomObj)
|
||||
{
|
||||
searchDamagedStructure();
|
||||
if(target_id.empty()) creep.move(rand()%5);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,6 @@ std::unique_ptr<Screeps::Structure> DouwcoHivemind::Supplier::getEnergyStructure
|
||||
if (!roomObj)
|
||||
{
|
||||
searchEnergyStructure();
|
||||
if(target_id.empty()) creep.move(rand()%5);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <Screeps/Game.hpp>
|
||||
#include <Screeps/Memory.hpp>
|
||||
#include <Screeps/StructureController.hpp>
|
||||
|
||||
#include "Engine.hpp"
|
||||
|
||||
@@ -38,6 +39,7 @@ void DouwcoHivemind::Engine::ReadOutRooms() {
|
||||
JS::console.log(std::string("Reading out owned rooms"));
|
||||
auto src_rooms = Screeps::Game.rooms();
|
||||
for (auto &room : src_rooms) {
|
||||
if(!room.second.controller().value().my()) continue;
|
||||
rooms.push_back(std::make_unique<Room>(room.second));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,37 +4,85 @@
|
||||
#include "Screeps/Room.hpp"
|
||||
#include "Screeps/RoomObject.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()) {
|
||||
_sourceContainerState =
|
||||
memory.contains("_sourceContainerState")
|
||||
? static_cast<ProgressState>(memory["_sourceContainerState"])
|
||||
: ProgressState::PLANNED;
|
||||
if (memory.contains("sourceContainers"))
|
||||
sourceContainers =
|
||||
static_cast<std::vector<std::string>>(memory["sourceContainers"]);
|
||||
}
|
||||
|
||||
DouwcoHivemind::Room::~Room() {
|
||||
memory["_sourceContainerState"] = _sourceContainerState;
|
||||
memory["sourceContainers"] = sourceContainers;
|
||||
room.setMemory(memory);
|
||||
}
|
||||
|
||||
void DouwcoHivemind::Room::loop() {
|
||||
if (assignConstructionSites())
|
||||
if (placeContainers())
|
||||
return;
|
||||
}
|
||||
|
||||
bool DouwcoHivemind::Room::assignConstructionSites() {
|
||||
if (_sourceContainerState == PLANNED) {
|
||||
auto sources = room.find(Screeps::FIND_SOURCES);
|
||||
bool DouwcoHivemind::Room::placeContainers() {
|
||||
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) {
|
||||
int x = source->pos().x();
|
||||
int y = source->pos().y();
|
||||
int source_x = source->pos().x();
|
||||
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 dy = -1; dy < 2; dy++) {
|
||||
if(dx == 0 && dy == 0) continue;
|
||||
auto pos = room.getPositionAt(x + dx, y + dy);
|
||||
if (dx == 0 && dy == 0)
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
room.createConstructionSite(best_x, best_y, Screeps::STRUCTURE_CONTAINER);
|
||||
sourceContainers.push_back("under_construction");
|
||||
}
|
||||
} else if (_sourceContainerState == IN_PROGRESS) {
|
||||
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;
|
||||
}
|
||||
@@ -12,10 +12,10 @@ void DouwcoHivemind::Spawn::loop()
|
||||
|
||||
int energyAvailable = spawn.room().energyAvailable();
|
||||
int energyCapacityAvailable = spawn.room().energyCapacityAvailable();
|
||||
|
||||
|
||||
int required_upgraders = 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;
|
||||
for (auto &creep : Screeps::Game.creeps())
|
||||
{
|
||||
@@ -31,8 +31,8 @@ void DouwcoHivemind::Spawn::loop()
|
||||
required_builders--;
|
||||
}
|
||||
|
||||
if (energyAvailable < energyCapacityAvailable && 3 < Screeps::Game.creeps().size())
|
||||
return;
|
||||
// if (energyAvailable < energyCapacityAvailable && 3 < Screeps::Game.creeps().size())
|
||||
// return;
|
||||
std::string name;
|
||||
JSON opts;
|
||||
if (required_suppliers > 0)
|
||||
|
||||
Reference in New Issue
Block a user