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

Increasing the frequency of my fuel/inventory checks?

Started by Frantiq, 21 April 2013 - 01:36 PM
Frantiq #1
Posted 21 April 2013 - 03:36 PM
Hey. I'm futzing with my own quarry program (the point of which over the default is to mine three layers at once), and I'm coming across an issue.

Look at the following:
Spoiler

loc = {x = 0, y = 0, z = 0}
savedLoc = {x = 0, y = 0, z = 0}
direction = 0
savedDir = 0
function turnLeft()
turtle.turnLeft()
direction = direction - 1
if direction < 0 then
  direction = direction + 4
end
end
function turnRight()
turtle.turnRight()
direction = direction + 1
if direction > 3 then
  direction = direction - 4
end
end
function forceTurn(dir)
local dirVal = 0
if dir == "forward" then
  dirVal = 0
  elseif dir == "right" then
  dirVal = 1
  elseif dir == "back" then
  dirVal = 2
  elseif dir == "left" then
  dirVal = 3
end
if (dirVal + 2) == direction or (dirVal - 2) == direction then
  turnRight()
  turnRight()
  elseif (direction + 1) == dirVal or (direction - 3) == dirVal then
  turnRight()
  elseif (direction - 1) == dirVal or (direction + 3) == dirVal then
  turnLeft()
end
end
function forceMove(dir,dist)
local moveDir = dir
local moveDist = dist
if moveDist == nil then
  moveDist = 1
end
if dir == nil then
  moveDir = "forward"
end
if moveDir == "up" then
  for elapsedForce = 1,moveDist do
   while move("up") == false do
	turtle.digUp()
   end
  end
  elseif moveDir == "down" then
  for elapsedForce = 1,moveDist do
   while move("down") == false do
	turtle.digDown()
   end
  end
  elseif moveDir == "forward" then
  for elapsedForce = 1,moveDist do
   while move() == false do
	turtle.dig()
   end
  end
end
end
function returnOriginZ()
while (loc.z < 0) do
  while move("up") == false do
   turtle.digUp()
  end
end
while (loc.z > 0) do
  while move("down") == false do
   turtle.digDown()
  end
end
end
function returnOriginX()
if loc.x > 0 then
  forceTurn("left")
  while loc.x > 0 do
   while move() == false do
	turtle.dig()
   end
  end
  elseif loc.x < 0 then
  forceTurn("right")
  while loc.x < 0 do
   while move() == false do
	turtle.dig()
   end
  end
end
end
function returnOriginY()
if loc.y > 0 then
  forceTurn("back")
  while loc.y > 0 do
   while move() == false do
	turtle.dig()
   end
  end
  elseif loc.y < 0 then
  forceTurn("forward")
  while loc.y < 0 do
   while move() == false do
	turtle.dig()
   end
  end
end
end
function saveLoc()
savedLoc.x = loc.x
savedLoc.y = loc.y
savedLoc.z = loc.z
savedDir = direction
end
function returnSavedZ()
while (savedLoc.z > loc.z) do
  while move("up") == false do
   turtle.digUp()
  end
end
while (savedLoc.z < loc.z) do
  while move("down") == false do
   turtle.digDown()
  end
end
end
function returnSavedX()
if (savedLoc.x < loc.x) then
  forceTurn("left")
  while (savedLoc.x < loc.x) do
   while move() == false do
	turtle.dig()
   end
  end
  elseif (savedLoc.x > loc.x) then
  forceTurn("right")
  while (savedLoc.x > loc.x) do
   while move() == false do
	turtle.dig()
   end
  end
end
end
function returnSavedY()
if savedLoc.y < loc.y then
  forceTurn("back")
  while savedLoc.y < loc.y do
   while move() == false do
	turtle.dig()
   end
  end
  elseif savedLoc.y > loc.y then
  forceTurn("forward")
  while savedLoc.y > loc.y do
   while move() == false do
	turtle.dig()
   end
  end
end
end
function returnSavedDir()
forceTurn(savedDir)
end
function move(dir)
if dir == nil or dir == "forward" then
  if turtle.forward() == true then
   if direction == 0 then
	loc.y = loc.y + 1
	elseif direction == 3 then
	 loc.x = loc.x - 1
	elseif direction == 2 then
	 loc.y = loc.y - 1
	elseif direction == 1 then
	 loc.x = loc.x + 1
   end
   return true
   else
   return false
  end
  elseif dir == "up" then
  if turtle.up() == false then
   return false
   else
   loc.z = loc.z + 1
   return true
  end
  elseif dir == "down" then
  if turtle.down() == false then
   return false
   else
   loc.z = loc.z - 1
   return true
  end
end
end
function determineEnderChest()
if turtle.getItemCount(16) == 1 then
  print("EnderChest detected.")
  return true
  else
  print("Using chest to the left for startup.")
  return false
end
end
function determineScrapSlots()
for i = 2,16 do
  if turtle.getItemCount(i) == 0 then
   return (i - 1)
  end
end
end
function digCycle(distCycle)
for activeCycle = 1,distCycle do
  forceMove("forward",1)
  if turtle.detectUp() then
   turtle.digUp()
  end
  if turtle.detectDown() then
   turtle.digDown()
  end
end
end
function getDistToOrigin()
return (math.abs(loc.x) + math.abs(loc.y) + math.abs(loc.z) + 10)
end
function smartDock()
for scrapCheck = 2,scrapSlots do
  if turtle.getItemCount(scrapCheck) > 32 then
   turtle.select(scrapCheck)
   turtle.drop(turtle.getItemCount(scrapCheck) - 1)
   turtle.select(1)
   print("Scrap in slot "..scrapCheck.." dumped.")
  end
end
if enderChest == true then
  if turtle.getItemCount(14) > 0 then
   print("Dumping into EnderChest.")
   forceMove("up",1)
   forceMove("down",1)
   turtle.select(16)
   turtle.placeUp()
   for dropSlot = (scrapSlots + 1),15 do
	turtle.select(dropSlot)
	turtle.dropUp()
   end
   turtle.select(16)
   turtle.digUp()
   turtle.select(1)
  end
  elseif turtle.getItemCount(15) > 0 then
  print("Returning for inventory dump.")
  saveLoc()
  returnOriginZ()
  returnOriginX()
  returnOriginY()
  forceTurn("left")
  print("Dumping inventory.")
  for cargoDropSlot = (scrapSlots + 1),16 do
   turtle.select(cargoDropSlot)
   turtle.drop()
  end
  turtle.select(1)
  print("Returning to saved location.")
  returnSavedY()
  returnSavedX()
  returnSavedZ()
  returnSavedDir()
  print("Resuming.")
end
if turtle.getFuelLevel() < (80) then
  turtle.select(1)
  if turtle.getItemCount(1) > 1 then
   turtle.refuel(1)
   print("Using refuel item!")
   elseif turtle.getFuelLevel() > (math.abs(loc.x) + math.abs(loc.y) + math.abs(loc.z) + 10) then
   saveLoc()
   returnOriginZ()
   returnOriginX()
   returnOriginY()
   while turtle.getItemCount(1) == 1 do
	sleep(1)
   end
   turtle.select(1)
   turtle.refuel(1)
   returnSavedY()
   returnSavedX()
   returnSavedZ()
   returnSavedLoc()
  end
end
end
function smartCycle()
smartDock()
if loc.y == digLength then
  forceTurn("back")
  digCycle(digLength - 1)
  elseif loc.y == 1 then
  forceTurn("forward")
  digCycle(digLength - 1)
end
end
function layerDig()
for layerElapsed = 1,digWidth do
  smartCycle()
  if (layerElapsed < digWidth) then
   forceTurn(layerDirection)
   digCycle(1)
  end
end
end
function prepLayer()
turtle.digDown()
move("down")
turtle.digDown()
move("down")
turtle.digDown()
if move("down") == false then
  atBedrock = true
  forceMove("up",1)
end
turtle.digDown()
if loc.x == (digWidth) then
  layerDirection = "left"
  else
  layerDirection = "right"
end
end
tArgs = {...}
if #tArgs ~= 2 then
argsClear = 0
term.clear()
term.setCursorPos(1,1)
print("intelliquarry [length] [width]")
print("Place coal in the first slot.")
print("Place items to filter out in the")
print("next few slots.")
print("If you have a EnderChest, put it")
print("in the last slot.")
else argsClear = 1
end
atBedrock = false
digLength = tonumber(tArgs[1])
digWidth = tonumber(tArgs[2])
enderChest = determineEnderChest()
scrapSlots = determineScrapSlots()
layerDirection = "right"
if argsClear ~= 0 then
turtle.select(1)
if turtle.getFuelLevel() == 0 then
  turtle.refuel(1)
  print("Using refuel item!")
end
digCycle(digLength)
forceTurn("right")
digCycle(1)
layerDig()
while atBedrock == false do
  prepLayer()
  layerDig()
end
returnOriginZ()
returnOriginX()
returnOriginY()
forceTurn("left")
for i = 1,16 do
  turtle.select(i)
  turtle.drop()
end
turtle.select(1)
forceTurn("forward")
end

I want the "smartDock()" function to do its thing every time the turtle moves. However, I can't incorporate it into my "move()" function, since the "smartDock()" function already uses "move()" itself, thereby creating an infinite loop. How should I go about this?
Spongy141 #2
Posted 21 April 2013 - 05:23 PM
Try moving the code from "smartCycle" to "move" so it will update as it moves… or just call the function more often.
PS do …
Frantiq #3
Posted 21 April 2013 - 09:25 PM
Try moving the code from "smartCycle" to "move" so it will update as it moves… or just call the function more often.
PS do …

As mentioned in the post, that won't work. The "smartCycle" function calls "move" inside of it, so by calling "move" I call "smartCycle", which calls "move"… (you get the picture).
Spongy141 #4
Posted 21 April 2013 - 09:44 PM
Well have turtle.refuel() often in almost every function so it will check it, or make a new function all together that checks both fuel and how full its inventory is…
Frantiq #5
Posted 22 April 2013 - 08:35 AM
Well have turtle.refuel() often in almost every function so it will check it, or make a new function all together that checks both fuel and how full its inventory is…

That's EXACTLY what "smartDock" is…
Spongy141 #6
Posted 22 April 2013 - 09:48 AM
Then make a new function that doesn't call move…. its a very simple fix.
Frantiq #7
Posted 23 April 2013 - 09:58 AM
Then make a new function that doesn't call move…. its a very simple fix.

The smartDock function is using tier upon tier of functions, all of which have move built in at some point or another. I'm certain that there's a specialized way to do this. Thank you for your input.