Compare commits

5 Commits

14 changed files with 326 additions and 183 deletions

View File

@@ -1 +1 @@
/home/douwe/Projects/screeps/build/compile_commands.json /mnt/douwe/hdd/Projects/Screeps/build/compile_commands.json

View File

@@ -0,0 +1,9 @@
#ifndef DOUWCO_HIVEMIND_CONSTANTS_HPP
#define DOUWCO_HIVEMIND_CONSTANTS_HPP
namespace DouwcoHivemind
{
enum CreepRole { UNEMPLOYED, SUPPLIER, UPGRADER, BUILDER, MAINTAINER, MINER };
}
#endif // DOUWCO_HIVEMIND_CONSTANTS_HPP

View File

@@ -2,6 +2,7 @@
#define DOUWCO_HIVEMIND_CREEPBASE_HPP #define DOUWCO_HIVEMIND_CREEPBASE_HPP
#include <Screeps/Creep.hpp> #include <Screeps/Creep.hpp>
#include "Constants.hpp"
namespace Screeps { namespace Screeps {
class RoomPosition; class RoomPosition;
@@ -10,7 +11,6 @@ class PathStep;
} // namespace Screeps } // namespace Screeps
namespace DouwcoHivemind { namespace DouwcoHivemind {
enum CreepRole { UNEMPLOYED, SUPPLIER, UPGRADER, BUILDER, MAINTAINER, MINER };
class CreepBase { class CreepBase {
public: public:

View File

@@ -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();
}; };
} }

View File

@@ -19,7 +19,7 @@ public:
void loop(); void loop();
protected: protected:
bool placeContainers(); void placeContainers();
}; };
} // namespace DouwcoHivemind } // namespace DouwcoHivemind

View File

@@ -0,0 +1,24 @@
#ifndef DOUWCO_HIVEMIND_TOWER_HPP
#define DOUWCO_HIVEMIND_TOWER_HPP
#include <Screeps/StructureTower.hpp>
#include "Structures/StructureBase.hpp"
namespace DouwcoHivemind
{
class Tower : public StructureBase
{
private:
Screeps::StructureTower tower;
public:
Tower(Screeps::StructureTower twr) : tower(twr),
StructureBase() {}
~Tower() {}
void loop() override;
};
}
#endif // DOUWCO_HIVEMIND_TOWER_HPP

View File

@@ -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);

View File

