第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

爬行-如何使路徑計(jì)算保持一致?

爬行-如何使路徑計(jì)算保持一致?

SMILET 2021-04-27 13:15:35
我有一個(gè)創(chuàng)建房間布局的原型。我正在嘗試將放置容器的位置計(jì)算為:生成器與控制器之間的最近點(diǎn),與控制器相距一個(gè)單位由于某種原因,它似乎給了我多個(gè)值(可能是由于使用了路徑算法),該值是根據(jù)API的“跳轉(zhuǎn)點(diǎn)”。每次如何獲得相同的結(jié)果,而不是三個(gè)不同的點(diǎn)?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);}多次運(yùn)行上述代碼(這是必需的)會導(dǎo)致多個(gè)建筑工地:
查看完整描述

2 回答

?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊

所以另一個(gè)答案涵蓋了這一點(diǎn),如果您將其更改findPath為更類似的 內(nèi)容,那將是一致的var path = this.findPath(s.pos, c.pos, {ignoreDestructibleStructures: true, ignoreCreeps: true});


但是,由于游戲的名稱是為了節(jié)省CPU,因此您真的不想每次都計(jì)算此路徑。


您可以將容器位置保存在內(nèi)存中,或者首先檢查在控制器的一個(gè)磁貼中是否已構(gòu)建容器。


選項(xiàng)A-將位置保存在內(nèi)存中

這將是我個(gè)人首選的選擇,因?yàn)槲医?jīng)常使用內(nèi)存。請記住,在此解決方案中,我添加了許多其他代碼,這些代碼無需使其正常工作,但可以在這種情況下使您的代碼更安全,更不易出錯(cuò)。


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

}

選項(xiàng)B-不使用內(nèi)存

該選項(xiàng)將產(chǎn)生相同的影響,就像控制器已經(jīng)在容器站點(diǎn)或容器已經(jīng)存在一樣,它不會嘗試構(gòu)建另一個(gè)站點(diǎn)。


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

}


查看完整回答
反對 回復(fù) 2021-05-06
  • 2 回答
  • 0 關(guān)注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號