Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local gpu = component.gpu
- local modem
- if component.isAvailable("modem") then modem = component.modem else print("Модем не установлен") end
- local term = require("term")
- local event = require("event")
- local unicode = require("unicode")
- local screen = component.screen
- local serialization = require("serialization")
- local areas = {}
- local lists = {main = {x=2, y=6, width=40, height=16, content={}, limit=160, top = false, posF = 1, sb={0x0000FA, true, length=0, shift=0, work=false}, cFore = 0xFAFA00, cBack = 0x222222},
- commands = {x=46, y=6, width=30, height=16, content={}, limit=160, top=true, posF = 1, sb={0x0000FA, true, length=0, shift=0, work=false}, cFore = 0xFAFA00, cBack = 0x222222}}
- local modemAddress = "1"
- local robotModemAddress = "2"
- local messages = {}
- local xCursor, yCursor, xCursorMove = 2, 23, 2
- local q = 0
- local funcs = {}
- local port = 384
- local cards = {}
- local wScreen, hScreen = gpu.maxResolution()
- local commandList = {"Команды:",
- "connect &&&",
- "Подключиться к роботу",
- "&&& - первые три символа сетевой карты робота",
- " ",
- "reg cmp|rbt &&&",
- "Зарегистрировать и записать в файл адреса: cmp - компьютера или rbt - робота; &&& - первые три символа сетевых карт компьютера или робота",
- " ",
- "get cmpt|rbt",
- "Возвращает адрес сетевой карты компьютера или робота",
- " ",
- "cng feed true|false",
- "Меняет параметр кормления телят на 'КОРМИТЬ' или 'НЕ КОРМИТЬ'",
- " ",
- "cng time|fdtm|fcnt number",
- "Меняет настройки: time - время ожидания перед началом фарминга; fdtm - количество пшеницы, которая будет скормлена одному теленку; fcnt - общее количество коров, number - любое числовое значение",
- " ",
- "req nrg|dura|wht|fdtm|fcnt|feed|time",
- "Запрашивает настройки: nrg - количетсво энергии в роботе; dura - прочность экипированного инструмента; wht - количество оставшейся пшеницы; fdtm - количество пшеницы на одного теленка; fcnt - количество коров; feed - будет кормить телят робот или нет; time - время ожидания перед началом фарминга",
- " ",
- "req slot|iSlot number|all",
- "Запрашивает данные по слотам: slot - инвентарь, в который перекладывается вся добыча; iSlot - внутренние (internal) слоты, number - чмсло, указывающее определнный слот; all - все слоты",
- " ",
- "exit",
- "Выход из программы"}
- local production = {}
- production["Сырая говядина"] = 0
- production["Кожа"] = 0
- local file = io.open("cards", "r")
- if file then
- local lines = {}
- for line in file:lines() do table.insert(lines, line) end
- for i = 1, #lines do
- for value, value1 in lines[i]:gmatch("(%w+) = (.+)") do
- cards[value] = value1
- end
- end
- modemAddress = cards.cmpt
- robotModemAddress = cards.rbt
- file:close()
- end
- if modem then
- modem.open(port)
- end
- function add(list, data)
- local wList = lists[list]
- local cPos, shift = 1, wList.width
- local totalLines = #wList.content
- if data then
- local count = math.ceil(unicode.wlen(data) / wList.width)
- if totalLines + count > 160 then
- for i = 1, count do
- table.remove(wList.content, 1)
- end
- end
- -- if totalLines > wList.height then
- -- wList.posF = totalLines - wList.height + 1
- -- end
- for i = 1, count do
- table.insert(wList.content, unicode.sub(data, cPos, cPos+shift-1))
- cPos = cPos + shift
- end
- totalLines = #wList.content
- if totalLines > wList.height and wList.top == false then
- wList.posF = totalLines - wList.height + 1
- end
- end
- if wList.sb then
- local length = 0
- local q = 0
- if totalLines > wList.height then
- length = math.floor(wList.height/totalLines*wList.height)
- length = length > 0 and length or 1
- q = (wList.height - 1 - length)/(totalLines - 1 - wList.height)
- q = q > 0 and q or 1
- wList.sb.q = q
- -- wList.sb.thumbPosition = math.ceil(math.abs(q*wlist.posF-realLength))
- wList.sb.work = true
- else
- length = wList.height
- q = 0
- end
- wList.sb.q = q
- wList.sb.thumb = string.rep(" ", length)
- end
- drawList(lists.main)
- end
- function funcs.reg(str)
- local data = getArgs(str)
- local result
- local file = io.open("cards", "w")
- --for modem in component.list("modem") do
- -- if modem ~= modemAddress then
- -- robotModemAddress = modem
- -- end
- --end
- -- if robotModemAddress ~= "" then
- -- data = "Карта зарегистрирована:".." "..robotModemAddress
- -- component.setPrimary("modem", modemAddress)
- -- else
- -- data = "Карта не зарегистрирована. Вставьте вторую сетевую карту и попробуйте снова"
- -- end
- if data[1] then
- if data[2] then
- if data[1] == "cmp" then
- modemAddress = component.get(data[2])
- if modemAddress then
- result = "Карта КОМПЬЮТЕРА зарегистрирована:".." "..modemAddress
- cards.cmpt = modemAddress
- else
- result = "Такого модема нет в системе"
- end
- elseif data[1] == "rbt" then
- robotModemAddress = component.get(data[2])
- if robotModemAddress then
- if robotModemAddress == modemAddress then
- result = "Эта карта уже зарегистрирована, как карта КОМПЬЮТЕРА. Введите другой адрес"
- else
- result = "Карта РОБОТА зарегистрирована:".." "..robotModemAddress
- cards.rbt = robotModemAddress
- end
- else
- result = "Такого модема нет в системе"
- end
- else
- result = "Неверная команда"
- end
- else
- result = "Не указан адрес модема или команда введена не верно"
- end
- else
- result = "Не указаны параметры регистрации"
- end
- for k,v in pairs(cards) do
- file:write(k.." = "..v.."\n")
- end
- file:close()
- add("main", result)
- drawList(lists.main)
- end
- function drawAreas(area)
- gpu.setForeground(area.cFore or 0xFFFFFF)
- gpu.setBackground(area.cBack or 0x000000)
- gpu.fill(area.x, area.y, area.width, area.height, " ")
- if area.sList then
- drawList(area.sList)
- end
- if area.data then
- local firstY = area.y
- for i = 1, #area.data do
- gpu.set(area.x, firstY, area.data[i])
- firstY = firstY + 1
- end
- if area.data.outTbl then
- for k,v in pairs(area.data.outTbl) do
- gpu.set(area.x, firstY, tostring(k) .. " " .. tostring(v))
- firstY = firstY + 1
- end
- end
- end
- gpu.setBackground(0x000000)
- end
- function drawList(list)
- gpu.setForeground(list.cFore)
- gpu.setBackground(list.cBack)
- local allLines = #list.content
- --gpu.set(8, 8, tostring(allLines))
- --gpu.set(16, 16, tostring(list.sb.shift))
- --gpu.set(80, 16, tostring(list.sb.shift))
- local linesCount = list.height
- gpu.fill(list.x, list.y, list.width, list.height, " ")
- --for k,v in pairs(list) do
- -- if not list.top then
- -- if #list.content > list.height then
- -- local fLine = allLines >= linesCount and 1 or math.abs(linesCount - allLines) + 1
- -- for i = linesCount, fLine, -1 do
- -- gpu.set(list.x, list.y+i-1, list.content[allLines-(linesCount-i)])
- -- end
- -- end
- -- else
- -- local fLine = allLines > linesCount and linesCount or allLines
- -- for i = 1, fLine do
- -- gpu.set(list.x, list.y+i-1, list.content[i])
- -- end
- -- end
- local shiftF = -1
- local fLine = allLines > linesCount and linesCount or allLines
- if not list.top and allLines < linesCount then
- shiftF = linesCount - allLines - 1
- end
- for i = 1, fLine do
- gpu.set(list.x, list.y+i+shiftF, list.content[i+list.posF-1])
- end
- if list.sb then
- gpu.setBackground(0x000044)
- gpu.fill(list.x+list.width, list.y, 1, list.height, " ")
- gpu.setBackground(list.sb[1])
- local thumbPosition = math.ceil(list.sb.q*(list.posF-1))
- gpu.set(list.x+list.width, list.y+thumbPosition, list.sb.thumb, list.sb[2])
- end
- --end
- gpu.setForeground(0xFAFA00)
- gpu.setBackground(0x222222)
- end
- --local funcs = {reg = regCard}
- function funcs.connect(str)
- local result = ""
- local addr = getArgs(str)
- if modemAddress ~= "1" and robotModemAddress ~= "2" and modemAddress and robotModemAddress then
- if addr[1] ~= nil then
- modem.send(robotModemAddress, port, "connect".." "..addr[1])
- local _,_,_,_,_,message = event.pull(2, "modem_message", modemAddress, robotModemAddress)
- if message == nil then
- result = "Не удалось подключиться к роботу"
- else
- result = message
- end
- else
- result = "Укажите адрес, к которому хотите подключиться"
- end
- else
- result = "Модем компьютера или робота не зарегистрирован"
- end
- add("main", result)
- end
- function getTableLength(tbl)
- local length = 0
- for k,v in pairs(tbl) do
- if k then length = length + 1 end
- end
- return length
- end
- function funcs.get(str)
- local cName = getArgs(str)
- local result = ""
- if getTableLength(cards) > 0 then
- if cards[cName[1]] then
- result = cards[cName[1]]
- else
- result = "Компонент не указан или его нет в списке"
- end
- else
- result = "Зарегистрированы не все модемы"
- end
- add("main", result)
- end
- function funcs.cng(str)
- local result = ""
- local parameters = getArgs(str)
- modem.send(robotModemAddress, port, "cng", parameters[1], parameters[2])
- local _,_,_,_,_,message = event.pull(2, "modem_message", modemAddress, robotModemAddress)
- if not message then
- result = "Нет связи с роботом"
- else
- result = "Robot: "..message
- end
- add("main", result)
- end
- function funcs.req(str)
- local result = ""
- local parameters = getArgs(str)
- modem.send(robotModemAddress, port, "req", parameters[1], parameters[2])
- local _,_,_,_,_,message = event.pull(20, "modem_message", modemAddress, robotModemAddress)
- if not message then
- result = "Нет связи с роботом"
- else
- result = "Robot: "..message
- end
- add("main", result)
- end
- gpu.setResolution(80, 25)
- gpu.setBackground(0x444444)
- term.clear()
- table.insert(areas, {x=lists.main.x, y=lists.main.y, width=lists.main.width, height=lists.main.height, sList=lists.main, cBack = lists.main.cBack})
- table.insert(areas, {x=lists.commands.x, y=lists.commands.y, width=lists.commands.width, height=lists.commands.height, sList=lists.commands, cBack = lists.commands.cBack})
- table.insert(areas, {x=2, y=2, width=40, height=3, data={"Добыча:", outTbl = production}, cFore = 0xFFFFFF, cBack = 0x222222})
- add("main")
- for i = 1, #commandList do
- add("commands", commandList[i])
- end
- for k,v in pairs(areas) do
- drawAreas(v)
- end
- gpu.setForeground(0xFAFA00)
- gpu.setBackground(0x222222)
- gpu.fill(xCursor, yCursor, lists.main.width, 1, " ")
- term.setCursor(xCursor, yCursor)
- local str = ""
- function getArgs(data)
- local gArgs = {}
- for arg in data:gmatch("%S+") do table.insert(gArgs, arg) end
- table.remove(gArgs, 1)
- return gArgs
- end
- function funcs.exit()
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- term.clear()
- gpu.setResolution(wScreen, hScreen)
- os.exit()
- end
- --print(modemAddress, robotModemAddress)
- while true do
- --local termData = term.read(_, false):match("[^\n]+")
- --add("main", termData)
- --drawList(lists.main)
- --gpu.fill(xCursor, yCursor, 40, 1, " ")
- --term.setCursor(xCursor, yCursor)
- local event, address, char, port, distance, message, message1 = term.pull()
- if event == "key_down" then
- local addition = unicode.char(char):match("[^%c]") or ""
- str = str .. addition
- local newStr = unicode.sub(str, -40, unicode.wlen(str))
- gpu.set(xCursor, yCursor, newStr)
- xCursorMove = xCursorMove + (xCursorMove - xCursor < 40 and unicode.wlen(addition) or 0)
- term.setCursor(xCursorMove, yCursor)
- if char == 13 then
- -- local fnc = str:match("%w+")
- -- if funcs[fnc] then funcs[fnc]() end
- add("main", str)
- drawList(lists.main)
- gpu.setForeground(0xFAFA00)
- gpu.setBackground(0x222222)
- gpu.fill(xCursor, yCursor, 40, 1, " ")
- xCursorMove = xCursor
- term.setCursor(xCursor, yCursor)
- local fnc = str:match("%w+")
- if funcs[fnc] then funcs[fnc](str) end
- str = ""
- elseif char == 8 then
- str = unicode.wtrunc(str, unicode.wlen(str))
- gpu.fill(xCursor, yCursor, 40, 1, " ")
- gpu.set(xCursor, yCursor, str)
- xCursorMove = xCursorMove - (xCursorMove > xCursor and 1 or 0)
- term.setCursor(xCursorMove, yCursor)
- end
- elseif event == "modem_message" and char == robotModemAddress then
- add("main", "Robot: "..message)
- if message1 then
- local prod = serialization.unserialize(message1)
- add("main", "Robot: добыча на этот раз:")
- for name, count in pairs(prod) do
- add("main", name .. " " .. tostring(count))
- if production[name] then
- production[name] = production[name] + count
- -- else
- -- production[name] = count
- end
- end
- drawAreas(areas[3])
- end
- elseif event == "scroll" then
- local lst = false
- for k,v in pairs(areas) do
- if char >= v.x and char < v.x+v.width and port >= v.y and port < v.y+v.height and v.sList then
- lst = v.sList
- break
- end
- end
- if lst and lst.sb.work then
- if lst.content[(lst.posF - distance) + ((lst.height - 1) * ((1-distance)/2))] then
- lst.posF = lst.posF - distance
- end
- drawList(lst)
- end
- end
- end
Add Comment
Please, Sign In to add comment