引言
在阅读本文章之前,请确保已完成阅读hoho大佬的Screep中文教程目录入门篇及配置篇第二章(搭建本地开发环境)
跳转链接:Screep 中文教程目录 - 简书 (jianshu.com)
文章基于角色驱动,是新手教程的个人改编JS版本,后续可以按需加入更多角色。本文将以房间必须的三个角色(harvester,upgrader,builder)为例,介绍如何进行初步的自动化。
harverter
主要设计思路是假如这个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',true);
}
if(!creep.memory.upgrading && creep.store.getFreeCapacity() == 0) { // 非升级状态&&能量满的时候,变为升级状态
creep.memory.upgrading = true;
creep.say('? upgrade',true);
}
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',true);
}
if(!creep.memory.building && creep.store.getFreeCapacity() == 0) { // 非building状态 && 背包满(空余为0)
creep.memory.building = true; // 变为 building状态
creep.say('?? build',true);
}
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
// 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);
}
}
}
写在最后
我们可以看到,这部分代码真的很入门-w-,比如搓像素,挖运分离,塔,多房,CPU优化等都没有涉及,从另一方面来讲给了刚入门的萌新们很大的自由空间,可以一边对照hoho的教程,以及自己的需求,一边进行增补。如果发现自己的代码已经是屎山时,恭喜,你可以重构(退坑)了!