Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --pastebin get 1bqpedUA rcRobot
- local component = require("component")
- local modem = component.modem
- local gpu = component.gpu
- local event = require("event")
- local unicode = require("unicode")
- local term = require("term")
- local serialization = require("serialization")
- modem.open(346)
- activatorSearch = true
- result = false
- namesAndAddresses = {}
- analyzes = {}
- rCmps = {}
- columnPositions = {1, 5, 9, 13}
- textures = {}
- filler = "▀"
- areas = {{73,30},{73,14},{92,32},{73,22},{89,22},{57,22}}
- rSide = "3"
- range = "1"
- file = io.open("addresses", "r")
- for word in file:lines() do
- if word:match("%/(%w+)") == "robot" then
- for name,type in word:gmatch("(%w+)=(%w+%-%w+%-%w+%-%w+%-%w+)") do namesAndAddresses[name]=type end
- end
- end
- file:close()
- --for kN,vN in pairs(namesAndAddresses) do
- --print(kN,vN)
- --end
- fileTextures = io.open("Textures", "r")
- for line in fileTextures:lines() do table.insert(textures, serialization.unserialize(line)) end
- fileTextures:close()
- gpu.set(134, 12, ">")
- function authorization()
- while true do
- modem.broadcast(346, "Authorize me!")
- local _,addressTo,addressFrom,_,_,message,message1,message2,message3,message4 = event.pull(0.1, "modem")
- if message == "Authorization complete" then
- rAddress = message1
- robotName = message2
- rCmps = serialization.unserialize(message3)
- rFacing = message4
- gpu.set(76, 11, rFacing)
- slotRaw = 1
- slotColumn = 1
- for i = 1, rCmps[1] do
- table.insert(buttons, {x=columnPositions[slotColumn], y=15+slotRaw, text="["..tostring(i).."]", func=rSelect, slot=i, active = true, cFore = 0xFFFFFF, cBack = 0x555555})
- slotColumn = slotColumn + 1
- if slotColumn == 5 then
- slotRaw = slotRaw + 1
- slotColumn = 1
- end
- end
- table.insert(buttons, {x=columnPositions[slotColumn], y=15+slotRaw+1, text=" + ", func=rSelect, slotType="robotSlot", active = true, cFore = 0xFFFFFF, cBack = 0x777777})
- drawButtons()
- for name1,type1 in pairs(namesAndAddresses) do
- if robotName == tostring(name1) and rAddress == type1 then
- result = true
- break
- else
- result = false
- end
- end
- if not result then
- file1 = io.open("addresses", "a")
- words = robotName.."="..rAddress
- for name2,type2 in words:gmatch("(%w+)=(%w+%-%w+%-%w+%-%w+%-%w+)") do namesAndAddresses[name2]=type2 end
- file1:write(robotName.."="..rAddress.."/robot")
- file1:close()
- --print(rAddress, robotName)
- --activatorSearch = false
- end
- break
- end
- end
- end
- function exit()
- term.clear()
- os.exit()
- end
- function moving(direction)
- if rAddress ~= nil then
- --if modem.send(rAddress, 346, direction, "1") then
- --print("true")
- --else
- --print("false")
- --end
- modem.send(rAddress, 346, direction, range)
- end
- local _,_,_,_,_,message,message2 = event.pull(25, "modem")
- if message ~= nil then
- analyzes = serialization.unserialize(message)
- for kTextures,vTextures in pairs(textures) do
- for i = 1, #analyzes do
- if analyzes[i] == vTextures[1] then
- analyzes[i] = vTextures[2]
- end
- end
- end
- if message2 ~= nil then
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- gpu.set(76, 11, " ")
- rFacing = message2
- gpu.set(76, 11, rFacing)
- end
- drawTexture()
- end
- end
- function analyze()
- if rAddress ~= nil then
- modem.send(rAddress, 346, "analyze", rSide)
- end
- local _,_,_,_,_,message = event.pull(1.5, "modem")
- gpu.set(50, 30, message)
- end
- function robotSwing()
- if rAddress ~= nil then
- modem.send(rAddress, 346, "swing", rSide)
- end
- local _,_,_,_,_,message = event.pull(1.5, "modem")
- if message == "Wrong instrument" then
- gpu.set(30, 40, "Wrong instrument")
- end
- end
- function robotPlace()
- if rAddress ~= nil then
- modem.send(rAddress, 346, "place", rSide)
- end
- local _,_,_,_,_,message = event.pull(1.5, "modem")
- if message == "Can't place this block" then
- gpu.set(30, 40, "Can't place this block")
- end
- end
- function rRecieve()
- file2 = io.open("oresAndBlocks", "a")
- modem.send(rAddress, 346, "recieve")
- local _,_,_,_,_,message = event.pull("modem")
- oresAndBlocks = serialization.unserialize(message)
- for i = 1, #oresAndBlocks do
- file2:write(oresAndBlocks[i].."\n")
- end
- file2:close()
- end
- function rConnect()
- count = 1
- for name3,type3 in pairs(namesAndAddresses) do
- table.insert(buttons, {x=1, y=5+count, text=tostring(name3), rType=type3, active=true, cFore = 0xFFFFFF, cBack = 0x000000})
- count = count+1
- end
- count = 1
- drawButtons()
- end
- function robotUse()
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- if rAddress ~= nil then
- modem.send(rAddress, 346, "use", rSide)
- end
- local _,_,_,_,_,message = event.pull(1.5, "modem")
- if messageLength ~= nil then
- for i = 0, messageLength do
- gpu.set(57+i, 33, " ")
- end
- end
- if message ~= nil then
- gpu.set(57, 33, message)
- messageLength = unicode.wlen(message)
- else
- gpu.set(57, 33, "false")
- messageLength = 5
- end
- end
- function rSelect(slotNumber, slotType)
- if slotType == nil then
- modem.send(rAddress, 346, "inventory", "rSelect", tostring(slotNumber))
- for i = buttonsCount, #buttons do
- if buttons[i].slot == newSlotNumber then
- buttons[i].cFore = 0xFFFFFF
- end
- end
- newSlotNumber = slotNumber
- drawButtons()
- elseif slotType == "robotSlot" then
- modem.send(rAddress, 346, "inventory", "internal", tostring(slotNumber))
- local _,_,_,_,_,message = event.pull(0.8, "modem")
- gpu.fill(15, 40, 45, 2, " ")
- if message ~= "Slot is empty" then
- itemsInSlot = {}
- for nSlot,vSlot in message:gmatch("(%w+)=(%w*%s*%w*%s*%w*%s*%w*%s*%w*)") do itemsInSlot[nSlot]=vSlot end
- --for kInv,vInv in pairs(itemsInSlot) do print(kInv,vInv) end
- gpu.set(15, 40, itemsInSlot.label)
- gpu.set(15, 41, itemsInSlot.size)
- gpu.set(17, 41, "/")
- gpu.set(18, 41, itemsInSlot.maxSize)
- end
- else
- gpu.set(15, 40, "Slot is empty")
- end
- end
- function rGenerator()
- if gCount == nil then
- modem.send(rAddress, 346, "generator", "cnt")
- else
- modem.send(rAddress, 346, "generator", gAct, gCount)
- end
- --gAct = nil
- --gCount = nil
- local _,_,_,_,_,message = event.pull(0.5, "modem")
- if gAct == nil then
- gpu.set(157, 16, " ")
- gpu.set(157, 16, message)
- elseif gAct == "rmv" then
- gpu.set(151, 21, message)
- elseif gAct == "inst" then
- gpu.set(151, 19, message)
- end
- gAct = nil
- gCount = nil
- end
- function gInsert()
- gpu.set(151, 19, " ")
- term.setCursor(151, 19)
- gCount = io.read()
- gAct = "inst"
- rGenerator()
- end
- function gRemove()
- gpu.set(151, 21, " ")
- term.setCursor(151, 21)
- gCount = io.read()
- gAct = "rmv"
- rGenerator()
- end
- function rEnergy()
- modem.send(rAddress, 346, "energy")
- local _,_,_,_,_,message = event.pull(0.5, "modem")
- rNrg = math.ceil(tonumber(message))
- gpu.set(151, 23, tostring(rNrg))
- end
- function drawTexture()
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- for i1 = 1, #areas do
- gpu.fill(areas[i1][1], areas[i1][2], 16, 8, " ")
- end
- for i = 1, #analyzes do
- if type(analyzes[i]) == "table" then
- for kDrawTexture,vDrawTexture in pairs(analyzes[i]) do
- gpu.setForeground(vDrawTexture.fPixel)
- gpu.setBackground(vDrawTexture.bPixel)
- gpu.set(vDrawTexture.xPixel+areas[i][1]-1, vDrawTexture.yPixel+areas[i][2]-1, filler)
- end
- elseif type(analyzes[i]) == "string" then
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- gpu.set(areas[i][1], areas[i][2], analyzes[i]:match("%w*%:*"))
- gpu.set(areas[i][1], areas[i][2]+1, analyzes[i]:match("%:(%w*)"))
- gpu.set(areas[i][1], areas[i][2]+2, analyzes[i]:match("%s(%w*)"))
- end
- end
- end
- function chooseSide(sideValue, xButton, yButton)
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- rSide = tostring(sideValue)
- for i = 1, 3 do
- gpu.set(134, 11+i-1, " ")
- end
- gpu.set(xButton-1, yButton, ">")
- end
- function robotRange(rangeLimit, xArea, yArea)
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- if newRange ~= nil then
- for i = 1, unicode.wlen(newRange) do
- gpu.set(xArea+i-1, yArea+1, " ")
- end
- end
- term.setCursor(xArea, yArea+1)
- newRange = io.read()
- if tonumber(newRange) > rangeLimit then
- newRange = tostring(rangeLimit)
- end
- gpu.set(xArea, yArea+1, " ")
- gpu.set(xArea, yArea+1, newRange)
- range = newRange
- end
- function rangeReset()
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- gpu.set(146, 4, " ")
- range = "1"
- gpu.set(146, 4, range)
- end
- function rEquip()
- modem.send(rAddress, 346, "equip")
- local _,_,_,_,_,message = event.pull(2, "modem")
- gpu.set(139, 27, " ")
- gpu.set(139, 27, message)
- end
- buttons = {{x=4, y=4, text="Authorize", func=authorization, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=160, y=1, text="X", func=exit, active=true, cFore = 0x00FFFF, cBack = 0xFF0000},
- {x=155, y=3, text=" E ", direction="up", func=moving, active=true, cFore = 0xFFFFFF, cBack = 0x333333},
- {x=155, y=7, text=" Q ", direction="down", func=moving, active=true, cFore = 0xFFFFFF, cBack = 0x333333},
- {x=148, y=3, text=" W ", direction="forward", func=moving, active=true, cFore = 0xFFFFFF, cBack = 0x333333},
- {x=148, y=7, text=" S ", direction="back", func=moving, active=true, cFore = 0xFFFFFF, cBack = 0x333333},
- {x=145, y=5, text=" A ", direction="left", func=moving, active=true, cFore = 0xFFFFFF, cBack = 0x333333},
- {x=151, y=5, text=" D ", direction="right", func=moving, active=true, cFore = 0xFFFFFF, cBack = 0x333333},
- {x=155, y=5, text=" R ", direction="around", func=moving, active=true, cFore = 0xFFFFFF, cBack = 0x333333},
- {x=145, y=9, text="Сканировать", func=analyze, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=145, y=13, text="Swing", func=robotSwing, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=145, y=11, text="Place", func=robotPlace, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=145, y=25, text="Recieve", func=rRecieve, active=false, cFore = 0xFFFFFF, cBack = 0x000000}, --кнопка нужна для того, чтобы получить данные о блоках, которые тот уже собрал (при первом запуске программы)
- {x=145, y=30, text="Connect", func=rConnect, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=151, y=13, text="Use", func=robotUse, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=151, y=16, text="Count", func=rGenerator, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=151, y=18, text="Insert", func=gInsert, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=151, y=20, text="Remove", func=gRemove, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=151, y=22, text="Energy", func=rEnergy, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=135, y=11, text="Top", func=chooseSide, sideOfRobot=1, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=135, y=12, text="Front", func=chooseSide, sideOfRobot=3, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=135, y=13, text="Bottom", func=chooseSide, sideOfRobot=0, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=146, y=3, text="+", func=robotRange, limitRange=10, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=139, y=3, text="Reset", func=rangeReset, active=true, cFore = 0xFFFFFF, cBack = 0x000000},
- {x=145, y=27, text="Equip", func=rEquip, active=true, cFore = 0xFFFFFF, cBack = 0x000000}}
- function drawButtons()
- for k,v in pairs(buttons) do
- if v.active == true then
- gpu.setForeground(v.cFore)
- gpu.setBackground(v.cBack)
- gpu.set(v.x, v.y, v.text)
- end
- end
- gpu.setForeground(0xFFFFFF)
- gpu.setBackground(0x000000)
- end
- function search()
- while activatorSearch do
- local _,_,x,y = event.pull("touch")
- for k1,v1 in pairs(buttons) do
- if (x >= v1.x and x < (v1.x+unicode.len(v1.text))) and y == v1.y and v1.active then
- if v1.direction ~= nil and v1.rType == nil then
- v1.func(v1.direction)
- elseif v1.direction == nil and v1.rType ~= nil then
- rAddress = v1.rType
- print(rAddress)
- elseif v1.slot ~= nil then
- v1.cFore = 0x00FB00
- drawButtons()
- newSlot = v1.slot
- v1.func(v1.slot)
- elseif v1.slotType ~= nil then
- v1.func(newSlot, v1.slotType)
- elseif v1.sideOfRobot ~= nil then
- v1.func(v1.sideOfRobot, v1.x, v1.y)
- elseif v1.limitRange ~= nil then
- v1.func(v1.limitRange, v1.x, v1.y)
- else
- v1.func()
- end
- end
- end
- end
- end
- buttonsCount = #buttons
- print(buttonsCount)
- drawButtons()
- search()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement