Advertisement
fzed51

[lua] [CC] Turtle - la culture du blé / wheat farming v2

Aug 23rd, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.28 KB | None | 0 0
  1. -- constante
  2. local minute = 1/60
  3. local heure = 1
  4. local jour = 24
  5. local step = 0.9
  6.  
  7. -- config
  8. conf = {}
  9. conf.attente = 7 * jour
  10. conf.x = 9
  11. conf.y = 9
  12.  
  13. -- setup des slot
  14. local charbon = 1
  15. local graine = 2
  16.  
  17. local function setupOk()
  18.     local s1 = turtle.getItemCount(charbon)
  19.     local s2 = turtle.getItemCount(graine)
  20.     if (s1 > 1) then
  21.         return true
  22.     else
  23.         if (s1 <= 1) then print("Il n'y a pas asses de charbon.") end
  24.         if (s2 <= 1) then print("Il n'y a pas asses de graines.") end
  25.         return false
  26.     end
  27. end
  28.  
  29. local function setup()
  30.     print("s1 : charbon")
  31.     print("s2 : graine")
  32.     print("Appuyer sur une touche pour continuer.")
  33.     local event, code = os.pullEvent("key")
  34. end
  35.  
  36. while not setupOk() do
  37.     setup()
  38. end
  39.  
  40. local function trySelect(slot)
  41.     local slots = {}
  42.     local nbItem = 0
  43.     turtle.select(slot)
  44.     for s = 16, 1, -1 do
  45.         if (s ~= slot) then
  46.             if (turtle.compareTo(s)) then
  47.                 slots[#slots+1] = s
  48.                 nbItem = nbItem + turtle.getItemCount(s)
  49.             end
  50.         end
  51.     end
  52.     slots[#slots+1] = slot
  53.     nbItem = nbItem + turtle.getItemCount(slot)
  54.    
  55.     if nbItem > 1 then
  56.         turtle.select(slots[1])
  57.         return true
  58.     else
  59.         return false
  60.     end
  61. end
  62.  
  63. local function farm()
  64.    
  65.     local function cueillir()
  66.        
  67.         local function plante()
  68.        
  69.             local function selectGraine()
  70.                 return trySelect(graine)
  71.             end
  72.        
  73.             if selectGraine() then
  74.                 turtle.placeDown()
  75.             end
  76.         end
  77.        
  78.         turtle.digDown()
  79.         plante()
  80.     end
  81.    
  82.     turtle.forward()
  83.     for pass = 1,conf.x do
  84.         for n = 1,conf.y do
  85.             cueillir()
  86.             if (n==conf.y) then
  87.                 if (pass % 2 == 1) then
  88.                     turtle.turnRight()
  89.                     if (pass<conf.x) then
  90.                         turtle.forward()
  91.                         turtle.turnRight()
  92.                     else
  93.                         turtle.turnRight()
  94.                         for _ = 2,conf.y do turtle.forward() end
  95.                         turtle.turnRight()
  96.                     end
  97.                 else
  98.                     if (pass <conf.x) then             
  99.                         turtle.turnLeft()
  100.                         turtle.forward()
  101.                         turtle.turnLeft()
  102.                     else
  103.                         turtle.turnRight()
  104.                     end
  105.                 end
  106.             else
  107.                 turtle.forward()
  108.             end
  109.         end
  110.     end
  111.     for n = 1, conf.x do
  112.         if n < conf.x then
  113.             turtle.forward()
  114.         else
  115.             turtle.turnRight()
  116.         end
  117.     end
  118.     turtle.back()
  119. end
  120.  
  121. local function libre()
  122.     -- test inventaire plein
  123.     local resteFuel = turtle.getFuelLevel()
  124.     if (resteFuel == 'unlimited' or resteFuel < 80) then
  125.         if (trySelect(charbon)) then
  126.             return turtle.refuel(1)
  127.         else
  128.             return false
  129.         end
  130.     else
  131.         return true
  132.     end
  133. end
  134.  
  135. local function dateTime()
  136.     local d, t = os.day(), os.time()
  137.     return (d*24)+t
  138. end
  139.  
  140. function readAlert()
  141.     local alert = nil
  142.     if fs.exists("memory") then
  143.       local memFile = io.open('memory','r')
  144.       alert = tonumber(memFile:read("*a"))
  145.       memFile:close()
  146.     end
  147.     return alert
  148. end
  149.  
  150. function  saveAlert(alert)
  151.     local memFile = io.open('memory','w')
  152.     memFile:write(alert)
  153.     memFile:close()
  154. end
  155.  
  156. local now = dateTime()
  157. local alert = readAlert() or (now + conf.attente)
  158. saveAlert(alert)
  159.  
  160. while true do
  161.     term.clear()
  162.     term.setCursorPos(1,1)
  163.     if (libre() and now > alert) then
  164.         print("Jardinage en cours!")
  165.         farm()
  166.         alert = now + conf.attente
  167.         saveAlert(alert)
  168.     else
  169.         local j,h = math.floor((alert-now)/jour), textutils.formatTime(((alert-now)%jour), true)
  170.         print("En attente ...")
  171.         print("reprise dans : " ..j.. " jour(s) et " ..h)
  172.     end
  173.     os.sleep(step)
  174.     now = dateTime()
  175. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement