146 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
			
		
		
	
	
			146 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
CropController = {}
 | 
						|
 | 
						|
function CropController:new(t)
 | 
						|
    t = t or {}
 | 
						|
    setmetatable(t, self)
 | 
						|
    self.__index = self
 | 
						|
    t.matrix = {}
 | 
						|
    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 |