Advertisement
Alakazard12

Ore Scanner

Jan 8th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.14 KB | None | 0 0
  1. local scanInterval = 0.2
  2. local renderInterval = 0.05
  3. local scannerRange = 8
  4. local scannerWidth = scannerRange * 2 + 1
  5.  
  6. local size = 0.5
  7. local cellSize = 16
  8. local offsetX = 75
  9. local offsetY = 75
  10.  
  11.  
  12. local ores = {
  13.     ["minecraft:diamond_ore"] = 10,
  14.     ["minecraft:emerald_ore"] = 10,
  15.  
  16.     ["minecraft:gold_ore"] = 8,
  17.  
  18.     ["minecraft:redstone_ore"] = 5,
  19.     ["minecraft:lapis_ore"] = 5,
  20.     ["minecraft:iron_ore"] = 2,
  21.     ["minecraft:coal_ore"] = 1
  22. }
  23.  
  24. local colours = {
  25.     ["minecraft:coal_ore"] = { 150, 150, 150 },
  26.     ["minecraft:iron_ore"] = { 255, 150, 50 },
  27.     ["minecraft:lava"] = { 150, 75, 0 },
  28.     ["minecraft:gold_ore"] = { 255, 255, 0 },
  29.     ["minecraft:diamond_ore"] = { 0, 255, 255 },
  30.     ["minecraft:redstone_ore"] = { 255, 0, 0 },
  31.     ["minecraft:lapis_ore"] = { 0, 50, 255 },
  32.     ["minecraft:emerald_ore"] = { 0, 255, 0 }
  33. }
  34.  
  35.  
  36. local modules = peripheral.find("neuralInterface")
  37. if not modules then error("Must have a neural interface", 0) end
  38. if not modules.hasModule("plethora:scanner") then error("The block scanner is missing", 0) end
  39. if not modules.hasModule("plethora:glasses") then error("The overlay glasses are missing", 0) end
  40.  
  41.  
  42.  
  43. local canvas = modules.canvas()
  44. canvas.clear()
  45.  
  46.  
  47.  
  48. local block_text = {}
  49. local blocks = {}
  50. for x = -scannerRange, scannerRange, 1 do
  51.     block_text[x] = {}
  52.     blocks[x] = {}
  53.  
  54.     for z = -scannerRange, scannerRange, 1 do
  55.         block_text[x][z] = canvas.addText({ 0, 0 }, " ", 0xFFFFFFFF, size)
  56.         blocks[x][z] = { y = nil, block = nil }
  57.     end
  58. end
  59.  
  60.  
  61.  
  62. canvas.addText({ offsetX, offsetY }, "^", 0xFFFFFFFF, size * 2)
  63.  
  64.  
  65. local function scan()
  66.     while true do
  67.         local scanned_blocks = modules.scan()
  68.         for x = -scannerRange, scannerRange do
  69.             for z = -scannerRange, scannerRange do
  70.                 local best_score, best_block, best_y = -1
  71.                 for y = -scannerRange, scannerRange do
  72.                     local scanned = scanned_blocks[scannerWidth ^ 2 * (x + scannerRange) + scannerWidth * (y + scannerRange) + (z + scannerRange) + 1]
  73.                     if scanned then
  74.                         local new_score = ores[scanned.name]
  75.                         if new_score and new_score > best_score then
  76.                             best_block = scanned.name
  77.                             best_score = new_score
  78.                             best_y = y
  79.                         end
  80.                     end
  81.                 end
  82.  
  83.                 -- Update our block table with this information.
  84.                 blocks[x][z].block = best_block
  85.                 blocks[x][z].y = best_y
  86.             end
  87.         end
  88.         sleep(scanInterval)
  89.     end
  90. end
  91.  
  92. local function render()
  93.     while true do
  94.         local meta = modules.getMetaOwner and modules.getMetaOwner()
  95.         local angle = meta and math.rad(-meta.yaw % 360) or 0
  96.         for x = -scannerRange, scannerRange do
  97.             for z = -scannerRange, scannerRange do
  98.                 local text = block_text[x][z]
  99.                 local block = blocks[x][z]
  100.  
  101.                 if block.block then
  102.                     local px = math.cos(angle) * -x - math.sin(angle) * -z
  103.                     local py = math.sin(angle) * -x + math.cos(angle) * -z
  104.  
  105.                     local sx = math.floor(px * size * cellSize)
  106.                     local sy = math.floor(py * size * cellSize)
  107.                     text.setPosition(offsetX + sx, offsetY + sy)
  108.                     text.setText(tostring(block.y))
  109.                     text.setColor(table.unpack(colours[block.block]))
  110.                 else
  111.                     text.setText(" ")
  112.                 end
  113.             end
  114.         end
  115.  
  116.         sleep(renderInterval)
  117.     end
  118. end
  119.  
  120.  
  121. parallel.waitForAll(render, scan)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement