Jump to content




Mining turtle program


43 replies to this topic

#21 saikit555

  • Members
  • 25 posts

Posted 02 June 2013 - 11:19 AM

 angellus, on 01 June 2013 - 01:29 PM, said:

here you go:
 function path() while not turtle.detectDown() do local slot = 2 -- starting slot turtle.select(slot) while not turtle.placeDown() do -- loop through until success slot = slot + 1 if slot > 16 then term.write("Enter more materials. and press ENTER") -- out of stuff D: read() slot = 2 end turtle.select(slot) end end end local refuelSlot = 1 turtle.select(refuelSlot) turtle.refuel() 
What's the differents between term.write and print? They seem to do the same thing..but there's a differents right?
Edit: and the refuel i was wondering how to set a variable for how many coal it consumes each time not which slot it uses.
Edit:
Spoiler

using a lua editor to edit this in. when debug gets Syntax: then expected near '&'.why?

#22 GopherAtl

  • Members
  • 888 posts

Posted 02 June 2013 - 11:21 AM

the write functions do not add a newline at the end, so you use write for prompts where you want the user to type into the same line, or when writing text to the bottom of the screen without making it scroll up.

There's term.write() and write(), which have a difference. term.write doesn't understand escape characters like \n, which can be used to manually insert newlines in output. write() does understand these. write() also does word-based line wrapping when it goes past the edge of the screen, while term.write() does not.

#23 saikit555

  • Members
  • 25 posts

Posted 02 June 2013 - 02:27 PM

hmm...what happens if i use a for loop for torch placing but my mining length is not a multiple of it.
for example could i do:
local length = 50
local torchInterval = 8

for i = 1,length/torchInterval do
           main() --e.g. main is a code that mines 8 rows at a time
end

what would happen?

#24 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 02 June 2013 - 06:06 PM

The loop will round your division results down. 50/8 = 6.25, so main() will be called six times, and at eight rows/per that means 48 rows will be dug.

#25 GopherAtl

  • Members
  • 888 posts

Posted 02 June 2013 - 10:31 PM

it would be more conventioanl to loop over the total distance, and use % (modulo, or remainder of integer division) to space torches, example:

local spacing=8

for i=1,length do
  turtle.dig()
  turtle.forward()
  turtle.digUp()
  --this will be true every "spacing" blocks
  if i%spacing==0 then
    turtle.place() --assumes torches selected
  end
end

This is obviously just an example, not a very smart shaft program in general but it demonstrates using % to do something only at intervals in a loop.

#26 saikit555

  • Members
  • 25 posts

Posted 03 June 2013 - 04:21 AM

 GopherAtl, on 02 June 2013 - 10:31 PM, said:

it would be more conventioanl to loop over the total distance, and use % (modulo, or remainder of integer division) to space torches, example:
 local spacing=8 for i=1,length do turtle.dig() turtle.forward() turtle.digUp() --this will be true every "spacing" blocks if i%spacing==0 then turtle.place() --assumes torches selected end end 
This is obviously just an example, not a very smart shaft program in general but it demonstrates using % to do something only at intervals in a loop.
could you explain more? when would i%spacing == 0?

#27 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 03 June 2013 - 04:33 AM

 saikit555, on 03 June 2013 - 04:21 AM, said:

could you explain more? when would i%spacing == 0?
modulo/modulus (%) divides number 1 (left) by number 2 (right) until it can no longer be divided and returns the remainder after so. For example:

9 % 3 = 0 as 3 goes into 9 3 times with a remainder of 0
10 % 3 = 1 as 3 goes into 10 3 times with a remainder of 1
11 % 3 = 2 as 3 goes into 11 3 times with a remainder of 2
12 % 3 = 0 as 3 goes into 12 4 times with a remainder of 0

example with another number
20 % 5 = 0
21 % 5 = 1
22 % 5 = 2
23 % 5 = 3
24 % 5 = 4
25 % 5 = 0
26 % 5 = 1
etc......

does that make sense?

#28 saikit555

  • Members
  • 25 posts

Posted 03 June 2013 - 05:11 AM

 theoriginalbit, on 03 June 2013 - 04:33 AM, said:

 saikit555, on 03 June 2013 - 04:21 AM, said:

could you explain more? when would i%spacing == 0?
modulo/modulus (%) divides number 1 (left) by number 2 (right) until it can no longer be divided and returns the remainder after so. For example: 9 % 3 = 0 as 3 goes into 9 3 times with a remainder of 0 10 % 3 = 1 as 3 goes into 10 3 times with a remainder of 1 11 % 3 = 2 as 3 goes into 11 3 times with a remainder of 2 12 % 3 = 0 as 3 goes into 12 4 times with a remainder of 0 example with another number 20 % 5 = 0 21 % 5 = 1 22 % 5 = 2 23 % 5 = 3 24 % 5 = 4 25 % 5 = 0 26 % 5 = 1 etc...... does that make sense?
yes. thanks.

#29 saikit555

  • Members
  • 25 posts

Posted 03 June 2013 - 05:23 AM

Does a program remember an argument variable until the program ends?

example:
local x = tonumber (args [1]) --i entered 1
local y = tonumber (args [2]) -- i entered 2

if #args ~= 2 then
   print("Syntax: <x> <y>")
  return
end

print(x) --these would still be 1 and 2?
print(y)



#30 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 03 June 2013 - 05:35 AM

all localised variables and functions are stored in memory until they are no longer referenced, which most of the time is when the program has finished running. They can last longer than the program runtime, very rarely do the last less than the program runtime.

#31 saikit555

  • Members
  • 25 posts

Posted 03 June 2013 - 05:59 AM

Spoiler

using a lua editor to edit this in. when debug gets Syntax: then expected near '&amp;'.why?

#32 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 03 June 2013 - 06:09 AM

because this
if slot &amp;gt; 16 then
is not valid at all in lua. &amp;gt; is not lua
it should read something like this (i assume)
if slot > 16 then
not too sure what you intend to actually do with this. also your term.write string is missing an opening "
lastly if you use term.write you will not have any line wrapping and such, you're better to use write or print.

#33 saikit555

  • Members
  • 25 posts

Posted 03 June 2013 - 08:29 AM

 saikit555, on 01 June 2013 - 11:49 AM, said:

Found something wrong with my program.
 function path() while not turtle.dectectDown() do turtle.select(2) turtle.placeDown() end end 
If it mines something else and place it in slot 2 like(e.g. coal, diamonds,redstone,etc.) it will just stop. How do i make it try slot 2 and if it fails try slot 3 etc. until it succeeds.(or should i just make it keep a slot with blocks just to path?) Edit: oh and i try to do
 local refuel = 1 turtle.refuel(refuel) --and turtle.refuel() = refuel 
both doesn't work. How do you do this properly?

 angellus, on 01 June 2013 - 01:29 PM, said:

here you go:
 
function path() 
while not turtle.detectDown() do 
          local slot = 2 -- starting slot 
          turtle.select(slot) 
          while not turtle.placeDown() do -- loop through until success 
                    slot = slot + 1 
                     if slot &amp;amp;gt; 16 then 
                        term.write("Enter more materials. and press ENTER") -- out of stuff D: 
                        read() 
                        slot = 2 
                     end 
                     turtle.select(slot) 
         end 
end 
 end 

 theoriginalbit, on 03 June 2013 - 06:09 AM, said:

because this
if slot &amp;amp;amp;gt; 16 then
is not valid at all in lua. &amp;amp;amp;gt; is not lua it should read something like this (i assume)
if slot &amp;gt; 16 then
not too sure what you intend to actually do with this. also your term.write string is missing an opening " lastly if you use term.write you will not have any line wrapping and such, you're better to use write or print.
So how would i do it?

#34 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 03 June 2013 - 08:44 AM

 saikit555, on 03 June 2013 - 08:29 AM, said:

So how would i do it?
Do what?

#35 saikit555

  • Members
  • 25 posts

Posted 03 June 2013 - 08:49 AM

 theoriginalbit, on 03 June 2013 - 08:44 AM, said:

 saikit555, on 03 June 2013 - 08:29 AM, said:

So how would i do it?
Do what?
A path function.
function path()
         while not turtle.dectectDown() do 
                   turtle.select(2) 
                   turtle.placeDown()
         end 
end 
it trys slot 2 first if it not place-able(example:redstone) i moves on to slot 3, 4 etc. until it places it.

#36 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 03 June 2013 - 09:16 AM

Quote

if slot &amp;amp;amp;gt; 16 then

:D

function path()
         local pathcounter = 2
         while not turtle.dectectDown() do 
                   turtle.select(pathcounter)
                   turtle.placeDown()
                   pathcounter = pathcounter + 1
         end 
end


#37 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 03 June 2013 - 09:29 AM

 Bomb Bloke, on 03 June 2013 - 09:16 AM, said:

:D

function path()
		 local pathcounter = 2
		 while not turtle.dectectDown() do
				   turtle.select(pathcounter)
				   turtle.placeDown()
				   pathcounter = pathcounter + 1
		 end
end
The turtle.select statement would error the program when pathcounter is above 16, hence the if statement, which saikit555 can't seem to put into the program because > keeps turning into &gt;

EDIT: So that being said, here is the solution that html won't mess up on you...
Spoiler

Edited by theoriginalbit, 03 June 2013 - 09:46 AM.


#38 saikit555

  • Members
  • 25 posts

Posted 03 June 2013 - 02:01 PM

Thanks for all your help. :)/&gt;/&amp;gt;

Here is the edited code after all the help with it. Still not done with it though.
Spoiler


Edit:What do you think of the code so far?

#39 saikit555

  • Members
  • 25 posts

Posted 06 June 2013 - 10:58 AM

After using it a few time found something wrong with it. When the turtle is doing the mine function if something gets under it while it tries to do turtle.down if will get confuse and not know what to do. Any solution?

Edit: It also some times get stuck moving forward. I'm unsure why. Either its something wrong with the code or something to do with a weird bug i been getting where ocelot keep spawning everywhere all the time.

Edit: figured out the moving forward part. forgot to add more refuel commands when editing code.

#40 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 06 June 2013 - 10:19 PM

Make sure you check fuel, make sure you allow for gravel/sand, make sure you check for mobs. An example with forward movement:

local function moveForward()
  --# keep trying to move forward until you can
  while not turtle.forward() do
    --# is a block in the way?
    if turtle.detect() then
      while turtle.detect() do
        turtle.dig()
        sleep(0.8) --# wait for sand/gravel to fall
      end
    --# out of fuel?
    elseif turtle.getFuelLevel() == 0 then
      --# refuel it in some manner
    --# a mob is in the way?
    elseif turtle.attack() then
      --# attack and kill the mob
      while turtle.attack() do end
    end
  end
end






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users