Skip to content

Instantly share code, notes, and snippets.

@kukiron
Last active April 28, 2018 14:52
Show Gist options
  • Select an option

  • Save kukiron/8fa3e65ef9054c3873a467a1be17af99 to your computer and use it in GitHub Desktop.

Select an option

Save kukiron/8fa3e65ef9054c3873a467a1be17af99 to your computer and use it in GitHub Desktop.
Scripts for Screeps - Latest progress
/* eslint indent: [ "error", 4 ], no-undef: 0 */
const roleHarvester = require("role.harvester")
const roleUpgrader = require("role.upgrader")
const roleBuilder = require("role.builder")
// _.sum will count the number of creeps for each role
const countCreeps = creepRole =>
_.sum(Game.creeps, c => c.memory.role == creepRole)
module.exports.loop = () => {
// check for memory entries of died creeps & delete the memory entry if not alive
for (let name in Memory.creeps) {
if (Game.creeps[name] == undefined) {
delete Memory.creeps[name]
}
}
// for every creep name in Game.creeps
for (let name in Game.creeps) {
let creep = Game.creeps[name]
// get the creep role
let { role } = creep.memory
// assign creep tasks based on roles
role == "harvester" && roleHarvester.run(creep)
role == "upgrader" && roleUpgrader.run(creep)
role == "builder" && roleBuilder.run(creep)
}
// goal: set the minimum numbers of creeps required for each role
const minimumNoOfHarvesters = 5
const minimumNoOfUpgraders = 4
const minimumNoOfBuilders = 4
// currently active creeps for each role
let currentHarvesters = countCreeps("harvester")
let currentUpgraders = countCreeps("upgrader")
let currentBuilders = countCreeps("builder")
// default creep name
let name = undefined
// try spawning creeps if enough for each role isn't available
if (currentHarvesters < minimumNoOfHarvesters) {
name = Game.spawns.Spawn1.createCreep(
[WORK, CARRY, MOVE, MOVE],
undefined,
{ role: "harvester", working: false }
)
} else if (currentUpgraders < minimumNoOfUpgraders) {
name = Game.spawns.Spawn1.createCreep(
[WORK, CARRY, MOVE, MOVE],
undefined,
{ role: "upgrader", working: false }
)
} else if (currentBuilders < minimumNoOfBuilders) {
name = Game.spawns.Spawn1.createCreep(
[WORK, CARRY, MOVE, MOVE],
undefined,
{ role: "builder", working: false }
)
} else {
// default case: try to spawn upgraders
name = Game.spawns.Spawn1.createCreep(
[WORK, CARRY, MOVE, MOVE],
undefined,
{ role: "upgrader", working: false }
)
}
// print name to console if spawning was a success
isNaN(name) && console.log("Spawned new creep: " + name)
}
/* eslint indent: [ "error", 4 ], no-undef: 0 */
const roleUpgrader = require("role.upgrader")
module.exports = {
run: creep => {
// switch state
// if creep is bringing energy to the spawn but has no energy left
if (creep.memory.working == true && creep.carry.energy == 0) {
creep.memory.working = false
}
// if creep is harvesting energy but is full
if (
creep.memory.working == false &&
creep.carry.energy == creep.carryCapacity
) {
creep.memory.working = true
}
if (creep.memory.working == true) {
// find closest constructionSite
let constructionSite = creep.pos.findClosestByPath(
FIND_CONSTRUCTION_SITES
)
// if one is found try to build
if (constructionSite != undefined) {
creep.build(constructionSite) == ERR_NOT_IN_RANGE &&
creep.moveTo(constructionSite)
} else {
// and if not found, go upgrade the controller
roleUpgrader.run(creep)
}
} else {
let source = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE)
creep.harvest(source) == ERR_NOT_IN_RANGE && creep.moveTo(source)
}
}
}
/* eslint indent: [ "error", 4 ], no-undef: 0 */
module.exports = {
run: creep => {
// switch state
// if creep is bringing energy to the spawn but has no energy left
if (creep.memory.working == true && creep.carry.energy == 0) {
creep.memory.working = false
}
// if creep is harvesting energy but is full
if (
creep.memory.working == false &&
creep.carry.energy == creep.carryCapacity
) {
creep.memory.working = true
}
// if creep is supposed to transfer energy to the spawn
if (creep.memory.working == true) {
// try to transfer energy, if the spawn is not in range
// move towards the spawn
creep.transfer(Game.spawns.Spawn1, RESOURCE_ENERGY) ==
ERR_NOT_IN_RANGE && creep.moveTo(Game.spawns.Spawn1)
} else {
// if creep is supposed to harvest energy from source
// find closest energy source
let source = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE)
// try to harvest energy, if the source is not in range
// move towards the source
creep.harvest(source) == ERR_NOT_IN_RANGE && creep.moveTo(source)
}
}
}
/* eslint indent: [ "error", 4 ], no-undef: 0 */
module.exports = {
run: creep => {
// switch state
// if creep is bringing energy to the controller but has no energy left
if (creep.memory.working == true && creep.carry.energy == 0) {
creep.memory.working = false
}
// or if creep is harvesting energy but is full
if (
creep.memory.working == false &&
creep.carry.energy == creep.carryCapacity
) {
creep.memory.working = true
}
// if creep is supposed to transfer energy to the controller
if (creep.memory.working == true) {
// we could also use:
// if (creep.transfer(creep.room.controller, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
// try to upgrade the controller
// if not in range, move towards the controller
creep.upgradeController(creep.room.controller) ==
ERR_NOT_IN_RANGE && creep.moveTo(creep.room.controller)
} else {
// if creep is supposed to harvest energy from source
// find closest source
let source = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE)
// try to harvest energy, if the source is not in range
// move towards the source
creep.harvest(source) == ERR_NOT_IN_RANGE && creep.moveTo(source)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment