local modem = peripheral.find("modem") or error("No modem found") modem.open(1) -- Open channel 1 for communication local nodes = {} local chunkMap = {} -- Function to load node IDs local function loadNodeIds() nodes = {} if fs.exists("ids.txt") then local file = fs.open("ids.txt", "r") for line in file.readLine do local nodeId, hierarchicalNum = line:match("(%d+)%s+(%d+)") if nodeId and hierarchicalNum then nodes[tonumber(hierarchicalNum)] = tonumber(nodeId) end end file.close() end end -- Function to load chunk map local function loadChunkMap() chunkMap = {} if fs.exists("chunklocation.txt") then local file = fs.open("chunklocation.txt", "r") for line in file.readLine do local filename, chunk, nodeId = line:match("(%S+)%s+(%S+)%s+(%S+)") if filename and chunk and nodeId then chunkMap[filename] = chunkMap[filename] or {} local chunkNum = tonumber(chunk:match("chunk(%d+)")) chunkMap[filename][chunkNum] = tonumber(nodeId) end end file.close() end end loadNodeIds() local nodeCount = 0 for _ in pairs(nodes) do nodeCount = nodeCount + 1 end if nodeCount == 0 then error("No storage nodes found. Make sure the storage nodes are running and connected.") end print("Found " .. nodeCount .. " storage node(s)") loadChunkMap() -- Function to send a request to a node local function sendRequest(nodeId, request) request.targetNode = nodeId modem.transmit(1, 1, request) local timer = os.startTimer(5) while true do local event, param1, param2, param3, param4, param5 = os.pullEvent() if event == "modem_message" and param4.id == request.id then return param4.response elseif event == "timer" and param1 == timer then error("Request timed out for node: " .. nodeId) end end end -- Advanced Dynamic Language Model with Enhanced Spatial AI and Pathfinding for CC: Tweaked local model = { wordTransitions = {}, layers = {}, complexIdeas = {}, selfImprovement = { learningRate = 0.1, adaptationRate = 0.01 }, spatialMemory = {}, position = {x = 0, y = 0, z = 0}, orientation = 0, -- 0: north, 1: east, 2: south, 3: west locations = {} } local wordList = {} local memory = {} local SAVE_FILE = "language_model.dat" local TRAINING_FILE = "trainingdata.txt" local exploring = false local function sendRequest(nodeId, request) request.targetNode = nodeId modem.transmit(1, 1, request) local timer = os.startTimer(5) while true do local event, param1, param2, param3, param4, param5 = os.pullEvent() if event == "modem_message" and param4.id == request.id then return param4.response elseif event == "timer" and param1 == timer then error("Request timed out for node: " .. nodeId) end end end -- Function to load the model from ndisk local function loadModel() if chunkMap[SAVE_FILE] then local content = {} for chunkNum, nodeId in pairs(chunkMap[SAVE_FILE]) do local response = sendRequest(nodeId, { id = os.getComputerID() .. "_" .. os.clock(), action = "read", path = SAVE_FILE, chunk = chunkNum }) if response.success then content[chunkNum] = response.content else error("Failed to read chunk " .. chunkNum .. " from node " .. nodeId) end end local data = table.concat(content) local loaded = textutils.unserialize(data) model.wordTransitions = loaded.wordTransitions or {} model.layers = loaded.layers or {} model.complexIdeas = loaded.complexIdeas or {} model.selfImprovement = loaded.selfImprovement or {learningRate = 0.1, adaptationRate = 0.01} model.spatialMemory = loaded.spatialMemory or {} model.position = loaded.position or {x = 0, y = 0, z = 0} model.orientation = tonumber(loaded.orientation) or 0 model.locations = loaded.locations or {} wordList = loaded.wordList or {} memory = loaded.memory or {} print("Model loaded from ndisk.") else print("No existing model found on ndisk. Starting fresh.") wordList = {"the", "be", "to", "of", "and", "a", "in", "that", "have", "I"} end end -- Function to save the model to ndisk local function saveModel() print("Saving model to ndisk...") local data = textutils.serialize({ wordTransitions = model.wordTransitions, layers = model.layers, complexIdeas = model.complexIdeas, selfImprovement = model.selfImprovement, spatialMemory = model.spatialMemory, position = model.position, orientation = model.orientation, locations = model.locations, wordList = wordList, memory = memory }) local nodeCount = 0 for _ in pairs(nodes) do nodeCount = nodeCount + 1 end local chunkSize = math.ceil(#data / nodeCount) chunkMap[SAVE_FILE] = {} local chunkNum = 1 for hierarchicalNum, nodeId in pairs(nodes) do local start = (chunkNum - 1) * chunkSize + 1 local chunk = data:sub(start, math.min(start + chunkSize - 1, #data)) local response = sendRequest(nodeId, { id = os.getComputerID() .. "_" .. os.clock(), action = "write", path = SAVE_FILE, chunk = chunkNum, content = chunk }) if not response.success then error("Failed to write chunk " .. chunkNum .. " to node " .. nodeId) end chunkMap[SAVE_FILE][chunkNum] = nodeId chunkNum = chunkNum + 1 end -- Save updated chunk map local file = fs.open("chunklocation.txt", "w") for filename, chunks in pairs(chunkMap) do for chunkNum, nodeId in pairs(chunks) do file.writeLine(filename .. " chunk" .. chunkNum .. " " .. nodeId) end end file.close() print("Model saved to ndisk.") end -- Function to tokenize input local function tokenize(input) if type(input) ~= "string" then return {} end local words = {} for word in input:gmatch("%w+") do table.insert(words, word:lower()) end return words end -- Function to update the model local function updateModel(words, modelTable) if type(words) ~= "table" or #words == 0 then return end for i = 1, #words - 1 do local currentWord = words[i] local nextWord = words[i + 1] if not modelTable.wordTransitions[currentWord] then modelTable.wordTransitions[currentWord] = {} end modelTable.wordTransitions[currentWord][nextWord] = (modelTable.wordTransitions[currentWord][nextWord] or 0) + 1 -- Add new words to wordList if not table.concat(wordList, " "):find(currentWord) then table.insert(wordList, currentWord) end if not table.concat(wordList, " "):find(nextWord) then table.insert(wordList, nextWord) end end end -- Function to generate a response with improved coherence and reduced hallucinations local function generateResponse(input, modelTable) local words = tokenize(input) local response = {} local currentWord = words[#words] or wordList[math.random(#wordList)] local maxWords = math.random(1, 15) -- Changed minimum to 1 local context = {} -- Keep track of recent words for context for i = 1, maxWords do if modelTable.wordTransitions[currentWord] then local totalWeight = 0 local weightedWords = {} for word, count in pairs(modelTable.wordTransitions[currentWord]) do totalWeight = totalWeight + (count or 0) table.insert(weightedWords, {word = word, weight = count or 0}) end if totalWeight > 0 then -- Use context to influence word selection local contextInfluence = 0.3 -- Adjust this value to change the influence of context local randomWeight = math.random() * (totalWeight + (#context * contextInfluence)) local cumulativeWeight = 0 for _, wordData in ipairs(weightedWords) do cumulativeWeight = cumulativeWeight + wordData.weight if context[wordData.word] then cumulativeWeight = cumulativeWeight + contextInfluence end if cumulativeWeight >= randomWeight then currentWord = wordData.word break end end else currentWord = wordList[math.random(#wordList)] end table.insert(response, currentWord) -- Update context context[currentWord] = (context[currentWord] or 0) + 1 if #response > 5 then -- Keep last 5 words in context local oldWord = response[#response - 5] context[oldWord] = (context[oldWord] or 1) - 1 if context[oldWord] <= 0 then context[oldWord] = nil end end else currentWord = wordList[math.random(#wordList)] table.insert(response, currentWord) end end return table.concat(response, " ") end -- Function to analyze patterns and predict improvements local function analyzePatterns(modelTable) local patterns = {} for word, transitions in pairs(modelTable.wordTransitions) do local sortedTransitions = {} for nextWord, weight in pairs(transitions) do table.insert(sortedTransitions, {word = nextWord, weight = weight}) end table.sort(sortedTransitions, function(a, b) return a.weight > b.weight end) if #sortedTransitions >= 3 then patterns[word] = { sortedTransitions[1].word, sortedTransitions[2].word, sortedTransitions[3].word } end end modelTable.predictedPatterns = patterns end -- Function to delete a saved location local function deleteLocation(name) if model.locations[name] then model.locations[name] = nil saveModel() -- Save the model to persist the change print("Location '" .. name .. "' has been deleted.") else print("Location '" .. name .. "' not found.") end end -- Function for self-improvement with pattern analysis local function selfImprove() model.selfImprovement.learningRate = model.selfImprovement.learningRate * (1 + model.selfImprovement.adaptationRate) analyzePatterns(model) -- Use predicted patterns to reinforce likely transitions for word, likelyNext in pairs(model.predictedPatterns) do for _, nextWord in ipairs(likelyNext) do if model.wordTransitions[word] and model.wordTransitions[word][nextWord] then model.wordTransitions[word][nextWord] = model.wordTransitions[word][nextWord] * 1.1 -- Increase weight by 10% end end end end -- Function to get coordinates in front of the turtle local function getFrontCoords() local x, y, z = model.position.x, model.position.y, model.position.z if model.orientation == 0 then -- North z = z - 1 elseif model.orientation == 1 then -- East x = x + 1 elseif model.orientation == 2 then -- South z = z + 1 else -- West x = x - 1 end return x, y, z end -- Function to move the turtle and update its position local function moveTurtle(direction) local success = false if direction == "forward" then success = turtle.forward() if success then local x, y, z = getFrontCoords() model.position.x, model.position.y, model.position.z = x, y, z end elseif direction == "back" then success = turtle.back() if success then local x, y, z = getFrontCoords() model.position.x, model.position.y, model.position.z = x, y, z end elseif direction == "up" then success = turtle.up() if success then model.position.y = model.position.y + 1 end elseif direction == "down" then success = turtle.down() if success then model.position.y = model.position.y - 1 end elseif direction == "left" then turtle.turnLeft() model.orientation = (model.orientation - 1) % 4 success = true elseif direction == "right" then turtle.turnRight() model.orientation = (model.orientation + 1) % 4 success = true end return success end -- Function to detect blocks and update spatial memory local function detectBlocks() local success, data = turtle.inspect() if success then local x, y, z = getFrontCoords() local key = x .. "," .. y .. "," .. z model.spatialMemory[key] = { name = data.name, metadata = data.metadata, explored = true } end end -- Function to create a new layer in the model local function createNewLayer(layerName, words, modelTable) if not modelTable.layers[layerName] then modelTable.layers[layerName] = {} end for i = 1, #words - 1 do local currentWord = words[i] local nextWord = words[i + 1] if not modelTable.layers[layerName][currentWord] then modelTable.layers[layerName][currentWord] = {} end modelTable.layers[layerName][currentWord][nextWord] = (modelTable.layers[layerName][currentWord][nextWord] or 0) + 1 end end -- Function to remember spatial context local function rememberSpatialContext() local context = {} for pos, data in pairs(model.spatialMemory) do table.insert(context, pos .. ": " .. data.name) end return table.concat(context, "\n") end -- Function to relate spatial memory to language model local function relateSpatialToLanguage() for pos, data in pairs(model.spatialMemory) do local words = tokenize(data.name) updateModel(words, model) createNewLayer("spatial_" .. pos, words, model) end print("Spatial data integrated into language model.") end -- Function to find unexplored directions local function findUnexploredDirections() local unexplored = {} local directions = {"forward", "left", "right", "up", "down"} for _, dir in ipairs(directions) do local x, y, z if dir == "forward" then x, y, z = getFrontCoords() elseif dir == "up" then x, y, z = model.position.x, model.position.y + 1, model.position.z elseif dir == "down" then x, y, z = model.position.x, model.position.y - 1, model.position.z else local tempOrientation = dir == "left" and (model.orientation - 1) % 4 or (model.orientation + 1) % 4 if tempOrientation == 0 then -- North x, y, z = model.position.x, model.position.y, model.position.z - 1 elseif tempOrientation == 1 then -- East x, y, z = model.position.x + 1, model.position.y, model.position.z elseif tempOrientation == 2 then -- South x, y, z = model.position.x, model.position.y, model.position.z + 1 else -- West x, y, z = model.position.x - 1, model.position.y, model.position.z end end local key = x .. "," .. y .. "," .. z if not model.spatialMemory[key] or not model.spatialMemory[key].explored then table.insert(unexplored, dir) end end return unexplored end -- A* Pathfinding Algorithm local function aStarPath(start, goal) local function heuristic(a, b) return math.abs(a.x - b.x) + math.abs(a.y - b.y) + math.abs(a.z - b.z) end local openSet = {} local closedSet = {} local cameFrom = {} local gScore = {[start] = 0} local fScore = {[start] = heuristic(start, goal)} table.insert(openSet, start) while #openSet > 0 do local current = openSet[1] for i = 2, #openSet do if fScore[openSet[i]] < fScore[current] then current = openSet[i] end end if current.x == goal.x and current.y == goal.y and current.z == goal.z then local path = {} while current do table.insert(path, 1, current) current = cameFrom[current] end return path end for i, v in ipairs(openSet) do if v == current then table.remove(openSet, i) break end end table.insert(closedSet, current) local neighbors = { {x = current.x + 1, y = current.y, z = current.z}, {x = current.x - 1, y = current.y, z = current.z}, {x = current.x, y = current.y + 1, z = current.z}, {x = current.x, y = current.y - 1, z = current.z}, {x = current.x, y = current.y, z = current.z + 1}, {x = current.x, y = current.y, z = current.z - 1} } for _, neighbor in ipairs(neighbors) do local inClosedSet = false for _, v in ipairs(closedSet) do if v.x == neighbor.x and v.y == neighbor.y and v.z == neighbor.z then inClosedSet = true break end end if not inClosedSet then local tentative_gScore = gScore[current] + 1 local inOpenSet = false for _, v in ipairs(openSet) do if v.x == neighbor.x and v.y == neighbor.y and v.z == neighbor.z then inOpenSet = true break end end if not inOpenSet then table.insert(openSet, neighbor) elseif tentative_gScore >= (gScore[neighbor] or math.huge) then goto continue end cameFrom[neighbor] = current gScore[neighbor] = tentative_gScore fScore[neighbor] = gScore[neighbor] + heuristic(neighbor, goal) ::continue:: end end end return nil -- No path found end -- Function to save a location local function saveLocation(name) model.locations[name] = {x = model.position.x, y = model.position.y, z = model.position.z} saveModel() -- Save the model immediately after adding a new location print("Location '" .. name .. "' saved.") end -- Function to go to a saved location local function gotoLocation(name) local coords = model.locations[name] if not coords then print("Location '" .. name .. "' not found.") return end local path = aStarPath(model.position, coords) if not path then print("No path found to '" .. name .. "'.") return end print("Path found. Moving to '" .. name .. "'...") for i, step in ipairs(path) do local dx = step.x - model.position.x local dy = step.y - model.position.y local dz = step.z - model.position.z -- Determine direction and move if dx ~= 0 then if dx > 0 then while model.orientation ~= 1 do -- Face East moveTurtle("right") end else while model.orientation ~= 3 do -- Face West moveTurtle("right") end end moveTurtle("forward") elseif dz ~= 0 then if dz > 0 then while model.orientation ~= 2 do -- Face South moveTurtle("right") end else while model.orientation ~= 0 do -- Face North moveTurtle("right") end end moveTurtle("forward") elseif dy ~= 0 then if dy > 0 then moveTurtle("up") else moveTurtle("down") end end print("Step " .. i .. " of " .. #path .. " completed.") end print("Arrived at '" .. name .. "'.") end -- Function to load training data local function loadTrainingData() if fs.exists(TRAINING_FILE) then local f = fs.open(TRAINING_FILE, "r") local data = f.readAll() f.close() if data and data ~= "" then local lines = {} for line in data:gmatch("[^\r\n]+") do table.insert(lines, line) end for i = 1, #lines, 2 do local prompt = lines[i] local response = lines[i + 1] if prompt and response then local promptWords = tokenize(prompt) local responseWords = tokenize(response) updateModel(promptWords, model) updateModel(responseWords, model) createNewLayer("prompt_" .. i, promptWords, model) createNewLayer("response_" .. i, responseWords, model) end end end end end -- Function to remember context local function rememberContext(input) table.insert(memory, input) if #memory > 10 then table.remove(memory, 1) end end -- Function to handle complex ideas (placeholder implementation) local function handleComplexIdea(idea, modelTable) modelTable.complexIdeas = modelTable.complexIdeas or {} modelTable.complexIdeas[idea] = (modelTable.complexIdeas[idea] or 0) + 1 end -- Function for reinforcement learning local function reinforcementLearning(rating, modelTable) local learningRate = model.selfImprovement.learningRate for i = 1, #memory do local words = tokenize(memory[i]) for j = 1, #words - 1 do local currentWord = words[j] local nextWord = words[j + 1] if modelTable.wordTransitions[currentWord] and modelTable.wordTransitions[currentWord][nextWord] then modelTable.wordTransitions[currentWord][nextWord] = modelTable.wordTransitions[currentWord][nextWord] * (1 + learningRate * (rating / 10 - 0.5)) end end end end -- Function to handle math expressions local function handleMath(input) local func, err = load("return " .. input) if func then local success, result = pcall(func) if success then return tostring(result) end end return "Error: Invalid math expression." end -- Main loop print("Advanced Dynamic Language Model with Enhanced Spatial AI and Pathfinding") print("Type 'exit' to quit, 'save' to save the model") loadNodeIds() local nodeCount = 0 for _ in pairs(nodes) do nodeCount = nodeCount + 1 end if nodeCount == 0 then error("No storage nodes found. Make sure the storage nodes are running and connected.") end print("Found " .. nodeCount .. " storage node(s)") loadChunkMap() loadModel() loadTrainingData() local function explorationTask() while true do if exploring then local unexploredDirections = findUnexploredDirections() if #unexploredDirections > 0 then local direction = unexploredDirections[math.random(#unexploredDirections)] if moveTurtle(direction) then detectBlocks() end else -- If surrounded by explored blocks, move randomly local directions = {"forward", "left", "right", "up", "down"} local direction = directions[math.random(#directions)] if moveTurtle(direction) then detectBlocks() end end -- Allow other processes to run os.sleep(0.1) if turtle.getFuelLevel() == 0 then print("Out of fuel. Exploration stopped.") exploring = false end else os.sleep(0.1) end end end -- Function to list saved locations local function listLocations() if not model.locations or next(model.locations) == nil then print("No locations saved.") return end print("Saved locations:") local index = 1 for name, coords in pairs(model.locations) do print(index .. ". " .. name .. " (" .. coords.x .. ", " .. coords.y .. ", " .. coords.z .. ")") index = index + 1 end end local function mainLoop() while true do write("> ") local input = read() if input:lower() == "exit" then saveModel() print("Goodbye!") break elseif input:lower() == "save" then saveModel() elseif input:lower() == "explore" then exploring = true print("Starting exploration. Type 'stop' to end.") elseif input:lower() == "stop" then exploring = false print("Exploration will stop after the current move.") elseif input:lower() == "list locations" then listLocations() elseif input:match("^save%s+(.+)$") then local name = input:match("^save%s+(.+)$") saveLocation(name) elseif input:match("^goto%s+(.+)$") then local name = input:match("^goto%s+(.+)$") gotoLocation(name) elseif input:match("^delete%s+(.+)$") then local name = input:match("^delete%s+(.+)$") deleteLocation(name) elseif input:match("^rate%s+(%d+)$") then local rating = tonumber(input:match("^rate%s+(%d+)$")) if rating and rating >= 1 and rating <= 10 then reinforcementLearning(rating, model) print("Thank you for your rating!") else print("Invalid rating. Please use a number between 1 and 10.") end elseif input:match("^%d+[%+%-%*/%^]%d+$") then local result = handleMath(input) print("Math: " .. result) elseif input:match("^move%s+(%w+)$") then local direction = input:match("^move%s+(%w+)$") if moveTurtle(direction) then print("Moved " .. direction) detectBlocks() else print("Failed to move " .. direction) end elseif input:match("^inspect$") then detectBlocks() print("Inspected block at current position.") elseif input:match("^remember%s+spatial$") then local context = rememberSpatialContext() print("Spatial Memory:\n" .. context) else local words = tokenize(input) updateModel(words, model) rememberContext(input) handleComplexIdea(input, model) local response = generateResponse(input, model) print("Model: " .. response) selfImprove() end end end parallel.waitForAny(explorationTask, mainLoop)