-- Function to read a 32-bit integer from memory
function readInt(address)
    local value = 0
    if cheatengine.isPointer(address) then
        value = tonumber(cheatengine.readInteger(address))
    else
        value = tonumber(cheatengine.readInteger(cheatengine.resolvePointer(address)))
    end
    return value
end

-- Function to write a 32-bit integer to memory
function writeInt(address, value)
    if cheatengine.isPointer(address) then
        cheatengine.writeInteger(address, value)
    else
        cheatengine.writeInteger(cheatengine.resolvePointer(address), value)
    end
end

-- Function to perform an initial scan for a specific value
function initialScan(value, valueType, scanType)
    cheatengine.scanNew()
    cheatengine.setScanType(scanType)
    cheatengine.setValueType(valueType)
    cheatengine.setScanValue(value)
    cheatengine.firstScan()
    return cheatengine.getResultsCount() > 0
end

-- Function to narrow down the results
function narrowDownScan(value, valueType, scanType)
    cheatengine.setScanType(scanType)
    cheatengine.setValueType(valueType)
    cheatengine.setScanValue(value)
    cheatengine.nextScan()
    return cheatengine.getResultsCount() > 0
end

-- Function to find the enemy's position
function findEnemyPosition()
    -- Initial scan for the enemy's X position (replace with the actual value)
    local enemyXValue = 1000 -- Replace with the actual value
    if not initialScan(enemyXValue, "4 bytes", "Exact Value") then
        print("Failed to find initial enemy X position.")
        return nil, nil, nil
    end

    -- Narrow down the results (replace with the new value after moving the enemy)
    local newEnemyXValue = 1500 -- Replace with the new value
    if not narrowDownScan(newEnemyXValue, "4 bytes", "Changed Value") then
        print("Failed to narrow down enemy X position.")
        return nil, nil, nil
    end

    -- Get the address of the enemy's X position
    local enemyXAddress = cheatengine.getResult(0).address

    -- Read the enemy's Y and Z positions
    local enemyYAddress = enemyXAddress + 4
    local enemyZAddress = enemyXAddress + 8

    local enemyX = readInt(enemyXAddress)
    local enemyY = readInt(enemyYAddress)
    local enemyZ = readInt(enemyZAddress)

    return enemyX, enemyY, enemyZ, enemyXAddress
end

-- Function to find the player's aim position
function findPlayerAimPosition()
    -- Initial scan for the player's aim X position (replace with the actual value)
    local playerAimXValue = 2000 -- Replace with the actual value
    if not initialScan(playerAimXValue, "4 bytes", "Exact Value") then
        print("Failed to find initial player aim X position.")
        return nil, nil, nil
    end

    -- Narrow down the results (replace with the new value after moving the aim)
    local newPlayerAimXValue = 2500 -- Replace with the new value
    if not narrowDownScan(newPlayerAimXValue, "4 bytes", "Changed Value") then
        print("Failed to narrow down player aim X position.")
        return nil, nil, nil
    end

    -- Get the address of the player's aim X position
    local playerAimXAddress = cheatengine.getResult(0).address

    -- Read the player's aim Y and Z positions
    local playerAimYAddress = playerAimXAddress + 4
    local playerAimZAddress = playerAimXAddress + 8

    local playerAimX = readInt(playerAimXAddress)
    local playerAimY = readInt(playerAimYAddress)
    local playerAimZ = readInt(playerAimZAddress)

    return playerAimX, playerAimY, playerAimZ, playerAimXAddress
end

-- Function to calculate the aim offset
function calculateAimOffset(enemyX, enemyY, enemyZ, playerAimX, playerAimY, playerAimZ)
    -- Calculate the offset to aim at the enemy's head
    local offsetX = enemyX - playerAimX
    local offsetY = enemyY - playerAimY
    local offsetZ = enemyZ - playerAimZ

    -- Adjust the aim (this is a simple example, you may need to fine-tune the values)
    local newAimX = playerAimX + offsetX
    local newAimY = playerAimY + offsetY
    local newAimZ = playerAimZ + offsetZ

    return newAimX, newAimY, newAimZ
end

-- Main loop
while true do
    -- Find the enemy's position
    local enemyX, enemyY, enemyZ, enemyXAddress = findEnemyPosition()
    if not enemyX or not enemyY or not enemyZ then
        print("Failed to find enemy position. Retrying...")
        os.sleep(1)
        goto continue
    end

    -- Find the player's aim position
    local playerAimX, playerAimY, playerAimZ, playerAimXAddress = findPlayerAimPosition()
    if not playerAimX or not playerAimY or not playerAimZ then
        print("Failed to find player aim position. Retrying...")
        os.sleep(1)
        goto continue
    end

    -- Calculate the aim offset
    local newAimX, newAimY, newAimZ = calculateAimOffset(enemyX, enemyY, enemyZ, playerAimX, playerAimY, playerAimZ)

    -- Write the new aim position back to memory
    writeInt(playerAimXAddress, newAimX)
    writeInt(playerAimXAddress + 4, newAimY)
    writeInt(playerAimXAddress + 8, newAimZ)

    ::continue::
    -- Sleep to avoid high CPU usage
    os.sleep(0.01)
end  mportant Notes:
Values: You need to replace the placeholder values (1000, 1500, 2000, 2500) with automatic process to get them from the game " (replace with the actual value)" replace this with automatic process