Advertisement
Guest User

Scan9

a guest
Apr 24th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.07 KB | None | 0 0
  1. local NAME
  2. --local NAME = "SillyPutty"
  3. --local NAME = "NetherJedi"
  4. local ORES =
  5. {
  6.   diamond = "diamond_ore",
  7.   gold = "gold_ore",
  8.   emerald = "emerald_ore",
  9.   coal = "coal_ore",
  10.   iron_ingot = "iron_ore",
  11.   lapis = "lapis_ore",
  12.   redstone = "redstone_ore",
  13.   quartz = "quartz_ore",
  14.   glowstone_dust = "glowstone"
  15. }
  16. local COLOR = "0xFF00FF"
  17. local TRANS = "10"
  18. local SCALE = 1
  19.  
  20. User = function()
  21.   local p = peripheral.wrap("back")
  22.   local s = p.sense()
  23.   for i=1,#s do
  24.     if s[i].x == 0 and
  25.        s[i].y == 0 and
  26.        s[i].z == 0 then
  27.       return s[i].id,s[i].name
  28.     end
  29.   end
  30. end
  31.  
  32. Held = function()
  33.   local p = peripheral.wrap("back")
  34.   local meta = p.getMetaByName(NAME)
  35.   if meta.heldItem then
  36.     return meta.heldItem.getMetadata().name
  37.   else
  38.     return nil
  39.   end
  40. end
  41.  
  42. LoadOreList = function(oreListFile)
  43.   oreList = {}
  44.   f = fs.open(oreListFile,"r")
  45.   if f then
  46.     oreList = textutils.unserialise(
  47.       f.readAll())
  48.   else
  49.     print("Warning: Ore list not found")
  50.   end
  51.   return oreList
  52. end
  53.  
  54. Colormap = function(oreList,item)
  55.   map = {}
  56.   for i=1,#oreList do
  57.     if oreList[i].active then
  58.       map[oreList[i].blockname] =
  59.         {color = oreList[i].color,
  60.          name = oreList[i].name}
  61. --        tonumber(oreList[i].color .. TRANS)}
  62.       if oreList[i].metadata then
  63.         map[oreList[i].blockname].metadata =
  64.           oreList[i].metadata
  65.           --print(textutils.serialise(oreList[i]))
  66.           --sleep(1)
  67.       end
  68.     end
  69.   end
  70.   --held item
  71.   if item then
  72.     local nickname = item
  73.     local colon = string.find(item,":")
  74.     if colon then
  75.       nickname = string.sub(item,colon+1,-1)
  76.     else
  77.       item = "minecraft:" .. nickname
  78.     end
  79.     local ore = ORES[nickname]
  80.     if ore then
  81.       nickname = ore
  82.       item = "minecraft:" .. ore
  83.     end
  84.     --print(item)
  85.      
  86.     map[item] =
  87.     {
  88.       color = COLOR,
  89.       name = nickname
  90.     }
  91.       --tonumber(COLOR .. "00")}
  92.   end
  93.   return map
  94. end
  95.  
  96.  
  97. DoScan = function(colorMap)
  98.   local p = peripheral.wrap("back")
  99.   local data = p.scan()
  100.   local results = {}
  101.  
  102.  
  103.   --local nickname = item
  104.   --local colon = string.find(item,":")
  105.   --if colon then
  106.   --  nickname = string.sub(item,colon+1,-1)
  107.   --else
  108.   --  item = "minecraft:" .. nickname
  109.   --end
  110.   --local ore = ORES[nickname]
  111.   --if ore then
  112.   --  nickname = ore
  113.   --  item = "minecraft:" .. ore
  114.   --end
  115.   ----print(item)
  116.  
  117.   for i=1,#data do
  118.     local v = vector.new(data[i].x,data[i].y,data[i].z)
  119.     data[i].dist = v:length()
  120.     --if data[i].name == "minecraft:lava" then
  121.     --  print(textutils.serialise(data[i]))
  122.     --end
  123.     local x = colorMap[data[i].name]
  124.     if x then
  125.       if x.metadata == nil or
  126.           data[i].state.level == x.metadata then
  127.         results[#results+1] = data[i]
  128.         results[#results].color =
  129.           x.color
  130.         results[#results].name =
  131.           x.name  
  132.       end
  133.     --else if x then
  134.     --  print(textutils.serialise(colorMap))
  135.     end
  136.   end
  137.   return results
  138. end
  139.  
  140. --FaceBlock = function(b)
  141. --  if b and b.x and b.y and b.z then
  142. --    local x,y,z = b.x,b.y,b.z
  143. --    local rho = math.sqrt(x*x + z*z)
  144. --    local pitch = math.atan2(-y,rho)
  145. --    local yaw = math.atan2(-x,z)
  146. --    p.look(math.deg(yaw),math.deg(pitch))    
  147. --  end
  148. --end
  149.  
  150. --FindNearest = function(data)
  151. --  if type(data) ~= "table" or not #data then
  152. --    return nil
  153. --  end
  154. --  
  155. --  local nearest = data[1]
  156. --  for i=2,#data do
  157. --    if data[i].dist < nearest.dist then
  158. --      nearest = data[i]
  159. --    end
  160. --  end
  161. --  return nearest
  162. --end
  163.  
  164. DrawBlocks = function(data,canvas,offset)
  165.   if not offset then
  166.     offset = {x=0,y=0,z=0}
  167.   end
  168.  
  169.   --local p = peripheral.wrap("back")
  170.   --local glasses3D = p.canvas3d()
  171.   --glasses3D.clear()
  172.   --canvas.clear()
  173.  
  174.   if type(data) ~= "table" or not #data then
  175.     return nil
  176.   end
  177.  
  178.   --local offset = player.withinBlock
  179.   --local canvas = glasses3D.create(
  180.   --  {-offset.x,-offset.y,-offset.z})
  181.    
  182.   for i=1,#data do
  183.     local b1 = canvas.addBox(
  184.       data[i].x + offset.x,
  185.       data[i].y + offset.y,
  186.       data[i].z + offset.z,
  187.       1,1,1,
  188.       tonumber(data[i].color .. TRANS))
  189.       --0x00FF0010)
  190.     b1.setDepthTested(false)
  191.   end
  192.   return canvas
  193. end
  194.  
  195. DrawSummary = function(data)
  196.   local p = peripheral.wrap("back")
  197.   local canvas = p.canvas()
  198.   canvas.clear()
  199.   if type(data) ~= "table" or not #data then
  200.     return nil
  201.   end
  202.  
  203.   local x = {}
  204.  
  205.   for i=1,#data do
  206.     if x[data[i].name] then
  207.       x[data[i].name].num =
  208.         x[data[i].name].num + 1
  209.     else --make new table entry
  210.       x[data[i].name] =
  211.       {
  212.         num = 1,
  213.         color = data[i].color
  214.       }
  215.     end
  216.   end
  217.  
  218.   y = 1
  219.   for k,v in pairs(x) do
  220.     --k is the block name
  221.     --v is table with color and num
  222.     canvas.addText(
  223.       {x=1,y=y},
  224.       "" .. v.num .. " " .. k,
  225.       --tonumber("0xFFFFFF" .. "FF"),
  226.       tonumber(v.color .. "FF"),
  227.       size)
  228.     y = y + 8*SCALE
  229.   end
  230.  
  231. end
  232.  
  233. --Program starts here
  234. _,NAME = User()
  235. local oreList = LoadOreList("OreList.txt")
  236. local p = peripheral.wrap("back")
  237. local glasses3D = p.canvas3d()
  238. glasses3D.clear()
  239. --local lastPos = vector.new(gps.locate())
  240. local lastPos = vector.new({0,0,0})
  241. local held = Held()
  242. local changed = true
  243. local lastCanvas
  244. --local lastSearch =
  245. --{
  246. --  name = nil,
  247. --  num = 0
  248. --}
  249. while true do
  250.   --held = Held()
  251.   --if held and changed then
  252.   --local p = peripheral.wrap("back")
  253.   --local player = p.getMetaByName(NAME)
  254.   --local glasses3D = p.canvas3d()
  255.    
  256.   colorMap = Colormap(oreList,Held())
  257.   local results = DoScan(colorMap)
  258.   --local offset = p.getMetaByName(
  259.   --  NAME).withinBlock
  260.    
  261.   --local results = DoScan(colorMap)
  262.  
  263.   local pos1 = {}
  264.   pos1.x, pos1.y, pos1.z =
  265.     gps.locate()
  266.   local offset1 = {}
  267.   offset1.x = pos1.x - math.floor(pos1.x)
  268.   offset1.y = pos1.y - math.floor(pos1.y)
  269.   offset1.z = pos1.z - math.floor(pos1.z)
  270.  
  271.   --print(textutils.serialize(offset))
  272.   --print(textutils.serialize(offset1))
  273.  
  274.  
  275.   local canvas = glasses3D.create(--{0,0,0})
  276.     {-offset1.x,
  277.      -offset1.y,
  278.      -offset1.z})
  279.  
  280.   --local results = DoScan(colorMap)
  281.  
  282.   --local pos2 = {}
  283.   --pos2.x, pos2.y, pos2.z =
  284.   --  gps.locate()
  285.   --local offset2 = {}
  286.   --offset2.x = pos2.x - math.floor(pos2.x)
  287.   --offset2.y = pos2.y - math.floor(pos2.y)
  288.   --offset2.z = pos2.z - math.floor(pos2.z)
  289.  
  290.  
  291.   --if held ~= lastSearch.name or
  292.   --    #results ~= lastSearch.num then
  293.   --  print("Found "..#results.." "..held.."s")
  294.   --  lastSearch.name = held
  295.   --  lastSearch.num = #results
  296.   --end
  297.   if lastCanvas then
  298.     pcall(lastCanvas.clear)
  299.   end
  300.  
  301.   --offset.x = -offset.x
  302.   --offset.y = -offset.y
  303.   --offset.z = -offset.z
  304.  
  305.   suc,lastCanvas = pcall(
  306.       DrawBlocks,
  307.       results,
  308.       canvas)--,offset)
  309.  
  310.   if suc then
  311.     DrawSummary(results)
  312.   else
  313.     lastCanas = nil
  314.   end
  315.   --end
  316.   --changed = false
  317.   sleep(1)
  318.   --look for changes
  319.   --local newHeld = Held()
  320.   --if held ~= newHeld then
  321.   --  held = newHeld
  322.   --  changed = true
  323.   --end
  324. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement