Screeps(初步理解)入门

这篇博客介绍了如何在 Screeps 游戏中创建和编程三种基本角色的 Creeps:采集者、升级控制器者和建造者。采集者负责收集能源,升级控制器者用于提升房间控制器的等级,而建造者则建造和修复结构。通过不同的工作职能分配和代码实现,确保 Creeps 能够自动执行各自的任务,从而提高游戏效率。
摘要由CSDN通过智能技术生成

首先来创建一个最简单的采集creep

1.创建一个最简单的creep,把他的工作职能赋值为采集(控制台中创建)

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

2.写采集者的代码块

var roleHarvester = {

    /** @param {Creep} creep **/
    run: function(creep) {
        //该存储的剩余可用容量(当大于0时,进行采集任务)
        if(creep.store.getFreeCapacity() > 0) {
            //寻找所有的资源
            var sources = creep.room.find(FIND_SOURCES);
            //如果目标太远了,则移动到目标点(creep.harvest采集)
            if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
                creep.moveTo(sources[0]);
            }
        }
        else {
            //transfer(转移)到基地,当目标不为远的时候进行移动
            if(creep.transfer(Game.spawns['Spawn1'], RESOURCE_ENERGY) ==    ERR_NOT_IN_RANGE) {
                creep.moveTo(Game.spawns['Spawn1']);
            }
        }
    }
};

module.exports = roleHarvester;

3.主项目中开始调用创建好的代码块

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

module.exports.loop = function () {

    for(var name in Game.creeps) {
        var creep = Game.creeps[name];
        //采集者则进行采集的操作
        if(creep.memory.role == 'harvester') {
            roleHarvester.run(creep);
        }
    }
}

此时该creep就可一直工作了

然后我们来创建一个最简单的升级控制器的creep

1.创建一个升级控制器的creep

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

2.写升级控制器的creep代码块

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.store[RESOURCE_ENERGY] < 50){
            if(creep.harvest(sources[1]) != OK) {
                if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(creep.room.controller);
                }
            }
        }
        //有能量的时候去移动升级控制器
        else {
            if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
                creep.moveTo(creep.room.controller);
            }
        }
    }
};

module.exports = roleUpgrader;

3.主项目中把升级者的工作写上

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

之后我们在创建一个用于建造建筑的creep

1.创建建造建筑的creep

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

2.写建造建筑的creep代码块

var roleBuilder = {

    /** @param {Creep} creep **/
    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) {
                //当有工地创建的时候,则移动建造,visualizePathStyle(可以标识路径)
                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;

 3.更新主项目

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

修改采集者到extension放置能量

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;

创建一个采集有四倍效率的采集者

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

自动添加采集者

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

module.exports.loop = function () {
    
    //清除掉已经gg的creep
     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);
    //当数量小于2的时候,开始生产采集者
    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);
        }
    }
}

激活安全模式

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

防御塔

//在界面中找到防御塔的id
var tower = Game.getObjectById('693092d10bdaba0a44c94e45');
    if(tower) {
        //寻找不属于我们自己的细胞,并且攻击他
        var closestHostile = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
        if(closestHostile) {
            tower.attack(closestHostile);
        }
    }

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值