CropController = {} CropController.title = "Wheat farm" function CropController:new(t) t = t or {} setmetatable(t, self) self.__index = self t.matrix = {} t.stats = {} t.stats["Harvest Interval"] = 900 return t end function CropController:getMatrix() local s, matrixString = self:doAction(self.slaveID, "getMatrix") local matrix if (s == true) then self.matrix = self:toMatrix(matrixString) end end function CropController:initMonitor() self.monitor = peripheral.find("monitor") self.monitor.setBackgroundColour(colours.black) self.monitor.clear() self.monitor.setTextScale(0.5) self.monitor.setTextColour(colours.white) term.redirect(self.monitor) end function CropController:displayMatrix(x,y) if(type(x) ~= "number") then x = 1 end if(type(y) ~= "number") then y = 1 end assert(type(self.matrix) == "table") assert(type(self.matrix[1]) == "table") local width = #self.matrix local length = #self.matrix[1] for i = 1, width do for j = 1, length do term.setCursorPos(i + x - 1,j + y - 1) if(self.matrix[i][j] >= 0) then if(self.matrix[i][j] >= 6) then term.setTextColour(colours.orange) elseif(self.matrix[i][j] >= 4) then term.setTextColour(colours.yellow) elseif(self.matrix[i][j] >= 2) then term.setTextColour(colours.lime) else term.setTextColour(colours.green) end term.write(tostring(self.matrix[i][j])) else term.setTextColour(colours.blue) term.write("W") end end end end function CropController:updateFuelLevel() local s, fuelLevel = self:doAction(self.slaveID, "getFuelLevel") if(s == true) then self.turtleFuelLevel = fuelLevel end return s end function CropController:toMatrix(str) local matrix = {} assert(type(str) == "string") assert(str:sub(1,1) == "{", "the string must begin with a \"{\"") local len = str:len() assert(str:sub(len,len) == "}", "the string must end with a \"}\"") local s1, ob = str:gsub("{", "{") local s2, cb = str:gsub("}", "}") assert(ob == cb, "the string must have the same amount of opening brackets as closing brackets") str = str:sub(2, len - 1) len = len - 2 if ob == 1 then return self:toArray(str) else local level = 0 local lastC = 0 for i = 1, len do local c = str:sub(i,i) if c == "{" then level = level + 1 elseif c == "}" then level = level - 1 elseif c == "," then if level == 0 then matrix[#matrix+1] = self:toMatrix(str:sub(lastC + 1, i - 1)) lastC = i end end end matrix[#matrix+1] = self:toMatrix(str:sub(lastC + 1, len)) return matrix end end function CropController:toArray(str) local len = str:len() local comma = str:find(",") local arr = {} while(type(comma)=="number") do arr[#arr+1] = str:sub(1, comma-1) local latest = tonumber(arr[#arr]) if(latest ~= nil) then arr[#arr] = latest end str = str:sub(comma + 1, len) len = str:len() comma = str:find(",") end if(str ~= "") then arr[#arr+1] = str local latest = tonumber(arr[#arr]) if(latest ~= nil) then arr[#arr] = latest end end return arr end function CropController:sendMessage(id, message, protocol) rednet.send(id, message, protocol) local id2, message2 = rednet.receive("received", 1) return message == message2 end function CropController:doAction(id, message, protocol) self:sendMessage(id, message, protocol) local id2, message2, protocol2 repeat id2, message2, protocol2 = rednet.receive() until id2 == id return protocol2 == "confirm", message2 end function CropController:setSlaveID(id) self.slaveID = id end function CropController:harvest() self:doAction(self.slaveID, "harvest") local s, duration = self:doAction(self.slaveID, "getLastHarvestDuration") if s == true then self.stats["Last harvest duration"] = duration end local s, yield = self:doAction(self.slaveID, "getLastYield") if s == true then self.stats["Last yield"] = yield end self:getMatrix() end function CropController:displayStats(x,y) local keyWidth = 0 for k, v in pairs(self.stats) do if string.len(tostring(k)) > keyWidth then keyWidth = string.len(tostring(k)) end end for k, v in pairs(self.stats) do term.setCursorPos(x, y) term.write(tostring(k)) term.setCursorPos(x + keyWidth + 1, y) term.write(tostring(v)) y = y + 1 end end function CropController:displayTitle(x,y) term.setCursorPos(x,y) term.write(self.title) local width = term.getSize() for i = 1, width do term.setCursorPos(i, y+1) term.write("_") end end function CropController:refreshDisplay() term.clear() term.setTextColour(colours.white) self:displayTitle(1,1) self:displayMatrix(1,3) term.setTextColour(colours.white) local matrixHeight = #self.matrix self:displayStats(1, matrixHeight + 4) end function CropController:updateTimeToHarvest(x,y) term.clearLine(y) term.setCursorPos(x,y) term.write("next harvest in " .. tostring(math.ceil(self.stats["Harvest Interval"] - (os.clock() - self.lastHarvest))) .. " seconds") end function CropController:main() local termWidth, termHeight = term.getSize() local cc = CropController:new() cc:setSlaveID(5) cc:doAction(cc.slaveID, "init") cc:initMonitor() while true do cc.lastHarvest = os.clock() cc:harvest() cc:refreshDisplay() while self.stats["Harvest Interval"] - (os.clock() - self.lastHarvest) > 0 do cc:updateTimeToHarvest(1, termHeight - 1) sleep(0.5) end end end