All files role.repairer.js

54.54% Statements 18/33
50% Branches 15/30
50% Functions 1/2
53.12% Lines 17/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 652x   2x       2x 1x 1x     2x   2x 2x     2x     2x   2x   1x 1x   1x             1x         1x                                               2x  
const roleRepairer = {
    run: function (creep) {
        Iif (creep.memory.repairing && creep.store[RESOURCE_ENERGY] === 0) {
            creep.memory.repairing = false;
            creep.say('⚡ harvest');
        }
        if (!creep.memory.repairing && creep.store.getFreeCapacity() === 0) {
            creep.memory.repairing = true;
            creep.say('🔧 repair');
        }
 
        if (creep.memory.repairing) {
            // ⚡ PERFORMANCE: Per-tick caching of damaged structures
            Eif (creep.room._damagedStructuresTick !== Game.time) {
                creep.room._damagedStructures = creep.room.find(FIND_STRUCTURES, {
                    filter: (s) => s.hits < s.hitsMax && s.structureType !== STRUCTURE_WALL,
                });
                creep.room._damagedStructuresTick = Game.time;
            }
 
            const targets = creep.room._damagedStructures;
 
            if (targets && targets.length > 0) {
                // ⚡ PERFORMANCE: Find target with minimum hits in O(N)
                let target = targets[0];
                let minHits = target.hits;
 
                for (let i = 1; i < targets.length; i++) {
                    if (targets[i].hits < minHits) {
                        target = targets[i];
                        minHits = target.hits;
                    }
                }
 
                Iif (creep.repair(target) === ERR_NOT_IN_RANGE) {
                    creep.moveTo(target, { visualizePathStyle: { stroke: '#ffff00' } });
                }
            } else {
                // 修理対象がない場合はアップグレード
                Iif (creep.upgradeController(creep.room.controller) === ERR_NOT_IN_RANGE) {
                    creep.moveTo(creep.room.controller, {
                        visualizePathStyle: { stroke: '#ffffff' },
                    });
                }
            }
        } else E{
            // エネルギーを採取
            // ⚡ PERFORMANCE: Per-tick caching of active sources (consistent across roles)
            if (creep.room._activeSourcesTick !== Game.time) {
                creep.room._activeSources = creep.room.find(FIND_SOURCES_ACTIVE);
                creep.room._activeSourcesTick = Game.time;
            }
            const sources = creep.room._activeSources;
 
            if (sources.length > 0) {
                if (creep.harvest(sources[0]) === ERR_NOT_IN_RANGE) {
                    creep.moveTo(sources[0], { visualizePathStyle: { stroke: '#ffaa00' } });
                }
            }
        }
    },
};
 
module.exports = roleRepairer;