Advertisement
Guest User

Scan11

a guest
Apr 24th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.88 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. --local function IsMoving(metadata)
  196. --  if metadata and metadata.motionX and
  197. --     metadata.motionX == 0 and
  198. --     metadata.motionZ == 0 and
  199. --     metadata.motionY > -0.08 and
  200. --     metadata.motionY <= 0.01 then
  201. --     print("not moving")
  202. --     print(metadata.motionX)
  203. --    return false
  204. --  else
  205. --     print("moving")
  206. --    return true
  207. --  end
  208. --end
  209.  
  210. DrawSummary = function(data)
  211.   local p = peripheral.wrap("back")
  212.   local canvas = p.canvas()
  213.   canvas.clear()
  214.   if type(data) ~= "table" or not #data then
  215.     return nil
  216.   end
  217.  
  218.   local x = {}
  219.  
  220.   for i=1,#data do
  221.     if x[data[i].name] then
  222.       x[data[i].name].num =
  223.         x[data[i].name].num + 1
  224.     else --make new table entry
  225.       x[data[i].name] =
  226.       {
  227.         num = 1,
  228.         color = data[i].color
  229.       }
  230.     end
  231.   end
  232.  
  233.   y = 1
  234.   for k,v in pairs(x) do
  235.     --k is the block name
  236.     --v is table with color and num
  237.     canvas.addText(
  238.       {x=1,y=y},
  239.       "" .. v.num .. " " .. k,
  240.       --tonumber("0xFFFFFF" .. "FF"),
  241.       tonumber(v.color .. "FF"),
  242.       size)
  243.     y = y + 8*SCALE
  244.   end
  245.  
  246. end
  247.  
  248. --Program starts here
  249. ID,NAME = User()
  250. local oreList = LoadOreList("OreList.txt")
  251. local p = peripheral.wrap("back")
  252. local glasses3D = p.canvas3d()
  253. glasses3D.clear()
  254.  
  255. local home        --origin of canvas
  256. local offset = {} --used to make canvas
  257. local canvas      --the canvas
  258. local oldCanvas   --canvas to be cleared
  259. local pos         --gps position
  260. local lastPos     --previous gps position
  261.  
  262. while true do
  263.   pos = gps.locate()
  264.   if (pos-lastPos):dist() < 0.001 then
  265.    
  266.   end
  267.  
  268.   if not canvas or not IsMoving(meta) then
  269.     print("New Canvas")
  270.     home = vector.new(gps.locate())
  271.     offset.x = home.x - math.floor(home.x)
  272.     offset.y = home.y - math.floor(home.y)
  273.     offset.z = home.z - math.floor(home.z)
  274.     home.x = math.floor(home.x)
  275.     home.y = math.floor(home.y)
  276.     home.z = math.floor(home.z)
  277.     canvas = glasses3D.create(
  278.       {-offset.x,
  279.        -offset.y,
  280.        -offset.z})
  281.   end
  282.  
  283.   --obtain scan results
  284.   colorMap = Colormap(oreList,Held())
  285.   local results = DoScan(colorMap)
  286.  
  287.   --get corresponding gps data
  288.   pos = vector.new(gps.locate())
  289.   lastPos = vector.new(pos.x,pos.y,pos.z)
  290.   pos.x = math.floor(pos.x) - home.x
  291.   pos.y = math.floor(pos.y) - home.y
  292.   pos.z = math.floor(pos.z) - home.z
  293.  
  294.   --local results = DoScan(colorMap)
  295.   if lastCanvas then
  296.     pcall(lastCanvas.clear)
  297.   end
  298.   suc = pcall(DrawBlocks,results,canvas,pos)
  299.   lastCanvas = canvas
  300.  
  301.   if suc then
  302.     DrawSummary(results)
  303.   elseif not suc then
  304.     pcall(canvas.clear)
  305.     canvas = nil
  306.   end
  307.   sleep(1)
  308. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement