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

58.97% Statements 23/39
52.63% Branches 20/38
50% Functions 1/2
57.89% Lines 22/38

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 67 68 69 70 71 72 73 74 75 76 772x   2x       2x 1x 1x     2x   2x 2x     2x   2x   2x   1x     1x   1x 1x   1x             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 (Shared across roles)
            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: Cache target ID to avoid redundant O(N) scans every tick
                let target = Game.getObjectById(creep.memory.repairTargetId);
 
                // If target is invalid or fully repaired, find a new one
                Eif (!target || target.hits === target.hitsMax) {
                    // ⚡ PERFORMANCE: Find target with minimum hits in O(N)
                    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;
                        }
                    }
 
                    if (target) {
                        creep.memory.repairTargetId = target.id;
                    } else E{
                        delete creep.memory.repairTargetId;
                    }
                }
 
                Iif (target && creep.repair(target) === ERR_NOT_IN_RANGE) {
                    creep.moveTo(target, { visualizePathStyle: { stroke: '#ffff00' } });
                }
            } else {
                delete creep.memory.repairTargetId;
                // 修理対象がない場合はアップグレード
                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;