17 posts
Posted 30 October 2016 - 02:16 AM
Hey there, so I have a lil project going on and I have been searching all day to try to find an answer. I am stumped. Sooooo, here is what I am trying to do.
I have 1 turtle that is running a program named "Farmer" which goes around, plants tree's, and if a tree is grown…it will chop it down. As of right now, the turtle will display on its prompt what it is currently doing (IE: Planting, Chopping, Dumping, etc).
I also have another program named "Receiver" that will receive commands from my main computer, and from my main computer, I am able to tell the turtle what programs to run.
What I want to do is…..while any program is running on the turtle, and the turtle displays the information to it's own prompt….I want THAT information to be passed on to my main computer so I can display it on an external monitor.
MAIN COMPUTER PROGRAM TO SEND COMMANDS
rednet.open("left")
while true do
input = read()
rednet.send(<turtleID>, input)
if input == "exit" then
break
end
end
rednet.close("left")
MAIN TURTLE PROGRAM LISTENING FOR COMMANDS
rednet.open("right")
while true do
senderID, message, distance = rednet.receive()
if message = "exit" then
break
end
shell.run(message)
end
rednet.close("right")
17 posts
Posted 30 October 2016 - 03:42 PM
I like the
http://www.computerc...session-resume/ link…..I believe that is exactly what I am looking for (if I can get it to actually work).
I am running Tekkit Legends 1.7.10 with ComputerCraft 1.7
I am utilizing KaiKaKu's "Powerful Tree Farm" program to run my turtles. (
http://www.computercraft.info/forums2/index.php?/topic/22238-powerful-tree-farm/)
Again….I'm in need where I can sit in the control center and run 8 different turtles and see it's display on monitors in the control center.
To be honest….having a table or something would be awesome which shows each turtles current activity would be amazing. Something like….
Farmer 01 - ONLINE - CHOPPING… - 11:35am Farmer 02 - ONLINE - PLANTING SAPLING - 11:36am Farmer 03 - OFFLINE - CURRENTLY OFFLINE - 10:15amFarmer 04 - ONLINE - DUMPING HARVEST - 11:36amThat is ultimately what I'm going for….however, my lack of LUA and ComputerCraft skills are limiting me at the moment. I am learning quickly, but it can't be ALL work and no play….lol.
17 posts
Posted 30 October 2016 - 10:16 PM
Ok, I decided to ditch that and just go with what I know….
I have a turtle that plants a tree….waits…and chops the tree down, but before chopping the tree….I wanted it to search an 8x8 area for saplings before returning to the original starting point to chop.
It….KINDA….worked once, where he backed up, moved around the parameter, then stopped and just spun in circles. Now when I run the program, all it does is spin. Anything wrong with the coding??
CODE:Spoiler
rednet.open("right")
local function tr()
turtle.turnRight()
end
function fuelcheck()
local fuelLevel = turtle.getFuelLevel()
if fuelLevel < 20 then
turtle.select(1)
turtle.refuel(1)
rednet.broadcast("Refueled")
sleep(0.5)
end
end
function forwardNum(blocks)
for i= 1, blocks do
turtle.forward()
end
end
turtle.back()
turtle.back()
turtle.back()
turtle.turnLeft()
forwardNum(4)
tr()
forwardNum(8)
tr()
forwardNum(8)
tr()
forwardNum(8)
tr()
forwardNum(7)
tr()
forwardNum(7)
tr()
forwardNum(6)
tr()
forwardNum(6)
tr()
forwardNum(5)
tr()
forwardNum(5)
tr()
forwardNum(4)
tr()
forwardNum(4)
tr()
forwardNum(3)
tr()
forwardNum(3)
tr()
forwardNum(3)
tr()
forwardNum(2)
tr()
forwardNum(2)
tr()
function chopTree()
local success, data = turtle.inspect()
if data.name == "minecraft:log" then
rednet.broadcast("Tree Detected...")
sleep(1)
fuelcheck()
rednet.broadcast("Searching for Saplings")
turtle.dig()
turtle.forward()
while turtle.detectUp() == true do
rednet.broadcast("Chopping Tree")
turtle.digUp()
turtle.up()
end
while turtle.detectDown() == false do
turtle.down()
end
rednet.broadcast("Return to start")
turtle.back()
turtle.select(2)
turtle.place()
turtle.select(15)
turtle.place()
turtle.place()
turtle.place()
turtle.place()
turtle.select(3)
rednet.broadcast("Delivering Logs")
turtle.dropDown()
turtle.select(1)
rednet.broadcast("Waiting")
end
end
local chopping = true
while chopping do
chopTree()
turtle.suck()
turtle.suckUp()
end
Edited on 30 October 2016 - 11:45 PM
7083 posts
Location
Tasmania (AU)
Posted 31 October 2016 - 12:50 AM
Odds are your turtle is out of fuel. Consider calling your fuel checking function earlier and / or more often; eg from your forwardNum() function.
A
table such as this:
local moves = {4, 8, 8, 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 3, 2, 2}
… could reduce this lot:
Spoiler
forwardNum(4)
tr()
forwardNum(8)
tr()
forwardNum(8)
tr()
forwardNum(8)
tr()
forwardNum(7)
tr()
forwardNum(7)
tr()
forwardNum(6)
tr()
forwardNum(6)
tr()
forwardNum(5)
tr()
forwardNum(5)
tr()
forwardNum(4)
tr()
forwardNum(4)
tr()
forwardNum(3)
tr()
forwardNum(3)
tr()
forwardNum(3)
tr()
forwardNum(2)
tr()
forwardNum(2)
tr()
… down to this:
for i = 1, #moves do
forwardNum(moves[i])
tr()
end
17 posts
Posted 31 October 2016 - 04:53 PM
That was such an extremely useful piece of code, Bomb Bloke….Thank you very much. Now I'm curious, with LUA…am I able to just have all functions in an organized section of the script, then just call upon an order of functions? IE:
EXAMPLE
function fuelcheck()
local fuelLevel = turtle.getFuelLevel()
if fuelLevel < 20 then
turtle.select(1)
turtle.refuel(1)
rednet.broadcast("Refueled")
sleep(0.5)
end
end
function chopTree()
local success, data = turtle.inspect()
if data.name == "minecraft:log" then
rednet.broadcast("Tree Detected…")
sleep(1)
fuelcheck()
rednet.broadcast("Searching for Saplings")
turtle.dig()
turtle.forward()
while turtle.detectUp() == true do
rednet.broadcast("Chopping Tree")
turtle.digUp()
turtle.up()
end
while turtle.detectDown() == false do
returnToStart()
end
end
function returnToStart()
rednet.broadcast("Return to start")
turtle.back()
turtle.select(2)
turtle.place()
turtle.select(15)
turtle.place()
turtle.place()
turtle.place()
turtle.place()
turtle.select(3)
rednet.broadcast("Delivering Logs")
turtle.dropDown()
turtle.select(1)
rednet.broadcast("Waiting")
end
fuelcheck() — order of operation —
chopTree()
returnToStart() — order of operation end —
Does LUA follow each function in order of function script or am I able to tell it what functions do, then call upon the order of functions as in Order of Operation?
2427 posts
Location
UK
Posted 31 October 2016 - 06:40 PM
you can call as many functions in whatever order you want, definition order has nothing to do with how they run
17 posts
Posted 31 October 2016 - 07:14 PM
…… definition order has nothing to do with how they run
That is what I'm asking. If every time I want a function to run, do I have to use the while snippet of code, or can I just define the function…then just call upon that function in whatever order I want and just make it run one sequence over and over, referring to the functions listed above. IE:
EXAMPLE:Spoiler
function name01()
what_function_does
end
function name02()
what_function_does
end
function name03()
what_function_does
end
---- sequence ----
local farming = true
while farming do
name01()
name02()
name01()
name02()
name03()
name01()
end
If you feel that you already answered this for me, I am sorry…but I don't understand your reply…could you reword?
Edited on 31 October 2016 - 06:19 PM
17 posts
Posted 31 October 2016 - 07:27 PM
Also….I am still getting errors where as my turtle has a starting position, runs its path, and returns to starting position. However, after a period of time…it will randomly stop in different areas and think that IS it's starting position. I'm confused on why these random occurrences happen. It will start with plenty of fuel (64 coal) and when it stops, it still has 45 or so coal left.
212 posts
Location
Somewhere in this dimension... I think.
Posted 31 October 2016 - 07:31 PM
::start::
someFunction()
goto start
Which is basically
while true do
someFunction()
end
Not entirely sure what is asked.
Edited on 31 October 2016 - 06:31 PM
17 posts
Posted 31 October 2016 - 10:05 PM
Ok, so I got it working pretty good so far, however….the random stopping is still happening. It'll run GREAT for about a half hour or so, but when I go back to check later……it seems to have found itself a new starting / stopping position.
I'm curious if this section is making it start and stop in different positions.
Spoiler
function forwardNum(blocks)
for i = 1, blocks do
tf()
turtle.suck()
end
end
local moves = {4, 8, 8, 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 3, 2, 2}
local function sapsearch()
fuelcheck()
rednet.broadcast("Searching for Saplings")
turtle.select(2)
tb()
tb()
tb()
tl()
for i = 1, #moves do
forwardNum(moves[i])
tr()
end
rednet.broadcast("Waiting")
end
THIS IS THE ENTIRE PROGRAMSpoiler
-- OPEN WIFI --
rednet.close("right")
rednet.open("right")
-- MOVE SHORTCUTS --
local function tr()
turtle.turnRight()
end
local function tl()
turtle.turnLeft()
end
local function tb()
turtle.back()
end
local function tf()
turtle.forward()
end
local function tu()
turtle.up()
end
local function td()
turtle.down()
end
-- GET MORE FUEL --
function fuelsourcecheck()
local fuelcount = turtle.getItemCount(1)
if fuelcount &lt; 9 then
rednet.broadcast("Getting Fuel")
tb()
tb()
tb()
tb()
tb()
tr()
tf()
tf()
turtle.select(1)
turtle.suckDown(56)
tb()
tb()
tl()
tf()
tf()
tf()
tf()
tf()
rednet.broadcast("Waiting")
end
end
-- CHECK FUEL LEVEL --
function fuelcheck()
local fuelLevel = turtle.getFuelLevel()
if fuelLevel &lt; 200 then
fuelsourcecheck()
turtle.select(1)
turtle.refuel(8)
rednet.broadcast("Refueled")
sleep(1)
rednet.broadcast("Waiting")
end
end
-- TREE DETECTION AND HARVEST --
function choptree()
local success, data = turtle.inspect()
if data.name == "minecraft:log" then
rednet.broadcast("Tree Detected...")
sleep(2)
fuelcheck()
rednet.broadcast("Chopping Tree...")
turtle.dig()
tf()
while turtle.detectUp() == true do
turtle.digUp()
tu()
turtle.suck()
turtle.suckUp()
end
while turtle.detectDown() == false do
rednet.broadcast("Return to Start")
td()
end
tb()
turtle.select(2)
turtle.place()
turtle.select(1)
rednet.broadcast("Waiting")
end
end
-- SAPLING SEARCH PATH --
function forwardNum(blocks)
for i = 1, blocks do
tf()
turtle.suck()
end
end
local moves = {4, 8, 8, 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 3, 2, 2}
local function sapsearch()
fuelcheck()
rednet.broadcast("Searching for Saplings")
turtle.select(2)
tb()
tb()
tb()
tl()
for i = 1, #moves do
forwardNum(moves[i])
tr()
end
rednet.broadcast("Waiting")
end
-- DELIVER LOGS --
function logUnload()
fuelcheck()
turtle.select(3)
local countlog = turtle.getItemCount(3)
if countlog &gt; 1 then
rednet.broadcast("Delivering Logs")
tb()
tb()
tb()
tb()
tb()
turtle.select(3)
turtle.dropDown()
turtle.select(1)
tf()
tf()
tf()
tf()
tf()
rednet.broadcast("Waiting")
end
end
-- SEQUENCE --
local Farming = true
while Farming do
choptree()
sleep(5)
logUnload()
sleep(5)
sapsearch()
sleep(5)
end
Edited on 31 October 2016 - 09:06 PM
2427 posts
Location
UK
Posted 31 October 2016 - 10:51 PM
Regarding the stopping and starting, was the turtle in an unloaded chunk at any point? did you walk (a fair distance) away from the turtle? you may want to place a chunk loader near the tree
17 posts
Posted 31 October 2016 - 10:53 PM
That is a negative, Lupus….I haven't left the area. and according to F9 to show chunks, it is all in the same chunk….however when the turtle wonders off…it may cross chunk thresholds.
7083 posts
Location
Tasmania (AU)
Posted 01 November 2016 - 09:59 AM
When a turtle movement command is executed, it doesn't return when the move completes - it'll also return if it decides the move is impossible. You get true / false back depending on the result. Since your script doesn't account for this, a mob wandering in your turtle's path could easily throw off its positioning.
You might like to re-write your movement functions along these lines:
local function tf()
while not turtle.forward() do -- Until an attempt to move forward succeeds...
turtle.dig()
turtle.attack()
end
end
In cases where you truly want one function to point directly to another, you can actually just specify that instead of creating a new wrapper function:
local tl = turtle.turnLeft