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

Railway Turtle: Lua related

Started by Jordansinn, 04 April 2013 - 07:57 AM
Jordansinn #1
Posted 04 April 2013 - 09:57 AM
I am writing a code for a turtle to mine through various things and lay tracks along the way. I am stuck as to what I should do to allow it to place more then the 64 minecarts it has in its inventory. I need a way to have it switch to using a different inventory location that has more tracks. I am thinking I could do some sort of if and then statement for it but I am unsure as to how I would execute this.


– Sets program up –
while true do
term.clear()
term.setCursorPos(12,1)
print("Made by Jordan Sinn")
term.setCursorPos(1,2)
print("Still in Alpha")
term.setCursorPos(1,3)
print("Multiple of 10")
term.setCursorPos(1,4)
print("Distance:")
input=read()

– Laying tracks –
for i = 1, input do
for i = 1,9 do
turtle.dig()
turtle.forward()
turtle.digDown()
turtle.select(1)
turtle.placeDown()
end

– Laying powered tracks –
turtle.dig()
turtle.forward()
turtle.digDown()
turtle.down()
turtle.digDown()
turtle.down()
turtle.digDown()
turtle.select(3)
turtle.placeDown()
turtle.up()
turtle.select(4)
turtle.placeDown()
turtle.up()
turtle.select(2)
turtle.placeDown()
end
end
Telokis #2
Posted 04 April 2013 - 10:00 AM
Could you use the "Code" button, please ? It would be easier to look at you code if you did so.
Bubba #3
Posted 04 April 2013 - 10:04 AM

local selected = 1

local function select()
 
  for i=1,16 do
	if turtle.getItemCount(i) > 0 then
	  turtle.select(i)
	  return true
	end
  end
  error("There is no track left")
end

--Call this if the program fails to place a track like so:
if not turtle.place() then
    select()
end

Is that something like what you're looking for?
LBPHacker #4
Posted 04 April 2013 - 10:09 AM
-snip-
*shivers* Do not overwrite select please :D/> (I know it's local but still…)
Bubba #5
Posted 04 April 2013 - 10:12 AM
-snip-
*shivers* Do not overwrite select please :D/> (I know it's local but still…)

I've never encountered select before, although I see from testing that it is indeed a function. Honestly it doesn't look that useful anyway. I suppose I can see it for things like filtering out the first argument of os.pullEvent or some such thing, but I always put the results of os.pullEvent in a table anyway so it's a bit pointless.
Jordansinn #6
Posted 05 April 2013 - 03:30 PM
Thanks everyone and sorry about not using the code button. How would I go about incorporating this into my code, just place it at the top or…
PixelToast #7
Posted 05 April 2013 - 03:56 PM
you have to include everything Bubba said at the top of the code
and then put this:

if not turtle.place() then
    select()
end
whenever you want to place tracks
Jordansinn #8
Posted 07 April 2013 - 10:38 PM
This is my updated code. The program stops when it goes to lay the powered track and returns a nil. Im over my head at this point

local selected = 1
local function select()

  for i=1,16 do
	    if turtle.getItemCount(i) > 0 then
		  turtle.select(i)
		  return true
	    end
  end
  error("There is no track left")
end
--Call this if the program fails to place a track like so:
if not turtle.place() then
    select()
end
--Sets program up
while true do
term.clear()
term.setCursorPos(12,1)
print("Made by Jordan Sinn")
term.setCursorPos(1,2)
print("Still in Alpha")
term.setCursorPos(1,3)
print("Multiple of 10")
term.setCursorPos(1,4)
print("Distance:")
input=read()

-- Laying tracks --
  for i = 1, input do
  for i = 1,9 do
  turtle.dig()
  turtle.forward()
  turtle.digDown()
  turtle.select(1)
    if not turtle.place() then
	  select()
    end
  turtle.placeDown()
  end
-- Laying powered tracks --
  turtle.dig()
  turtle.forward()
  turtle.digDown()
  turtle.down()
  turtle.digDown()
  turtle.down()
  turtle.digDown()
  turtle.select(3)
  turtle.placeDown()
  turtle.up()
  turtle.select(4)
  turtle.placeDown()
  turtle.up()
  turtle.select(2)
  turtle.placedown()
  end
end
PixelToast #9
Posted 08 April 2013 - 05:09 AM
.-.
i fixed some things, im not sure it will work though
http://ptoast.tk/sl/Jordansinn
EDIT:
lolderp, i forgot i had placedown instead of placeDown
syntax highlighting ftw!
EDIT2:
forgot something else

also a note: put all the rails on the last 8 slots kthx
remiX #10
Posted 08 April 2013 - 05:52 AM
How did you do that Pixel? :P/>
PixelToast #11
Posted 08 April 2013 - 06:00 AM
How did you do that Pixel? :P/>
the syntax highlighting or the fix?
remiX #12
Posted 08 April 2013 - 06:02 AM
syntax highlighting. I mean, I doubt you spent your day highlighting every word perfectly :P/>
Kilobyte #13
Posted 08 April 2013 - 11:50 AM
Actually, please indent your code properly. it makes it much easier for us to help and for you to understand the code after some time. Don't know what i mean? example:
Unindented:

if foo then
for k, v in ipairs(bar) do
print(k..", "..v)
end
end

Indented:

if foo then
    for k, v in ipairs(bar) do
	    print(k..", "..v)
    end
end
Jordansinn #14
Posted 09 April 2013 - 12:14 PM
thanks for the help pixle. I tried to run the program but the turtle loses the items as moves. It spits them out and moves away. I think another way of getting this to work is needed.
LordIkol #15
Posted 09 April 2013 - 11:48 PM
Actually i dont know what you have in slot 1 to 9 but i think the problem is that in the code of Pixel there is a lot of turtle.drop()
change them to turtle.placeDown() or turtle.place() where you have tracks or torches etc. and it should work.

Greets Loki
PixelToast #16
Posted 10 April 2013 - 01:18 AM
syntax highlighting. I mean, I doubt you spent your day highlighting every word perfectly :P/>
i used a php script to generate it, what else :P/>
Actually i dont know what you have in slot 1 to 9 but i think the problem is that in the code of Pixel there is a lot of turtle.drop()
change them to turtle.placeDown() or turtle.place() where you have tracks or torches etc. and it should work.

Greets Loki
nope, the places are correct
im using turtle.drop so dirt,etc dosent overflow to other slots, lemme modify the program some more