0% found this document useful (0 votes)
62 views

Screeps

The document contains code for a Screeps game that defines roles for creeps to harvest energy from sources and upgrade the controller. It spawns creeps with the roles of "harvester" and "upgrader" and uses separate scripts to define the run behaviors for each role. The main script loops through all creeps and runs their role behavior based on their memory role value. This allows the game to have creeps that specialize in different tasks like harvesting or upgrading to work together efficiently.

Uploaded by

princesspeachyum
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Screeps

The document contains code for a Screeps game that defines roles for creeps to harvest energy from sources and upgrade the controller. It spawns creeps with the roles of "harvester" and "upgrader" and uses separate scripts to define the run behaviors for each role. The main script loops through all creeps and runs their role behavior based on their memory role value. This allows the game to have creeps that specialize in different tasks like harvesting or upgrading to work together efficiently.

Uploaded by

princesspeachyum
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Console

Game.spawns['Spawn1'].spawnCreep( [WORK, CARRY, MOVE], 'Harvester1' );

Script - main

module.exports.loop = function () {

var creep = Game.creeps['Harvester1'];

var sources = creep.room.find(FIND_SOURCES);

if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {

creep.moveTo(sources[0]);

module.exports.loop = function () {

var creep = Game.creeps['Harvester1'];

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( creep.transfer(Game.spawns['Spawn1'], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE ) {

creep.moveTo(Game.spawns['Spawn1']);

Console
Game.spawns['Spawn1'].spawnCreep( [WORK, CARRY, MOVE], 'Harvester2' );
Script - main
module.exports.loop = function () {
for(var name in Game.creeps) {
var creep = Game.creeps[name];

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(creep.transfer(Game.spawns['Spawn1'], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(Game.spawns['Spawn1']);
}
}
}
}

Script role.harvester
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(creep.transfer(Game.spawns['Spawn1'], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(Game.spawns['Spawn1']);
}
}
}
};

module.exports = roleHarvester;

script – main
var roleHarvester = require('role.harvester');

module.exports.loop = function () {

for(var name in Game.creeps) {


var creep = Game.creeps[name];
roleHarvester.run(creep);
}
}

Console
Game.spawns['Spawn1'].spawnCreep( [WORK, CARRY, MOVE], 'Upgrader1' );
Game.creeps['Harvester1'].memory.role = 'harvester';
Game.creeps['Upgrader1'].memory.role = 'upgrader';

Script-role.upgrader
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;

script – main
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);
}
}
}

Console
Game.spawns['Spawn1'].spawnCreep( [WORK, CARRY, MOVE], 'Builder1',
{ memory: { role: 'builder' } } );

Script – main (this doesn’t include role.upgrader)


var roleHarvester = require('role.harvester');
var roleBuilder = require('role.builder');

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 == 'builder') {
roleBuilder.run(creep);
}
}
}

Script – role.harvester
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;

console
Game.spawns['Spawn1'].spawnCreep( [WORK,WORK,WORK,WORK,CARRY,MOVE,MOVE],
'HarvesterBig',
{ memory: { role: 'harvester' } } );

Final code
Script – main
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);
}
}
}

Script – role.harvester
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(creep.transfer(Game.spawns['Spawn1'], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(Game.spawns['Spawn1']);
}
}
}
};

module.exports = roleHarvester;

script – role.upgrader
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;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy