11--- @class Builder_Lib
22local Builder_Lib = {
33 movementDirection = {
4- height = " bottom " ,
4+ height = " up " ,
55 width = " right"
66 }
77}
@@ -36,34 +36,44 @@ local function placeDownItem(itemname)
3636 if not succ then error (txt ) end
3737end
3838
39- local function returnToStartingPos ()
39+ local function getTurningDirection (builderRef ,cHorizontal , cVertical , maxWidth )
40+ assert (type (builderRef ) == " table" , " needs self reference!" )
41+ cHorizontal = cHorizontal or 1
42+ cVertical = cVertical or 1
43+ maxWidth = maxWidth or 2 -- default is even
4044
41- end
45+ local hModulo = cHorizontal % 2
46+ local vModulo = cVertical % 2
47+
48+ -- if odd, pretent to always be at base y-level and CONTINUE the left / right toggle
49+ -- making vModulo static
50+ if (maxWidth % 2 == 1 ) then
51+ vModulo = 1
52+ end
4253
43- --- builds the floor with the given size
44- --- @param length number
45- --- @param width number
46- function Builder_Lib :floor (length , width )
54+ if builderRef .movementDirection .width == " left" then
55+ hModulo = 1 - hModulo -- invert 1 <=> 0
56+ end
4757
48- local t
49- if self .movementDirection .width == " right" then
50- t = function (modulo )
51- if modulo == 1 then
52- turtle .turnRight ()
53- else
54- turtle .turnLeft ()
55- end
56- end
58+ if (hModulo == vModulo ) then
59+ turtle .turnRight ()
5760 else
58- t = function (modulo )
59- if modulo == 1 then
60- turtle .turnLeft ()
61- else
62- turtle .turnRight ()
63- end
64- end
61+ turtle .turnLeft ()
6562 end
6663
64+ end
65+
66+ local function returnToStartingPos ()
67+
68+ end
69+
70+ --- builds the floor with the given size
71+ --- @param length number | nil
72+ --- @param width number | nil
73+ --- @return boolean success
74+ function Builder_Lib :floor (length , width )
75+ length = length or 1
76+ width = width or 1
6777 turtle .select (1 )
6878 local block = turtle .getItemDetail ()
6979 if block == nil then error (" No block at item slot 1" ) end
@@ -74,11 +84,81 @@ function Builder_Lib:floor(length, width)
7484 end
7585 placeDownItem (block .name )
7686 if (j < width ) then
77- t ( j % 2 )
87+ getTurningDirection ( self , j )
7888 turtleController :goStraight (1 )
79- t ( j % 2 )
89+ getTurningDirection ( self , j )
8090 end
8191 end
8292 returnToStartingPos ()
93+ return true
94+ end
95+
96+ --- clears an area with of specified size
97+ --- @param length number | nil
98+ --- @param width number | nil
99+ --- @param height number | nil
100+ --- @return boolean success
101+ function Builder_Lib :clearArea (length , width , height )
102+ local upDownDig = function (cHeight , maxHeight )
103+ if Builder_Lib .movementDirection .height == " up" then
104+ if (cHeight < maxHeight ) then
105+ turtleController :tryAction (" digU" )
106+ end
107+ if (cHeight > 1 ) then
108+ turtleController :tryAction (" digD" )
109+ end
110+ else
111+ if (cHeight < maxHeight ) then
112+ turtleController :tryAction (" digD" )
113+ end
114+ if (cHeight > 1 ) then
115+ turtleController :tryAction (" digU" )
116+ end
117+ end
118+ end
119+ length = length or 1
120+ width = width or 1
121+ height = height or 1
122+
123+
124+ local currentHeight = 1
125+ local k = 1
126+ while true do
127+ for j = 1 , width , 1 do
128+ for i = 1 , length - 1 , 1 do
129+ upDownDig (currentHeight , height )
130+ turtleController :goStraight (1 )
131+ end
132+ upDownDig (currentHeight , height )
133+ if (j < width ) then
134+ getTurningDirection (self , j , k , width )
135+ turtleController :goStraight (1 )
136+ getTurningDirection (self , j , k , width )
137+ else
138+ end
139+ end
140+ upDownDig (currentHeight , height )
141+
142+ -- go up 3 blocks
143+ local diff = height - currentHeight
144+ if diff > 3 then
145+ diff = 3
146+ end
147+ if diff <= 1 then
148+ break
149+ end
150+ if self .movementDirection .height == " up" then
151+ turtleController :goUp (diff )
152+ else
153+ turtleController :goDown (diff )
154+ end
155+ currentHeight = currentHeight + diff
156+
157+ k = k + 1
158+ turtleController :tryMove (" tA" )
159+ end
160+
161+ returnToStartingPos ()
162+ return true
83163end
84164return Builder_Lib
0 commit comments