Improved walking system.
This commit is contained in:
61
Screeps/Commands.js
Normal file
61
Screeps/Commands.js
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
setup(){
|
||||||
|
global.fullReset = fullReset;
|
||||||
|
global.resetRooms = resetRooms;
|
||||||
|
global.resetStructures = resetStructures;
|
||||||
|
global.resetCreeps = resetCreeps;
|
||||||
|
global.resetConstruction = resetConstruction;
|
||||||
|
|
||||||
|
global.buildRoads = buildRoads;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function fullReset(){
|
||||||
|
resetRooms();
|
||||||
|
resetCreeps();
|
||||||
|
resetStructures();
|
||||||
|
resetConstruction();
|
||||||
|
global.started = false;
|
||||||
|
global.compiled = false;
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
function resetRooms(){
|
||||||
|
Object.values(Game.rooms).forEach(room => room.memory = {});
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
function resetStructures(){
|
||||||
|
Object.values(Game.structures).forEach(structure => structure.memory = {});
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
function resetCreeps(){
|
||||||
|
Object.values(Game.creeps).forEach(creep => creep.suicide());
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
function resetConstruction(){
|
||||||
|
Object.values(Game.rooms).forEach(room=>room.find(FIND_CONSTRUCTION_SITES).forEach(cs=>cs.remove()));
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildRoads(){
|
||||||
|
Object.values(Game.rooms).forEach(room=>
|
||||||
|
{
|
||||||
|
room.memory.layout.sources.forEach(
|
||||||
|
sId => {
|
||||||
|
const source = Game.getObjectById(sId);
|
||||||
|
var roads = [];
|
||||||
|
roads = roads.concat(source.pos.findPathTo(room.controller));
|
||||||
|
roads.pop();
|
||||||
|
roads = roads.concat(source.pos.findPathTo(room.find(FIND_MY_STRUCTURES, { filter:{ structureType:STRUCTURE_SPAWN }})[0]));
|
||||||
|
roads.pop();
|
||||||
|
roads.forEach(tile => room.createConstructionSite(tile.x, tile.y, STRUCTURE_ROAD));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
45
Screeps/CreepClass.js
Normal file
45
Screeps/CreepClass.js
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
const jobBuilder = require("JobBuilder");
|
||||||
|
const jobCleaner = require("JobCleaner");
|
||||||
|
const jobMiner = require("JobMiner");
|
||||||
|
const jobSupplier = require("JobSupplier");
|
||||||
|
const jobUpgrader = require("JobUpgrader");
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
setup: function () {
|
||||||
|
Creep.prototype = _Creep.prototype;
|
||||||
|
global.Role = Role;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const Role = {
|
||||||
|
BUILDER: 0,
|
||||||
|
CLEANER: 1,
|
||||||
|
MINER: 2,
|
||||||
|
SUPPLIER: 3,
|
||||||
|
UPGRADER: 4
|
||||||
|
}
|
||||||
|
|
||||||
|
class _Creep extends Creep {
|
||||||
|
begin(){
|
||||||
|
if(!this.memory.job) this.memory.job = { role: Role.HARVESTER };
|
||||||
|
switch (this.memory.job.role) {
|
||||||
|
case Role.BUILDER: jobBuilder.begin(this); break;
|
||||||
|
case Role.CLEANER: jobCleaner.begin(this); break;
|
||||||
|
case Role.MINER: jobMiner.begin(this); break;
|
||||||
|
case Role.SUPPLIER: jobSupplier.begin(this); break;
|
||||||
|
case Role.UPGRADER: jobUpgrader.begin(this); break;
|
||||||
|
}
|
||||||
|
this.memory.init = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
tick(){
|
||||||
|
if(!this.memory.init) this.begin();
|
||||||
|
switch (this.memory.job.role) {
|
||||||
|
case Role.BUILDER: jobBuilder.tick(this); break;
|
||||||
|
case Role.CLEANER: jobCleaner.tick(this); break;
|
||||||
|
case Role.MINER: jobMiner.tick(this); break;
|
||||||
|
case Role.SUPPLIER: jobSupplier.tick(this); break;
|
||||||
|
case Role.UPGRADER: jobUpgrader.tick(this); break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
94
Screeps/JobBuilder.js
Normal file
94
Screeps/JobBuilder.js
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
module.exports = {
|
||||||
|
begin(creep){
|
||||||
|
if(!creep.memory.collecting) creep.memory.collecting = false;
|
||||||
|
if(!creep.memory.counter) creep.memory.counter = 0;
|
||||||
|
},
|
||||||
|
|
||||||
|
tick(creep){
|
||||||
|
if(creep.memory.collecting) {
|
||||||
|
GetEnergy(creep);
|
||||||
|
energyFullCheck(creep);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
BuildOrRepair(creep);
|
||||||
|
energyEmptyCheck(creep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function GetEnergy(creep){
|
||||||
|
if(!creep.memory.target) findEnergyTarget(creep);
|
||||||
|
const target = Game.getObjectById(creep.memory.target);
|
||||||
|
if(!target) creep.memory.target = undefined;
|
||||||
|
if(creep.pos.isNearTo(target)) {
|
||||||
|
if(!target.store) creep.pickup(target);
|
||||||
|
else creep.withdraw(target, RESOURCE_ENERGY);
|
||||||
|
}
|
||||||
|
else creep.moveTo(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
function energyFullCheck(creep){
|
||||||
|
if(!creep.store.getFreeCapacity(RESOURCE_ENERGY)) {
|
||||||
|
creep.memory.collecting = false;
|
||||||
|
creep.memory.target = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function BuildOrRepair(creep){
|
||||||
|
if(!creep.memory.target) findBuildOrRepairTarget(creep);
|
||||||
|
if(!creep.memory.target) return;
|
||||||
|
const target = Game.getObjectById(creep.memory.target);
|
||||||
|
if(!target) { creep.memory.target = undefined; return; }
|
||||||
|
if(target.hits && target.hits === target.hitsMax) { creep.memory.target = undefined; return; }
|
||||||
|
if(creep.pos.isNearTo(target))
|
||||||
|
if(!target.hits) creep.build(target);
|
||||||
|
else creep.repair(target);
|
||||||
|
else creep.moveTo(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
function energyEmptyCheck(creep){
|
||||||
|
if(!creep.store.getUsedCapacity(RESOURCE_ENERGY)) {
|
||||||
|
creep.memory.collecting = true;
|
||||||
|
creep.memory.target = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function findEnergyTarget(creep){
|
||||||
|
var target;
|
||||||
|
if(creep.room.storage && creep.room.storage.store.getUsedCapacity(RESOURCE_ENERGY) > creep.store.getFreeCapacity(RESOURCE_ENERGY))
|
||||||
|
target = creep.room.storage;
|
||||||
|
if(!target) target = creep.pos.findClosestByRange(FIND_STRUCTURES, {
|
||||||
|
filter: (st)=>{
|
||||||
|
return st.structureType == STRUCTURE_CONTAINER && st.store.getUsedCapacity(RESOURCE_ENERGY) > creep.store.getFreeCapacity(RESOURCE_ENERGY);
|
||||||
|
}});
|
||||||
|
|
||||||
|
if(!target) target = creep.pos.findClosestByRange(FIND_DROPPED_RESOURCES, {
|
||||||
|
filter: (r)=>{
|
||||||
|
return r.resourceType == RESOURCE_ENERGY && r.amount > creep.store.getFreeCapacity(RESOURCE_ENERGY);
|
||||||
|
}});
|
||||||
|
if(!target) target = creep.pos.findClosestByRange(FIND_RUINS, {
|
||||||
|
filter: (r)=>{
|
||||||
|
return r.store.getUsedCapacity(RESOURCE_ENERGY) > creep.store.getFreeCapacity(RESOURCE_ENERGY);
|
||||||
|
}});
|
||||||
|
if(!target) target = creep.pos.findClosestByRange(FIND_TOMBSTONES, {
|
||||||
|
filter: (t)=>{
|
||||||
|
return t.store.getUsedCapacity(RESOURCE_ENERGY) > creep.store.getFreeCapacity(RESOURCE_ENERGY);
|
||||||
|
}});
|
||||||
|
|
||||||
|
if(target) creep.memory.target = target.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
function findBuildOrRepairTarget(creep) {
|
||||||
|
var target;
|
||||||
|
switch (creep.memory.counter%2) {
|
||||||
|
case 0: target = creep.pos.findClosestByRange(FIND_MY_CONSTRUCTION_SITES); break;
|
||||||
|
case 1: target = creep.pos.findClosestByRange(FIND_STRUCTURES, {
|
||||||
|
filter:(s)=> {
|
||||||
|
return s.hits < s.hitsMax ||
|
||||||
|
((s.structureType === STRUCTURE_WALL || s.structureType === STRUCTURE_RAMPART) && s.hits < 100000 );
|
||||||
|
}});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(target) creep.memory.target = target.id;
|
||||||
|
creep.memory.counter++;
|
||||||
|
}
|
||||||
81
Screeps/JobCleaner.js
Normal file
81
Screeps/JobCleaner.js
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
module.exports = {
|
||||||
|
begin(creep){
|
||||||
|
if(!creep.memory.collecting) creep.memory.collecting = false;
|
||||||
|
},
|
||||||
|
|
||||||
|
tick(creep){
|
||||||
|
if(creep.memory.collecting) {
|
||||||
|
searchForLooseEnergy(creep);
|
||||||
|
energyFullCheck(creep);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
DepositEnergy(creep);
|
||||||
|
energyEmptyCheck(creep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function searchForLooseEnergy(creep){
|
||||||
|
if(!creep.memory.target) findLooseEnergyTarget(creep);
|
||||||
|
const target = Game.getObjectById(creep.memory.target);
|
||||||
|
if(!target) creep.memory.target = undefined;
|
||||||
|
if(creep.pos.isNearTo(target)) {
|
||||||
|
if(!target.store) creep.pickup(target);
|
||||||
|
else creep.withdraw(target, RESOURCE_ENERGY);
|
||||||
|
}
|
||||||
|
else creep.moveTo(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
function energyFullCheck(creep){
|
||||||
|
if(!creep.store.getFreeCapacity(RESOURCE_ENERGY)) {
|
||||||
|
creep.memory.collecting = false;
|
||||||
|
creep.memory.target = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function DepositEnergy(creep){
|
||||||
|
if(!creep.memory.target) findDepositTarget(creep);
|
||||||
|
if(!creep.memory.target) return;
|
||||||
|
const target = Game.getObjectById(creep.memory.target);
|
||||||
|
if(target.store.getFreeCapacity(RESOURCE_ENERGY)==0) { creep.memory.target = undefined; creep.memory.counter++; }
|
||||||
|
if(creep.pos.isNearTo(target)) creep.transfer(target, RESOURCE_ENERGY);
|
||||||
|
else creep.moveTo(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
function energyEmptyCheck(creep){
|
||||||
|
if(!creep.store.getUsedCapacity(RESOURCE_ENERGY)) {
|
||||||
|
creep.memory.collecting = true;
|
||||||
|
creep.memory.target = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function findLooseEnergyTarget(creep){
|
||||||
|
var target = creep.pos.findClosestByRange(FIND_DROPPED_RESOURCES, {
|
||||||
|
filter: (r)=>{
|
||||||
|
return r.resourceType == RESOURCE_ENERGY && r.amount > creep.store.getFreeCapacity(RESOURCE_ENERGY);
|
||||||
|
}});
|
||||||
|
if(!target) target = creep.pos.findClosestByRange(FIND_RUINS, {
|
||||||
|
filter: (r)=>{
|
||||||
|
return r.store.getUsedCapacity(RESOURCE_ENERGY) > creep.store.getFreeCapacity(RESOURCE_ENERGY);
|
||||||
|
}});
|
||||||
|
if(!target) target = creep.pos.findClosestByRange(FIND_TOMBSTONES, {
|
||||||
|
filter: (t)=>{
|
||||||
|
return t.store.getUsedCapacity(RESOURCE_ENERGY) > creep.store.getFreeCapacity(RESOURCE_ENERGY);
|
||||||
|
}});
|
||||||
|
if(target) creep.memory.target = target.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
function findDepositTarget(creep) {
|
||||||
|
var target = creep.pos.findClosestByRange(FIND_STRUCTURES, {
|
||||||
|
filter:(s)=> {
|
||||||
|
return s.structureType == STRUCTURE_CONTAINER
|
||||||
|
&& s.store.getFreeCapacity(RESOURCE_ENERGY) > 1000;
|
||||||
|
}});
|
||||||
|
if(!target) target = creep.pos.findClosestByRange(FIND_MY_STRUCTURES, {
|
||||||
|
filter:(s)=> {
|
||||||
|
return (s.structureType == STRUCTURE_SPAWN || s.structureType == STRUCTURE_EXTENSION)
|
||||||
|
&& s.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
|
||||||
|
}});
|
||||||
|
if(!target) target = creep.room.storage;
|
||||||
|
if(target) creep.memory.target = target.id;
|
||||||
|
}
|
||||||
25
Screeps/JobMiner.js
Normal file
25
Screeps/JobMiner.js
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
module.exports = {
|
||||||
|
begin(creep){},
|
||||||
|
|
||||||
|
tick(creep){
|
||||||
|
const source = Game.getObjectById(creep.memory.job.source);
|
||||||
|
if(!creep.pos.isNearTo(source)) { creep.moveTo(source); return; }
|
||||||
|
if(!(Game.time%100)) scanForContainer(creep, source);
|
||||||
|
moveToContainer(creep);
|
||||||
|
creep.harvest(source);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function moveToContainer(creep){
|
||||||
|
const container = Game.getObjectById(creep.memory.container);
|
||||||
|
if(!container) return;
|
||||||
|
if(container.pos.x === creep.pos.x && container.pos.y === creep.pos.y) return;
|
||||||
|
creep.moveTo(container);
|
||||||
|
}
|
||||||
|
|
||||||
|
function scanForContainer(creep, source){
|
||||||
|
const container = source.pos.findInRange(FIND_STRUCTURES, 1, {
|
||||||
|
filter:{structureType:STRUCTURE_CONTAINER}
|
||||||
|
})[0];
|
||||||
|
if(container) creep.memory.container = container.id;
|
||||||
|
}
|
||||||
90
Screeps/JobSupplier.js
Normal file
90
Screeps/JobSupplier.js
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
module.exports = {
|
||||||
|
begin(creep){
|
||||||
|
if(!creep.memory.collecting) creep.memory.collecting = false;
|
||||||
|
},
|
||||||
|
|
||||||
|
tick(creep){
|
||||||
|
if(creep.memory.collecting) {
|
||||||
|
withdrawEnergy(creep);
|
||||||
|
energyFullCheck(creep);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
transferEnergy(creep);
|
||||||
|
energyEmptyCheck(creep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function withdrawEnergy(creep){
|
||||||
|
if(!creep.memory.target) findWithdrawTarget(creep);
|
||||||
|
const target = Game.getObjectById(creep.memory.target);
|
||||||
|
if(!target) creep.memory.target = undefined;
|
||||||
|
if(creep.pos.isNearTo(target)) {
|
||||||
|
if(!target.store) creep.pickup(target);
|
||||||
|
else creep.withdraw(target, RESOURCE_ENERGY);
|
||||||
|
}
|
||||||
|
else creep.moveTo(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
function energyFullCheck(creep){
|
||||||
|
if(!creep.store.getFreeCapacity(RESOURCE_ENERGY)) {
|
||||||
|
creep.memory.collecting = false;
|
||||||
|
creep.memory.target = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function transferEnergy(creep){
|
||||||
|
if(!creep.memory.target) findTransferTarget(creep);
|
||||||
|
if(!creep.memory.target) return;
|
||||||
|
const target = Game.getObjectById(creep.memory.target);
|
||||||
|
if(target.store.getFreeCapacity(RESOURCE_ENERGY)==0) { creep.memory.target = undefined; creep.memory.counter++; }
|
||||||
|
if(creep.pos.isNearTo(target)) creep.transfer(target, RESOURCE_ENERGY);
|
||||||
|
else creep.moveTo(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
function energyEmptyCheck(creep){
|
||||||
|
if(!creep.store.getUsedCapacity(RESOURCE_ENERGY)) {
|
||||||
|
creep.memory.collecting = true;
|
||||||
|
creep.memory.target = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function findWithdrawTarget(creep){
|
||||||
|
var target = creep.pos.findClosestByRange(FIND_DROPPED_RESOURCES, {
|
||||||
|
filter: (r)=>{
|
||||||
|
return r.resourceType == RESOURCE_ENERGY && r.amount > creep.store.getFreeCapacity(RESOURCE_ENERGY);
|
||||||
|
}});
|
||||||
|
if(!target) target = creep.pos.findClosestByRange(FIND_RUINS, {
|
||||||
|
filter: (r)=>{
|
||||||
|
return r.store.getUsedCapacity(RESOURCE_ENERGY) > creep.store.getFreeCapacity(RESOURCE_ENERGY);
|
||||||
|
}});
|
||||||
|
if(!target) target = creep.pos.findClosestByRange(FIND_TOMBSTONES, {
|
||||||
|
filter: (t)=>{
|
||||||
|
return t.store.getUsedCapacity(RESOURCE_ENERGY) > creep.store.getFreeCapacity(RESOURCE_ENERGY);
|
||||||
|
}});
|
||||||
|
if(!target) target = creep.pos.findClosestByRange(FIND_STRUCTURES, {
|
||||||
|
filter: (st)=>{
|
||||||
|
return st.structureType == STRUCTURE_CONTAINER && st.store.getUsedCapacity(RESOURCE_ENERGY) > 1000;
|
||||||
|
}});
|
||||||
|
if(target) creep.memory.target = target.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
function findTransferTarget(creep) {
|
||||||
|
var target = creep.pos.findClosestByRange(FIND_MY_STRUCTURES, {
|
||||||
|
filter:(s)=> {
|
||||||
|
return (s.structureType == STRUCTURE_EXTENSION)
|
||||||
|
&& s.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
|
||||||
|
}});
|
||||||
|
if(!target) target = creep.pos.findClosestByRange(FIND_MY_STRUCTURES, {
|
||||||
|
filter:(s)=> {
|
||||||
|
return (s.structureType == STRUCTURE_SPAWN)
|
||||||
|
&& s.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
|
||||||
|
}});
|
||||||
|
if(!target) target = creep.pos.findClosestByRange(FIND_STRUCTURES, {
|
||||||
|
filter:(s)=> {
|
||||||
|
return s.structureType == STRUCTURE_CONTAINER
|
||||||
|
&& s.store.getFreeCapacity(RESOURCE_ENERGY) > 1000;
|
||||||
|
}})
|
||||||
|
if(!target) target = creep.room.storage;
|
||||||
|
if(target) creep.memory.target = target.id;
|
||||||
|
}
|
||||||
28
Screeps/JobUpgrader.js
Normal file
28
Screeps/JobUpgrader.js
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
module.exports = {
|
||||||
|
begin(creep){
|
||||||
|
},
|
||||||
|
|
||||||
|
tick(creep){
|
||||||
|
if(!creep.pos.isNearTo(creep.room.controller)) { creep.moveTo(creep.room.controller); return; }
|
||||||
|
if(creep.store.getUsedCapacity(RESOURCE_ENERGY) == 0) { getOrWaitForEnergy(creep); return; }
|
||||||
|
creep.upgradeController(creep.room.controller);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getOrWaitForEnergy(creep){
|
||||||
|
if(!creep.memory.container) scanForContainer(creep);
|
||||||
|
if(creep.memory.container) getEnergyFromContainer(creep);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getEnergyFromContainer(creep){
|
||||||
|
const container = Game.getObjectById(creep.memory.container);
|
||||||
|
if(creep.withdraw(container, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) creep.moveTo(container);
|
||||||
|
}
|
||||||
|
|
||||||
|
// duplicate see miner
|
||||||
|
function scanForContainer(creep){
|
||||||
|
const container = creep.room.controller.pos.findInRange(FIND_STRUCTURES, 1, {
|
||||||
|
filter:{structureType:STRUCTURE_CONTAINER}
|
||||||
|
})[0];
|
||||||
|
if(container) creep.memory.container = container.id;
|
||||||
|
}
|
||||||
7
Screeps/Math.js
Normal file
7
Screeps/Math.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
module.exports = {
|
||||||
|
clamp: function(v, min, max){
|
||||||
|
if(v<min) v = min;
|
||||||
|
else if (max < v) v = max;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
}
|
||||||
44
Screeps/RoomClass.js
Normal file
44
Screeps/RoomClass.js
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
module.exports = {
|
||||||
|
setup: function () { Room.prototype = _Room.prototype; }
|
||||||
|
}
|
||||||
|
|
||||||
|
class _Room extends Room {
|
||||||
|
begin(){
|
||||||
|
roomLayoutScan(this);
|
||||||
|
jobScan(this);
|
||||||
|
vacancyScan(this);
|
||||||
|
this.memory.init = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
tick(){
|
||||||
|
if(!this.memory.init) this.begin();
|
||||||
|
if(!(Game.time%100)) {
|
||||||
|
vacancyScan(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function roomLayoutScan(room){
|
||||||
|
if(!room.memory.layout) room.memory.layout = {};
|
||||||
|
if(!room.memory.layout.sources) room.memory.layout.sources = room.find(FIND_SOURCES).map(s=>s.id);
|
||||||
|
room.memory.layout.containers = room.find(FIND_MY_STRUCTURES, {filter:{structureType:STRUCTURE_CONTAINER}}).map(c=>c.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
function jobScan(room){
|
||||||
|
if(!room.memory.jobs) room.memory.jobs = [{role:Role.UPGRADER}]
|
||||||
|
.concat(Array(4).fill({role: Role.BUILDER}))
|
||||||
|
.concat(Array(2).fill({role: Role.SUPPLIER}))
|
||||||
|
.concat(room.memory.layout.sources.map(s=> {return {role: Role.MINER, source: s}}));
|
||||||
|
}
|
||||||
|
|
||||||
|
function vacancyScan(room){
|
||||||
|
const activeJobs = room.find(FIND_MY_CREEPS).map(creep=>creep.memory.job);
|
||||||
|
const jobs = room.memory.jobs.filter((j)=>{
|
||||||
|
const index = activeJobs.findIndex(aj=> _.isEqual(aj,j));
|
||||||
|
if(index < 0) return true;
|
||||||
|
activeJobs.splice(index,1);
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
room.memory.vacancies = jobs;
|
||||||
|
}
|
||||||
|
|
||||||
21
Screeps/Structure.js
Normal file
21
Screeps/Structure.js
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
const spawnClass = require("StructureSpawnClass");
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
setup() {
|
||||||
|
spawnClass.setup();
|
||||||
|
},
|
||||||
|
|
||||||
|
begin(structure){
|
||||||
|
switch (structure.structureType) {
|
||||||
|
case STRUCTURE_SPAWN: structure.begin(); break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
tick(structure){
|
||||||
|
switch (structure.structureType) {
|
||||||
|
case STRUCTURE_SPAWN: structure.tick(); break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
64
Screeps/StructureSpawnClass.js
Normal file
64
Screeps/StructureSpawnClass.js
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
module.exports = {
|
||||||
|
setup: function () { StructureSpawn.prototype = _StructureSpawn.prototype; }
|
||||||
|
}
|
||||||
|
|
||||||
|
class _StructureSpawn extends StructureSpawn {
|
||||||
|
begin(){
|
||||||
|
if (!this.memory.creepCounter) this.memory.creepCounter = 0;
|
||||||
|
this.memory.init = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
tick(){
|
||||||
|
if(!this.memory.init) this.begin();
|
||||||
|
if(Game.time%100) return;
|
||||||
|
if(this.room.energyAvailable < 300) return;
|
||||||
|
const job = this.room.memory.vacancies.pop();
|
||||||
|
if(job){
|
||||||
|
const name = getJobName(job.role);
|
||||||
|
const body = getBodyByJob(job.role, this.room.energyAvailable);
|
||||||
|
if(this.createCreep(job, name, body) != OK) this.room.memory.vacancies.push(job);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
createCreep(job, name, body) {
|
||||||
|
const response = this.spawnCreep(body, name + ": " + this.memory.creepCounter, {
|
||||||
|
memory: { job: job }
|
||||||
|
});
|
||||||
|
if (response == OK) this.memory.creepCounter++;
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getJobName(role){
|
||||||
|
switch (role) {
|
||||||
|
case Role.BUILDER: return "Bob";
|
||||||
|
case Role.MINER: return "minny";
|
||||||
|
case Role.SUPPLIER: return "Sully";
|
||||||
|
case Role.UPGRADER: return "Uppa";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getBodyByJob(role, energyAvailability){
|
||||||
|
const body = [];
|
||||||
|
switch (role) {
|
||||||
|
case Role.BUILDER: body.push(WORK); energyAvailability -= 100; break;
|
||||||
|
case Role.UPGRADER: body.push(CARRY); body.push(MOVE); energyAvailability -= 100; break;
|
||||||
|
case Role.MINER: body.push(MOVE); energyAvailability -= 100; break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var unitEnergyCost = 0;
|
||||||
|
while(unitEnergyCost <= energyAvailability) {
|
||||||
|
switch (role) {
|
||||||
|
case Role.BUILDER: body.push(MOVE); body.push(CARRY); unitEnergyCost = 100; break;
|
||||||
|
case Role.UPGRADER:
|
||||||
|
case Role.MINER:
|
||||||
|
body.push(WORK); unitEnergyCost = 100;
|
||||||
|
if(role === Role.MINER && body.length > 5) energyAvailability = -1;
|
||||||
|
break;
|
||||||
|
case Role.SUPPLIER: body.push(CARRY); body.push(MOVE); unitEnergyCost = 100; break;
|
||||||
|
default: energyAvailability = -1; break;
|
||||||
|
}
|
||||||
|
energyAvailability -= unitEnergyCost;
|
||||||
|
}
|
||||||
|
return body;
|
||||||
|
}
|
||||||
44
Screeps/main.js
Normal file
44
Screeps/main.js
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
const Commands = require("Commands");
|
||||||
|
const CreepClass = require("CreepClass");
|
||||||
|
const RoomClass = require("RoomClass");
|
||||||
|
const Structure = require("Structure");
|
||||||
|
|
||||||
|
module.exports.loop = function () {
|
||||||
|
if(!global.compiled) onRecompile();
|
||||||
|
else if (!global.started) onRestart();
|
||||||
|
else onTick();
|
||||||
|
}
|
||||||
|
|
||||||
|
function onRecompile(){
|
||||||
|
setupClasses();
|
||||||
|
console.log("Script recompiled...");
|
||||||
|
global.compiled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function onRestart(){
|
||||||
|
Object.values(Game.rooms).forEach(room => room.begin());
|
||||||
|
Object.values(Game.creeps).forEach(creep => creep.begin());
|
||||||
|
Object.values(Game.structures).forEach(structure => Structure.begin(structure));
|
||||||
|
global.started = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function onTick(){
|
||||||
|
Object.values(Game.rooms).forEach(room => room.tick());
|
||||||
|
Object.values(Game.creeps).forEach(creep => creep.tick());
|
||||||
|
Object.values(Game.structures).forEach(structure => Structure.tick(structure));
|
||||||
|
if(!(Game.time % 100)) cleanUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function setupClasses(){
|
||||||
|
Commands.setup();
|
||||||
|
CreepClass.setup();
|
||||||
|
RoomClass.setup();
|
||||||
|
Structure.setup();
|
||||||
|
}
|
||||||
|
|
||||||
|
function cleanUp(){
|
||||||
|
Object.keys(Memory.rooms).forEach(roomName => { if(!Game.rooms[roomName]) Memory.rooms[roomName] = undefined; });
|
||||||
|
Object.keys(Memory.creeps).forEach(creepName => { if(!Game.creeps[creepName]) Memory.creeps[creepName] = undefined; });
|
||||||
|
}
|
||||||
|
|
||||||
2
dist/douwco_hivemind_loader.js
vendored
2
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.
@@ -2,9 +2,12 @@
|
|||||||
#define DOUWCO_HIVEMIND_CREEP_HPP
|
#define DOUWCO_HIVEMIND_CREEP_HPP
|
||||||
|
|
||||||
#include <Screeps/Creep.hpp>
|
#include <Screeps/Creep.hpp>
|
||||||
#include <Screeps/ReturnTypes.hpp>
|
|
||||||
#include "Tools/JsonTool.hpp"
|
namespace Screeps{
|
||||||
#include "Tools/PathTool.hpp"
|
class RoomPosition;
|
||||||
|
class RoomObject;
|
||||||
|
class PathStep;
|
||||||
|
}
|
||||||
|
|
||||||
namespace DouwcoHivemind
|
namespace DouwcoHivemind
|
||||||
{
|
{
|
||||||
@@ -13,39 +16,26 @@ namespace DouwcoHivemind
|
|||||||
UNEMPLOYED,
|
UNEMPLOYED,
|
||||||
HARVESTER
|
HARVESTER
|
||||||
};
|
};
|
||||||
|
|
||||||
class Creep
|
class Creep
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CreepRole role;
|
CreepRole role;
|
||||||
std::string target_id;
|
std::string target_id;
|
||||||
std::vector<Screeps::PathStep> path;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Screeps::Creep creep;
|
Screeps::Creep creep;
|
||||||
JSON memory;
|
JSON memory;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Creep(Screeps::Creep crp) : creep(crp),
|
Creep(Screeps::Creep crp);
|
||||||
memory(crp.memory())
|
virtual ~Creep();
|
||||||
{
|
|
||||||
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() {}
|
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();
|
||||||
bool isNearTo(const Screeps::RoomPosition &pos1, const Screeps::RoomPosition &pos2, int dist);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
#ifndef DOUWCO_HIVEMIND_HARVESTER_HPP
|
#ifndef DOUWCO_HIVEMIND_HARVESTER_HPP
|
||||||
#define DOUWCO_HIVEMIND_HARVESTER_HPP
|
#define DOUWCO_HIVEMIND_HARVESTER_HPP
|
||||||
|
|
||||||
#include <Screeps/Creep.hpp>
|
|
||||||
|
|
||||||
#include "Creeps/Creep.hpp"
|
#include "Creeps/Creep.hpp"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
class Creep;
|
||||||
|
}
|
||||||
|
|
||||||
namespace DouwcoHivemind
|
namespace DouwcoHivemind
|
||||||
{
|
{
|
||||||
class HarvesterRole : public Creep
|
class HarvesterRole : public Creep
|
||||||
@@ -14,17 +17,8 @@ namespace DouwcoHivemind
|
|||||||
int taskCounter;
|
int taskCounter;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
HarvesterRole(Screeps::Creep crp) : Creep(crp)
|
HarvesterRole(Screeps::Creep crp);
|
||||||
{
|
~HarvesterRole() override;
|
||||||
harvesting = memory.contains("harvesting") ? static_cast<bool>(memory["harvesting"]) : false;
|
|
||||||
taskCounter = memory.contains("taskCounter") ? static_cast<int>(memory["taskCounter"]) : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
~HarvesterRole() override
|
|
||||||
{
|
|
||||||
memory["harvesting"] = harvesting;
|
|
||||||
memory["taskCounter"] = taskCounter;
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() override;
|
void loop() override;
|
||||||
|
|
||||||
|
|||||||
@@ -2,20 +2,12 @@
|
|||||||
#define DOUWCO_HIVEMIND_ENGINE_HPP
|
#define DOUWCO_HIVEMIND_ENGINE_HPP
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <Screeps/JS.hpp>
|
|
||||||
#include <Screeps/Game.hpp>
|
|
||||||
#include <Screeps/Creep.hpp>
|
|
||||||
|
|
||||||
#include "Creeps/Creep.hpp"
|
#include "Creeps/Creep.hpp"
|
||||||
#include "Creeps/Harvester.hpp"
|
|
||||||
#include "Structures/Structure.hpp"
|
#include "Structures/Structure.hpp"
|
||||||
#include "Structures/Spawn.hpp"
|
|
||||||
|
|
||||||
namespace DouwcoHivemind
|
namespace DouwcoHivemind
|
||||||
{
|
{
|
||||||
class Creep;
|
|
||||||
class Structure;
|
|
||||||
|
|
||||||
class Engine
|
class Engine
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
@@ -23,50 +15,12 @@ namespace DouwcoHivemind
|
|||||||
std::vector<std::unique_ptr<Structure>> structures;
|
std::vector<std::unique_ptr<Structure>> structures;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Engine()
|
Engine();
|
||||||
{
|
void loop();
|
||||||
ReadOutCreeps();
|
|
||||||
ReadOutStructures();
|
|
||||||
}
|
|
||||||
~Engine() {}
|
|
||||||
|
|
||||||
void loop()
|
|
||||||
{
|
|
||||||
for (auto &creep : creeps)
|
|
||||||
creep->loop();
|
|
||||||
|
|
||||||
for (auto &structure : structures)
|
|
||||||
structure->loop();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void ReadOutCreeps()
|
void ReadOutCreeps();
|
||||||
{
|
void ReadOutStructures();
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
15
douwco_hivemind/include/Rooms/Room.hpp
Normal file
15
douwco_hivemind/include/Rooms/Room.hpp
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#ifndef DOUWCO_HIVEMIND_ROOMS_HPP
|
||||||
|
#define DOUWCO_HIVEMIND_ROOMS_HPP
|
||||||
|
|
||||||
|
|
||||||
|
namespace DouwcoHivemind
|
||||||
|
{
|
||||||
|
class Room
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Room();
|
||||||
|
void loop();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // DOUWCO_HIVEMIND_ROOMS_HPP
|
||||||
19
douwco_hivemind/include/Tools/MeasureTool.hpp
Normal file
19
douwco_hivemind/include/Tools/MeasureTool.hpp
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#ifndef DOUWCO_HIVEMIND_MEASURE_TOOL_HPP
|
||||||
|
#define DOUWCO_HIVEMIND_MEASURE_TOOL_HPP
|
||||||
|
|
||||||
|
#include <Screeps/RoomPosition.hpp>
|
||||||
|
|
||||||
|
namespace DouwcoHivemind
|
||||||
|
{
|
||||||
|
static 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();
|
||||||
|
}
|
||||||
|
} // namespace Screeps
|
||||||
|
|
||||||
|
#endif // DOUWCO_HIVEMIND_MEASURE_TOOL_HPP
|
||||||
@@ -6,7 +6,6 @@
|
|||||||
|
|
||||||
namespace DouwcoHivemind
|
namespace DouwcoHivemind
|
||||||
{
|
{
|
||||||
|
|
||||||
static std::vector<int> flattenPathSteps(const std::vector<Screeps::PathStep> &pathSteps)
|
static std::vector<int> flattenPathSteps(const std::vector<Screeps::PathStep> &pathSteps)
|
||||||
{
|
{
|
||||||
std::vector<int> flattened;
|
std::vector<int> flattened;
|
||||||
|
|||||||
139
douwco_hivemind/src/Creep.cpp
Normal file
139
douwco_hivemind/src/Creep.cpp
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
#include <Screeps/Game.hpp>
|
||||||
|
#include <Screeps/Room.hpp>
|
||||||
|
#include <Screeps/RoomPosition.hpp>
|
||||||
|
#include <Screeps/ReturnTypes.hpp>
|
||||||
|
|
||||||
|
#include "Creeps/Creep.hpp"
|
||||||
|
|
||||||
|
#include "Tools/JsonTool.hpp"
|
||||||
|
#include "Tools/PathTool.hpp"
|
||||||
|
#include "Tools/MeasureTool.hpp"
|
||||||
|
|
||||||
|
DouwcoHivemind::Creep::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();
|
||||||
|
}
|
||||||
|
|
||||||
|
DouwcoHivemind::Creep::~Creep()
|
||||||
|
{
|
||||||
|
memory["target_id"] = target_id;
|
||||||
|
creep.setMemory(memory);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DouwcoHivemind::Creep::moveToTarget(int dist)
|
||||||
|
{
|
||||||
|
// Is move required?
|
||||||
|
auto target = getRoomObjectTarget();
|
||||||
|
if (isNearTo(target->pos(), dist))
|
||||||
|
{
|
||||||
|
memory.erase("path");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Is wating?
|
||||||
|
if (memory.contains("wait") && memory["wait"] > 0)
|
||||||
|
{
|
||||||
|
memory["wait"] = memory["wait"].get<int>() - 1;
|
||||||
|
creep.say("Waiting..");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Is there a path to walk?
|
||||||
|
if (!memory.contains("path") || memory["path"].empty())
|
||||||
|
{
|
||||||
|
creep.say("Searching route!");
|
||||||
|
auto target_pos = target->pos();
|
||||||
|
auto path = creep.room().findPath(creep.pos(), target_pos);
|
||||||
|
auto last = path.back();
|
||||||
|
if (last.x - target_pos.x() > dist || last.y - target_pos.y() > dist)
|
||||||
|
{
|
||||||
|
creep.say("No possible path");
|
||||||
|
memory["wait"] = rand() % 20;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
memory["path"] = vectorToJson(flattenPathSteps(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
// JS::console.log(std::string("creep pos: [") +
|
||||||
|
// std::to_string(creep.pos().x()) +
|
||||||
|
// std::string(",") +
|
||||||
|
// std::to_string(creep.pos().y()) +
|
||||||
|
// std::string("]"));
|
||||||
|
|
||||||
|
// Get step from memory
|
||||||
|
int pathStepData[5] = {0};
|
||||||
|
if (memory["path"].size() > 5)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
pathStepData[i] = memory["path"][i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
memory.erase("path");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Is the move of last tick executed?
|
||||||
|
int x = creep.pos().x();
|
||||||
|
int y = creep.pos().y();
|
||||||
|
|
||||||
|
auto step = Screeps::PathStep(pathStepData[0], pathStepData[1], pathStepData[2], pathStepData[3], pathStepData[4]);
|
||||||
|
|
||||||
|
if (memory.contains("last_pos"))
|
||||||
|
{
|
||||||
|
int last_x = memory["last_pos"]["x"];
|
||||||
|
int last_y = memory["last_pos"]["y"];
|
||||||
|
memory.erase("last_pos");
|
||||||
|
if (x == last_x && y == last_y)
|
||||||
|
{
|
||||||
|
creep.say("I'm stuck!");
|
||||||
|
memory["wait"] = rand() % 5;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Is the creep on the place intended by the path?
|
||||||
|
if (!(x == step.x - step.dx && y == step.y - step.dy))
|
||||||
|
{
|
||||||
|
creep.say("I'm lost!");
|
||||||
|
memory["wait"] = rand() % 5;
|
||||||
|
memory.erase("path");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lets move forward
|
||||||
|
int resp = creep.move(step.direction);
|
||||||
|
if (resp == Screeps::OK)
|
||||||
|
{
|
||||||
|
memory["last_pos"]["x"] = x;
|
||||||
|
memory["last_pos"]["y"] = y;
|
||||||
|
|
||||||
|
for (int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
memory["path"].erase(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 DouwcoHivemind::isNearTo(creep.pos(), pos, dist);
|
||||||
|
}
|
||||||
48
douwco_hivemind/src/Engine.cpp
Normal file
48
douwco_hivemind/src/Engine.cpp
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
#include <Screeps/Game.hpp>
|
||||||
|
|
||||||
|
#include "Engine.hpp"
|
||||||
|
|
||||||
|
#include "Creeps/Harvester.hpp"
|
||||||
|
#include "Structures/Spawn.hpp"
|
||||||
|
|
||||||
|
DouwcoHivemind::Engine::Engine()
|
||||||
|
{
|
||||||
|
ReadOutCreeps();
|
||||||
|
ReadOutStructures();
|
||||||
|
}
|
||||||
|
|
||||||
|
void DouwcoHivemind::Engine::loop()
|
||||||
|
{
|
||||||
|
for (auto &creep : creeps)
|
||||||
|
creep->loop();
|
||||||
|
|
||||||
|
for (auto &structure : structures)
|
||||||
|
structure->loop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void DouwcoHivemind::Engine::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:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void DouwcoHivemind::Engine::ReadOutStructures()
|
||||||
|
{
|
||||||
|
auto spawns = Screeps::Game.spawns();
|
||||||
|
for (auto &spawn : spawns)
|
||||||
|
{
|
||||||
|
structures.push_back(std::make_unique<Spawn>(spawn.second));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,6 +18,18 @@
|
|||||||
|
|
||||||
#include "Creeps/Harvester.hpp"
|
#include "Creeps/Harvester.hpp"
|
||||||
|
|
||||||
|
DouwcoHivemind::HarvesterRole::HarvesterRole(Screeps::Creep crp) : Creep(crp)
|
||||||
|
{
|
||||||
|
harvesting = memory.contains("harvesting") ? static_cast<bool>(memory["harvesting"]) : false;
|
||||||
|
taskCounter = memory.contains("taskCounter") ? static_cast<int>(memory["taskCounter"]) : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
DouwcoHivemind::HarvesterRole::~HarvesterRole()
|
||||||
|
{
|
||||||
|
memory["harvesting"] = harvesting;
|
||||||
|
memory["taskCounter"] = taskCounter;
|
||||||
|
}
|
||||||
|
|
||||||
void DouwcoHivemind::HarvesterRole::loop()
|
void DouwcoHivemind::HarvesterRole::loop()
|
||||||
{
|
{
|
||||||
if (harvesting)
|
if (harvesting)
|
||||||
@@ -1,9 +1,5 @@
|
|||||||
#include <nlohmann/json.hpp>
|
|
||||||
#include <Screeps/JS.hpp>
|
|
||||||
#include <Screeps/Game.hpp>
|
#include <Screeps/Game.hpp>
|
||||||
#include <Screeps/Room.hpp>
|
#include <Screeps/Room.hpp>
|
||||||
#include <Screeps/StructureSpawn.hpp>
|
|
||||||
#include <emscripten.h>
|
|
||||||
|
|
||||||
#include "Creeps/Creep.hpp"
|
#include "Creeps/Creep.hpp"
|
||||||
#include "Structures/Spawn.hpp"
|
#include "Structures/Spawn.hpp"
|
||||||
@@ -13,16 +9,14 @@ void DouwcoHivemind::Spawn::loop()
|
|||||||
int creepcount = spawn.room().find(Screeps::FIND_MY_CREEPS).size();
|
int creepcount = spawn.room().find(Screeps::FIND_MY_CREEPS).size();
|
||||||
if (creepcount > 10)
|
if (creepcount > 10)
|
||||||
{
|
{
|
||||||
EM_ASM({ console.log('To much creeps in this room'); });
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
EM_ASM({ console.log('Creating a harvester'); });
|
|
||||||
|
|
||||||
JSON opts;
|
JSON opts;
|
||||||
opts["memory"]["role"] = CreepRole::HARVESTER;
|
opts["memory"]["role"] = CreepRole::HARVESTER;
|
||||||
|
|
||||||
int resp = spawn.spawnCreep(
|
int resp = spawn.spawnCreep(
|
||||||
{"work", "carry", "move"},
|
{"work", "work", "carry", "move"},
|
||||||
"harvester" + std::to_string(Screeps::Game.time()),
|
"harvester" + std::to_string(Screeps::Game.time()),
|
||||||
opts);
|
opts);
|
||||||
}
|
}
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
#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();
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user