psicubed

Untitled

May 11th, 2024
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.83 KB | None | 0 0
  1. -- Define ANSI color codes and setup
  2. local colors = {
  3.     Reset = colors.white,
  4.     Black = colors.black,
  5.     Red = colors.red,
  6.     Green = colors.green,
  7.     Yellow = colors.yellow,
  8.     Blue = colors.blue,
  9.     Magenta = colors.magenta,
  10.     Cyan = colors.cyan,
  11.     White = colors.white,
  12. }
  13.  
  14. local DEBUG_MODE = true  -- Toggle this to enable/disable debug messages
  15. local visited = {}  -- Map to track visited coordinates
  16. local x, y, z = 0, 0, 0 -- Current position of the turtle
  17. local facing = 0 -- Current facing of the turtle: 0=north, 1=east, 2=south, 3=west
  18.  
  19. -- Helper functions
  20. local function debugPrint(message, color)
  21.     if DEBUG_MODE then
  22.         print(colors[color] .. message .. colors.Reset)
  23.     end
  24. end
  25.  
  26. local function saveKnowledge(data, filename)
  27.     local file = fs.open(filename, "w")
  28.     if file then
  29.         file.write(textutils.serialize(data))
  30.         file.close()
  31.         debugPrint("Data saved to " .. filename, "Green")
  32.         return true
  33.     else
  34.         debugPrint("Failed to save data to " .. filename, "Red")
  35.         return false
  36.     end
  37. end
  38.  
  39. local function isVisited(x, y, z)
  40.     return visited[x .. "," .. y .. "," .. z] or false
  41. end
  42.  
  43. local function markVisited(x, y, z)
  44.     visited[x .. "," .. y .. "," .. z] = true
  45.     debugPrint("Marked visited: (" .. x .. ", " .. y .. ", " .. z .. ")", "Yellow")
  46. end
  47.  
  48. local function getNextMove()
  49.     -- Simple heuristic: prioritize unvisited directions
  50.     local directions = {
  51.         {dx = 1, dy = 0, dz = 0}, {dx = -1, dy = 0, dz = 0}, -- East, West
  52.         {dx = 0, dy = 1, dz = 0}, {dx = 0, dy = -1, dz = 0}, -- North, South
  53.         {dx = 0, dy = 0, dz = 1}, {dx = 0, dy = 0, dz = -1}  -- Up, Down
  54.     }
  55.  
  56.     for _, direction in ipairs(directions) do
  57.         local newX = x + direction.dx
  58.         local newY = y + direction.dy
  59.         local newZ = z + direction.dz
  60.         if not isVisited(newX, newY, newZ) then
  61.             return direction.dx, direction.dy, direction.dz
  62.         end
  63.     end
  64.     debugPrint("No unvisited moves available.", "Red")
  65.     return nil, nil, nil
  66. end
  67.  
  68. local function moveTurtle(dx, dy, dz)
  69.     -- Adjust the direction before moving
  70.     if dz ~= 0 then
  71.         if dz > 0 then turtle.up() else turtle.down() end
  72.     elseif dx ~= 0 then
  73.         while facing ~= (dx > 0 and 1 or 3) do
  74.             turtle.turnRight()
  75.             facing = (facing + 1) % 4
  76.         end
  77.         turtle.forward()
  78.     elseif dy ~= 0 then
  79.         while facing ~= (dy > 0 and 0 or 2) do
  80.             turtle.turnRight()
  81.             facing = (facing + 1) % 4
  82.         end
  83.         turtle.forward()
  84.     end
  85.     markVisited(x + dx, y + dy, z + dz)
  86. end
  87.  
  88. -- Main function to start scanning
  89. local function startScanning()
  90.     markVisited(x, y, z)  -- Mark the starting position as visited
  91.     while true do
  92.         local dx, dy, dz = getNextMove()
  93.         if dx or dy or dz then
  94.             moveTurtle(dx, dy, dz)
  95.         else
  96.             break  -- No moves available, finish scanning
  97.         end
  98.     end
  99.     saveKnowledge(visited, "visitedLocations.dat")
  100. end
  101.  
  102. -- Call to start scanning
  103. startScanning()
  104.  
  105.  
  106.  
  107. -- Function to fill the turtle's inventory
  108. local function fillInventory()
  109.     debugPrint("Filling inventory...", colors.Green)
  110.     for slot = 1, 16 do
  111.         turtle.select(slot)
  112.         turtle.suck()
  113.     end
  114.     debugPrint("Inventory filled.", colors.Green)
  115. end
  116.  
  117. -- Function to learn what blocks are in what slots
  118. local function learnInventory()
  119.     debugPrint("Learning inventory...", colors.Yellow)
  120.     local inventory = {}
  121.     for slot = 1, 16 do
  122.         local item = turtle.getItemDetail(slot)
  123.         if item then
  124.             inventory[slot] = item.name
  125.         end
  126.     end
  127.     debugPrint("Inventory learned.", colors.Yellow)
  128.     return inventory
  129. end
  130.  
  131. -- Function to save block names to an output file
  132. local function saveBlockNames(inventory)
  133.     debugPrint("Saving block names to output file...", colors.Blue)
  134.     local file = fs.open("output.txt", "w")
  135.     if file then
  136.         for slot, blockName in pairs(inventory) do
  137.             file.writeLine("Slot " .. slot .. ": " .. blockName)
  138.         end
  139.         file.close()
  140.     end
  141.     debugPrint("Block names saved to output file.", colors.Blue)
  142. end
  143.  
  144. -- Function to load block names from a file
  145. local function loadBlockNames(filename)
  146.     debugPrint("Loading block names from file...", colors.Magenta)
  147.     local blockNames = {}
  148.     local file = fs.open(filename, "r")
  149.     if file then
  150.         while true do
  151.             local line = file.readLine()
  152.             if line == nil then
  153.                 break
  154.             end
  155.             local slot, blockName = line:match("Slot (%d+): (.+)")
  156.             if slot and blockName then
  157.                 blockNames[tonumber(slot)] = blockName
  158.             end
  159.         end
  160.         file.close()
  161.     end
  162.     debugPrint("Block names loaded from file.", colors.Magenta)
  163.     return blockNames
  164. end
  165.  
  166. -- Function to assign each block a chest and save chest information
  167. local function assignChests(inventory)
  168.     debugPrint("Assigning chests...", colors.Magenta)
  169.     local chestInfo = {}
  170.     for slot, blockName in pairs(inventory) do
  171.         local chest = {
  172.             x = math.random(1, STORAGE_WIDTH),
  173.             y = math.random(1, STORAGE_HEIGHT),
  174.             z = math.random(1, STORAGE_LENGTH)
  175.         }
  176.         chestInfo[blockName] = chest
  177.     end
  178.     saveKnowledge(chestInfo, "chest_info.txt")
  179.     debugPrint("Chests assigned.", colors.Magenta)
  180. end
  181.  
  182. -- Function to place blocks in assigned chests
  183. local function placeBlocks(inventory, chestInfo)
  184.     debugPrint("Placing blocks in assigned chests...", colors.Red)
  185.     for slot, blockName in pairs(inventory) do
  186.         local chest = chestInfo[blockName]
  187.         if chest then
  188.             -- Go to the assigned chest location
  189.             -- Place the block in the chest
  190.             -- Update knowledge with new information
  191.         else
  192.             debugPrint("No chest assigned for block: " .. blockName, colors.Red)
  193.         end
  194.     end
  195.     debugPrint("Blocks placed in assigned chests.", colors.Red)
  196. end
  197.  
  198. -- Function to return the turtle to its initial position
  199. local function returnToInitialPosition()
  200.     -- Implement this function as per your requirements
  201. end
  202.  
  203. -- Function to check for items in the chest below and retrieve them
  204. local function checkAndRetrieveItems()
  205.     -- Implement this function as per your requirements
  206. end
  207.  
  208. -- Main function to control the turtle's actions
  209. local function main()
  210.     debugPrint("Starting program...", colors.Cyan)
  211.    
  212.     -- Perform initial startup task if needed
  213.     if not fs.exists("chest_locations.txt") then
  214.         initialStartup()
  215.     else
  216.         debugPrint("Chest locations file found. Skipping initial startup task.", colors.Cyan)
  217.     end
  218.    
  219.     -- Fill the turtle's inventory
  220.     fillInventory()
  221.  
  222.     -- Learn block names from an existing file
  223.     local blockNames = loadBlockNames("block_names.txt")
  224.  
  225.     -- Learn what blocks are in what slots
  226.     local inventory = learnInventory()
  227.  
  228.     -- Save block names to an output file
  229.     saveBlockNames(inventory)
  230.  
  231.     -- Assign each block a chest and save chest information
  232.     assignChests(inventory)
  233.  
  234.     -- Place blocks in assigned chests
  235.     local chestInfo = loadKnowledge("chest_info.txt")
  236.     placeBlocks(inventory, chestInfo)
  237.  
  238.     -- Check for restarts
  239.     while true do
  240.         local event, key = os.pullEvent("key")
  241.         if event == "key" and key == keys.r then
  242.             print("Restart detected. Do you want to 'Go to home' or 'Terminate'?")
  243.             local choice = io.read()
  244.             if choice == "Go to home" then
  245.                 returnToInitialPosition()
  246.             elseif choice == "Terminate" then
  247.                 return -- End the program
  248.             end
  249.         end
  250.     end
  251. end
  252.  
  253. -- Call the main function to start the process
  254. main()
  255.  
Add Comment
Please, Sign In to add comment