-
Notifications
You must be signed in to change notification settings - Fork 0
Game UI and basic scripting
You play by writing code in the panel in the bottom of the screen.
Click the "Console" tab.
You can enter your code in this field. It will run once.
Type anything in this field and press Enter.
Your command returns a response (or execution error) in the console below.
For example this command :- "\n\n\n\n\n\n\n\n\n\n\n"
sends 11 line feeds to console
All output is duplicated into your browser console (Ctrl+Shift+J) where you can expand objects for debugging purposes. You can open and close the bottom panel by pressing Alt+Enter.
Energy sources are the main game resource. They can be harvested by worker creeps. The amount of energy in a source is limited, but resumes once in 5 minutes.
Spawns are your colony centers. They can accumulate mined energy and use it to create your units. There may be no more than 3 spawns in a room. Hence, by bulding 3 spawns in a room you actually conquer it. You can have multiple spawns totally in all your rooms though.
A spawn itself can build only basic units. In order to build more complex ones, you have to construct one or more spawn extensions.
You build (spawn) units called creeps the same way as in other strategy games, but with one exception: you construct the "body" of a new creep out of 7 available body part types, the resulting body being a sequence up to 50 parts. It allows thousands of creep types and their roles: ordinary workers, huge construction machines able to build or repair a structure within a few cycles, weaselly couriers, heavy capacious trucks, fast and cheap scouts, well-equipped fighters with regeneration ability, etc. It may even be creeps resembling towers or fortresses for mining, defending, or seizing, with very little speed (couple of tiles per minute), but monstrous characteristics. Everything is up to you, your tactics and imagination.
However, remember that any creep has a life cycle of 1500 game ticks (approx. 30-60 minutes depending on the tick duration). Then it "ages" and dies. So you need not just to control existing creeps but set up manufacturing and automatic control of superseding generations of your creeps.
A standard spawn can spawn only regular creeps with the total cost of up to 300 energy units. Spawning more expensive creeps requires a spawn extension in the room. Each extension can contain up to 50 extra energy units that may be spent on creation of a creep.
Your spawn creates new units called "creeps" by its method createCreep.
Usage of this method is described in the documentation. Each creep has a name and certain body parts that give it various skills.
You can address your spawn by its name the following way: Game.spawns.Spawn1.
Create a worker creep with the body array [WORK,CARRY,MOVE] and name Harvester1 (the name is important for the tutorial!). You can type the code in the console yourself or copy & paste the hint below.
Game.spawns.Spawn1.createCreep( [WORK, CARRY, MOVE], 'Harvester1' );
You now have a creep with the name "Harvester1" that you can control.
You can see all the characteristics of your creep (or other objects) by utilizing the "View" action.
Hide the editor panel with Alt+Enter and select your creep with the help of the "View" action.
Here you can see the characteristics of the object you are now looking at.
You operate the game through the global Game object which is described in detail in the API Reference section. This object lets you access the complete list of your creeps, "review" rooms, pass commands, etc.
var target = Game.spawns.Spawn1;
for(var i in Game.creeps) {
Game.creeps[i].moveTo(target);
}
No changes in the Game object are passed from tick to tick. Even if you manually change any properties of the object, it will not affect the game state. Changing properties and giving commands are possible only through special methods of game objects.
The Game object is created from scratch and filled with data at each tick. In order to memorize information between game ticks, you can use the Memory object. See the next article for more about it.
This yellow square is an energy source — a valuable game resource. It can be harvested by creeps with one or more WORK body parts and transported to the spawn by creeps with CARRY parts.
To give your creep a permanently working command, the console is not enough, since we want the creep to work all the time. So we'll be using the Script tab rather than the console.
Here you can write scripts that will run on a permanent basis, each game tick in a loop. It allows writing constantly working programs to control behaviour of your creeps which will work even while you are offline (in the real game only, not the Simulation Room mode).
To commit a script to the game so it can run, use this button or Ctrl+Enter.
The code for each Tutorial section is created in its own branch. You can view code from these branches for further use in your scripts. ###Scripting fundamentals Writing scripts for Screeps is similar to writing any other JavaScript application. You write your scripts in a special in-game editor, and they are executed continuously even while you are offline (with the exception of the Simulation Room).
Your game script is executed each game tick in a loop. The game tick duration depends on the current load of servers. The goal of a script is to process the current situation within the game and pass orders to your creeps and spawns. Commands are not executed instantly. Rather, the game remembers your commands and executes them later, after all players' scripts have been executed.
Please remember that the exact duration of the execution of your script is limited by the CPU time available in your account. In case of exceeding the limit, the script execution will be stopped. The exception is the Simulation Room where the script execution time is unlimited.
To send a creep to harvest energy, you need to use the methods described in the documentation section below. Commands will be passed each game tick. The harvest method requires that the energy source is adjacent to the creep.
You give orders to a creep by its name this way: Game.creeps.Harvester1.
Use the FIND_SOURCES constant as an argument to the Room.find method.
Send your creep to harvest energy by typing code in the "Script" tab.
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]);
}
}
A bubbling yellow spot inside the creep means that it has started collecting energy from the source.
To make the creep transfer energy back to the spawn, you need to use the method Creep.transfer. However, remember that it should be done when the creep is next to the spawn, so the creep needs to walk back.
If you modify the code by adding the check .carry.energy < .carryCapacity to the creep, it will be able to go back and forth on its own, giving energy to the spawn and returning to the source.
Extend the creep program so that it can transfer harvested energy to the spawn and return back to work.
module.exports.loop = function () {
var creep = Game.creeps.Harvester1;
if(creep.carry.energy < creep.carryCapacity) {
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);
}
}
}
Great! This creep will now work as a harvester until it dies. Remember that almost any creep has a life cycle of 1500 game ticks, then it "ages" and dies (this behavior is disabled in the Tutorial).
Let's create another worker creep to help the first one. It will cost another 200 energy units, so you may need to wait until your harvester collects enough energy. The createCreep method will return an error code ERR_NOT_ENOUGH_ENERGY (-6) until then.
Remember: to execute code once just type it in the "Console" tab.
Spawn a second creep with the body [WORK,CARRY,MOVE] and name Harvester2.
Game.spawns.Spawn1.createCreep( [WORK, CARRY, MOVE], 'Harvester2' );
The second creep is ready, but it won't move until we include it into the program.
To set the behavior of both creeps we could just duplicate the entire script for the second one, but it's much better to use the for loop against all the screeps in Game.creeps.
Expand your program to both the creeps.
module.exports.loop = function () {
for(var name in Game.creeps) {
var creep = Game.creeps[name];
if(creep.carry.energy < creep.carryCapacity) {
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);
}
}
}
}
Now let's improve our code by taking the workers' behavior out into a separate module. Create a module called role.harvester with the help of the Modules section on the left of the script editor and define a run function inside the module.exports object, containing the creep behavior.
Create a role.harvester module.
var roleHarvester = {
/** @param {Creep} creep **/
run: function(creep) {
if(creep.carry.energy < creep.carryCapacity) {
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;
Now you can rewrite the main module code, leaving only the loop and a call to your new module by the method require('role.harvester').
Include the role.harvester module in the main module.
var roleHarvester = require('role.harvester');
module.exports.loop = function () {
for(var name in Game.creeps) {
var creep = Game.creeps[name];
roleHarvester.run(creep);
}
}
By adding new roles and modules to your creeps this way, you can control and manage the work of many creeps. In the next Tutorial section, we’ll develop a new creep role.