Improved miners and workers to work together.
This commit is contained in:
@@ -1 +1 @@
|
|||||||
/home/douwe/Projects/screeps/build/compile_commands.json
|
/mnt/douwe/hdd/Projects/Screeps/build/compile_commands.json
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
#define DOUWCO_HIVEMIND_HARVESTER_HPP
|
#define DOUWCO_HIVEMIND_HARVESTER_HPP
|
||||||
|
|
||||||
#include "Creeps/CreepBase.hpp"
|
#include "Creeps/CreepBase.hpp"
|
||||||
|
#include "Screeps/StructureContainer.hpp"
|
||||||
|
|
||||||
namespace DouwcoHivemind
|
namespace DouwcoHivemind
|
||||||
{
|
{
|
||||||
@@ -9,6 +10,7 @@ namespace DouwcoHivemind
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
bool harvesting;
|
bool harvesting;
|
||||||
|
bool foundContainer;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Worker(Screeps::Creep crp);
|
Worker(Screeps::Creep crp);
|
||||||
@@ -22,8 +24,11 @@ namespace DouwcoHivemind
|
|||||||
private:
|
private:
|
||||||
void getEnergy();
|
void getEnergy();
|
||||||
void harvestSource();
|
void harvestSource();
|
||||||
|
void collectEnergyFromContainers();
|
||||||
std::unique_ptr<Screeps::Source> getSourceTarget();
|
std::unique_ptr<Screeps::Source> getSourceTarget();
|
||||||
|
std::unique_ptr<Screeps::StructureContainer> getContainerTarget();
|
||||||
void searchSource();
|
void searchSource();
|
||||||
|
void searchContainer();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,10 +30,8 @@ void DouwcoHivemind::Miner::loop() {
|
|||||||
arrivedAtContainer = true;
|
arrivedAtContainer = true;
|
||||||
int x = creep.pos().x();
|
int x = creep.pos().x();
|
||||||
int y = creep.pos().y();
|
int y = creep.pos().y();
|
||||||
auto roomObjs = creep.room().lookForAtArea(Screeps::LOOK_SOURCES, y - 1,
|
auto roomObjs = creep.pos().findClosestByRange(Screeps::FIND_SOURCES, 2);
|
||||||
x - 1, y + 1, x + 1);
|
auto source = static_cast<Screeps::Source *>(roomObjs.release());
|
||||||
return; // fault is here somewhere
|
|
||||||
auto source = static_cast<Screeps::Source *>(roomObjs[0].release());
|
|
||||||
target_id = source->id();
|
target_id = source->id();
|
||||||
} else {
|
} else {
|
||||||
moveToTarget(0);
|
moveToTarget(0);
|
||||||
|
|||||||
@@ -24,9 +24,15 @@ DouwcoHivemind::Worker::Worker(Screeps::Creep crp) : CreepBase(crp) {
|
|||||||
harvesting = memory.contains("harvesting")
|
harvesting = memory.contains("harvesting")
|
||||||
? static_cast<bool>(memory["harvesting"])
|
? static_cast<bool>(memory["harvesting"])
|
||||||
: false;
|
: false;
|
||||||
|
foundContainer = memory.contains("foundContainer")
|
||||||
|
? static_cast<bool>(memory["foundContainer"])
|
||||||
|
: false;
|
||||||
}
|
}
|
||||||
|
|
||||||
DouwcoHivemind::Worker::~Worker() { memory["harvesting"] = harvesting; }
|
DouwcoHivemind::Worker::~Worker() {
|
||||||
|
memory["harvesting"] = harvesting;
|
||||||
|
memory["foundContainer"] = foundContainer;
|
||||||
|
}
|
||||||
|
|
||||||
void DouwcoHivemind::Worker::loop() {
|
void DouwcoHivemind::Worker::loop() {
|
||||||
if (harvesting) {
|
if (harvesting) {
|
||||||
@@ -45,21 +51,20 @@ void DouwcoHivemind::Worker::loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DouwcoHivemind::Worker::getEnergy() {
|
void DouwcoHivemind::Worker::getEnergy() {
|
||||||
for (auto containersJSON : creep.room().memory()["sourceContainers"]) {
|
if (foundContainer || Screeps::Game.time()%100==0) collectEnergyFromContainers();
|
||||||
auto containerID = static_cast<std::string>(containersJSON);
|
else harvestSource();
|
||||||
target_id = containerID;
|
}
|
||||||
auto containerRoomObject = getRoomObjectTarget();
|
|
||||||
auto container = static_cast<Screeps::StructureContainer *>(
|
void DouwcoHivemind::Worker::collectEnergyFromContainers() {
|
||||||
containerRoomObject.release());
|
auto container = getContainerTarget();
|
||||||
if (0 < container->store().getUsedCapacity(Screeps::RESOURCE_ENERGY)) {
|
if (!container)
|
||||||
|
return;
|
||||||
|
|
||||||
if (isNearTo(container->pos(), 1)) {
|
if (isNearTo(container->pos(), 1)) {
|
||||||
int resp = creep.withdraw(*container, Screeps::RESOURCE_ENERGY);
|
int resp = creep.withdraw(*container, Screeps::RESOURCE_ENERGY);
|
||||||
} else {
|
} else {
|
||||||
moveToTarget();
|
moveToTarget();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
harvestSource();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DouwcoHivemind::Worker::harvestSource() {
|
void DouwcoHivemind::Worker::harvestSource() {
|
||||||
@@ -74,6 +79,33 @@ void DouwcoHivemind::Worker::harvestSource() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<Screeps::StructureContainer>
|
||||||
|
DouwcoHivemind::Worker::getContainerTarget() {
|
||||||
|
auto roomObj = getRoomObjectTarget();
|
||||||
|
if (!roomObj) {
|
||||||
|
searchContainer();
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if found roomobject is an actual source
|
||||||
|
auto container = std::unique_ptr<Screeps::StructureContainer>(
|
||||||
|
dynamic_cast<Screeps::StructureContainer *>(roomObj.release()));
|
||||||
|
if (!container) {
|
||||||
|
// EM_ASM({console.log($0 + ': Can\'t cast target to Source')},
|
||||||
|
// creep.name().c_str());
|
||||||
|
searchContainer();
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the source still has energy to harvest
|
||||||
|
if (container->store().getUsedCapacity(Screeps::RESOURCE_ENERGY) == 0) {
|
||||||
|
searchContainer();
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::move(container);
|
||||||
|
}
|
||||||
|
|
||||||
std::unique_ptr<Screeps::Source> DouwcoHivemind::Worker::getSourceTarget() {
|
std::unique_ptr<Screeps::Source> DouwcoHivemind::Worker::getSourceTarget() {
|
||||||
auto roomObj = getRoomObjectTarget();
|
auto roomObj = getRoomObjectTarget();
|
||||||
if (!roomObj) {
|
if (!roomObj) {
|
||||||
@@ -102,29 +134,42 @@ std::unique_ptr<Screeps::Source> DouwcoHivemind::Worker::getSourceTarget() {
|
|||||||
|
|
||||||
void DouwcoHivemind::Worker::searchSource() {
|
void DouwcoHivemind::Worker::searchSource() {
|
||||||
target_id.clear();
|
target_id.clear();
|
||||||
|
auto sourceObject = creep.pos().findClosestByRange(Screeps::FIND_SOURCES);
|
||||||
auto sources = creep.room().find(Screeps::FIND_SOURCES_ACTIVE);
|
if (!sourceObject)
|
||||||
if (sources.size() == 0)
|
|
||||||
return;
|
return;
|
||||||
|
auto source = dynamic_cast<Screeps::Source *>(sourceObject.release());
|
||||||
int x = creep.pos().x();
|
target_id = source->id();
|
||||||
int y = creep.pos().y();
|
}
|
||||||
|
|
||||||
int closestDistance = INT16_MAX;
|
void DouwcoHivemind::Worker::searchContainer() {
|
||||||
Screeps::Source *selectedSource;
|
target_id.clear();
|
||||||
for (auto &sourceObject : sources) {
|
|
||||||
auto source = dynamic_cast<Screeps::Source *>(sourceObject.get());
|
auto structureObjects = creep.room().find(Screeps::FIND_STRUCTURES);
|
||||||
if (source->energy() == 0)
|
if (structureObjects.size() == 0)
|
||||||
continue;
|
return;
|
||||||
int dx = source->pos().x() - x;
|
|
||||||
int dy = source->pos().y() - y;
|
int fullestContents = 0;
|
||||||
int distance = dx * dx + dy * dy;
|
Screeps::StructureContainer *selectedContainer;
|
||||||
if (distance < closestDistance) {
|
for (auto &structureObject : structureObjects) {
|
||||||
closestDistance = distance;
|
auto structure =
|
||||||
selectedSource = source;
|
dynamic_cast<Screeps::Structure *>(structureObject.release());
|
||||||
}
|
if (structure->structureType() != Screeps::STRUCTURE_CONTAINER)
|
||||||
}
|
continue;
|
||||||
if (!selectedSource)
|
auto container = dynamic_cast<Screeps::StructureContainer *>(structure);
|
||||||
return;
|
int availableEnergy =
|
||||||
target_id = selectedSource->id();
|
container->store().getUsedCapacity(Screeps::RESOURCE_ENERGY).value();
|
||||||
|
if (availableEnergy == 0)
|
||||||
|
continue;
|
||||||
|
if (fullestContents < availableEnergy) {
|
||||||
|
fullestContents = availableEnergy;
|
||||||
|
selectedContainer = container;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!selectedContainer) {
|
||||||
|
foundContainer = false;
|
||||||
|
} else {
|
||||||
|
foundContainer = true;
|
||||||
|
target_id = selectedContainer->id();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "Entity/Room.hpp"
|
#include "Entity/Room.hpp"
|
||||||
|
|
||||||
#include "Screeps/Constants.hpp"
|
#include "Screeps/Constants.hpp"
|
||||||
|
#include "Screeps/Game.hpp"
|
||||||
#include "Screeps/Room.hpp"
|
#include "Screeps/Room.hpp"
|
||||||
#include "Screeps/RoomObject.hpp"
|
#include "Screeps/RoomObject.hpp"
|
||||||
#include "Screeps/RoomPosition.hpp"
|
#include "Screeps/RoomPosition.hpp"
|
||||||
@@ -28,6 +29,7 @@ DouwcoHivemind::Room::~Room() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DouwcoHivemind::Room::loop() {
|
void DouwcoHivemind::Room::loop() {
|
||||||
|
if(Screeps::Game.time()%100!=0)return;
|
||||||
if (placeContainers())
|
if (placeContainers())
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -36,7 +38,7 @@ bool DouwcoHivemind::Room::placeContainers() {
|
|||||||
auto sources = room.find(Screeps::FIND_SOURCES);
|
auto sources = room.find(Screeps::FIND_SOURCES);
|
||||||
|
|
||||||
// No containers planned or build, planning now
|
// No containers planned or build, planning now
|
||||||
if (sourceContainers.size() == 0) {
|
if (sourceContainers.size() != sources.size()) {
|
||||||
int controller_x = room.controller().value().pos().x();
|
int controller_x = room.controller().value().pos().x();
|
||||||
int controller_y = room.controller().value().pos().y();
|
int controller_y = room.controller().value().pos().y();
|
||||||
|
|
||||||
@@ -84,5 +86,10 @@ bool DouwcoHivemind::Room::placeContainers() {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
// Check if containers still exist
|
||||||
|
for(int i = 0; i < sourceContainers.size(); i++){
|
||||||
|
auto roomObj = Screeps::Game.getObjectById(sourceContainers[i]);
|
||||||
|
if(!roomObj) sourceContainers.erase(sourceContainers.begin() + i);
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -64,7 +64,8 @@ void DouwcoHivemind::Spawn::loop() {
|
|||||||
std::vector<std::string> body;
|
std::vector<std::string> body;
|
||||||
if (opts["memory"]["role"] == CreepRole::MINER) {
|
if (opts["memory"]["role"] == CreepRole::MINER) {
|
||||||
body.push_back("move");
|
body.push_back("move");
|
||||||
for (int i = 0; i < (energyAvailable - 50) / 100 && i < 5; i++) {
|
body.push_back("work");
|
||||||
|
for (int i = 0; i < (energyAvailable - 150) / 100 && i < 5; i++) {
|
||||||
body.push_back("work");
|
body.push_back("work");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user