All files / my-screeps-repo utils.ai.js

91.17% Statements 31/34
70.83% Branches 17/24
100% Functions 3/3
91.17% Lines 31/34

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 881x   7x 3x           7x 7x 7x   7x 1x     6x 6x 6x   6x 1x     5x 1x     4x 1x     3x 3x             1x   1x                     1x       2x   2x     2x       2x 2x 2x   2x                     2x       1x  
const AIHelper = {
    getAIDecision(room) {
        if (!Memory.aiState) {
            Memory.aiState = {
                phase: 'expansion',
                priority: 'energy',
            };
        }
 
        const creeps = room.find(FIND_MY_CREEPS);
        const structures = room.find(FIND_MY_STRUCTURES);
        const hostiles = room.find(FIND_HOSTILE_CREEPS);
 
        if (hostiles.length > 0) {
            return { phase: 'defense', priority: 'survival' };
        }
 
        const rcl = room.controller ? room.controller.level : 0;
        const energyRatio = room.energyAvailable / room.energyCapacityAvailable;
        const constructionSites = room.find(FIND_CONSTRUCTION_SITES);
 
        if (rcl < 3) {
            return { phase: 'early_game', priority: 'expansion' };
        }
 
        if (energyRatio < 0.5) {
            return { phase: 'farming', priority: 'energy' };
        }
 
        if (constructionSites.length > 5) {
            return { phase: 'building', priority: 'construction' };
        }
 
        Eif (rcl < 8) {
            return { phase: 'growth', priority: 'upgrade' };
        }
 
        return { phase: 'endgame', priority: 'optimization' };
    },
 
    suggestCreepCount(room) {
        const decision = this.getAIDecision(room);
 
        const suggestions = {
            early_game: { harvester: 5, upgrader: 2, builder: 1, repairer: 0 },
            farming: { harvester: 6, upgrader: 1, builder: 1, repairer: 0 },
            expansion: { harvester: 4, upgrader: 2, builder: 2, repairer: 1 },
            building: { harvester: 3, upgrader: 1, builder: 3, repairer: 1 },
            growth: { harvester: 2, upgrader: 4, builder: 1, repairer: 1 },
            defense: { harvester: 1, upgrader: 0, builder: 0, repairer: 0 },
            endgame: { harvester: 1, upgrader: 2, builder: 1, repairer: 1 },
            optimization: { harvester: 1, upgrader: 1, builder: 1, repairer: 1 },
        };
 
        return suggestions[decision.phase] || suggestions.expansion;
    },
 
    shouldBuildStructure(room) {
        const decision = this.getAIDecision(room);
 
        Iif (decision.phase === 'defense') {
            return false;
        }
        Iif (decision.phase === 'early_game') {
            return false;
        }
 
        const structures = room.find(FIND_MY_STRUCTURES);
        const structureCount = structures.length;
        const rcl = room.controller ? room.controller.level : 0;
 
        const maxStructures = {
            1: 0,
            2: 5,
            3: 10,
            4: 20,
            5: 30,
            6: 40,
            7: 50,
            8: 60,
        };
 
        return structureCount < (maxStructures[rcl] || 0);
    },
};
 
module.exports = AIHelper;