@@ -1,120 +1,122 @@
#include <vector>
#include <optional>
#include <emscripten.h> #include <emscripten.h>
#include <optional>
#include <string>
#include <vector>
#include <Screeps/Game.hpp>
#include <Screeps/Creep.hpp> #include <Screeps/Creep.hpp>
#include <Screeps/Source.hpp> #include <Screeps/Game.hpp>
#include <Screeps/Room.hpp> #include <Screeps/Room.hpp>
#include <Screeps/RoomPosition.hpp>
#include <Screeps/RoomObject.hpp> #include <Screeps/RoomObject.hpp>
#include <Screeps/RoomPosition.hpp>
#include <Screeps/Source.hpp>
#include <Screeps/Store.hpp>
#include <Screeps/Structure.hpp> #include <Screeps/Structure.hpp>
#include <Screeps/StructureController.hpp> #include <Screeps/StructureController.hpp>
#include <Screeps/StructureSpawn.hpp>
#include <Screeps/StructureExtension.hpp> #include <Screeps/StructureExtension.hpp>
#include <Screeps/StructureSpawn.hpp>
#include <Screeps/StructureStorage.hpp>
#include <Screeps/StructureTower.hpp> #include <Screeps/StructureTower.hpp>
#include <Screeps/Store.hpp>
#include "Creeps/Supplier.hpp" #include "Creeps/Supplier.hpp"
#include "Screeps/Constants.hpp"
void DouwcoHivemind::Supplier::depositEnergy() void DouwcoHivemind::Supplier::depositEnergy() {
{ auto structure = getEnergyStructureTarget();
auto structure = getEnergyStructureTarget(); if (!structure)
if (!structure) return; return;
if (isNearTo(structure->pos(), 1)) if (isNearTo(structure->pos(), 1)) {
{ int resp = creep.transfer(*structure, Screeps::RESOURCE_ENERGY);
int resp = creep.transfer(*structure, Screeps::RESOURCE_ENERGY); } else {
} moveToTarget();
else }
{
moveToTarget();
}
} }
std::unique_ptr<Screeps::Structure> DouwcoHivemind::Supplier::getEnergyStructureTarget() std::unique_ptr<Screeps::Structure>
{ DouwcoHivemind::Supplier::getEnergyStructureTarget() {
auto roomObj = getRoomObjectTarget(); auto roomObj = getRoomObjectTarget();
if (!roomObj) if (!roomObj) {
{ searchEnergyStructure();
searchEnergyStructure(); return nullptr;
return nullptr; }
}
auto structure = std::unique_ptr<Screeps::Structure>(dynamic_cast<Screeps::Structure *>(roomObj.release())); auto structure = std::unique_ptr<Screeps::Structure>(
dynamic_cast<Screeps::Structure *>(roomObj.release()));
if (!structure) {
searchEnergyStructure();
return nullptr;
}
// Check if the structure can receive energy to harvest
int energyCapacity;
auto structureType = structure->structureType();
if (structureType == Screeps::STRUCTURE_TOWER) {
auto tower = dynamic_cast<Screeps::StructureTower *>(structure.get());
energyCapacity =
tower->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
} else if (structureType == Screeps::STRUCTURE_SPAWN) {
auto spawn = dynamic_cast<Screeps::StructureSpawn *>(structure.get());
energyCapacity =
spawn->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
} else if (structureType == Screeps::STRUCTURE_EXTENSION) {
auto extension =
dynamic_cast<Screeps::StructureExtension *>(structure.get());
energyCapacity =
extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
} else if (structureType == Screeps::STRUCTURE_STORAGE) {
auto storage = dynamic_cast<Screeps::StructureStorage *>(structure.get());
energyCapacity =
storage->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
}
if (energyCapacity == 0) {
searchEnergyStructure();
return nullptr;
}
return std::move(structure);
}
void DouwcoHivemind::Supplier::searchEnergyStructure() {
int mostEnergyNeeded = 0;
std::string selectedStructureId;
std::string storageStructureId;
auto structures = creep.room().find(Screeps::FIND_MY_STRUCTURES);
for (auto &structureObject : structures) {
auto structure = dynamic_cast<Screeps::Structure *>(structureObject.get());
if (!structure) if (!structure)
{ continue;
searchEnergyStructure();
return nullptr; int energyRequired = 0;
}
// Check if the structure can receive energy to harvest
int energyCapacity;
auto structureType = structure->structureType(); auto structureType = structure->structureType();
if (structureType == Screeps::STRUCTURE_SPAWN) if (structureType == Screeps::STRUCTURE_TOWER) {
{ auto tower = dynamic_cast<Screeps::StructureTower *>(structure);
auto spawn = dynamic_cast<Screeps::StructureSpawn *>(structure.get()); energyRequired =
energyCapacity = spawn->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value(); tower->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
} } else if (structureType == Screeps::STRUCTURE_SPAWN) {
else if (structureType == Screeps::STRUCTURE_EXTENSION) auto spawn = dynamic_cast<Screeps::StructureSpawn *>(structure);
{ energyRequired =
auto extension = dynamic_cast<Screeps::StructureExtension *>(structure.get()); spawn->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
energyCapacity = extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value(); } else if (structureType == Screeps::STRUCTURE_EXTENSION) {
} auto extension = dynamic_cast<Screeps::StructureExtension *>(structure);
else if (structureType == Screeps::STRUCTURE_TOWER) energyRequired =
{ extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
auto extension = dynamic_cast<Screeps::StructureTower *>(structure.get()); } else if (structureType == Screeps::STRUCTURE_STORAGE) {
energyCapacity = extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value(); storageStructureId = structure->id();
} }
if (energyCapacity == 0) if (energyRequired > mostEnergyNeeded) {
{ mostEnergyNeeded = energyRequired;
searchEnergyStructure(); selectedStructureId = structure->id();
return nullptr;
} }
}
return std::move(structure); if (mostEnergyNeeded == 0)
} selectedStructureId = storageStructureId;
void DouwcoHivemind::Supplier::searchEnergyStructure() if (selectedStructureId.empty())
{ target_id.clear();
int mostEnergyNeeded = 0; else
Screeps::Structure *selectedStructure; target_id = selectedStructureId;
auto structures = creep.room().find(Screeps::FIND_MY_STRUCTURES);
for (auto &structureObject : structures)
{
auto structure = dynamic_cast<Screeps::Structure *>(structureObject.get());
if (!structure)
continue;
int energyRequired;
auto structureType = structure->structureType();
if (structureType == Screeps::STRUCTURE_SPAWN)
{
auto spawn = dynamic_cast<Screeps::StructureSpawn *>(structure);
energyRequired = spawn->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
}
else if (structureType == Screeps::STRUCTURE_EXTENSION)
{
auto extension = dynamic_cast<Screeps::StructureExtension *>(structure);
energyRequired = extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
}
else if (structureType == Screeps::STRUCTURE_TOWER)
{
auto extension = dynamic_cast<Screeps::StructureTower *>(structure);
energyRequired = extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
}
if (energyRequired > mostEnergyNeeded)
{
mostEnergyNeeded = energyRequired;
selectedStructure = structure;
}
}
if (selectedStructure)
target_id = selectedStructure->id();
else
target_id.clear();
} }

View File

@@ -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)
if (isNearTo(container->pos(), 1)) { return;
int resp = creep.withdraw(*container, Screeps::RESOURCE_ENERGY);
} else { if (isNearTo(container->pos(), 1)) {
moveToTarget(); int resp = creep.withdraw(*container, Screeps::RESOURCE_ENERGY);
} } else {
} 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);
if (!sourceObject)
return;
auto source = dynamic_cast<Screeps::Source *>(sourceObject.release());
target_id = source->id();
}
auto sources = creep.room().find(Screeps::FIND_SOURCES_ACTIVE); void DouwcoHivemind::Worker::searchContainer() {
if (sources.size() == 0) target_id.clear();
auto structureObjects = creep.room().find(Screeps::FIND_STRUCTURES);
if (structureObjects.size() == 0)
return; return;
int x = creep.pos().x(); int fullestContents = 0;
int y = creep.pos().y(); Screeps::StructureContainer *selectedContainer;
for (auto &structureObject : structureObjects) {
int closestDistance = INT16_MAX; auto structure =
Screeps::Source *selectedSource; dynamic_cast<Screeps::Structure *>(structureObject.release());
for (auto &sourceObject : sources) { if (structure->structureType() != Screeps::STRUCTURE_CONTAINER)
auto source = dynamic_cast<Screeps::Source *>(sourceObject.get());
if (source->energy() == 0)
continue; continue;
int dx = source->pos().x() - x; auto container = dynamic_cast<Screeps::StructureContainer *>(structure);
int dy = source->pos().y() - y; int availableEnergy =
int distance = dx * dx + dy * dy; container->store().getUsedCapacity(Screeps::RESOURCE_ENERGY).value();
if (distance < closestDistance) { if (availableEnergy == 0)
closestDistance = distance; continue;
selectedSource = source; if (fullestContents < availableEnergy) {
fullestContents = availableEnergy;
selectedContainer = container;
} }
} }
if (!selectedSource)
return; if (!selectedContainer) {
target_id = selectedSource->id(); foundContainer = false;
} } else {
foundContainer = true;
target_id = selectedContainer->id();
}
}

View File

@@ -1,6 +1,7 @@
#include <Screeps/Game.hpp> #include <Screeps/Game.hpp>
#include <Screeps/Memory.hpp> #include <Screeps/Memory.hpp>
#include <Screeps/StructureController.hpp> #include <Screeps/StructureController.hpp>
#include <memory>
#include "Engine.hpp" #include "Engine.hpp"
@@ -11,7 +12,10 @@
#include "Creeps/Upgrader.hpp" #include "Creeps/Upgrader.hpp"
#include "Creeps/Miner.hpp" #include "Creeps/Miner.hpp"
#include "Screeps/Constants.hpp"
#include "Screeps/StructureSpawn.hpp"
#include "Structures/Spawn.hpp" #include "Structures/Spawn.hpp"
#include "Structures/Tower.hpp"
DouwcoHivemind::Engine::Engine() { DouwcoHivemind::Engine::Engine() {
ReadOutRooms(); ReadOutRooms();
@@ -66,9 +70,15 @@ void DouwcoHivemind::Engine::ReadOutCreeps() {
void DouwcoHivemind::Engine::ReadOutStructures() { void DouwcoHivemind::Engine::ReadOutStructures() {
JS::console.log(std::string("Reading out structures")); JS::console.log(std::string("Reading out structures"));
auto spawns = Screeps::Game.spawns(); auto structures_src =Screeps::Game.structures();
for (auto &spawn : spawns) { for (auto &structure : structures_src) {
structures.push_back(std::make_unique<Spawn>(spawn.second)); std::string structureType = structure.second.structureType();
if(structureType == Screeps::STRUCTURE_TOWER){
structures.push_back(std::make_unique<Tower>(static_cast<Screeps::StructureTower>(structure.second)));
}
else if(structureType == Screeps::STRUCTURE_SPAWN){
structures.push_back(std::make_unique<Spawn>(static_cast<Screeps::StructureSpawn>(structure.second)));
}
} }
} }

View File

@@ -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"
@@ -8,13 +9,8 @@
#include "Screeps/Structure.hpp" #include "Screeps/Structure.hpp"
#include "Screeps/StructureContainer.hpp" #include "Screeps/StructureContainer.hpp"
#include "Screeps/StructureController.hpp" #include "Screeps/StructureController.hpp"
#include <algorithm>
#include <cmath> #include "Constants.hpp"
#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()) {
if (memory.contains("sourceContainers")) if (memory.contains("sourceContainers"))
@@ -27,16 +23,17 @@ DouwcoHivemind::Room::~Room() {
room.setMemory(memory); room.setMemory(memory);
} }
void DouwcoHivemind::Room::loop() { void DouwcoHivemind::Room::loop() {
if (placeContainers()) // Check on building setup
return; if(Screeps::Game.time()%1000!=0) return;
placeContainers();
} }
bool DouwcoHivemind::Room::placeContainers() { void 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();
@@ -67,7 +64,7 @@ bool DouwcoHivemind::Room::placeContainers() {
room.createConstructionSite(best_x, best_y, Screeps::STRUCTURE_CONTAINER); room.createConstructionSite(best_x, best_y, Screeps::STRUCTURE_CONTAINER);
sourceContainers.push_back("under_construction"); sourceContainers.push_back("under_construction");
} }
return true; return;
} }
// Check if construction container is done. // Check if construction container is done.
for (auto entry : sourceContainers) { for (auto entry : sourceContainers) {
@@ -82,7 +79,11 @@ bool DouwcoHivemind::Room::placeContainers() {
index++; index++;
} }
} }
return true; return;
} }
return false; // 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);
}
}

View File

