105 lines
3.1 KiB
C++
105 lines
3.1 KiB
C++
#include <Screeps/Game.hpp>
|
|
#include <Screeps/Room.hpp>
|
|
|
|
#include "Creeps/CreepBase.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();
|
|
|
|
// Calculate needed creep roles
|
|
int required_upgraders = 1;
|
|
int required_suppliers = 2;
|
|
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();
|
|
|
|
// 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()) {
|
|
CreepRole role = creep.second.memory()["role"];
|
|
|
|
if (role == CreepRole::SUPPLIER)
|
|
existing_suppliers++;
|
|
else if (role == CreepRole::UPGRADER)
|
|
existing_upgraders++;
|
|
else if (role == CreepRole::MAINTAINER)
|
|
existing_maintainers++;
|
|
else if (role == CreepRole::BUILDER)
|
|
existing_builders++;
|
|
else if (role == CreepRole::MINER)
|
|
existing_miners++;
|
|
}
|
|
|
|
// Check if max energy used
|
|
if (energyAvailable < energyCapacityAvailable && 0 < existing_suppliers)
|
|
return;
|
|
|
|
// Set creep properties
|
|
std::string name;
|
|
JSON opts;
|
|
CreepRole role = CreepRole::UNEMPLOYED;
|
|
if (required_miners > existing_miners) {
|
|
role = CreepRole::MINER;
|
|
opts["memory"]["target_id"] =
|
|
spawn.room().memory()["sourceContainers"][0]; // make logic for more
|
|
name = "Miner: ";
|
|
} else if (required_suppliers > existing_suppliers) {
|
|
role = CreepRole::SUPPLIER;
|
|
name = "Supplier: ";
|
|
} else if (required_upgraders > existing_upgraders) {
|
|
role = CreepRole::UPGRADER;
|
|
name = "Upgrader: ";
|
|
} else if (required_builders > existing_builders) {
|
|
role = CreepRole::BUILDER;
|
|
name = "Builder: ";
|
|
} else if (required_maintainers > existing_maintainers) {
|
|
role = CreepRole::MAINTAINER;
|
|
name = "Maintainer: ";
|
|
} else
|
|
return;
|
|
|
|
opts["memory"]["role"] = role;
|
|
|
|
// Build creep body
|
|
std::vector<std::string> body;
|
|
if (role == CreepRole::MINER) {
|
|
body.push_back("move");
|
|
body.push_back("work");
|
|
for (int i = 0; i < (energyAvailable - 150) / 100 && i < 5; i++) {
|
|
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 {
|
|
for (int i = 0; i < energyAvailable / 200; i++) {
|
|
body.push_back("work");
|
|
body.push_back("carry");
|
|
body.push_back("move");
|
|
}
|
|
}
|
|
|
|
// Create creep
|
|
spawn.spawnCreep(body, name + std::to_string(Screeps::Game.time()), opts);
|
|
} |