diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5347502..6d0ab4a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -30,10 +30,10 @@ jobs: luarocks install luasocket luarocks install luasec - - name: Git Submodule Update + - name: fetch dependencies run: | - git pull --recurse-submodules - git submodule update --init --remote --recursive + chmod +x ./fetch-deps.sh + ./fetch-deps.sh - name: test run: | diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..73df968 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/libs \ No newline at end of file diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 4e65a3d..0000000 --- a/.gitmodules +++ /dev/null @@ -1,8 +0,0 @@ -[submodule "turtleController-lib"] - path = turtleController-lib - url = https://github.com/mc-cc-scripts/turtleController-lib.git - branch = master -[submodule "turtleEmulator-lib"] - path = turtleEmulator-lib - url = https://github.com/mc-cc-scripts/turtleEmulator-lib.git - branch = master diff --git a/README.md b/README.md index 4394c6d..d1703d9 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,12 @@ builder help ```lua -- Builds a floor with the specified size. -- Uses the Item in Slot 1 as Buildingblock! -builder floor -w -l -m <"left"|"right"> +builder floor -w -l -mH <"left"|"right"> ``` -# Dev - Documentaion +### clearArea -### Submodules used -- turtleControler-lib -- turtleEmulator-lib -- - plus submodules +```lua +-- clears an Area of the specifed size +builder clearArea -w -l -h -mH <"left"|"right"> -mV <"up"|"down"> +``` diff --git a/builder-lib.lua b/builder-lib.lua index 83a42b8..ff81bbd 100644 --- a/builder-lib.lua +++ b/builder-lib.lua @@ -1,7 +1,7 @@ ---@class Builder_Lib local Builder_Lib = { movementDirection = { - height = "bottom", + height = "up", width = "right" } } @@ -36,34 +36,44 @@ local function placeDownItem(itemname) if not succ then error(txt) end end -local function returnToStartingPos() +local function getTurningDirection(builderRef ,cHorizontal, cVertical, maxWidth) + assert(type(builderRef) == "table", "needs self reference!") + cHorizontal = cHorizontal or 1 + cVertical = cVertical or 1 + maxWidth = maxWidth or 2 -- default is even -end + local hModulo = cHorizontal % 2 + local vModulo = cVertical % 2 + + -- if odd, pretent to always be at base y-level and CONTINUE the left / right toggle + -- making vModulo static + if (maxWidth % 2 == 1) then + vModulo = 1 + end ----builds the floor with the given size ----@param length number ----@param width number -function Builder_Lib:floor(length, width) + if builderRef.movementDirection.width == "left" then + hModulo = 1 - hModulo -- invert 1 <=> 0 + end - local t - if self.movementDirection.width == "right" then - t = function(modulo) - if modulo == 1 then - turtle.turnRight() - else - turtle.turnLeft() - end - end + if (hModulo == vModulo) then + turtle.turnRight() else - t = function(modulo) - if modulo == 1 then - turtle.turnLeft() - else - turtle.turnRight() - end - end + turtle.turnLeft() end +end + +local function returnToStartingPos() + +end + +---builds the floor with the given size +---@param length number | nil +---@param width number | nil +---@return boolean success +function Builder_Lib:floor(length, width) + length = length or 1 + width = width or 1 turtle.select(1) local block = turtle.getItemDetail() if block == nil then error("No block at item slot 1") end @@ -74,11 +84,81 @@ function Builder_Lib:floor(length, width) end placeDownItem(block.name) if (j < width) then - t(j % 2) + getTurningDirection(self, j) turtleController:goStraight(1) - t(j % 2) + getTurningDirection(self, j) end end returnToStartingPos() + return true +end + +---clears an area with of specified size +---@param length number | nil +---@param width number | nil +---@param height number | nil +---@return boolean success +function Builder_Lib:clearArea(length, width, height) + local upDownDig = function(cHeight, maxHeight) + if Builder_Lib.movementDirection.height == "up" then + if(cHeight < maxHeight) then + turtleController:tryAction("digU") + end + if(cHeight > 1) then + turtleController:tryAction("digD") + end + else + if(cHeight < maxHeight) then + turtleController:tryAction("digD") + end + if(cHeight > 1) then + turtleController:tryAction("digU") + end + end + end + length = length or 1 + width = width or 1 + height = height or 1 + + + local currentHeight = 1 + local k = 1 + while true do + for j = 1, width, 1 do + for i = 1, length - 1, 1 do + upDownDig(currentHeight, height) + turtleController:goStraight(1) + end + upDownDig(currentHeight, height) + if (j < width) then + getTurningDirection(self, j, k, width) + turtleController:goStraight(1) + getTurningDirection(self, j, k, width) + else + end + end + upDownDig(currentHeight, height) + + -- go up 3 blocks + local diff = height - currentHeight + if diff > 3 then + diff = 3 + end + if diff <= 1 then + break + end + if self.movementDirection.height == "up" then + turtleController:goUp(diff) + else + turtleController:goDown(diff) + end + currentHeight = currentHeight + diff + + k = k + 1 + turtleController:tryMove("tA") + end + + returnToStartingPos() + return true end return Builder_Lib \ No newline at end of file diff --git a/builder.lua b/builder.lua index 52e8aaa..f129a28 100644 --- a/builder.lua +++ b/builder.lua @@ -1,21 +1,22 @@ --- UI for Builder ---@class Builder_Lib -local builder = require("/progs/builder-prog/builder-lib") +local builder = require("builder-lib") local function help() print("Usage:") print("builder ") - print("") print("Commands: ") - print("- 'floor'") - print("") + print("'floor'") + print("'clearArea'") print("Options:") - print("-m[movementDirection] <'left'|'right'> [default: right]") + print("-mH[movementDir. Horizon] <'left'|'right'> [default: right]") + print("-mV[movementDir. Vertical] <'down'|'up'> [default: up]") print("-w[width] ") print("-l[length] ") - print("-h[help]") + print("-h[height] ") + print("-help") end if #arg == 0 then help() @@ -26,16 +27,20 @@ end local stopExec = false local size = { - ["length"] = 0, - ["width"] = 0, - ["height"] = 0 + ["length"] = nil, + ["width"] = nil, + ["height"] = nil } local argsSwitch = { - ["-m"] = function(no) + ["-mH"] = function(no) no[1] = no[1] + 1 builder.movementDirection.width = arg[no[1]] end, + ["-mV"] = function (no) + no[1] = no[1] + 1 + builder.movementDirection.height = arg[no[1]] + end, ["-l"] = function(no) no[1] = no[1] + 1 size["length"] = tonumber(arg[no[1]]) or error("length not valid") @@ -44,7 +49,16 @@ local argsSwitch = { no[1] = no[1] + 1 size["width"] = tonumber(arg[no[1]]) or error("width not valid") end, - ["-h"] = function() + ["-h"] = function(no) + no[1] = no[1] + 1 + if not tonumber(arg[no[1]]) then + help() + stopExec = true + return + end + size["height"] = tonumber(arg[no[1]]) + end, + ["-help"] = function() help() stopExec = true end, @@ -56,6 +70,9 @@ local argsSwitch = { local commands = { ["floor"] = function() builder:floor(size["length"], size["width"]) + end, + ["clearArea"] = function() + builder:clearArea(size["length"], size["width"], size["height"]) end } diff --git a/ccPackage.lua b/ccPackage.lua deleted file mode 100644 index 98d7cf4..0000000 --- a/ccPackage.lua +++ /dev/null @@ -1,6 +0,0 @@ -local spath = - debug.getinfo(1,'S').source:sub(2):gsub("/+", "/"):gsub("[^/]*$","") -package.path = spath.."?.lua;" - .. spath.."turtleController-lib/?.lua;" - .. package.path -require(spath.."turtleEmulator-lib/ccPackage") \ No newline at end of file diff --git a/fetch-deps.sh b/fetch-deps.sh new file mode 100755 index 0000000..921ddf9 --- /dev/null +++ b/fetch-deps.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +# ---- Whats happening ---- # + +# This fetches the dependencies listed in the "libs" variable and saves them in the targetFolder + + + +set -e + +libs=( + "helperFunctions-lib" + "ccClass-lib" + "testSuite-lib" + "turtleEmulator-lib" + "turtleController-lib" + "eventHandler-lib" +) + +# Basic setup variables +repo="mc-cc-scripts" +branch="master" +targetFolderName=libs + + +# fetch files.txt and save each file into the targetFolder +fetch() { + files_txt=$(curl -fsSL "https://raw.githubusercontent.com/$repo/$1/$branch/files.txt") + if [ -z "$files_txt" ]; then + echo "Could not load files.txt for $1" + exit 1 + fi + while IFS= read -r FILE; do + url="https://raw.githubusercontent.com/$repo/$1/$branch/$FILE" + + mkdir -p "$(dirname "$targetFolderName/$FILE")" # create the folder (and subfolders specified in the files.txt) + rm -f $targetFolderName/$FILE.lua # rm existing file + if ! curl -s -o "$targetFolderName/$FILE" "$url"; then + echo "could not get / write the file $i: '$FILE' to the folder '$targetFolderName'" + exit 1 + fi + # echo "saved $1: '$FILE' in '$targetFolderName'" + done < <(echo "$files_txt") +} + +if [[ $# -eq 0 ]]; then + # No arguments given, fetch all + for i in "${libs[@]}"; do + fetch "$i" + done +else + # Argument given, fetch arguemt + fetch "$1" +fi \ No newline at end of file diff --git a/tests/clearArea_spec.lua b/tests/clearArea_spec.lua new file mode 100644 index 0000000..68a3632 --- /dev/null +++ b/tests/clearArea_spec.lua @@ -0,0 +1,225 @@ +---@class are +---@field same function +---@field equal function +---@field equals function + +---@class is +---@field truthy function +---@field falsy function +---@field not_true function +---@field not_false function + +---@class has +---@field error function +---@field errors function + +---@class assert +---@field are are +---@field is is +---@field are_not are +---@field is_not is +---@field has has +---@field has_no has +---@field True function +---@field False function +---@field has_error function +---@field is_false function +---@field is_true function +---@field equal function +assert = assert + +package.path = package.path .. ";" + .."libs/?.lua;" + .."libs/inventory/?.lua;" + .."libs/peripherals/?.lua;" + +---@type TurtleEmulator +local turtleEmulator = require("turtleEmulator") + +---@type HelperFunctions +local helper = require("helperFunctions") + +---@type Vector +_G.vector = require("vector") + +---@type TextUtils +_G.textutils = require("textutils") + +---@type Builder_Lib +local builder + +local function beforeeach() + turtleEmulator:clearBlocks() + turtleEmulator:clearTurtles() + _G.turtle = turtleEmulator:createTurtle() + local peripheral = turtle.getPeripheralModule() + _G.peripheral = peripheral + if builder == nil then + builder = require("builder-lib") + end + builder.movementDirection.width = "right" + builder.movementDirection.height = "up" + + ---@type Item + local coal = {name = "minecraft:coal", count = 64, fuelgain = 8} + turtle.addItemToInventory(coal, 1) +end + +local function fillWorld() + local existingBlock = {item = {count = 1, name = "minecraft:stone", placeAble = true}} + local tmpBlock + local n = 0 + for y = 0, 9, 1 do + for x = 0, 9, 1 do + for z = 0, 9, 1 do + if x == 0 and y == 0 and z == 0 then + --skip + else + n = n + 1 + tmpBlock = helper.copyTable(existingBlock) --[[@as block]] + tmpBlock.position = vector.new(x, y, z) + turtleEmulator:createBlock(tmpBlock) + end + end + end + end +end + +---counts the length of a table +---@param t table +---@return number +local function countTableLength(t) + local length = 0 + for _, _ in pairs(t) do + length = length + 1 + end + return length +end + + + +---@param p1 Vector +---@param p2 Vector +---@return table +local function get_bounds(p1, p2) + return { + x_min = math.min(p1.x, p2.x), + x_max = math.max(p1.x, p2.x), + y_min = math.min(p1.y, p2.y), + y_max = math.max(p1.y, p2.y), + z_min = math.min(p1.z, p2.z), + z_max = math.max(p1.z, p2.z), + } +end + +---@param pos1 Vector +---@param pos2 Vector +---@return number +---@return table +local function checkEmptyFromTo(pos1, pos2) + local tmpBlock + local n = 0 + ---@type table + local blocks = {} + local bounds = get_bounds(pos1, pos2) + for x = bounds.x_min, bounds.x_max do + for y = bounds.y_min, bounds.y_max do + for z = bounds.z_min, bounds.z_max do + tmpBlock = turtleEmulator:getBlock(vector.new(x,y,z)) --[[@as block|TurtleEmulator]] + if tmpBlock ~= nil and not tmpBlock.item.name:find("turtle") then + n = n + 1 + table.insert(blocks, tmpBlock) + end + end + end + end + return n, blocks +end + +describe("Testing ClearArea Function", function() + ---@type block + + before_each(function () + beforeeach() + fillWorld() + end) + + it("testing the basic Test setup", function() + -- pending("jup") + local n, _ = checkEmptyFromTo(vector.new(0,0,0),vector.new(3,3,3)) + assert.are.equal(63, n) + assert(998, countTableLength(turtleEmulator.blocks)) + end) + + it("Clear 4 x 4 x 4 | Moving: right up", function() + -- pending("pending") + builder.movementDirection.height = "up" + builder.movementDirection.width = "right" + + + builder:clearArea(4,4,4) + + local n = countTableLength(turtleEmulator.blocks) + assert(998 - (4*4*4), n) + local m, blocks = checkEmptyFromTo(vector.new(0,0,0),vector.new(3,3,3)) + assert.are.equal(0,m) + end) + + it("Clear 4 x 4 x 4 | Moving: left up", function() + -- pending("pending") + builder.movementDirection.height = "up" + builder.movementDirection.width = "left" + + ---position turtle at 0, 0, 3 + turtle.position = vector.new(-1, 0, 3) + turtle.refuel() + turtle.dig() + assert.is_true(turtle.forward()) + + builder:clearArea(4,4,4) + + local n = countTableLength(turtleEmulator.blocks) + assert(998 - (4*4*4), n) + local m, blocks = checkEmptyFromTo(vector.new(0,0,0),vector.new(3,3,3)) + assert.are.equal(0,m) + end) + + it("Clear 5 x 5 x 5 | Moving: right down", function() + -- pending("pending") + builder.movementDirection.height = "down" + builder.movementDirection.width = "right" + + ---position turtle at 0, 4, 0 + turtle.position = vector.new(-1, 4, 0) + turtle.refuel() + turtle.dig() + assert.is_true(turtle.forward()) + + builder:clearArea(5,5,5) + + local n = countTableLength(turtleEmulator.blocks) + assert(998 - (5*5*5), n) + local m, blocks = checkEmptyFromTo(vector.new(0,0,0),vector.new(4,4,4)) + assert.are.equal(0,m) + end) + + it("Clear 5 x 5 x 5 | Moving: left down", function() + -- pending("pending") + builder.movementDirection.height = "down" + builder.movementDirection.width = "left" + + ---position turtle at 0, 4, 0 + turtle.position = vector.new(-1, 4, 4) + turtle.refuel() + turtle.dig() + assert.is_true(turtle.forward()) + + builder:clearArea(5,5,5) + + local n = countTableLength(turtleEmulator.blocks) + assert(998 - (5*5*5), n) + local m, blocks = checkEmptyFromTo(vector.new(0,0,0),vector.new(4,4,4)) + assert.are.equal(0,m) + end) + +end) \ No newline at end of file diff --git a/tests/test_spec.lua b/tests/floor_spec.lua similarity index 92% rename from tests/test_spec.lua rename to tests/floor_spec.lua index 16edaf2..c1fe056 100644 --- a/tests/test_spec.lua +++ b/tests/floor_spec.lua @@ -28,18 +28,20 @@ ---@field equal function assert = assert -local spath = debug.getinfo(1,'S').source:sub(2):gsub("/+", "/"):gsub("[^/]*$",""):gsub("/tests", ""):gsub("tests","") -if spath == "" then - spath = "./" -end -require(spath.."ccPackage") ----@class Vector +package.path = package.path .. ";" + .."libs/?.lua;" + .."libs/inventory/?.lua;" + .."libs/peripherals/?.lua;" + +---@type Vector _G.vector = require("vector") ----@class Builder_Lib +---@type Builder_Lib local builder +_G.textutils = require("textutils") + ----@class TurtleEmulator +---@type TurtleEmulator local turtleEmulator = require("turtleEmulator") local function beforeeach() @@ -69,12 +71,12 @@ describe("Testing placing Floor", function () local blockAmount = 64 before_each(function () beforeeach() - ---@class Item + ---@type Item local itemToAdd = {name = "minecraft:cobblestone", count = blockAmount, placeAble = true} local itemToAdd2 = {name = "minecraft:cobblestone", count = blockAmount, placeAble = true} turtle.addItemToInventory(itemToAdd, 1) turtle.addItemToInventory(itemToAdd2, 3) - ---@class Item + ---@type Item local coal = {name = "minecraft:coal", count = 64, fuelgain = 8} turtle.addItemToInventory(coal, 2) end) @@ -97,6 +99,7 @@ describe("Testing placing Floor", function () it("Create 9x9 floor", function() assert.are.equal(0, countTableLength(turtleEmulator.blocks)) local succ, err = xpcall(function () + builder.movementDirection.width = "right" builder:floor(9, 9) end, debug.traceback) if err then error(err) end @@ -137,6 +140,7 @@ describe("Testing placing Floor", function () it("Create 10x10 floor", function() assert.are.equal(0, countTableLength(turtleEmulator.blocks)) local succ, err = xpcall(function () + builder.movementDirection.width = "right" builder:floor(10, 10) end, debug.traceback) if err then error(err) end @@ -162,6 +166,7 @@ describe("Testing placing Floor", function () if err then error(err) end assert.are.equal(100, countTableLength(turtleEmulator.blocks)) local blocksTested = 0 + -- print("Blocks placed", textutils.serialize(turtleEmulator.blocks)) for j = -9, 0, 1 do for i = 0, 9, 1 do blocksTested = blocksTested + 1 diff --git a/turtleController-lib b/turtleController-lib deleted file mode 160000 index 8816bf4..0000000 --- a/turtleController-lib +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 8816bf4de67da590aa75c2a6ad7b537b63aca446 diff --git a/turtleEmulator-lib b/turtleEmulator-lib deleted file mode 160000 index 6c41199..0000000 --- a/turtleEmulator-lib +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6c411996cd8ac7117def9ebcf67c91a9ea82d946