View difference between Paste ID: dBQfsv3p and KepZTZTq
SHOW: | | - or go back to the newest paste.
1
------------------------------------------------------------------------------------------
2
-- ORE3D - Fully Immersive Augmented Reality X-RAY Vision for Ore Mining using Plethora --
3
------------------------------------------------------------------------------------------
4
5
-- CREATED BY:
6
--   HydroNitrogen (a.k.a. GoogleTech, Wendelstein7)
7
--   Bram S. (a.k.a ThatBram0101, bram0101)
8
9
-- LICENCE: ZLIB/libpng Licence (Zlib) (MODIFIED)
10
--   Copyright (c) 2018 HydroNitrogen & Bram S.
11
--   This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
12
--   Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
13
--   1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
14
--   2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
15
--   3. Every version, altered and original, must always contain a link to the following internet page: https://energetic.pw/computercraft/ore3d
16
--   4. This notice may not be removed or altered from any source distribution.
17
18
-- VERSION 2018-03-29 15:52 (Dates are way easier than version numbers, :P )
19
20
-- GLOBAL SETTINGS
21
-- Change the settings here to your likings.
22
local fov = math.rad(70) -- Change according to your Minecraft settings! Minecraft default: 75
23
local ar = 1.8 -- Aspect ratio of your view - for a full HD screen: fullscreen: 1.7777, windowed: 1.8
24
local hintToBehind = true -- Show all blocks behind you on the sides of your screen
25
local charSize = 16 -- the base size of the indicators
26
local maxOreGroups = 32 -- The max amount of ore groups/veins to show.
27
local maxOreGroupRadius = 2 -- Radius to look for adjecent ores to combine to group
28
29
-- PERFORMANCE OPTIONS: These affect how badly this program will use your CPU.
30
-- SinglePlayer reccommended values: scansPerSecond = 10, rendersPerSecond = 30
31
-- MultiPlayer reccommended values: scansPerSecond = 1, rendersPerSecond = 5
32
local scansPerSecond = 1 -- how many times per second should it scan for new blocks?
33
local rendersPerSecond = 5 -- how many times per second should it draw a new frame?
34
35
36
-- Initialising modules [ IF YOU ERROR HERE, THEN YOU PROBABLY MISS ONE OR MORE REQUIRED MODULES!]
37
-- Required modules: Overlay Glasses, Introspection Module, Block Scanner
38
local modules = peripheral.find("neuralInterface")
39
local modb = peripheral.wrap("back")
40
local can = modb.canvas()
41
can.clear()
42
43
44
-- Performance optimalisation precalculations and initialising
45
46
local renderDelay = 1 / rendersPerSecond
47
local scanDelay = 1 / scansPerSecond
48
49
local scrhor = (1 / math.tan(fov / 2)) / ar
50
local scrver = (1 / math.tan(fov / 2))
51
52
local oreGroups = { }
53
local oreTexts = { }
54
for i = 1,maxOreGroups do
55
    oreGroups[i] =  { ["x"]=0, ["y"]=0, ["z"]=0, ["color"]=0x0, ["amount"]=0 }
56
    oreTexts[i] = can.addText({0,0}, " ", 0xFFFFFF00, 1)
57
end
58
59
local checkedBlocks = { }
60
61
local cx, cy = can.getSize()
62
local cxhalf = cx / 2
63
local cyhalf = cy / 2
64
65
local blocksToShow = { -- Add any blocks here to show up with respective colors. Blocks with the same color will be considered as a group.
66
    ["minecraft:emerald_ore"] = 0x46FF26AA,
67
    ["minecraft:diamond_ore"] = 0x50F8FFAA,
68
    ["minecraft:gold_ore"] = 0xFFDF50AA,
69
    ["minecraft:redstone_ore"] = 0xCC121566,
70
    ["minecraft:lit_redstone_ore"] = 0xCC121566,
71
    ["minecraft:iron_ore"] = 0xFFAC8766,
72
    ["minecraft:lapis_ore"] = 0x0A107F66,
73
    ["minecraft:coal_ore"] = 0x20202066,
74
    ["quark:biotite_ore"] = 0x02051C66,
75
    ["minecraft:quartz_ore"] = 0xCCCCCC66,
76-
    ["minecraft:glowstone"] = 0xFFDFA166
76+
    ["thermalfoundation:ore:1"] = 0xFFDFA166
77
}
78
79
function rotate(x, y, z) -- Matrix operation: rotate (optimized, uses precalculated values)
80
    newx = ycos * x + ysin * z
81
    newz = -ysin * x + ycos * z
82
83
    newy = pcos * y - psin * newz
84
    newz = psin * y + pcos * newz
85
86
    return newx,newy,newz
87
end
88
89
function toPerspective(x, y, z) -- Convert to perspective projection, removes a dimention (z)  (optimized, uses precalculated values)
90
    x = scrhor * x / z
91
    y = scrver * y / z
92
93
    return x,y
94
end
95
96
function ndcToSpc(x, y)
97
    x = x * cxhalf + cxhalf
98
    y = y * cyhalf + cyhalf
99
100
    return x, y
101
end
102
103
function getCharSize(d) -- Calculates size of indicators using distance
104
    return charSize / d
105
end
106
107
function isOnScreen(x, y, d) -- determines if something is visible
108
    return ( x >= 1 and x - getCharSize(d) < cx ) and ( y >= 1 and y - getCharSize(d) < cy )
109
end
110
111
function getDistance(x, y, z) -- calculates distance. We love you, Pythagoras.
112
    return math.sqrt(x * x + y * y + z * z)
113
end
114
115
function drawOre(x, y, z, color, amount, n) -- draw function for an ore, combines the above matrix operations
116
    d = getDistance(x, y, z)
117
118
    x,y,z = rotate(x, y, z)
119
120
    if hintToBehind and z < 0 then z = 0.001 end
121
122
    if z >= 0 or hintToBehind then -- render only if point is visible OR hintToBehind is enabled
123
        x,y = toPerspective(x, y, -z)
124
        x,y = ndcToSpc(x, y)
125
126
        if not hintToBehind and not isOnScreen(x, y, d) then
127
            oreTexts[n].setText(" ")
128
            return -- don't render.
129
        end
130
131
        x = math.min(math.max(x, 1), cx - 10 * getCharSize(d))
132
        y = math.min(math.max(cy - y, 1), cy - 10 * getCharSize(d))
133
134
        oreTexts[n].setPosition(x, y)
135
        oreTexts[n].setText(tostring(amount))
136
        oreTexts[n].setScale(getCharSize(d))
137
        oreTexts[n].setColor(color)
138
    else
139
        oreTexts[n].setText(" ")
140
    end
141
end
142
143
function scan() -- Scan blocks and group them
144
    while true do
145
        blocks = modb.scan()
146
147
        for i = 1,maxOreGroups do
148-
            oreGroups[i] =  { ["x"]=0, ["y"]=0, ["z"]=0, ["color"]=0x0, ["amount"]=0 }
148+
            oreGroups[i] =  { ["x"]=0, ["y"]=0, ["z"]=0, ["color"]=0x0, ["amount"]=0, ["name"]="none" }
149
        end
150
151
        checkedBlocks = { }
152
153
        local n = 1
154
155
        local finished = false
156
157
158
        for x = -8,8 do
159
160
            if finished then break end
161
162
            for y = -8,8 do
163
164
                if finished then break end
165
166
                for z = -8,8 do
167
                    if n > maxOreGroups then
168
                        finished = true
169
                        break
170
                    end
171
172
                    if checkedBlocks[17^2 * (x + 8) + 17 * (y + 8) + (z + 8) + 1] == nil then
173
                        local block = blocks[17^2 * (x + 8) + 17 * (y + 8) + (z + 8) + 1]
174
                        local color = blocksToShow[block.name]
175
176
                        local blockb = false
177
                        local colorb = false
178
179
                        if color ~= nil then
180
                            local amount = 0
181
                            local xa,ya,za = 0,0,0
182
183
                            for xb = x-maxOreGroupRadius, x+maxOreGroupRadius do
184
                                for yb = y-maxOreGroupRadius, y+maxOreGroupRadius do
185
                                    for zb = z-maxOreGroupRadius, z+maxOreGroupRadius do
186
                                        if xb >= -8 and xb <= 8 and yb >= -8 and yb <= 8 and zb >= -8 and zb <= 8 and not checkedBlocks[17^2 * (xb + 8) + 17 * (yb + 8) + (zb + 8) + 1] then
187
                                            blockb = blocks[17^2 * (xb + 8) + 17 * (yb + 8) + (zb + 8) + 1]
188
                                            colorb = blocksToShow[blockb.name]
189
190
                                            if color == colorb then
191
                                                amount = amount + 1
192
                                                xa,ya,za = xa+xb,ya+yb,za+zb
193
                                                checkedBlocks[17^2 * (xb + 8) + 17 * (yb + 8) + (zb + 8) + 1] = true
194
                                            end
195
                                        end
196
                                    end
197
                                end
198
                            end
199
200
                            xa,ya,za = xa / amount, ya / amount, za / amount
201
202-
                            oreGroups[n] = { ["x"]=xa, ["y"]=ya, ["z"]=za, ["color"]=color, ["amount"]=amount }
202+
                            oreGroups[n] = { ["x"]=xa, ["y"]=ya, ["z"]=za, ["color"]=color, ["amount"]=amount, ["name"]=block.name}
203
204
                            n = n + 1
205
                        end
206
                    end
207
                end
208
            end
209
        end
210
        sleep(scanDelay)
211
    end
212
end
213
214
function render()
215
    while true do
216
        local meta = modules.getMetaByID("af2ab211-3315-4937-90c2-bc8da6cf3d70")
217
        local yaw = math.rad(meta.yaw)
218
        local pitch = math.rad(meta.pitch)
219
220
        ysin = math.sin(yaw)
221
        ycos = math.cos(yaw)
222
223
        psin = math.sin(pitch)
224
        pcos = math.cos(pitch)
225
226
        local block = false
227
228
        for n = 1, maxOreGroups do
229
            block = oreGroups[n]
230
            if block.amount > 0 then
231
                drawOre(block.x - meta.x + 0.5, -block.y + meta.y + 1, block.z - meta.z + 0.5, block.color, block.amount, n)
232
            else
233
                oreTexts[n].setText(" ")
234
            end
235
        end
236
237
        sleep(renderDelay)
238
    end
239
end
240
241
parallel.waitForAny(
242
    render,
243
    scan
244
)