diff --git a/builder-lib.lua b/builder-lib.lua index 870b632..df77c23 100644 --- a/builder-lib.lua +++ b/builder-lib.lua @@ -3,7 +3,8 @@ local Builder_Lib = { movementDirection = { height = "up", width = "right" - } + }, + placeDirection = "down" } @@ -23,19 +24,33 @@ local function selectItemToPlace(itemname) if item == nil or item.name ~= itemname then local slot = turtleController:findItemInInventory(itemname) if slot == nil then - error("No more blocks available to build") + error("No more blocks available to build" .. tostring(turtle.getItemCount())) end turtle.select(slot) end end -local function placeDownItem(itemname) +--- +---@param self Builder_Lib +---@param itemname string +local function placeItem(self, itemname) selectItemToPlace(itemname) - if turtle.detectDown() then - local _, down = turtle.inspectDown() - turtleController:tryAction("digD") + local function action() + if (self.placeDirection == "down") then + if turtle.detectDown() then + local _, down = turtle.inspectDown() + turtleController:tryAction("digD") + end + return turtle.placeDown() + else + if turtle.detectUp() then + local _, down = turtle.inspectUp() + turtleController:tryAction("digU") + end + return turtle.placeUp() + end end - local succ, txt = turtle.placeDown() + local succ, txt = action() if not succ then error(txt) end end @@ -82,10 +97,10 @@ function Builder_Lib:floor(length, width) if block == nil then error("No block at item slot 1") end for j = 1, width, 1 do for i = 1, length - 1, 1 do - placeDownItem(block.name) + placeItem(self, block.name) turtleController:goStraight(1) end - placeDownItem(block.name) + placeItem(self, block.name) if (j < width) then getTurningDirection(self, j) turtleController:goStraight(1) diff --git a/builder.lua b/builder.lua index f129a28..7db64f3 100644 --- a/builder.lua +++ b/builder.lua @@ -13,10 +13,11 @@ local function help() print("Options:") print("-mH[movementDir. Horizon] <'left'|'right'> [default: right]") print("-mV[movementDir. Vertical] <'down'|'up'> [default: up]") + print("-p (place) up | down") print("-w[width] ") print("-l[length] ") print("-h[height] ") - print("-help") + print("help") end if #arg == 0 then help() @@ -58,9 +59,9 @@ local argsSwitch = { end size["height"] = tonumber(arg[no[1]]) end, - ["-help"] = function() - help() - stopExec = true + ["-p"] = function(no) + no[1] = no[1] + 1 + builder.placeDirection = arg[no[1]] end, ["help"] = function() help() diff --git a/tests/floor_spec.lua b/tests/floor_spec.lua index d637a1e..0660492 100644 --- a/tests/floor_spec.lua +++ b/tests/floor_spec.lua @@ -181,6 +181,28 @@ describe("Testing placing Floor", function () assert.are.equal(100, blocksTested) end) + it("Create 10x10 floor - to Left - above", function() + assert.are.equal(0, countTableLength(turtleEmulator.blocks)) + local succ, err = xpcall(function () + builder.movementDirection.width = "left" + builder.placeDirection = "up" + builder:floor(10, 10) + end, debug.traceback) + 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 + assert.is.truthy( + turtleEmulator:getBlock(vector.new(i, 1, j)) + ) + end + end + assert.are.equal(100, blocksTested) + end) + it("Error on too few Blocks", function() local blocksToTest = blockAmount * 2 + 1