当前位置: 首页 > 工具软件 > Screeps > 使用案例 >

screeps学习笔记

薛烨霖
2023-12-01

Screeps 即时战略游戏

1. 基本脚本

1. 创建 creep(小兵) 采集资源

  1. 创建一个creep:功能,名字

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

  3. 搜索矿产资源,资源可能不止一处,所以获取到的是一个数组

  4. 采集矿产资源,利用下标确定采集哪一个

  5. 移动到矿产坐标,用下标确定移动到哪一个

    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]);
        }
    }
    
  6. 每一个 creep 都有可采集的上限,到达上限时需要放回自己的地方

  7. 获取 creep 自身的存储空间,空间大于0时,说明还有位置可以继续采矿,否则就该回家了

  8. 把资源放回到指定目标上

    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']);
            }
        }
    }
    

2. 可以创建多个 creep

  1. 但是名字需要有区分

    Game.spawns['Spawn1'].spawnCreep([WORK,CARRY,MOVE], 'Harvester2');
    
  2. 使用 for 循环获取所有的 creep 执行指令

    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']);
                }
            }
        }
    }
    
  3. 可以使用一个单独的模块定义一个功能,使 creep 更好的得到控制

    role.harvester 模块

    var roleHarvester = {
        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;
    

    引入模块

    var roleHarvester = require('role.harvester');
    
    module.exports.loop = function () {
        for(var name in Game.creeps) {
            var creep = Game.creeps[name];
            roleHarvester.run(creep);
        }
    }
    

2. 升级控制器

你需要一个新的工人怪物来升级你的控制器等级。我们叫它 pgrader1。在接下来的部分中,我们将讨论如何自动创建creeps,但是现在让我们手动向控制台发送一个命令。

1. 创建一个 creep 用于控制器操作

  1. 创建一个名叫 Upgrader1creep

    Game.spawns['Spawn1'].spawnCreep( [WORK, CARRY, MOVE], 'Upgrader1' );
    
  2. 给不同 creep 分配不同的任务

    Game.creeps['Harvester1'].memory.role = 'harvester';
    Game.creeps['Upgrader1'].memory.role = 'upgrader';
    
  3. 定义 role.upgrader 模块用于升级控制器

    var roleUpgrader = {
        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;
    

    导入模块

    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);
            }
        }
    }
    

3. 建筑结构

有了控制器之后,可以建造其他的建筑

1. 创建一个 creep

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

2. 定义一个模块,创建一个建造者的任务

  1. 当存储对象为0时,停止建造,开始采集资源,say一个
  2. 当前可用容量为0时 (背包满了),停止采集,开始建造,say一个
var roleBuilder = {

    run: function(creep) {

	    if(creep.memory.building && creep.store[RESOURCE_ENERGY] == 0) {
            creep.memory.building = false;
            creep.say(' harvest');
	    }
	    if(!creep.memory.building && creep.store.getFreeCapacity() == 0) {
	        creep.memory.building = true;
	        creep.say(' build');
	    }

	    if(creep.memory.building) {
	        var targets = creep.room.find(FIND_CONSTRUCTION_SITES);
            if(targets.length) {
                if(creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
                }
            }
	    } else {
	        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;

引入模块

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);
        }
    }
}

3. 改变采集兵的任务

使采集的资源可以放进其他容器里

var roleHarvester = {
    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;

引入模块

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

module.exports.loop = function () {

    for(var name in Game.rooms) {
        console.log('Room "'+name+'" has '+Game.rooms[name].energyAvailable+' energy');
    }

    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);
        }
    }
}

4. 创建一个更强大的 creep

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

4. 自动生成 creep

到目前为止,我们已经直接在控制台创建了新的怪物。一直这样做不是一个好主意,因为爬行的想法就是让你的蚁群自己控制自己。如果你教你的生物在房间里自己制造生物,你会做得很好这是一个相当复杂的主题,许多交易者花费了数月的时间来完善和改进他们的自动生成代码。

1. 获取当前 creep 的数量

var roleHarvester = require('role.harvester');
var roleUpgrader = require('role.upgrader');

module.exports.loop = function () {

    var harvesters = _.filter(Game.creeps, (creep) => creep.memory.role == 'harvester');
    console.log('Harvesters: ' + harvesters.length);

    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);
        }
    }
}

2. 保证 creep 的数量一直是2个

var roleHarvester = require('role.harvester');
var roleUpgrader = require('role.upgrader');

