From 714ae6013b90fbc2567e6582efa82a59480c10d7 Mon Sep 17 00:00:00 2001 From: Brulijam Date: Sun, 26 Nov 2023 19:11:05 +0100 Subject: [PATCH] Finished third tutorial --- screeps.com/tutorial-3/main.js | 19 +++++++++++ screeps.com/tutorial-3/role.builder.js | 41 ++++++++++++++++++++++++ screeps.com/tutorial-3/role.harvester.js | 27 ++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 screeps.com/tutorial-3/main.js create mode 100644 screeps.com/tutorial-3/role.builder.js create mode 100644 screeps.com/tutorial-3/role.harvester.js diff --git a/screeps.com/tutorial-3/main.js b/screeps.com/tutorial-3/main.js new file mode 100644 index 0000000..b62b586 --- /dev/null +++ b/screeps.com/tutorial-3/main.js @@ -0,0 +1,19 @@ +var roleHarvester = require('role.harvester'); +var roleBuilder = require('role.builder'); + +module.exports.loop = function () { + + for(var name in Game.rooms) { + console.log('Room "'+name+'" has '+Game.rooms[name].energyAvailable+' energy'); + } + + for(var name in Game.creeps) { + var creep = Game.creeps[name]; + if(creep.memory.role == 'harvester') { + roleHarvester.run(creep); + } + if(creep.memory.role == 'builder') { + roleBuilder.run(creep); + } + } +} \ No newline at end of file diff --git a/screeps.com/tutorial-3/role.builder.js b/screeps.com/tutorial-3/role.builder.js new file mode 100644 index 0000000..4ba342f --- /dev/null +++ b/screeps.com/tutorial-3/role.builder.js @@ -0,0 +1,41 @@ +/* + * Module code goes here. Use 'module.exports' to export things: + * module.exports.thing = 'a thing'; + * + * You can import it from another modules like this: + * var mod = require('role.builder'); + * mod.thing == 'a thing'; // true + */ + +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) { + if(creep.build(targets[0]) == ERR_NOT_IN_RANGE) { + creep.moveTo(targets[0], {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 = roleBuilder; \ No newline at end of file diff --git a/screeps.com/tutorial-3/role.harvester.js b/screeps.com/tutorial-3/role.harvester.js new file mode 100644 index 0000000..95e28a6 --- /dev/null +++ b/screeps.com/tutorial-3/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