this is my full code '<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AI Agent Survival Simulation</title>
    <style>
        body {
            margin: 0;
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
            display: flex;
            flex-direction: column;
            align-items: center;
            height: 100vh;
        }
        #simulation-container {
            position: relative;
            width: 800px;
            height: 600px;
            background-color: #cce7ff;
            border: 5px solid #000;
            margin: 20px auto;
            overflow: hidden;
        }
        #agent {
            position: absolute;
            width: 20px;
            height: 20px;
            background-color: red;
            border-radius: 50%;
            z-index: 10;
        }
        .block {
            position: absolute;
            width: 20px;
            height: 20px;
            background-color: blue;
            border: 1px solid #000;
        }
        .food {
            position: absolute;
            width: 10px;
            height: 10px;
            background-color: green;
            border-radius: 50%;
        }
        #health-bar {
            width: 200px;
            height: 20px;
            background-color: #ddd;
            margin: 10px auto;
        }
        #health-bar-fill {
            height: 100%;
            background-color: #4CAF50;
            width: 100%;
            transition: width 0.3s ease-in-out;
        }
        #status {
            text-align: center;
            margin-top: 10px;
            font-size: 18px;
        }
        #countdown {
            font-size: 24px;
            text-align: center;
            margin-top: 10px;
        }
        #controls {
            margin-top: 20px;
        }
        button {
            font-size: 16px;
            margin: 0 10px;
            padding: 5px 10px;
        }
    </style>
