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

72.13% Statements 44/61
48.07% Branches 25/52
33.33% Functions 1/3
72.13% Lines 44/61

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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 1212x 2x   2x     4x 4x       4x 1x 1x 1x   4x 1x 1x     4x   2x 2x 2x   2x   2x 1x 1x   1x   1x     1x 1x                 2x 2x             2x   2x   2x 1x 1x   1x   1x 1x     1x                 1x 1x         1x   1x   1x                     1x   1x           1x                     2x  
const gamification = require('gamification');
const vfx = require('visual.effects');
 
const roleHarvester = {
    run: function (creep) {
        // レインボートレイル
        Eif (Game.time % 2 === 0) {
            vfx.rainbowTrail(creep);
        }
 
        // エネルギーの満タンチェック
        if (creep.memory.harvesting && creep.store.getFreeCapacity() === 0) {
            creep.memory.harvesting = false;
            creep.say('📦 deliver');
            vfx.particles(creep.pos, '#FFD700', 15);
        }
        if (!creep.memory.harvesting && creep.store[RESOURCE_ENERGY] === 0) {
            creep.memory.harvesting = true;
            creep.say('⚡ harvest');
        }
 
        if (creep.memory.harvesting) {
            // ⚡ PERFORMANCE: Per-tick caching of active sources
            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;
 
            if (sources.length > 0) {
                const source = sources[0];
                const result = creep.harvest(source);
 
                if (result === OK) {
                    // 採取成功!
                    gamification.trackAction(creep, 'harvest');
 
                    // 偶数tickでエフェクト
                    Eif (Game.time % 5 === 0) {
                        vfx.particles(source.pos, '#FFFF00', 8);
                    }
                } else Eif (result === ERR_NOT_IN_RANGE) {
                    creep.moveTo(source, { visualizePathStyle: { stroke: '#ffaa00' } });
                }
            }
        } else {
            // エネルギーをスポーンまたはエクステンション・タワーに渡す
            // ⚡ PERFORMANCE: Per-tick caching of energy targets
            Eif (creep.room._energyTargetsTick !== Game.time) {
                creep.room._energyTargets = creep.room.find(FIND_STRUCTURES, {
                    filter: (s) =>
                        (s.structureType === STRUCTURE_SPAWN ||
                            s.structureType === STRUCTURE_EXTENSION ||
                            s.structureType === STRUCTURE_TOWER) &&
                        s.store.getFreeCapacity(RESOURCE_ENERGY) > 0,
                });
                creep.room._energyTargetsTick = Game.time;
            }
            const targets = creep.room._energyTargets;
 
            if (targets.length > 0) {
                const target = targets[0];
                const result = creep.transfer(target, RESOURCE_ENERGY);
 
                if (result === OK) {
                    // 配達成功!
                    vfx.scorePopup(creep.pos, 5, 'DELIVERY');
                    gamification.addXP(5, 'Energy delivery');
 
                    // ターゲットに星エフェクト
                    Iif (Game.time % 3 === 0) {
                        vfx.stars(target.pos, 4);
                    }
                } else Eif (result === ERR_NOT_IN_RANGE) {
                    creep.moveTo(target, { visualizePathStyle: { stroke: '#ffffff' } });
                }
            } else {
                // 満杯な時はコンテナに充電
                // ⚡ PERFORMANCE: Per-tick caching of container targets
                Eif (creep.room._containerTargetsTick !== Game.time) {
                    creep.room._containerTargets = creep.room.find(FIND_STRUCTURES, {
                        filter: (s) =>
                            s.structureType === STRUCTURE_CONTAINER &&
                            s.store.getFreeCapacity(RESOURCE_ENERGY) > 0,
                    });
                    creep.room._containerTargetsTick = Game.time;
                }
                const containers = creep.room._containerTargets;
 
                Iif (containers.length > 0) {
                    const target = containers[0];
                    const result = creep.transfer(target, RESOURCE_ENERGY);
 
                    if (result === OK) {
                        vfx.scorePopup(creep.pos, 3, 'STORAGE');
                    } else if (result === ERR_NOT_IN_RANGE) {
                        creep.moveTo(target, { visualizePathStyle: { stroke: '#ffffff' } });
                    }
                } else {
                    // それ以外はコントローラアップグレード
                    const result = creep.upgradeController(creep.room.controller);
 
                    Iif (result === OK) {
                        gamification.trackAction(creep, 'upgrade');
 
                        if (Game.time % 10 === 0) {
                            vfx.particles(creep.room.controller.pos, '#00FF00', 10);
                        }
                    } else Iif (result === ERR_NOT_IN_RANGE) {
                        creep.moveTo(creep.room.controller, {
                            visualizePathStyle: { stroke: '#ffffff' },
                        });
                    }
                }
            }
        }
    },
};
 
module.exports = roleHarvester;