Advertisement
fauconjona

Class Farmer

Jan 29th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. local component = require("component")
  2. local gpu = component.gpu -- get primary gpu component
  3. local Robot = require("robotModule")
  4.  
  5.  
  6. --[[
  7. Class Farmer
  8. ]]--
  9.  
  10. Farmer = Robot:new()
  11.  
  12. function Farmer:new(x, y, z, f, invIgnore, size, wait, useItem)
  13. local o = Robot:new(x, y, z, f, invIgnore)
  14. setmetatable(o, self)
  15. self.__index = self
  16. self.size = size
  17. self.wait = wait
  18. self.placing = true
  19. self.useItem = useItem or false
  20. return o
  21. end
  22.  
  23. function Farmer:selectSeed()
  24. local index = -1
  25. if not self.isTest then
  26. for i = 1, self.invIgnore do
  27. if self.robot.count(16 - i + 1) > 1 then
  28. index = 16 - i + 1
  29. break
  30. end
  31. end
  32. end
  33. return index
  34. end
  35.  
  36. function Farmer:step()
  37. self:setLightColor("green")
  38. self:setState((self.placing and "Planting" or "Gathering") .. " : x = " .. self.x .. " / y = " .. self.y .. " / z = " .. self.z)
  39. if not self.isTest then
  40. if self.placing then
  41. local seedIndex = self:selectSeed()
  42. if seedIndex <= 0 then
  43. return false
  44. end
  45. self.robot.useDown()
  46. self.robot.select(seedIndex)
  47. self.robot.placeDown()
  48. else
  49. if self.useItem then
  50. self.robot.useDown()
  51. else
  52. self.robot.swingDown()
  53. end
  54. self.robot.suckDown()
  55. end
  56. else
  57. os.sleep(1)
  58. end
  59. return true
  60. end
  61.  
  62. function Farmer:displayFarm()
  63. self:displayInfo()
  64.  
  65. local width, height = gpu.getViewport()
  66.  
  67. for i = 0, self.size do
  68. local line = ""
  69. for j = 0, self.size do
  70. if i == self.x - self.startX and j == self.y - self.startY then
  71. line = line .. " X"
  72. elseif i ~= 0 and j ~= 0 then
  73. line = line .. " -"
  74. else
  75. line = line .. " "
  76. end
  77. end
  78. gpu.set((width / 2) - string.len(line) / 2, 4 + i, line)
  79. end
  80. end
  81.  
  82. function Farmer:farm()
  83. self.goHome = false
  84.  
  85. if self.f ~= self.startF then
  86. self:turnTowards(self.startF)
  87. end
  88.  
  89. for i = 1, self.size do
  90. for j = 1, self.size do
  91. self:moveTo(self.startX + i, self.startY + ((i % 2 == 0) and (self.size - j + 1) or j), self.startZ + 1, false)
  92. if not self:step() then
  93. self.goHome = true
  94. self:setLightColor("yellow")
  95. self:setState("No more seed, going home")
  96. break
  97. end
  98. end
  99.  
  100. if self.goHome then
  101. break
  102. end
  103. end
  104.  
  105. self:dropInventory()
  106.  
  107. if self.placing then
  108. for i = 1, self.wait do
  109. self:setLightColor("blue")
  110. self:setState("Waiting " .. tostring(self.wait - i) .. " minutes")
  111. os.sleep(60)
  112. end
  113. self:setState("Shit! Here we go again!")
  114. end
  115.  
  116. self.placing = not self.placing
  117.  
  118. return true
  119. end
  120.  
  121.  
  122. return Farmer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement