前言
这篇博客给想要入坑Screeps但是不知道一开始不知道如何下手的玩家的教程,关于要不要入坑这篇博文介绍的很详细:Screeps 介绍及入坑,其他关于Screeps的信息也空余参考如下文档:
中文官方文档
官方教程
Screeps API
Screep 中文教程目录
设计
这篇文章主要设计了初步自动化运行的代码,在找到并确定一个合适的房间后,选取某合适的位置设定为spawn(可以将其理解为虫族母巢),前期先确定使用如下三个角色
harvester | 采矿者 | 将能量或元素矿开采出来并存入指定的结构中 |
---|---|---|
upgrader | 升级者 | 从指定的结构中取出能量升级房间控制器 |
builder | 建造者 | 从指定的结构中取出能量并建造结构 |
角色设计
harvester
主要设计思路是假如这个harvester的store还有空间,那么就先找到一个source,然后到他附近收集能源,能源收集足够之后,遍历各个建筑,找出需要补充能量的建筑,然后去各个补充能量。
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.structureType == STRUCTURE_TOWER) &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
}
});
if(targets.length > 0) { // 需要维护的建筑数目 > 0
if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
}
}
}
}
};
module.exports = roleHarvester;
upgrader
upgrader主要是升级控制器用的,设计思路是用upgrading
来设定是否是升级状态,是的话就去升级控制器,不是的话就去采集能量。
var roleUpgrader = {
/** @param {Creep} creep **/
run: function(creep) {
if(creep.memory.upgrading && creep.store[RESOURCE_ENERGY] == 0) { // 升级状态&&能量不足的时候,变为采集者
creep.memory.upgrading = false;
creep.say('🔄 harvest');
}
if(!creep.memory.upgrading && creep.store.getFreeCapacity() == 0) { // 非升级状态&&能量满的时候,变为升级状态
creep.memory.upgrading = true;
creep.say('⚡ upgrade');
}
if(creep.memory.upgrading) { // 升级状态,找到控制器并升级 + 可视化
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.controller, {visualizePathStyle: {stroke: '#ffffff'}});
}
}
else { // 采集状态 + 可视化
var sources = creep.room.find(FIND_SOURCES);
if(creep.harvest(sources[1]) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[1], {visualizePathStyle: {stroke: '#ffaa00'}});
}
}
}
};
module.exports = roleUpgrader;
builder
builder主要是用来建造各种建筑,比如控制器升级到2级之后,可以建造5个EXTENSION,每个可以存储50个能量,这个主要是跟spawn共享能量。设计思路也是设定一个building
来判断是否是建造状态,是的话就去建造,不是的话就去采集能量。
var roleBuilder = {
/** @param {Creep} creep **/
run: function(creep) {
if(creep.memory.building && creep.store[RESOURCE_ENERGY] == 0) { // building && 背包为空
creep.memory.building = false; // 变为 非building状态
creep.say('🔄 harvest');
}
if(!creep.memory.building && creep.store.getFreeCapacity() == 0) { // 非building状态 && 背包满(空余为0)
creep.memory.building = true; // 变为 building状态
creep.say('🚧 build');
}
if(creep.memory.building) { // building状态的时候
var targets = creep.room.find(FIND_CONSTRUCTION_SITES); // 寻找建筑位
if(targets.length) { // targets.length > 0 || 建筑位 > 0
if(creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}}); // 绘制路径
}
}
}
else { // 非building状态的时候, 到source旁边并采集
var sources = creep.room.find(FIND_SOURCES);
if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}});
}
}
}
};
module.exports = roleBuilder;
main
自动生产两个harvest,一个upgrader,控制器2级的时候,生产一个builder。
分别计算harvest,upgrader,builder的数量,少于其某个设定值的时候,命令spawn生产相应的creep。
// creep自杀 释放内存 + 保证至少2个
var roleHarvester = require('role.harvester');
var roleUpgrader = require('role.upgrader');
var roleBuilder = require('role.builder');
module.exports.loop = function () {
for(var name in Memory.creeps) { // 释放内存
if(!Game.creeps[name]) {
delete Memory.creeps[name];
console.log('Clearing non-existing creep memory:', name);
}
}
var harvesters = _.filter(Game.creeps, (creep) => creep.memory.role == 'harvester');
var upgraders = _.filter(Game.creeps, (creep) => creep.memory.role == 'upgrader');
var builders = _.filter(Game.creeps, (creep) => creep.memory.role == 'builder');
//console.log('Harvesters: ' + harvesters.length);
var controller_level = Game.spawns['Spawn1'].room.controller.level; // 查看控制器等级
//console.log('controller:' + Game.spawns['Spawn1'].room.controller.level)
// harvester少于2的时候生产harvester
if(harvesters.length < 2) {
var newName = 'Harvester' + Game.time;
console.log('Spawning new harvester: ' + newName);
Game.spawns['Spawn1'].spawnCreep([WORK,CARRY,MOVE], newName,
{memory: {role: 'harvester'}}); // 指定relo属性
}
// harvester等于2的时候生产 upgrader
if(harvesters.length == 2 && upgraders.length < 2) {
var newName = 'Upgrader' + Game.time;
console.log('Spawning new upgrader: ' + newName);
Game.spawns['Spawn1'].spawnCreep([WORK,CARRY,MOVE], newName,
{memory: {role: 'upgrader'}}); // 指定relo属性
}
// 生产builder
if(controller_level >= 2 && builders.length < 2){
var nameBuilder = 'Builder' + Game.time;
console.log('Spawing new builder:' + nameBuilder);
Game.spawns['Spawn1'].spawnCreep([WORK,CARRY,MOVE], nameBuilder,
{memory:{role: 'builder'}});
}
if(Game.spawns['Spawn1'].spawning) { // 孵化过程可视化
var spawningCreep = Game.creeps[Game.spawns['Spawn1'].spawning.name];
Game.spawns['Spawn1'].room.visual.text(
'🛠️' + spawningCreep.memory.role,
Game.spawns['Spawn1'].pos.x + 1,
Game.spawns['Spawn1'].pos.y,
{align: 'left', opacity: 0.8});
}
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);
}
if(creep.memory.role == 'builder') {
roleBuilder.run(creep);
}
}
}