diff --git a/douwco_hivemind/include/Creeps/CreepBase.hpp b/douwco_hivemind/include/Creeps/CreepBase.hpp index a3b5027..efd649a 100644 --- a/douwco_hivemind/include/Creeps/CreepBase.hpp +++ b/douwco_hivemind/include/Creeps/CreepBase.hpp @@ -3,47 +3,35 @@ #include -namespace Screeps{ - class RoomPosition; - class RoomObject; - class PathStep; -} +namespace Screeps { +class RoomPosition; +class RoomObject; +class PathStep; +} // namespace Screeps -namespace DouwcoHivemind -{ - enum CreepRole - { - UNEMPLOYED, - SUPPLIER, - UPGRADER, - BUILDER, - MAINTAINER - }; +namespace DouwcoHivemind { +enum CreepRole { UNEMPLOYED, SUPPLIER, UPGRADER, BUILDER, MAINTAINER, MINER }; - class CreepBase - { - public: - CreepRole role; - std::string target_id; +class CreepBase { +public: + CreepRole role; + std::string target_id; - protected: - Screeps::Creep creep; - JSON memory; +protected: + Screeps::Creep creep; + JSON memory; - public: - CreepBase(Screeps::Creep crp); - virtual ~CreepBase(); - virtual void loop() {} +public: + CreepBase(Screeps::Creep crp); + virtual ~CreepBase(); + virtual void loop() {} + bool isNearTo(const Screeps::RoomPosition &pos, int dist); - - bool isNearTo(const Screeps::RoomPosition &pos, int dist); - - - protected: - void moveToTarget(int dist = 1); - std::unique_ptr getRoomObjectTarget(); - }; -} +protected: + void moveToTarget(int dist = 1); + std::unique_ptr getRoomObjectTarget(); +}; +} // namespace DouwcoHivemind #endif // DOUWCO_HIVEMIND_CREEPBASE_HPP \ No newline at end of file diff --git a/douwco_hivemind/include/Creeps/Miner.hpp b/douwco_hivemind/include/Creeps/Miner.hpp new file mode 100644 index 0000000..f9afb35 --- /dev/null +++ b/douwco_hivemind/include/Creeps/Miner.hpp @@ -0,0 +1,21 @@ +#ifndef DOUWCO_HIVEMIND_MINER_HPP +#define DOUWCO_HIVEMIND_MINER_HPP + +#include "Creeps/CreepBase.hpp" +#include + +namespace DouwcoHivemind +{ + class Miner : public CreepBase + { + private: + bool arrivedAtContainer; + + public: + Miner(Screeps::Creep crp); + ~Miner() override; + void loop() override; + }; +} + +#endif // DOUWCO_HIVEMIND_MINER_HPP \ No newline at end of file diff --git a/douwco_hivemind/include/Creeps/Worker.hpp b/douwco_hivemind/include/Creeps/Worker.hpp index 7804459..277b9c0 100644 --- a/douwco_hivemind/include/Creeps/Worker.hpp +++ b/douwco_hivemind/include/Creeps/Worker.hpp @@ -20,6 +20,7 @@ namespace DouwcoHivemind virtual void depositEnergy(){} private: + void getEnergy(); void harvestSource(); std::unique_ptr getSourceTarget(); void searchSource(); diff --git a/douwco_hivemind/include/Engine.hpp b/douwco_hivemind/include/Engine.hpp index 625ffe9..0e1620f 100644 --- a/douwco_hivemind/include/Engine.hpp +++ b/douwco_hivemind/include/Engine.hpp @@ -4,7 +4,7 @@ #include #include "Creeps/CreepBase.hpp" -#include "Room.hpp" +#include "Entity/Room.hpp" #include "Structures/StructureBase.hpp" namespace DouwcoHivemind { diff --git a/douwco_hivemind/include/Room.hpp b/douwco_hivemind/include/Entity/Room.hpp similarity index 100% rename from douwco_hivemind/include/Room.hpp rename to douwco_hivemind/include/Entity/Room.hpp diff --git a/douwco_hivemind/src/Creeps/CreepBase.cpp b/douwco_hivemind/src/Creeps/CreepBase.cpp index 95af63e..679239d 100644 --- a/douwco_hivemind/src/Creeps/CreepBase.cpp +++ b/douwco_hivemind/src/Creeps/CreepBase.cpp @@ -4,144 +4,142 @@ #include #include #include +#include #include "Creeps/CreepBase.hpp" #include "Tools/JsonTool.hpp" -#include "Tools/PathTool.hpp" #include "Tools/MeasureTool.hpp" +#include "Tools/PathTool.hpp" -DouwcoHivemind::CreepBase::CreepBase(Screeps::Creep crp) : creep(crp), - memory(crp.memory()) -{ - role = memory.contains("role") ? static_cast(memory["role"]) : CreepRole::UNEMPLOYED; - target_id = memory.contains("target_id") ? static_cast(memory["target_id"]) : std::string(); +DouwcoHivemind::CreepBase::CreepBase(Screeps::Creep crp) + : creep(crp), memory(crp.memory()) { + role = memory.contains("role") ? static_cast(memory["role"]) + : CreepRole::UNEMPLOYED; + target_id = memory.contains("target_id") + ? static_cast(memory["target_id"]) + : std::string(); } -DouwcoHivemind::CreepBase::~CreepBase() -{ - memory["role"] = role; - memory["target_id"] = target_id; - creep.setMemory(memory); +DouwcoHivemind::CreepBase::~CreepBase() { + memory["role"] = role; + memory["target_id"] = target_id; + creep.setMemory(memory); } -void DouwcoHivemind::CreepBase::moveToTarget(int dist) -{ - // Is move required? - auto target = getRoomObjectTarget(); - if (isNearTo(target->pos(), dist)) - { - memory.erase("path"); - return; +void DouwcoHivemind::CreepBase::moveToTarget(int dist) { + // Is move required? + auto target = getRoomObjectTarget(); + if (isNearTo(target->pos(), dist)) { + memory.erase("path"); + return; + } + + // Is wating? + if (memory.contains("wait") && memory["wait"] > 0) { + memory["wait"] = memory["wait"].get() - 1; + creep.say("Waiting.."); + return; + } + + // Is there a path to walk? + if (!memory.contains("path") || memory["path"].empty()) { + creep.say("Searching route!"); + auto target_pos = target->pos(); + auto path = creep.room().findPath(creep.pos(), target_pos); + auto last = path.back(); + if (dist == 0) { + Screeps::Room::PathStep step; + step.x = target_pos.x(); + step.y = target_pos.y(); + step.dx = step.x - last.x; + step.dy = step.y - last.y; + path.push_back(step); } - - // Is wating? - if (memory.contains("wait") && memory["wait"] > 0) - { - memory["wait"] = memory["wait"].get() - 1; - creep.say("Waiting.."); - return; + if (abs(last.x - target_pos.x()) > dist || + abs(last.y - target_pos.y()) > dist) { + creep.say("No possible path"); + memory["wait"] = rand() % 20; + return; } + memory["path"] = vectorToJson(flattenPathSteps(path)); + } - // Is there a path to walk? - if (!memory.contains("path") || memory["path"].empty()) - { - creep.say("Searching route!"); - auto target_pos = target->pos(); - auto path = creep.room().findPath(creep.pos(), target_pos); - auto last = path.back(); - if (last.x - target_pos.x() > dist || last.y - target_pos.y() > dist) - { - creep.say("No possible path"); - memory["wait"] = rand() % 20; - return; - } - memory["path"] = vectorToJson(flattenPathSteps(path)); + // JS::console.log(std::string("creep pos: [") + + // std::to_string(creep.pos().x()) + + // std::string(",") + + // std::to_string(creep.pos().y()) + + // std::string("]")); + + // Get step from memory + int pathStepData[5] = {0}; + if (memory["path"].size() > 5) { + for (int i = 0; i < 5; i++) { + pathStepData[i] = memory["path"][i]; } + } else { + memory.erase("path"); + return; + } - // JS::console.log(std::string("creep pos: [") + - // std::to_string(creep.pos().x()) + - // std::string(",") + - // std::to_string(creep.pos().y()) + - // std::string("]")); + // Is the move of last tick executed? + int x = creep.pos().x(); + int y = creep.pos().y(); - // Get step from memory - int pathStepData[5] = {0}; - if (memory["path"].size() > 5) - { - for (int i = 0; i < 5; i++) - { - pathStepData[i] = memory["path"][i]; - } + Screeps::Room::PathStep step; + step.x = pathStepData[0]; + step.y = pathStepData[1]; + step.dx = pathStepData[2]; + step.dy = pathStepData[3]; + step.direction = pathStepData[4]; + + if (memory.contains("last_pos")) { + int last_x = memory["last_pos"]["x"]; + int last_y = memory["last_pos"]["y"]; + memory.erase("last_pos"); + if (x == last_x && y == last_y) { + creep.say("I'm stuck!"); + memory["wait"] = rand() % 5; + return; } - else - { - memory.erase("path"); - return; - } - - // Is the move of last tick executed? - int x = creep.pos().x(); - int y = creep.pos().y(); - - Screeps::Room::PathStep step; - step.x = pathStepData[0]; - step.y = pathStepData[1]; - step.dx = pathStepData[2]; - step.dy = pathStepData[3]; - step.direction = pathStepData[4]; - - if (memory.contains("last_pos")) - { - int last_x = memory["last_pos"]["x"]; - int last_y = memory["last_pos"]["y"]; - memory.erase("last_pos"); - if (x == last_x && y == last_y) - { - creep.say("I'm stuck!"); - memory["wait"] = rand() % 5; - return; - } - } - - // Is the creep on the place intended by the path? - if (!(x == step.x - step.dx && y == step.y - step.dy)) - { - creep.say("I'm lost!"); - memory["wait"] = rand() % 5; - memory.erase("path"); - return; - } - - // Lets move forward - int resp = creep.move(step.direction); - if (resp == Screeps::OK) - { - memory["last_pos"]["x"] = x; - memory["last_pos"]["y"] = y; - - for (int i = 0; i < 5; i++) - { - memory["path"].erase(0); - } + } + + // Is the creep on the place intended by the path? + if (!(x == step.x - step.dx && y == step.y - step.dy)) { + creep.say("I'm lost!"); + memory["wait"] = rand() % 5; + memory.erase("path"); + return; + } + + // Lets move forward + int resp = creep.move(step.direction); + if (resp == Screeps::OK) { + memory["last_pos"]["x"] = x; + memory["last_pos"]["y"] = y; + + for (int i = 0; i < 5; i++) { + memory["path"].erase(0); } + } } -std::unique_ptr DouwcoHivemind::CreepBase::getRoomObjectTarget() -{ - // Check if any target is present - if(target_id.empty()) return nullptr; - // Check if game can find target - auto roomObj = Screeps::Game.getObjectById(target_id); - if (!roomObj) - { - JS::console.log(creep.name() + ": Game can\'t find target id: " + target_id); - return nullptr; - } - return std::move(roomObj); +std::unique_ptr +DouwcoHivemind::CreepBase::getRoomObjectTarget() { + // Check if any target is present + if (target_id.empty()) + return nullptr; + // Check if game can find target + auto roomObj = Screeps::Game.getObjectById(target_id); + if (!roomObj) { + JS::console.log(creep.name() + + ": Game can\'t find target id: " + target_id); + return nullptr; + } + return std::move(roomObj); } -bool DouwcoHivemind::CreepBase::isNearTo(const Screeps::RoomPosition &pos, int dist) -{ - return DouwcoHivemind::isNearTo(creep.pos(), pos, dist); +bool DouwcoHivemind::CreepBase::isNearTo(const Screeps::RoomPosition &pos, + int dist) { + return DouwcoHivemind::isNearTo(creep.pos(), pos, dist); } \ No newline at end of file diff --git a/douwco_hivemind/src/Creeps/Maintainer.cpp b/douwco_hivemind/src/Creeps/Maintainer.cpp index dbc9c0a..ffec34c 100644 --- a/douwco_hivemind/src/Creeps/Maintainer.cpp +++ b/douwco_hivemind/src/Creeps/Maintainer.cpp @@ -17,6 +17,7 @@ #include "Creeps/Maintainer.hpp" #include "Creeps/CreepBase.hpp" +#include "Screeps/Constants.hpp" void DouwcoHivemind::Maintainer::depositEnergy() { @@ -61,22 +62,24 @@ std::unique_ptr DouwcoHivemind::Maintainer::getDamagedStruct void DouwcoHivemind::Maintainer::searchDamagedStructure() { - int lowestMaintaince = INT16_MAX; + int highestDamage = 0; Screeps::Structure *selectedStructure; auto structures = creep.room().find(Screeps::FIND_STRUCTURES); for (auto &structureObject : structures) { - auto structure = dynamic_cast(structureObject.get()); + auto structure = dynamic_cast(structureObject.release()); if (!structure) continue; - int maintance = structure->hits(); + int damage = structure->hitsMax() - structure->hits(); auto structureType = structure->structureType(); if (structureType == Screeps::STRUCTURE_CONTROLLER) continue; - if (maintance < lowestMaintaince) + if(structureType == Screeps::STRUCTURE_WALL || structureType == Screeps::STRUCTURE_RAMPART) + continue; // todo cover this situation as well. + if (highestDamage < damage) { - lowestMaintaince = maintance; + highestDamage = damage; selectedStructure = structure; } } diff --git a/douwco_hivemind/src/Creeps/Miner.cpp b/douwco_hivemind/src/Creeps/Miner.cpp new file mode 100644 index 0000000..12e7e37 --- /dev/null +++ b/douwco_hivemind/src/Creeps/Miner.cpp @@ -0,0 +1,42 @@ +#include "Creeps/Miner.hpp" +#include "Creeps/CreepBase.hpp" +#include "Screeps/Constants.hpp" +#include "Screeps/Creep.hpp" +#include "Screeps/Room.hpp" +#include "Screeps/RoomPosition.hpp" +#include "Screeps/Source.hpp" +#include + +DouwcoHivemind::Miner::Miner(Screeps::Creep crp) : CreepBase(crp) { + arrivedAtContainer = memory.contains("arrivedAtContainer") + ? static_cast(memory["arrivedAtContainer"]) + : false; +} + +DouwcoHivemind::Miner::~Miner() { + memory["arrivedAtContainer"] = arrivedAtContainer; +} + +void DouwcoHivemind::Miner::loop() { + if (arrivedAtContainer) { + auto source = + static_cast(getRoomObjectTarget().release()); + creep.harvest(*source); + } else { + auto container = getRoomObjectTarget(); + if (!container) + return; + if (isNearTo(container->pos(), 0)) { + arrivedAtContainer = true; + int x = creep.pos().x(); + int y = creep.pos().y(); + auto roomObjs = creep.room().lookForAtArea(Screeps::LOOK_SOURCES, y - 1, + x - 1, y + 1, x + 1); + return; // fault is here somewhere + auto source = static_cast(roomObjs[0].release()); + target_id = source->id(); + } else { + moveToTarget(0); + } + } +} \ No newline at end of file diff --git a/douwco_hivemind/src/Creeps/Worker.cpp b/douwco_hivemind/src/Creeps/Worker.cpp index ce2685b..e9003e0 100644 --- a/douwco_hivemind/src/Creeps/Worker.cpp +++ b/douwco_hivemind/src/Creeps/Worker.cpp @@ -1,123 +1,130 @@ -#include -#include #include +#include +#include +#include -#include +#include #include -#include +#include #include -#include #include +#include +#include +#include #include #include -#include #include +#include #include -#include -#include #include "Creeps/Worker.hpp" +#include "Screeps/StructureContainer.hpp" -DouwcoHivemind::Worker::Worker(Screeps::Creep crp) : CreepBase(crp) -{ - harvesting = memory.contains("harvesting") ? static_cast(memory["harvesting"]) : false; +DouwcoHivemind::Worker::Worker(Screeps::Creep crp) : CreepBase(crp) { + harvesting = memory.contains("harvesting") + ? static_cast(memory["harvesting"]) + : false; } -DouwcoHivemind::Worker::~Worker() -{ - memory["harvesting"] = harvesting; +DouwcoHivemind::Worker::~Worker() { memory["harvesting"] = harvesting; } + +void DouwcoHivemind::Worker::loop() { + if (harvesting) { + if (creep.store().getFreeCapacity(Screeps::RESOURCE_ENERGY) == 0) { + harvesting = false; + target_id.clear(); + } + getEnergy(); + } else { + if (creep.store().getUsedCapacity(Screeps::RESOURCE_ENERGY) == 0) { + harvesting = true; + target_id.clear(); + } + depositEnergy(); + } } -void DouwcoHivemind::Worker::loop() -{ - if (harvesting) - { - if (creep.store().getFreeCapacity(Screeps::RESOURCE_ENERGY) == 0) - { - harvesting = false; - target_id.clear(); - } - harvestSource(); - } - else - { - if (creep.store().getUsedCapacity(Screeps::RESOURCE_ENERGY) == 0) - { - harvesting = true; - target_id.clear(); - } - depositEnergy(); - } -} - -void DouwcoHivemind::Worker::harvestSource() -{ - auto source = getSourceTarget(); - if (!source) - return; - - if (isNearTo(source->pos(), 1)) - { - int resp = creep.harvest(*source); - } - else - { +void DouwcoHivemind::Worker::getEnergy() { + for (auto containersJSON : creep.room().memory()["sourceContainers"]) { + auto containerID = static_cast(containersJSON); + target_id = containerID; + auto containerRoomObject = getRoomObjectTarget(); + auto container = static_cast( + containerRoomObject.release()); + if (0 < container->store().getUsedCapacity(Screeps::RESOURCE_ENERGY)) { + if (isNearTo(container->pos(), 1)) { + int resp = creep.withdraw(*container, Screeps::RESOURCE_ENERGY); + } else { moveToTarget(); + } } + } + harvestSource(); } -std::unique_ptr DouwcoHivemind::Worker::getSourceTarget() -{ - auto roomObj = getRoomObjectTarget(); - if (!roomObj) - { - searchSource(); - return nullptr; - } +void DouwcoHivemind::Worker::harvestSource() { + auto source = getSourceTarget(); + if (!source) + return; - // Check if found roomobject is an actual source - auto source = std::unique_ptr(dynamic_cast(roomObj.release())); - if (!source) - { - // EM_ASM({console.log($0 + ': Can\'t cast target to Source')}, creep.name().c_str()); - searchSource(); - return nullptr; - } + if (isNearTo(source->pos(), 1)) { + int resp = creep.harvest(*source); + } else { + moveToTarget(); + } +} - // Check if the source still has energy to harvest +std::unique_ptr DouwcoHivemind::Worker::getSourceTarget() { + auto roomObj = getRoomObjectTarget(); + if (!roomObj) { + searchSource(); + return nullptr; + } + + // Check if found roomobject is an actual source + auto source = std::unique_ptr( + dynamic_cast(roomObj.release())); + if (!source) { + // EM_ASM({console.log($0 + ': Can\'t cast target to Source')}, + // creep.name().c_str()); + searchSource(); + return nullptr; + } + + // Check if the source still has energy to harvest + if (source->energy() == 0) { + searchSource(); + return nullptr; + } + + return std::move(source); +} + +void DouwcoHivemind::Worker::searchSource() { + target_id.clear(); + + auto sources = creep.room().find(Screeps::FIND_SOURCES_ACTIVE); + if (sources.size() == 0) + return; + + int x = creep.pos().x(); + int y = creep.pos().y(); + + int closestDistance = INT16_MAX; + Screeps::Source *selectedSource; + for (auto &sourceObject : sources) { + auto source = dynamic_cast(sourceObject.get()); if (source->energy() == 0) - { - searchSource(); - return nullptr; + continue; + int dx = source->pos().x() - x; + int dy = source->pos().y() - y; + int distance = dx * dx + dy * dy; + if (distance < closestDistance) { + closestDistance = distance; + selectedSource = source; } - - return std::move(source); -} - -void DouwcoHivemind::Worker::searchSource() -{ - target_id.clear(); - - auto sources = creep.room().find(Screeps::FIND_SOURCES_ACTIVE); - if (sources.size() == 0) - return; - - int x = creep.pos().x(); - int y = creep.pos().y(); - - int closestDistance = INT16_MAX; - Screeps::Source *selectedSource; - for(auto &sourceObject : sources){ - auto source =dynamic_cast(sourceObject.get()); - if(source->energy()==0) continue; - int dx = source->pos().x() - x; - int dy = source->pos().y() - y; - int distance = dx*dx + dy*dy; - if(distance < closestDistance){ - closestDistance = distance; - selectedSource = source; - } - } - if(!selectedSource) return; - target_id = selectedSource->id(); + } + if (!selectedSource) + return; + target_id = selectedSource->id(); } diff --git a/douwco_hivemind/src/Engine.cpp b/douwco_hivemind/src/Engine.cpp index 9f17eca..b0a55f1 100644 --- a/douwco_hivemind/src/Engine.cpp +++ b/douwco_hivemind/src/Engine.cpp @@ -6,8 +6,10 @@ #include "Creeps/Builder.hpp" #include "Creeps/Maintainer.hpp" +#include "Creeps/Miner.hpp" #include "Creeps/Supplier.hpp" #include "Creeps/Upgrader.hpp" +#include "Creeps/Miner.hpp" #include "Structures/Spawn.hpp" @@ -57,6 +59,8 @@ void DouwcoHivemind::Engine::ReadOutCreeps() { creeps.push_back(std::make_unique(creep.second)); else if (role == CreepRole::MAINTAINER) creeps.push_back(std::make_unique(creep.second)); + else if (role == CreepRole::MINER) + creeps.push_back(std::make_unique(creep.second)); } } diff --git a/douwco_hivemind/src/Room.cpp b/douwco_hivemind/src/Entity/Room.cpp similarity index 98% rename from douwco_hivemind/src/Room.cpp rename to douwco_hivemind/src/Entity/Room.cpp index 7d5ca60..8fd52c7 100644 --- a/douwco_hivemind/src/Room.cpp +++ b/douwco_hivemind/src/Entity/Room.cpp @@ -1,4 +1,4 @@ -#include "Room.hpp" +#include "Entity/Room.hpp" #include "Screeps/Constants.hpp" #include "Screeps/Room.hpp" diff --git a/douwco_hivemind/src/Structures/Spawn.cpp b/douwco_hivemind/src/Structures/Spawn.cpp index 0d15a0b..c602990 100644 --- a/douwco_hivemind/src/Structures/Spawn.cpp +++ b/douwco_hivemind/src/Structures/Spawn.cpp @@ -4,70 +4,76 @@ #include "Creeps/CreepBase.hpp" #include "Structures/Spawn.hpp" -void DouwcoHivemind::Spawn::loop() -{ - // Only run every 50 ticks - if (Screeps::Game.time() % 50 != 0) - return; +void DouwcoHivemind::Spawn::loop() { + // Only run every 50 ticks + if (Screeps::Game.time() % 50 != 0) + return; - 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_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()) - { - CreepRole role = creep.second.memory()["role"]; + int energyAvailable = spawn.room().energyAvailable(); + int energyCapacityAvailable = spawn.room().energyCapacityAvailable(); - if (role == CreepRole::SUPPLIER) - required_suppliers--; - else if (role == CreepRole::UPGRADER) - required_upgraders--; - else if (role == CreepRole::MAINTAINER) - required_maintainers--; - else if (role == CreepRole::BUILDER) - required_builders--; - } + int required_upgraders = 1; + int required_suppliers = 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; + int required_miners = 1; //spawn.room().memory()["sourceContainers"].size(); + for (auto &creep : Screeps::Game.creeps()) { + CreepRole role = creep.second.memory()["role"]; - // if (energyAvailable < energyCapacityAvailable && 3 < Screeps::Game.creeps().size()) - // return; - std::string name; - JSON opts; - if (required_suppliers > 0) - { - opts["memory"]["role"] = CreepRole::SUPPLIER; - name = "Supplier: "; - } - else if (required_upgraders > 0) - { - opts["memory"]["role"] = CreepRole::UPGRADER; - name = "Upgrader: "; - } - else if (required_builders > 0) - { - opts["memory"]["role"] = CreepRole::BUILDER; - name = "Builder: "; - } - else if (required_maintainers > 0) - { - opts["memory"]["role"] = CreepRole::MAINTAINER; - name = "Maintainer: "; - } - else - return; + if (role == CreepRole::SUPPLIER) + required_suppliers--; + else if (role == CreepRole::UPGRADER) + required_upgraders--; + else if (role == CreepRole::MAINTAINER) + required_maintainers--; + else if (role == CreepRole::BUILDER) + required_builders--; + else if (role == CreepRole::MINER) + required_miners--; + } - std::vector body; - for (int i = 0; i < energyAvailable / 200; i++) - { - body.push_back("work"); - body.push_back("carry"); - body.push_back("move"); - } + // if (energyAvailable < energyCapacityAvailable && 3 < + // Screeps::Game.creeps().size()) + // return; + std::string name; + JSON opts; + if (required_upgraders > 0) { + opts["memory"]["role"] = CreepRole::UPGRADER; + name = "Upgrader: "; + } else if (required_miners > 0) { + opts["memory"]["role"] = CreepRole::MINER; + opts["memory"]["target_id"] = + spawn.room().memory()["sourceContainers"][0]; // make logic for more + name = "Miner: "; + } else if (required_suppliers > 0) { + opts["memory"]["role"] = CreepRole::SUPPLIER; + name = "Supplier: "; + } else if (required_builders > 0) { + opts["memory"]["role"] = CreepRole::BUILDER; + name = "Builder: "; + } else if (required_maintainers > 0) { + opts["memory"]["role"] = CreepRole::MAINTAINER; + name = "Maintainer: "; + } else + return; - spawn.spawnCreep( - body, - name + std::to_string(Screeps::Game.time()), - opts); + std::vector body; + if (opts["memory"]["role"] == CreepRole::MINER) { + body.push_back("move"); + for (int i = 0; i < (energyAvailable - 50) / 100 && i < 5; i++) { + body.push_back("work"); + } + } else { + for (int i = 0; i < energyAvailable / 200; i++) { + body.push_back("work"); + body.push_back("carry"); + body.push_back("move"); + } + } + + spawn.spawnCreep(body, name + std::to_string(Screeps::Game.time()), opts); } \ No newline at end of file diff --git a/screepsxx b/screepsxx index bf5fd1f..b81be2b 160000 --- a/screepsxx +++ b/screepsxx @@ -1 +1 @@ -Subproject commit bf5fd1f69b251c6829d7847e3d5d2dbc7764a3b9 +Subproject commit b81be2be5d0eb6f6a19e5a0abf8bf199a61ec7e5