</head>
<body>
    <div id="simulation-container">
        <div id="agent"></div>
    </div>
    <div id="countdown">Time Left: 120s</div>
    <div id="health-bar">
        <div id="health-bar-fill"></div>
    </div>
    <div id="status"></div>
    <div id="controls">
        <button id="start-btn">Start</button>
        <button id="pause-btn">Pause</button>
        <button id="reset-btn">Reset</button>
    </div>

    <script>
        // Constants
        const GRID_SIZE = 40;
        const CELL_WIDTH = 800 / GRID_SIZE;
        const CELL_HEIGHT = 600 / GRID_SIZE;
        const HEALTH_LOSS_HOT = 2;
        const HEALTH_LOSS_HOT_IN_SHELTER = 1;
        const HEALTH_GAIN_COOL = 0.5;
        const HEALTH_GAIN_FOOD = 20;
        const REWARD_LIFT_BLOCK = 10;
        const REWARD_DROP_BLOCK = 20;
        const REWARD_BUILD_SHELTER = 100;
        const SMOOTH_FACTOR = 0.2;

        // DOM Elements
        const container = document.getElementById('simulation-container');
        const agent = document.getElementById('agent');
        const healthBarFill = document.getElementById('health-bar-fill');
        const status = document.getElementById('status');
        const countdown = document.getElementById('countdown');
        const startBtn = document.getElementById('start-btn');
        const pauseBtn = document.getElementById('pause-btn');
        const resetBtn = document.getElementById('reset-btn');

        // Game State
        let agentX = 20;
        let agentY = 20;
        let targetX = agentX;
        let targetY = agentY;
        let health = 100;
        let foodCollected = 0;
        let isCarryingBlock = false;
        let carriedBlock = null;
        let rewardPoints = 0;
        let isHotCycle = false;
        let timeLeft = 120;
        let blocks = [];
        let foods = [];
        let shelterBlocks = [];
        let timeUnderShelter = 0;
        let consecutiveLowFoodCycles = 0;
        let isSimulationRunning = false;

        // Initialize the simulation
        function init() {
            createRandomBlocks(20);
            createRandomFood(12);
            updateAgentPosition();
            updateStatus();
        }

        // Create random blocks in the environment
        function createRandomBlocks(num) {
            for (let i = 0; i < num; i++) {
                const block = document.createElement('div');
                block.classList.add('block');
                let x, y;
                let attempts = 0;
                do {
                    x = Math.floor(Math.random() * GRID_SIZE);
                    y = Math.floor(Math.random() * GRID_SIZE);
                    attempts++;
                    if (attempts > 100) break;
                } while (isOccupied(x, y));
                block.style.left = x * CELL_WIDTH + 'px';
                block.style.top = y * CELL_HEIGHT + 'px';
                container.appendChild(block);
                blocks.push(block);
            }
        }

        // Create random food items in the environment
        function createRandomFood(num) {
            for (let i = 0; i < num; i++) {
                const food = document.createElement('div');
                food.classList.add('food');
                let x, y;
                let attempts = 0;
                do {
                    x = Math.floor(Math.random() * GRID_SIZE);
                    y = Math.floor(Math.random() * GRID_SIZE);
                    attempts++;
                    if (attempts > 100) break;
                } while (isOccupied(x, y));
                food.style.left = x * CELL_WIDTH + 'px';
                food.style.top = y * CELL_HEIGHT + 'px';
                container.appendChild(food);
                foods.push(food);
            }
        }

        // Check if a grid cell is occupied
        function isOccupied(x, y) {
            return blocks.some(block => 
                Math.abs(parseFloat(block.style.left) / CELL_WIDTH - x) < 1 &&
                Math.abs(parseFloat(block.style.top) / CELL_HEIGHT - y) < 1
            ) || foods.some(food => 
                Math.abs(parseFloat(food.style.left) / CELL_WIDTH - x) < 1 &&
                Math.abs(parseFloat(food.style.top) / CELL_HEIGHT - y) < 1
            ) || (Math.floor(agentX) === x && Math.floor(agentY) === y);
        }

        // Move the agent towards a target position
        function moveAgent(dx, dy) {
            targetX = Math.max(0, Math.min(GRID_SIZE - 1, targetX + dx));
            targetY = Math.max(0, Math.min(GRID_SIZE - 1, targetY + dy));
        }

        // Update the agent's position smoothly
        function updateAgentPosition() {
            const lerp = (start, end, t) => start + (end - start) * t;

            agentX = lerp(agentX, targetX, SMOOTH_FACTOR);
            agentY = lerp(agentY, targetY, SMOOTH_FACTOR);

            agent.style.left = agentX * CELL_WIDTH + 'px';
            agent.style.top = agentY * CELL_HEIGHT + 'px';

            if (isCarryingBlock && carriedBlock) {
                carriedBlock.style.left = agent.style.left;
                carriedBlock.style.top = agent.style.top;
            }
        }

        // Build an enclosed shelter if possible
        function buildEnclosedShelter() {
            const shelterPositions = [
                {x: Math.floor(agentX) - 1, y: Math.floor(agentY)},
                {x: Math.floor(agentX) + 1, y: Math.floor(agentY)},
                {x: Math.floor(agentX), y: Math.floor(agentY) - 1},
                {x: Math.floor(agentX), y: Math.floor(agentY) + 1}
            ];

            const missingBlocks = shelterPositions.filter(pos => !isBlockAt(pos.x, pos.y));

            if (missingBlocks.length > 0) {
                if (!isCarryingBlock) {
                    let nearestBlock = findNearest(blocks, agentX, agentY);
                    if (nearestBlock) {
                        moveToward(nearestBlock);
                        if (isNear(agentX, agentY, nearestBlock)) {
                            pickUpBlock(nearestBlock);
                        }
                    } else {
                        moveRandomly();
                    }
                } else {
                    const targetPosition = missingBlocks[0];
                    moveTowardPosition(targetPosition.x, targetPosition.y);
                    if (Math.abs(agentX - targetPosition.x) < 0.1 && Math.abs(agentY - targetPosition.y) < 0.1) {
                        dropBlockAt(targetPosition.x, targetPosition.y);
                        shelterBlocks.push({x: targetPosition.x, y: targetPosition.y});
                        rewardPoints += REWARD_DROP_BLOCK;
                    }
                }
            }
        }

        // Find the nearest object (block or food)
        function findNearest(objects, x, y) {
            return objects.reduce((nearest, obj) => {
                const objX = parseFloat(obj.style.left) / CELL_WIDTH;
                const objY = parseFloat(obj.style.top) / CELL_HEIGHT;
                const distance = Math.hypot(objX - x, objY - y);
                return (!nearest || distance < nearest.distance) ? {obj, distance} : nearest;
            }, null)?.obj;
        }

        // Move towards a target object
        function moveToward(target) {
            const targetX = parseFloat(target.style.left) / CELL_WIDTH;
            const targetY = parseFloat(target.style.top) / CELL_HEIGHT;
            moveTowardPosition(targetX, targetY);
        }

        // Move towards a specific position on the grid
        function moveTowardPosition(targetX, targetY) {
            const dx = targetX - agentX;
            const dy = targetY - agentY;
            const stepX = Math.abs(dx) >= Math.abs(dy) ? Math.sign(dx) : 0;
            const stepY = Math.abs(dy) > Math.abs(dx) ? Math.sign(dy) : 0;
            moveAgent(stepX, stepY);
        }

        // Check if the agent is near a target object
        function isNear(x1, y1, obj) {
            const x2 = parseFloat(obj.style.left) / CELL_WIDTH;
            const y2 = parseFloat(obj.style.top) / CELL_HEIGHT;
            return Math.hypot(x1 - x2, y1 - y2) < 1;
        }

        // Agent picks up a block
        function pickUpBlock(block) {
            isCarryingBlock = true;
            carriedBlock = block;
            blocks = blocks.filter(b => b !== block);
            container.removeChild(block);
            rewardPoints += REWARD_LIFT_BLOCK;
            updateAgentPosition();
        }

        // Agent drops the carried block
        function dropBlockAt(x, y) {
            if (isCarryingBlock && carriedBlock) {
                carriedBlock.style.left = x * CELL_WIDTH + 'px';
                carriedBlock.style.top = y * CELL_HEIGHT + 'px';
                container.appendChild(carriedBlock);
                blocks.push(carriedBlock);
                isCarryingBlock = false;
                carriedBlock = null;
            }
        }

        // Check if there is a shelter block at a position
        function isBlockAt(x, y) {
            return shelterBlocks.some(block => block.x === x && block.y === y);
        }

        // Check if the agent is inside the shelter
        function isAgentInShelter() {
            const requiredPositions = [
                {x: Math.floor(agentX) - 1, y: Math.floor(agentY)},
                {x: Math.floor(agentX) + 1, y: Math.floor(agentY)},
                {x: Math.floor(agentX), y: Math.floor(agentY) - 1},
                {x: Math.floor(agentX), y: Math.floor(agentY) + 1}
            ];
            return requiredPositions.every(pos => isBlockAt(pos.x, pos.y));
        }

        // AI decision-making process
        function aiDecision() {
            const priorities = [
                { condition: () => health < 30, action: findAndEatFood },
                { condition: () => isHotCycle && !isAgentInShelter(), action: buildEnclosedShelter },
                { condition: () => foods.length === 0, action: findAndEatFood },
                { condition: () => health < 70, action: findAndEatFood },
                { condition: () => timeLeft < 30 && !isAgentInShelter(), action: buildEnclosedShelter },
                { condition: () => blocks.length > 0 && shelterBlocks.length < 4, action: collectBlocks },
                { condition: () => true, action: moveRandomly }
            ];

            for (const { condition, action } of priorities) {
                if (condition()) {
                    action();
                    break;
                }
            }
        }

        // Agent moves randomly
        function moveRandomly() {
            const dx = Math.floor(Math.random() * 3) - 1;
            const dy = Math.floor(Math.random() * 3) - 1;
            moveAgent(dx, dy);
        }

        // Update the agent's health
        function updateHealth() {
            if (isHotCycle) {
                if (isAgentInShelter()) {
                    timeUnderShelter++;
                    if (timeUnderShelter >= 80) {
                        health = Math.max(0, health - HEALTH_LOSS_HOT_IN_SHELTER);
                    }
                } else {
                    health = Math.max(0, health - HEALTH_LOSS_HOT);
                }
            } else {
                health = Math.min(100, health + HEALTH_GAIN_COOL);
                timeUnderShelter = 0;
            }
            healthBarFill.style.width = health + '%';

            if (health <= 0) {
                endSimulation('Game Over! Agent died.');
            }
        }

        // Agent finds and eats food
        function findAndEatFood() {
            let nearestFood = findNearest(foods, agentX, agentY);
            if (nearestFood) {
                moveToward(nearestFood);
                if (isNear(agentX, agentY, nearestFood)) {
                    eatFood(nearestFood);
                }
            } else {
                moveRandomly(); // If no food is found, move randomly
            }
        }

        // Agent collects blocks for shelter
        function collectBlocks() {
            let nearestBlock = findNearest(blocks, agentX, agentY);
            if (nearestBlock) {
                moveToward(nearestBlock);
                if (isNear(agentX, agentY, nearestBlock)) {
                    pickUpBlock(nearestBlock);
                }
            } else {
                moveRandomly(); // If no blocks are available, move randomly
            }
        }

        // Agent eats a food item
        function eatFood(food) {
            container.removeChild(food);
            foods = foods.filter(f => f !== food);
            foodCollected++;
            health = Math.min(100, health + HEALTH_GAIN_FOOD);
            rewardPoints += 5; // Added reward for eating food
            updateStatus();
        }

        // Toggle between hot and cool cycles
        function toggleTemperatureCycle() {
            isHotCycle = !isHotCycle;
            container.style.backgroundColor = isHotCycle ? '#ffcccc' : '#cce7ff'; // Change background color
            if (!isHotCycle) {
                if (foodCollected < 9) {
                    consecutiveLowFoodCycles++;
                    if (consecutiveLowFoodCycles >= 5) {
                        endSimulation('Game Over! Agent starved.');
                    }
                } else {
                    consecutiveLowFoodCycles = 0;
                }
                foodCollected = 0;
                createRandomFood(12); // Regenerate food
            }
            updateStatus();
        }

        // Update the countdown timer
        function updateCountdown() {
            countdown.textContent = `Time Left: ${Math.round(timeLeft)}s`;
            timeLeft -= 1 / 60; // Approximately 60 frames per second
            if (timeLeft <= 0) {
                toggleTemperatureCycle();
                timeLeft = 120;
            }
        }

        // Update the status display
        function updateStatus() {
            status.textContent = `Health: ${Math.round(health)} | Food Collected: ${foodCollected} | Reward Points: ${rewardPoints} | Temperature: ${isHotCycle ? 'Hot' : 'Cool'}`;
        }

        // Reset the simulation
        function resetSimulation() {
            // Reset agent
            agentX = targetX = 20;
            agentY = targetY = 20;
            health = 100;
            foodCollected = 0;
            rewardPoints = 0;
            isCarryingBlock = false;
            carriedBlock = null;

            // Reset environment
            timeLeft = 120;
            isHotCycle = false;
            shelterBlocks = [];
            timeUnderShelter = 0;
            consecutiveLowFoodCycles = 0;

            // Remove existing blocks and foods
            blocks.forEach(block => container.removeChild(block));
            foods.forEach(food => container.removeChild(food));
            blocks = [];
            foods = [];

            // Re-initialize the simulation
            createRandomBlocks(20);
            createRandomFood(12);
            updateAgentPosition();
            updateStatus();
            
            // Reset UI
            container.style.backgroundColor = '#cce7ff';
            healthBarFill.style.width = '100%';
        }

        // End the simulation
        function endSimulation(message) {
            isSimulationRunning = false;
            alert(message);
            resetSimulation();
        }

        // Main simulation loop using requestAnimationFrame
        let lastTime = 0;
        const fixedDeltaTime = 1000 / 60; // 60 FPS
        let accumulator = 0;

        function simulationLoop(currentTime) {
            if (!isSimulationRunning) return;

            const deltaTime = currentTime - lastTime;
            lastTime = currentTime;
            accumulator += deltaTime;

            while (accumulator >= fixedDeltaTime) {
                updateCountdown();
                updateHealth();
                aiDecision();
                updateAgentPosition();
                accumulator -= fixedDeltaTime;
            }

            updateStatus();
            requestAnimationFrame(simulationLoop);
        }

        // Event listeners for controls
        startBtn.addEventListener('click', () => {
            if (!isSimulationRunning) {
                isSimulationRunning = true;
                lastTime = performance.now();
                requestAnimationFrame(simulationLoop);
            }
        });

        pauseBtn.addEventListener('click', () => {
            isSimulationRunning = false;
        });

        resetBtn.addEventListener('click', () => {
            isSimulationRunning = false;
            resetSimulation();
        });

        // Initialize the simulation
        init();

        // Optional: Add event listener for manual reset using 'R' key
        document.addEventListener('keydown', (event) => {
            if (event.key === 'r' || event.key === 'R') {
                isSimulationRunning = false;
                resetSimulation();
            }
        });
    </script>
</body>
</html>' and this is my fix '// AI decision-making process
function aiDecision() {
    const priorities = [
        { condition: () => health < 30, action: findAndEatFood },
        { condition: () => isHotCycle && !isAgentInShelter(), action: buildEnclosedShelter },
        { condition: () => foods.length > 0 && health < 70, action: findAndEatFood },
        { condition: () => timeLeft < 30 && !isAgentInShelter(), action: buildEnclosedShelter },
        { condition: () => blocks.length > 0 && shelterBlocks.length < 4, action: collectBlocks },
        { condition: () => true, action: moveRandomly }
    ];

    for (const { condition, action } of priorities) {
        if (condition()) {
            action();
            break;
        }
    }
}

// Agent finds and eats food
function findAndEatFood() {
    let nearestFood = findNearest(foods, agentX, agentY);
    if (nearestFood) {
        moveToward(nearestFood);
        if (isNear(agentX, agentY, nearestFood)) {
            eatFood(nearestFood);
        }
    } else {
        moveRandomly();
    }
}

// Agent eats a food item
function eatFood(food) {
    if (foods.includes(food)) {  // Check if the food still exists
        container.removeChild(food);
        foods = foods.filter(f => f !== food);
        foodCollected++;
        health = Math.min(100, health + HEALTH_GAIN_FOOD);
        rewardPoints += 5;
        updateStatus();
    }
}

// Build an enclosed shelter if possible
function buildEnclosedShelter() {
    const shelterPositions = [
        {x: Math.floor(agentX) - 1, y: Math.floor(agentY)},
        {x: Math.floor(agentX) + 1, y: Math.floor(agentY)},
        {x: Math.floor(agentX), y: Math.floor(agentY) - 1},
        {x: Math.floor(agentX), y: Math.floor(agentY) + 1}
    ];

    const missingBlocks = shelterPositions.filter(pos => !isBlockAt(pos.x, pos.y));

    if (missingBlocks.length > 0) {
        if (!isCarryingBlock) {
            let nearestBlock = findNearest(blocks, agentX, agentY);
            if (nearestBlock) {
                moveToward(nearestBlock);
                if (isNear(agentX, agentY, nearestBlock)) {
                    pickUpBlock(nearestBlock);
                }
            } else {
                moveRandomly();
            }
        } else {
            const targetPosition = missingBlocks[0];
            moveTowardPosition(targetPosition.x, targetPosition.y);
            if (Math.abs(agentX - targetPosition.x) < 0.1 && Math.abs(agentY - targetPosition.y) < 0.1) {
                dropBlockAt(targetPosition.x, targetPosition.y);
                shelterBlocks.push({x: targetPosition.x, y: targetPosition.y});
                rewardPoints += REWARD_DROP_BLOCK;
            }
        }
    }
}

// Move towards a specific position on the grid
function moveTowardPosition(targetX, targetY) {
    const dx = targetX - agentX;
    const dy = targetY - agentY;
    const distance = Math.hypot(dx, dy);
    if (distance > 0.1) {
        const stepX = (dx / distance) * SMOOTH_FACTOR;
        const stepY = (dy / distance) * SMOOTH_FACTOR;
        moveAgent(stepX, stepY);
    }
}

// Update the agent's position smoothly
function updateAgentPosition() {
    agent.style.left = agentX * CELL_WIDTH + 'px';
    agent.style.top = agentY * CELL_HEIGHT + 'px';

    if (isCarryingBlock && carriedBlock) {
        carriedBlock.style.left = agent.style.left;
        carriedBlock.style.top = agent.style.top;
    }
}'. help me update the full code with my fix