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

Any ways to improve my simple tunnel script? (not urgent)

Started by AdrenalineDM, 15 August 2014 - 07:30 AM
AdrenalineDM #1
Posted 15 August 2014 - 09:30 AM
So this is the script I came up with for a simple (1 by 3 by X) tunnel, the third variable depending on user input, and I was wondering if anybody had any advice for things I could do or add to improve the script. This was made keeping in mind that gravel does exist and is annoying.

http://pastebin.com/r82nw00f
Edited on 15 August 2014 - 05:50 PM
theoriginalbit #2
Posted 15 August 2014 - 11:03 AM
Please post the script so we can give you suggestions based upon it.

A tutorial that should help you learn techniques in dealing with gravel and various other scenarios.
Edited on 15 August 2014 - 12:29 PM
Maxskywalker #3
Posted 15 August 2014 - 02:00 PM
To deal with the gravel issue, you could use turtle.compare() (and maybe turtle.compareUp()) to detect whether the turtle is about to deal with gravel, and then take action to deal with it (for example, going into a mining loop until the gravel is gone).

Edit: Actually, you could use

while turtle.detect() do turtle.dig() end turtle.digUp()

and save an inventory slot.
Edited on 15 August 2014 - 08:27 PM
AdrenalineDM #4
Posted 15 August 2014 - 07:51 PM
Yeah sorry I wanted to edit the post to add the script but it needed to be approved first and I was asleep. Added it now
Bomb Bloke #5
Posted 16 August 2014 - 03:34 AM
This:

local detectBlockUp = turtle.detectUp()
if detectBlockUp == true then
  turtle.digUp()
end

… can be reduced to this:

if turtle.detectUp() then
  turtle.digUp()
end

The "if" statement already checks to see if your condition resolves as true or false. Specifically asking it to check if a value equals true, is therefore the same as asking it to see if "turtle.detectUp() is true is true".

"read()" returns a string, not a number. Sometimes Lua will let you treat a string as a number, but you'll run into trouble if you try to rely on that behaviour. It's good practise to convert its output to the specific type you want:

local times = tonumber( read() )

Better yet, make sure the user typed a number. Bearing in mind that if you pass an invalid value to tonumber() it'll return nil, and for the purposes of a conditional check a variable set to nil counts as false (whereas most other values evaluate as true), you can do this:

local times
repeat
  term.clear()
  term.setCursorPos(1, 1)
  term.write("Times to run: ")
  times = tonumber( read() )
until times
AdrenalineDM #6
Posted 17 August 2014 - 08:19 AM
This is my code http://www.pastebin.com/ASBZuZtH

What I am wanting my turtle to do is detect if a block is in front of it, above it, or below it. If it detects any block it will mine it. If it doesn't detect a block at all it will move forward. For every time that it moves forward it should print ("Moved Forward: " ..i) (i is equal to the amount of times the user tells the script to run.)

The problem that I am having is that my turtle going one more space than I would like it to, and when it is going that extra space it is not telling me that it 'Moved Forward'.

http://prntscr.com/4dmlpj
Edited on 17 August 2014 - 07:52 AM
sEi #7
Posted 17 August 2014 - 03:26 PM
From your code:

while not turtle.detect() do
	turtle.forward()
end
This code will move the turtle forward until it detects a block in front. And will NOT tell you anything while it moves.

Hope this helps

/sEi
RickiHN #8
Posted 17 August 2014 - 05:30 PM
Could you not do something like this?

	while not turtle.forward() do
		  turtle.dig()
	end


