Posted 19 March 2013 - 04:53 PM
I have written a program for a turtle to harvest village wheat farms. I need to add some logic based on where the turtle is placed, but placing it in the correct spot and running it works - but adding a sleep() command and looping it introduces problems. Upon a second run it might end it's run in an odd part of the farm area, or it could go off and wander.
At first I thought it was a GPS host issue where I was triangulating the wrong coordinates, and I was right. The gps host computers I set up had a couple of incorrect hard-coded X coordinates. It seems that's not the only issue though, and I'm stumped. They still seem to go off sometimes.
Another thing to mention - I was reading up on wheat growth in vanilla Minecraft and read that rows grow faster when there is not wheat next to it, so I took those village farms and only harvest 1 of the two columns, leaving the other tilled and not used.
At first I thought it was a GPS host issue where I was triangulating the wrong coordinates, and I was right. The gps host computers I set up had a couple of incorrect hard-coded X coordinates. It seems that's not the only issue though, and I'm stumped. They still seem to go off sometimes.
Another thing to mention - I was reading up on wheat growth in vanilla Minecraft and read that rows grow faster when there is not wheat next to it, so I took those village farms and only harvest 1 of the two columns, leaving the other tilled and not used.
-- current location and known gps points
local home = vector.new(gps.locate())
local refill = vector.new(0, 0, 0) -- replace with gps of inventory to get stock
local refuel = vector.new(0, 0, 0) -- replace with gps of inventory to get fuel
local dump = vector.new(0, 0, 0) -- replace with gps of inventory to dump to
-- item slots used to compare against inventory and produce
local fuel = 1
local seedCompare = 2
local wheatCompare = 3
local dirtCompare = 4
-- configuration
local fuelIncrement = 2 -- the amount of item to use to refuel
local rowSize = 7 -- the distance to plant and sow, this is size of village row
local rows = 4 -- the number of rows to plant
local skip = 2 -- the number of rows to skip, water, empty field, etc
-- FUNCTIONS
-- check fuel level
local function fuelCheck()
print("checking fuel levels...")
local fuelLevel = turtle.getFuelLevel()
-- refuel if necessary
if fuelLevel > 10 then
print("fuel level: "..fuelLevel)
print("no fuel necessary at this time")
else
turtle.select(fuel)
print("...refueling")
turtle.refuel(fuelIncrement)
print("fuel level: "..fuelLevel)
end
end
local function selectSeed()
turtle.select(seedCompare)
print("compare to slot: "..seedCompare)
local seeds = seedCompare + 1
repeat
turtle.compareTo(seeds)
print("comparing to slot: "..seeds)
seeds = seeds + 1
until turtle.compareTo(seeds) == true
if turtle.compareTo(seeds) == true then
print("seeds found in slot: ".. seeds)
return turtle.select(seeds)
else
print("no seeds in inventory")
end
end
local function harvest()
turtle.digDown()
end
local function plant()
turtle.placeDown()
end
function harvestRow()
-- combines harvesting, planting, and movement for a whole row
selectSeed()
print("selected inventory")
for start = 1, rowSize do
turtle.forward() -- move to first block
print("moved forward")
harvest() -- harvest the block
print("harvested wheat")
plant()
print("planted seed")
end
turtle.forward() -- otherwise standing over last seed placed
end
function newRowEven()
-- move on to the next row
print("starting a new row")
checkOrientation()
turtle.forward() -- variable "skip" implies it is one row further away
for start=1, skip do
turtle.forward()
end
-- turtle.turnLeft() -- now facing new row
checkOrientation()
end
function newRowOdd()
-- move on to the next row
print("starting a new row")
checkOrientation()
turtle.forward() -- variable "skip" implies it is one row further away
for start=1, skip do
turtle.forward()
end
checkOrientation()
end
-- function checkDistance()
-- local current = vector.new(gps.locate())
-- local travel = home - current
-- print("Traveled distance from origin: ")
-- print(travel)
-- return travel
-- end
function checkOrientation()
-- this should run the xyz comparison and determine the direction. If distance is over zero turn this way, if under 0 turn that way
local current = vector.new(gps.locate())
local travel = current - home
print("Traveled distance from origin: ")
print(travel)
-- compares coordinates and sets turtle to turn away from origin while harvesting. need logic for when turtle is facing other directions, eg where x < 0
if travel.x == 0 then
if travel.z > 0 then
turtle.turnLeft()
elseif travel.z == 0 then
-- do nothing, went nowhere
end
elseif travel.x > 0 then
if travel.z > 0 then
turtle.turnLeft()
elseif travel.z == 0 then
turtle.turnRight()
end
end
end
local function returnHome ()
-- get final gps location and compare to origin
local current = vector.new(gps.locate())
local travel = home - current -- haven't considered why I reversed this for the orientation logic, but I did and it works. This was original formula.
print("Traveled distance from origin: ")
print(travel)
-- similar to the check orientation above but built to turn turtle facing the right way to go to origin point
if travel.x > 0 then
turtle.turnRight()
elseif travel.x < 0 then
turtle.turnLeft()
elseif travel.x == 0 then
turtle.turnLeft()
end
-- a set of loop variables to travel the difference
local xt, yt, zt = 0, 0, 0
-- travel logic
if travel.x > 0 then -- went east
repeat
turtle.forward()
xt = xt + 1
until xt == travel.x
elseif travel.x < 0 then -- went west
repeat
turtle.forward()
xt = xt - 1
until xt == travel.x
elseif travel.x == 0 then -- no change
-- do nothing
else
-- do nothing
end
if travel.y > 0 then -- went up
repeat
turtle.up()
yt = yt + 1
until yt == travel.y
elseif travel.y < 0 then -- went down
repeat
turtle.down()
yt = yt - 1
until yt == travel.y
elseif travel.y == 0 then -- no change
-- do nothing
else
-- do nothing
end
if travel.z > 0 then -- went south
repeat
turtle.forward()
zt = zt + 1
until zt == travel.z
elseif travel.z < 0 then -- went north
repeat
turtle.back()
zt = zt - 1
until zt == travel.z
elseif travel.z == 0 then -- no change
-- do nothing
else
-- do nothing
end
-- same idea as above, but making orientation correct for next run
if travel.x > 0 then
turtle.turnRight()
elseif travel.x < 0 then
turtle.turnLeft()
elseif travel.x == 0 then
-- nothing
end
end -- end returnHome
-- PROGRAM
while true do
fuelCheck() -- check fuel level
for r = 1, rows do
harvestRow()
r = r + 1
if r == 2 then -- would like a math check to see if even or odd, to handle better
newRowEven()
elseif r == 3 then
newRowOdd()
elseif r == 4 then
newRowEven()
end
end
returnHome() -- go to origin
print("waiting for wheat to grow")
sleep(2400)
end