Advertisement
Guest User

1

a guest
Aug 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.13 KB | None | 0 0
  1. -- Pam's greenhouse farming turtle by _TheSettler_
  2.  
  3. -- User inputs      
  4. local isActive      = "true"
  5. local fieldML       = 5
  6. local fuelSlot      = 16
  7. xmax = 10
  8. ymax = 30
  9. local mitPrintTime  = 3
  10.  
  11.  
  12. local xLine         = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  13. local status        = "Booting"
  14. local activeM       = " Active = "
  15. local statusM       = " Status = "
  16. local minFuel       = 1500
  17. local triedRefuel   = false
  18. local x, y, z, f
  19. local n = 0
  20.  
  21. local function syncFile(file, cord)
  22.   posOut = io.open(file, "w")
  23.   posOut:write(cord)
  24.   posOut:close()
  25. end
  26.  
  27. local function abort(msg)
  28.   syncFile()
  29.   status("ERROR: "..msg)
  30.   mitPrint(4, statusM, status)
  31.   do return end
  32. end
  33.  
  34. local function up(n)
  35.   for i = 1, n do
  36.         while not turtle.up() do
  37.           turtle.digUp()
  38.         end
  39.         y = y + 1
  40.         syncFile("data/y.txt", y)
  41.   end
  42. end
  43.  
  44. local function down(n)
  45.   for i = 1, n do
  46.         while not turtle.down() do
  47.           turtle.digDown()
  48.         end
  49.         y = y - 1
  50.         syncFile("data/y.txt", y)
  51.   end
  52. end
  53.  
  54. local function moveF(dir, n)
  55.   if dir == 0 or 1 or 2 or 3 then
  56.     print("dir was not 0-3 ", dir)
  57.     while f ~= dir do
  58.     turtle.turnRight()
  59.     f = f + 1
  60.       if f >= 4 then
  61.       f = 0
  62.       end
  63.     end
  64.   else
  65.   abort("invalid dir")
  66.   end
  67.   f = dir
  68.     if n ~= 0 then
  69.       for i = 1, n do
  70.          turtle.forward()
  71.         if f == 0 then
  72.         x = tonumber(f) + 1
  73.         elseif f == 1 then
  74.               y = tonumber(y) + 1
  75.               elseif f == 2 then
  76.               x = tonumber(x) - 1
  77.               elseif f == 3 then
  78.               y = tonumber(y) - 1
  79.               end
  80.       end
  81.      else
  82.       abort("invalid state for F")
  83.           end
  84.     syncFile("data/x.txt", x)
  85.     syncFile("data/y.txt", y)
  86.     syncFile("data/f.txt", f)
  87.   end
  88.  
  89.  
  90. local function getPos(file)
  91. posIn = io.open(file, "r")
  92. pos = posIn:read()
  93. posIn:close()
  94. return pos
  95. end
  96.  
  97. local function background()
  98. term.clear()
  99. term.setCursorPos(2,1)
  100. print(xLine)
  101. term.setCursorPos(2,3)
  102. print(xLine)
  103.  term.setCursorPos(2,5)
  104. print(xLine)
  105. mitPrint(3, activeM, isActive, false)
  106. mitPrint(5, statusM, status, false)
  107. mitPrint(13,"Writen by _TheSettler_", " ", false)
  108. end
  109.  
  110. function mitPrint(h, m1, m2, s)
  111. m1 = tostring(m1)
  112. m2 = tostring(m2)
  113. m1l = string.len(m1)
  114. m2l = string.len(m2)
  115. len = 39 - m1l - m2l
  116. len = len / 2
  117. term.setCursorPos(len, h)
  118. term.clearLine()
  119. print(m1, m2)
  120.   if s == false then
  121.   else
  122.   os.sleep(mitPrintTime)
  123.   end
  124. end
  125.  
  126. local function fuelCheck()
  127. status = "Checking fuelLevel"
  128. mitPrint(4, statusM, status)
  129. fuel = turtle.getFuelLevel()
  130.   if fuel < minFuel then
  131.   status = "Fuel is to low"
  132.   mitPrint(4, statusM, status)
  133.   mitPrint(2, activeM, isActive)
  134.    if triedRefuel == false then
  135.     refuel()
  136.    else
  137.     isActive = "false"
  138.     status = "Unable to refuel"
  139.     mitPrint(4, statusM, status)
  140.     mitPrint(2, activeM, isActive)
  141.    end
  142.   else
  143.   status = "Confirmed fuel level"
  144.   mitPrint(4, statusM, status)
  145.   end
  146. end
  147.  
  148. local function refuel()
  149. status = "Refueling"
  150. mitPrint(4, statusM, status)
  151. turtle.select(fuelSlot)
  152. space = turtle.getItemSpace()
  153. turtle.suckDown(space)
  154. count = turtle.getItemCount()
  155. turtle.refuel(count)
  156. status = "refuled"
  157. mitPrint(4, statusM, status)
  158. triedRefuel = true
  159. fuelCheck()
  160. end
  161.  
  162. local function setPos(nx, ny, nz, nf)
  163. x = nx
  164. y = ny
  165. z = nz
  166. f = nf
  167.     syncFile("data/x.txt", x)
  168.     syncFile("data/y.txt", y)
  169.     syncFile("data/z.txt", z)
  170.     syncFile("data/f.txt", f)
  171. end
  172.  
  173. local function checkPos()
  174. x = getPos("data/x.txt")
  175. y = getPos("data/y.txt")
  176. z = getPos("data/z.txt")
  177. f = getPos("data/f.txt")
  178. success, inspectData = turtle.inspectDown()    
  179.   if inspectData.name == "minecraft:chest" then
  180.   setPos(0, 0, 0 ,0)
  181.   status = "Parked on chest, set 0,0 "
  182.   mitPrint(4, statusM, status)
  183.   else
  184.   status = "Not parked on chest"
  185.   --navigateFromBoot()
  186.   mitPrint(4, statusM, status)
  187.   isActive = "false"
  188.   end
  189. end
  190.  
  191. local function navigateFromBoot()
  192.     if x ~= 0 and y ~= 0 and f ~= 0 then
  193.     moveF(2,x)
  194.     moveF(3,y)
  195.     end
  196. end
  197.  
  198. local function checkActive()
  199.     if isActive == "false" then
  200.     status = "Turtle is not active"
  201.     mitPrint(4, statusM, status)
  202.     sleep(10000000)
  203.     os.shutdown()
  204.     end
  205. end
  206.  
  207. local function go()
  208.   while y ~= ymax do
  209.     if turtle.detectUp() == true then
  210.     status = "Field found"
  211.     mitPrint(4, statusM, status)
  212.     harvestLine()
  213.     print("done harvest")
  214.     moveF(0, 1)
  215.     else
  216.     x = getPos("data/x.txt")
  217.     y = getPos("data/y.txt")
  218.       if x == xmax then
  219.       moveF(1,ymax)
  220.       moveF(2, 1)
  221.       else
  222.       moveF(f,1)
  223.       end
  224.     end
  225.   end
  226. end
  227.  
  228. function harvestLine()
  229. print("runn harvestline")
  230. y = tonumber(getPos("data/y.txt"))
  231.   if y == 0 then
  232.   dir = 1
  233.   elseif y == ymax then
  234.   dir = 3
  235.   else
  236.   print("Error line 240")
  237.   end
  238.  
  239. -- simplefied replant
  240. turtle.digDown()
  241. turtle.placeDown()
  242.  
  243. moveF(dir,1)
  244.   fm = fieldML - 2
  245.   for i = 1, fm do
  246.   turtle.digDown()
  247.   turtle.placeDown()
  248.   moveF(dir, 1)
  249.   i = i + 1
  250.   end
  251. d =  dir + 2
  252. moveF(d, fieldML)    
  253. end  
  254.    
  255.      
  256.  
  257.  
  258. background()
  259. checkPos()
  260.  
  261. while true do
  262. fuelCheck()
  263. checkActive()
  264. go()
  265. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement