Added custom move behaviour to creeps
This commit is contained in:
64
douwco_hivemind/src/creep.cpp
Normal file
64
douwco_hivemind/src/creep.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#include <algorithm>
|
||||
|
||||
#include <Screeps/Game.hpp>
|
||||
#include <Screeps/Room.hpp>
|
||||
#include <Screeps/RoomPosition.hpp>
|
||||
|
||||
#include "Creeps/Creep.hpp"
|
||||
|
||||
void DouwcoHivemind::Creep::moveToTarget(int dist)
|
||||
{
|
||||
auto target = getRoomObjectTarget();
|
||||
|
||||
if (isNearTo(target->pos(), dist))
|
||||
return;
|
||||
|
||||
if (path.size() == 0){
|
||||
path = creep.room().findPath(creep.pos(), target->pos());
|
||||
std::reverse(path.begin(), path.end());
|
||||
}
|
||||
|
||||
JS::console.log(std::string("creep pos: [") +
|
||||
std::to_string(creep.pos().x()) +
|
||||
std::string(",") +
|
||||
std::to_string(creep.pos().y()) +
|
||||
std::string("]"));
|
||||
|
||||
auto step = path.back();
|
||||
path.pop_back();
|
||||
|
||||
if (creep.pos().x() == step.x - step.dx && creep.pos().y() == step.y - step.dy)
|
||||
{
|
||||
int resp = creep.move(step.direction);
|
||||
if(resp != Screeps::ERR_INVALID_ARGS) return;
|
||||
}
|
||||
|
||||
path.clear();
|
||||
}
|
||||
|
||||
std::unique_ptr<Screeps::RoomObject> DouwcoHivemind::Creep::getRoomObjectTarget()
|
||||
{
|
||||
// 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");
|
||||
return nullptr;
|
||||
}
|
||||
return std::move(roomObj);
|
||||
}
|
||||
|
||||
bool DouwcoHivemind::Creep::isNearTo(const Screeps::RoomPosition &pos, int dist)
|
||||
{
|
||||
return isNearTo(creep.pos(), pos, dist);
|
||||
}
|
||||
|
||||
bool DouwcoHivemind::Creep::isNearTo(const Screeps::RoomPosition &pos1, const Screeps::RoomPosition &pos2, int dist)
|
||||
{
|
||||
int dx = pos1.x() - pos2.x();
|
||||
int dy = pos1.y() - pos2.y();
|
||||
int dist2 = dist * dist;
|
||||
return dx * dx <= dist2 &&
|
||||
dy * dy <= dist2 &&
|
||||
pos1.roomName() == pos2.roomName();
|
||||
}
|
||||
236
douwco_hivemind/src/harvester.cpp
Normal file
236
douwco_hivemind/src/harvester.cpp
Normal file
@@ -0,0 +1,236 @@
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include <emscripten.h>
|
||||
|
||||
#include <Screeps/Game.hpp>
|
||||
#include <Screeps/Creep.hpp>
|
||||
#include <Screeps/Source.hpp>
|
||||
#include <Screeps/Room.hpp>
|
||||
#include <Screeps/RoomPosition.hpp>
|
||||
#include <Screeps/RoomObject.hpp>
|
||||
#include <Screeps/Structure.hpp>
|
||||
#include <Screeps/StructureController.hpp>
|
||||
#include <Screeps/StructureSpawn.hpp>
|
||||
#include <Screeps/StructureExtension.hpp>
|
||||
#include <Screeps/StructureTower.hpp>
|
||||
#include <Screeps/Constants.hpp>
|
||||
#include <Screeps/Store.hpp>
|
||||
|
||||
#include "Creeps/Harvester.hpp"
|
||||
|
||||
void DouwcoHivemind::HarvesterRole::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::HarvesterRole::harvestSource()
|
||||
{
|
||||
auto source = getSourceTarget();
|
||||
if (!source)
|
||||
return;
|
||||
|
||||
if (isNearTo(source->pos(), 1))
|
||||
{
|
||||
int resp = creep.harvest(*source);
|
||||
}
|
||||
else
|
||||
{
|
||||
moveToTarget();
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<Screeps::Source> DouwcoHivemind::HarvesterRole::getSourceTarget()
|
||||
{
|
||||
auto roomObj = getRoomObjectTarget();
|
||||
if (!roomObj)
|
||||
{
|
||||
searchSource();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Check if found roomobject is an actual source
|
||||
auto source = std::unique_ptr<Screeps::Source>(dynamic_cast<Screeps::Source *>(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::HarvesterRole::searchSource()
|
||||
{
|
||||
target_id.clear();
|
||||
|
||||
auto sources = creep.room().find(Screeps::FIND_SOURCES_ACTIVE);
|
||||
if (sources.size() == 0)
|
||||
return;
|
||||
|
||||
Screeps::Source *selectedSource;
|
||||
int maxEnergy = 0;
|
||||
for (auto &sourceObj : sources)
|
||||
{
|
||||
auto source = dynamic_cast<Screeps::Source *>(sourceObj.get());
|
||||
if (!source)
|
||||
continue;
|
||||
|
||||
auto sourceEnergy = source->energy();
|
||||
if (sourceEnergy > maxEnergy)
|
||||
{
|
||||
maxEnergy = sourceEnergy;
|
||||
selectedSource = source;
|
||||
}
|
||||
}
|
||||
|
||||
if (!selectedSource)
|
||||
{
|
||||
// EM_ASM({console.log($0 + ': No sources with energy found!')}, creep.name().c_str());
|
||||
return;
|
||||
}
|
||||
target_id = selectedSource->id();
|
||||
}
|
||||
|
||||
void DouwcoHivemind::HarvesterRole::depositEnergy()
|
||||
{
|
||||
auto structure = getDepositTarget();
|
||||
if (!structure)
|
||||
return;
|
||||
|
||||
if (structure->structureType() == Screeps::STRUCTURE_CONTROLLER)
|
||||
{
|
||||
if (isNearTo(structure->pos(), 3))
|
||||
{
|
||||
auto controller = dynamic_cast<Screeps::StructureController *>(structure.get());
|
||||
if (!controller)
|
||||
return;
|
||||
int resp = creep.upgradeController(*controller);
|
||||
}
|
||||
else
|
||||
{
|
||||
moveToTarget();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isNearTo(structure->pos(), 1))
|
||||
{
|
||||
int resp = creep.transfer(*structure, Screeps::RESOURCE_ENERGY);
|
||||
}
|
||||
else
|
||||
{
|
||||
moveToTarget();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<Screeps::Structure> DouwcoHivemind::HarvesterRole::getDepositTarget()
|
||||
{
|
||||
auto roomObj = getRoomObjectTarget();
|
||||
if (!roomObj)
|
||||
{
|
||||
searchDeposit();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Check if found roomobject is an actual structure
|
||||
auto structure = std::unique_ptr<Screeps::Structure>(dynamic_cast<Screeps::Structure *>(roomObj.release()));
|
||||
if (!structure)
|
||||
{
|
||||
// EM_ASM({console.log($0 + ': Can\'t cast target to Source')}, creep.name().c_str());
|
||||
searchDeposit();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Check if the structure can receive energy to harvest
|
||||
int energyCapacity;
|
||||
auto structureType = structure->structureType();
|
||||
if (structureType == Screeps::STRUCTURE_CONTROLLER)
|
||||
{
|
||||
energyCapacity = 1;
|
||||
}
|
||||
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();
|
||||
}
|
||||
if (energyCapacity == 0)
|
||||
{
|
||||
searchDeposit();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return std::move(structure);
|
||||
}
|
||||
|
||||
void DouwcoHivemind::HarvesterRole::searchDeposit()
|
||||
{
|
||||
int highestEnergyNeed = 0;
|
||||
Screeps::Structure *selectedStructure;
|
||||
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 energyNeed;
|
||||
|
||||
auto structureType = structure->structureType();
|
||||
if (structureType == Screeps::STRUCTURE_SPAWN)
|
||||
{
|
||||
auto spawn = dynamic_cast<Screeps::StructureSpawn *>(structure);
|
||||
energyNeed = spawn->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
|
||||
}
|
||||
else if (structureType == Screeps::STRUCTURE_EXTENSION)
|
||||
{
|
||||
auto extension = dynamic_cast<Screeps::StructureExtension *>(structure);
|
||||
energyNeed = extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
|
||||
}
|
||||
else if (structureType == Screeps::STRUCTURE_CONTROLLER)
|
||||
{
|
||||
energyNeed = 1;
|
||||
}
|
||||
|
||||
if (energyNeed > highestEnergyNeed)
|
||||
{
|
||||
highestEnergyNeed = energyNeed;
|
||||
selectedStructure = structure;
|
||||
}
|
||||
}
|
||||
|
||||
if (selectedStructure)
|
||||
target_id = selectedStructure->id();
|
||||
else
|
||||
target_id.clear();
|
||||
}
|
||||
32
douwco_hivemind/src/loop.cpp
Normal file
32
douwco_hivemind/src/loop.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <Screeps/Context.hpp>
|
||||
#include <Screeps/Creep.hpp>
|
||||
#include <Screeps/StructureSpawn.hpp>
|
||||
|
||||
#include <emscripten.h>
|
||||
#include <emscripten/bind.h>
|
||||
#include <emscripten/val.h>
|
||||
|
||||
#include "Engine.hpp"
|
||||
#include "Structures/Spawn.hpp"
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE
|
||||
extern "C" void loop()
|
||||
{
|
||||
Screeps::Context::update();
|
||||
|
||||
JS::console.log(std::string("\n\n\n\n\n\n\n\n\n"));
|
||||
JS::console.log(std::string("Processing tick:\t") + std::to_string(Screeps::Game.time()));
|
||||
|
||||
{
|
||||
DouwcoHivemind::Engine engine;
|
||||
engine.loop();
|
||||
}
|
||||
|
||||
JS::console.log("Used CPU:\t" + std::to_string(Screeps::Game.cpuGetUsed()));
|
||||
JS::console.log("Bucket:\t" + std::to_string(static_cast<int>(Screeps::Game.cpu()["bucket"])));
|
||||
}
|
||||
|
||||
EMSCRIPTEN_BINDINGS(loop)
|
||||
{
|
||||
emscripten::function("loop", &loop);
|
||||
}
|
||||
28
douwco_hivemind/src/spawn.cpp
Normal file
28
douwco_hivemind/src/spawn.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <Screeps/JS.hpp>
|
||||
#include <Screeps/Game.hpp>
|
||||
#include <Screeps/Room.hpp>
|
||||
#include <Screeps/StructureSpawn.hpp>
|
||||
#include <emscripten.h>
|
||||
|
||||
#include "Creeps/Creep.hpp"
|
||||
#include "Structures/Spawn.hpp"
|
||||
|
||||
void DouwcoHivemind::Spawn::loop()
|
||||
{
|
||||
int creepcount = spawn.room().find(Screeps::FIND_MY_CREEPS).size();
|
||||
if (creepcount > 10)
|
||||
{
|
||||
EM_ASM({ console.log('To much creeps in this room'); });
|
||||
return;
|
||||
}
|
||||
EM_ASM({ console.log('Creating a harvester'); });
|
||||
|
||||
JSON opts;
|
||||
opts["memory"]["role"] = CreepRole::HARVESTER;
|
||||
|
||||
int resp = spawn.spawnCreep(
|
||||
{"work", "carry", "move"},
|
||||
"harvester" + std::to_string(Screeps::Game.time()),
|
||||
opts);
|
||||
}
|
||||
Reference in New Issue
Block a user