Finished fourth tutorial
This commit is contained in:
parent
714ae6013b
commit
e1e40f02e0
41
screeps.com/tutorial-4/main.js
Normal file
41
screeps.com/tutorial-4/main.js
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
27
screeps.com/tutorial-4/role.harvester.js
Normal file
27
screeps.com/tutorial-4/role.harvester.js
Normal file
@ -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;
|
29
screeps.com/tutorial-4/role.upgrader.js
Normal file
29
screeps.com/tutorial-4/role.upgrader.js
Normal file
@ -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;
|
Loading…
Reference in New Issue
Block a user