initial commit

This commit is contained in:
Frieder Schlesier 2022-07-16 11:46:43 +02:00
commit c791da0653
6 changed files with 244 additions and 0 deletions

29
README.org Normal file
View File

@ -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

76
main.js Normal file
View File

@ -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);
}
}
}

43
role.builder.js Normal file
View File

@ -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;

36
role.harvester.js Normal file
View File

@ -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;

42
role.upgrader.js Normal file
View File

@ -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;

18
strategy.creepSize.js Normal file
View File

@ -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;