@@ -5,68 +5,93 @@
#include "Structures/Spawn.hpp" #include "Structures/Spawn.hpp"
void DouwcoHivemind::Spawn::loop() { void DouwcoHivemind::Spawn::loop() {
// Only run every 50 ticks // Only run every 500 ticks
if (Screeps::Game.time() % 50 != 0) if (Screeps::Game.time() % 100 != 0)
return; return;
// Get energy data
int energyAvailable = spawn.room().energyAvailable(); int energyAvailable = spawn.room().energyAvailable();
int energyCapacityAvailable = spawn.room().energyCapacityAvailable(); int energyCapacityAvailable = spawn.room().energyCapacityAvailable();
// Calculate needed creep roles
int required_upgraders = 1; int required_upgraders = 1;
int required_suppliers = 1; int required_suppliers = 2;
int required_maintainers = int required_maintainers =
spawn.room().find(Screeps::FIND_STRUCTURES).size() <= 2 ? 0 : 1; spawn.room().find(Screeps::FIND_STRUCTURES).size() <= 2 ? 0 : 1;
; ;
int required_builders = int required_builders =
spawn.room().find(Screeps::FIND_MY_CONSTRUCTION_SITES).size() == 0 ? 0 spawn.room().find(Screeps::FIND_MY_CONSTRUCTION_SITES).size() == 0 ? 0
: 1; : 1;
int required_miners = 1; //spawn.room().memory()["sourceContainers"].size(); int required_miners = 1; // spawn.room().memory()["sourceContainers"].size();
// Count existing roles
int existing_upgraders = 0;
int existing_suppliers = 0;
int existing_maintainers = 0;
int existing_builders = 0;
int existing_miners = 0;
for (auto &creep : Screeps::Game.creeps()) { for (auto &creep : Screeps::Game.creeps()) {
CreepRole role = creep.second.memory()["role"]; CreepRole role = creep.second.memory()["role"];
if (role == CreepRole::SUPPLIER) if (role == CreepRole::SUPPLIER)
required_suppliers--; existing_suppliers++;
else if (role == CreepRole::UPGRADER) else if (role == CreepRole::UPGRADER)
required_upgraders--; existing_upgraders++;
else if (role == CreepRole::MAINTAINER) else if (role == CreepRole::MAINTAINER)
required_maintainers--; existing_maintainers++;
else if (role == CreepRole::BUILDER) else if (role == CreepRole::BUILDER)
required_builders--; existing_builders++;
else if (role == CreepRole::MINER) else if (role == CreepRole::MINER)
required_miners--; existing_miners++;
} }
// if (energyAvailable < energyCapacityAvailable && 3 < // Check if max energy used
// Screeps::Game.creeps().size()) if (energyAvailable < energyCapacityAvailable && 0 < existing_suppliers)
// return; return;
// Set creep properties
std::string name; std::string name;
JSON opts; JSON opts;
if (required_upgraders > 0) { CreepRole role = CreepRole::UNEMPLOYED;
opts["memory"]["role"] = CreepRole::UPGRADER; if (required_miners > existing_miners) {
name = "Upgrader: "; role = CreepRole::MINER;
} else if (required_miners > 0) {
opts["memory"]["role"] = CreepRole::MINER;
opts["memory"]["target_id"] = opts["memory"]["target_id"] =
spawn.room().memory()["sourceContainers"][0]; // make logic for more spawn.room().memory()["sourceContainers"][0]; // make logic for more
name = "Miner: "; name = "Miner: ";
} else if (required_suppliers > 0) { } else if (required_suppliers > existing_suppliers) {
opts["memory"]["role"] = CreepRole::SUPPLIER; role = CreepRole::SUPPLIER;
name = "Supplier: "; name = "Supplier: ";
} else if (required_builders > 0) { } else if (required_upgraders > existing_upgraders) {
opts["memory"]["role"] = CreepRole::BUILDER; role = CreepRole::UPGRADER;
name = "Upgrader: ";
} else if (required_builders > existing_builders) {
role = CreepRole::BUILDER;
name = "Builder: "; name = "Builder: ";
} else if (required_maintainers > 0) { } else if (required_maintainers > existing_maintainers) {
opts["memory"]["role"] = CreepRole::MAINTAINER; role = CreepRole::MAINTAINER;
name = "Maintainer: "; name = "Maintainer: ";
} else } else
return; return;
opts["memory"]["role"] = role;
// Build creep body
std::vector<std::string> body; std::vector<std::string> body;
if (opts["memory"]["role"] == CreepRole::MINER) { if (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 if (role == SUPPLIER) {
body.push_back("move");
body.push_back("work");
body.push_back("carry");
for (int i = 0; i < (energyAvailable - 200) / 100 && i * 50 < energyCapacityAvailable; i++) {
body.push_back("move");
body.push_back("carry");
}
} else { } else {
for (int i = 0; i < energyAvailable / 200; i++) { for (int i = 0; i < energyAvailable / 200; i++) {
body.push_back("work"); body.push_back("work");
@@ -75,5 +100,6 @@ void DouwcoHivemind::Spawn::loop() {
} }
} }
// Create creep
spawn.spawnCreep(body, name + std::to_string(Screeps::Game.time()), opts); spawn.spawnCreep(body, name + std::to_string(Screeps::Game.time()), opts);
} }

View File

@@ -0,0 +1,23 @@
#include <Screeps/Game.hpp>
#include <Screeps/Room.hpp>
#include <cstdint>
#include "Creeps/CreepBase.hpp"
#include "Screeps/Constants.hpp"
#include "Screeps/Creep.hpp"
#include "Structures/Tower.hpp"
void DouwcoHivemind::Tower::loop() {
int minHits = INT16_MAX;
Screeps::Creep *target;
auto hostile_creeps = tower.room().find(Screeps::FIND_HOSTILE_CREEPS);
for (auto &creepRoomObj : hostile_creeps) {
Screeps::Creep* creep = static_cast<Screeps::Creep*>(creepRoomObj.get());
int health = creep->hits();
if(health<minHits){
minHits=health;
target = creep;
}
}
tower.attack(*target);
}