Changed engine vectors to mappings for later lookup functionality.

This commit is contained in:
2026-07-10 10:24:19 +02:00
parent 09de8b5b4b
commit a2d12f691f
5 changed files with 40 additions and 35 deletions

View File

@@ -1,6 +1,8 @@
#ifndef DOUWCO_HIVEMIND_ENGINE_HPP #ifndef DOUWCO_HIVEMIND_ENGINE_HPP
#define DOUWCO_HIVEMIND_ENGINE_HPP #define DOUWCO_HIVEMIND_ENGINE_HPP
#include <map>
#include <memory>
#include <vector> #include <vector>
#include "Creeps/CreepBase.hpp" #include "Creeps/CreepBase.hpp"
@@ -9,10 +11,10 @@
namespace DouwcoHivemind { namespace DouwcoHivemind {
class Engine { class Engine {
private: public:
std::vector<std::unique_ptr<Room>> rooms; std::map<std::string, std::unique_ptr<Room>> Rooms;
std::vector<std::unique_ptr<CreepBase>> creeps; std::map<std::string, std::unique_ptr<CreepBase>> Creeps;
std::vector<std::unique_ptr<StructureBase>> structures; std::map<std::string, std::unique_ptr<StructureBase>> Structures;
public: public:
// Readout data from screeps API into C++ vectors // Readout data from screeps API into C++ vectors
@@ -30,6 +32,6 @@ private:
// Clear dead screep memory // Clear dead screep memory
void clearDeadCreepMemory(); void clearDeadCreepMemory();
}; };
} } // namespace DouwcoHivemind
#endif // DOUWCO_HIVEMIND_ENGINE_HPP #endif // DOUWCO_HIVEMIND_ENGINE_HPP

View File

@@ -57,7 +57,7 @@ std::unique_ptr<Screeps::ConstructionSite> DouwcoHivemind::Builder::getConstruct
void DouwcoHivemind::Builder::searchConstructionSite() void DouwcoHivemind::Builder::searchConstructionSite()
{ {
int leastProgressLeft = INT16_MAX; int leastProgressLeft = INT32_MAX;
Screeps::ConstructionSite *selectedConstructionSite; Screeps::ConstructionSite *selectedConstructionSite;
auto constructionSites = creep.room().find(Screeps::FIND_MY_CONSTRUCTION_SITES); auto constructionSites = creep.room().find(Screeps::FIND_MY_CONSTRUCTION_SITES);
for (auto &constructionSiteObject : constructionSites) for (auto &constructionSiteObject : constructionSites)
@@ -68,7 +68,7 @@ void DouwcoHivemind::Builder::searchConstructionSite()
int progressLeft = constructionSite->progressTotal() - constructionSite->progress(); int progressLeft = constructionSite->progressTotal() - constructionSite->progress();
if(constructionSite->structureType() == Screeps::STRUCTURE_ROAD) progressLeft *= 100; if(constructionSite->structureType() == Screeps::STRUCTURE_ROAD) progressLeft *= 10;
if (progressLeft < leastProgressLeft) if (progressLeft < leastProgressLeft)
{ {
leastProgressLeft = progressLeft; leastProgressLeft = progressLeft;

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 <algorithm>
#include <memory> #include <memory>
#include "Engine.hpp" #include "Engine.hpp"
@@ -10,7 +11,6 @@
#include "Creeps/Miner.hpp" #include "Creeps/Miner.hpp"
#include "Creeps/Supplier.hpp" #include "Creeps/Supplier.hpp"
#include "Creeps/Upgrader.hpp" #include "Creeps/Upgrader.hpp"
#include "Creeps/Miner.hpp"
#include "Screeps/Constants.hpp" #include "Screeps/Constants.hpp"
#include "Screeps/StructureSpawn.hpp" #include "Screeps/StructureSpawn.hpp"
@@ -18,23 +18,26 @@
#include "Structures/Tower.hpp" #include "Structures/Tower.hpp"
DouwcoHivemind::Engine::Engine() { DouwcoHivemind::Engine::Engine() {
JS::console.log(std::string("Reading out owned rooms"));
ReadOutRooms(); ReadOutRooms();
JS::console.log(std::string("Reading out creeps"));
ReadOutCreeps(); ReadOutCreeps();
JS::console.log(std::string("Reading out structures"));
ReadOutStructures(); ReadOutStructures();
} }
void DouwcoHivemind::Engine::loop() { void DouwcoHivemind::Engine::loop() {
JS::console.log(std::string("Iterating over rooms")); JS::console.log(std::string("Iterating over rooms"));
for (auto &room : rooms) for (auto &room : Rooms)
room->loop(); room.second->loop();
JS::console.log(std::string("Iterating over creeps")); JS::console.log(std::string("Iterating over creeps"));
for (auto &creep : creeps) for (auto &creep : Creeps)
creep->loop(); creep.second->loop();
JS::console.log(std::string("Iterating over structures")); JS::console.log(std::string("Iterating over structures"));
for (auto &structure : structures) for (auto &structure : Structures)
structure->loop(); structure.second->loop();
if (Screeps::Game.time() % 100 == 0) { if (Screeps::Game.time() % 100 == 0) {
clearDeadCreepMemory(); clearDeadCreepMemory();
@@ -42,42 +45,45 @@ void DouwcoHivemind::Engine::loop() {
} }
void DouwcoHivemind::Engine::ReadOutRooms() { void DouwcoHivemind::Engine::ReadOutRooms() {
JS::console.log(std::string("Reading out owned rooms"));
auto src_rooms = Screeps::Game.rooms(); auto src_rooms = Screeps::Game.rooms();
for (auto &room : src_rooms) { for (auto &room : src_rooms) {
if(!room.second.controller().value().my()) continue; if (!room.second.controller().value().my())
rooms.push_back(std::make_unique<Room>(room.second)); continue;
Rooms.emplace(room.first, std::make_unique<Room>(room.second));
} }
} }
void DouwcoHivemind::Engine::ReadOutCreeps() { void DouwcoHivemind::Engine::ReadOutCreeps() {
JS::console.log(std::string("Reading out creeps"));
auto src_creeps = Screeps::Game.creeps(); auto src_creeps = Screeps::Game.creeps();
for (auto &creep : src_creeps) { for (auto &creep : src_creeps) {
CreepRole role = creep.second.memory()["role"]; CreepRole role = creep.second.memory()["role"];
if (role == CreepRole::SUPPLIER) if (role == CreepRole::SUPPLIER)
creeps.push_back(std::make_unique<Supplier>(creep.second)); Creeps.emplace(creep.first, std::make_unique<Supplier>(creep.second));
else if (role == CreepRole::UPGRADER) else if (role == CreepRole::UPGRADER)
creeps.push_back(std::make_unique<Upgrader>(creep.second)); Creeps.emplace(creep.first, std::make_unique<Upgrader>(creep.second));
else if (role == CreepRole::BUILDER) else if (role == CreepRole::BUILDER)
creeps.push_back(std::make_unique<Builder>(creep.second)); Creeps.emplace(creep.first, std::make_unique<Builder>(creep.second));
else if (role == CreepRole::MAINTAINER) else if (role == CreepRole::MAINTAINER)
creeps.push_back(std::make_unique<Maintainer>(creep.second)); Creeps.emplace(creep.first, std::make_unique<Maintainer>(creep.second));
else if (role == CreepRole::MINER) else if (role == CreepRole::MINER)
creeps.push_back(std::make_unique<Miner>(creep.second)); Creeps.emplace(creep.first, std::make_unique<Miner>(creep.second));
} }
} }
void DouwcoHivemind::Engine::ReadOutStructures() { void DouwcoHivemind::Engine::ReadOutStructures() {
JS::console.log(std::string("Reading out structures")); auto structures_src = Screeps::Game.structures();
auto structures_src =Screeps::Game.structures();
for (auto &structure : structures_src) { for (auto &structure : structures_src) {
std::string structureType = structure.second.structureType(); std::string structureType = structure.second.structureType();
if(structureType == Screeps::STRUCTURE_TOWER){ if (structureType == Screeps::STRUCTURE_TOWER) {
structures.push_back(std::make_unique<Tower>(static_cast<Screeps::StructureTower>(structure.second))); Structures.emplace(
} structure.first,
else if(structureType == Screeps::STRUCTURE_SPAWN){ std::make_unique<Tower>(
structures.push_back(std::make_unique<Spawn>(static_cast<Screeps::StructureSpawn>(structure.second))); static_cast<Screeps::StructureTower>(structure.second)));
} else if (structureType == Screeps::STRUCTURE_SPAWN) {
Structures.emplace(
structure.first,
std::make_unique<Spawn>(
static_cast<Screeps::StructureSpawn>(structure.second)));
} }
} }
} }

View File

@@ -3,13 +3,10 @@
#include <Screeps/StructureController.hpp> #include <Screeps/StructureController.hpp>
#include "Creeps/CreepBase.hpp" #include "Creeps/CreepBase.hpp"
#include "Engine.hpp"
#include "Structures/Spawn.hpp" #include "Structures/Spawn.hpp"
void DouwcoHivemind::Spawn::loop() { void DouwcoHivemind::Spawn::loop() {
// Only run every 500 ticks
if (Screeps::Game.time() % 100 != 0)
return;
// Get energy data // Get energy data
int energyAvailable = spawn.room().energyAvailable(); int energyAvailable = spawn.room().energyAvailable();
int energyCapacityAvailable = spawn.room().energyCapacityAvailable(); int energyCapacityAvailable = spawn.room().energyCapacityAvailable();