88 lines
2.5 KiB
C++
88 lines
2.5 KiB
C++
#include "Room.hpp"
|
|
|
|
#include "Screeps/Constants.hpp"
|
|
#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()) {
|
|
if (memory.contains("sourceContainers"))
|
|
sourceContainers =
|
|
static_cast<std::vector<std::string>>(memory["sourceContainers"]);
|
|
}
|
|
|
|
DouwcoHivemind::Room::~Room() {
|
|
memory["sourceContainers"] = sourceContainers;
|
|
room.setMemory(memory);
|
|
}
|
|
|
|
void DouwcoHivemind::Room::loop() {
|
|
if (placeContainers())
|
|
return;
|
|
}
|
|
|
|
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 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;
|
|
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");
|
|
}
|
|
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;
|
|
} |