Advertisement
Sascha_T

Plethora Block Scanner

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