Finished second tutorial
This commit is contained in:
parent
63c65c4a2b
commit
b3904c1209
15
screeps.com/tutorial-2/main.js
Normal file
15
screeps.com/tutorial-2/main.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
var roleHarvester = require('role.harvester');
|
||||||
|
var roleUpgrader = require('role.upgrader');
|
||||||
|
|
||||||
|
module.exports.loop = function () {
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
screeps.com/tutorial-2/role.harvester.js
Normal file
19
screeps.com/tutorial-2/role.harvester.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(Game.spawns['Spawn1'].energy < Game.spawns['Spawn1'].energyCapacity) {
|
||||||
|
if(creep.transfer(Game.spawns['Spawn1'], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
|
||||||
|
creep.moveTo(Game.spawns['Spawn1']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = roleHarvester;
|
28
screeps.com/tutorial-2/role.upgrader.js
Normal file
28
screeps.com/tutorial-2/role.upgrader.js
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* 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.upgrader');
|
||||||
|
* mod.thing == 'a thing'; // true
|
||||||
|
*/
|
||||||
|
|
||||||
|
var roleUpgrader = {
|
||||||
|
|
||||||
|
/** @param {Creep} creep **/
|
||||||
|
run: function(creep) {
|
||||||
|
if(creep.store[RESOURCE_ENERGY] == 0) {
|
||||||
|
var sources = creep.room.find(FIND_SOURCES);
|
||||||
|
if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
|
||||||
|
creep.moveTo(sources[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
|
||||||
|
creep.moveTo(creep.room.controller);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = roleUpgrader;
|
Loading…
Reference in New Issue
Block a user