-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminingClient.lua
More file actions
157 lines (142 loc) · 4.91 KB
/
miningClient.lua
File metadata and controls
157 lines (142 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
---@class miningLib
-- Settings:
-- - MiningSettings
miningLib = {}
---@type scm
local scm = require("./scm")
---@type Scanner
local scanner = scm:load("scanner")
---@type turtleController
local tC = scm:load("turtleController")
---@type HelperFunctions
local helper = scm:load("helperFunctions")
---@type TurtleResourceManager
local tResourceManager = scm:load("turtleResourceManager")
-- DEFINITIONS
---@class MiningSettings
---@field miningDepth number
---@field miningHight number
---@field miningDiameter number
---@field scanRadius number
local miningSettings = {}
local postionmappingTable = {
["X"] = 0,
["Z"] = 1,
["-X"] = 2,
["-Z"] = 3,
}
miningLib.scanStartFacingTo = nil
miningLib.hasChuncky = false
-- ToDo: Add Points.
-- TODO: Save original Point to rdeturn to
-- Doing: Mining the Points
---corrects the Points relative to the direction the turtle is facing
---@param data ScanDataTable
---@return ScanDataTable
local function modifyForFacingPosition(data)
assert(miningLib.scanStartFacingTo ~= nil, "Missing facing direction, cannot convert data")
return scanner.correctToFacing(data, postionmappingTable[miningLib.scanStartFacingTo])
end
---uses the Data from the Scanner to mine all blocks provided in the Parameter
---@param data ScanDataTable
local function mineWithScannData(data)
data = scanner.sortFilteredScan(data)
data = modifyForFacingPosition(data)
if data == nil then
return
end
local path = scanner.createPath(data)
if #path < 2 then
return
end
for _, pathToPoint in ipairs(path) do
tC:compactMove(pathToPoint)
end
end
local function filterFunc(item)
local keepItem = false
keepItem = string.find(item.name, "coal") ~= nil or keepItem
keepItem = string.find(item.name, "geo_scanner") ~= nil or keepItem
keepItem = string.find(item.name, "pickaxe") ~= nil or keepItem
keepItem = string.find(item.name, "chunk_controller") or keepItem
return not keepItem
end
--- turtle goes to the provided point
---@param point ScanData
function miningLib:goToPoint(point)
local singlePoint = {}
table.insert(singlePoint, point)
local path = scanner.createPath(singlePoint)
path = path[1]
tC:compactMove(path)
end
--- Checks if the Scanner is attached. Get all the scanned
--- WIP: Not finished: if no scanner is Attached -> stripmines
---@param manageSpace boolean If the turtleResourceManager is setup correctly and allows for inventoryManagement
function miningLib:mineArea(manageSpace)
---@type ScanDataTable | nil
local ores
if self.hasChuncky then
local slot = tC:findItemInInventory("advancedperipherals:geo_scanner")
assert(slot ~= nil, "No Scanner found")
turtle.select(slot)
turtle.equipRight()
end
ores = scanner.find(miningSettings.scanRadius)
if self.hasChuncky then
local slot = tC:findItemInInventory("minecraft:diamond_pickaxe")
assert(slot ~= nil, "pickaxe not found")
turtle.select(slot)
turtle.equipRight()
end
if manageSpace and tResourceManager:getFreeSlots() < 5 then
if tResourceManager:manageSpace(12, filterFunc) == 3 then
error("Errorhandling not finished, cound not pickup Chest!")
end
end
if ores ~= nil then
mineWithScannData(ores)
return
end
error("No Scanner -> not implemented")
end
--- Runs though all points in the ScanDataTable.
--- on each point, it uses the Scanner and Mines all Ores it finds
--- in the end, it returns to the Startingposition
---@param points ScanDataTable
function miningLib:main(points)
assert(nil ~= tC:findItemInInventory("advancedperipherals:geo_scanner"), "No Scanner found")
local slot = tC:findItemInInventory("advancedperipherals:chunk_controller")
local manageSpace = tResourceManager:checkSetup()
if slot then
turtle.select(slot)
turtle.equipLeft()
miningLib.hasChuncky = true
else
local slot = tC:findItemInInventory("advancedperipherals:geo_scanner")
assert(slot ~= nil, "No Scanner found")
turtle.select(slot)
turtle.equipLeft()
end
slot = tC:findItemInInventory("minecraft:diamond_pickaxe")
assert(slot ~= nil, "pickaxe not found")
turtle.select(slot)
turtle.equipRight()
local movedfromStart = { x = 0, y = 0, z = 0 }
tC.canBreakBlocks = true
miningSettings = { miningDepth = -50, miningHight = 3, miningDiameter = 9, scanRadius = 4 };
miningSettings = settings.get('MiningSettings',miningSettings);
for _, w in ipairs(points) do
movedfromStart.x = movedfromStart.x + w.x
movedfromStart.y = movedfromStart.y + w.y
movedfromStart.z = movedfromStart.z + w.z
self:goToPoint(w)
self:mineArea(manageSpace)
end
local path = scanner.createPath({ movedfromStart })
if #path > 2 then
tC:compactMove(path[2])
tC:compactMove(path[3])
end
end
return miningLib