Implemented auto building of containers.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user