Skip to content

Commit 98eb242

Browse files
committed
feat: clearArea Integration
1 parent 1086aa4 commit 98eb242

File tree

4 files changed

+101
-39
lines changed

4 files changed

+101
-39
lines changed

builder-lib.lua

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,20 @@ local function getTurningDirection(builderRef ,cHorizontal, cVertical, maxWidth)
4141
cHorizontal = cHorizontal or 1
4242
cVertical = cVertical or 1
4343
maxWidth = maxWidth or 2 -- default is even
44-
local even = maxWidth % 2
44+
4545
local hModulo = cHorizontal % 2
4646
local vModulo = cVertical % 2
47-
if builderRef.movementDirection.width == "right" then
48-
hModulo = 1 - hModulo -- toggle between 1 / 0
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
4952
end
5053

51-
-- TODO Simplify
52-
-- with vModulo == hModulo xor even
53-
if (even == 1 and vModulo == 0) then
54-
if (hModulo ~= vModulo) then
55-
turtle.turnRight()
56-
else
57-
turtle.turnLeft()
58-
end
59-
return
54+
if builderRef.movementDirection.width == "left" then
55+
hModulo = 1 - hModulo -- invert 1 <=> 0
6056
end
57+
6158
if (hModulo == vModulo) then
6259
turtle.turnRight()
6360
else
@@ -71,8 +68,9 @@ local function returnToStartingPos()
7168
end
7269

7370
---builds the floor with the given size
74-
---@param length number
75-
---@param width number
71+
---@param length number | nil
72+
---@param width number | nil
73+
---@return boolean success
7674
function Builder_Lib:floor(length, width)
7775
length = length or 1
7876
width = width or 1
@@ -92,17 +90,37 @@ function Builder_Lib:floor(length, width)
9290
end
9391
end
9492
returnToStartingPos()
93+
return true
9594
end
9695

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
97101
function Builder_Lib:clearArea(length, width, height)
98102
local upDownDig = function(cHeight, maxHeight)
99-
if(cHeight < maxHeight) then
100-
turtleController:tryAction("digU")
101-
end
102-
if(cHeight > 1) then
103-
turtleController:tryAction("digD")
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
104117
end
105118
end
119+
length = length or 1
120+
width = width or 1
121+
height = height or 1
122+
123+
106124
local currentHeight = 1
107125
local k = 1
108126
while true do
@@ -116,6 +134,7 @@ function Builder_Lib:clearArea(length, width, height)
116134
getTurningDirection(self, j, k, width)
117135
turtleController:goStraight(1)
118136
getTurningDirection(self, j, k, width)
137+
else
119138
end
120139
end
121140
upDownDig(currentHeight, height)
@@ -125,7 +144,7 @@ function Builder_Lib:clearArea(length, width, height)
125144
if diff > 3 then
126145
diff = 3
127146
end
128-
if diff == 0 then
147+
if diff <= 1 then
129148
break
130149
end
131150
if self.movementDirection.height == "up" then
@@ -140,5 +159,6 @@ function Builder_Lib:clearArea(length, width, height)
140159
end
141160

142161
returnToStartingPos()
162+
return true
143163
end
144164
return Builder_Lib

builder.lua

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ local builder = require("builder-lib")
77
local function help()
88
print("Usage:")
99
print("builder <command> <options>")
10-
print("")
1110
print("Commands: ")
12-
print("- 'floor'")
13-
print("")
11+
print("'floor'")
12+
print("'clearArea'")
1413
print("Options:")
15-
print("-m[movementDirection] <'left'|'right'> [default: right]")
14+
print("-mH[movementDir. Horizon] <'left'|'right'> [default: right]")
15+
print("-mV[movementDir. Vertical] <'left'|'right'> [default: right]")
1616
print("-w[width] <number>")
1717
print("-l[length] <number>")
18-
print("-h[help]")
18+
print("-h[height] <number>")
19+
print("-help")
1920
end
2021
if #arg == 0 then
2122
help()
@@ -26,16 +27,20 @@ end
2627
local stopExec = false
2728

2829
local size = {
29-
["length"] = 0,
30-
["width"] = 0,
31-
["height"] = 0
30+
["length"] = nil,
31+
["width"] = nil,
32+
["height"] = nil
3233
}
3334

3435
local argsSwitch = {
35-
["-m"] = function(no)
36+
["-mH"] = function(no)
3637
no[1] = no[1] + 1
3738
builder.movementDirection.width = arg[no[1]]
3839
end,
40+
["-mV"] = function (no)
41+
no[1] = no[1] + 1
42+
builder.movementDirection.height = arg[no[1]]
43+
end,
3944
["-l"] = function(no)
4045
no[1] = no[1] + 1
4146
size["length"] = tonumber(arg[no[1]]) or error("length not valid")
@@ -44,7 +49,16 @@ local argsSwitch = {
4449
no[1] = no[1] + 1
4550
size["width"] = tonumber(arg[no[1]]) or error("width not valid")
4651
end,
47-
["-h"] = function()
52+
["-h"] = function(no)
53+
no[1] = no[1] + 1
54+
if not tonumber(arg[no[1]]) then
55+
help()
56+
stopExec = true
57+
return
58+
end
59+
size["height"] = tonumber(arg[no[1]])
60+
end,
61+
["-help"] = function()
4862
help()
4963
stopExec = true
5064
end,
@@ -56,6 +70,9 @@ local argsSwitch = {
5670
local commands = {
5771
["floor"] = function()
5872
builder:floor(size["length"], size["width"])
73+
end,
74+
["clearArea"] = function()
75+
builder:clearArea(size["length"], size["width"], size["height"])
5976
end
6077
}
6178

tests/clearArea_spec.lua

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,12 @@ describe("Testing ClearArea Function", function()
151151
assert(998, countTableLength(turtleEmulator.blocks))
152152
end)
153153

154-
it("Clear 4 x 4 x 4 | Moving: left up", function()
154+
it("Clear 4 x 4 x 4 | Moving: right up", function()
155155
-- pending("pending")
156156
builder.movementDirection.height = "up"
157-
builder.movementDirection.width = "left"
157+
builder.movementDirection.width = "right"
158+
159+
158160
builder:clearArea(4,4,4)
159161

160162
local n = countTableLength(turtleEmulator.blocks)
@@ -163,16 +165,17 @@ describe("Testing ClearArea Function", function()
163165
assert.are.equal(0,m)
164166
end)
165167

166-
it("Clear 4 x 4 x 4 | Moving: right up", function()
168+
it("Clear 4 x 4 x 4 | Moving: left up", function()
167169
-- pending("pending")
168170
builder.movementDirection.height = "up"
169-
builder.movementDirection.width = "right"
171+
builder.movementDirection.width = "left"
172+
170173
---position turtle at 0, 0, 3
171174
turtle.position = vector.new(-1, 0, 3)
172175
turtle.refuel()
173176
turtle.dig()
174177
assert.is_true(turtle.forward())
175-
178+
176179
builder:clearArea(4,4,4)
177180

178181
local n = countTableLength(turtleEmulator.blocks)
@@ -181,10 +184,10 @@ describe("Testing ClearArea Function", function()
181184
assert.are.equal(0,m)
182185
end)
183186

184-
it("Clear 5 x 5 x 5 | Moving: left down", function()
185-
pending("pending")
187+
it("Clear 5 x 5 x 5 | Moving: right down", function()
188+
-- pending("pending")
186189
builder.movementDirection.height = "down"
187-
builder.movementDirection.width = "left"
190+
builder.movementDirection.width = "right"
188191

189192
---position turtle at 0, 4, 0
190193
turtle.position = vector.new(-1, 4, 0)
@@ -197,7 +200,26 @@ describe("Testing ClearArea Function", function()
197200
local n = countTableLength(turtleEmulator.blocks)
198201
assert(998 - (5*5*5), n)
199202
local m, blocks = checkEmptyFromTo(vector.new(0,0,0),vector.new(4,4,4))
200-
print(textutils.serialize(blocks))
201203
assert.are.equal(0,m)
202204
end)
205+
206+
it("Clear 5 x 5 x 5 | Moving: left down", function()
207+
-- pending("pending")
208+
builder.movementDirection.height = "down"
209+
builder.movementDirection.width = "left"
210+
211+
---position turtle at 0, 4, 0
212+
turtle.position = vector.new(-1, 4, 4)
213+
turtle.refuel()
214+
turtle.dig()
215+
assert.is_true(turtle.forward())
216+
217+
builder:clearArea(5,5,5)
218+
219+
local n = countTableLength(turtleEmulator.blocks)
220+
assert(998 - (5*5*5), n)
221+
local m, blocks = checkEmptyFromTo(vector.new(0,0,0),vector.new(4,4,4))
222+
assert.are.equal(0,m)
223+
end)
224+
203225
end)

tests/floor_spec.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ _G.vector = require("vector")
3838
---@type Builder_Lib
3939
local builder
4040

41+
_G.textutils = require("textutils")
42+
4143

4244
---@type TurtleEmulator
4345
local turtleEmulator = require("turtleEmulator")
@@ -164,6 +166,7 @@ describe("Testing placing Floor", function ()
164166
if err then error(err) end
165167
assert.are.equal(100, countTableLength(turtleEmulator.blocks))
166168
local blocksTested = 0
169+
-- print("Blocks placed", textutils.serialize(turtleEmulator.blocks))
167170
for j = -9, 0, 1 do
168171
for i = 0, 9, 1 do
169172
blocksTested = blocksTested + 1

0 commit comments

Comments
 (0)