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

62.74% Statements 32/51
39.21% Branches 20/51
20% Functions 1/5
65.3% Lines 32/49

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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 1022x   4x   4x     4x       4x   2x 2x               2x   2x   2x   2x     2x 2x 2x 2x           2x 2x                 2x 2x       2x   2x     2x 2x 2x   2x     2x   2x 2x   2x   2x                                       2x           2x  
const roleTransporter = {
    run: function (creep) {
        creep.say('🚚');
 
        Iif (creep.memory.transporting && creep.store[RESOURCE_ENERGY] === 0) {
            creep.memory.transporting = false;
        }
        Iif (!creep.memory.transporting && creep.store.getFreeCapacity() === 0) {
            creep.memory.transporting = true;
        }
 
        if (creep.memory.transporting) {
            // ⚡ PERFORMANCE: Per-tick caching of energy delivery targets
            Eif (creep.room._deliveryTargetsTick !== Game.time) {
                creep.room._deliveryTargets = creep.room.find(FIND_STRUCTURES, {
                    filter: (s) =>
                        (s.structureType === STRUCTURE_SPAWN ||
                            s.structureType === STRUCTURE_EXTENSION ||
                            s.structureType === STRUCTURE_TOWER ||
                            s.structureType === STRUCTURE_LAB) &&
                        s.store.getFreeCapacity(RESOURCE_ENERGY) > 0,
                });
                creep.room._deliveryTargetsTick = Game.time;
            }
            const targets = creep.room._deliveryTargets;
 
            if (targets.length > 0) {
                // ⚡ PERFORMANCE: ターゲットIDをキャッシュして毎ティックの再探索を回避
                let target = Game.getObjectById(creep.memory.deliveryTargetId);
 
                // キャッシュされたターゲットがまだ有効かチェック(存在し、空き容量があるか)
                Eif (!target || !targets.some((t) => t.id === target.id)) {
                    target = creep.pos.findClosestByRange(targets);
                    if (target) {
                        creep.memory.deliveryTargetId = target.id;
                    } else E{
                        delete creep.memory.deliveryTargetId;
                    }
                }
 
                Eif (target) {
                    Iif (creep.transfer(target, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
                        creep.moveTo(target, { visualizePathStyle: { stroke: '#00ffff' } });
                    }
                }
            } else E{
                delete creep.memory.deliveryTargetId;
            }
        } else {
            // ⚡ PERFORMANCE: Per-tick caching of withdrawal containers
            Eif (creep.room._withdrawalContainersTick !== Game.time) {
                creep.room._withdrawalContainers = creep.room.find(FIND_STRUCTURES, {
                    filter: (s) =>
                        s.structureType === STRUCTURE_CONTAINER && s.store[RESOURCE_ENERGY] > 0,
                });
                creep.room._withdrawalContainersTick = Game.time;
            }
            const containers = creep.room._withdrawalContainers;
 
            // ⚡ PERFORMANCE: 部屋全体の引き出し元リストをキャッシュして、クリープごとの重複計算を回避
            Eif (creep.room._withdrawalSourcesTick !== Game.time) {
                const storage = creep.room.storage;
                const sources = [];
 
                Iif (storage && storage.store[RESOURCE_ENERGY] > 1000) {
                    sources.push(storage);
                }
                sources.push(...containers);
 
                creep.room._withdrawalSources = sources;
                creep.room._withdrawalSourcesTick = Game.time;
            }
            const sources = creep.room._withdrawalSources;
 
            Iif (sources.length > 0) {
                // ⚡ PERFORMANCE: ターゲットIDをキャッシュ
                let target = Game.getObjectById(creep.memory.withdrawalTargetId);
 
                // キャッシュが有効かチェック(存在し、エネルギーがあるか)
                if (!target || !sources.some((s) => s.id === target.id)) {
                    target = creep.pos.findClosestByRange(sources);
                    if (target) {
                        creep.memory.withdrawalTargetId = target.id;
                    } else {
                        delete creep.memory.withdrawalTargetId;
                    }
                }
 
                if (target) {
                    if (creep.withdraw(target, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
                        creep.moveTo(target, { visualizePathStyle: { stroke: '#ffff00' } });
                    }
                }
            } else {
                delete creep.memory.withdrawalTargetId;
            }
        }
    },
};
 
module.exports = roleTransporter;