TechManDylan

IskallOreFinder

Apr 22nd, 2021 (edited)
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.78 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. local ores = {
  12.     ["iskalliumreactors:iskallium_stone_ore"] = 10,
  13.     ["xo:xp_ore"] = 10,
  14. }
  15.  
  16. local colours = {
  17.     ["xo:xp_ore"] = { 150, 255, 150 },
  18.     ["iskalliumreactors:iskallium_stone_ore"] = { 0, 255, 0 }
  19. }
  20.  
  21. local modules = peripheral.find("neuralInterface")
  22. if not modules then error("Must have a neural interface", 0) end
  23. if not modules.hasModule("plethora:scanner") then error("The block scanner is missing", 0) end
  24. if not modules.hasModule("plethora:glasses") then error("The overlay glasses are missing", 0) end
  25.  
  26. local canvas = modules.canvas()
  27. canvas.clear()
  28.  
  29. local block_text = {}
  30. local blocks = {}
  31. for x = -scannerRange, scannerRange, 1 do
  32.     block_text[x] = {}
  33.     blocks[x] = {}
  34.  
  35.     for z = -scannerRange, scannerRange, 1 do
  36.         block_text[x][z] = canvas.addText({ 0, 0 }, " ", 0xFFFFFFFF, size)
  37.         blocks[x][z] = { y = nil, block = nil }
  38.     end
  39. end
  40.  
  41. canvas.addText({ offsetX, offsetY }, "^", 0xFFFFFFFF, size * 2)
  42.  
  43. local function scan()
  44.     while true do
  45.         local scanned_blocks = modules.scan()
  46.  
  47.         for x = -scannerRange, scannerRange do
  48.             for z = -scannerRange, scannerRange do
  49.                 local best_score, best_block, best_y = -1
  50.                 for y = -scannerRange, scannerRange do
  51.                
  52.                     local scanned = scanned_blocks[scannerWidth ^ 2 * (x + scannerRange) + scannerWidth * (y + scannerRange) + (z + scannerRange) + 1]
  53.                    
  54.                     if scanned then
  55.                         local new_score = ores[scanned.name]
  56.                         if new_score and new_score > best_score then
  57.                             best_block = scanned.name
  58.                             best_score = new_score
  59.                             best_y = y
  60.                         end
  61.                     end
  62.                 end
  63.  
  64.                 -- Update our block table with this information.
  65.                 blocks[x][z].block = best_block
  66.                 blocks[x][z].y = best_y
  67.             end
  68.         end
  69.        
  70.         sleep(scanInterval)
  71.     end
  72. end
  73.  
  74. local function render()
  75.     while true do
  76.  
  77.         local meta = modules.getMetaOwner and modules.getMetaOwner()
  78.         local angle = meta and math.rad(-meta.yaw % 360) or math.rad(180)
  79.        
  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.                
  87.                     local px = math.cos(angle) * -x - math.sin(angle) * -z
  88.                     local py = math.sin(angle) * -x + math.cos(angle) * -z
  89.  
  90.                     local sx = math.floor(px * size * cellSize)
  91.                     local sy = math.floor(py * size * cellSize)
  92.                     text.setPosition(offsetX + sx, offsetY + sy)
  93.                    
  94.                     text.setText(tostring(block.y))
  95.                     text.setColor(table.unpack(colours[block.block]))
  96.                 else
  97.                
  98.                     text.setText(" ")
  99.                 end
  100.             end
  101.         end
  102.  
  103.         sleep(renderInterval)
  104.     end
  105. end
  106.  
  107. parallel.waitForAll(render, scan)
  108.  
Add Comment
Please, Sign In to add comment