I use this mod to replace the CC variety because CC doesn't work well with my mod selections.OC is far better anyway.
So I set out to learn LUA programming yesterday, and read lots of API's and looked at lots of code.
I discovered that direct interaction with the robot means I need to be in the LUA prompt and to run the LUA scripts I make, I have to be in the shell.. this wasn't immediatly obvious and it took me a bot to figure that out.
Once I got the jest of how a LUA program for OC is put together, i started my first program last night and I got something that works but I have a bug that I can't track down, so I assume it's because I'm not allowed to have beginners luck.
The program is a simple tree felling program. the steps are simple:
Plant the saplings
move 1 space up
wait for the tree to be detected
drop 1 block down
chop the tree from bottom to top and back down.
rinse an repeat.
However, after the robot plants the saplings, it is just 1 block away like this: Rss
ss
First issue:
The saplings never grew. wide open sky, nothing above.
I moved the robot back from the saplings so there is a space between the robot and the first sapling, but this means the robot cannot detect the grown sapling from 2 blocks away.
I thought i'd add a timer to move the Robot forward 1 space to detect every 15 seconds and if it detected the tree, it would drop down 1 space, move forward one space and fell the tree as usual.
Second issue:
The second problem I had was after several trees were felled, the robot would lose track of the height variable so, while the robot was felling the second half of the tree, it would continue to dig into the ground as much as 5 blocks and muddle in that hole.
This bug seems random. and my thought is that the timer I added but be interfering with the height count?.. but being new to LUA and OpenComputers mod and programming in general.
Would anyone mind giving a guy a hand to *fix this second issue?
I will include my program code here:
[Edit] just realized how shabby the code looks at the end where I was working last. apologies.
local robot = require("robot")
local sides = require("sides")
local event = require("event")
local height = 0
function Harvest()
robot.swing()
robot.forward()
robot.swing()
while robot.detectUp() do
robot.swingUp()
robot.up()
robot.swing()
height = height + 1
end
for i = 1,height do
robot.swing()
robot.swingDown()
robot.down()
end
robot.swing()
robot.turnAround()
robot.forward()
robot.turnRight()
robot.forward()
robot.turnRight()
Saplings()
end
function Saplings()
if not robot.detect() then do
robot.select(12) -- Saplings here
robot.forward()
robot.place()
robot.turnRight()
robot.forward()
robot.turnLeft()
robot.place()
robot.turnAround()
robot.forward()
robot.turnAround()
robot.place()
robot.turnLeft()
robot.forward()
robot.turnRight()
robot.place()
robot.up()
robot.back()
event.timer(15,onTimer,20)
end
end
end
function onTimer()
robot.forward()
if robot.detect() then
Harvest()
else
robot.back()
end
You can post now and register later.
If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.
First, I love the Mod.
I use this mod to replace the CC variety because CC doesn't work well with my mod selections.OC is far better anyway.
So I set out to learn LUA programming yesterday, and read lots of API's and looked at lots of code.
I discovered that direct interaction with the robot means I need to be in the LUA prompt and to run the LUA scripts I make, I have to be in the shell.. this wasn't immediatly obvious and it took me a bot to figure that out.
Once I got the jest of how a LUA program for OC is put together, i started my first program last night and I got something that works but I have a bug that I can't track down, so I assume it's because I'm not allowed to have beginners luck.
The program is a simple tree felling program. the steps are simple:
Plant the saplings
move 1 space up
wait for the tree to be detected
drop 1 block down
chop the tree from bottom to top and back down.
rinse an repeat.
However, after the robot plants the saplings, it is just 1 block away like this: Rss
ss
First issue:
The saplings never grew. wide open sky, nothing above.
I moved the robot back from the saplings so there is a space between the robot and the first sapling, but this means the robot cannot detect the grown sapling from 2 blocks away.
I thought i'd add a timer to move the Robot forward 1 space to detect every 15 seconds and if it detected the tree, it would drop down 1 space, move forward one space and fell the tree as usual.
Second issue:
The second problem I had was after several trees were felled, the robot would lose track of the height variable so, while the robot was felling the second half of the tree, it would continue to dig into the ground as much as 5 blocks and muddle in that hole.
This bug seems random. and my thought is that the timer I added but be interfering with the height count?.. but being new to LUA and OpenComputers mod and programming in general.
Would anyone mind giving a guy a hand to *fix this second issue?
I will include my program code here:
[Edit] just realized how shabby the code looks at the end where I was working last. apologies.
Link to post
Share on other sites