Added custom move behaviour to creeps

This commit is contained in:
douwe
2025-08-23 18:15:27 +02:00
parent 6ee67eac47
commit 98b123ee0d
21 changed files with 240 additions and 108 deletions

2
.gitmodules vendored
View File

@@ -3,4 +3,4 @@
url = git@github.com:emscripten-core/emsdk.git
[submodule "screepsxx"]
path = screepsxx
url = git@github.com:UltraCoderRU/screepsxx.git
url = git@github.com:DouweRavers/screepsxx.git

View File

@@ -108,6 +108,9 @@
"coroutine": "cpp",
"source_location": "cpp",
"stdfloat": "cpp",
"typeindex": "cpp"
"typeindex": "cpp",
"version": "cpp",
"__functional_base_03": "cpp",
"__memory": "cpp"
}
}

View File

@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16)
project(example CXX)
project(douwco_hivemind CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
@@ -16,9 +16,9 @@ add_subdirectory(screepsxx)
# If you change TARGET_NAME, please, make corresponding changes in main.js.
set(TARGET_NAME douwco_hivemind)
include_directories(${CMAKE_SOURCE_DIR}/include)
include_directories(${CMAKE_SOURCE_DIR}/douwco_hivemind/include)
file(GLOB SRC_FILES ${CMAKE_SOURCE_DIR}/src/*.cpp)
file(GLOB SRC_FILES ${CMAKE_SOURCE_DIR}/douwco_hivemind/src/*.cpp)
add_executable(${TARGET_NAME} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} screepsxx)
target_link_options(${TARGET_NAME} PUBLIC -sMODULARIZE=1 --no-entry --bind -sEXPORT_ES6=0)

View File

@@ -1,5 +1,34 @@
# screeps
My source code for the MMO-programming game: Screeps.
# Douwco Hivemind
This is the sourcecode of my hivemind implementation for the MMO-programming game Screeps. This is mainly designed to learn programming with C++. This is possible throught the work done in the screepxx project, which I will and try to expand as well.
# Status
As a way to learn c++ programming I used the screepsxx repo for controlling the screeps with c++.
# Running the project
All commands are run starting from the root directory.
## Emsdk setup
Screeps only supports ES5 js syntax which can only be guaranteed by using emsdk 2.0.0.
```
cd emsdk && \
./emsdk install 2.0.0 && \
./emsdk activate 2.0.0 && \
source ./emsdk_env.sh && \
cd ..
```
## Cmake setup
Create the makefiles using cmake. For more info look at the readme in screepsxx.
```
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake ..
```
# Build
To build the project run the following. In the dist folder the "screeps ready" project is copied.
```
cd build
cmake --build .
```

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@@ -0,0 +1,52 @@
#ifndef DOUWCO_HIVEMIND_CREEP_HPP
#define DOUWCO_HIVEMIND_CREEP_HPP
#include <Screeps/Creep.hpp>
#include <Screeps/ReturnTypes.hpp>
#include "Tools/JsonTool.hpp"
#include "Tools/PathTool.hpp"
namespace DouwcoHivemind
{
enum CreepRole
{
UNEMPLOYED,
HARVESTER
};
class Creep
{
public:
CreepRole role;
std::string target_id;
std::vector<Screeps::PathStep> path;
protected:
Screeps::Creep creep;
JSON memory;
public:
Creep(Screeps::Creep crp) : creep(crp),
memory(crp.memory())
{
role = memory.contains("role") ? static_cast<CreepRole>(memory["role"]) : CreepRole::UNEMPLOYED;
target_id = memory.contains("target_id") ? static_cast<std::string>(memory["target_id"]) : std::string();
path = memory.contains("path") ? unflattenPathSteps(jsonToVector<int>(memory["path"])) : std::vector<Screeps::PathStep>();
}
virtual ~Creep()
{
memory["target_id"] = target_id;
memory["path"] = vectorToJson(flattenPathSteps(path));
creep.setMemory(memory);
}
virtual void loop() {}
bool isNearTo(const Screeps::RoomPosition &pos, int dist);
protected:
void moveToTarget(int dist = 1);
std::unique_ptr<Screeps::RoomObject> getRoomObjectTarget();
bool isNearTo(const Screeps::RoomPosition &pos1, const Screeps::RoomPosition &pos2, int dist);
};
}
#endif // DOUWCO_HIVEMIND_CREEP_HPP

View File

@@ -11,21 +11,18 @@ namespace DouwcoHivemind
{
private:
bool harvesting;
std::string target_id;
int taskCounter;
public:
HarvesterRole(Screeps::Creep crp) : Creep(crp)
{
harvesting = memory.contains("harvesting") ? static_cast<bool>(memory["harvesting"]) : false;
target_id = memory.contains("target_id") ? static_cast<std::string>(memory["target_id"]) : std::string();
taskCounter = memory.contains("taskCounter") ? static_cast<int>(memory["taskCounter"]) : 0;
}
~HarvesterRole() override
{
memory["harvesting"] = harvesting;
memory["target_id"] = target_id;
memory["taskCounter"] = taskCounter;
}
@@ -39,8 +36,6 @@ namespace DouwcoHivemind
void depositEnergy();
std::unique_ptr<Screeps::Structure> getDepositTarget();
void searchDeposit();
std::unique_ptr<Screeps::RoomObject> getRoomObjectTarget();
};
}

View File

@@ -6,7 +6,6 @@
#include <Screeps/Game.hpp>
#include <Screeps/Creep.hpp>
#include "Constants.hpp"
#include "Creeps/Creep.hpp"
#include "Creeps/Harvester.hpp"
#include "Structures/Structure.hpp"

View File

@@ -0,0 +1,33 @@
#ifndef DOUWCO_HIVEMIND_JSON_TOOLS_HPP
#define DOUWCO_HIVEMIND_JSON_TOOLS_HPP
#include <nlohmann/json.hpp>
#include <vector>
namespace DouwcoHivemind
{
template <typename T>
std::vector<T> jsonToVector(const nlohmann::json &json)
{
std::vector<T> vector;
for (const auto &item : json)
{
vector.emplace_back(item.get<T>());
}
return vector;
};
template <typename T>
nlohmann::json vectorToJson(const std::vector<T> &vector)
{
nlohmann::json json;
for (const auto &item : vector)
{
json.emplace_back(item);
}
return json;
};
}
#endif // DOUWCO_HIVEMIND_JSON_TOOLS_HPP

View File

@@ -0,0 +1,35 @@
#ifndef DOUWCO_HIVEMIND_PATH_TOOL_HPP
#define DOUWCO_HIVEMIND_PATH_TOOL_HPP
#include <vector>
#include <Screeps/ReturnTypes.hpp>
namespace DouwcoHivemind
{
static std::vector<int> flattenPathSteps(const std::vector<Screeps::PathStep> &pathSteps)
{
std::vector<int> flattened;
for (const auto &step : pathSteps)
{
flattened.push_back(step.x);
flattened.push_back(step.y);
flattened.push_back(step.dx);
flattened.push_back(step.dy);
flattened.push_back(step.direction);
}
return flattened;
}
static std::vector<Screeps::PathStep> unflattenPathSteps(const std::vector<int> &flattened)
{
std::vector<Screeps::PathStep> pathSteps;
for (size_t i = 0; i < flattened.size(); i += 5)
{
pathSteps.emplace_back(Screeps::PathStep(flattened[i], flattened[i + 1], flattened[i + 2], flattened[i + 3], flattened[i + 4]));
}
return pathSteps;
}
} // namespace Screeps
#endif // DOUWCO_HIVEMIND_PATH_TOOL_HPP

View 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();
}

View File

@@ -18,8 +18,6 @@
#include "Creeps/Harvester.hpp"
bool isNearTo(const Screeps::RoomPosition &pos1, const Screeps::RoomPosition &pos2, int dist);
void DouwcoHivemind::HarvesterRole::loop()
{
if (harvesting)
@@ -48,12 +46,14 @@ void DouwcoHivemind::HarvesterRole::harvestSource()
if (!source)
return;
if (isNearTo(creep.pos(), source->pos(), 1))
if (isNearTo(source->pos(), 1))
{
int resp = creep.harvest(*source);
}
else
creep.moveTo(*source);
{
moveToTarget();
}
}
std::unique_ptr<Screeps::Source> DouwcoHivemind::HarvesterRole::getSourceTarget()
@@ -124,7 +124,7 @@ void DouwcoHivemind::HarvesterRole::depositEnergy()
if (structure->structureType() == Screeps::STRUCTURE_CONTROLLER)
{
if (isNearTo(creep.pos(), structure->pos(), 3))
if (isNearTo(structure->pos(), 3))
{
auto controller = dynamic_cast<Screeps::StructureController *>(structure.get());
if (!controller)
@@ -132,16 +132,20 @@ void DouwcoHivemind::HarvesterRole::depositEnergy()
int resp = creep.upgradeController(*controller);
}
else
creep.moveTo(*structure);
{
moveToTarget();
}
}
else
{
if (isNearTo(creep.pos(), structure->pos(), 1))
if (isNearTo(structure->pos(), 1))
{
int resp = creep.transfer(*structure, Screeps::RESOURCE_ENERGY);
}
else
creep.moveTo(*structure);
{
moveToTarget();
}
}
}
@@ -230,26 +234,3 @@ void DouwcoHivemind::HarvesterRole::searchDeposit()
else
target_id.clear();
}
std::unique_ptr<Screeps::RoomObject> DouwcoHivemind::HarvesterRole::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 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();
}

View File

@@ -6,7 +6,6 @@
#include <emscripten/bind.h>
#include <emscripten/val.h>
#include "Constants.hpp"
#include "Engine.hpp"
#include "Structures/Spawn.hpp"

View File

@@ -5,7 +5,7 @@
#include <Screeps/StructureSpawn.hpp>
#include <emscripten.h>
#include "Constants.hpp"
#include "Creeps/Creep.hpp"
#include "Structures/Spawn.hpp"
void DouwcoHivemind::Spawn::loop()

View File

@@ -1,10 +0,0 @@
#ifndef DOUWCO_HIVEMIND_CONSTANTS_HPP
#define DOUWCO_HIVEMIND_CONSTANTS_HPP
namespace DouwcoHivemind{
enum CreepRole{ UNEMPLOYED, HARVESTER };
}
#endif // DOUWCO_HIVEMIND_CONSTANTS_HPP

View File

@@ -1,33 +0,0 @@
#ifndef DOUWCO_HIVEMIND_CREEP_HPP
#define DOUWCO_HIVEMIND_CREEP_HPP
#include <Screeps/Creep.hpp>
#include "Constants.hpp"
namespace DouwcoHivemind
{
class Creep
{
public:
CreepRole role;
protected:
Screeps::Creep creep;
JSON memory;
public:
Creep(Screeps::Creep crp) : creep(crp),
memory(crp.memory())
{
role = memory.contains("role") ? static_cast<CreepRole>(memory["role"]) : CreepRole::UNEMPLOYED;
}
virtual ~Creep() {
creep.setMemory(memory);
}
virtual void loop() {}
};
}
#endif // DOUWCO_HIVEMIND_CREEP_HPP

View File

@@ -1,15 +0,0 @@
# Emsdk
Use emsdk 2.0.0 to only use ES5 syntax for js.
cd emsdk && \
./emsdk install 2.0.0 && \
./emsdk activate 2.0.0 && \
source ./emsdk_env.sh && \
cd ..
# Cmake
In screeps project run dit om cmake setup in te stellen:
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake ..
Run dit om de code te compilen:
cmake --build .