Nvm, missunderstood problem… :P/>
Edited on 17 August 2014 - 03:33 PM
TwitchBlade #9
Posted 17 August 2014 - 06:24 PM
also ive been doing things like this aswell and found that keeping track of fuel when moving forward is a good way to make sure youve cleared an area
and if you wanted to get more reliable you could save that to a file includeing the length moved and the last action done and the length you want to move so if the server restarted in the middle or the chunck unloaded you could continue on digging
Dragon53535 #10
Posted 17 August 2014 - 09:50 PM
Replace your for loop to a repeat loop, i find them more fun :P/> Simple way to do it, (just put your if turtle.detect()'s in there :P/>)

local i=0
repeat
  if turtle.detect() then
	turtle.dig()
  end

  --# put your other if turtle.detect()'s here
  while not turtle.detect() then
	i = i + 1
	print("Turtle Moved: ".. i .. " Times.")
	turtle.forward()
  end
until i == times --# ends the loop when i is the same as what the user inputed.
Edited on 17 August 2014 - 07:54 PM
AdrenalineDM #11
Posted 17 August 2014 - 10:13 PM
My current code makes a turtle place a torch after it has moved forward 10 times

http://pastebin.com/F6PPxu05

I need to figure out how to make the turtle place a torch every 10 blocks, not just after the first 10.

Any help please?

Line 25-29 is my current torch placement code.
Dragon53535 #12
Posted 17 August 2014 - 10:21 PM
if i % 10 == 0 then

If "i" divided by 10's remainder is 0 then which mean if i is a multiple of 10

EDIT: Also it's probably best if you were to just restrict it down to 1 topic, because while you may need help, you're using the same script which it's easier to look back on previous help on just 1 topic :P/> anyways, the solution above can be used for your if i == 60 then statement
Edited on 17 August 2014 - 08:32 PM
AdrenalineDM #13
Posted 17 August 2014 - 10:35 PM
Thank you, and will do
theoriginalbit #14
Posted 18 August 2014 - 02:46 AM
Merged threads.

In the future please try to stick to a single thread for your program, it allows us to easier track your progress and give you better suggestions and help.
subzero22 #15
Posted 18 August 2014 - 07:33 AM
[indent=1]ya my first program was a stripmining program. Since I also play with a lot of people who don't know computercraft I made it userfriendly. Here's some tips and stuff you can make it even better like I have mine. although mine still needs a little bit of tweaking but I'm a protectionist so not sure if I'll ever stop working on it lol.[/indent]

[indent=1]Don't add everything all at once as I added one thing at a time till it worked perfectly. Next I'm attempting to make it so it can save it's spot incase it shuts down or server crashes.[/indent]

[indent=1]1. on the user input make it so it changes it's input to a number. example x = tonumber( read() ) it can make stuff easier when you start having it do math.[/indent]

[indent=1]2. Not only should you make it do single strips but multiple ones. I got mine so it asks the user how many strips it wants and how far apart they want them.[/indent]

[indent=1]3. chest or ender chest support. I got mine to ask the user if they have an ender chest in slot 16 of it. This way once the inv is full it can dump all the items into it or sit and wait for the user to empty it.[/indent]

[indent=1]4. A lot of people that I see that do turtle programs make it so the turtle has to refill. I got mine set up to do the math (see suggestion 1) to make sure the turtle has enough fuel in it to do it's job before it starts. The math I have needs tweaking as it's not perfect but does give the turtle a buffer in how much it says it needs and how much is actually used as it uses a little less than it says it will.[/indent]

[indent=1]5. Torch support. The easiest way I found to do torch is when I put a counter in my move forward function. It will also ask the user if they want the turtle to place torches and how far apart the torches will be. When ever the counter reaches the user input then it will place a torch and reset the counter. Also when placeing torches you should do something like:[/indent]

[indent=1]

turtle.select(15)
if turtle.placeDown() == false then
  if turtle.placeUp() == false then
	turtle.turnLeft()
	turtle.turnLeft()
	turtle.place()
	turtle.turnLeft()
	turtle.turnLeft()
  end
end
turtle.select(1)[/indent]
this makes it so if it can't place it torch down then it tries placing it up. If it can't place it up then it tries placing it behind it. if that fails it will just turn back around and give up turn back around and continue mining.

6. When you do got it to do multiple strips make it so the user can input which direction they want it to strip. example would be it would make a strip then turn left or right before making the next strip.

these are just some suggestions to try out and learn more on coding that I have on my turtle. I also have a floor/wall and flatten area program as well as some others that I make depending on what I need at the time lol. On one server they didn't have pastebin enabled and I had a turtle programed to completely make a reactor complete with wiring it up and filling it with the parts it needs inside it. Only thing I had to do was the restone wiring.

7. Also while you are wanting your turtle to move forward until it detects a block could run into a problem if a mob is in front of it. Here's the code I use.


while not turtle.forward() do
  turtle.dig()
  turtle.attack()
  sleep(0.6) -- sleep incase mob is still alive or gravel falls then it will dig the gravel that falls also.
Edited on 18 August 2014 - 05:37 AM