This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Azeranth's profile picture

The Superior Branch Miner

Started by Azeranth, 23 July 2017 - 06:48 PM
Azeranth #1
Posted 23 July 2017 - 08:48 PM
So I have been exceedingly frustrated with the subpar branch mining programs so I took it upon myself to build one myself. I have created it, but I am having some issues with it, mostly an error whenever I try to use

Orientation = Orientation - 1
and it repeatedly give me the error:
Attempt to perform arithmetic __sub on nil and number
I am however absolutely certain that Orientation has a value, I tried changing its default from 0 to 1 even and have it print the value of orientation AND do math with it. Its not nil, but I have no clue whats wrong. please help, the code can be found here https://pastebin.com/jWkctcq6

But also, its code is here

-- Azeranth's Branch Mine Program
-- Version 0.1 Jul 22 2017
-- Declaration Of Functions
function Advance(Position)
if Position == "Top" then
  FuelCheck()
  InventoryCheck()
  TorchCheck()
  if not CentralChestConfig then
   ChestCheck()
  end
  SafeDig()
  SafeDigUp()
  PlaceCobbleUp()
  turtle.turnRight()
  UpdateOrientation("Right")
  PlaceCobble()
  turnAround()
  PlaceCobble()
  turtle.turnRight()
  UpdateOrientation("Right")
  turtle.forward()
  UpdatePosition("Forward")
  return
end
if Position == "Bottom" then
  FuelCheck()
  InventoryCheck()
  TorchCheck()
  if not CentralChestConfig then
   ChestCheck()
  end
  SafeDig()
  PlaceCobbleDown()
  turtle.turnRight()
  UpdateOrientation("Right")
  PlaceCobble()
  turnAround()
  PlaceCobble()
  turtle.turnRight()
  UpdateOrientation("Right")
  turtle.forward()
  UpdatePosition("Forward")
  return
end
end
function turnAround()
turtle.turnRight()
turtle.turnRight()
UpdateOrientation("Right")
UpdateOrientation("Right")
end
function SafeDig()
while turtle.detect() do
  turtle.dig()
  os.sleep(0.5)
end
end
function SafeDigUp()
while turtle.detectUp() do
  Turt.digUp()
  os.sleep(0.5)
end
end
function PlaceCobble()
turtle.select(1)
turtle.place()
end
function PlaceCobbleUp()
turtle.select(1)
turtle.placeUp()
end
function PlaceCobbleDown()
turtle.select(1)
turtle.placeDown()
end
function PlaceTorch()
turtle.select(15)
turtle.place()
end
function PlaceTorchUp()
turtle.select(15)
turtle.placeUp()
end
function PlaceTorchDown()
turtle.select(15)
turtle.placeDown()
end
function Restock()
ResumeX = X
ResumeY = Y
ResumeZ = Z
if not X == 0 and Y == 0 and Z == 0 then
  NavigateHome()
end
for i = 2 , 13 , 1 do
  turtle.select(i)
  turtle.dropDown()
end
if (CentralChestConfig) then
  turtle.select(14)
  turtle.dropDown()
else
  turtle.select(14)
  turtle.turnLeft()
  UpdateOrientation("Left")
  turtle.suck(getItemSpace())
end
turnAround()
turtle.select(15)
turtle.suck(getItemSpace())
turtle.turnLeft()
print(Orientation + 1)
UpdateOrientation("Left")
while turtle.getFuelLimit() > (turtle.getFuelLevel() + 10) do
  turtle.select(16)
  if turtle.suckUp(getItemSpace()) then
   turtle.refuel(getItemCount() - 1)
  else
   break
  end
end
Cobble = getItemCount(1)
Torches = getItemCount(15)
Chests = getItemCount(14)
Fuel = getItemCount(16)
if turtle.getFuelLevel() + 10 < (math.abs(ResumeX) + math.abs(ResumeY) + math.abs(ResumeZ)) then
  Error("Fuel")
end
if turtle.getItemCount(15) < 16 then
  Error("Torches")
end
if not CentralChestConfig then
  if getItemCount(14) < 4 then
   Error("Chests")
  end
end
Resume()
end
function FuelCheck()
turtle.select(16)
turtle.refuel(turtle.getItemCount()-1)
local DistanceHome = 0
DistanceHome = DistanceHome + math.abs(X)
DistanceHome = DistanceHome + math.abs(Y)
DistanceHome = DistanceHome + math.abs(Z)
if DistanceHome + 10 > turtle.getFuelLevel() then
  print("The Turtle will drop below minimum fuel to return home on its next movement. Returning to dump and refuel")
  Restock()
else
  print("The Turtle is ".. DistanceHome .. " Blocks from Home and has enough fuel to continue")
  return
end
end
function InventoryCheck()
local EmptySlots = 0
if getItemCount(2) == 0 then
  EmptySlots = EmptySlots + 1
end
if getItemCount(3) == 0 then
  EmptySlots = EmptySlots + 1
end
if getItemCount(4) == 0 then
  EmptySlots = EmptySlots + 1
end
if getItemCount(5) == 0 then
  EmptySlots = EmptySlots + 1
end
if getItemCount(6) == 0 then
  EmptySlots = EmptySlots + 1
end
if getItemCount(7) == 0 then
  EmptySlots = EmptySlots + 1
end
if getItemCount(8) == 0 then
  EmptySlots = EmptySlots + 1
end
if getItemCount(9) == 0 then
  EmptySlots = EmptySlots + 1
end
if getItemCount(10) == 0 then
  EmptySlots = EmptySlots + 1
end
if getItemCount(11) == 0 then
  EmptySlots = EmptySlots + 1
end
if getItemCount(12) == 0 then
  EmptySlots = EmptySlots + 1
end
if getItemCount(13) == 0 then
  EmptySlots = EmptySlots + 1
end
if CentralChestConfig then
  if getItemCount(14) == 0 then
   EmptySlots = EmptySlots + 1
  end
end
if EmptySlots == 0 then
  print("The Turtle's avaiable inventory has become full and is no longer able to collect unique items. returning to dump and refuel")
  Restock()
else
  print("The Turtle has " .. EmptySlots .. " inventory spaces still available. Continuing Mining")
end
end
function TorchCheck()
turtle.select(15)
if turtle.getItemCount() < 2 then
  print("The Turtle has run out of torches, andis unable to continue lighting Tunnels. Returning to dump and refuel")
  Restock()
else
  print("The Turtle has " .. turtle.getItemCount() .. " torches remaining. Continuing Mining")
end
end
function ChestCheck()
turtle.select(14)
if turtle.getItemCount() < 2 then
  print("The Turtle has run out of chests, and is unable to continue dumoing resources. Returning to dump and refuel")
  Restock()
else
  print("The Turtle has " .. turtle.getItemCount() .. " chests remaining. Continuing Mining")
end
end
function UpdateOrientation(LorR)
Orientation = tonumber(Orientation)
if LorR == "Left" then
  if Orientation == 0 then
   Orientation = 3
  else
   Orientation = Orientation - 1
  end
end
if LorR == "Right" then
  if Orientation == 3 then
   Orientation = 1
  else
  Orientation = Orientation + 1
  end
end
return Orientation
end
function UpdatePosition (Direction)
if Direction == "Forward" then
  if Orientation == 0 then
   X = X + 1
  end
  if Orientation == 1 then
   Z = Z + 1
  end
  if Orientation == 2 then
   X = X - 1
  end
  if Orientation == 3 then
   Z = Z - 1
  end
end
if Direction == "Up" then
Y = Y + 1
end
if Direction == "Down" then
  Y = Y - 1
end
end
function Error(cause)
if cause == "Fuel" then
  print("The Turtle was not able to collect sufficient fuel to continue Mining. Please fill the Fuel Chest and then press ENTER to attempt restock again")
  read()
  Restock()
end
if cause == "Torches" then
  print("The Turtle was not able to collect sufficient torches to continue Mining. Please fill the Torch Chest and then press ENTER to attempt restock again")
  read()
  Restock()
end
if cause == "Chests" then
  print("The Turtle was not able to collect sufficient chests to continue Mining. Please fill the Chest Chest and then press ENTER to attempt restock again")
  read()
  Restock()
end
end
function NavigateHome()
while Y < 0 do
  SafeDigUp()
  turtle.up()
  UpdatePosition("Up")
end
while Y > 0 do
  turtle.digDown()
  turtle.down()
  UpdatePosition("Down")
end
if Z > 0 then
  while Orientation ~= 3 do
   turtle.turnRight()
   UpdateOrientation("Right")
  end
end
if Z < 0 then
  while Orientation ~= 1 do
   turtle.turnRight()
   UpdateOrientation("Right")
  end
end
while Z ~= 0 do
  turtle.forward()
  UpdatePosition("Forward")
end
while Orientation ~= 2 do
  turtle.turnRight()
  UpdateOrientation("Right")
end
while X > 0 do
  turtle.forward()
  UpdatePosition("Forward")
end
end
function Resume()
while X ~= ResumeX do
  turtle.forward()
  UpdatePosition("Forward")
end
if ResumeZ > 0 then
  while Orientation ~= 1 do
   turtle.turnRight()
   UpdateOrientation("Right")
  end
end
if ResumeZ < 0 then
  while Orientation ~= 3 do
   turtle.turnRight()
   UpdateOrientation("Right")
  end
end
while Z ~= ResumeZ do
  turtle.forward()
  UpdatePosition("Forward")
end
if Y > ResumeY then
  turtle.digDown()
  turtle.down()
  UpdatePosition("Down")
end
if Y < ResumeY then
  SafeDigUp()
  turtle.up()
  UpdatePosition("Up")
end
end
-- Global Variables
--[[
X, Y, Z all refer to the Turtles position in relation to where it started from.
This allows it to properly navigate back to its chest to dump, refuel, and Refill Torches.
It is important to keep in mind that the Coordinates are strictly relative.
X increases in the direction the Turtle is facing.
Y increases up from the Turtle.
Z increases to the right of the Turtle. (Which is also the default directio in which the turtle mines)
The Turtle starts at 0,0,0.
The Chest into which it dumps is placed at 0,-1,0
The Chest from which it refuels is at 0,1,0
The Chest from which it gets more Torches is 0,0,1
]]
local X = 0
local Y = 0
local Z = 0
--[[
Resume Coordinates are a set of saved coordinates which are used to navigate back to where the Turtle broke its pattern for any number of reasons, be it no fuel, no torches, or no inventory
]]
local ResumeX = 0
local ResumeY = 0
local ResumeZ = 0
--[[
Length, Depth, and Thickness all refer to the number of tunnels to dig, how long each of those tunnles will be, and how much space goes between each of those tunnels.
]]
local Length = 0
local Depth = 0
local Thickness = 0
--[[
Fuel, Cobble, and Torches are counts of how many torches are in their respective slots.
Fuel goes in Slot 16.
Cobble goes in Slot 1.
Torches go in Slot 15.
If Configured, Chests go in Slot 14
]]
local Fuel = 0
local Cobble = 0
local Torches = 0
local Chests = 0
--[[
Orientation Describes which direction the Turtle is facing in relation to its starting direction. Them being from a limited number of values, 0-3.
0 Is the original Direction of Travel (Positive X)
1 Is to the Right of the original Direction of Travel (Positive Y)
2 Is in the opposite of the original Direction of Travel (Negative X)
3 Is to the Left of the original Directio of Travel (Negative Y)
]]
local Orientation = 0
--[[
Configurations are all user options which will be prompted at program StartUp.
CentralChestConfig is a boolean which determines whether or not the Turtle should rely on a central chest, or place ones along its path when full. This config defaults to true
]]
local CentralChestConfig = true
-- Execution
print (Orientation + 1)
print("--------------------------------------")
print("Welcome to Azeranth's Branch Mining Program v0.1")
print("--------------------------------------")
print()
print("Before continuing. please check the following")
print()
print("1.) Assure that the Turtle is on the desired eye level of your mine and facing the direction you desire it to mine in")
print("Press ENTER to continue...")
read()
print("2.) Place a chest below the Turtle into which all resources will be dumped")
print("3.) Place a chest to the right of the Turtle from which it will pull Torches")
print("4.) Place a chest above the Turtle from which it will pull fuel (Lava Fuel Compatibility not yet implemented)")
print("5.) Place a chest to the left of the Turtle from which it will pull Chests should the Central Chest be configured to false (The Chest in Item 2 is still reqired)")
print("Press ENTER to continue...")
read()
term.clear()
print("How Many Tunnels Would you like to dig?")
Length = tonumber(read())
print("The Turtle will dig " .. Length .. " Tunnels. Processing...")
sleep(1.5)
term.clear()
print("How deep do you want each tunnel to be?")
Depth = tonumber(read())
print("The Turtle will dig each Tunnel " .. Depth .. " blocks deep. Processing...")
sleep(1.5)
term.clear()
print("How many blocks would you like between each tunnel? The recommended value is 3")
Thickness = tonumber(read())
print("The Turtle will leave " .. Thickness .. " blocks between each Tunnel. Processing...")
sleep(1.5)
term.clear()
print("The Turtle will now begin excavation. If at any point it becomes impossible for the turtle to continue, it will wait at its starting point with a message displaying its issues.")
print("--------------------------------------")
Restock()
SafeDig()
turtle.forward()
UpdatePosition("Forward")
local TorchLay = 0
for i=0, (Thickness + 1)*Length, 1 do
turtle.digDown()
if string.sub(i,-1) == 0 then
  PlaceTorchDown()
end
Advance("Top")
end
Restock()
turtle.forward()
UpdatePosition("Forward")
turtle.turnRight()
UpdateOrientation("Right")
Advance("Top")
for u=0, Length, 1 do
for i=0,Depth,1 do
  Advance("Top")
end
turtle.digDown()
turtle.down()
UpdatePosition("Down")
turnAround()
for i=0,Depth,1 do
  Advance("Bottom")
end
turtle.up()
UpdatePosition("Up")
turtle.turnRight()
UpdateOrientation("Right")
for i = 0, Thickness + 1, 1 do
  turtle.forward()
  UpdatePosition("Forward")
end
turtle.turnRight()
UpdateOrientation("Right")
end
Restock()
turtle.forward()
UpdatePosition("Forward")
turtle.turnLeft()
UpdateOrientation("Left")
Advance("Top")
for u=0, Length, 1 do
for i=0,Depth,1 do
  Advance("Top")
end
turtle.digDown()
turtle.down()
UpdatePosition("Down")
turnAround()
for i=0,Depth,1 do
  Advance("Bottom")
end
turtle.up()
UpdatePosition("Up")
turtle.turnLeft()
UpdateOrientation("Left")
for i = 0, Thickness + 1, 1 do
  turtle.forward()
  UpdatePosition("Forward")
end
turtle.turnLeft()
UpdateOrientation("Left")
end
NavigateHome()
Restock()
print("Done!")
Lyqyd #2
Posted 23 July 2017 - 09:25 PM
Moved to Ask a Pro.
KingofGamesYami #3
Posted 23 July 2017 - 10:41 PM
I think this is a scope issue, though I'm not 100% sure. IIRC, your updateOrientation function is using the global, rather than local, value of orientation b/c it hasnt been declared yet.

Move the variables to the top of the file. This should solve that issue.
Azeranth #4
Posted 24 July 2017 - 01:34 AM
I think this is a scope issue, though I'm not 100% sure. IIRC, your updateOrientation function is using the global, rather than local, value of orientation b/c it hasnt been declared yet.

Move the variables to the top of the file. This should solve that issue.

But all of the variables precede the actual execution, or is it a compilation issue where the function doesn't know what variable I'm talking about yet so it instantiates its own?
Bomb Bloke #5
Posted 24 July 2017 - 06:36 AM
The function definitions are executed prior to the point where your localised "orientation" variable is defined. They will indeed be built to refer to a global "orientation" instead.