From e1e40f02e0ea84bb69979989df90192ecb40ff2f Mon Sep 17 00:00:00 2001 From: Brulijam Date: Sun, 26 Nov 2023 19:16:25 +0100 Subject: [PATCH] Finished fourth tutorial --- screeps.com/tutorial-4/main.js | 41 ++++++++++++++++++++++++ screeps.com/tutorial-4/role.harvester.js | 27 ++++++++++++++++ screeps.com/tutorial-4/role.upgrader.js | 29 +++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 screeps.com/tutorial-4/main.js create mode 100644 screeps.com/tutorial-4/role.harvester.js create mode 100644 screeps.com/tutorial-4/role.upgrader.js diff --git a/screeps.com/tutorial-4/main.js b/screeps.com/tutorial-4/main.js new file mode 100644 index 0000000..328af35 --- /dev/null +++ b/screeps.com/tutorial-4/main.js @@ -0,0 +1,41 @@ +var roleHarvester = require('role.harvester'); +var roleUpgrader = require('role.upgrader'); + +module.exports.loop = function () { + + for(var name in Memory.creeps) { + if(!Game.creeps[name]) { + delete Memory.creeps[name]; + console.log('Clearing non-existing creep memory:', name); + } + } + + var harvesters = _.filter(Game.creeps, (creep) => creep.memory.role == 'harvester'); + console.log('Harvesters: ' + harvesters.length); + + if(harvesters.length < 2) { + var newName = 'Harvester' + Game.time; + console.log('Spawning new harvester: ' + newName); + Game.spawns['Spawn1'].spawnCreep([WORK,CARRY,MOVE], newName, + {memory: {role: 'harvester'}}); + } + + if(Game.spawns['Spawn1'].spawning) { + var spawningCreep = Game.creeps[Game.spawns['Spawn1'].spawning.name]; + Game.spawns['Spawn1'].room.visual.text( + '🛠️' + spawningCreep.memory.role, + Game.spawns['Spawn1'].pos.x + 1, + Game.spawns['Spawn1'].pos.y, + {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); + } + } +} \ No newline at end of file diff --git a/screeps.com/tutorial-4/role.harvester.js b/screeps.com/tutorial-4/role.harvester.js new file mode 100644 index 0000000..95e28a6 --- /dev/null +++ b/screeps.com/tutorial-4/role.harvester.js @@ -0,0 +1,27 @@ +var roleHarvester = { + + /** @param {Creep} creep **/ + run: function(creep) { + if(creep.store.getFreeCapacity() > 0) { + var sources = creep.room.find(FIND_SOURCES); + if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { + creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}}); + } + } + else { + var targets = creep.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN) && + structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0; + } + }); + if(targets.length > 0) { + if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}}); + } + } + } + } +}; + +module.exports = roleHarvester; \ No newline at end of file diff --git a/screeps.com/tutorial-4/role.upgrader.js b/screeps.com/tutorial-4/role.upgrader.js new file mode 100644 index 0000000..f49f951 --- /dev/null +++ b/screeps.com/tutorial-4/role.upgrader.js @@ -0,0 +1,29 @@ +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 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; \ No newline at end of file