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

Strip miner inspecting block for "ore" in name

Started by Akxe, 26 April 2015 - 01:08 AM
Akxe #1
Posted 26 April 2015 - 03:08 AM
I have thought of a mining program, that would strip mine, while looking for ores around him, and if he finds one, he mines it in recursive manner…

This is code responsible for checking if it is ore or not

local success, data = turtle.inspect()
if success and string.find(string.lower(data.name),"ore") then
  print(data.name)  --  ore name (minecraft:redstone_ore, or something like that)
end

If there is any existing program, I am sorry, but couldn't find any.

I have tried, but can't think in recursion… help or program is appreciated.

function seek(ore,side)
  local name = string.find(string.lower(ore),":")
  name = string.sub(ore,name+1)
  print("Found " .. name)
  shell.run("refuel")
  if side=="up" then
	turtle.digUp()
	turtle.up()
  elseif side=="down" then
	turtle.digDown()
	turtle.down()
  else
	turtle.dig()
	turtle.forward()
  end
  try("down")
  turtle.back()
end
function try(side)
  if side=="down" then
	local success, data = turtle.inspect()
	if success and string.find(string.lower(data.name),"ore") then
	  seek(data.name)
	else
	  turtle.dig()
	end
	shell.run("refuel")
	turtle.forward()
  else
	local success, data = turtle.inspectUp()
	if success and string.find(string.lower(data.name),"ore") then
	  seek(data.name,"up")
	else
	  turtle.digUp()
	end
	shell.run("refuel")
	turtle.up()
  end
  turtle.turnRight()
  success, data = turtle.inspect()
  if success and string.find(string.lower(data.name),"ore") then
	seek(data.name)
  end
  turtle.turnLeft()
  turtle.turnLeft()
  success, data = turtle.inspect()
  if success and string.find(string.lower(data.name),"ore") then
	seek(data.name)
  end
  turtle.turnRight()
  if side=="down" then
	success, data = turtle.inspectDown()
  else
	success, data = turtle.inspectUp()
  end
  if success and string.find(string.lower(data.name),"ore") then
	seek(data.name,side)
  end
end
local left = tonumber(...)
local level = 0
if left == nil then
  print("How far should it try to dig?")
  left = tonumber(io.read())
end
while left > 0 do
  try("down")
  shell.run("refuel")
  turtle.up()
  try("up")
  shell.run("refuel")
  turtle.down()
  left = left -1
end
print("I am done")
Creator #2
Posted 26 April 2015 - 11:53 AM
I think it works fine waht you did
Emma #3
Posted 26 April 2015 - 03:37 PM
If by "I can't think in a recursive manner" you mean by using recursion to mine the ore, I think I can help you out.
So the way I would go about this is creating a table to keep track of where you have been and what sides you have checked in each location you have been, and for each ore you break go into it's space and then go back, that way, its like a stack, and you get all the ore that is connected to other ore.
Here is an example (I can't guarantee that it will work as I am typing in the browser not MC)
EDIT: Just kidding, kinda long so here is a pastebin: http://pastebin.com/Nx88KCRR
EDIT 2: Just as a side note, my example is extremely inefficient and could be improved in many ways, and while it does work (Now, I fixed some bugs with it), it is not something you would want in a high quality program
Edited on 27 April 2015 - 01:44 AM
Akxe #4
Posted 27 April 2015 - 08:54 AM
<div>Made it :)/>
</div>
<p>function ScanAndDig()<br />
  local success, data = turtle.inspect()                      --  Forward<br />
  if success and string.find(string.lower(data.name),&amp;quot;ore&amp;quot;) then<br />
    turtle.dig()<br />
    shell.run(&amp;quot;refuel&amp;quot;)<br />
    turtle.forward()<br />
    ScanAndDig()<br />
    shell.run(&amp;quot;refuel&amp;quot;)<br />
    turtle.back()<br />
  end<br />
  local success, data = turtle.inspectUp()                    --  Above<br />
  if success and string.find(string.lower(data.name),&amp;quot;ore&amp;quot;) then<br />
    turtle.digUp()<br />
    shell.run(&amp;quot;refuel&amp;quot;)<br />
    turtle.up()<br />
    ScanAndDig()<br />
    shell.run(&amp;quot;refuel&amp;quot;)<br />
    turtle.down()<br />
  end<br />
  local success, data = turtle.inspectDown()                  --  Below<br />
  if success and string.find(string.lower(data.name),&amp;quot;ore&amp;quot;) then<br />
    turtle.digDown()<br />
    shell.run(&amp;quot;refuel&amp;quot;)<br />
    turtle.down()<br />
    ScanAndDig()<br />
    shell.run(&amp;quot;refuel&amp;quot;)<br />
    turtle.up()<br />
  end<br />
  turtle.turnRight()<br />
  local success, data = turtle.inspect()                      --  Right<br />
  if success and string.find(string.lower(data.name),&amp;quot;ore&amp;quot;) then<br />
    turtle.dig()<br />
    shell.run(&amp;quot;refuel&amp;quot;)<br />
    turtle.forward()<br />
    ScanAndDig()<br />
    shell.run(&amp;quot;refuel&amp;quot;)<br />
    turtle.back()<br />
  end<br />
  turtle.turnLeft()<br />
  turtle.turnLeft()<br />
  local success, data = turtle.inspect()                      --  Left<br />
  if success and string.find(string.lower(data.name),&amp;quot;ore&amp;quot;) then<br />
    turtle.dig()<br />
    shell.run(&amp;quot;refuel&amp;quot;)<br />
    turtle.forward()<br />
    ScanAndDig()<br />
    shell.run(&amp;quot;refuel&amp;quot;)<br />
    turtle.back()<br />
  end<br />
  turtle.turnRight()<br />
end</p>
<p>local left = tonumber(...)<br />
local level = 0<br />
if left == nil then<br />
  print(&amp;quot;How far should it try to dig?&amp;quot;)<br />
  left = tonumber(io.read())<br />
end</p>
<p>while left &amp;gt; 0 do<br />
  ScanAndDig()<br />
  turtle.digUp()<br />
  turtle.dig()<br />
  shell.run(&amp;quot;refuel&amp;quot;)<br />
  turtle.forward()<br />
  left = left - 1<br />
end<br />
print(&amp;quot;I am done&amp;quot;)<br />
</p>
Edited on 27 April 2015 - 10:35 PM
Emma #5
Posted 28 April 2015 - 12:03 AM
Why should it use a table? Woudn't be simplier if he had a fixed scan function (forward, up, down, left, right) and if he see an ore he would dig it, scan at place o the ore, then go back
scan
found (front)
dig
move forward
scan
move back
found (up)

That is basically what I said, just the table makes it more efficient