Compare commits
7 Commits
ee44b60087
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 078c824c47 | |||
| 69816925ab | |||
| fe24faafe6 | |||
| aacf582e10 | |||
| 1e9b14f21c | |||
| ceed83358b | |||
| 6fcf742cfa |
@@ -1 +1 @@
|
|||||||
/home/douwe/Projects/screeps/build/compile_commands.json
|
/mnt/douwe/hdd/Projects/Screeps/build/compile_commands.json
|
||||||
9
douwco_hivemind/include/Constants.hpp
Normal file
9
douwco_hivemind/include/Constants.hpp
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#ifndef DOUWCO_HIVEMIND_CONSTANTS_HPP
|
||||||
|
#define DOUWCO_HIVEMIND_CONSTANTS_HPP
|
||||||
|
|
||||||
|
namespace DouwcoHivemind
|
||||||
|
{
|
||||||
|
enum CreepRole { UNEMPLOYED, SUPPLIER, UPGRADER, BUILDER, MAINTAINER, MINER };
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // DOUWCO_HIVEMIND_CONSTANTS_HPP
|
||||||
@@ -2,26 +2,17 @@
|
|||||||
#define DOUWCO_HIVEMIND_CREEPBASE_HPP
|
#define DOUWCO_HIVEMIND_CREEPBASE_HPP
|
||||||
|
|
||||||
#include <Screeps/Creep.hpp>
|
#include <Screeps/Creep.hpp>
|
||||||
|
#include "Constants.hpp"
|
||||||
|
|
||||||
namespace Screeps {
|
namespace Screeps {
|
||||||
class RoomPosition;
|
class RoomPosition;
|
||||||
class RoomObject;
|
class RoomObject;
|
||||||
class PathStep;
|
class PathStep;
|
||||||
}
|
} // namespace Screeps
|
||||||
|
|
||||||
namespace DouwcoHivemind
|
namespace DouwcoHivemind {
|
||||||
{
|
|
||||||
enum CreepRole
|
|
||||||
{
|
|
||||||
UNEMPLOYED,
|
|
||||||
SUPPLIER,
|
|
||||||
UPGRADER,
|
|
||||||
BUILDER,
|
|
||||||
MAINTAINER
|
|
||||||
};
|
|
||||||
|
|
||||||
class CreepBase
|
class CreepBase {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
CreepRole role;
|
CreepRole role;
|
||||||
std::string target_id;
|
std::string target_id;
|
||||||
@@ -35,15 +26,12 @@ namespace DouwcoHivemind
|
|||||||
virtual ~CreepBase();
|
virtual ~CreepBase();
|
||||||
virtual void loop() {}
|
virtual void loop() {}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool isNearTo(const Screeps::RoomPosition &pos, int dist);
|
bool isNearTo(const Screeps::RoomPosition &pos, int dist);
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void moveToTarget(int dist = 1);
|
void moveToTarget(int dist = 1);
|
||||||
std::unique_ptr<Screeps::RoomObject> getRoomObjectTarget();
|
std::unique_ptr<Screeps::RoomObject> getRoomObjectTarget();
|
||||||
};
|
};
|
||||||
}
|
} // namespace DouwcoHivemind
|
||||||
|
|
||||||
#endif // DOUWCO_HIVEMIND_CREEPBASE_HPP
|
#endif // DOUWCO_HIVEMIND_CREEPBASE_HPP
|
||||||
21
douwco_hivemind/include/Creeps/Miner.hpp
Normal file
21
douwco_hivemind/include/Creeps/Miner.hpp
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#ifndef DOUWCO_HIVEMIND_MINER_HPP
|
||||||
|
#define DOUWCO_HIVEMIND_MINER_HPP
|
||||||
|
|
||||||
|
#include "Creeps/CreepBase.hpp"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace DouwcoHivemind
|
||||||
|
{
|
||||||
|
class Miner : public CreepBase
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
bool arrivedAtContainer;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Miner(Screeps::Creep crp);
|
||||||
|
~Miner() override;
|
||||||
|
void loop() override;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // DOUWCO_HIVEMIND_MINER_HPP
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
#define DOUWCO_HIVEMIND_HARVESTER_HPP
|
#define DOUWCO_HIVEMIND_HARVESTER_HPP
|
||||||
|
|
||||||
#include "Creeps/CreepBase.hpp"
|
#include "Creeps/CreepBase.hpp"
|
||||||
|
#include "Screeps/StructureContainer.hpp"
|
||||||
|
|
||||||
namespace DouwcoHivemind
|
namespace DouwcoHivemind
|
||||||
{
|
{
|
||||||
@@ -9,6 +10,7 @@ namespace DouwcoHivemind
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
bool harvesting;
|
bool harvesting;
|
||||||
|
bool foundContainer;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Worker(Screeps::Creep crp);
|
Worker(Screeps::Creep crp);
|
||||||
@@ -20,9 +22,13 @@ namespace DouwcoHivemind
|
|||||||
virtual void depositEnergy(){}
|
virtual void depositEnergy(){}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void getEnergy();
|
||||||
void harvestSource();
|
void harvestSource();
|
||||||
|
void collectEnergyFromContainers();
|
||||||
std::unique_ptr<Screeps::Source> getSourceTarget();
|
std::unique_ptr<Screeps::Source> getSourceTarget();
|
||||||
|
std::unique_ptr<Screeps::StructureContainer> getContainerTarget();
|
||||||
void searchSource();
|
void searchSource();
|
||||||
|
void searchContainer();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "Creeps/CreepBase.hpp"
|
#include "Creeps/CreepBase.hpp"
|
||||||
#include "Room.hpp"
|
#include "Entity/Room.hpp"
|
||||||
#include "Structures/StructureBase.hpp"
|
#include "Structures/StructureBase.hpp"
|
||||||
|
|
||||||
namespace DouwcoHivemind {
|
namespace DouwcoHivemind {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ public:
|
|||||||
void loop();
|
void loop();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool placeContainers();
|
void placeContainers();
|
||||||
};
|
};
|
||||||
} // namespace DouwcoHivemind
|
} // namespace DouwcoHivemind
|
||||||
|
|
||||||
24
douwco_hivemind/include/Structures/Tower.hpp
Normal file
24
douwco_hivemind/include/Structures/Tower.hpp
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#ifndef DOUWCO_HIVEMIND_TOWER_HPP
|
||||||
|
#define DOUWCO_HIVEMIND_TOWER_HPP
|
||||||
|
|
||||||
|
#include <Screeps/StructureTower.hpp>
|
||||||
|
|
||||||
|
#include "Structures/StructureBase.hpp"
|
||||||
|
|
||||||
|
namespace DouwcoHivemind
|
||||||
|
{
|
||||||
|
class Tower : public StructureBase
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
Screeps::StructureTower tower;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Tower(Screeps::StructureTower twr) : tower(twr),
|
||||||
|
StructureBase() {}
|
||||||
|
~Tower() {}
|
||||||
|
|
||||||
|
void loop() override;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // DOUWCO_HIVEMIND_TOWER_HPP
|
||||||
@@ -4,54 +4,60 @@
|
|||||||
#include <Screeps/Room.hpp>
|
#include <Screeps/Room.hpp>
|
||||||
#include <Screeps/RoomPosition.hpp>
|
#include <Screeps/RoomPosition.hpp>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
#include "Creeps/CreepBase.hpp"
|
#include "Creeps/CreepBase.hpp"
|
||||||
|
|
||||||
#include "Tools/JsonTool.hpp"
|
#include "Tools/JsonTool.hpp"
|
||||||
#include "Tools/PathTool.hpp"
|
|
||||||
#include "Tools/MeasureTool.hpp"
|
#include "Tools/MeasureTool.hpp"
|
||||||
|
#include "Tools/PathTool.hpp"
|
||||||
|
|
||||||
DouwcoHivemind::CreepBase::CreepBase(Screeps::Creep crp) : creep(crp),
|
DouwcoHivemind::CreepBase::CreepBase(Screeps::Creep crp)
|
||||||
memory(crp.memory())
|
: creep(crp), memory(crp.memory()) {
|
||||||
{
|
role = memory.contains("role") ? static_cast<CreepRole>(memory["role"])
|
||||||
role = memory.contains("role") ? static_cast<CreepRole>(memory["role"]) : CreepRole::UNEMPLOYED;
|
: CreepRole::UNEMPLOYED;
|
||||||
target_id = memory.contains("target_id") ? static_cast<std::string>(memory["target_id"]) : std::string();
|
target_id = memory.contains("target_id")
|
||||||
|
? static_cast<std::string>(memory["target_id"])
|
||||||
|
: std::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
DouwcoHivemind::CreepBase::~CreepBase()
|
DouwcoHivemind::CreepBase::~CreepBase() {
|
||||||
{
|
|
||||||
memory["role"] = role;
|
memory["role"] = role;
|
||||||
memory["target_id"] = target_id;
|
memory["target_id"] = target_id;
|
||||||
creep.setMemory(memory);
|
creep.setMemory(memory);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DouwcoHivemind::CreepBase::moveToTarget(int dist)
|
void DouwcoHivemind::CreepBase::moveToTarget(int dist) {
|
||||||
{
|
|
||||||
// Is move required?
|
// Is move required?
|
||||||
auto target = getRoomObjectTarget();
|
auto target = getRoomObjectTarget();
|
||||||
if (isNearTo(target->pos(), dist))
|
if (isNearTo(target->pos(), dist)) {
|
||||||
{
|
|
||||||
memory.erase("path");
|
memory.erase("path");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Is wating?
|
// Is wating?
|
||||||
if (memory.contains("wait") && memory["wait"] > 0)
|
if (memory.contains("wait") && memory["wait"] > 0) {
|
||||||
{
|
|
||||||
memory["wait"] = memory["wait"].get<int>() - 1;
|
memory["wait"] = memory["wait"].get<int>() - 1;
|
||||||
creep.say("Waiting..");
|
creep.say("Waiting..");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Is there a path to walk?
|
// Is there a path to walk?
|
||||||
if (!memory.contains("path") || memory["path"].empty())
|
if (!memory.contains("path") || memory["path"].empty()) {
|
||||||
{
|
|
||||||
creep.say("Searching route!");
|
creep.say("Searching route!");
|
||||||
auto target_pos = target->pos();
|
auto target_pos = target->pos();
|
||||||
auto path = creep.room().findPath(creep.pos(), target_pos);
|
auto path = creep.room().findPath(creep.pos(), target_pos);
|
||||||
auto last = path.back();
|
auto last = path.back();
|
||||||
if (last.x - target_pos.x() > dist || last.y - target_pos.y() > dist)
|
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);
|
||||||
|
}
|
||||||
|
if (abs(last.x - target_pos.x()) > dist ||
|
||||||
|
abs(last.y - target_pos.y()) > dist) {
|
||||||
creep.say("No possible path");
|
creep.say("No possible path");
|
||||||
memory["wait"] = rand() % 20;
|
memory["wait"] = rand() % 20;
|
||||||
return;
|
return;
|
||||||
@@ -67,15 +73,11 @@ void DouwcoHivemind::CreepBase::moveToTarget(int dist)
|
|||||||
|
|
||||||
// Get step from memory
|
// Get step from memory
|
||||||
int pathStepData[5] = {0};
|
int pathStepData[5] = {0};
|
||||||
if (memory["path"].size() > 5)
|
if (memory["path"].size() > 5) {
|
||||||
{
|
for (int i = 0; i < 5; i++) {
|
||||||
for (int i = 0; i < 5; i++)
|
|
||||||
{
|
|
||||||
pathStepData[i] = memory["path"][i];
|
pathStepData[i] = memory["path"][i];
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
memory.erase("path");
|
memory.erase("path");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -91,13 +93,11 @@ void DouwcoHivemind::CreepBase::moveToTarget(int dist)
|
|||||||
step.dy = pathStepData[3];
|
step.dy = pathStepData[3];
|
||||||
step.direction = pathStepData[4];
|
step.direction = pathStepData[4];
|
||||||
|
|
||||||
if (memory.contains("last_pos"))
|
if (memory.contains("last_pos")) {
|
||||||
{
|
|
||||||
int last_x = memory["last_pos"]["x"];
|
int last_x = memory["last_pos"]["x"];
|
||||||
int last_y = memory["last_pos"]["y"];
|
int last_y = memory["last_pos"]["y"];
|
||||||
memory.erase("last_pos");
|
memory.erase("last_pos");
|
||||||
if (x == last_x && y == last_y)
|
if (x == last_x && y == last_y) {
|
||||||
{
|
|
||||||
creep.say("I'm stuck!");
|
creep.say("I'm stuck!");
|
||||||
memory["wait"] = rand() % 5;
|
memory["wait"] = rand() % 5;
|
||||||
return;
|
return;
|
||||||
@@ -105,8 +105,7 @@ void DouwcoHivemind::CreepBase::moveToTarget(int dist)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Is the creep on the place intended by the path?
|
// Is the creep on the place intended by the path?
|
||||||
if (!(x == step.x - step.dx && y == step.y - step.dy))
|
if (!(x == step.x - step.dx && y == step.y - step.dy)) {
|
||||||
{
|
|
||||||
creep.say("I'm lost!");
|
creep.say("I'm lost!");
|
||||||
memory["wait"] = rand() % 5;
|
memory["wait"] = rand() % 5;
|
||||||
memory.erase("path");
|
memory.erase("path");
|
||||||
@@ -115,33 +114,32 @@ void DouwcoHivemind::CreepBase::moveToTarget(int dist)
|
|||||||
|
|
||||||
// Lets move forward
|
// Lets move forward
|
||||||
int resp = creep.move(step.direction);
|
int resp = creep.move(step.direction);
|
||||||
if (resp == Screeps::OK)
|
if (resp == Screeps::OK) {
|
||||||
{
|
|
||||||
memory["last_pos"]["x"] = x;
|
memory["last_pos"]["x"] = x;
|
||||||
memory["last_pos"]["y"] = y;
|
memory["last_pos"]["y"] = y;
|
||||||
|
|
||||||
for (int i = 0; i < 5; i++)
|
for (int i = 0; i < 5; i++) {
|
||||||
{
|
|
||||||
memory["path"].erase(0);
|
memory["path"].erase(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Screeps::RoomObject> DouwcoHivemind::CreepBase::getRoomObjectTarget()
|
std::unique_ptr<Screeps::RoomObject>
|
||||||
{
|
DouwcoHivemind::CreepBase::getRoomObjectTarget() {
|
||||||
// Check if any target is present
|
// Check if any target is present
|
||||||
if(target_id.empty()) return nullptr;
|
if (target_id.empty())
|
||||||
|
return nullptr;
|
||||||
// Check if game can find target
|
// Check if game can find target
|
||||||
auto roomObj = Screeps::Game.getObjectById(target_id);
|
auto roomObj = Screeps::Game.getObjectById(target_id);
|
||||||
if (!roomObj)
|
if (!roomObj) {
|
||||||
{
|
JS::console.log(creep.name() +
|
||||||
JS::console.log(creep.name() + ": Game can\'t find target id: " + target_id);
|
": Game can\'t find target id: " + target_id);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return std::move(roomObj);
|
return std::move(roomObj);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DouwcoHivemind::CreepBase::isNearTo(const Screeps::RoomPosition &pos, int dist)
|
bool DouwcoHivemind::CreepBase::isNearTo(const Screeps::RoomPosition &pos,
|
||||||
{
|
int dist) {
|
||||||
return DouwcoHivemind::isNearTo(creep.pos(), pos, dist);
|
return DouwcoHivemind::isNearTo(creep.pos(), pos, dist);
|
||||||
}
|
}
|
||||||
@@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
#include "Creeps/Maintainer.hpp"
|
#include "Creeps/Maintainer.hpp"
|
||||||
#include "Creeps/CreepBase.hpp"
|
#include "Creeps/CreepBase.hpp"
|
||||||
|
#include "Screeps/Constants.hpp"
|
||||||
|
|
||||||
void DouwcoHivemind::Maintainer::depositEnergy()
|
void DouwcoHivemind::Maintainer::depositEnergy()
|
||||||
{
|
{
|
||||||
@@ -61,22 +62,24 @@ std::unique_ptr<Screeps::Structure> DouwcoHivemind::Maintainer::getDamagedStruct
|
|||||||
|
|
||||||
void DouwcoHivemind::Maintainer::searchDamagedStructure()
|
void DouwcoHivemind::Maintainer::searchDamagedStructure()
|
||||||
{
|
{
|
||||||
int lowestMaintaince = INT16_MAX;
|
int highestDamage = 0;
|
||||||
Screeps::Structure *selectedStructure;
|
Screeps::Structure *selectedStructure;
|
||||||
auto structures = creep.room().find(Screeps::FIND_STRUCTURES);
|
auto structures = creep.room().find(Screeps::FIND_STRUCTURES);
|
||||||
for (auto &structureObject : structures)
|
for (auto &structureObject : structures)
|
||||||
{
|
{
|
||||||
auto structure = dynamic_cast<Screeps::Structure *>(structureObject.get());
|
auto structure = dynamic_cast<Screeps::Structure *>(structureObject.release());
|
||||||
if (!structure)
|
if (!structure)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
int maintance = structure->hits();
|
int damage = structure->hitsMax() - structure->hits();
|
||||||
auto structureType = structure->structureType();
|
auto structureType = structure->structureType();
|
||||||
if (structureType == Screeps::STRUCTURE_CONTROLLER)
|
if (structureType == Screeps::STRUCTURE_CONTROLLER)
|
||||||
continue;
|
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;
|
selectedStructure = structure;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
40
douwco_hivemind/src/Creeps/Miner.cpp
Normal file
40
douwco_hivemind/src/Creeps/Miner.cpp
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#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 <string>
|
||||||
|
|
||||||
|
DouwcoHivemind::Miner::Miner(Screeps::Creep crp) : CreepBase(crp) {
|
||||||
|
arrivedAtContainer = memory.contains("arrivedAtContainer")
|
||||||
|
? static_cast<bool>(memory["arrivedAtContainer"])
|
||||||
|
: false;
|
||||||
|
}
|
||||||
|
|
||||||
|
DouwcoHivemind::Miner::~Miner() {
|
||||||
|
memory["arrivedAtContainer"] = arrivedAtContainer;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DouwcoHivemind::Miner::loop() {
|
||||||
|
if (arrivedAtContainer) {
|
||||||
|
auto source =
|
||||||
|
static_cast<Screeps::Source *>(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.pos().findClosestByRange(Screeps::FIND_SOURCES, 2);
|
||||||
|
auto source = static_cast<Screeps::Source *>(roomObjs.release());
|
||||||
|
target_id = source->id();
|
||||||
|
} else {
|
||||||
|
moveToTarget(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,49 +1,48 @@
|
|||||||
#include <vector>
|
|
||||||
#include <optional>
|
|
||||||
#include <emscripten.h>
|
#include <emscripten.h>
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <Screeps/Game.hpp>
|
|
||||||
#include <Screeps/Creep.hpp>
|
#include <Screeps/Creep.hpp>
|
||||||
#include <Screeps/Source.hpp>
|
#include <Screeps/Game.hpp>
|
||||||
#include <Screeps/Room.hpp>
|
#include <Screeps/Room.hpp>
|
||||||
#include <Screeps/RoomPosition.hpp>
|
|
||||||
#include <Screeps/RoomObject.hpp>
|
#include <Screeps/RoomObject.hpp>
|
||||||
|
#include <Screeps/RoomPosition.hpp>
|
||||||
|
#include <Screeps/Source.hpp>
|
||||||
|
#include <Screeps/Store.hpp>
|
||||||
#include <Screeps/Structure.hpp>
|
#include <Screeps/Structure.hpp>
|
||||||
#include <Screeps/StructureController.hpp>
|
#include <Screeps/StructureController.hpp>
|
||||||
#include <Screeps/StructureSpawn.hpp>
|
|
||||||
#include <Screeps/StructureExtension.hpp>
|
#include <Screeps/StructureExtension.hpp>
|
||||||
|
#include <Screeps/StructureSpawn.hpp>
|
||||||
|
#include <Screeps/StructureStorage.hpp>
|
||||||
#include <Screeps/StructureTower.hpp>
|
#include <Screeps/StructureTower.hpp>
|
||||||
#include <Screeps/Store.hpp>
|
|
||||||
|
|
||||||
#include "Creeps/Supplier.hpp"
|
#include "Creeps/Supplier.hpp"
|
||||||
|
#include "Screeps/Constants.hpp"
|
||||||
|
|
||||||
void DouwcoHivemind::Supplier::depositEnergy()
|
void DouwcoHivemind::Supplier::depositEnergy() {
|
||||||
{
|
|
||||||
auto structure = getEnergyStructureTarget();
|
auto structure = getEnergyStructureTarget();
|
||||||
if (!structure) return;
|
if (!structure)
|
||||||
|
return;
|
||||||
|
|
||||||
if (isNearTo(structure->pos(), 1))
|
if (isNearTo(structure->pos(), 1)) {
|
||||||
{
|
|
||||||
int resp = creep.transfer(*structure, Screeps::RESOURCE_ENERGY);
|
int resp = creep.transfer(*structure, Screeps::RESOURCE_ENERGY);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
moveToTarget();
|
moveToTarget();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Screeps::Structure> DouwcoHivemind::Supplier::getEnergyStructureTarget()
|
std::unique_ptr<Screeps::Structure>
|
||||||
{
|
DouwcoHivemind::Supplier::getEnergyStructureTarget() {
|
||||||
auto roomObj = getRoomObjectTarget();
|
auto roomObj = getRoomObjectTarget();
|
||||||
if (!roomObj)
|
if (!roomObj) {
|
||||||
{
|
|
||||||
searchEnergyStructure();
|
searchEnergyStructure();
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto structure = std::unique_ptr<Screeps::Structure>(dynamic_cast<Screeps::Structure *>(roomObj.release()));
|
auto structure = std::unique_ptr<Screeps::Structure>(
|
||||||
if (!structure)
|
dynamic_cast<Screeps::Structure *>(roomObj.release()));
|
||||||
{
|
if (!structure) {
|
||||||
searchEnergyStructure();
|
searchEnergyStructure();
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@@ -51,24 +50,26 @@ std::unique_ptr<Screeps::Structure> DouwcoHivemind::Supplier::getEnergyStructure
|
|||||||
// Check if the structure can receive energy to harvest
|
// Check if the structure can receive energy to harvest
|
||||||
int energyCapacity;
|
int energyCapacity;
|
||||||
auto structureType = structure->structureType();
|
auto structureType = structure->structureType();
|
||||||
if (structureType == Screeps::STRUCTURE_SPAWN)
|
if (structureType == Screeps::STRUCTURE_TOWER) {
|
||||||
{
|
auto tower = dynamic_cast<Screeps::StructureTower *>(structure.get());
|
||||||
|
energyCapacity =
|
||||||
|
tower->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
|
||||||
|
} else if (structureType == Screeps::STRUCTURE_SPAWN) {
|
||||||
auto spawn = dynamic_cast<Screeps::StructureSpawn *>(structure.get());
|
auto spawn = dynamic_cast<Screeps::StructureSpawn *>(structure.get());
|
||||||
energyCapacity = spawn->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
|
energyCapacity =
|
||||||
}
|
spawn->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
|
||||||
else if (structureType == Screeps::STRUCTURE_EXTENSION)
|
} else if (structureType == Screeps::STRUCTURE_EXTENSION) {
|
||||||
{
|
auto extension =
|
||||||
auto extension = dynamic_cast<Screeps::StructureExtension *>(structure.get());
|
dynamic_cast<Screeps::StructureExtension *>(structure.get());
|
||||||
energyCapacity = extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
|
energyCapacity =
|
||||||
}
|
extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
|
||||||
else if (structureType == Screeps::STRUCTURE_TOWER)
|
} else if (structureType == Screeps::STRUCTURE_STORAGE) {
|
||||||
{
|
auto storage = dynamic_cast<Screeps::StructureStorage *>(structure.get());
|
||||||
auto extension = dynamic_cast<Screeps::StructureTower *>(structure.get());
|
energyCapacity =
|
||||||
energyCapacity = extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
|
storage->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (energyCapacity == 0)
|
if (energyCapacity == 0) {
|
||||||
{
|
|
||||||
searchEnergyStructure();
|
searchEnergyStructure();
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@@ -76,45 +77,46 @@ std::unique_ptr<Screeps::Structure> DouwcoHivemind::Supplier::getEnergyStructure
|
|||||||
return std::move(structure);
|
return std::move(structure);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DouwcoHivemind::Supplier::searchEnergyStructure()
|
void DouwcoHivemind::Supplier::searchEnergyStructure() {
|
||||||
{
|
|
||||||
int mostEnergyNeeded = 0;
|
int mostEnergyNeeded = 0;
|
||||||
Screeps::Structure *selectedStructure;
|
std::string selectedStructureId;
|
||||||
|
std::string storageStructureId;
|
||||||
auto structures = creep.room().find(Screeps::FIND_MY_STRUCTURES);
|
auto structures = creep.room().find(Screeps::FIND_MY_STRUCTURES);
|
||||||
for (auto &structureObject : structures)
|
for (auto &structureObject : structures) {
|
||||||
{
|
|
||||||
auto structure = dynamic_cast<Screeps::Structure *>(structureObject.get());
|
auto structure = dynamic_cast<Screeps::Structure *>(structureObject.get());
|
||||||
if (!structure)
|
if (!structure)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
int energyRequired;
|
int energyRequired = 0;
|
||||||
|
|
||||||
auto structureType = structure->structureType();
|
auto structureType = structure->structureType();
|
||||||
if (structureType == Screeps::STRUCTURE_SPAWN)
|
if (structureType == Screeps::STRUCTURE_TOWER) {
|
||||||
{
|
auto tower = dynamic_cast<Screeps::StructureTower *>(structure);
|
||||||
|
energyRequired =
|
||||||
|
tower->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
|
||||||
|
} else if (structureType == Screeps::STRUCTURE_SPAWN) {
|
||||||
auto spawn = dynamic_cast<Screeps::StructureSpawn *>(structure);
|
auto spawn = dynamic_cast<Screeps::StructureSpawn *>(structure);
|
||||||
energyRequired = spawn->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
|
energyRequired =
|
||||||
}
|
spawn->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
|
||||||
else if (structureType == Screeps::STRUCTURE_EXTENSION)
|
} else if (structureType == Screeps::STRUCTURE_EXTENSION) {
|
||||||
{
|
|
||||||
auto extension = dynamic_cast<Screeps::StructureExtension *>(structure);
|
auto extension = dynamic_cast<Screeps::StructureExtension *>(structure);
|
||||||
energyRequired = extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
|
energyRequired =
|
||||||
}
|
extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
|
||||||
else if (structureType == Screeps::STRUCTURE_TOWER)
|
} else if (structureType == Screeps::STRUCTURE_STORAGE) {
|
||||||
{
|
storageStructureId = structure->id();
|
||||||
auto extension = dynamic_cast<Screeps::StructureTower *>(structure);
|
|
||||||
energyRequired = extension->store().getFreeCapacity(Screeps::RESOURCE_ENERGY).value();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (energyRequired > mostEnergyNeeded)
|
if (energyRequired > mostEnergyNeeded) {
|
||||||
{
|
|
||||||
mostEnergyNeeded = energyRequired;
|
mostEnergyNeeded = energyRequired;
|
||||||
selectedStructure = structure;
|
selectedStructureId = structure->id();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (selectedStructure)
|
if (mostEnergyNeeded == 0)
|
||||||
target_id = selectedStructure->id();
|
selectedStructureId = storageStructureId;
|
||||||
else
|
|
||||||
|
if (selectedStructureId.empty())
|
||||||
target_id.clear();
|
target_id.clear();
|
||||||
|
else
|
||||||
|
target_id = selectedStructureId;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,48 +1,48 @@
|
|||||||
#include <vector>
|
|
||||||
#include <optional>
|
|
||||||
#include <emscripten.h>
|
#include <emscripten.h>
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <Screeps/Game.hpp>
|
#include <Screeps/Constants.hpp>
|
||||||
#include <Screeps/Creep.hpp>
|
#include <Screeps/Creep.hpp>
|
||||||
#include <Screeps/Source.hpp>
|
#include <Screeps/Game.hpp>
|
||||||
#include <Screeps/Room.hpp>
|
#include <Screeps/Room.hpp>
|
||||||
#include <Screeps/RoomPosition.hpp>
|
|
||||||
#include <Screeps/RoomObject.hpp>
|
#include <Screeps/RoomObject.hpp>
|
||||||
|
#include <Screeps/RoomPosition.hpp>
|
||||||
|
#include <Screeps/Source.hpp>
|
||||||
|
#include <Screeps/Store.hpp>
|
||||||
#include <Screeps/Structure.hpp>
|
#include <Screeps/Structure.hpp>
|
||||||
#include <Screeps/StructureController.hpp>
|
#include <Screeps/StructureController.hpp>
|
||||||
#include <Screeps/StructureSpawn.hpp>
|
|
||||||
#include <Screeps/StructureExtension.hpp>
|
#include <Screeps/StructureExtension.hpp>
|
||||||
|
#include <Screeps/StructureSpawn.hpp>
|
||||||
#include <Screeps/StructureTower.hpp>
|
#include <Screeps/StructureTower.hpp>
|
||||||
#include <Screeps/Constants.hpp>
|
|
||||||
#include <Screeps/Store.hpp>
|
|
||||||
|
|
||||||
#include "Creeps/Worker.hpp"
|
#include "Creeps/Worker.hpp"
|
||||||
|
#include "Screeps/StructureContainer.hpp"
|
||||||
|
|
||||||
DouwcoHivemind::Worker::Worker(Screeps::Creep crp) : CreepBase(crp)
|
DouwcoHivemind::Worker::Worker(Screeps::Creep crp) : CreepBase(crp) {
|
||||||
{
|
harvesting = memory.contains("harvesting")
|
||||||
harvesting = memory.contains("harvesting") ? static_cast<bool>(memory["harvesting"]) : false;
|
? static_cast<bool>(memory["harvesting"])
|
||||||
|
: false;
|
||||||
|
foundContainer = memory.contains("foundContainer")
|
||||||
|
? static_cast<bool>(memory["foundContainer"])
|
||||||
|
: false;
|
||||||
}
|
}
|
||||||
|
|
||||||
DouwcoHivemind::Worker::~Worker()
|
DouwcoHivemind::Worker::~Worker() {
|
||||||
{
|
|
||||||
memory["harvesting"] = harvesting;
|
memory["harvesting"] = harvesting;
|
||||||
|
memory["foundContainer"] = foundContainer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DouwcoHivemind::Worker::loop()
|
void DouwcoHivemind::Worker::loop() {
|
||||||
{
|
if (harvesting) {
|
||||||
if (harvesting)
|
if (creep.store().getFreeCapacity(Screeps::RESOURCE_ENERGY) == 0) {
|
||||||
{
|
|
||||||
if (creep.store().getFreeCapacity(Screeps::RESOURCE_ENERGY) == 0)
|
|
||||||
{
|
|
||||||
harvesting = false;
|
harvesting = false;
|
||||||
target_id.clear();
|
target_id.clear();
|
||||||
}
|
}
|
||||||
harvestSource();
|
getEnergy();
|
||||||
}
|
} else {
|
||||||
else
|
if (creep.store().getUsedCapacity(Screeps::RESOURCE_ENERGY) == 0) {
|
||||||
{
|
|
||||||
if (creep.store().getUsedCapacity(Screeps::RESOURCE_ENERGY) == 0)
|
|
||||||
{
|
|
||||||
harvesting = true;
|
harvesting = true;
|
||||||
target_id.clear();
|
target_id.clear();
|
||||||
}
|
}
|
||||||
@@ -50,43 +50,81 @@ void DouwcoHivemind::Worker::loop()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DouwcoHivemind::Worker::harvestSource()
|
void DouwcoHivemind::Worker::getEnergy() {
|
||||||
{
|
if (foundContainer || Screeps::Game.time()%100==0) collectEnergyFromContainers();
|
||||||
auto source = getSourceTarget();
|
else harvestSource();
|
||||||
if (!source)
|
}
|
||||||
|
|
||||||
|
void DouwcoHivemind::Worker::collectEnergyFromContainers() {
|
||||||
|
auto container = getContainerTarget();
|
||||||
|
if (!container)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (isNearTo(source->pos(), 1))
|
if (isNearTo(container->pos(), 1)) {
|
||||||
{
|
int resp = creep.withdraw(*container, Screeps::RESOURCE_ENERGY);
|
||||||
int resp = creep.harvest(*source);
|
} else {
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
moveToTarget();
|
moveToTarget();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Screeps::Source> DouwcoHivemind::Worker::getSourceTarget()
|
void DouwcoHivemind::Worker::harvestSource() {
|
||||||
{
|
auto source = getSourceTarget();
|
||||||
|
if (!source)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (isNearTo(source->pos(), 1)) {
|
||||||
|
int resp = creep.harvest(*source);
|
||||||
|
} else {
|
||||||
|
moveToTarget();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<Screeps::StructureContainer>
|
||||||
|
DouwcoHivemind::Worker::getContainerTarget() {
|
||||||
auto roomObj = getRoomObjectTarget();
|
auto roomObj = getRoomObjectTarget();
|
||||||
if (!roomObj)
|
if (!roomObj) {
|
||||||
{
|
searchContainer();
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if found roomobject is an actual source
|
||||||
|
auto container = std::unique_ptr<Screeps::StructureContainer>(
|
||||||
|
dynamic_cast<Screeps::StructureContainer *>(roomObj.release()));
|
||||||
|
if (!container) {
|
||||||
|
// EM_ASM({console.log($0 + ': Can\'t cast target to Source')},
|
||||||
|
// creep.name().c_str());
|
||||||
|
searchContainer();
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the source still has energy to harvest
|
||||||
|
if (container->store().getUsedCapacity(Screeps::RESOURCE_ENERGY) == 0) {
|
||||||
|
searchContainer();
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::move(container);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<Screeps::Source> DouwcoHivemind::Worker::getSourceTarget() {
|
||||||
|
auto roomObj = getRoomObjectTarget();
|
||||||
|
if (!roomObj) {
|
||||||
searchSource();
|
searchSource();
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if found roomobject is an actual source
|
// Check if found roomobject is an actual source
|
||||||
auto source = std::unique_ptr<Screeps::Source>(dynamic_cast<Screeps::Source *>(roomObj.release()));
|
auto source = std::unique_ptr<Screeps::Source>(
|
||||||
if (!source)
|
dynamic_cast<Screeps::Source *>(roomObj.release()));
|
||||||
{
|
if (!source) {
|
||||||
// EM_ASM({console.log($0 + ': Can\'t cast target to Source')}, creep.name().c_str());
|
// EM_ASM({console.log($0 + ': Can\'t cast target to Source')},
|
||||||
|
// creep.name().c_str());
|
||||||
searchSource();
|
searchSource();
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the source still has energy to harvest
|
// Check if the source still has energy to harvest
|
||||||
if (source->energy() == 0)
|
if (source->energy() == 0) {
|
||||||
{
|
|
||||||
searchSource();
|
searchSource();
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@@ -94,30 +132,44 @@ std::unique_ptr<Screeps::Source> DouwcoHivemind::Worker::getSourceTarget()
|
|||||||
return std::move(source);
|
return std::move(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DouwcoHivemind::Worker::searchSource()
|
void DouwcoHivemind::Worker::searchSource() {
|
||||||
{
|
target_id.clear();
|
||||||
|
auto sourceObject = creep.pos().findClosestByRange(Screeps::FIND_SOURCES);
|
||||||
|
if (!sourceObject)
|
||||||
|
return;
|
||||||
|
auto source = dynamic_cast<Screeps::Source *>(sourceObject.release());
|
||||||
|
target_id = source->id();
|
||||||
|
}
|
||||||
|
|
||||||
|
void DouwcoHivemind::Worker::searchContainer() {
|
||||||
target_id.clear();
|
target_id.clear();
|
||||||
|
|
||||||
auto sources = creep.room().find(Screeps::FIND_SOURCES_ACTIVE);
|
auto structureObjects = creep.room().find(Screeps::FIND_STRUCTURES);
|
||||||
if (sources.size() == 0)
|
if (structureObjects.size() == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
int x = creep.pos().x();
|
int fullestContents = 0;
|
||||||
int y = creep.pos().y();
|
Screeps::StructureContainer *selectedContainer;
|
||||||
|
for (auto &structureObject : structureObjects) {
|
||||||
|
auto structure =
|
||||||
|
dynamic_cast<Screeps::Structure *>(structureObject.release());
|
||||||
|
if (structure->structureType() != Screeps::STRUCTURE_CONTAINER)
|
||||||
|
continue;
|
||||||
|
auto container = dynamic_cast<Screeps::StructureContainer *>(structure);
|
||||||
|
int availableEnergy =
|
||||||
|
container->store().getUsedCapacity(Screeps::RESOURCE_ENERGY).value();
|
||||||
|
if (availableEnergy == 0)
|
||||||
|
continue;
|
||||||
|
if (fullestContents < availableEnergy) {
|
||||||
|
fullestContents = availableEnergy;
|
||||||
|
selectedContainer = container;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int closestDistance = INT16_MAX;
|
if (!selectedContainer) {
|
||||||
Screeps::Source *selectedSource;
|
foundContainer = false;
|
||||||
for(auto &sourceObject : sources){
|
} else {
|
||||||
auto source =dynamic_cast<Screeps::Source *>(sourceObject.get());
|
foundContainer = true;
|
||||||
if(source->energy()==0) continue;
|
target_id = selectedContainer->id();
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,15 +1,21 @@
|
|||||||
#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 <memory>
|
||||||
|
|
||||||
#include "Engine.hpp"
|
#include "Engine.hpp"
|
||||||
|
|
||||||
#include "Creeps/Builder.hpp"
|
#include "Creeps/Builder.hpp"
|
||||||
#include "Creeps/Maintainer.hpp"
|
#include "Creeps/Maintainer.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/StructureSpawn.hpp"
|
||||||
#include "Structures/Spawn.hpp"
|
#include "Structures/Spawn.hpp"
|
||||||
|
#include "Structures/Tower.hpp"
|
||||||
|
|
||||||
DouwcoHivemind::Engine::Engine() {
|
DouwcoHivemind::Engine::Engine() {
|
||||||
ReadOutRooms();
|
ReadOutRooms();
|
||||||
@@ -57,14 +63,22 @@ void DouwcoHivemind::Engine::ReadOutCreeps() {
|
|||||||
creeps.push_back(std::make_unique<Builder>(creep.second));
|
creeps.push_back(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.push_back(std::make_unique<Maintainer>(creep.second));
|
||||||
|
else if (role == CreepRole::MINER)
|
||||||
|
creeps.push_back(std::make_unique<Miner>(creep.second));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DouwcoHivemind::Engine::ReadOutStructures() {
|
void DouwcoHivemind::Engine::ReadOutStructures() {
|
||||||
JS::console.log(std::string("Reading out structures"));
|
JS::console.log(std::string("Reading out structures"));
|
||||||
auto spawns = Screeps::Game.spawns();
|
auto structures_src =Screeps::Game.structures();
|
||||||
for (auto &spawn : spawns) {
|
for (auto &structure : structures_src) {
|
||||||
structures.push_back(std::make_unique<Spawn>(spawn.second));
|
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)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "Room.hpp"
|
#include "Entity/Room.hpp"
|
||||||
|
|
||||||
#include "Screeps/Constants.hpp"
|
#include "Screeps/Constants.hpp"
|
||||||
|
#include "Screeps/Game.hpp"
|
||||||
#include "Screeps/Room.hpp"
|
#include "Screeps/Room.hpp"
|
||||||
#include "Screeps/RoomObject.hpp"
|
#include "Screeps/RoomObject.hpp"
|
||||||
#include "Screeps/RoomPosition.hpp"
|
#include "Screeps/RoomPosition.hpp"
|
||||||
@@ -8,13 +9,8 @@
|
|||||||
#include "Screeps/Structure.hpp"
|
#include "Screeps/Structure.hpp"
|
||||||
#include "Screeps/StructureContainer.hpp"
|
#include "Screeps/StructureContainer.hpp"
|
||||||
#include "Screeps/StructureController.hpp"
|
#include "Screeps/StructureController.hpp"
|
||||||
#include <algorithm>
|
|
||||||
#include <cmath>
|
#include "Constants.hpp"
|
||||||
#include <cstdlib>
|
|
||||||
#include <memory>
|
|
||||||
#include <optional>
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
DouwcoHivemind::Room::Room(Screeps::Room rm) : room(rm), memory(rm.memory()) {
|
DouwcoHivemind::Room::Room(Screeps::Room rm) : room(rm), memory(rm.memory()) {
|
||||||
if (memory.contains("sourceContainers"))
|
if (memory.contains("sourceContainers"))
|
||||||
@@ -28,15 +24,16 @@ DouwcoHivemind::Room::~Room() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DouwcoHivemind::Room::loop() {
|
void DouwcoHivemind::Room::loop() {
|
||||||
if (placeContainers())
|
// Check on building setup
|
||||||
return;
|
if(Screeps::Game.time()%1000!=0) return;
|
||||||
|
placeContainers();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DouwcoHivemind::Room::placeContainers() {
|
void DouwcoHivemind::Room::placeContainers() {
|
||||||
auto sources = room.find(Screeps::FIND_SOURCES);
|
auto sources = room.find(Screeps::FIND_SOURCES);
|
||||||
|
|
||||||
// No containers planned or build, planning now
|
// No containers planned or build, planning now
|
||||||
if (sourceContainers.size() == 0) {
|
if (sourceContainers.size() < sources.size()) {
|
||||||
int controller_x = room.controller().value().pos().x();
|
int controller_x = room.controller().value().pos().x();
|
||||||
int controller_y = room.controller().value().pos().y();
|
int controller_y = room.controller().value().pos().y();
|
||||||
|
|
||||||
@@ -67,7 +64,7 @@ bool DouwcoHivemind::Room::placeContainers() {
|
|||||||
room.createConstructionSite(best_x, best_y, Screeps::STRUCTURE_CONTAINER);
|
room.createConstructionSite(best_x, best_y, Screeps::STRUCTURE_CONTAINER);
|
||||||
sourceContainers.push_back("under_construction");
|
sourceContainers.push_back("under_construction");
|
||||||
}
|
}
|
||||||
return true;
|
return;
|
||||||
}
|
}
|
||||||
// Check if construction container is done.
|
// Check if construction container is done.
|
||||||
for (auto entry : sourceContainers) {
|
for (auto entry : sourceContainers) {
|
||||||
@@ -82,7 +79,11 @@ bool DouwcoHivemind::Room::placeContainers() {
|
|||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return;
|
||||||
|
}
|
||||||
|
// Check if containers still exist
|
||||||
|
for(int i = 0; i < sourceContainers.size(); i++){
|
||||||
|
auto roomObj = Screeps::Game.getObjectById(sourceContainers[i]);
|
||||||
|
if(!roomObj) sourceContainers.erase(sourceContainers.begin() + i);
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
@@ -4,70 +4,102 @@
|
|||||||
#include "Creeps/CreepBase.hpp"
|
#include "Creeps/CreepBase.hpp"
|
||||||
#include "Structures/Spawn.hpp"
|
#include "Structures/Spawn.hpp"
|
||||||
|
|
||||||
void DouwcoHivemind::Spawn::loop()
|
void DouwcoHivemind::Spawn::loop() {
|
||||||
{
|
// Only run every 500 ticks
|
||||||
// Only run every 50 ticks
|
if (Screeps::Game.time() % 100 != 0)
|
||||||
if (Screeps::Game.time() % 50 != 0)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
// Get energy data
|
||||||
int energyAvailable = spawn.room().energyAvailable();
|
int energyAvailable = spawn.room().energyAvailable();
|
||||||
int energyCapacityAvailable = spawn.room().energyCapacityAvailable();
|
int energyCapacityAvailable = spawn.room().energyCapacityAvailable();
|
||||||
|
|
||||||
|
// Calculate needed creep roles
|
||||||
int required_upgraders = 1;
|
int required_upgraders = 1;
|
||||||
int required_suppliers = 1;
|
int required_suppliers = 2;
|
||||||
int required_maintainers = spawn.room().find(Screeps::FIND_STRUCTURES).size() <= 2 ? 0 : 1;;
|
int required_maintainers =
|
||||||
int required_builders = spawn.room().find(Screeps::FIND_MY_CONSTRUCTION_SITES).size() == 0 ? 0 : 1;
|
spawn.room().find(Screeps::FIND_STRUCTURES).size() <= 2 ? 0 : 1;
|
||||||
for (auto &creep : Screeps::Game.creeps())
|
;
|
||||||
{
|
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"];
|
CreepRole role = creep.second.memory()["role"];
|
||||||
|
|
||||||
if (role == CreepRole::SUPPLIER)
|
if (role == CreepRole::SUPPLIER)
|
||||||
required_suppliers--;
|
existing_suppliers++;
|
||||||
else if (role == CreepRole::UPGRADER)
|
else if (role == CreepRole::UPGRADER)
|
||||||
required_upgraders--;
|
existing_upgraders++;
|
||||||
else if (role == CreepRole::MAINTAINER)
|
else if (role == CreepRole::MAINTAINER)
|
||||||
required_maintainers--;
|
existing_maintainers++;
|
||||||
else if (role == CreepRole::BUILDER)
|
else if (role == CreepRole::BUILDER)
|
||||||
required_builders--;
|
existing_builders++;
|
||||||
|
else if (role == CreepRole::MINER)
|
||||||
|
existing_miners++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (energyAvailable < energyCapacityAvailable && 3 < Screeps::Game.creeps().size())
|
// Check if max energy used
|
||||||
// return;
|
if (energyAvailable < energyCapacityAvailable && 0 < existing_suppliers)
|
||||||
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;
|
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;
|
std::vector<std::string> body;
|
||||||
for (int i = 0; i < energyAvailable / 200; i++)
|
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("work");
|
||||||
body.push_back("carry");
|
body.push_back("carry");
|
||||||
body.push_back("move");
|
body.push_back("move");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
spawn.spawnCreep(
|
|
||||||
body,
|
// Create creep
|
||||||
name + std::to_string(Screeps::Game.time()),
|
spawn.spawnCreep(body, name + std::to_string(Screeps::Game.time()), opts);
|
||||||
opts);
|
|
||||||
}
|
}
|
||||||
23
douwco_hivemind/src/Structures/Tower.cpp
Normal file
23
douwco_hivemind/src/Structures/Tower.cpp
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#include <Screeps/Game.hpp>
|
||||||
|
#include <Screeps/Room.hpp>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "Creeps/CreepBase.hpp"
|
||||||
|
#include "Screeps/Constants.hpp"
|
||||||
|
#include "Screeps/Creep.hpp"
|
||||||
|
#include "Structures/Tower.hpp"
|
||||||
|
|
||||||
|
void DouwcoHivemind::Tower::loop() {
|
||||||
|
int minHits = INT16_MAX;
|
||||||
|
Screeps::Creep *target;
|
||||||
|
auto hostile_creeps = tower.room().find(Screeps::FIND_HOSTILE_CREEPS);
|
||||||
|
for (auto &creepRoomObj : hostile_creeps) {
|
||||||
|
Screeps::Creep* creep = static_cast<Screeps::Creep*>(creepRoomObj.get());
|
||||||
|
int health = creep->hits();
|
||||||
|
if(health<minHits){
|
||||||
|
minHits=health;
|
||||||
|
target = creep;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tower.attack(*target);
|
||||||
|
}
|
||||||
Submodule screepsxx updated: bf5fd1f69b...0eca2f2867
Reference in New Issue
Block a user