Skip to content

Commit 1086aa4

Browse files
committed
feat: 🚧 WIP clearArea integration
1 parent 3cc75b2 commit 1086aa4

File tree

4 files changed

+73
-19
lines changed

4 files changed

+73
-19
lines changed

‎builder-lib.lua‎

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,29 @@ local function placeDownItem(itemname)
3636
if not succ then error(txt) end
3737
end
3838

39-
local function getTurningDirection(builderRef ,cHorizontal, cVertical)
39+
local function getTurningDirection(builderRef ,cHorizontal, cVertical, maxWidth)
4040
assert(type(builderRef) == "table", "needs self reference!")
4141
cHorizontal = cHorizontal or 1
4242
cVertical = cVertical or 1
43+
maxWidth = maxWidth or 2 -- default is even
44+
local even = maxWidth % 2
4345
local hModulo = cHorizontal % 2
4446
local vModulo = cVertical % 2
45-
if builderRef.movementDirection.width == "left" then
47+
if builderRef.movementDirection.width == "right" then
4648
hModulo = 1 - hModulo -- toggle between 1 / 0
4749
end
48-
if builderRef.movementDirection.height == "down" and cVertical ~= 0 then
49-
vModulo = 1 - vModulo -- toggle between 1 / 0
50-
end
50+
5151
-- TODO Simplify
52-
if (hModulo == 1 and vModulo == 1) or (hModulo == 0 and vModulo == 0) then
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
60+
end
61+
if (hModulo == vModulo) then
5362
turtle.turnRight()
5463
else
5564
turtle.turnLeft()
@@ -104,24 +113,29 @@ function Builder_Lib:clearArea(length, width, height)
104113
end
105114
upDownDig(currentHeight, height)
106115
if (j < width) then
107-
getTurningDirection(self, j, k)
116+
getTurningDirection(self, j, k, width)
108117
turtleController:goStraight(1)
109-
getTurningDirection(self, j, k)
118+
getTurningDirection(self, j, k, width)
110119
end
111120
end
112121
upDownDig(currentHeight, height)
113122

114123
-- go up 3 blocks
115-
local u = height - currentHeight
116-
if u > 3 then
117-
u = 3
124+
local diff = height - currentHeight
125+
if diff > 3 then
126+
diff = 3
118127
end
119-
if u == 0 then
128+
if diff == 0 then
120129
break
121130
end
122-
currentHeight = currentHeight + u
131+
if self.movementDirection.height == "up" then
132+
turtleController:goUp(diff)
133+
else
134+
turtleController:goDown(diff)
135+
end
136+
currentHeight = currentHeight + diff
137+
123138
k = k + 1
124-
turtleController:goUp(u)
125139
turtleController:tryMove("tA")
126140
end
127141

‎builder.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
--- UI for Builder
22

33
---@class Builder_Lib
4-
local builder = require("/progs/builder-prog/builder-lib")
4+
local builder = require("builder-lib")
55

66

77
local function help()

‎tests/clearArea_spec.lua‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,23 +143,61 @@ describe("Testing ClearArea Function", function()
143143
beforeeach()
144144
fillWorld()
145145
end)
146+
146147
it("testing the basic Test setup", function()
147148
-- pending("jup")
148149
local n, _ = checkEmptyFromTo(vector.new(0,0,0),vector.new(3,3,3))
149150
assert.are.equal(63, n)
150151
assert(998, countTableLength(turtleEmulator.blocks))
151152
end)
153+
152154
it("Clear 4 x 4 x 4 | Moving: left up", function()
155+
-- pending("pending")
153156
builder.movementDirection.height = "up"
154-
builder.movementDirection.height = "left"
157+
builder.movementDirection.width = "left"
155158
builder:clearArea(4,4,4)
156-
local filter = function(t)
157-
return t.position.y == 3
158-
end
159159

160160
local n = countTableLength(turtleEmulator.blocks)
161161
assert(998 - (4*4*4), n)
162162
local m, blocks = checkEmptyFromTo(vector.new(0,0,0),vector.new(3,3,3))
163163
assert.are.equal(0,m)
164164
end)
165+
166+
it("Clear 4 x 4 x 4 | Moving: right up", function()
167+
-- pending("pending")
168+
builder.movementDirection.height = "up"
169+
builder.movementDirection.width = "right"
170+
---position turtle at 0, 0, 3
171+
turtle.position = vector.new(-1, 0, 3)
172+
turtle.refuel()
173+
turtle.dig()
174+
assert.is_true(turtle.forward())
175+
176+
builder:clearArea(4,4,4)
177+
178+
local n = countTableLength(turtleEmulator.blocks)
179+
assert(998 - (4*4*4), n)
180+
local m, blocks = checkEmptyFromTo(vector.new(0,0,0),vector.new(3,3,3))
181+
assert.are.equal(0,m)
182+
end)
183+
184+
it("Clear 5 x 5 x 5 | Moving: left down", function()
185+
pending("pending")
186+
builder.movementDirection.height = "down"
187+
builder.movementDirection.width = "left"
188+
189+
---position turtle at 0, 4, 0
190+
turtle.position = vector.new(-1, 4, 0)
191+
turtle.refuel()
192+
turtle.dig()
193+
assert.is_true(turtle.forward())
194+
195+
builder:clearArea(5,5,5)
196+
197+
local n = countTableLength(turtleEmulator.blocks)
198+
assert(998 - (5*5*5), n)
199+
local m, blocks = checkEmptyFromTo(vector.new(0,0,0),vector.new(4,4,4))
200+
print(textutils.serialize(blocks))
201+
assert.are.equal(0,m)
202+
end)
165203
end)

‎tests/floor_spec.lua‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ describe("Testing placing Floor", function ()
9797
it("Create 9x9 floor", function()
9898
assert.are.equal(0, countTableLength(turtleEmulator.blocks))
9999
local succ, err = xpcall(function ()
100+
builder.movementDirection.width = "right"
100101
builder:floor(9, 9)
101102
end, debug.traceback)
102103
if err then error(err) end
@@ -137,6 +138,7 @@ describe("Testing placing Floor", function ()
137138
it("Create 10x10 floor", function()
138139
assert.are.equal(0, countTableLength(turtleEmulator.blocks))
139140
local succ, err = xpcall(function ()
141+
builder.movementDirection.width = "right"
140142
builder:floor(10, 10)
141143
end, debug.traceback)
142144
if err then error(err) end

0 commit comments

Comments
 (0)