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