From c791da06538eeea7855455e113808e3d52653480 Mon Sep 17 00:00:00 2001 From: Frieder Schlesier Date: Sat, 16 Jul 2022 11:46:43 +0200 Subject: [PATCH] initial commit --- README.org | 29 +++++++++++++++++ main.js | 76 +++++++++++++++++++++++++++++++++++++++++++ role.builder.js | 43 ++++++++++++++++++++++++ role.harvester.js | 36 ++++++++++++++++++++ role.upgrader.js | 42 ++++++++++++++++++++++++ strategy.creepSize.js | 18 ++++++++++ 6 files changed, 244 insertions(+) create mode 100644 README.org create mode 100644 main.js create mode 100644 role.builder.js create mode 100644 role.harvester.js create mode 100644 role.upgrader.js create mode 100644 strategy.creepSize.js diff --git a/README.org b/README.org new file mode 100644 index 0000000..4533bcd --- /dev/null +++ b/README.org @@ -0,0 +1,29 @@ +* Screeps + +(scripted creeps). Online programming game. Simulate a growing colony. Manage resource harvesting and building with JavaScript Code. + +Single Player Sandbox: https://screeps.com/a/#!/sim/survival + +- reddit: https://www.reddit.com/r/screeps/ +- + + +** manage code + +*TODO* + +- https://docs.screeps.com/commit.html +- API: https://docs.screeps.com/api/ +- Creep Cost and Build planner https://screeps.admon.dev/creep-designer +- find support for alternative programming languages: https://docs.screeps.com/third-party.html + +** private server and mods + +- start here: https://www.reddit.com/r/screeps/comments/orvw47/no_private_server_without_steam/ +- https://docs.screeps.com/contributed/ps_ubuntu.html +- simple client: https://github.com/ricochet1k/screeps-client (PIXI.js) +- 3D client: https://github.com/thmsndk/Screeps3D + - https://github.com/ScreepsMods/screepsmod-auth for user/password auth + - https://github.com/ScreepsMods/screepsmod-admin-utils +- ? modern approach to run in docker https://gitlab.demannu.com/demannu/screeps_docker/-/tree/master/config + diff --git a/main.js b/main.js new file mode 100644 index 0000000..7097b23 --- /dev/null +++ b/main.js @@ -0,0 +1,76 @@ +var roleHarvester = require('role.harvester'); +var roleUpgrader = require('role.upgrader'); +var roleBuilder = require('role.builder'); +var strategyCreepSize = require('strategy.creepSize'); + +module.exports.loop = function () { + + var tower = Game.getObjectById('61dc669250ab766c4128b6a4'); + if(tower) { + var closestDamagedStructure = tower.pos.findClosestByRange(FIND_STRUCTURES, { + filter: (structure) => structure.hits < structure.hitsMax + }); + if(closestDamagedStructure) { + tower.repair(closestDamagedStructure); + } + + var closestHostile = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS); + if(closestHostile) { + tower.attack(closestHostile); + } + } + for(var name in Memory.creeps) { + if(!Game.creeps[name]) { + delete Memory.creeps[name]; + console.log('Clearing non-existing creep memory:', name); + } + } + + var spawn = Game.spawns['Spawn1'] + var harvesters = _.filter(Game.creeps, (creep) => creep.memory.role == 'harvester'); + var builders = _.filter(Game.creeps, (creep) => creep.memory.role == 'builder'); + var upgraders = _.filter(Game.creeps, (creep) => creep.memory.role == 'upgrader'); + if (Game.time % 20 === 0) { + console.log(Game.time + 'tick: H:: ' + harvesters.length + " B: " + builders.length + " U: " + upgraders.length + " with energy: " + spawn.room.energyCapacityAvailable); + } + + var newSize = strategyCreepSize.choose(spawn.room.energyCapacityAvailable); + var newRole = undefined; + if(harvesters.length < 4) { + var newCreep = 'Harvester' + Game.time; + newRole = 'harvester' + } else if(upgraders.length < 3) { + var newCreep = 'Upgrader ' + Game.time; + newRole = 'upgrader' + } else if(builders.length < 3) { + var newCreep = 'Builder ' + Game.time; + newRole = 'builder' + //newSize = [WORK, WORK, MOVE, CARRY]; + } + if (newRole !== undefined) { + console.log('Spawning new : ' + newCreep + ' with size: ' + newSize.length); + spawn.spawnCreep(newSize, newCreep, {memory: {role: newRole}}) + } + + if(spawn.spawning) { + var spawningCreep = Game.creeps[spawn.spawning.name]; + spawn.room.visual.text( + '🛠️' + spawningCreep.memory.role, + spawn.pos.x - 1, + spawn.pos.y + 1, + {align: 'left', opacity: 0.8}); + } + + for(var name in Game.creeps) { + var creep = Game.creeps[name]; + if(creep.memory.role == 'harvester') { + roleHarvester.run(creep); + } + if(creep.memory.role == 'upgrader') { + roleUpgrader.run(creep); + } + if(creep.memory.role == 'builder') { + roleBuilder.run(creep); + } + } +} diff --git a/role.builder.js b/role.builder.js new file mode 100644 index 0000000..46e1279 --- /dev/null +++ b/role.builder.js @@ -0,0 +1,43 @@ +var roleBuilder = { + + /** @param {Creep} creep **/ + run: function(creep) { + if(creep.memory.building && creep.store[RESOURCE_ENERGY] == 0) { + creep.memory.building = false; + creep.say('🔄 harvest'); + } + if(!creep.memory.building && creep.store.getFreeCapacity() == 0) { + creep.memory.building = true; + creep.say('🚧 build'); + } + + if(creep.memory.building) { + var targets = creep.room.find(FIND_CONSTRUCTION_SITES); + if(targets.length) { + var nearestBuild = creep.pos.findClosestByRange(targets) + if(creep.build(nearestBuild) == ERR_NOT_IN_RANGE) { + creep.moveTo(nearestBuild, {visualizePathStyle: {stroke: '#ffffff'}}); + } + } else { + const repairs = creep.room.find(FIND_STRUCTURES, { + filter:(struc)=>{ + return(struc.hits < (struc.hitsMax*0.75)) + } + }); + var nearestRepair = creep.pos.findClosestByRange(repairs); + if(creep.repair(nearestRepair) == ERR_NOT_IN_RANGE) { + creep.moveTo(nearestRepair, {visualizePathStyle: {stroke: '#ffffff'}}); + } + } + } else { + var sources = creep.room.find(FIND_SOURCES); + var nearestSource = creep.pos.findClosestByRange(sources) + var nearestSource = sources[1] + if(creep.harvest(nearestSource) == ERR_NOT_IN_RANGE) { + creep.moveTo(nearestSource, {visualizePathStyle: {stroke: '#ffaa00'}}); + } + } + } +}; + +module.exports = roleBuilder; diff --git a/role.harvester.js b/role.harvester.js new file mode 100644 index 0000000..e878a6e --- /dev/null +++ b/role.harvester.js @@ -0,0 +1,36 @@ +var roleBuilder = require('role.builder'); + +var roleHarvester = { + + /** @param {Creep} creep **/ + run: function(creep) { + if(creep.store.getFreeCapacity() > 0) { + var sources = creep.room.find(FIND_SOURCES); + var t = creep.pos.findClosestByRange(sources) + if(creep.harvest(t) == ERR_NOT_IN_RANGE) { + creep.moveTo(t, {visualizePathStyle: {stroke: '#aaff00'}}); + } + } else { + // TODO spawn should be priotized + var targets = creep.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_EXTENSION || + structure.structureType == STRUCTURE_SPAWN || + structure.structureType == STRUCTURE_CONTAINER || + structure.structureType == STRUCTURE_TOWER) && + structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0; + } + }); + if(targets.length > 0) { + var t = creep.pos.findClosestByRange(targets) + if(creep.transfer(t, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(t, {visualizePathStyle: {stroke: '#ffffff'}}); + } + } else { // no sturctures with free storage + roleBuilder.run(creep); + } + } + } +}; + +module.exports = roleHarvester; diff --git a/role.upgrader.js b/role.upgrader.js new file mode 100644 index 0000000..508e28d --- /dev/null +++ b/role.upgrader.js @@ -0,0 +1,42 @@ +var roleUpgrader = { + + /** @param {Creep} creep **/ + run: function(creep) { + + if(creep.memory.upgrading && creep.store[RESOURCE_ENERGY] == 0) { + creep.memory.upgrading = false; + creep.say('🔄 harvest'); + } + if(!creep.memory.upgrading && creep.store.getFreeCapacity() == 0) { + creep.memory.upgrading = true; + creep.say('⚡ upgrade'); + } + + if(creep.memory.upgrading) { + if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { + creep.moveTo(creep.room.controller, {visualizePathStyle: {stroke: '#ffffff'}}); + } + } + else { + var targets = creep.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_CONTAINER && + structure.store.getUsedCapacity(RESOURCE_ENERGY)) > 0; + } + }); + if(targets.length > 0) { + var t = creep.pos.findClosestByRange(targets) + if(creep.withdraw(t, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(t, {visualizePathStyle: {stroke: '#ffffff'}}); + } + } else { + var sources = creep.room.find(FIND_SOURCES); + if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { + creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}}); + } + } + } + } +}; + +module.exports = roleUpgrader; diff --git a/strategy.creepSize.js b/strategy.creepSize.js new file mode 100644 index 0000000..52ec4f6 --- /dev/null +++ b/strategy.creepSize.js @@ -0,0 +1,18 @@ +var strategyCreepSize = { + + /** @param {energyCapacity} energyCapacity **/ + choose: function(energyCapacity) { + var newSize = []; + if(energyCapacity < 500){ + newSize = [WORK, WORK, MOVE, CARRY]; + } else if (energyCapacity >= 500 && energyCapacity < 700){ + newSize = [WORK, WORK, WORK, MOVE, MOVE, CARRY, CARRY]; + } else { + newSize = [WORK, WORK, WORK, WORK, MOVE, MOVE, MOVE, MOVE, CARRY, CARRY]; + } + //console.log('newCreepSize: with energy: ' + energyCapacity + ' returning ' + newSize.length); + return newSize; + } +}; + +module.exports = strategyCreepSize;