All files / agent_38f93a0c-4365-420d-91fa-21b0fc665b05 utils.emotions.js

57.25% Statements 75/131
46.66% Branches 35/75
80% Functions 12/15
58.26% Lines 67/115

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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298              3x                                                         3x                       3x 3x       12x 6x                       6x 6x               4x 4x 3x 2x             4x 4x 3x             4x                   4x   4x 4x             4x                       4x         5x   5x   5x 5x 1x     4x 4x   4x           4x 12x 3x 3x       4x 4x       4x           4x   4x       4x 4x       1x 1x       17x         17x   17x           17x 5x     17x   17x 85x         2x 2x   2x 1x 1x 1x                                   3x 3x   3x 2x 1x 1x 1x       2x                 2x                           2x                                                           3x  
/**
 * ๐Ÿ˜Š Creep Emotion System
 *
 * Creeps have emotions based on their experiences!
 * They express their feelings through emojis.
 */
 
const EMOTIONS = {
    HAPPY: '๐Ÿ˜Š',
    EXCITED: '๐Ÿคฉ',
    PROUD: '๐Ÿ˜Ž',
    LOVE: '๐Ÿ˜',
    ENERGETIC: 'โšก',
    SUCCESSFUL: 'โญ',
    WORKING: '๐Ÿ› ๏ธ',
    THINKING: '๐Ÿค”',
    FOCUSED: '๐ŸŽฏ',
    WALKING: '๐Ÿšถ',
    TIRED: '๐Ÿ˜ซ',
    CONFUSED: '๐Ÿ˜ต',
    WORRIED: '๐Ÿ˜Ÿ',
    HURT: '๐Ÿค•',
    STUCK: '๐Ÿ˜จ',
    HUNGRY: '๐Ÿ”',
    HARVESTING: 'โ›๏ธ',
    BUILDING: '๐Ÿ› ๏ธ',
    UPGRADING: 'โฌ†๏ธ',
    REPAIRING: '๐Ÿ”ง',
    HEALING: '๐Ÿ’Š',
    FIGHTING: 'โš”๏ธ',
    TRANSPORTING: '๐Ÿšš',
    BIRTHDAY: '๐ŸŽ‰',
    LEVELUP: '๐ŸŽ†',
    CELEBRATING: '๐ŸŽŠ',
};
 
const MOOD_LEVELS = {
    VERY_HAPPY: 5,
    HAPPY: 4,
    NEUTRAL: 3,
    SAD: 2,
    VERY_SAD: 1,
};
 
/**
 * Security: Limits for memory-intensive structures to prevent Memory DoS.
 * Screeps memory is limited to 2MB; unbounded arrays can crash the AI.
 */
const MAX_ACHIEVEMENTS = 10;
const MAX_ACHIEVEMENT_NAME_LENGTH = 100;
 
class EmotionSystem {
    static initialize(creep) {
        if (!creep.memory.emotions) {
            creep.memory.emotions = {
                mood: MOOD_LEVELS.NEUTRAL,
                lastEmotion: EMOTIONS.HAPPY,
                experiencePoints: 0,
                achievements: [],
                personalityTraits: this.generatePersonality(),
                birthTick: Game.time,
            };
        }
    }
 
    static generatePersonality() {
        const traits = ['cheerful', 'serious', 'energetic', 'calm', 'curious', 'determined'];
        return traits[Math.floor(Math.random() * traits.length)];
    }
 
    /**
     * ใ‚จใƒใƒซใ‚ฎใƒผใƒฌใƒ™ใƒซใซๅŸบใฅใๆ„Ÿๆƒ…ใ‚’ๅ–ๅพ—
     */
    static _getEnergyEmotion(creep) {
        const energyPercent =
            creep.store.getUsedCapacity(RESOURCE_ENERGY) / creep.store.getCapacity(RESOURCE_ENERGY);
        if (energyPercent < 0.1) return { emoji: EMOTIONS.HUNGRY, moodChange: -1 };
        if (energyPercent > 0.9) return { emoji: EMOTIONS.ENERGETIC, moodChange: 1 };
        return null;
    }
 
    /**
     * ๅฅๅบท็Šถๆ…‹ใซๅŸบใฅใๆ„Ÿๆƒ…ใ‚’ๅ–ๅพ—
     */
    static _getHealthEmotion(creep) {
        const healthPercent = creep.hits / creep.hitsMax;
        if (healthPercent < 0.5) return { emoji: EMOTIONS.HURT, moodChange: -2 };
        return null;
    }
 
    /**
     * ใ‚นใ‚ฟใƒƒใ‚ฏ็Šถๆ…‹ใซๅŸบใฅใๆ„Ÿๆƒ…ใ‚’ๅ–ๅพ—
     */
    static _getStuckEmotion(creep) {
        Iif (
            creep.memory.lastPos &&
            creep.pos.x === creep.memory.lastPos.x &&
            creep.pos.y === creep.memory.lastPos.y
        ) {
            creep.memory.stuckCounter = (creep.memory.stuckCounter || 0) + 1;
            if (creep.memory.stuckCounter > 3) {
                return { emoji: EMOTIONS.STUCK, moodChange: -1 };
            }
        } else {
            creep.memory.stuckCounter = 0;
        }
        creep.memory.lastPos = { x: creep.pos.x, y: creep.pos.y };
        return null;
    }
 
    /**
     * ใƒญใƒผใƒซใซๅŸบใฅใๆ„Ÿๆƒ…ใ‚’ๅ–ๅพ—
     */
    static _getRoleEmotion(creep) {
        switch (creep.memory.role) {
            case 'harvester':
                return creep.harvest ? EMOTIONS.HARVESTING : null;
            case 'builder':
                return creep.build ? EMOTIONS.BUILDING : null;
            case 'upgrader':
                return EMOTIONS.UPGRADING;
            case 'repairer':
                return EMOTIONS.REPAIRING;
            case 'medic':
                return EMOTIONS.HEALING;
            default:
                return null;
        }
    }
 
    static updateEmotion(creep) {
        this.initialize(creep);
 
        const emotions = creep.memory.emotions;
 
        const updateOffset = creep.name.length % 5;
        if ((Game.time + updateOffset) % 5 !== 0 && emotions.lastEmotion) {
            return emotions.lastEmotion;
        }
 
        let emoji = EMOTIONS.WORKING;
        let moodChange = 0;
 
        const emotionChecks = [
            this._getEnergyEmotion(creep),
            this._getHealthEmotion(creep),
            this._getStuckEmotion(creep),
        ];
 
        for (const result of emotionChecks) {
            if (result) {
                emoji = result.emoji;
                moodChange += result.moodChange;
            }
        }
 
        const roleEmoji = this._getRoleEmotion(creep);
        Iif (roleEmoji) {
            emoji = roleEmoji;
        }
 
        Iif (Game.time - emotions.birthTick === 1500) {
            emoji = EMOTIONS.BIRTHDAY;
            this.celebrate(creep, 'Lived 1500 ticks!');
            moodChange = 3;
        }
 
        emotions.mood = Math.max(1, Math.min(5, emotions.mood + moodChange));
 
        Iif (emotions.personalityTraits === 'cheerful' && Math.random() > 0.7) {
            emoji = EMOTIONS.HAPPY;
        }
 
        emotions.lastEmotion = emoji;
        return emoji;
    }
 
    static display(creep) {
        const emoji = this.updateEmotion(creep);
        creep.say(emoji, true);
    }
 
    static celebrate(creep, achievement) {
        Iif (!creep.memory.emotions.achievements) {
            creep.memory.emotions.achievements = [];
        }
 
        // Security: Truncate achievement name to avoid Memory DoS
        const sanitizedName = String(achievement).substring(0, MAX_ACHIEVEMENT_NAME_LENGTH);
 
        creep.memory.emotions.achievements.push({
            name: sanitizedName,
            tick: Game.time,
        });
 
        // Security: Immediate rotation to prevent Memory DoS
        if (creep.memory.emotions.achievements.length > MAX_ACHIEVEMENTS) {
            creep.memory.emotions.achievements.shift();
        }
 
        creep.memory.emotions.mood = MOOD_LEVELS.VERY_HAPPY;
 
        for (let i = 0; i < 5; i++) {
            creep.say(EMOTIONS.CELEBRATING, true);
        }
    }
 
    static getMoodDescription(creep) {
        this.initialize(creep);
        const mood = creep.memory.emotions.mood;
 
        if (mood >= 5) return 'Very Happy ๐Ÿ˜„';
        Iif (mood >= 4) return 'Happy ๐Ÿ˜Š';
        Iif (mood >= 3) return 'Neutral ๐Ÿ˜';
        Eif (mood >= 2) return 'Sad ๐Ÿ˜Ÿ';
        return 'Very Sad ๐Ÿ˜ญ';
    }
 
    static interact(creep1, creep2) {
        if (creep1.pos.inRangeTo(creep2, 1)) {
            this.initialize(creep1);
            this.initialize(creep2);
 
            creep1.memory.emotions.mood = Math.min(5, creep1.memory.emotions.mood + 0.5);
            creep2.memory.emotions.mood = Math.min(5, creep2.memory.emotions.mood + 0.5);
 
            creep1.say('๐Ÿ‘‹', true);
            creep2.say('๐Ÿ˜Š', true);
        }
    }
 
    static getPerformanceModifier(creep) {
        this.initialize(creep);
        const mood = creep.memory.emotions.mood;
 
        if (mood >= 5) return 1.1;
        if (mood >= 4) return 1.05;
        Iif (mood >= 3) return 1.0;
        Iif (mood >= 2) return 0.95;
        return 0.9;
    }
 
    static getStats() {
        const stats = {
            veryHappy: 0,
            happy: 0,
            neutral: 0,
            sad: 0,
            verySad: 0,
            total: 0,
        };
 
        for (const name in Game.creeps) {
            const creep = Game.creeps[name];
            this.initialize(creep);
            const mood = creep.memory.emotions.mood;
 
            if (mood >= 5) stats.veryHappy++;
            else if (mood >= 4) stats.happy++;
            else if (mood >= 3) stats.neutral++;
            else if (mood >= 2) stats.sad++;
            else stats.verySad++;
 
            stats.total++;
        }
 
        return stats;
    }
 
    static checkCreep(creepName) {
        const creep = Game.creeps[creepName];
        if (!creep) {
            console.log('โŒ Creep not found');
            return;
        }
 
        this.initialize(creep);
        const emotions = creep.memory.emotions;
 
        console.log('\n๐Ÿค– Creep Emotion Report');
        console.log('Name:', creepName);
        console.log('Mood:', this.getMoodDescription(creep));
        console.log('Current Emotion:', emotions.lastEmotion);
        console.log('Personality:', emotions.personalityTraits);
        console.log('Age:', Game.time - emotions.birthTick, 'ticks');
        console.log('Achievements:', emotions.achievements.length);
 
        if (emotions.achievements.length > 0) {
            console.log('\n๐Ÿ† Achievements:');
            emotions.achievements.forEach((a) => {
                console.log('-', a.name, '(tick', a.tick, ')');
            });
        }
    }
}
 
module.exports = EmotionSystem;