Skip to content

Commit

Permalink
Add role: builder
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamYuhangLee committed Mar 28, 2019
1 parent 8fb0671 commit ada2c31
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
3 changes: 3 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var roleHarvester = require("role.harvester");
var roleUpgrader = require("role.upgrader");
var roleBuilder = require("role.builder");

module.exports.loop = function () {
for (let name in Game.creeps) {
Expand All @@ -8,6 +9,8 @@ module.exports.loop = function () {
roleHarvester.run(creep);
} else if (creep.memory.role === "upgrader") {
roleUpgrader.run(creep);
} else if (creep.memory.role === "builder") {
roleBuilder.run(creep);
}
}
};
34 changes: 34 additions & 0 deletions role.builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.harvester');
* mod.thing == 'a thing'; // true
*/
var roleUpgrader = require("role.upgrader");

module.exports = {
run: function (creep) {
if (creep.carry.energy === 0) {
creep.memory.collecting = true;
} else if (creep.carry.energy === creep.carryCapacity) {
creep.memory.collecting = false;
}
if (creep.memory.collecting) {
let source = creep.pos.findClosestByRange(FIND_SOURCES);
if (creep.harvest(source) === ERR_NOT_IN_RANGE) {
creep.moveTo(source);
}
} else {
let construction = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES);
if (construction) {
if (creep.build(construction) === ERR_NOT_IN_RANGE) {
creep.moveTo(construction);
}
} else {
roleUpgrader.run(creep);
}
}
}
};
7 changes: 6 additions & 1 deletion role.harvester.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@

module.exports = {
run: function (creep) {
if (creep.carry.energy < creep.carryCapacity) {
if (creep.carry.energy === 0) {
creep.memory.collecting = true;
} else if (creep.carry.energy === creep.carryCapacity) {
creep.memory.collecting = false;
}
if (creep.memory.collecting) {
let source = creep.pos.findClosestByRange(FIND_SOURCES);
if (creep.harvest(source) === ERR_NOT_IN_RANGE) {
creep.moveTo(source);
Expand Down
7 changes: 6 additions & 1 deletion role.upgrader.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@

module.exports = {
run: function (creep) {
if (creep.carry.energy < creep.carryCapacity) {
if (creep.carry.energy === 0) {
creep.memory.collecting = true;
} else if (creep.carry.energy === creep.carryCapacity) {
creep.memory.collecting = false;
}
if (creep.memory.collecting) {
let source = creep.pos.findClosestByRange(FIND_SOURCES);
if (creep.harvest(source) === ERR_NOT_IN_RANGE) {
creep.moveTo(source);
Expand Down

0 comments on commit ada2c31

Please sign in to comment.