Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require('component')
- local serialization = require('serialization')
- local term = require('term')
- local event = require('event')
- local r = require('robot')
- local inv = component.inventory_controller
- local tunnel = component.tunnel
- local datafile = 'data.txt'
- local function saveTbl(tbl, fl) file = io.open(fl, 'w') file:write(serialization.serialize(tbl)) file:close() end
- local function loadTbl(fl) file = io.open(fl, 'r') if not file then file = io.open(fl, 'w') file:write('{}') return {} else return serialization.unserialize(file:read('*a')) end file:close() end
- locr={}
- loc = {["x"]=0,["y"]=0,["z"]=0}
- -- навигация
- function goToHome() r.turnLeft()
- if loc["z"] > 0 then for i=1,loc["z"] do r.down() end elseif loc["z"]<0 then loc["z"]=loc["z"]*-1 for i=1,loc["z"] do r.up() end end
- if loc["y"] > 0 then for i=1,loc["y"] do r.forward() end end r.turnLeft()
- if loc["x"] > 0 then for i=1,loc["x"] do r.forward() end end r.turnAround()
- loc["z"]=0 loc["y"]=0 loc["x"]=0
- end
- function goTo(arr) x=loc["x"]-arr["x"] y=loc["y"]-arr["y"] z=loc["z"]-arr["z"]
- if x < 0 then x=x*-1 for i=1,x do r.forward() loc["x"]=loc["x"]+1 end elseif x> 0 then for i=1,x do loc["x"]=loc["x"]-1 r.back() end end r.turnRight()
- if y < 0 then y=y*-1 for i=1,y do r.forward() loc["y"]=loc["y"]+1 end elseif y > 0 then for i=1,y do loc["y"]=loc["y"]-1 r.back() end end
- if z < 0 then z=z*-1 for i=1,z do r.up() loc["z"]=loc["z"]+1 end elseif z > 0 then for i=1,z do loc["z"]=loc["z"]-1 r.down() end end r.turnLeft()
- end
- --функция добавления сундука в базу
- local function addChest()
- local data = loadTbl(datafile)
- term.clear()
- print('Внимание координаты вводятся относительно робота!')
- print('Введите Х')
- local x = tonumber(io.read())
- print('Введите Y')
- local y = tonumber(io.read())
- print('Введите Z')
- local z = tonumber(io.read())
- data.chests[#data.chests+1] = {["x"]=x,["y"]=y,["z"]=z}
- saveTbl(data,datafile)
- end
- -- функция подсчета вещей
- function getItems()
- local data = loadTbl(datafile)
- data.items = {}
- for i=1,#data.chests do
- goTo(data.chests[i])
- for i=1,inv.getInventorySize(3) do
- item = inv.getStackInSlot(3,i)
- if item ~= nil and data.items[item.name] == nil then
- data.items[item.name] = item.size
- elseif item~= nil and data.items[item.name] ~= nil then
- data.items[item.name] = data.items[item.name] + item.size
- end
- end
- end
- saveTbl(data,datafile)
- return data.items
- end
- --главный цикл
- while true do
- local evnt = {event.pull(0)}
- if evnt[1] == 'key_up' then
- if tonumber(evnt[4]) == 49 then
- addChest()
- print('Сундук добавлен!')
- os.sleep(2)
- term.clear()
- end
- elseif evnt[1] == 'modem_message' then
- message = serialization.unserialize(evnt[6])
- if message.command == 'getAllStorage' then
- print(message.command)
- tunnel.send(serialization.serialize(getItems()))
- goToHome()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement