Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Farmimg
- --Dustin Luhmann
- --Version 0.1.1
- --3/5/2015
- --http://pastebin.com/NqH36dT5
- --[[TO RUN: pastebin get NqH36dT5 <filename>; <filename>]]
- --Example > pastebin get NqH36dT5 test
- -- > test
- -- USER NOTES (**IMPORTANT**)
- -- 1) Must be placed facing north
- -- 2) Seeds should be placed in slots 1 - 7
- -- 3) Bonemeal should be placed in slot 8
- -- 4) Nothing should be in slots 9 - 15
- -- 5) A crop sensor card should be placed in slot 16
- os.loadAPI("ocs/apis/sensor")
- mySensor = sensor.wrap("left")
- HEADINGS = {"north","east","south","west"}
- WHEAT_IDX = 1
- CROP_TWO_IDX = 2
- CROP_THREE_IDX = 3
- BONE_MEAL_IDX = 8
- SENSOR_CARD_SLOT = 16
- isUp = false
- heading = 1
- currentPosition = vector.new(0,0,0)
- function tablelength(T)
- local count = 0
- for _ in pairs(T) do count = count + 1 end
- return count
- end
- function forward()
- if HEADINGS[heading] == "north" then
- currentPosition["z"] = currentPosition["z"] - 1
- elseif HEADINGS[heading] == "south" then
- currentPosition["z"] = currentPosition["z"] + 1
- elseif HEADINGS[heading] == "east" then
- currentPosition["x"] = currentPosition["x"] + 1
- elseif HEADINGS[heading] == "west" then
- currentPosition["x"] = currentPosition["x"] - 1
- end
- turtle.forward()
- end
- function turnLeft()
- if HEADINGS[heading] == "north" then
- heading = 4
- else
- heading = heading - 1
- end
- turtle.turnLeft()
- end
- function turnRight()
- if HEADINGS[heading] == "west" then
- heading = 1
- else
- heading = heading + 1
- end
- turtle.turnRight()
- end
- function pickUpNormalCrop()
- turtle.digDown()
- end
- function placeSeed(idx)
- turtle.select(idx)
- turtle.placeDown()
- if turtle.getItemCount(idx) == 0 then
- end
- end
- function tillLand()
- turtle.digDown()
- end
- function plantCrop(idx)
- tillLand()
- placeSeed(idx)
- useBoneMeal()
- end
- function useBoneMeal()
- turtle.select(BONE_MEAL_IDX)
- count = turtle.getItemCount(BONE_MEAL_IDX)
- turtle.placeDown()
- while turtle.getItemCount(BONE_MEAL_IDX) ~= count do
- count = turtle.getItemCount(BONE_MEAL_IDX)
- turtle.placeDown()
- end
- end
- function scan()
- targets = mySensor.getTargets()
- numberOfPlantsToCollect = 0
- plantLocations = {}
- for target, key in pairs(targets) do
- for attribute, attributeKey in pairs(mySensor.getTargetDetails(target)) do
- if attribute == "Position" then
- for direction, value in pairs(attributeKey) do
- if direction == "Y" then
- y = value
- end
- if direction == "X" then
- x = value
- end
- if direction == "Z" then
- z = value
- end
- end
- location = vector.new(x,y,z)
- end
- if attribute == "Status" then
- if attributeKey == "Grown" then
- numberOfPlantsToCollect = numberOfPlantsToCollect + 1
- plantLocations[numberOfPlantsToCollect] = location
- end
- end
- end
- end
- return plantLocations
- end
- function move(location)
- if not isUp then
- moveInZ()
- end
- if location["x"] > 0 then
- while HEADINGS[heading] ~= "east" do
- turnLeft()
- end
- end
- if location["x"] < 0 then
- while HEADINGS[heading] ~= "west" do
- turnLeft()
- end
- location["x"] = -1 * location["x"]
- end
- for i = 1, location["x"] do
- forward()
- end
- if location["z"] > 0 then
- while HEADINGS[heading] ~= "south" do
- turnLeft()
- end
- end
- if location["z"] < 0 then
- while HEADINGS[heading] ~= "north" do
- turnLeft()
- end
- location["z"] = -1 * location["z"]
- end
- for i = 1, location["z"] do
- forward()
- end
- end
- function calculateHome()
- return home - currentPosition
- end
- function goHome()
- if currentPosition["x"] ~= home["x"] or currentPosition["y"] ~= home["y"] or currentPosition["z"] ~= home["z"] then
- move(calculateHome())
- while HEADINGS[heading] ~= "north" do
- turnLeft()
- end
- if isUp then
- moveInZ()
- end
- end
- end
- function moveInZ()
- if isUp then
- isUp = false
- turtle.down()
- else
- isUp = true
- turtle.up()
- end
- end
- function goToWork()
- while true do
- plantLocations = scan()
- if (tablelength(plantLocations) == 0) then
- goHome()
- else
- move(plantLocations[1])
- pickUpNormalCrop()
- plantCrop(WHEAT_IDX)
- end
- end
- return
- end
- function exitListen()
- while true do
- print( "Press Space exit program." )
- print("Farming...")
- local event, key = os.pullEvent( "key" ) -- limit os.pullEvent to the 'key' event
- if key == keys.space then -- if the key pressed was 'e'
- print( "You pressed [Space]. Exiting program..." )
- break
- end
- end
- return
- end
- function run()
- home = vector.new(0,0,0)
- parallel.waitForAny(goToWork, exitListen)
- goHome()
- end
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement