【问题标题】:Screeps- How to make path calculation consistent?Screeps-如何使路径计算一致?
【发布时间】:2019-12-13 16:24:42
【问题描述】:

我有一个原型来创建房间布局。我正在尝试计算放置容器的位置:

重生点和控制器之间距离控制器一个单位的最近点

由于某种原因,它似乎给了我多个值(可能是由于路径算法),即 Jump Point according to the API。我怎样才能每次都得到相同的结果,而不是三个不同的点?

Room.prototype.layoutRoom=function(){
    var s:Spawn=this.spawns()[0]
    var c:Controller=this.controller;

    //get path from spawn to controller
    var path = this.findPath(s.pos, c.pos, {ignoreDestructibleStructures: true});

    //place container on last part of path -3 to stay 1 away from controller, and closest to spawn
    //length-1= on endpoint, -2 is one step away, -3 is two steps away 
    var loc=path[path.length-3]
    console.log('layout room, put container: '+loc.x+' '+loc.y)
    this.createConstructionSite(loc.x, loc.y, STRUCTURE_CONTAINER);
}

多次运行上述代码(这是必需的)会导致多个施工现场:

【问题讨论】:

  • 选项ignoreCreeps的默认值为false。也许差异来自小兵的存在?我也不太确定寻路是否完全确定。

标签: javascript screeps


【解决方案1】:

默认情况下,寻路会将小兵视为不可通过的图块。 要解决此问题,请将其添加到选项中:

ignoreCreeps: 是的

【讨论】:

    【解决方案2】:

    所以其他答案涵盖了这一点,如果您将findPath 更改为更像 var path = this.findPath(s.pos, c.pos, {ignoreDestructibleStructures: true, ignoreCreeps: true});

    但是,由于游戏的名称是为了节省 CPU,所以你真的不想每次都计算这条路径。

    您可以将容器位置保存在内存中,或者首先检查容器是否已构建在控制器的一个磁贴中。


    选项 A - 将位置保存在内存中

    这将是我个人的首选选项,因为我经常使用内存。 请记住,在此解决方案中,我添加了许多额外代码,您不需要使其正常工作,但可以使您的代码在此上下文中更安全且不易出错。

    Room.prototype.layoutRoom=function(forceUpdate: boolean){
        var s: Spawn = this.spawns()[0];
        var c: Controller = this.controller;
        
        // Adding this to check that this spawn/controller exists (or code will error out if one doesn't exist)
        if(!s || !c){
            return;
        }
        // Check if a memory value has been set for this container already, and we aren't forcing the location to update via parameter; we can end early otherwise
        if(this.memory.controllerContainer && !forceUpdate){
            // you could uncomment this out to have it place a site on the same location if we find the location is saved in memory already
            // var loc = this.memory.controllerContainer;
            // this.createConstructionSite(controllerContainer.x, controllerContainer.y, STRUCTURE_CONTAINER);
            return;
        }
    
        //get path from spawn to controller
        var path = this.findPath(s.pos, c.pos, {
            ignoreDestructibleStructures: true, ignoreCreeps: true
        });
    
        //place container on last part of path -3 to stay 1 away from controller, and closest to spawn
        //length-1= on endpoint, -2 is one step away, -3 is two steps away 
        var loc=path[path.length-3];
        console.log('layout room, put container: '+loc.x+' '+loc.y);
        
        // Note that I am not saving the RoomPosition object into memory, when the JSON parses 
        // your memory back out you won't be able to use it as a RoomPosition object, so its safer 
        // to save as a custom object that mimics Room position and have a function available to convert 
        // it to one if you need to use it as one for the purpose of passing as a parameter to something
        this.memory.controllerContainer = {x: loc.x, y: lox.y};
        this.createConstructionSite(loc.x, loc.y, STRUCTURE_CONTAINER);
    }
    

    选项 B - 不使用内存

    这个选项会产生同样的效果,就像容器站点或容器已经被控制器存在一样,它不会尝试构建另一个站点。

    Room.prototype.layoutRoom=function(){
        var s: Spawn = this.spawns()[0];
        var c: Controller = this.controller;
        
        // Adding this to check that this spawn/controller exists (or code will error out if one doesn't exist)
        if(!s || !c){
            return;
        }
    
        // Check if a construction site or container exists near the controller, exit if so
        // For brevity, i will include here, but normally I would pull this into its own function
        var numConstructionSites = room.find(FIND_CONSTRUCTION_SITES, { filter: (site) => 
            site.isNearTo(c) && site.structureType === STRUCTURE_CONTAINER
        }).length;
        var numExistingContainers = room.find(FIND_STRUCTURES, { filter: (struct) => 
            struct.isNearTo(c) && struct.structureType === STRUCTURE_CONTAINER
        }).length;
        if(numConstructionSites > 0 || numExistingContainers > 0) {
            return;
        }
    
        //get path from spawn to controller
        var path = this.findPath(s.pos, c.pos, {
            ignoreDestructibleStructures: true, ignoreCreeps: true
        });
    
        //place container on last part of path -3 to stay 1 away from controller, and closest to spawn
        //length-1= on endpoint, -2 is one step away, -3 is two steps away 
        var loc=path[path.length-3];
        console.log('layout room, put container: '+loc.x+' '+loc.y);
        this.createConstructionSite(loc.x, loc.y, STRUCTURE_CONTAINER);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 2010-11-11
      • 2020-11-05
      • 1970-01-01
      • 2020-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多