Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.37 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. //These values aren’t very exciting, they just control what the minimap looks like
  6.  
  7. local size = 0.5
  8. local cellSize = 16
  9. local offsetX = 75
  10. local offsetY = 75
  11. //We end our configuration section by defining the ores we’re interested in and what colour we’ll draw them as. We define some ores as //having a higher priority, so large ore veins don’t mask smaller veins of more precious ores.
  12.  
  13. local ores = {
  14.     ["minecraft:diamond_ore"] = 10,
  15.     ["minecraft:emerald_ore"] = 10,
  16.     ["minecraft:gold_ore"] = 8,
  17.     ["minecraft:redstone_ore"] = 5,
  18.     ["minecraft:lapis_ore"] = 5,
  19.     ["minecraft:iron_ore"] = 2,
  20.     ["minecraft:coal_ore"] = 1
  21. }
  22.  
  23. local colours = {
  24.     ["minecraft:coal_ore"] = { 150, 150, 150 },
  25.     ["minecraft:iron_ore"] = { 255, 150, 50 },
  26.     ["minecraft:lava"] = { 150, 75, 0 },
  27.     ["minecraft:gold_ore"] = { 255, 255, 0 },
  28.     ["minecraft:diamond_ore"] = { 0, 255, 255 },
  29.     ["minecraft:redstone_ore"] = { 255, 0, 0 },
  30.     ["minecraft:lapis_ore"] = { 0, 50, 255 },
  31.     ["minecraft:emerald_ore"] = { 0, 255, 0 }
  32. }
  33. //Now let’s get into the interesting stuff! Let’s look for a neural interface and check we’ve got all the required modules.
  34.  
  35. local modules = peripheral.find("neuralInterface")
  36. if not modules then error("Must have a neural interface", 0) end
  37. if not modules.hasModule("plethora:scanner") then error("The block scanner is missing", 0) end
  38. if not modules.hasModule("plethora:glasses") then error("The overlay glasses are missing", 0) end
  39. //Now we’ve got our neural interface, let’s extract the canvas and ensure nothing else is on it.
  40.  
  41. local canvas = modules.canvas()
  42. canvas.clear()
  43. //We now need to set up our minimap. We create a 2D array of text objects around the player, each starting off displaying an empty
  44. string. If we find an ore, we’ll update their colour and text.
  45.  
  46. local block_text = {}
  47. local blocks = {}
  48. for x = -scannerRange, scannerRange, 1 do
  49.     block_text[x] = {}
  50.     blocks[x] = {}
  51.  
  52.     for z = -scannerRange, scannerRange, 1 do
  53.         block_text[x][z] = canvas.addText({ 0, 0 }, " ", 0xFFFFFFFF, size)
  54.         blocks[x][z] = { y = nil, block = nil }
  55.     end
  56. end
  57. //We also create a marker showing the current player’s location.
  58.  
  59. canvas.addText({ offsetX, offsetY }, "^", 0xFFFFFFFF, size * 2)
  60. //Our first big function is the scanner: this searches for ores near the player, finds the most important ones, and updates the block table.
  61.  
  62. local function scan()
  63.     while true do
  64.         local scanned_blocks = modules.scan()
  65. //For each nearby position, we search the y axis for interesting ores. We look for the one which has the highest priority and update the block information
  66.  
  67.         for x = -scannerRange, scannerRange do
  68.             for z = -scannerRange, scannerRange do
  69.                 local best_score, best_block, best_y = -1
  70.                 for y = -scannerRange, scannerRange do
  71. //The block scanner returns blocks in a flat array, so we index into it with this rather scary formulae.
  72.  
  73.                     local scanned = scanned_blocks[scannerWidth ^ 2 * (x + scannerRange) + scannerWidth * (y + scannerRange) + (z + scannerRange) + 1]
  74. //If there is a block here, and it’s more interesting than our previous ores, then let’s use that!
  75.  
  76.                     if scanned then
  77.                         local new_score = ores[scanned.name]
  78.                         if new_score and new_score > best_score then
  79.                             best_block = scanned.name
  80.                             best_score = new_score
  81.                             best_y = y
  82.                         end
  83.                     end
  84.                 end
  85.  
  86.                 -- Update our block table with this information.
  87.                 blocks[x][z].block = best_block
  88.                 blocks[x][z].y = best_y
  89.             end
  90.         end
  91. //We wait for some delay before starting again. This isn’t strictly needed, but helps reduce server load
  92.  
  93.         sleep(scanInterval)
  94.     end
  95. end
  96. //The render function takes our block information generated in the previous function and updates the text elements.
  97.  
  98. local function render()
  99.     while true do
  100. //If possible, we rotate the map using the current player’s look direction. If it’s not available, we’ll just use north as up.
  101.  
  102.         local meta = modules.getMetaOwner and modules.getMetaOwner()
  103.         local angle = meta and math.rad(-meta.yaw % 360) or math.rad(180)
  104. //Like before, loop over every nearby block and update something. Though this time we’re updating objects on the overlay canvas.
  105.  
  106.         for x = -scannerRange, scannerRange do
  107.             for z = -scannerRange, scannerRange do
  108.                 local text = block_text[x][z]
  109.                 local block = blocks[x][z]
  110.  
  111.                 if block.block then
  112. //If we’ve got a block here, we update the position of our text element to account for rotation,
  113.  
  114.                     local px = math.cos(angle) * -x - math.sin(angle) * -z
  115.                     local py = math.sin(angle) * -x + math.cos(angle) * -z
  116.  
  117.                     local sx = math.floor(px * size * cellSize)
  118.                     local sy = math.floor(py * size * cellSize)
  119.                     text.setPosition(offsetX + sx, offsetY + sy)
  120. //Then change the text and colour to match the location of the ore
  121.  
  122.                     text.setText(tostring(block.y))
  123.                     text.setColor(table.unpack(colours[block.block]))
  124.                 else
  125. //Otherwise we just make sure the text is empty. We don’t need to faff about with clearing the colour or position, as we’ll change it next iteration anyway.
  126.  
  127.                     text.setText(" ")
  128.                 end
  129.             end
  130.         end
  131.  
  132.         sleep(renderInterval)
  133.     end
  134. end
  135. //We now run our render and scan loops in parallel, continually updating our block list and redisplaying it to the wearer.
  136.  
  137. parallel.waitForAll(render, scan)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement