Advertisement
fatboychummy

SimpleFarming.lua

Nov 25th, 2018
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.25 KB | None | 0 0
  1.  
  2.  
  3. local dataLocation = "/data/"
  4. local dataFile = "data"
  5. local data = {}
  6. local flag = true
  7.  
  8.  
  9.  
  10.  
  11. local function doSort(min)
  12.   local function swap(a,b,data)
  13.     turtle.select(a)
  14.     turtle.transferTo(16)
  15.     turtle.select(b)
  16.     turtle.transferTo(a)
  17.     turtle.select(16)
  18.     turtle.transferTo(b)
  19.     local tmp = data[a]
  20.     data[a] = data[b]
  21.     data[b] = tmp
  22.   end
  23.  
  24.   local dat = {}
  25.   local flg = true
  26.   for i = min,16 do --Get what items are there
  27.     local meta = turtle.getItemDetail(i)
  28.     if meta then
  29.       dat[i] = meta.name
  30.     end
  31.   end
  32.   if dat[16] then--If item in "swap" slot, move it to another slot.
  33.     for i = min,15 do
  34.       if not dat[i] then
  35.         swap(16,i,dat)
  36.         break
  37.       end
  38.     end
  39.   end
  40.  
  41.   while flg do
  42.     flg = false
  43.     for i = min,15 do
  44.       print(i,"=",textutils.serialize(dat))
  45.       print()
  46.       if not dat[i] and dat[i+1] ~= nil then--If item[i] doesnt exist, but item[i+1] exists, search for the first available slot, then send the item there.
  47.         print("Item a does not exist, item b does")
  48.         flg = true
  49.         for i2 = i,min,-1 do
  50.           print("StartLoop")
  51.           if dat[i2] then
  52.             print("Dati2")
  53.             turtle.select(i+1)
  54.             if dat[i2] == dat[i+1] then --If they are the same, balance.
  55.               print("dati2 = dati+1")
  56.               turtle.transferTo(i2)
  57.               if turtle.getItemCount(i+1) == 0 then --If there is no item left, remove from cache, otherwise move it to i2+1
  58.                 print("dati+1 = nil")
  59.                 dat[i+1] = nil
  60.               end
  61.             end
  62.             print("selecti+1")
  63.             turtle.transferTo(i2+1)
  64.             dat[i2+1] = dat[i+1]
  65.             dat[i+1] = nil
  66.             break
  67.           elseif i2 == min and not dat[i2] then -- If nothing in slot min, then move current item to slot min.
  68.             turtle.select(i+1)
  69.             turtle.transferTo(1)
  70.             dat[1] = dat[i+1]
  71.             dat[i+1] = nil
  72.           end
  73.         end
  74.       elseif ( dat[i] and dat[i+1] and dat[i] < dat[i+1] )  then
  75.         --if both exist, and item[i] < item[i+1] then swap them
  76.         flg = true
  77.         swap(i,i+1,dat)
  78.       elseif dat[i] == dat[i+1] and dat[i] ~= nil then
  79.         --if they are equal, try to balance.
  80.         turtle.select(i+1)
  81.         turtle.transferTo(i)
  82.         if turtle.getItemCount(i+1) == 0 then
  83.           dat[i+1] = nil
  84.         end
  85.       end
  86.     end
  87.   end
  88. end
  89.  
  90. --[[
  91. Sort: Sorts items according to seed requirements.
  92. ]]
  93. local function sort()
  94.   print("Sorting...")
  95.   if data.RequireSeeds then
  96.     local dat = {}
  97.     for i = 1,16 do --Get what items are there
  98.       local meta = turtle.getItemDetail(i)
  99.       if meta then
  100.         dat[i] = meta.name
  101.       end
  102.     end
  103.     if dat[1] ~= data.SeedName then --If the seedslot is not occupied by seeds, move them out of the way.
  104.       for i = 1,16 do
  105.         if not dat[i] then
  106.           dat[i] = dat[1]
  107.           dat[1] = nil
  108.           turtle.select(1)
  109.           turtle.transferTo(i)
  110.           break
  111.         end
  112.       end
  113.     end
  114.     for i = 1,16 do
  115.       if dat[i] and dat[i] == data.SeedName then --Move all seeds to slot 1
  116.         turtle.select(i)
  117.         turtle.transferTo(1)
  118.       end
  119.     end
  120.     doSort(2) --Sort all slots except slot 1
  121.   else
  122.     doSort(1) --Sort all slots
  123.   end
  124. end
  125.  
  126.  
  127. --[[
  128. Count: Counts how many slots are used, returns it.
  129. ]]
  130. local function count()
  131.   print("Counting inventory...")
  132.   local spUsed = 0
  133.  
  134.   for i = 1,16 do
  135.     --turtle.select(i)
  136.     if turtle.getItemCount(i) > 0 then
  137.       spUsed = spUsed + 1
  138.     end
  139.   end
  140.   print(spUsed, "of 16 slots used")
  141.   return spUsed
  142. end
  143.  
  144.  
  145. --[[
  146. Watcher: Reads inventory, and sets a flag to false if it's full, otherwise the flag is set to true.
  147. Possibly shut down instead?
  148. Also will sort the inventory according to data.
  149. ]]
  150. local function watcher()
  151.     local ca = count()
  152.     if ca < 14 then
  153.       print("Good to continue")
  154.       flag = true
  155.     else
  156.       print("Stopping.")
  157.       flag = false
  158.     end
  159.     sort()
  160. end
  161.  
  162.  
  163. --[[
  164. Dig: Digs a block, and checks if it needs to plant seeds.
  165. ]]
  166. local function dig()
  167.   print("Dig and replace")
  168.   turtle.dig()
  169.   if data.RequireSeeds then
  170.     turtle.select(1)
  171.     turtle.place()
  172.   end
  173. end
  174.  
  175.  
  176. --[[
  177. Farmer: Checks the block in front of it, and sees if it needs to be farmed.  Turns then sleeps
  178. if inventory is full, sleep 10 seconds, otherwise do nothing.
  179. ]]
  180. local function farmer()
  181.   local i = 0
  182.   while true do
  183.     if flag then
  184.       print("Inspecting...")
  185.       local block,meta = turtle.inspect()
  186.       if block then
  187.         if data.MaxGrowth > 0 then
  188.           if meta.state then
  189.             if meta.state.age >= data.MaxGrowth then
  190.               dig()
  191.             end
  192.           else
  193.             if meta.metadata >= data.MaxGrowth then
  194.               dig()
  195.             end
  196.           end
  197.         else
  198.           dig()
  199.         end
  200.       else
  201.         if data.RequireSeeds then
  202.           turtle.select(1)
  203.           turtle.place()
  204.         end
  205.       end
  206.     else
  207.       print("NO OP")
  208.       os.sleep(data.Delay * 5)
  209.       watcher()
  210.     end
  211.     if i % 10 == 0 then
  212.       watcher()
  213.       i = 0
  214.     end
  215.     i = i + 1
  216.     print("Turning")
  217.     turtle.turnRight()
  218.     print("Sleeping", data.Delay, "seconds.")
  219.     os.sleep(data.Delay)
  220.   end
  221. end
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230. --[[
  231. CreateData: Creates your data boi
  232. ]]
  233. local function createData(location)
  234.   if not fs.isDir(dataLocation) then
  235.     fs.makeDir(dataLocation)
  236.   end
  237.   local h = fs.open(dataLocation..dataFile,"w")
  238.   h.writeLine("return {")
  239.   h.writeLine("  MaxGrowth  = 7,")
  240.   h.writeLine("  Delay = 3,")
  241.   h.writeLine("  RequireSeeds = true,")
  242.   h.writeLine("  SeedName = \"minecraft:wheat_seeds\",")
  243.   h.writeLine("}")
  244.   h.close()
  245. end
  246.  
  247.  
  248. --[[
  249. ReadData: Reads the data into variable "data"
  250. ]]
  251. local function readData()
  252.   data = dofile(dataLocation..dataFile)
  253. end
  254.  
  255. -----------------------------------------------------------Program starts here
  256.  
  257. print("Starting up.")
  258. if fs.exists(dataLocation..dataFile) then
  259.   readData()
  260. else
  261.   createData(dataLocation..dataFile)
  262. end
  263.  
  264.  
  265.  
  266. print("Ready.")
  267.  
  268. local ok,err = pcall(farmer)
  269. if not ok then
  270.   printError(err)
  271.   os.sleep(10)
  272.   os.reboot()
  273. else print(ok,err)
  274. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement