Compare commits
1 Commits
6ee67eac47
...
2022
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
85215ebd97 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +0,0 @@
|
||||
build
|
||||
6
.gitmodules
vendored
6
.gitmodules
vendored
@@ -1,6 +0,0 @@
|
||||
[submodule "emsdk"]
|
||||
path = emsdk
|
||||
url = git@github.com:emscripten-core/emsdk.git
|
||||
[submodule "screepsxx"]
|
||||
path = screepsxx
|
||||
url = git@github.com:UltraCoderRU/screepsxx.git
|
||||
114
.vscode/settings.json
vendored
114
.vscode/settings.json
vendored
@@ -1,113 +1,7 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"__hash_table": "cpp",
|
||||
"__string": "cpp",
|
||||
"algorithm": "cpp",
|
||||
"functional": "cpp",
|
||||
"locale": "cpp",
|
||||
"utility": "cpp",
|
||||
"mutex": "cpp",
|
||||
"shared_mutex": "cpp",
|
||||
"stop_token": "cpp",
|
||||
"cassert": "cpp",
|
||||
"vector": "cpp",
|
||||
"optional": "cpp",
|
||||
"*.inc": "cpp",
|
||||
"thread": "cpp",
|
||||
"atomic": "cpp",
|
||||
"__config": "cpp",
|
||||
"__debug": "cpp",
|
||||
"__errc": "cpp",
|
||||
"__functional_03": "cpp",
|
||||
"__mutex_base": "cpp",
|
||||
"__nullptr": "cpp",
|
||||
"__split_buffer": "cpp",
|
||||
"__tree": "cpp",
|
||||
"any": "cpp",
|
||||
"array": "cpp",
|
||||
"bit": "cpp",
|
||||
"bitset": "cpp",
|
||||
"cctype": "cpp",
|
||||
"charconv": "cpp",
|
||||
"chrono": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cmath": "cpp",
|
||||
"codecvt": "cpp",
|
||||
"compare": "cpp",
|
||||
"complex": "cpp",
|
||||
"condition_variable": "cpp",
|
||||
"cstdarg": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cstring": "cpp",
|
||||
"ctime": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"cwctype": "cpp",
|
||||
"deque": "cpp",
|
||||
"exception": "cpp",
|
||||
"filesystem": "cpp",
|
||||
"forward_list": "cpp",
|
||||
"iterator": "cpp",
|
||||
"list": "cpp",
|
||||
"map": "cpp",
|
||||
"memory_resource": "cpp",
|
||||
"regex": "cpp",
|
||||
"set": "cpp",
|
||||
"string": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"unordered_set": "cpp",
|
||||
"fstream": "cpp",
|
||||
"future": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iomanip": "cpp",
|
||||
"ios": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"iostream": "cpp",
|
||||
"istream": "cpp",
|
||||
"limits": "cpp",
|
||||
"memory": "cpp",
|
||||
"new": "cpp",
|
||||
"numeric": "cpp",
|
||||
"ostream": "cpp",
|
||||
"queue": "cpp",
|
||||
"random": "cpp",
|
||||
"ratio": "cpp",
|
||||
"span": "cpp",
|
||||
"sstream": "cpp",
|
||||
"stack": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"string_view": "cpp",
|
||||
"strstream": "cpp",
|
||||
"system_error": "cpp",
|
||||
"tuple": "cpp",
|
||||
"typeinfo": "cpp",
|
||||
"valarray": "cpp",
|
||||
"variant": "cpp",
|
||||
"cerrno": "cpp",
|
||||
"cfloat": "cpp",
|
||||
"climits": "cpp",
|
||||
"concepts": "cpp",
|
||||
"format": "cpp",
|
||||
"numbers": "cpp",
|
||||
"semaphore": "cpp",
|
||||
"cfenv": "cpp",
|
||||
"cinttypes": "cpp",
|
||||
"__bit_reference": "cpp",
|
||||
"__functional_base": "cpp",
|
||||
"__locale": "cpp",
|
||||
"__node_handle": "cpp",
|
||||
"__sso_allocator": "cpp",
|
||||
"__std_stream": "cpp",
|
||||
"__threading_support": "cpp",
|
||||
"__tuple": "cpp",
|
||||
"csignal": "cpp",
|
||||
"coroutine": "cpp",
|
||||
"source_location": "cpp",
|
||||
"stdfloat": "cpp",
|
||||
"typeindex": "cpp"
|
||||
"files.exclude": {
|
||||
".*": true,
|
||||
"LICENSE": true,
|
||||
"*.md": true
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(example CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
|
||||
|
||||
# set(COMPILE_FLAGS --cache ${CMAKE_BINARY_DIR}/cache)
|
||||
set(LINK_FLAGS -sASSERTIONS=0 -sMALLOC=emmalloc)
|
||||
add_compile_options(${COMPILE_FLAGS})
|
||||
add_link_options(${COMPILE_FLAGS} ${LINK_FLAGS})
|
||||
|
||||
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)
|
||||
|
||||
file(GLOB SRC_FILES ${CMAKE_SOURCE_DIR}/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)
|
||||
|
||||
# Collect all artifacts in 'dist' directory
|
||||
# WASM-module and corresponding JS-module must have different base names in order to use them in Screeps, so we add suffixes.
|
||||
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/${TARGET_NAME}.wasm ${CMAKE_SOURCE_DIR}/dist/${TARGET_NAME}_module.wasm)
|
||||
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/${TARGET_NAME}.js ${CMAKE_SOURCE_DIR}/dist/${TARGET_NAME}_loader.js)
|
||||
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/js/main.js ${CMAKE_SOURCE_DIR}/dist/main.js)
|
||||
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/js/wasm_loader.js ${CMAKE_SOURCE_DIR}/dist/wasm_loader.js)
|
||||
|
||||
# Following post-build step will automatically upload artifacts to
|
||||
# official Screeps server. If you want to use it, please,
|
||||
# set SCREEPS_TOKEN environment variable to your Screeps API token
|
||||
# (https://docs.screeps.com/auth-tokens.html).
|
||||
# If you don't want to use this script, please, remove following lines.
|
||||
#find_package(Python COMPONENTS Interpreter REQUIRED)
|
||||
#add_custom_command(TARGET ${TARGET_NAME} POST_BUILD COMMAND ${Python_EXECUTABLE} ${screepsxx_SOURCE_DIR}/tools/upload.py ${CMAKE_SOURCE_DIR}/dist $ENV{SCREEPS_TOKEN})
|
||||
15
GLOBAL_VARIABLES.js
Normal file
15
GLOBAL_VARIABLES.js
Normal file
@@ -0,0 +1,15 @@
|
||||
module.exports = {
|
||||
|
||||
// JOBS
|
||||
|
||||
JOB_HARVESTER: 0,
|
||||
JOB_MINER: 1,
|
||||
JOB_SUPPLIER: 2,
|
||||
JOB_BUILDER:3,
|
||||
JOB_UPGRADER: 4,
|
||||
JOB_DEFENDER: 5,
|
||||
JOB_RESERVER: 6,
|
||||
JOB_RESERVED_HARVESTER: 7
|
||||
|
||||
// OTHER
|
||||
};
|
||||
@@ -2,4 +2,4 @@
|
||||
My source code for the MMO-programming game: Screeps.
|
||||
|
||||
# Status
|
||||
As a way to learn c++ programming I used the screepsxx repo for controlling the screeps with c++.
|
||||
To those interested in this code. I wrote the main body of the source code when just learning programming so its kind of a mess. I'm now slowly rewriting the code base to a more SOLID version. :)
|
||||
|
||||
65
ai_main.js
Normal file
65
ai_main.js
Normal file
@@ -0,0 +1,65 @@
|
||||
|
||||
/* ###########################
|
||||
# Cross room managment AI #
|
||||
########################### */
|
||||
|
||||
var GLOBAL = require("GLOBAL_VARIABLES");
|
||||
|
||||
module.exports = {
|
||||
run_front: function()
|
||||
{
|
||||
if(Memory.AImain == null) Memory.AImain = {};
|
||||
if(Memory.AImain.MainHub == null) Memory.AImain.MainHub = "insert name"
|
||||
for(var n_flag in Game.flags){
|
||||
var flag = Game.flags[n_flag];
|
||||
if(flag.color == COLOR_YELLOW){
|
||||
if(flag.room != undefined && flag.room.find(FIND_MY_CREEPS).length != flag.room.find(FIND_CREEPS).length){
|
||||
Game.spawns[Memory.AImain.MainHub].spawnCreep([TOUGH, TOUGH, TOUGH, TOUGH, TOUGH, TOUGH, TOUGH, ATTACK, ATTACK, ATTACK, MOVE],
|
||||
"Defender", { memory: { role:GLOBAL.JOB_DEFENDER , flag: flag.name }});
|
||||
break;
|
||||
}
|
||||
if(flag.room == undefined || flag.room.controller.reservation == undefined || (
|
||||
flag.room.controller.reservation.username == "DouweRavers" && flag.room.controller.reservation.ticksToEnd < 3500))
|
||||
{
|
||||
if(Memory.AImain.MainHub != "insert name") Game.spawns[Memory.AImain.MainHub].spawnCreep([MOVE, CLAIM, CLAIM], "columbus", { memory: { role:GLOBAL.JOB_RESERVER, target: flag.name, home: Memory.AImain.MainHub}});
|
||||
}
|
||||
if(flag.room != undefined && flag.room.controller.reservation != undefined && flag.room.controller.reservation.username == "DouweRavers")
|
||||
{
|
||||
var containers = flag.room.find(FIND_STRUCTURES, { filter:(s) => ( s.structureType == STRUCTURE_CONTAINER )});
|
||||
if(containers.length > 0){
|
||||
var has_miner = false;
|
||||
console.log("test");
|
||||
var creeps = flag.room.find(FIND_CREEPS);
|
||||
for(var cp in creeps){
|
||||
console.log(creeps[cp]);
|
||||
var creep = creeps[cp];
|
||||
if(creep.memory.role == GLOBAL.JOB_MINER){
|
||||
has_miner = true;
|
||||
}
|
||||
}
|
||||
if(!has_miner) {
|
||||
console.log("needs new miner")
|
||||
Game.spawns[Memory.AImain.MainHub].spawnCreep([ WORK, WORK, WORK, WORK, WORK, MOVE ], "ForeignMiner", { memory: { role:GLOBAL.JOB_MINER, container_id: containers[0].id}});
|
||||
}
|
||||
}
|
||||
var harvs = flag.room.find(FIND_MY_CREEPS).length;
|
||||
var source_id = flag.room.find(FIND_SOURCES)[0].id;
|
||||
if(harvs < 4){
|
||||
var spawn = Game.spawns[Memory.AImain.MainHub];
|
||||
var body = [];
|
||||
for(let i=0;i<Math.floor(spawn.room.energyAvailable/200);i++){
|
||||
body.push(WORK);
|
||||
body.push(CARRY);
|
||||
body.push(MOVE);
|
||||
}
|
||||
var name = "ForeignHarvy"+spawn.memory.creep_iterate;
|
||||
spawn.spawnCreep( body, name, { memory:
|
||||
{role: GLOBAL.JOB_RESERVED_HARVESTER, source_id: source_id}});
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
run_back: function(){}
|
||||
};
|
||||
41
controller_creeps.js
Normal file
41
controller_creeps.js
Normal file
@@ -0,0 +1,41 @@
|
||||
|
||||
/* #######################################
|
||||
# Controls the jobs of all the creeps #
|
||||
#######################################*/
|
||||
|
||||
var GLOBAL = require("GLOBAL_VARIABLES");
|
||||
|
||||
var job_harvester = require("job_harvester");
|
||||
var job_miner = require("job_miner");
|
||||
var job_supplier = require("job_supplier");
|
||||
var job_builder = require("job_builder");
|
||||
var job_upgrader = require("job_upgrader");
|
||||
var job_defender = require("job_defender");
|
||||
var job_reserver = require("job_reserver");
|
||||
var job_reserved_harvester = require("job_reserved_harvester");
|
||||
|
||||
module.exports = {
|
||||
run: function(){
|
||||
for(var cr in Game.creeps){
|
||||
cr = Game.creeps[cr];
|
||||
switch(cr.memory.role){
|
||||
case GLOBAL.JOB_HARVESTER: job_harvester.do(cr);
|
||||
break;
|
||||
case GLOBAL.JOB_MINER: job_miner.do(cr);
|
||||
break;
|
||||
case GLOBAL.JOB_SUPPLIER: job_supplier.do(cr);
|
||||
break;
|
||||
case GLOBAL.JOB_BUILDER: job_builder.do(cr);
|
||||
break;
|
||||
case GLOBAL.JOB_UPGRADER: job_upgrader.do(cr);
|
||||
break;
|
||||
case GLOBAL.JOB_DEFENDER: job_defender.do(cr);
|
||||
break;
|
||||
case GLOBAL.JOB_RESERVER: job_reserver.do(cr);
|
||||
break;
|
||||
case GLOBAL.JOB_RESERVED_HARVESTER: job_reserved_harvester.do(cr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
171
controller_spawns.js
Normal file
171
controller_spawns.js
Normal file
@@ -0,0 +1,171 @@
|
||||
|
||||
/* #####################################
|
||||
# Controls the way creeps are spawn #
|
||||
#####################################*/
|
||||
|
||||
var GLOBAL = require("GLOBAL_VARIABLES");
|
||||
|
||||
module.exports = {
|
||||
run: function(){
|
||||
for(var sp in Game.spawns){
|
||||
sp = Game.spawns[sp]
|
||||
// values
|
||||
var creep_count = 0
|
||||
for(var cr in Game.creeps){
|
||||
if(Game.creeps[cr].room.name == sp.room.name) creep_count += 1;
|
||||
}
|
||||
var source_count = 0;
|
||||
if(sp.memory.sources_ids != undefined) source_count = sp.memory.sources_ids.length;
|
||||
|
||||
|
||||
// Memory setup
|
||||
if(sp.memory.creep_iterate == undefined) sp.memory.creep_iterate = 0;
|
||||
|
||||
if(sp.memory.sources_ids == undefined){
|
||||
sp.memory.sources_ids = [];
|
||||
var sources = sp.room.find(FIND_SOURCES);
|
||||
for(var source in sources){
|
||||
source = sources[source];
|
||||
sp.memory.sources_ids.push(source.id);
|
||||
}
|
||||
source_count = sp.memory.sources_ids.length;
|
||||
}
|
||||
|
||||
if(sp.memory.containers_ids == undefined || sp.memory.containers_ids.length != source_count){
|
||||
sp.memory.containers_ids = [];
|
||||
var containers = sp.room.find(FIND_STRUCTURES, { filter:{ structureType: STRUCTURE_CONTAINER }});
|
||||
if(containers.length > 0){
|
||||
for(let i=0;i<containers.length;i+=1){
|
||||
sp.memory.containers_ids.push(containers[i].id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Different spawning configurations
|
||||
|
||||
// Add improved versions at top of if-else statement
|
||||
if(sp.memory.containers_ids.length >= source_count && sp.room.energyCapacityAvailable >= 550 && creep_count > source_count ){ // Miner method: Miners fille containers rest picks up from them
|
||||
// Check current creep_jobs
|
||||
var jobs = { miner: 0, supplier: 0, builder: 0, upgrader: 0, defender: 0};
|
||||
var container_id = sp.memory.containers_ids[0];
|
||||
for(var creep in Game.creeps){
|
||||
var creep = Game.creeps[creep];
|
||||
if(creep.room.name != sp.room.name) continue;
|
||||
switch (creep.memory.role) {
|
||||
case GLOBAL.JOB_MINER:
|
||||
jobs.miner += 1;
|
||||
for(let i=0;i<sp.memory.containers_ids.length;i++){
|
||||
if(creep.memory.container_id != sp.memory.containers_ids[i]) container_id = sp.memory.containers_ids[i];
|
||||
}
|
||||
break;
|
||||
case GLOBAL.JOB_SUPPLIER:
|
||||
jobs.supplier += creep.body.length;
|
||||
break;
|
||||
case GLOBAL.JOB_BUILDER:
|
||||
jobs.builder += creep.body.length;
|
||||
break;
|
||||
case GLOBAL.JOB_UPGRADER:
|
||||
jobs.upgrader += creep.body.length;
|
||||
break;
|
||||
case GLOBAL.JOB_DEFENDER:
|
||||
jobs.defender += 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(jobs.miner < source_count){
|
||||
if(sp.room.energyAvailable >= 550) create_miner(sp, container_id);
|
||||
}
|
||||
else if(jobs.supplier < source_count * 15 && sp.room.energyAvailable >= 300 && jobs.miner > 0) create_supplier(sp);
|
||||
else if(jobs.builder < source_count * 5 && sp.room.energyAvailable >= 300 && jobs.miner > 0) create_builder(sp);
|
||||
else if(jobs.upgrader < source_count * 5 && sp.room.energyAvailable >= 300 && jobs.miner > 0) create_upgrader(sp);
|
||||
else if(jobs.defender < 0 && sp.room.energyAvailable >= sp.room.energyCapacityAvailable*0.8 && jobs.miner > 0) create_defender(sp);
|
||||
} else { // Harvesting method: everybody mines own sources
|
||||
if (sp.room.find(FIND_MY_CREEPS).length < source_count * 5 && sp.room.energyAvailable >= 300){
|
||||
var source_id = sp.memory.sources_ids[sp.memory.creep_iterate % source_count];
|
||||
create_harvester(sp, source_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
function create_harvester(sp, source_id){
|
||||
var body = [];
|
||||
for(let i=0;i<Math.floor(sp.room.energyAvailable/200);i++){
|
||||
body.push(WORK);
|
||||
body.push(CARRY);
|
||||
body.push(MOVE);
|
||||
}
|
||||
var name = "Harvy"+sp.memory.creep_iterate;
|
||||
sp.spawnCreep( body, name, { memory:
|
||||
{role: GLOBAL.JOB_HARVESTER, source_id: source_id}});
|
||||
sp.memory.creep_iterate += 1;
|
||||
}
|
||||
|
||||
function create_miner(sp, container_id){
|
||||
var body = [ WORK, WORK, WORK, WORK, WORK, MOVE ]; // (creep)5*work(2energy/tick)=10energy/tick -> (source)3000energy/300tick=10energy/tick
|
||||
var name = "Mindy"+sp.memory.creep_iterate;
|
||||
sp.spawnCreep( body, name, { memory:
|
||||
{role: GLOBAL.JOB_MINER, container_id: container_id}});
|
||||
sp.memory.creep_iterate += 1;
|
||||
}
|
||||
|
||||
function create_supplier(sp){
|
||||
var body = [WORK];
|
||||
|
||||
for(let i=0;i<Math.floor(sp.room.energyAvailable/100)-1 && i < 8;i++){ // as big as possible
|
||||
body.push(CARRY);
|
||||
body.push(MOVE);
|
||||
}
|
||||
var name = "Suppry"+sp.memory.creep_iterate;
|
||||
sp.spawnCreep( body, name, { memory:
|
||||
{role: GLOBAL.JOB_SUPPLIER}});
|
||||
sp.memory.creep_iterate += 1;
|
||||
}
|
||||
|
||||
function create_builder(sp){
|
||||
var body = [];
|
||||
|
||||
for(let i=0;i<Math.floor(sp.room.energyAvailable/200);i++){ // as big as possible
|
||||
body.push(CARRY);
|
||||
body.push(MOVE);
|
||||
body.push(WORK);
|
||||
}
|
||||
var name = "Bobby"+sp.memory.creep_iterate;
|
||||
sp.spawnCreep( body, name, { memory:
|
||||
{role: GLOBAL.JOB_BUILDER}});
|
||||
sp.memory.creep_iterate += 1;
|
||||
}
|
||||
|
||||
function create_upgrader(sp){
|
||||
var body = [];
|
||||
|
||||
for(let i=0;i<Math.floor(sp.room.energyAvailable/300);i++){ // as big as possible
|
||||
body.push(CARRY);
|
||||
body.push(MOVE);
|
||||
body.push(WORK);
|
||||
body.push(WORK);
|
||||
}
|
||||
var name = "Undy"+sp.memory.creep_iterate;
|
||||
sp.spawnCreep( body, name, { memory:
|
||||
{role: GLOBAL.JOB_UPGRADER}});
|
||||
sp.memory.creep_iterate += 1;
|
||||
}
|
||||
|
||||
function create_defender(sp){
|
||||
var body = [];
|
||||
|
||||
for(let i=0;i<Math.floor(sp.room.energyAvailable/150);i++){ // as big as possible
|
||||
body.push(TOUGH);
|
||||
body.push(TOUGH);
|
||||
body.push(ATTACK);
|
||||
body.push(MOVE);
|
||||
}
|
||||
var name = "Destroyer CODE"+sp.memory.creep_iterate;
|
||||
sp.spawnCreep( body, name, { memory:
|
||||
{role: GLOBAL.JOB_DEFENDER}});
|
||||
sp.memory.creep_iterate += 1;
|
||||
}
|
||||
37
controller_towers.js
Normal file
37
controller_towers.js
Normal file
@@ -0,0 +1,37 @@
|
||||
module.exports = {
|
||||
|
||||
run: function(){
|
||||
for(var spawn in Game.spawns){
|
||||
var room = Game.spawns[spawn].room;
|
||||
var towers = room.find(FIND_MY_STRUCTURES,
|
||||
{filter: (s)=>(s.structureType == STRUCTURE_TOWER)});
|
||||
for(var tower in towers){
|
||||
tower = towers[tower];
|
||||
var enemy = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
|
||||
if(enemy != null) tower.attack(enemy);
|
||||
else {
|
||||
var healt_creep = false;
|
||||
for(var creep in Game.creeps){
|
||||
creep = Game.creeps[creep];
|
||||
if(creep.hits < creep.hitsMax) {
|
||||
tower.heal(creep);
|
||||
healt_creep = true;
|
||||
}
|
||||
}
|
||||
if(!healt_creep && 500 < tower.energy){
|
||||
var structure = tower.pos.findClosestByPath(FIND_STRUCTURES,
|
||||
{filter: (s)=>( (s.structureType != STRUCTURE_RAMPART && s.structureType != STRUCTURE_WALL && s.hits < s.hitsMax )
|
||||
|| ( s.structureType == STRUCTURE_RAMPART && s.hits < 10000 ))});
|
||||
if(structure == null) structure = tower.pos.findClosestByRange(FIND_STRUCTURES,
|
||||
{filter: (s)=>((s.structureType == STRUCTURE_RAMPART) && s.hits < 1000)});
|
||||
if(structure == null) structure = tower.pos.findClosestByRange(FIND_STRUCTURES,
|
||||
{filter: (s)=>((s.structureType == STRUCTURE_WALL) && s.hits < 1000)});
|
||||
tower.repair(structure);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
22
dist/douwco_hivemind_loader.js
vendored
22
dist/douwco_hivemind_loader.js
vendored
File diff suppressed because one or more lines are too long
BIN
dist/douwco_hivemind_module.wasm
vendored
BIN
dist/douwco_hivemind_module.wasm
vendored
Binary file not shown.
14
dist/main.js
vendored
14
dist/main.js
vendored
@@ -1,14 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const wasm_loader = require('wasm_loader')
|
||||
|
||||
var mod;
|
||||
wasm_loader('douwco_hivemind_loader', 'douwco_hivemind_module').then((instance) => {
|
||||
console.log("WASM module loaded.");
|
||||
mod = instance;
|
||||
});
|
||||
|
||||
module.exports.loop = function () {
|
||||
if (mod !== undefined)
|
||||
mod.loop();
|
||||
}
|
||||
24
dist/wasm_loader.js
vendored
24
dist/wasm_loader.js
vendored
@@ -1,24 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = ((mod_js, mod_wasm, opts) => {
|
||||
|
||||
const mod_file = require(mod_js);
|
||||
const bin_file = require(mod_wasm);
|
||||
|
||||
opts = opts || {};
|
||||
|
||||
opts.wasmBinary = bin_file;
|
||||
opts.print = opts.print || ((text) => console.log(text));
|
||||
opts.printErr = opts.printErr || ((text) => console.log(`error: ${text}`));
|
||||
opts.onAbort = opts.onAbort || (() => console.log('WASM aborted!!!'));
|
||||
|
||||
// == don't call main()
|
||||
if (typeof opts.noInitialRun === "undefined")
|
||||
opts.noInitialRun = true;
|
||||
|
||||
// == don't terminate after returning from main()
|
||||
if (typeof opts.noExitRuntime === "undefined")
|
||||
opts.noExitRuntime = true;
|
||||
|
||||
return mod_file(opts);
|
||||
});
|
||||
1
emsdk
1
emsdk
Submodule emsdk deleted from f39e849eff
@@ -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
|
||||
@@ -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
|
||||
@@ -1,47 +0,0 @@
|
||||
#ifndef DOUWCO_HIVEMIND_HARVESTER_HPP
|
||||
#define DOUWCO_HIVEMIND_HARVESTER_HPP
|
||||
|
||||
#include <Screeps/Creep.hpp>
|
||||
|
||||
#include "Creeps/Creep.hpp"
|
||||
|
||||
namespace DouwcoHivemind
|
||||
{
|
||||
class HarvesterRole : public Creep
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
void loop() override;
|
||||
|
||||
private:
|
||||
void harvestSource();
|
||||
std::unique_ptr<Screeps::Source> getSourceTarget();
|
||||
void searchSource();
|
||||
|
||||
void depositEnergy();
|
||||
std::unique_ptr<Screeps::Structure> getDepositTarget();
|
||||
void searchDeposit();
|
||||
|
||||
std::unique_ptr<Screeps::RoomObject> getRoomObjectTarget();
|
||||
};
|
||||
}
|
||||
|
||||
#endif // DOUWCO_HIVEMIND_HARVESTER_HPP
|
||||
@@ -1,74 +0,0 @@
|
||||
#ifndef DOUWCO_HIVEMIND_ENGINE_HPP
|
||||
#define DOUWCO_HIVEMIND_ENGINE_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <Screeps/JS.hpp>
|
||||
#include <Screeps/Game.hpp>
|
||||
#include <Screeps/Creep.hpp>
|
||||
|
||||
#include "Constants.hpp"
|
||||
#include "Creeps/Creep.hpp"
|
||||
#include "Creeps/Harvester.hpp"
|
||||
#include "Structures/Structure.hpp"
|
||||
#include "Structures/Spawn.hpp"
|
||||
|
||||
namespace DouwcoHivemind
|
||||
{
|
||||
class Creep;
|
||||
class Structure;
|
||||
|
||||
class Engine
|
||||
{
|
||||
private:
|
||||
std::vector<std::unique_ptr<Creep>> creeps;
|
||||
std::vector<std::unique_ptr<Structure>> structures;
|
||||
|
||||
public:
|
||||
Engine()
|
||||
{
|
||||
ReadOutCreeps();
|
||||
ReadOutStructures();
|
||||
}
|
||||
~Engine() {}
|
||||
|
||||
void loop()
|
||||
{
|
||||
for (auto &creep : creeps)
|
||||
creep->loop();
|
||||
|
||||
for (auto &structure : structures)
|
||||
structure->loop();
|
||||
}
|
||||
|
||||
private:
|
||||
void ReadOutCreeps()
|
||||
{
|
||||
auto src_creeps = Screeps::Game.creeps();
|
||||
for (auto &creep : src_creeps)
|
||||
{
|
||||
CreepRole role = creep.second.memory()["role"];
|
||||
switch (role)
|
||||
{
|
||||
case CreepRole::HARVESTER:
|
||||
creeps.push_back(std::make_unique<HarvesterRole>(creep.second));
|
||||
break;
|
||||
case CreepRole::UNEMPLOYED:
|
||||
default:
|
||||
EM_ASM({console.log('Undefined role for creep' + $0)}, creep.first.c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ReadOutStructures()
|
||||
{
|
||||
auto spawns = Screeps::Game.spawns();
|
||||
for (auto &spawn : spawns)
|
||||
{
|
||||
structures.push_back(std::make_unique<Spawn>(spawn.second));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // DOUWCO_HIVEMIND_ENGINE_HPP
|
||||
@@ -1,24 +0,0 @@
|
||||
#ifndef DOUWCO_HIVEMIND_SPAWN_HPP
|
||||
#define DOUWCO_HIVEMIND_SPAWN_HPP
|
||||
|
||||
#include <Screeps/StructureSpawn.hpp>
|
||||
|
||||
#include "Structures/Structure.hpp"
|
||||
|
||||
namespace DouwcoHivemind
|
||||
{
|
||||
class Spawn : public Structure
|
||||
{
|
||||
private:
|
||||
Screeps::StructureSpawn spawn;
|
||||
|
||||
public:
|
||||
Spawn(Screeps::StructureSpawn spwn) : spawn(spwn),
|
||||
Structure() {}
|
||||
~Spawn() {}
|
||||
|
||||
void loop() override;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // DOUWCO_HIVEMIND_SPAWN_HPP
|
||||
@@ -1,15 +0,0 @@
|
||||
#ifndef DOUWCO_HIVEMIND_STRUCTURE_HPP
|
||||
#define DOUWCO_HIVEMIND_STRUCTURE_HPP
|
||||
|
||||
#include <Screeps/Structure.hpp>
|
||||
|
||||
namespace DouwcoHivemind
|
||||
{
|
||||
class Structure
|
||||
{
|
||||
public:
|
||||
virtual void loop(){}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // DOUWCO_HIVEMIND_STRUCTURE_HPP
|
||||
57
job_builder.js
Normal file
57
job_builder.js
Normal file
@@ -0,0 +1,57 @@
|
||||
|
||||
/* #######################################################
|
||||
# Deliver energy to spawns, controller and extencions #
|
||||
####################################################### */
|
||||
|
||||
module.exports = {
|
||||
do: function(creep){
|
||||
//creep.memory.target = null;
|
||||
|
||||
// Memory creation
|
||||
if(creep.memory.restock == undefined) creep.memory.restock = true;
|
||||
if(creep.memory.target == undefined) creep.memory.target = null;
|
||||
|
||||
// Job implementation
|
||||
if(creep.memory.restock){
|
||||
if(creep.memory.target == null){
|
||||
if(creep.room.storage != null && 1000 < creep.room.storage.store.getUsedCapacity(RESOURCE_ENERGY)) creep.memory.target = creep.room.storage.id;
|
||||
else {
|
||||
var container = creep.pos.findClosestByPath(FIND_STRUCTURES,
|
||||
{filter: (s)=>(s.structureType == STRUCTURE_CONTAINER
|
||||
&& 0 < s.store.getUsedCapacity(RESOURCE_ENERGY))});
|
||||
if(container != null) creep.memory.target = container.id;
|
||||
}
|
||||
}
|
||||
var target = Game.getObjectById(creep.memory.target);
|
||||
if(creep.withdraw(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) creep.moveTo(target);
|
||||
if(target != null && target.store.getUsedCapacity(RESOURCE_ENERGY) < creep.store.getFreeCapacity(RESOURCE_ENERGY)*0.4) creep.memory.target = null;
|
||||
if(creep.store.getFreeCapacity(RESOURCE_ENERGY) == 0){
|
||||
creep.memory.restock = false;
|
||||
creep.memory.target = null;
|
||||
}
|
||||
} else {
|
||||
if(creep.memory.target == null){ // aquire target
|
||||
// constuction
|
||||
var structure = creep.pos.findClosestByRange(FIND_MY_CONSTRUCTION_SITES);
|
||||
|
||||
// ramparts
|
||||
if(structure == null){
|
||||
var ramparts = creep.room.find(FIND_STRUCTURES,
|
||||
{filter: (s)=>((s.structureType == STRUCTURE_RAMPART || s.structureType == STRUCTURE_WALL) && s.hits < s.hitsMax)});
|
||||
for(var ramp in ramparts){
|
||||
ramp = ramparts[ramp];
|
||||
if(structure == null || structure.hits > ramp.hits) structure = ramp;
|
||||
}
|
||||
}
|
||||
creep.memory.target = structure.id;
|
||||
}
|
||||
var target = Game.getObjectById(creep.memory.target);
|
||||
if(target == null || target.hits == target.hitsMax) creep.memory.target = null;
|
||||
if(creep.build(target) == ERR_NOT_IN_RANGE || creep.repair(target) == ERR_NOT_IN_RANGE) creep.moveTo(target);
|
||||
if(creep.store.getUsedCapacity(RESOURCE_ENERGY) == 0){
|
||||
creep.memory.restock = true;
|
||||
creep.memory.target = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
22
job_defender.js
Normal file
22
job_defender.js
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
/* #######################################################
|
||||
# Deliver energy to spawns, controller and extencions #
|
||||
####################################################### */
|
||||
|
||||
module.exports = {
|
||||
do: function(creep){
|
||||
if(creep.memory.flag != undefined && Game.flags[creep.memory.flag].room.name != creep.room.name) creep.moveTo(Game.flags[creep.memory.flag]);
|
||||
if(creep.memory.target == undefined) creep.memory.target = null;
|
||||
if(creep.memory.target == null){
|
||||
var enemy = creep.pos.findClosestByPath(FIND_HOSTILE_CREEPS);
|
||||
if(enemy != null) creep.memory.target = enemy.id;
|
||||
}
|
||||
var enemy = Game.getObjectById(creep.memory.target);
|
||||
if(enemy != null){
|
||||
creep.say("ATTACK");
|
||||
if(creep.attack(enemy) == ERR_NOT_IN_RANGE) creep.moveTo(enemy);
|
||||
} else {
|
||||
creep.memory.target = null;
|
||||
}
|
||||
}
|
||||
};
|
||||
92
job_harvester.js
Normal file
92
job_harvester.js
Normal file
@@ -0,0 +1,92 @@
|
||||
|
||||
/* ###########################################
|
||||
# Mine energy and do all fundamental jobs #
|
||||
###########################################*/
|
||||
|
||||
module.exports = {
|
||||
do: function(creep){
|
||||
//memory setup
|
||||
if(creep.memory.harvesting == undefined) creep.memory.harvesting = true;
|
||||
if(creep.memory.counter == undefined) creep.memory.counter = 0;
|
||||
if(creep.memory.target == undefined) creep.memory.target = null;
|
||||
|
||||
if (creep.memory.harvesting){
|
||||
var source = Game.getObjectById(creep.memory.source_id);
|
||||
var result = creep.harvest(source);
|
||||
if (result == ERR_NOT_IN_RANGE){
|
||||
creep.moveTo(source);
|
||||
}
|
||||
if (result == ERR_NOT_ENOUGH_RESOURCES || creep.store.getFreeCapacity(RESOURCE_ENERGY) == 0){
|
||||
creep.memory.harvesting = false;
|
||||
creep.memory.target = null;
|
||||
}
|
||||
} else {
|
||||
if(creep.memory.target == null){
|
||||
switch(creep.memory.counter%10){
|
||||
case 0: // upgrading
|
||||
creep.memory.target = creep.room.controller.id;
|
||||
break;
|
||||
case 1: // suppling
|
||||
case 2:
|
||||
case 3:
|
||||
case 4:
|
||||
creep.memory.target = creep.pos.findClosestByRange(FIND_MY_STRUCTURES,{
|
||||
filter:(s) => ( s.structureType == STRUCTURE_SPAWN || s.structureType == STRUCTURE_EXTENSION ) && s.store.getFreeCapacity(RESOURCE_ENERGY) > 0});
|
||||
if(creep.memory.target != null) creep.memory.target = creep.memory.target.id;
|
||||
else creep.memory.counter += 1;
|
||||
break;
|
||||
case 5:
|
||||
case 6:
|
||||
case 7:
|
||||
case 8:
|
||||
creep.memory.target = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES);
|
||||
if(creep.memory.target != null) creep.memory.target = creep.memory.target.id;
|
||||
else creep.memory.counter += 1;
|
||||
break;
|
||||
case 9: // repairing (no walls or ramparts)
|
||||
creep.memory.target = creep.pos.findClosestByRange(FIND_STRUCTURES,{
|
||||
filter:(s) => ( s.structureType != STRUCTURE_WALL && s.structureType != STRUCTURE_RAMPART) && s.hits < s.hitsMax});
|
||||
if(creep.memory.target != null) creep.memory.target = creep.memory.target.id;
|
||||
else creep.memory.counter += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var target = Game.getObjectById(creep.memory.target);
|
||||
var response = OK;
|
||||
switch(creep.memory.counter%10){
|
||||
case 0:
|
||||
response = creep.transfer(target, RESOURCE_ENERGY);
|
||||
break;
|
||||
case 1:
|
||||
case 2:
|
||||
case 3:
|
||||
case 4:
|
||||
response = creep.transfer(target, RESOURCE_ENERGY);
|
||||
if(target != null && target.store.getFreeCapacity(RESOURCE_ENERGY) == 0) creep.memory.target = null;
|
||||
break;
|
||||
case 5:
|
||||
case 6:
|
||||
case 7:
|
||||
case 8:
|
||||
response = creep.build(target);
|
||||
if(target != null && target.progress == target.progressTotal) creep.memory.target = null;
|
||||
break;
|
||||
case 9:
|
||||
response = creep.repair(target);
|
||||
if(target != null && target.hits == target.hitsMax) creep.memory.target = null;
|
||||
break;
|
||||
}
|
||||
if(response == ERR_NOT_IN_RANGE){
|
||||
creep.moveTo(target);
|
||||
} else if(response == ERR_FULL || response == ERR_INVALID_TARGET){
|
||||
creep.memory.target = null;
|
||||
}
|
||||
if (creep.store.getUsedCapacity(RESOURCE_ENERGY) == 0){
|
||||
creep.memory.harvesting = true;
|
||||
creep.memory.target = null;
|
||||
creep.memory.counter += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
16
job_miner.js
Normal file
16
job_miner.js
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
/* ########################################
|
||||
# Mine energy and drop it in container #
|
||||
########################################*/
|
||||
|
||||
module.exports = {
|
||||
do: function(creep){
|
||||
var container = Game.getObjectById(creep.memory.container_id);
|
||||
if(container != null && creep.pos.isEqualTo(container.pos)){
|
||||
if(creep.memory.source_id == undefined) creep.memory.source_id = creep.pos.findClosestByPath(FIND_SOURCES).id;
|
||||
creep.harvest(Game.getObjectById(creep.memory.source_id));
|
||||
} else {
|
||||
creep.moveTo(Game.getObjectById(creep.memory.container_id));
|
||||
}
|
||||
}
|
||||
};
|
||||
93
job_reserved_harvester.js
Normal file
93
job_reserved_harvester.js
Normal file
@@ -0,0 +1,93 @@
|
||||
|
||||
/* ###########################################
|
||||
# Mine energy and do all fundamental jobs #
|
||||
###########################################*/
|
||||
|
||||
module.exports = {
|
||||
do: function(creep){
|
||||
creep.memory.home = "Central"
|
||||
//memory setup
|
||||
if(creep.memory.harvesting == undefined) creep.memory.harvesting = true;
|
||||
if(creep.memory.counter == undefined) creep.memory.counter = 0;
|
||||
if(creep.memory.target == undefined) creep.memory.target = null;
|
||||
|
||||
if (creep.memory.harvesting){
|
||||
var source = Game.getObjectById(creep.memory.source_id);
|
||||
if(source != undefined && creep.room.name == source.room.name){
|
||||
var container = creep.pos.findClosestByPath(FIND_STRUCTURES, { filter:(s) => ( s.structureType == STRUCTURE_CONTAINER && s.store.getUsedCapacity(RESOURCE_ENERGY) > creep.store.getFreeCapacity(RESOURCE_ENERGY))});
|
||||
if(container == null) {
|
||||
var source = Game.getObjectById(creep.memory.source_id);
|
||||
result = creep.harvest(source);
|
||||
if (result == ERR_NOT_IN_RANGE){
|
||||
creep.moveTo(source);
|
||||
}
|
||||
} else {
|
||||
var result = creep.withdraw(container, RESOURCE_ENERGY);
|
||||
if (result == ERR_NOT_IN_RANGE){
|
||||
creep.moveTo(container);
|
||||
}
|
||||
}
|
||||
if (creep.store.getFreeCapacity(RESOURCE_ENERGY) == 0){
|
||||
creep.memory.harvesting = false;
|
||||
creep.memory.target = null;
|
||||
}
|
||||
} else {
|
||||
creep.moveTo(source);
|
||||
}
|
||||
} else {
|
||||
if(creep.memory.target == null){
|
||||
switch(creep.memory.counter%3){
|
||||
case 0: // building
|
||||
creep.memory.target = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES);
|
||||
if(creep.memory.target != null) creep.memory.target = creep.memory.target.id;
|
||||
else creep.memory.counter += 1;
|
||||
break;
|
||||
case 1: // repairing (no walls or ramparts)
|
||||
creep.memory.target = creep.pos.findClosestByRange(FIND_STRUCTURES,{
|
||||
filter:(s) => ( s.structureType != STRUCTURE_WALL && s.structureType != STRUCTURE_RAMPART) && s.hits < s.hitsMax-creep.store.getUsedCapacity(RESOURCE_ENERGY)*3/4});
|
||||
if(creep.memory.target != null) creep.memory.target = creep.memory.target.id;
|
||||
else creep.memory.counter += 1;
|
||||
break;
|
||||
case 2:
|
||||
var home = Game.spawns[creep.memory.home];
|
||||
if(creep.room.name == home.room.name){
|
||||
creep.memory.target = creep.pos.findClosestByRange(FIND_MY_STRUCTURES,{
|
||||
filter:(s) => ( s.structureType == STRUCTURE_SPAWN || s.structureType == STRUCTURE_EXTENSION || s.structureType == STRUCTURE_STORAGE ) && s.store.getFreeCapacity(RESOURCE_ENERGY) > 0});
|
||||
if(creep.memory.target != null) creep.memory.target = creep.memory.target.id;
|
||||
else creep.memory.counter += 1;
|
||||
} else {
|
||||
creep.moveTo(home);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var target = Game.getObjectById(creep.memory.target);
|
||||
var response = OK;
|
||||
switch(creep.memory.counter%3){
|
||||
case 0: // building
|
||||
response = creep.build(target);
|
||||
if(target != null && target.progress == target.progressTotal) creep.memory.target = null;
|
||||
break;
|
||||
case 1:
|
||||
response = creep.repair(target);
|
||||
if(target != null && target.hits == target.hitsMax) creep.memory.target = null;
|
||||
break;
|
||||
case 2:
|
||||
response = creep.transfer(target, RESOURCE_ENERGY);
|
||||
if(target != null && target.store.getFreeCapacity(RESOURCE_ENERGY) == 0) creep.memory.target = null;
|
||||
break;
|
||||
}
|
||||
if(response == ERR_NOT_IN_RANGE){
|
||||
creep.moveTo(target);
|
||||
} else if(response == ERR_FULL || response == ERR_INVALID_TARGET){
|
||||
creep.memory.target = null;
|
||||
}
|
||||
if (creep.store.getUsedCapacity(RESOURCE_ENERGY) == 0){
|
||||
creep.memory.harvesting = true;
|
||||
creep.memory.target = null;
|
||||
creep.memory.counter += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
16
job_reserver.js
Normal file
16
job_reserver.js
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
/* ###########################################
|
||||
# Go to a controller and reserve the room #
|
||||
###########################################*/
|
||||
|
||||
module.exports = {
|
||||
do: function(creep){
|
||||
if(Game.flags[creep.memory.target].room != undefined && Game.flags[creep.memory.target].room == creep.room){
|
||||
if(creep.reserveController(creep.room.controller) == ERR_NOT_IN_RANGE){
|
||||
creep.moveTo(creep.room.controller);
|
||||
}
|
||||
} else {
|
||||
creep.moveTo(Game.flags[creep.memory.target]);
|
||||
}
|
||||
}
|
||||
};
|
||||
54
job_supplier.js
Normal file
54
job_supplier.js
Normal file
@@ -0,0 +1,54 @@
|
||||
|
||||
/* #######################################################
|
||||
# Deliver energy to spawns, controller and extencions #
|
||||
####################################################### */
|
||||
|
||||
module.exports = {
|
||||
do: function(creep){
|
||||
// Memory creation
|
||||
if(creep.memory.restock == undefined) creep.memory.restock = true;
|
||||
if(creep.memory.target == undefined) creep.memory.target = null;
|
||||
// Job implementation
|
||||
if(creep.memory.restock){
|
||||
if(creep.memory.target == null){ // aquire target
|
||||
var containers = creep.room.find(FIND_STRUCTURES,
|
||||
{filter: (s)=>(s.structureType == STRUCTURE_CONTAINER
|
||||
&& s.store.getUsedCapacity(RESOURCE_ENERGY) >= creep.store.getFreeCapacity(RESOURCE_ENERGY)/2)});
|
||||
var container = containers[0];
|
||||
for(var c in containers){
|
||||
c = containers[c];
|
||||
if(c.store.getUsedCapacity(RESOURCE_ENERGY) > container.store.getUsedCapacity(RESOURCE_ENERGY)) container = c;
|
||||
}
|
||||
if(container != null) creep.memory.target = container.id;
|
||||
else creep.memory.target = creep.room.storage.id;
|
||||
}
|
||||
var target = Game.getObjectById(creep.memory.target);
|
||||
if(creep.withdraw(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) creep.moveTo(target);
|
||||
if(target != null && target.store.getUsedCapacity(RESOURCE_ENERGY) < creep.store.getFreeCapacity(RESOURCE_ENERGY)*0.4) creep.memory.target = null;
|
||||
if(creep.store.getFreeCapacity(RESOURCE_ENERGY) == 0){
|
||||
creep.memory.restock = false;
|
||||
creep.memory.target = null;
|
||||
}
|
||||
} else {
|
||||
if(creep.memory.target == null){ // aquire target
|
||||
var structure = creep.pos.findClosestByPath(FIND_MY_STRUCTURES,
|
||||
{filter: (s)=>(s.structureType == STRUCTURE_TOWER && s.store.getUsedCapacity(RESOURCE_ENERGY) < 900)});
|
||||
if(structure == null) structure = creep.pos.findClosestByPath(FIND_MY_STRUCTURES,
|
||||
{filter: (s)=>(( s.structureType == STRUCTURE_SPAWN || s.structureType == STRUCTURE_EXTENSION)
|
||||
&& s.store.getFreeCapacity(RESOURCE_ENERGY) > s.store.getCapacity(RESOURCE_ENERGY)*0.1)});
|
||||
if(structure == null) structure = creep.pos.findClosestByPath(FIND_MY_STRUCTURES,
|
||||
{filter: (s)=>(( s.id == "5f3e44e1014017c66fc5df10") && s.store.getFreeCapacity() > 100)});
|
||||
if(structure == null) structure = creep.room.storage;
|
||||
if(structure == null) return;
|
||||
creep.memory.target = structure.id;
|
||||
}
|
||||
var target = Game.getObjectById(creep.memory.target);
|
||||
if(creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) creep.moveTo(target);
|
||||
if(target != null && target.store.getFreeCapacity(RESOURCE_ENERGY) == 0) creep.memory.target = null;
|
||||
if(creep.store.getUsedCapacity(RESOURCE_ENERGY) == 0){
|
||||
creep.memory.restock = true;
|
||||
creep.memory.target = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
43
job_upgrader.js
Normal file
43
job_upgrader.js
Normal file
@@ -0,0 +1,43 @@
|
||||
|
||||
/* #######################################################
|
||||
# Deliver energy to spawns, controller and extencions #
|
||||
####################################################### */
|
||||
|
||||
module.exports = {
|
||||
do: function(creep){
|
||||
// Memory creation
|
||||
if(creep.memory.restock == undefined) creep.memory.restock = true;
|
||||
if(creep.memory.target == undefined) creep.memory.target = null;
|
||||
|
||||
// Job implementation
|
||||
if(creep.memory.restock){
|
||||
if(creep.memory.target == null){
|
||||
if(creep.room.storage != null && 1000 < creep.room.storage.store.getUsedCapacity(RESOURCE_ENERGY)) creep.memory.target = creep.room.storage.id;
|
||||
else {
|
||||
var container = creep.pos.findClosestByPath(FIND_STRUCTURES,
|
||||
{filter: (s)=>(s.structureType == STRUCTURE_CONTAINER
|
||||
&& 0 < s.store.getUsedCapacity(RESOURCE_ENERGY))});
|
||||
if(container != null) creep.memory.target = container.id;
|
||||
}
|
||||
}
|
||||
var target = Game.getObjectById(creep.memory.target);
|
||||
if(creep.withdraw(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) creep.moveTo(target);
|
||||
if(target != null && target.store.getUsedCapacity(RESOURCE_ENERGY) < creep.store.getFreeCapacity(RESOURCE_ENERGY)*0.4) creep.memory.target = null;
|
||||
if(creep.store.getFreeCapacity(RESOURCE_ENERGY) == 0){
|
||||
creep.memory.restock = false;
|
||||
creep.memory.target = null;
|
||||
}
|
||||
} else {
|
||||
if(creep.memory.target == null){ // aquire target
|
||||
var structure = structure = creep.room.controller;
|
||||
creep.memory.target = structure.id;
|
||||
}
|
||||
var target = Game.getObjectById(creep.memory.target);
|
||||
if(creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) creep.moveTo(target);
|
||||
if(creep.store.getUsedCapacity(RESOURCE_ENERGY) == 0){
|
||||
creep.memory.restock = true;
|
||||
creep.memory.target = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
14
js/main.js
14
js/main.js
@@ -1,14 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const wasm_loader = require('wasm_loader')
|
||||
|
||||
var mod;
|
||||
wasm_loader('douwco_hivemind_loader', 'douwco_hivemind_module').then((instance) => {
|
||||
console.log("WASM module loaded.");
|
||||
mod = instance;
|
||||
});
|
||||
|
||||
module.exports.loop = function () {
|
||||
if (mod !== undefined)
|
||||
mod.loop();
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = ((mod_js, mod_wasm, opts) => {
|
||||
|
||||
const mod_file = require(mod_js);
|
||||
const bin_file = require(mod_wasm);
|
||||
|
||||
opts = opts || {};
|
||||
|
||||
opts.wasmBinary = bin_file;
|
||||
opts.print = opts.print || ((text) => console.log(text));
|
||||
opts.printErr = opts.printErr || ((text) => console.log(`error: ${text}`));
|
||||
opts.onAbort = opts.onAbort || (() => console.log('WASM aborted!!!'));
|
||||
|
||||
// == don't call main()
|
||||
if (typeof opts.noInitialRun === "undefined")
|
||||
opts.noInitialRun = true;
|
||||
|
||||
// == don't terminate after returning from main()
|
||||
if (typeof opts.noExitRuntime === "undefined")
|
||||
opts.noExitRuntime = true;
|
||||
|
||||
return mod_file(opts);
|
||||
});
|
||||
26
main.js
Normal file
26
main.js
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
var controller_spawns = require("controller_spawns");
|
||||
var controller_creeps = require("controller_creeps");
|
||||
var controller_towers = require("controller_towers");
|
||||
var ai_main = require("ai_main");
|
||||
|
||||
module.exports.loop = function () {
|
||||
ai_main.run_front();
|
||||
controller_creeps.run();
|
||||
controller_towers.run();
|
||||
controller_spawns.run();
|
||||
if (Game.time % 60 == 30) clear_dead_creeps_from_memory();
|
||||
ai_main.run_back();
|
||||
}
|
||||
|
||||
/*
|
||||
GLOBAL FUNCTIONS
|
||||
*/
|
||||
|
||||
function clear_dead_creeps_from_memory() {
|
||||
for (var cr in Memory.creeps) {
|
||||
if (Game.creeps[cr] == null) {
|
||||
delete (Memory.creeps[cr]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 .
|
||||
Submodule screepsxx deleted from 8500506e1c
@@ -1,255 +0,0 @@
|
||||
#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"
|
||||
|
||||
bool isNearTo(const Screeps::RoomPosition &pos1, const Screeps::RoomPosition &pos2, int dist);
|
||||
|
||||
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(creep.pos(), source->pos(), 1))
|
||||
{
|
||||
int resp = creep.harvest(*source);
|
||||
}
|
||||
else
|
||||
creep.moveTo(*source);
|
||||
}
|
||||
|
||||
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(creep.pos(), structure->pos(), 3))
|
||||
{
|
||||
auto controller = dynamic_cast<Screeps::StructureController *>(structure.get());
|
||||
if (!controller)
|
||||
return;
|
||||
int resp = creep.upgradeController(*controller);
|
||||
}
|
||||
else
|
||||
creep.moveTo(*structure);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isNearTo(creep.pos(), structure->pos(), 1))
|
||||
{
|
||||
int resp = creep.transfer(*structure, Screeps::RESOURCE_ENERGY);
|
||||
}
|
||||
else
|
||||
creep.moveTo(*structure);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
33
src/loop.cpp
33
src/loop.cpp
@@ -1,33 +0,0 @@
|
||||
#include <Screeps/Context.hpp>
|
||||
#include <Screeps/Creep.hpp>
|
||||
#include <Screeps/StructureSpawn.hpp>
|
||||
|
||||
#include <emscripten.h>
|
||||
#include <emscripten/bind.h>
|
||||
#include <emscripten/val.h>
|
||||
|
||||
#include "Constants.hpp"
|
||||
#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);
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <Screeps/JS.hpp>
|
||||
#include <Screeps/Game.hpp>
|
||||
#include <Screeps/Room.hpp>
|
||||
#include <Screeps/StructureSpawn.hpp>
|
||||
#include <emscripten.h>
|
||||
|
||||
#include "Constants.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