RoomPosition.isWalkable



  • RoomPosition.isWalkable()

    Can a creep walk into area?

    posDiag = RoomPosition(creep.pos.x +1, creep.pos.y+1, creep.pos.roomName);
    if (posDiag.isWalkable())
      creep.move(posDiag);
    else
    ... move somewhere else ...

    Either that or return an error ERR_TILE_NOT_WALKABLE error when trying to enter impassable terrain that does not cost a movement action.


  • Culture

    You can extend this yourself by extending the RoomPosition object.
    I would not want this to be part of the move action as it costs extra cputo calculate (get terrain, get buildings, check buildings, get creeps, check creeps) thisnis a lot of overhead.



  • Here is my effort, feel free to correct. Blame the bad formatting on the forum software.

    A working copy of this function would need to be updated with changes in game mechanics, like new structures. Which is partly why I think it should be a API function.

    isWalkable: function (pos) {
    var atPos = pos.look();
    var SWAMP = "swamp";
    var PLAIN = "plain";
    for ( var i = 0 ; i < atPos.length ; i++ )
    {
    switch (atPos[i].type) {
    case LOOK_CREEPS:
    case LOOK_SOURCES:
    case LOOK_MINERALS:
    case LOOK_NUKES:
    if (atPos[i][atPos[i].type] !== undefined)
    return false;
    break;
    case LOOK_TERRAIN:
    if (atPos[i].terrain != PLAIN && atPos[i].terrain != SWAMP)
    return false;
    break;
    case LOOK_STRUCTURES:
    if (atPos[i].structure.structureType == STRUCTURE_SPAWN
    || atPos[i].structure.structureType == STRUCTURE_EXTENSION
    || atPos[i].structure.structureType == STRUCTURE_WALL
    || atPos[i].structure.structureType == STRUCTURE_KEEPER_LAIR
    || atPos[i].structure.structureType == STRUCTURE_CONTROLLER
    || atPos[i].structure.structureType == STRUCTURE_LINK
    || atPos[i].structure.structureType == STRUCTURE_STORAGE
    || atPos[i].structure.structureType == STRUCTURE_TOWER
    || atPos[i].structure.structureType == STRUCTURE_POWER_BANK
    || atPos[i].structure.structureType == STRUCTURE_POWER_SPAWN
    || atPos[i].structure.structureType == STRUCTURE_EXTRACTOR
    || atPos[i].structure.structureType == STRUCTURE_LAB
    || atPos[i].structure.structureType == STRUCTURE_TERMINAL
    || atPos[i].structure.structureType == STRUCTURE_NUKER)
    return false;
    break;
    case LOOK_ENERGY:
    case LOOK_RESOURCES:
    case LOOK_FLAGS:
    case LOOK_CONSTRUCTION_SITES:
    default:
    }
    }
    return true;
    }


  • Culture

    I suggest you look into 

    OBSTACLE_OBJECT_TYPES

    From the constants file (in the manual) and

    room.lookForAt() in combination with

    LOOK_STRUCTURES
    and
    LOOK_CREEPS

     



  • Thanks for that I had not noticed OBSTACLE_OBJECT_TYPES.

     

    Now using belwo. As you see I am a fan of big ugly switch statments."

    https://gist.github.com/anonymous/bc4ccac0cea464d0e98a9a176035808f#file-gistfile1-txt

     



  • I know this is quite old, but the code @ayrtep came up with can be simplified a lot using lodash, and made more efficient by creating a lookup of obstacle types. This makes the function time complexity linear over the number of entities at the position (O(n)) instead of quadratic over the number of entities and the number of obstacle types (O(n2)).

    const isObstacle = _.transform(
        OBSTACLE_OBJECT_TYPES,
        (o, type) => { o[type] = true; },
        {}
    );
    
    function isEnterable(pos) {
        return _.every(pos.look(), item =>
            item.type === 'terrain' ?
            item.terrain !== 'wall' :
            !isObstacle[item.structureType]
        );
    }
    
    👌


  • @theincrediblewheelofcheese this does not check for creeps though (but maybe it shouldn't). An enemy Rampart is also not walkable. Also you can simplify it as item => item.terrain !== 'wall' && !isObstacle[item.structureType], if item is not a terrain, the corresponding property will be undefined which is not equal to 'wall' -- this was incorrect



  • @poma This is just a rewrite of OP's code. It has the same features and defects, but it performs better.

    @ayrtep said in RoomPosition.isWalkable:

    https://gist.github.com/anonymous/bc4ccac0cea464d0e98a9a176035808f#file-gistfile1-txt