screeps所遇到的难题

Posted action0

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了screeps所遇到的难题相关的知识,希望对你有一定的参考价值。

首先记录一下遇见的问题:
1.使不同的小兵分别移动到不同的目的地——已解决,sources数组改下标就好。

每个mode中的代码中,以roleUpgrader教程代码为例:

 1 var roleUpgrader = {
 2 
 3     /** @param {Creep} creep **/
 4     run: function(creep) {
 5         if(creep.store[RESOURCE_ENERGY] == 0) {
 6             var sources = creep.room.find(FIND_SOURCES);
 7             if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
 8                 creep.moveTo(sources[0]);
 9             }
10         }
11         else {
12             if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
13                 creep.moveTo(creep.room.controller);
14             }
15         }
16     }
17 };
18 
19 module.exports = roleUpgrader;

 


2.使它们自动补充兵力——已解决,照着教程按着自己兵的名字进行修改

以我修改的教程代码为例,复制第15~23行,然后根据自己命名修改,后面24~32,33~41,44~51,都是根据这样的原理修改,会暂用一点CPU,但考虑先跑起来,以后再来提高效率:

 1 var roleHarvester = require(role.builder);
 2 var roleUpgrader = require(role.upgrader);
 3 var roleBuilder = require(role.upgrader);
 4 var roleBiger = require(role.biger)
 5 
 6 module.exports.loop = function () {
 7     
 8     for(var name in Memory.creeps) {
 9         if(!Game.creeps[name]) {
10             delete Memory.creeps[name];
11             console.log(Clearing non-existing creep memory:, name);
12         }
13     }
14 
15     var harvesters = _.filter(Game.creeps, (creep) => creep.memory.role == harvester);
16     console.log(Harvesters:  + harvesters.length);
17 
18     if(harvesters.length < 2) {
19         var newName = Harvester + Game.time;
20         console.log(Spawning new harvester:  + newName);
21         Game.spawns[Spawn1].spawnCreep([WORK,CARRY,MOVE], newName, 
22             {memory: {role: harvester}});
23     }
24 var builders = _.filter(Game.creeps, (creep) => creep.memory.role == builder); 25 console.log(builders: + builders.length); 26 27 if(builders.length < 2) { 28 var newName = Builder + Game.time; 29 console.log(Spawning new builder: + newName); 30 Game.spawns[Spawn1].spawnCreep([WORK,CARRY,MOVE], newName, 31 {memory: {role: builder}}); 32 }
33 var upgraders = _.filter(Game.creeps, (creep) => creep.memory.role == upgrader); 34 console.log(upgraders: + upgraders.length); 35 36 if(upgraders.length < 6) { 37 var newName = Upgrader + Game.time; 38 console.log(Spawning new upgrader: + newName); 39 Game.spawns[Spawn1].spawnCreep([WORK,CARRY,MOVE], newName, 40 {memory: {role: upgrader}}); 41 }
42 43 var bigers = _.filter(Game.creeps, (creep) => creep.memory.role == biger); 44 console.log(bigers: + bigers.length); 45 46 if(bigers.length < 1) { 47 var newName = Biger + Game.time; 48 console.log(Spawning new biger: + newName); 49 Game.spawns[Spawn1].spawnCreep([WORK,CARRY,MOVE], newName, 50 {memory: {role: biger}}); 51 }
52 53 if(Game.spawns[Spawn1].spawning) { 54 var spawningCreep = Game.creeps[Game.spawns[Spawn1].spawning.name]; 55 Game.spawns[Spawn1].room.visual.text( 56 ??? + spawningCreep.memory.role, 57 Game.spawns[Spawn1].pos.x + 1, 58 Game.spawns[Spawn1].pos.y, 59 {align: left, opacity: 0.8}); 60 } 61 62 for(var name in Game.creeps) { 63 var creep = Game.creeps[name]; 64 if(creep.memory.role == harvester) { 65 roleHarvester.run(creep); 66 } 67 if(creep.memory.role == upgrader) { 68 roleUpgrader.run(creep); 69 } 70 if(creep.memory.role == builder) { 71 roleBuilder.run(creep); 72 } 73 if(creep.memory.role == biger) { 74 roleBiger.run(creep); 75 } 76 } 77 }

 


3.建筑物的建造方法找了很久——已解决,照着教程的Game.spawns[‘Spawn1‘].room.createConstructionSite( 23, 22, STRUCTURE_TOWER );

对蓝色部分更改成自己想修改的建筑,有关建筑介绍,看这个网址:https://twodam.net/Tutorial-for-Screeps-3

当时教程直接就建造好了5个扩容器,到我自己入手时,一直找怎么建扩容器EXTENSION,找的十分久。

Game.spawns[‘Spawn1‘].room.createConstructionSite( X, Y, STRUCTURE_EXTENSION );

 

4.小兵工作内容同步——已解决

每个小兵的工作内容,是根据模块来控制的。

技术图片

 

在主函数中,开头要接入这些模块

 

 技术图片

 

同时在主函数中,让小兵工作时,调用模块

 

技术图片

 

 

 我记得我当时修改的时候,直接复制,忘记修改红字,使得两种小兵干同一个工作,我愣了很久,以为它们太相亲相爱,放着没管,下图是当时误认为它们相亲相爱的代码,可以比对上面:

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);
        }
        if(creep.memory.role == ‘biger‘) {
            roleHarvester.run(creep);
        }
    }

 

5.小兵对建筑工作的优先级——已解决

 以我修改的教程代码为例:

var roleBiger = {

    /** @param {Creep} creep **/
    run: function(creep) {
        if(creep.store.getFreeCapacity() > 0) {
            var sources = creep.room.find(FIND_SOURCES);
            if(creep.harvest(sources[1]) == ERR_NOT_IN_RANGE) {
                creep.moveTo(sources[1], {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_CONTAINER||
                                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 = roleBiger;

红色的区域就是要让小兵去补充资源的代码,放在第一行,为targets[0],放在第二行,为targets[1],按照自己觉得的优先级,照着格式,把建筑名称加上就好,有关建筑的名称记得大写,有关建筑介绍,看这个网址:https://twodam.net/Tutorial-for-Screeps-3

 

6.给控制器送资源的小兵,一次只给它一个能量——未解决

我看了下判断条件,第五行

技术图片

 

 当他有资源的时候,它就会送货,等于0时才会补货。

我想让它和其他小兵一样补充完再去送,所以更改了条件,然后它变成补充50,送1个,再去补充。。。我傻眼了。后续再来更改这个地方的条件。

只能周末玩一会。

技术图片

 

 

7.建不了人工墙,塔,大兵,等等。——未解决

我猜想,可能2级控制器不能建吧。可能要升级控制器才行。

 

8.安排目标不合理,浪费了很多时间。——正在改进每个小兵的计划安排

 

9.只有周末能玩,比不过其他玩家,可能被侵占。——复盘重来

 

以上是关于screeps所遇到的难题的主要内容,如果未能解决你的问题,请参考以下文章

985高校的高材生只会写代码片段,丢人吗?

985大学的高材生只会写代码片段,丢人吗?

985大学的高材生只会写代码片段,丢人吗?

985大学的高材生只会写代码片段,丢人吗?

程序员专属游戏 Screeps:使用 JS/TS 代码控制自己的殖民地

idc管理系统应该如何去解决IDC行业所遇到的难题