Advertisement
bob558

Cactus_ferm

Nov 23rd, 2016
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.86 KB | None | 0 0
  1. local com = require("component")
  2. local robot = require("robot")
  3. local computer = require('computer')
  4. local term = require("term")
  5. local note = require("note")
  6. local len,w = 8,8   -- длинна и ширина поля
  7. local s = 200 -- время ожидания созревания
  8.  
  9. local check_new, ch_n , side, n, cacPl = true, true, 0, 0, 0
  10. local wX, wY, draw, wXst, Vst, ch, clr = 5, 5, {" ", "#", "*", "█", "░"}, 0, 0, false, false
  11.  
  12. --------=== Графика ===--------
  13. local background = {1,99,50,0,1,99,44,2,2,1,1,99,4,0,1,99,4,2,2,1,99,38,2,2,1,2,1,1,0,
  14. 1,99,4,2,2,1,2,1,99,36,2,2,1,2,1,1,0,1,99,4,2,2,1,2,1,99,36,2,2,1,2,1,1,0,
  15. 1,99,4,2,2,1,2,1,99,36,2,99,4,1,1,0,1,99,4,2,99,4,1,99,34,2,1,2,2,1,99,4,0,
  16. 1,1,2,1,2,2,1,99,36,2,1,2,2,1,99,4,0,1,1,2,1,2,2,1,2,1,99,34,2,99,4,1,99,4,0,
  17. 1,1,2,1,2,2,1,2,1,99,36,2,2,1,2,1,1,0,1,1,2,1,2,99,4,1,99,36,2,2,1,2,1,1,0,
  18. 1,1,2,99,4,1,99,38,2,99,4,1,1,0,1,99,4,2,2,1,99,9,5,99,23,1,99,6,2,2,1,99,4,0,
  19. 1,99,4,2,2,1,99,5,5,99,31,1,1,2,2,1,99,4,0,5,5,1,1,2,2,1,1,5,99,42,0,5,99,50}
  20.  
  21. local signboard = {1,99,30,0,1,99,30,0,
  22. 4,1,1,4,1,99,4,4,4,1,4,1,1,4,1,4,4,4,1,4,1,1,4,1,1,1,4,4,1,0,
  23. 4,1,4,1,99,4,4,1,4,1,4,1,4,1,1,1,4,1,1,4,1,1,4,1,1,4,1,1,4,0,
  24. 4,1,4,1,1,1,4,1,1,4,1,4,1,4,1,1,1,4,1,1,1,4,1,4,1,4,1,99,4,0,
  25. 4,4,1,1,1,4,1,1,1,4,1,4,4,1,99,4,4,1,1,1,4,4,1,1,4,1,99,4,0,
  26. 4,1,4,1,1,4,99,5,1,4,1,4,1,1,1,4,1,99,4,4,1,1,4,1,99,4,0,
  27. 4,1,1,4,1,4,1,1,1,4,1,4,1,1,4,1,1,4,1,1,1,4,1,99,4,4,1,1,4,0,
  28. 4,1,1,4,1,4,1,1,1,4,1,4,1,1,4,1,1,4,1,1,4,1,99,6,4,4,1,}
  29.  
  30. local menu = {3,99,30,0,3,3,1,99,26,3,3,0,3,3,1,99,26,3,3,0,3,3,1,99,26,3,3,0,
  31. 3,3,1,99,26,3,3,0,3,3,1,99,26,3,3,0,3,3,1,99,26,3,3,0,3,3,1,99,26,3,3,0,3,99,30}
  32.  
  33. --------=== Звуки ===---------
  34. local m = {69,0.03,71,0.04,71,0.08,71,0.08,71,0.08,76,0.2,79,0.1}
  35. local m2 = {71,0.05,79,0.03}
  36. local m3 = {55,0.05,50,0.03}
  37.  
  38. ---------- Функции----------------
  39. local function check_energy() return computer.energy()>((computer.maxEnergy()*20)/100) end
  40.  
  41. local function farm(len)
  42.   for l = 1, len do
  43.     if robot.detectDown() then
  44.       robot.swingDown(); os.sleep(0.3)
  45.       robot.down(); os.sleep(0.3)
  46.       robot.swingDown(); os.sleep(0.3)
  47.       robot.up()
  48.       robot.forward()
  49.     else robot.forward(); os.sleep(0.3)
  50.     end
  51.   end
  52. end
  53.  
  54. local function work(len, side)
  55.   turn = robot[(side == "right") and "turnRight" or "turnLeft"]
  56.   farm(len)
  57.   turn()
  58.   robot.forward(); os.sleep(0.3)
  59.   turn()
  60. end
  61.  
  62. local function back (len, w, side)
  63.   if side == "right" then
  64.     robot.turnRight()
  65.     for i = 1, w do
  66.       robot.forward(); os.sleep(0.3)
  67.     end
  68.   else
  69.     for i = 1, len do
  70.       robot.forward(); os.sleep(0.3)
  71.     end
  72.     robot.turnLeft()
  73.       for i = 1, w do
  74.       robot.forward(); os.sleep(0.3)
  75.       end
  76.   end
  77.   robot.turnRight()
  78.   robot.forward(); os.sleep(0.3); robot.forward(); os.sleep(0.3)
  79. end
  80.  
  81. local function unload()
  82.   for c = 1, 16 do
  83.     robot.select(c)
  84.     if robot.count() > 0 then
  85.       robot.drop()
  86.     else
  87.       robot.select(1)
  88.       break
  89.     end
  90.   end
  91.   robot.turnAround()
  92. end
  93.  
  94. local function trim(str)
  95.   return string.gsub(str, "%s+","")
  96. end
  97.  
  98. local function drawfn(clr, t, wX, wY)
  99.   if clr then
  100.     term.clear()
  101.     clr = false
  102.   end
  103.   wXst=wX
  104.   term.setCursor(wX, wY)
  105.   for i = 1, #t do
  106.     if t[i]==0 then
  107.       wY=wY+1
  108.       wX=wXst
  109.     elseif t[i]==99 then
  110.       ch=true
  111.       elseif ch then
  112.         ch=false
  113.         tt=t[i]
  114.         for a=1, tt-1 do
  115.           term.setCursor(wX, wY)
  116.           term.write(draw[Vst])
  117.           wX=wX+1
  118.         end
  119.       else
  120.         Vst=t[i]
  121.         term.setCursor(wX, wY)
  122.         term.write(draw[t[i]])
  123.         wX=wX+1
  124.     end
  125.   end
  126. end
  127.  
  128. local function song(n)
  129.   for i = 1, #n do
  130.   if ch_n then
  131.     ch_n = false
  132.     n1 = n[i]
  133.     else
  134.       ch_n = true
  135.       n2 = n[i]
  136.       note.play(n1, n[i])
  137.     end
  138.   end
  139. end
  140.  
  141. local function StatEnrg()
  142.   term.setCursor(15, 9)
  143.   term.write("MAX заряд:     "..(computer.maxEnergy()))
  144.   term.setCursor(15, 10)
  145.   term.write("Заряд батареи: "..(math.floor(computer.energy())))
  146. end
  147.  
  148. ------------ Main -----------------
  149. clr = true
  150. wX = 1; wY = 1
  151. t = background
  152. drawfn(clr, t, wX, wY) -- вывести фоновую заставку
  153. os.sleep(0.5)
  154. wX = 11; wY = 3
  155. clr = false
  156. t = signboard
  157. drawfn(clr, t, wX, wY)
  158. term.setCursor(20, 3)
  159. term.write("Ф Е Р М А")
  160. n = m2
  161. song(n)
  162. os.sleep(2)
  163.  
  164. wX = 11; wY = 3
  165. t = menu
  166. drawfn(clr, t, wX, wY)
  167. term.setCursor(17, 4)
  168. term.write("Если нужно засеять")
  169. term.setCursor(17, 5)
  170. term.write("  поле "..len.." на "..w)
  171. term.setCursor(21, 6)
  172. term.write("кактусами")
  173. term.setCursor(17, 8)
  174. term.write("в 1 слот положи - ░")
  175. term.setCursor(17, 9)
  176. term.write("в 2 слот положи - #")
  177. term.setCursor(17, 15)
  178. term.write("Засеять поле? y/n ")
  179.  
  180. if trim(term.read())=="y" then
  181.   n = m2
  182.   song(n)
  183.   clr = false; wX = 1; wY = 1
  184.   t = background
  185.   drawfn(clr, t, wX, wY)
  186.   wX = 11; wY = 3; clr = false
  187.   t = menu
  188.   drawfn(clr, t, wX, wY)
  189.   robot.forward(); os.sleep(0.3);robot.forward(); os.sleep(0.3)
  190.   for ww = 1, w do
  191.     for l = 1, len do
  192.       if check_new  then
  193.         robot.down(); os.sleep(0.3);robot.down(); os.sleep(0.3);robot.down(); os.sleep(0.3)
  194.         robot.select(1);robot.swingDown(0);robot.placeDown(0) -- поставить блок песка
  195.         robot.up();robot.select(2);robot.placeDown(0) -- поставить блок кактуса
  196.         robot.up(); robot.up(); robot.forward(); os.sleep(0.3)
  197.         cacPl = cacPl + 1
  198.         term.setCursor(15, 5)
  199.         term.write("Посажено кактусов "..cacPl.." шт")
  200.         check_new = false
  201.       else robot.forward(); os.sleep(0.3)
  202.         check_new = true
  203.       end
  204.     end
  205.        if check_new  then check_new = false else check_new = true end
  206.         if ww % 2 == 1 then
  207.           robot.turnLeft()
  208.           robot.forward()
  209.           robot.turnLeft()
  210.          else
  211.           robot.turnRight()
  212.           robot.forward()
  213.           robot.turnRight()
  214.         end
  215.    end
  216.    back(len, w, (w % 2 == 0) and "right" or "Around")
  217.    unload()
  218.    wX=11; wY=3; clr = false
  219.    t = menu
  220.    drawfn(clr, t, wX, wY)
  221.    n = m
  222.    song(n)
  223.    term.setCursor(15, 5)
  224.    term.write("Посажено кактусов "..cacPl.." шт")
  225.    term.setCursor(17, 7)
  226.    term.write("Ожидаю первый урожай ")
  227.    term.setCursor(20, 8)
  228.    term.write(s.." секунд")
  229.    for i=1, 10 do
  230.      os.sleep(s/10)
  231.      term.setCursor(20, 9)
  232.      print("Прошло ",(10*i)..'%')
  233.    end
  234. end
  235.  
  236.  
  237. ----------======= Цикл обработки поля =======-----------
  238.  
  239.  
  240. while true do
  241.   n = m2
  242.   song(n)
  243.   wX=11; wY=3; clr = false
  244.   t = menu
  245.   drawfn(clr, t, wX, wY)
  246.   term.setCursor(17, 4)
  247.   term.write("Ферма кактусов "..cacPl.." шт")
  248.   term.setCursor(17, 6)
  249.   term.write("Между сбором урожая ")
  250.   term.setCursor(20, 7)
  251.   term.write(s.." секунд")
  252.   StatEnrg()
  253.   robot.forward(); os.sleep(0.3);robot.forward(); os.sleep(0.3)
  254.   for i = 1, w do
  255.       work(len, (i % 2 == 0) and "right" or "left")
  256.   end
  257.     back(len, w, (w % 2 == 0) and "right" or "Around")
  258.     unload()
  259.     n = m
  260.     song(n)
  261.     for i=1, 10 do
  262.       os.sleep(s/10)
  263.       term.setCursor(20, 10)
  264.       print("Прошло ",(10*i)..'%')
  265.       StatEnrg()
  266.     end
  267.       if check_energy() then
  268.         wX=11; wY=3; clr = false
  269.         t = menu
  270.         drawfn(clr, t, wX, wY)
  271.         term.setCursor(17, 4)
  272.         term.write("Низкий уровень заряда батареи!")
  273.         term.setCursor(17, 6)
  274.         term.write("Требуется подзарядка")
  275.         n = m3
  276.         song(n)
  277.         repeat  -- зарядка от солнечных батарей
  278.           StatEnrg()
  279.         until (computer.energy())==(computer.maxEnergy())
  280.        
  281.       end
  282. end
  283. ------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement