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

View File

@@ -57,7 +57,7 @@ std::unique_ptr<Screeps::ConstructionSite> DouwcoHivemind::Builder::getConstruct
void DouwcoHivemind::Builder::searchConstructionSite()
{
int leastProgressLeft = INT16_MAX;
int leastProgressLeft = INT32_MAX;
Screeps::ConstructionSite *selectedConstructionSite;
auto constructionSites = creep.room().find(Screeps::FIND_MY_CONSTRUCTION_SITES);
for (auto &constructionSiteObject : constructionSites)
@@ -68,7 +68,7 @@ void DouwcoHivemind::Builder::searchConstructionSite()
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)
{
leastProgressLeft = progressLeft;

View File

@@ -1,6 +1,7 @@
#include <Screeps/Game.hpp>
#include <Screeps/Memory.hpp>
#include <Screeps/StructureController.hpp>
#include <algorithm>
#include <memory>
#include "Engine.hpp"
@@ -10,7 +11,6 @@
#include "Creeps/Miner.hpp"
#include "Creeps/Supplier.hpp"
#include "Creeps/Upgrader.hpp"
#include "Creeps/Miner.hpp"
#include "Screeps/Constants.hpp"
#include "Screeps/StructureSpawn.hpp"
@@ -18,23 +18,26 @@
#include "Structures/Tower.hpp"
DouwcoHivemind::Engine::Engine() {
JS::console.log(std::string("Reading out owned rooms"));
ReadOutRooms();
JS::console.log(std::string("Reading out creeps"));
ReadOutCreeps();
JS::console.log(std::string("Reading out structures"));
ReadOutStructures();
}
void DouwcoHivemind::Engine::loop() {
JS::console.log(std::string("Iterating over rooms"));
for (auto &room : rooms)
room->loop();
for (auto &room : Rooms)
room.second->loop();
JS::console.log(std::string("Iterating over creeps"));
for (auto &creep : creeps)
creep->loop();
for (auto &creep : Creeps)
creep.second->loop();
JS::console.log(std::string("Iterating over structures"));
for (auto &structure : structures)
structure->loop();
for (auto &structure : Structures)
structure.second->loop();
if (Screeps::Game.time() % 100 == 0) {
clearDeadCreepMemory();
@@ -42,42 +45,45 @@ void DouwcoHivemind::Engine::loop() {
}
void DouwcoHivemind::Engine::ReadOutRooms() {
JS::console.log(std::string("Reading out owned rooms"));
auto src_rooms = Screeps::Game.rooms();
for (auto &room : src_rooms) {
if(!room.second.controller().value().my()) continue;
rooms.push_back(std::make_unique<Room>(room.second));
if (!room.second.controller().value().my())
continue;
Rooms.emplace(room.first, std::make_unique<Room>(room.second));
}
}
void DouwcoHivemind::Engine::ReadOutCreeps() {
JS::console.log(std::string("Reading out creeps"));
auto src_creeps = Screeps::Game.creeps();
for (auto &creep : src_creeps) {
CreepRole role = creep.second.memory()["role"];
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)
creeps.push_back(std::make_unique<Upgrader>(creep.second));
Creeps.emplace(creep.first, std::make_unique<Upgrader>(creep.second));
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)
creeps.push_back(std::make_unique<Maintainer>(creep.second));
Creeps.emplace(creep.first, std::make_unique<Maintainer>(creep.second));
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() {
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) {
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)));
if (structureType == Screeps::STRUCTURE_TOWER) {
Structures.emplace(
structure.first,
std::make_unique<Tower>(
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 "Creeps/CreepBase.hpp"
#include "Engine.hpp"
#include "Structures/Spawn.hpp"
void DouwcoHivemind::Spawn::loop() {
// Only run every 500 ticks
if (Screeps::Game.time() % 100 != 0)
return;
// Get energy data
int energyAvailable = spawn.room().energyAvailable();
int energyCapacityAvailable = spawn.room().energyCapacityAvailable();