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

Help with advanced system coding appreciated

Started by f1amekeeper, 11 November 2015 - 02:50 PM
f1amekeeper #1
Posted 11 November 2015 - 03:50 PM
I am requesting help with creating a program. What my plan is is to have a central computer with a monitor display above it displaying the status of multiple turtles as in if they are stuck out of fuel or all good. I plan on having turtles act as relays to extend the wireless range. I would also like help coding a turtle lumberjack. planing on having it use a compare to check if the birch tree has regrown if it has refuel once dig forward dig up go up and detect if it still has birch logs above it then have it go back then down as many times as it went up to return to staring location turn around place excess logs in a chest then turnaround and wait for tree to regrow.
Creator #2
Posted 11 November 2015 - 06:00 PM
Have you started the code? If yes post it, if not try to do something on your own, because you have to understand this: no one will write a program for you, BUT this community is willing to help you free code of bugs.
f1amekeeper #3
Posted 11 November 2015 - 06:06 PM
i did try but it wound up being some of the messiest code I have written in a long time and scrapped it. I've gotten a few friends who are willing to help me code the monitor(monitors aren't really my forteigh mostly work with turtles) I have been pretty busy and haven't had much time to sit down and recode the program much but once I have a more complete and stable code I will probably post it for help debugging. Still if anyone would like to help me with the coding it would be greatly appreciated even if it is just suggestions of events peripherals or whatever you believe would be useful in writing the code I would be greatful.
KingofGamesYami #4
Posted 11 November 2015 - 11:39 PM
For a tree farm, I usually do something like this:


turtle.select( 1 )
function tree()
  turtle.dig()
  turtle.forward()
  while turtle.digUp() do
    turtle.up()
    for i = 1, 4 do
      turtle.dig()
      turtle.turnRight()
    end
  end
  repeat until not turtle.down()
  turtle.digDown()
  turtle.placeDown()
  turtle.back()
end

while true do
  if turtle.detect() then
    tree()
  else
    sleep( 10 )
  end
end

The turtle starts above the ground, next to the tree.

T
T
TX
T
GGGGGG

The X is where the turtle is, the T is where the trunk of the tree would be. G shows where the ground is.

Logic behind it: If a tree is detected, I mine the block in front of it. Then, I start digging up. When turtle.digUp digs a block, I move up one block and mine around the turtle. When the turtle reaches the top of the tree, turtle.digUp will find an air block and return false. The next step is to get back down, so I move down until turtle.down returns false. Once it has, I dig the stump the turtle had previously left, and place a new sapling. Then I move back into position and resume waiting for a tree to be detected.
f1amekeeper #5
Posted 12 November 2015 - 07:25 PM
that's really good turtle tree farm code, but what if you had the turtle start at the base of the tree and at the beginning of function tree() you did

local itemCount = turtle.getItemCount()
itemCount = itemCount - 3
turtle.refuel(1)
turtle.turnLeft(2) -- make it face a chest or other storage
turtle.drop(itemCount)
turtle.turnLeft(2) -- make the turtle face the tree again
-- then resume your code from turtle.dig() as well as changing the turtle.detect() to turtle.compare()
then have a vaccuum hopper going into a chest to the left of the turtles starting point and have the turtle use elseif not turtle.detect() then turn left suck then select 2nd slot turn right place(should place a sapling) turn left drop turn right then the else sleep(10) already present in the code as this would fully automate the turtle.

almost have the programing or the monitor done but need the parallel command can anyone tell me how to use it?
KingofGamesYami #6
Posted 12 November 2015 - 11:46 PM
Well, the reason I don't start him at ground level is because I'd rather use turtle.detect than turtle.compare. Honestly, if you wanted to have a chest, just place it below the turtle - then use turtle.dropDown / turtle.suckDown to access it.

As for parallel, presumably you would want parallel.waitForAny. Simply pass it a couple functions and it'll run them in parallel.


local function main()
 --#do things here
end

local function notmain()
 --#do more things here
end

parallel.waitForAny( main, notmain )

A common mistake is to include the () when passing the functions to parallel.waitForAny - you should not. The parallel API does all the calling internally (well, not really, but that's complicated).

It would be good to note that if either of the functions completes (returns a value, finishes, etc.) the other function will end at the same time. If you don't want this to happen, use parallel.waitForAll. In addition, the functions have to yield (eg pull an event). Most turtle functions, sleep, etc. will all yield. If your code would normally error 'too long without yielding', it won't work any better in parallel.
Edited on 12 November 2015 - 10:51 PM