All files / my-screeps-repo role.explorer.js

50% Statements 14/28
36.36% Branches 8/22
100% Functions 1/1
50% Lines 14/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 572x     3x 2x     2x   2x 2x 2x             1x 1x     1x   1x 1x 1x                                                         2x  
const roleExplorer = {
    run: function (creep) {
        // メモリにターゲットの部屋がなければ設定(例: 隣の部屋)
        if (!creep.memory.targetRoom) {
            const exits = Game.map.describeExits(creep.room.name);
 
            // Check if exits is valid
            Eif (!exits || Object.keys(exits).length === 0) {
                // No exits available, stay in current room
                creep.say('🤔 No exits');
                creep.moveTo(25, 25);
                return;
            }
 
            const exitDir = Object.keys(exits)[0];
            creep.memory.targetRoom = exits[exitDir];
        }
 
        if (creep.room.name !== creep.memory.targetRoom) {
            const exitDir = creep.room.findExitTo(creep.memory.targetRoom);
 
            // Check if exit direction is valid
            Eif (exitDir === ERR_NO_PATH || exitDir === ERR_INVALID_ARGS) {
                // Cannot find exit, reset target
                creep.say('❌ No path');
                delete creep.memory.targetRoom;
                return;
            }
 
            const exit = creep.pos.findClosestByRange(exitDir);
 
            if (exit) {
                creep.moveTo(exit, { visualizePathStyle: { stroke: '#ffffff' } });
            } else {
                // No exit found, reset target
                delete creep.memory.targetRoom;
            }
        } else E{
            // 部屋に着いたら適当に動いて視界を確保
            creep.moveTo(25, 25);
            creep.say('👀 scouting');
 
            // After exploring, find new target
            if (creep.pos.x === 25 && creep.pos.y === 25) {
                const exits = Game.map.describeExits(creep.room.name);
                if (exits && Object.keys(exits).length > 0) {
                    // Pick a random exit
                    const exitDirs = Object.keys(exits);
                    const randomDir = exitDirs[Math.floor(Math.random() * exitDirs.length)];
                    creep.memory.targetRoom = exits[randomDir];
                }
            }
        }
    },
};
module.exports = roleExplorer;