-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder-lib.lua
More file actions
193 lines (169 loc) · 5.27 KB
/
builder-lib.lua
File metadata and controls
193 lines (169 loc) · 5.27 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
---@class Builder_Lib
local Builder_Lib = {
movementDirection = {
height = "up",
width = "right"
},
placeDirection = "down"
}
---@type SCM
local scm = require("./scm")
---@type turtleController
local turtleController = scm:load("turtleController")
---@type TurtleResourceManager
local tResourceManager = scm:load("turtleResourceManager")
turtleController.canBreakBlocks = true
local function selectItemToPlace(itemname)
local item = turtle.getItemDetail()
if item == nil or item.name ~= itemname then
local slot = turtleController:findItemInInventory(itemname)
if slot == nil then
error("No more blocks available to build" .. tostring(turtle.getItemCount()))
end
turtle.select(slot)
end
end
---
---@param self Builder_Lib
---@param itemname string
local function placeItem(self, itemname)
selectItemToPlace(itemname)
local function action()
if (self.placeDirection == "down") then
if turtle.detectDown() then
local _, down = turtle.inspectDown()
turtleController:tryAction("digD")
end
return turtle.placeDown()
else
if turtle.detectUp() then
local _, down = turtle.inspectUp()
turtleController:tryAction("digU")
end
return turtle.placeUp()
end
end
local succ, txt = action()
if not succ then error(txt) end
end
local function getTurningDirection(builderRef ,cHorizontal, cVertical, maxWidth)
assert(type(builderRef) == "table", "needs self reference!")
cHorizontal = cHorizontal or 1
cVertical = cVertical or 1
maxWidth = maxWidth or 2 -- default is even
local hModulo = cHorizontal % 2
local vModulo = cVertical % 2
-- if odd, pretent to always be at base y-level and CONTINUE the left / right toggle
-- making vModulo static
if (maxWidth % 2 == 1) then
vModulo = 1
end
if builderRef.movementDirection.width == "left" then
hModulo = 1 - hModulo -- invert 1 <=> 0
end
if (hModulo == vModulo) then
turtle.turnRight()
else
turtle.turnLeft()
end
end
local function returnToStartingPos()
end
---builds the floor with the given size
---@param length number | nil
---@param width number | nil
---@return boolean success
function Builder_Lib:floor(length, width)
length = length or 1
width = width or 1
turtle.select(1)
local block = turtle.getItemDetail()
if block == nil then error("No block at item slot 1") end
for j = 1, width, 1 do
for i = 1, length - 1, 1 do
placeItem(self, block.name)
turtleController:goStraight(1)
end
placeItem(self, block.name)
if (j < width) then
getTurningDirection(self, j)
turtleController:goStraight(1)
getTurningDirection(self, j)
end
end
returnToStartingPos()
return true
end
---clears an area with of specified size
---@param length number | nil
---@param width number | nil
---@param height number | nil
---@return boolean success
function Builder_Lib:clearArea(length, width, height)
local upDownDig = function(cHeight, maxHeight)
if Builder_Lib.movementDirection.height == "up" then
if(cHeight < maxHeight) then
turtleController:tryAction("digU")
end
if(cHeight > 1) then
turtleController:tryAction("digD")
end
else
if(cHeight < maxHeight) then
turtleController:tryAction("digD")
end
if(cHeight > 1) then
turtleController:tryAction("digU")
end
end
end
local filterFunc = function(item)
return string.find(item.name, "coal") == nil
end
length = length or 1
width = width or 1
height = height or 1
local currentHeight = 1
local k = 1
local manageSpace = tResourceManager:checkSetup()
while true do
for j = 1, width, 1 do
for i = 1, length - 1, 1 do
upDownDig(currentHeight, height)
turtleController:goStraight(1)
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
upDownDig(currentHeight, height)
if (j < width) then
getTurningDirection(self, j, k, width)
turtleController:goStraight(1)
getTurningDirection(self, j, k, width)
else
end
end
upDownDig(currentHeight, height)
-- go up 3 blocks
local diff = height - currentHeight
if diff > 3 then
diff = 3
end
if diff <= 1 then
break
end
if self.movementDirection.height == "up" then
turtleController:goUp(diff)
else
turtleController:goDown(diff)
end
currentHeight = currentHeight + diff
k = k + 1
turtleController:tryMove("tA")
end
returnToStartingPos()
return true
end
return Builder_Lib