xray.lua

by anonymous
226 days agolua
COPY
1
local block = ...
2
local scanSpeed = 0.5
3
local doHighLight = true
4
local findDamage =  ({...})[2]
5
local damage = ({...})[4]
6
local highlighter = colors.green
7
local customDisplay = ({...})[3]
8
if customDisplay == "nil" then customDisplay = block end
9
if not block or block == "" then error("use: find blockname",0) end
10
if type(damage) == "string" then damage = tonumber(damage) end
11
local findDamage = tonumber(findDamage)
12
if type(customDisplay) ~= "string" or customDisplay == "" then customDisplay = nil end
13
if not damage or damage == "" then damage = nil end
14
local per = peripheral.wrap("back")
15
if not per then error("missing modules! (glasses,scanner)",0) end
16
if not per.scan or not per.canvas then error("missing modules! (glasses,scanner)",0) end
17
local c = per.canvas()
18
local c3d = per.canvas3d()
19
local obj
20
c.clear()
21
local main = function()
22
    local rec = c.addRectangle(0,0,60,20)
23
    local text = c.addText({3,6},"")
24
    rec.setAlpha(122)
25
    rec.setColor(0,0,0)
26
    local function getClosest(tbl)
27
        local pyth = function(x,y,z)
28
            return math.sqrt(x^2+y^2+z^2)  
29
        end
30
        local result = {}
31
        for k,v in pairs(tbl) do
32
            result[k] = pyth(v.x,v.y,v.z)
33
        end
34
        local minv = math.min(table.unpack(result))
35
        local resultFinal
36
		local resIndex
37
        for k,v in pairs(tbl) do
38
            if pyth(v.x,v.y,v.z) == minv then
39
                resultFinal = v
40
				resIndex = k
41
                break
42
            end
43
        end
44
        return resultFinal, resIndex
45
	end
46
	local function conv(ins)
47
        local r,g,b = term.getPaletteColor(ins)
48
        return r*255,g*255,b*255
49
    end
50
    local blc
51
    local offset
52
	local stabilize = function()
53
		local x, y, z = gps.locate(0.2)
54
		if x then
55
			offset = {
56
				-((x%1)-0.5),
57
                -((y%1)-0.5),
58
				-((z%1)-0.5)
59
			}
60
		else
61
			offset = nil
62
		end
63
	end
64
	local scanBlocks = function()
65
		blc = per.scan()
66
	end
67
    while true do
68
        parallel.waitForAll(stabilize,scanBlocks)
69
		c3d.clear()
70
        local matches = {}
71
        for k,v in pairs(blc) do
72
			if type(findDamage) == "number" then
73
            	if v.name == block and v.metadata == findDamage then
74
                	table.insert(matches,v)
75
            	end
76
			else
77
				if v.name == block then
78
                	table.insert(matches,v)
79
            	end
80
			end
81
        end
82
		if not next(matches) then matches = 'no block "'..block..'" was found' end
83
		do
84
			local v,xs,ys,zs,c,str,usedIndex
85
			if type(matches) ~= "string" then
86
                local obj
87
                if offset then
88
                obj = c3d.create(offset)
89
                else
90
                obj = c3d.create()
91
                end
92
                local frame = obj.addFrame(vector.new())
93
                v, usedIndex = getClosest(matches)
94
                xs,ys,zs = tostring(v.x),tostring(v.y),tostring(v.z)
95
                c = xs.." "..ys.." "..zs   
96
                str = block.." found at: "..c
97
				for k,v in pairs(matches) do
98
					if k ~= usedIndex or not doHighLight then
99
                        
100
                        local item = frame.addItem({v.x,v.y,v.z},customDisplay or block)
101
                        
102
					end
103
				end
104
				local item = frame.addItem({v.x,v.y,v.z},customDisplay or block)
105
				if doHighLight then
106
                    local highlight = obj.addBox(v.x-0.6,v.y-0.6,v.z-0.6,1.2,1.2,1.2)
107
                    highlight.setDepthTested(false)
108
                    highlight.setColor(conv(highlighter))
109
                    highlight.setAlpha(64)
110
				end
111
				frame.setDepthTested(false)
112
			else
113
				str = matches
114
			end
115
            rec.setSize(#str*5+2,20)
116
            text.setText(str)
117
		end
118
        sleep(scanSpeed or 0.5)
119
    end
120
end
121
local ok,err = pcall(main)
122
local clear = function()
123
	if c then c.clear() end
124
	if c3d then c3d.clear() end
125
end
126
clear()
127
if not ok then
128
	error(err,0)
129
end
130