Advertisement
HandieAndy

geoscan_logger.lua

Oct 4th, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.96 KB | Gaming | 0 0
  1. local ms = require("movescript")
  2. local geo = peripheral.find("geoScanner")
  3.  
  4. local MS_SETTINGS = {
  5.     debug=false,
  6.     destructive=true
  7. }
  8.  
  9. local function isLog(data)
  10.     if not data then return false end
  11.     local tags = data.tags
  12.     return tags["minecraft:logs"] == true
  13. end
  14.  
  15. local function getNearbyLogs(radius)
  16.     local cooldown = geo.getOperationCooldown("scanBlocks")
  17.     if cooldown > 0 then
  18.         cooldown = cooldown / 1000
  19.         print("Sleeping for "..cooldown.." seconds...")
  20.         os.sleep(cooldown)
  21.     end
  22.     local logs = {}
  23.     radius = radius or 16
  24.     for _,b in pairs(geo.scan(radius)) do
  25.         for _,t in pairs(b.tags) do
  26.             if t == "minecraft:block/minecraft:logs" then
  27.                 table.insert(
  28.                     logs,
  29.                     {x=b.x,y=b.y,z=b.z}
  30.                 )
  31.                 break
  32.             end
  33.         end
  34.     end
  35.     return logs
  36. end
  37.  
  38. local function getTreeLocations(logs)
  39.     local locations = {}
  40.     for _,log in pairs(logs) do
  41.         local exists = false
  42.         for _,loc in pairs(locations) do
  43.             if loc.x == log.x and loc.z == log.z then
  44.                 exists = true
  45.                 break
  46.             end
  47.         end
  48.         if not exists then
  49.             table.insert(locations,{x=log.x,z=log.z})
  50.         end
  51.     end
  52.     return locations
  53. end
  54.  
  55. local function getHighestLog(logs)
  56.     if #logs == 0 then return nil end
  57.     local maxY = -1000
  58.     for _,log in pairs(logs) do
  59.         if log.y > maxY then
  60.             maxY = log.y
  61.         end
  62.     end
  63.     return maxY
  64. end
  65.  
  66. local function moveAboveHighestTree(logs, loc, r)
  67.     local maxY = getHighestLog(logs)
  68.     if maxY == nil then return end
  69.     while maxY >= r do -- The highest log is at the top of scan height, so go up and keep scanning.
  70.         ms.run((maxY+1).."U", MS_SETTINGS)
  71.         loc.y = loc.y + (maxY+1)
  72.         logs = getNearbyLogs(r)
  73.         maxY = getHighestLog(logs)
  74.     end
  75.     if maxY > 0 then
  76.         ms.run((maxY+1).."U", MS_SETTINGS)
  77.         loc.y = loc.y + (maxY+1)
  78.     elseif maxY < 0 then
  79.         ms.run((maxY-1).."D", MS_SETTINGS)
  80.         loc.y = loc.y - (maxY-1)
  81.     end
  82. end
  83.  
  84. local function moveTo(target, loc)
  85.     print("Going to x="..target.x..", z="..target.z)
  86.     print("At x="..loc.x..", y="..loc.y..", z="..loc.z)
  87.     -- Navigate horizontally.
  88.     -- Assume we start North.
  89.     local dx = target.x - loc.x
  90.     local dz = target.z - loc.z
  91.     if dz < 0 then
  92.         print("Moving on -Z axis...")
  93.         ms.run(math.abs(dz).."F", MS_SETTINGS)
  94.         loc.z = loc.z + dz
  95.     elseif dz > 0 then
  96.         print("Moving on +Z axis...")
  97.         ms.run("2R"..dz.."F2R", MS_SETTINGS)
  98.         loc.z = loc.z + dz
  99.     end
  100.     if dx < 0 then
  101.         print("Moving on -X axis...")
  102.         ms.run("L"..math.abs(dx).."FR", MS_SETTINGS)
  103.         loc.x = loc.x + dx
  104.     elseif dx > 0 then
  105.         print("Moving on +X axis...")
  106.         ms.run("R"..dx.."FL", MS_SETTINGS)
  107.         loc.x = loc.x + dx
  108.     end
  109. end
  110.  
  111. local function chopDownTree()
  112.     local yDif = 0
  113.     local success,data = turtle.inspectDown()
  114.     while not success or data.tags["minecraft:logs"] or data.tags["minecraft:leaves"] do
  115.         ms.run("DgdD", MS_SETTINGS)
  116.         yDif = yDif + 1
  117.         success,data = turtle.inspectDown()
  118.     end
  119.     while yDif > 0 do
  120.         ms.run("U", MS_SETTINGS)
  121.         yDif = yDif - 1
  122.     end
  123. end
  124.  
  125. local function chopArea(r)
  126.     r = r or 16
  127. -- Assume we're facing North.
  128.     local logs = getNearbyLogs(r)
  129.     print("Found "..#logs.." log blocks.")
  130.     local trees = getTreeLocations(logs)
  131.     print("For "..#trees.." trees.")
  132.     local loc = {x=0,y=0,z=0,dir="N"}
  133.     print("Moving above the highest tree...")
  134.     moveAboveHighestTree(logs, loc, r)
  135.     for _,tree in pairs(trees) do
  136.         print("Going to tree ".._)
  137.         moveTo(tree, loc)
  138.         chopDownTree()
  139.     end
  140.     moveTo({x=0,z=0}, loc)
  141.     ms.run(loc.y.."D", MS_SETTINGS)
  142. end
  143.  
  144. chopArea(16)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement