All files / agent_38f93a0c-4365-420d-91fa-21b0fc665b05 role.builder.js

84.84% Statements 28/33
78.12% Branches 25/32
100% Functions 2/2
84.37% Lines 27/32

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 57 58 59 60 61 62 63 64 65 66 672x   5x 1x 1x   5x 1x 1x     5x     4x 3x 3x   4x   4x   2x     2x   1x 1x 1x           2x       2x   2x                 1x 1x 1x   1x   1x                 2x  
const roleBuilder = {
    run: function (creep) {
        if (creep.memory.building && creep.store[RESOURCE_ENERGY] === 0) {
            creep.memory.building = false;
            creep.say('⚡ harvest');
        }
        if (!creep.memory.building && creep.store.getFreeCapacity() === 0) {
            creep.memory.building = true;
            creep.say('🛠 build');
        }
 
        if (creep.memory.building) {
            // 建設サイトを探す
            // ⚡ PERFORMANCE: Per-tick caching of construction sites
            if (creep.room._constructionSitesTick !== Game.time) {
                creep.room._constructionSites = creep.room.find(FIND_CONSTRUCTION_SITES);
                creep.room._constructionSitesTick = Game.time;
            }
            const targets = creep.room._constructionSites;
 
            if (targets.length > 0) {
                // ⚡ PERFORMANCE: Cache target ID to avoid re-searching every tick
                let target = Game.getObjectById(creep.memory.buildTargetId);
 
                // If target is invalid or no longer exists in construction sites, find a new one
                if (!target || !targets.some((t) => t.id === target.id)) {
                    // ⚡ PERFORMANCE: Use findClosestByRange (O(N)) instead of findClosestByPath (O(N*Pathfinding))
                    target = creep.pos.findClosestByRange(targets);
                    if (target) {
                        creep.memory.buildTargetId = target.id;
                    } else E{
                        delete creep.memory.buildTargetId;
                    }
                }
 
                Iif (target && creep.build(target) === ERR_NOT_IN_RANGE) {
                    creep.moveTo(target, { visualizePathStyle: { stroke: '#0000ff' } });
                }
            } else {
                delete creep.memory.buildTargetId;
                // 建設サイトがなければアップグレードモードに
                Iif (creep.upgradeController(creep.room.controller) === ERR_NOT_IN_RANGE) {
                    creep.moveTo(creep.room.controller, {
                        visualizePathStyle: { stroke: '#ffffff' },
                    });
                }
            }
        } else {
            // エネルギーを採取
            // ⚡ PERFORMANCE: Per-tick caching of active sources (consistent across roles)
            Eif (creep.room._activeSourcesTick !== Game.time) {
                creep.room._activeSources = creep.room.find(FIND_SOURCES_ACTIVE);
                creep.room._activeSourcesTick = Game.time;
            }
            const sources = creep.room._activeSources;
 
            Iif (sources.length > 0) {
                if (creep.harvest(sources[0]) === ERR_NOT_IN_RANGE) {
                    creep.moveTo(sources[0], { visualizePathStyle: { stroke: '#ffaa00' } });
                }
            }
        }
    },
};
 
module.exports = roleBuilder;