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

New to CC and singing the frame quarry blues

Started by arempi, 05 December 2013 - 07:13 PM
arempi #1
Posted 05 December 2013 - 08:13 PM
Hello Everyone,

This is my first post on the forum and first time playing with cc. I dont have much, if any programming or scripting experience so this might be a simple question - or a funny monkey rigged concept. I'm trying to make a Redstone in motion mining well quarry with control via CC. I'm using the "carriage engine" as the pilot for platform and utilizing rs.setOutput give it a redstone pulse. One of the first real issues I've noticed was that Computers and Turtles reboot when moved. To over come this I came up with the idea of having a control computer on the ground somewhere -rednet.send to 4 wireless turtles sorrounding the carriage engine. I dont really know the range of the rednet signals?

I started looking for existing programs out there and found this:

Spoiler

term.clear()
term.setCursorPos(1,1)
local first
local number
local notANumber
local counter
local current
local backToStartInput
backToStartInput = "no"
blue = "colors.green"
counter = 0
notANumber = false
first = true
firstbackToStart = true
function whatDirection()
while true do
  term.clear()
  term.setCursorPos(1,1)
  if first == true then
   first = false
   print ("What direction do you want to go?")
   print (" - up")
   print (" - down")
   print (" - north")
   print (" - east")
   print (" - south")
   print (" - west")
   directionInput = read ()
  else
   print ("Not a valid direction, please type a direction.")
   print ("example: up")
   print ("")
   print ("What direction do you want to go?")
   print (" - up")
   print (" - down")
   print (" - north")
   print (" - east")
   print (" - south")
   print (" - west")
   directionInput = read ()
  end
  if directionInput == "up" or directionInput == "down" or directionInput == "north" or directionInput == "north" or directionInput == "east" or directionInput == "south" or directionInput == "west" then
   break
  else
   first = false
  end
end
end
function backToStart()
while true do
  term.clear()
  term.setCursorPos(1,1)
  if firstbackToStart == true then
   firstbackToStart = false
   print ("Would you like to move the quarry to original position after it's finished?")
   print (" - yes")
   print (" - no")
   backToStartInput = read ()
  else
   print ("Please type yes or no")
   print ("")
   print ("Would you like to move the quarry to original position after it's finished?")
   print (" - yes")
   print (" - no")
   backToStartInput = read ()
  end
  if backToStartInput == "yes" or backToStartInput == "no" then
   break
  else
   firstbackToStart = false
  end
end
end
function howMany()
while true do
  term.clear()
  term.setCursorPos(1,1)
  if notANumber == true then
   term.clear()
   term.setCursorPos(1,1)
   print ("'"..blocks.."' is not a number")
   print ("")
   print ("Direction: "..directionInput.."")
   print ("")
   print ("How many blocks?")
   blocks = read()
  else
   print ("Direction: "..directionInput.."")
   print ("")
   print ("How many blocks?")
   blocks = read()
  end
  blocks = tonumber(blocks)
  if blocks then
   notANumber = false
   break --break out of while true loop
  else notANumber = true
  end
end
end
function moveQuarry()
--blue wire is north, signal 501
--purple wire is east, signal 502
--orange wire is south, signal 503
--yellow wire is west, signal 504
--white wire is up, signal 505
--red wire is down, signal 506
--green wire is block breakers, signal 507
if directionInput == "up" then
  redstone.setBundledOutput("back",colors.white)
  sleep(2)
  redstone.setBundledOutput("back",0)
  sleep(5)
  redstone.setBundledOutput("back",colors.white)
  sleep(2)
  redstone.setBundledOutput("back",0)
  sleep(5)
elseif directionInput == "down" then
  redstone.setBundledOutput("back",colors.red)
  sleep(2)
  redstone.setBundledOutput("back",0)
  sleep(5)
  redstone.setBundledOutput("back",colors.red)
  sleep(2)
  redstone.setBundledOutput("back",0)
  sleep(5)
elseif directionInput == "north" then
  redstone.setBundledOutput("back",colors.blue)
  sleep(2)
  redstone.setBundledOutput("back",0)
  sleep(5)
  redstone.setBundledOutput("back",colors.blue)
  sleep(2)
  redstone.setBundledOutput("back",0)
  sleep(5)
elseif directionInput == "east" then
  redstone.setBundledOutput("back",colors.purple)
  sleep(2)
  redstone.setBundledOutput("back",0)
  sleep(5)
  redstone.setBundledOutput("back",colors.purple)
  sleep(2)
  redstone.setBundledOutput("back",0)
  sleep(5)
elseif directionInput == "south" then
  redstone.setBundledOutput("back",colors.orange)
  sleep(2)
  redstone.setBundledOutput("back",0)
  sleep(5)
  redstone.setBundledOutput("back",colors.orange)
  sleep(2)
  redstone.setBundledOutput("back",0)
  sleep(5)
elseif directionInput == "west" then
  redstone.setBundledOutput("back",colors.yellow)
  sleep(2)
  redstone.setBundledOutput("back",0)
  sleep(5)
  redstone.setBundledOutput("back",colors.yellow)
  sleep(2)
  redstone.setBundledOutput("back",0)
  sleep(5)
end
end
function moveQuarryBack()
--blue wire is north, signal 501
--purple wire is east, signal 502
--orange wire is south, signal 503
--yellow wire is west, signal 504
--white wire is up, signal 505
--red wire is down, signal 506
--green wire is block breakers, signal 507
if directionInput == "up" then
  directionInput = "down"
elseif directionInput == "down" then
  directionInput = "up"
elseif directionInput == "north" then
  directionInput = "south"
elseif directionInput == "east" then
  directionInput = "east"
elseif directionInput == "south" then
  directionInput = "north"
elseif directionInput == "west" then
  directionInput = "east"
end
end
function moveFirst()
while true do
  term.clear()
  term.setCursorPos(1,1)
  current = counter + 1
  if counter == blocks then
   if backToStartInput == "no" then
	print("Complete. Exiting now")
	sleep(1)
	counter = 0
	break
   else
	print("Complete. Now moving back")
	sleep(1)
	counter = 0
	break
   end
   break
  else
   print("Quarry currently in operation, please monitor it's movement and if it gets stuck then hold down 'Ctrl + T' while in this window")
   print("")
   print ("Direction: "..directionInput.."	Blocks: "..blocks.."")
   if backToStartInput == "yes" then
	print("Moving back to original position: Yes")
   end
   print("")
   print ("Quarry moved "..current.." blocks")
   redstone.setBundledOutput("back",colors.green)
   sleep(1)
   redstone.setBundledOutput("back",0)
   moveQuarry()
   counter = counter +1
  end
end
end
function moveSecond()
moveQuarryBack()
while true do
  term.clear()
  term.setCursorPos(1,1)
  current = counter + 1
  if counter == blocks then
   print("Complete. Exiting now")
   sleep(5)
   break
  else
   print("Quarry currently moving back to it's original position, please monitor it's movement and if it gets stuck then hold down 'Ctrl + T' while in this window")
   print("")
   print ("Direction: "..directionInput.."	Blocks: "..blocks.."")
   print ("Quarry moved "..current.." blocks")
   redstone.setBundledOutput("back",colors.green)
   sleep(1)
   redstone.setBundledOutput("back",0)
   moveQuarry()
   counter = counter +1
  end
end
end
whatDirection()
howMany()
backToStart()
moveFirst()
if backToStartInput == "yes" then
moveSecond()
end


http://pastebin.com/nTFx8ajQ

However its using rs.setBundledOutput and I did not know how to edit the program itself to send a msg to specified turtle to tell it to emit redstone puls. So I being the monkey rigger tried to have one computer run the quarry program and send the signals though MFR rednet cable to another computer that was open to wireless and had directions to send msg to right computer depending on color of bundled cable signal it received. The program I pieced together for this broadcaster:


rednet.open("right")
while true do
  if (colors.test (rs.getBundledInput("left"), colors.blue)) then
  rednet.send(4, "pulse")
  end
  if (colors.test (rs.getBundledInput("left"), colors.orange)) then
  rednet.send(5, "pulse")
  end
  if (colors.test (rs.getBundledInput("left"), colors.purple)) then
  rednet.send(6, "pulse")
  end
  if (colors.test (rs.getBundledInput("left"), colors.yellow)) then
  rednet.send(2, "pulse")
  end
end
http://pastebin.com/j7a54102
(however I'm having issues keeping this program looping indefinetely so it can always broadcast when it recieves a redstone pulse)

The turtles have this program running:

rednet.open("right")
while true do
  id,message = rednet.receive()
	if id == 3 then
	  if message == "pulse" then
		rs.setOutput("front", true)
		end
	  end
	end
http://pastebin.com/UDjmKyRZ
and
Using this for thier Startup program:

sleep(1)
shell.run("wireless")
http://pastebin.com/KJU5mBtV

I think the main problem right now is getting the broadcast computer to keep looping without getting the "Too long without yeilding"
Might be having an issue with the startup program on the frame computers.

Like I said before, I'm rather new to all this. Not sure what to make of it all but after messing around for some time I got a bit frustrated ;p
I saw another program on this forum that would save the blocks to move/direction in a file and would modify it every reboot. But I did not get that to work: Link Kept getting some 'nil' error.

Is this a viable way to do this? If so, how can I get this all to mesh together and work? Any input would be apprecieted. If you have a working program other than this I'd love to try to give it a go.

Thanks,
Arempi
awsmazinggenius #2
Posted 05 December 2013 - 08:56 PM
Hello,

You are missing and 'end' statement on your while statement. Also, indent with 2 or 4 spaces (tabs don't always like to play nicely), but please keep it consistent. Use sleep() calls every once in a while (and actually delay a second or two if possible - it reduces server load) to yield. os.pullEvent(), as well as other functions, yield your program as well. I'm not completely getting your issue, feel absolutely to post again with further details or to shoot me a PM. Have you tried a syntax checker? Some are actually half-decent at doing their job.
arempi #3
Posted 07 December 2013 - 06:05 AM
Thanks for the reply. Not sure which 'while' statement you were refering too. The program the turtles are using seems right, when I add another end it errors. I think I might just use a timer and go low tech.
awsmazinggenius #4
Posted 08 December 2013 - 12:59 PM
Sorry, that would be my mistake. I am referring to the turtle program (3rd example including spoiler), which is indented inaccurately so I thought an end statement was missing. (Sometimes the forum derps up your posts). "Too long without yielding" is when your program runs for 10 seconds without a yield (sleep(), os.pullEvent(), read()) and is CC's protection to make sure your computer - and the server - doesn't blow up.
Bomb Bloke #5
Posted 08 December 2013 - 04:16 PM
os.pullEvent() yields until certain events happen. Usually you'd store information about those events in some variables, but in this case, all you're interested in is that the event has happened (in this case, a change in the redstone input):

rednet.open("right")
while true do
  os.pullEvent("redstone")
  if (colors.test (rs.getBundledInput("left"), colors.blue)) then
  rednet.send(4, "pulse")
.
.
.

Or maybe add that line at the bottom of the loop, depending on whether you want it to hold off on the if/then checks until the first redstone state change.

If you want it to keep sending pulses "all the time", not just when the redstone input changes, then use sleep() (which also yields). It doesn't need to sleep for long - but since ComputerCraft can only execute one script at a time (per MineCraft server!), yielding is necessary so that other scripts get a chance to do their thing. Hence why it shuts them down if they don't do it.
Edited on 08 December 2013 - 03:21 PM
awsmazinggenius #6
Posted 08 December 2013 - 07:25 PM
For pulses "all the time", could one not simply use a redstone clock circut? It could be activated by a computer if necessary.