module.exports.loop = function () {

    var harvesters = _.filter(Game.creeps, (creep) => creep.memory.role == 'harvester');
    console.log('Harvesters: ' + harvesters.length);

    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'}});        
    }
    
    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);
        }
    }
}

3. 使指定的一个 creep 自杀

Game.creeps['Harvester1'].suicide()

4. 清除一个不存在的 creep 对象

var roleHarvester = require('role.harvester');
var roleUpgrader = require('role.upgrader');

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');
    console.log('Harvesters: ' + harvesters.length);

    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'}});
    }
    
    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);
        }
    }
}

5. 捍卫你的房间

这鬼混的世界不是最安全的地方。其他的掠夺者可能对你的领土有主权。此外,你的房间可能偶尔会被中立NPC的怪物袭击。所以你应该考虑一下你的殖民地防御,以便成功地发展它。

1. 房间安全模式

抵御攻击最可靠的方法是使用房间安全模式。在安全模式下,其他怪物都不能在房间里使用任何有害的方法(但你仍然可以抵御陌生人)。

Game.spawns['Spawn1'].room.controller.activateSafeMode();

2. 在指定位置创建一个地基

塔是防御房间最简单的方法。它们使用能量,可以对房间里的任何怪物进行攻击或治疗。效果取决于塔和目标之间的距离。

Game.spawns['Spawn1'].room.createConstructionSite( 23, 22, STRUCTURE_TOWER );

3. 建筑一个炮塔

var roleHarvester = {
    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) {
                if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
                }
            }
        }
	}
};

module.exports = roleHarvester;

4. 使用炮塔

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

module.exports.loop = function () {

    var tower = Game.getObjectById('f4d67720621a96973ffff071');
    if(tower) {
        var closestHostile = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
        if(closestHostile) {
            tower.attack(closestHostile);
        }
    }

    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);
        }
    }
}

5. 修复炮塔 / 建筑

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

module.exports.loop = function () {

    var tower = Game.getObjectById('f4d67720621a96973ffff071');
    if(tower) {
        var closestDamagedStructure = tower.pos.findClosestByRange(FIND_STRUCTURES, {
            filter: (structure) => structure.hits < structure.hitsMax
        });
        if(closestDamagedStructure) {
            tower.repair(closestDamagedStructure);
        }

        var closestHostile = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
        if(closestHostile) {
            tower.attack(closestHostile);
        }
    }

    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);
        }
    }
}

6. API

Game.spawns['Spawn1'] 获取母巢 (spawn) ,你的殖民地中心

Game.spawns['Spawn1'].spawnCreep 启动 creep 孵化过程,生产一个小兵

Game.creeps[名字] 获取 creep

creep.room.find(FIND_SOURCES) 获取矿产资源

creep.harvest(矿产[0]) 采集资源

creep.moveTo(矿产[0]) 移动到指定位置

creep.store[RESOURCE_ENERGY] 存储资源的对象,(仓库/背包)

creep.store.getFreeCapacity() 获取存储空间对象

  1. getCapacity() 返回总存储容量

  2. getFreeCapacity() 返回可用容量

  3. getUsedCapacity 返回已用容量

creep.transfer(目标,常量) 把采集的资源放回到目标处

creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});creep 的移动线路上画一条线

Game.rooms[name].energyAvailable 本房间所有的能量总额

Game.spawns['Spawn1'].room.visual 使用该对象在房间中绘制简单的形状 (线条,圆,文本标签)

  1. Game.spawns[].room.visual.line 绘制一条线
  2. Game.spawns[].room.visual.circle 绘制一个圆
  3. Game.spawns[].room.visual.rect 绘制一个矩形
  4. Game.spawns[].room.visual.poly 绘制一个折线
  5. Game.spawns[].room.visual.text 绘制一个文本标签
  6. Game.spawns[].room.visual.clear 移除该房间的所有视觉效果

7. 错误值

常量value描述
ERR_NOT_OWNER-1你不是该 creep 的所有者
ERR_BUSY-4这个 creep 依然在孵化中
ERR_NOT_FOUND-5未找到 extractor。你必须建造一个 extractor 来开采矿物
ERR_NOT_ENOUGH_RESOURCES-6目标中已经没有可采集的能量或者矿物
ERR_INVALID_TARGET-7目标不是有效的 source 或者 mineral 对象
ERR_NOT_IN_RANGE-9目标太远了
ERR_TIRED-11extractor 仍在冷却中
ERR_NO_BODYPART-12这个 creep 身上没有 WORK 部件
 类似资料: