Skip to content

Commit 5ecc1a6

Browse files
authored
Merge pull request #2 from mc-cc-scripts/ClearArea-addition
Clear area addition
2 parents 52b08d6 + 05ee370 commit 5ecc1a6

File tree

5 files changed

+375
-43
lines changed

5 files changed

+375
-43
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ builder help
1818
```lua
1919
-- Builds a floor with the specified size.
2020
-- Uses the Item in Slot 1 as Buildingblock!
21-
builder floor -w <width> -l <length> -m <"left"|"right">
21+
builder floor -w <width> -l <length> -mH <"left"|"right">
2222
```
2323

24-
# Dev - Documentaion
24+
### clearArea
25+
26+
```lua
27+
-- clears an Area of the specifed size
28+
builder clearArea -w <width> -l <length> -h <height> -mH <"left"|"right"> -mV <"up"|"down">
29+
```

builder-lib.lua

Lines changed: 105 additions & 25 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,34 +36,44 @@ local function placeDownItem(itemname)
3636
if not succ then error(txt) end
3737
end
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
83163
end
84164
return Builder_Lib

builder.lua

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
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()
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] <'down'|'up'> [default: up]")
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

0 commit comments

Comments
 (0)