Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Crop Breeder from hoblin
- -- pastebin get fVj7fU52 cb
- -- Version 0.2.3
- --
- -- LAYOUT
- --
- -- Layer 1
- -- - - -
- -- D D -
- -- W D -
- --
- -- Layer 2
- -- P P T C
- -- - - N C
- -- C - P C
- --
- -- W - water block
- -- P - Autonomous Activator
- -- T - Trashcan
- -- N - Crop Analyser
- -- C - Chest
- -- D - Soil
- --
- -- System functions
- --
- function debug(message)
- if debugging then
- print(message)
- end
- end
- function resetScreen()
- if not debugging then
- term.clear()
- term.setCursorPos(1,1)
- end
- end
- --
- -- Breeder
- --
- Breeder = {}
- function Breeder:new()
- local obj = {
- redstone_pulse_length = 0.9,
- replace_seed_index = 0,
- seed = {name = '', gr = 0, ga = 0, st = 0},
- slot = {
- fuel = 1,
- sticks = 2,
- seed = 3,
- extra = 4
- },
- positions = {
- x = 2,
- y = 1,
- chest = {x = 0, y = 0},
- analyser = {x = 2, y = 1},
- trashcan = {x = 2, y = 2},
- seed = {},
- activator = {}
- }
- }
- obj.positions.seed[0] = {x = 1, y = 0}
- obj.positions.seed[1] = {x = 0, y = 1}
- obj.positions.seed[2] = {x = 1, y = 1}
- obj.positions.activator[0] = {x = 2, y = 0}
- obj.positions.activator[1] = {x = 0, y = 2}
- obj.positions.activator[2] = {x = 1, y = 2}
- function obj:getFuel()
- local current_amount = turtle.getItemCount(self.slot.fuel)
- if current_amount < 2 then
- self:move_to(self.positions.trashcan)
- turtle.select(self.slot.fuel)
- turtle.turnRight()
- turtle.suck(64 - current_amount)
- turtle.turnLeft()
- if turtle.getItemCount() == 0 then
- error('Out of fuel')
- end
- end
- end
- function obj:getSticks()
- local current_amount = turtle.getItemCount(self.slot.sticks)
- if current_amount < 60 then
- self:move_to(self.positions.activator[0])
- turtle.select(self.slot.sticks)
- turtle.turnRight()
- turtle.suck(60 - current_amount)
- turtle.turnLeft()
- if turtle.getItemCount() == 0 then
- error('Out of sticks')
- end
- end
- end
- function obj:getSeed()
- self:move_to(self.positions.analyser)
- turtle.select(self.slot.seed)
- local current_amount = turtle.getItemCount()
- if current_amount == 0 then
- turtle.turnRight()
- turtle.suck(2)
- turtle.turnLeft()
- end
- if turtle.getItemCount() == 0 then
- error('Out of seeds')
- end
- self:updateSeedStats(self:analyse())
- seed_info = turtle.getItemDetail()
- self.seed.name = seed_info.name
- end
- function obj:updateSeedStats(stats)
- self.seed.gr = stats[1]
- self.seed.ga = stats[2]
- self.seed.st = stats[3]
- end
- function obj:analyse()
- self:move_to(self.positions.analyser)
- analyser = peripheral.wrap("bottom")
- turtle.select(self.slot.seed)
- turtle.dropDown()
- analyser.analyze()
- while not analyser.isAnalyzed() do
- os.sleep(0.2)
- end
- gr, ga, st = analyser.getSpecimenStats()
- turtle.suckDown()
- return {gr, ga, st}
- end
- function obj:move_to(move_to)
- x_offset = self.positions.x - move_to.x
- y_offset = self.positions.y - move_to.y
- if x_offset > 0 then
- turtle.turnLeft()
- for i = 1, x_offset do
- turtle.forward()
- end
- turtle.turnRight()
- elseif x_offset < 0 then
- turtle.turnRight()
- for i = 1, math.abs(x_offset) do
- turtle.forward()
- end
- turtle.turnLeft()
- end
- if y_offset > 0 then
- for i = 1, y_offset do
- turtle.back()
- end
- elseif y_offset < 0 then
- for i = 1, math.abs(y_offset) do
- turtle.forward()
- end
- end
- self.positions.x = move_to.x
- self.positions.y = move_to.y
- return true
- end
- function obj:redstonePulse()
- redstone.setOutput("bottom", true)
- os.sleep(self.redstone_pulse_length)
- redstone.setOutput("bottom", false)
- end
- function obj:sow(index)
- self:move_to(self.positions.seed[index])
- turtle.select(self.slot.sticks)
- turtle.placeDown(1)
- self:move_to(self.positions.activator[index])
- turtle.select(self.slot.seed)
- turtle.dropDown(1)
- self:redstonePulse()
- end
- function obj:breed()
- self:move_to(self.positions.seed[2])
- turtle.select(self.slot.sticks)
- turtle.placeDown(1)
- self:move_to(self.positions.activator[2])
- turtle.dropDown(1)
- self:redstonePulse()
- end
- function obj:fixCrossCropSticks()
- self:checkFuel()
- self:checkSticks()
- self:move_to(self.positions.activator[2])
- turtle.select(self.slot.sticks)
- turtle.suckDown(1)
- turtle.dropDown(1)
- self:redstonePulse()
- self:move_to(self.positions.analyser)
- end
- function obj:harvest(index)
- self:move_to(self.positions.seed[index])
- turtle.select(self.slot.sticks)
- turtle.digDown()
- end
- function obj:waitForBreeding()
- self:move_to(self.positions.analyser)
- analyser = peripheral.wrap("bottom")
- while not analyser.hasPlant("SOUTH") do
- if not analyser.isCrossCrop("SOUTH") then
- self:fixCrossCropSticks()
- end
- if analyser.hasWeeds("SOUTH") then
- self:harvest(2)
- self:breed()
- self:move_to(self.positions.analyser)
- end
- sleep(1)
- end
- end
- function obj:setupField()
- if turtle.getItemCount(self.slot.seed) == 2 then
- self:sow(0)
- self:breed()
- self:sow(1)
- elseif turtle.getItemCount(self.slot.seed) == 1 then
- self:sow(0)
- self:breed()
- self:waitForBreeding()
- self:harvest(2)
- self:analyse()
- self:sow(1)
- self:breed()
- else
- error('Wrong seeds amount')
- end
- end
- function obj:cleanField()
- self:harvest(0)
- self:cleanInventory(self.slot.seed)
- self:cleanInventory(self.slot.extra)
- self:harvest(1)
- self:cleanInventory(self.slot.seed)
- self:cleanInventory(self.slot.extra)
- end
- function obj:isSeedBreeded()
- return (self.seed.gr + self.seed.ga + self.seed.st == 30)
- end
- function obj:cleanInventory(index)
- items_to_drop_amount = 0
- for i=index, self.slot.extra + 1 do
- items_to_drop_amount = items_to_drop_amount + turtle.getItemCount(i)
- end
- if items_to_drop_amount > 0 then
- self:move_to(self.positions.trashcan)
- for i=index, self.slot.extra + 1 do
- turtle.select(i)
- turtle.dropDown()
- end
- end
- end
- function obj:checkFuel()
- while turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 50 do
- turtle.select(self.slot.fuel)
- turtle.refuel(1)
- self:getFuel()
- end
- end
- function obj:checkSticks()
- if turtle.getItemCount(self.slot.sticks) < 10 then
- self:getSticks()
- end
- end
- function obj:isSameSpecies()
- seed_info = turtle.getItemDetail(self.slot.seed)
- return self.seed.name == seed_info.name
- end
- function obj:work()
- self:checkFuel()
- self:getFuel()
- self:getSticks()
- self:getSeed()
- self:setupField()
- while not self:isSeedBreeded() do
- self:checkFuel()
- self:checkSticks()
- self:waitForBreeding()
- self:harvest(2)
- breeded_stats = self:analyse()
- current_stats = {self.seed.gr, self.seed.ga, self.seed.st}
- self:updateSeedStats(breeded_stats)
- if self:isSeedBreeded() then
- -- Store breeded seed
- self:move_to(self.positions.chest)
- turtle.select(self.slot.seed)
- turtle.dropDown()
- self:cleanInventory(self.slot.extra)
- self:cleanField()
- else
- if ((current_stats[1] + current_stats[2] + current_stats[3]) < (breeded_stats[1] + breeded_stats[2] + breeded_stats[3])) and self:isSameSpecies() then
- -- replace parent with breeded
- self:harvest(self.replace_seed_index)
- self:sow(self.replace_seed_index)
- self:breed()
- self:cleanInventory(self.slot.extra)
- self.replace_seed_index = self.replace_seed_index + 1
- if self.replace_seed_index > 1 then
- self.replace_seed_index = 0
- end
- else
- -- trash duplicate
- self:breed()
- self:cleanInventory(self.slot.seed)
- end
- end
- end
- end
- setmetatable(obj, self)
- self.__index = self; return obj
- end
- worker = Breeder:new()
- while true do
- worker:work()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement