All files role.medic.js

66.66% Statements 28/42
44.44% Branches 16/36
100% Functions 2/2
65.85% Lines 27/41

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 802x   2x     2x 2x 2x       2x 2x 2x   2x     2x 2x 2x   2x     2x       2x 1x 1x     2x 1x   1x 1x     1x 1x                             1x 1x       1x                               2x  
const roleMedic = {
    run: function (creep) {
        creep.say('💊');
 
        // âš¡ PERFORMANCE: Per-tick caching of my creeps (using unique tick key to avoid collisions)
        Eif (creep.room._myCreepsTick !== Game.time) {
            creep.room._myCreeps = creep.room.find(FIND_MY_CREEPS);
            creep.room._myCreepsTick = Game.time;
        }
 
        // âš¡ PERFORMANCE: Per-tick caching of injured creeps
        Eif (creep.room._injuredCreepsTick !== Game.time) {
            creep.room._injuredCreeps = creep.room._myCreeps.filter((c) => c.hits < c.hitsMax);
            creep.room._injuredCreepsTick = Game.time;
        }
        const injured = creep.room._injuredCreeps;
 
        // âš¡ PERFORMANCE: Per-tick caching of active sources (shared 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;
 
        // State machine: Gather energy or Heal
        Iif (creep.memory.healing && creep.store[RESOURCE_ENERGY] === 0) {
            creep.memory.healing = false;
            creep.say('âš¡ harvest');
        }
        if (!creep.memory.healing && creep.store.getFreeCapacity() === 0) {
            creep.memory.healing = true;
            creep.say('💊 heal');
        }
 
        if (creep.memory.healing) {
            if (injured.length > 0) {
                // Optimized targeting: healing while moving
                const target = injured[0];
                Iif (creep.pos.isNearTo(target)) {
                    creep.heal(target);
                } else {
                    creep.rangedHeal(target);
                    creep.moveTo(target, { visualizePathStyle: { stroke: '#00ff00' } });
                }
            } else E{
                // No one to heal: Move to idle position
                const idlePos = creep.room.controller
                    ? creep.room.controller.pos
                    : new RoomPosition(25, 25, creep.room.name);
                if (!creep.pos.inRangeTo(idlePos, 3)) {
                    creep.moveTo(idlePos, {
                        visualizePathStyle: { stroke: '#ffffff', opacity: 0.2 },
                    });
                }
            }
        } else {
            // Gathering mode
            const canHarvest = creep.getActiveBodyparts(WORK) > 0;
            Iif (canHarvest && sources.length > 0) {
                if (creep.harvest(sources[0]) === ERR_NOT_IN_RANGE) {
                    creep.moveTo(sources[0], { visualizePathStyle: { stroke: '#ffaa00' } });
                }
            } else Iif (injured.length > 0) {
                // Secondary behavior: heal even if not in "healing" state if we have some energy
                const target = injured[0];
                if (creep.store[RESOURCE_ENERGY] > 0) {
                    if (creep.pos.isNearTo(target)) {
                        creep.heal(target);
                    } else {
                        creep.rangedHeal(target);
                        creep.moveTo(target, { visualizePathStyle: { stroke: '#00ff00' } });
                    }
                }
            }
        }
    },
};
 
module.exports = roleMedic;