Skip to content

Commit 3cc75b2

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

File tree

3 files changed

+239
-28
lines changed

3 files changed

+239
-28
lines changed

builder-lib.lua

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---@class Builder_Lib
22
local Builder_Lib = {
33
movementDirection = {
4-
height = "bottom",
4+
height = "up",
55
width = "right"
66
}
77
}
@@ -36,6 +36,27 @@ local function placeDownItem(itemname)
3636
if not succ then error(txt) end
3737
end
3838

39+
local function getTurningDirection(builderRef ,cHorizontal, cVertical)
40+
assert(type(builderRef) == "table", "needs self reference!")
41+
cHorizontal = cHorizontal or 1
42+
cVertical = cVertical or 1
43+
local hModulo = cHorizontal % 2
44+
local vModulo = cVertical % 2
45+
if builderRef.movementDirection.width == "left" then
46+
hModulo = 1 - hModulo -- toggle between 1 / 0
47+
end
48+
if builderRef.movementDirection.height == "down" and cVertical ~= 0 then
49+
vModulo = 1 - vModulo -- toggle between 1 / 0
50+
end
51+
-- TODO Simplify
52+
if (hModulo == 1 and vModulo == 1) or (hModulo == 0 and vModulo == 0) then
53+
turtle.turnRight()
54+
else
55+
turtle.turnLeft()
56+
end
57+
58+
end
59+
3960
local function returnToStartingPos()
4061

4162
end
@@ -44,26 +65,8 @@ end
4465
---@param length number
4566
---@param width number
4667
function Builder_Lib:floor(length, width)
47-
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
57-
else
58-
t = function(modulo)
59-
if modulo == 1 then
60-
turtle.turnLeft()
61-
else
62-
turtle.turnRight()
63-
end
64-
end
65-
end
66-
68+
length = length or 1
69+
width = width or 1
6770
turtle.select(1)
6871
local block = turtle.getItemDetail()
6972
if block == nil then error("No block at item slot 1") end
@@ -74,11 +77,54 @@ function Builder_Lib:floor(length, width)
7477
end
7578
placeDownItem(block.name)
7679
if (j < width) then
77-
t(j % 2)
80+
getTurningDirection(self, j)
7881
turtleController:goStraight(1)
79-
t(j % 2)
82+
getTurningDirection(self, j)
8083
end
8184
end
8285
returnToStartingPos()
8386
end
87+
88+
function Builder_Lib:clearArea(length, width, height)
89+
local upDownDig = function(cHeight, maxHeight)
90+
if(cHeight < maxHeight) then
91+
turtleController:tryAction("digU")
92+
end
93+
if(cHeight > 1) then
94+
turtleController:tryAction("digD")
95+
end
96+
end
97+
local currentHeight = 1
98+
local k = 1
99+
while true do
100+
for j = 1, width, 1 do
101+
for i = 1, length - 1, 1 do
102+
upDownDig(currentHeight, height)
103+
turtleController:goStraight(1)
104+
end
105+
upDownDig(currentHeight, height)
106+
if (j < width) then
107+
getTurningDirection(self, j, k)
108+
turtleController:goStraight(1)
109+
getTurningDirection(self, j, k)
110+
end
111+
end
112+
upDownDig(currentHeight, height)
113+
114+
-- go up 3 blocks
115+
local u = height - currentHeight
116+
if u > 3 then
117+
u = 3
118+
end
119+
if u == 0 then
120+
break
121+
end
122+
currentHeight = currentHeight + u
123+
k = k + 1
124+
turtleController:goUp(u)
125+
turtleController:tryMove("tA")
126+
end
127+
128+
returnToStartingPos()
129+
end
84130
return Builder_Lib

tests/clearArea_spec.lua

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
---@class are
2+
---@field same function
3+
---@field equal function
4+
---@field equals function
5+
6+
---@class is
7+
---@field truthy function
8+
---@field falsy function
9+
---@field not_true function
10+
---@field not_false function
11+
12+
---@class has
13+
---@field error function
14+
---@field errors function
15+
16+
---@class assert
17+
---@field are are
18+
---@field is is
19+
---@field are_not are
20+
---@field is_not is
21+
---@field has has
22+
---@field has_no has
23+
---@field True function
24+
---@field False function
25+
---@field has_error function
26+
---@field is_false function
27+
---@field is_true function
28+
---@field equal function
29+
assert = assert
30+
31+
package.path = package.path .. ";"
32+
.."libs/?.lua;"
33+
.."libs/inventory/?.lua;"
34+
.."libs/peripherals/?.lua;"
35+
36+
---@type TurtleEmulator
37+
local turtleEmulator = require("turtleEmulator")
38+
39+
---@type HelperFunctions
40+
local helper = require("helperFunctions")
41+
42+
---@type Vector
43+
_G.vector = require("vector")
44+
45+
---@type TextUtils
46+
_G.textutils = require("textutils")
47+
48+
---@type Builder_Lib
49+
local builder
50+
51+
local function beforeeach()
52+
turtleEmulator:clearBlocks()
53+
turtleEmulator:clearTurtles()
54+
_G.turtle = turtleEmulator:createTurtle()
55+
local peripheral = turtle.getPeripheralModule()
56+
_G.peripheral = peripheral
57+
if builder == nil then
58+
builder = require("builder-lib")
59+
end
60+
builder.movementDirection.width = "right"
61+
builder.movementDirection.height = "up"
62+
63+
---@type Item
64+
local coal = {name = "minecraft:coal", count = 64, fuelgain = 8}
65+
turtle.addItemToInventory(coal, 1)
66+
end
67+
68+
local function fillWorld()
69+
local existingBlock = {item = {count = 1, name = "minecraft:stone", placeAble = true}}
70+
local tmpBlock
71+
local n = 0
72+
for y = 0, 9, 1 do
73+
for x = 0, 9, 1 do
74+
for z = 0, 9, 1 do
75+
if x == 0 and y == 0 and z == 0 then
76+
--skip
77+
else
78+
n = n + 1
79+
tmpBlock = helper.copyTable(existingBlock) --[[@as block]]
80+
tmpBlock.position = vector.new(x, y, z)
81+
turtleEmulator:createBlock(tmpBlock)
82+
end
83+
end
84+
end
85+
end
86+
end
87+
88+
---counts the length of a table
89+
---@param t table
90+
---@return number
91+
local function countTableLength(t)
92+
local length = 0
93+
for _, _ in pairs(t) do
94+
length = length + 1
95+
end
96+
return length
97+
end
98+
99+
100+
101+
---@param p1 Vector
102+
---@param p2 Vector
103+
---@return table<string, number>
104+
local function get_bounds(p1, p2)
105+
return {
106+
x_min = math.min(p1.x, p2.x),
107+
x_max = math.max(p1.x, p2.x),
108+
y_min = math.min(p1.y, p2.y),
109+
y_max = math.max(p1.y, p2.y),
110+
z_min = math.min(p1.z, p2.z),
111+
z_max = math.max(p1.z, p2.z),
112+
}
113+
end
114+
115+
---@param pos1 Vector
116+
---@param pos2 Vector
117+
---@return number
118+
---@return table<block>
119+
local function checkEmptyFromTo(pos1, pos2)
120+
local tmpBlock
121+
local n = 0
122+
---@type table<block>
123+
local blocks = {}
124+
local bounds = get_bounds(pos1, pos2)
125+
for x = bounds.x_min, bounds.x_max do
126+
for y = bounds.y_min, bounds.y_max do
127+
for z = bounds.z_min, bounds.z_max do
128+
tmpBlock = turtleEmulator:getBlock(vector.new(x,y,z)) --[[@as block|TurtleEmulator]]
129+
if tmpBlock ~= nil and not tmpBlock.item.name:find("turtle") then
130+
n = n + 1
131+
table.insert(blocks, tmpBlock)
132+
end
133+
end
134+
end
135+
end
136+
return n, blocks
137+
end
138+
139+
describe("Testing ClearArea Function", function()
140+
---@type block
141+
142+
before_each(function ()
143+
beforeeach()
144+
fillWorld()
145+
end)
146+
it("testing the basic Test setup", function()
147+
-- pending("jup")
148+
local n, _ = checkEmptyFromTo(vector.new(0,0,0),vector.new(3,3,3))
149+
assert.are.equal(63, n)
150+
assert(998, countTableLength(turtleEmulator.blocks))
151+
end)
152+
it("Clear 4 x 4 x 4 | Moving: left up", function()
153+
builder.movementDirection.height = "up"
154+
builder.movementDirection.height = "left"
155+
builder:clearArea(4,4,4)
156+
local filter = function(t)
157+
return t.position.y == 3
158+
end
159+
160+
local n = countTableLength(turtleEmulator.blocks)
161+
assert(998 - (4*4*4), n)
162+
local m, blocks = checkEmptyFromTo(vector.new(0,0,0),vector.new(3,3,3))
163+
assert.are.equal(0,m)
164+
end)
165+
end)

tests/test_spec.lua renamed to tests/floor_spec.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ package.path = package.path .. ";"
3333
.."libs/inventory/?.lua;"
3434
.."libs/peripherals/?.lua;"
3535

36-
---@class Vector
36+
---@type Vector
3737
_G.vector = require("vector")
38-
---@class Builder_Lib
38+
---@type Builder_Lib
3939
local builder
4040

4141

42-
---@class TurtleEmulator
42+
---@type TurtleEmulator
4343
local turtleEmulator = require("turtleEmulator")
4444

4545
local function beforeeach()
@@ -69,12 +69,12 @@ describe("Testing placing Floor", function ()
6969
local blockAmount = 64
7070
before_each(function ()
7171
beforeeach()
72-
---@class Item
72+
---@type Item
7373
local itemToAdd = {name = "minecraft:cobblestone", count = blockAmount, placeAble = true}
7474
local itemToAdd2 = {name = "minecraft:cobblestone", count = blockAmount, placeAble = true}
7575
turtle.addItemToInventory(itemToAdd, 1)
7676
turtle.addItemToInventory(itemToAdd2, 3)
77-
---@class Item
77+
---@type Item
7878
local coal = {name = "minecraft:coal", count = 64, fuelgain = 8}
7979
turtle.addItemToInventory(coal, 2)
8080
end)

0 commit comments

Comments
 (0)