Advertisement
PandaDoddo72Rus

Robot

Jul 13th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. local component = require('component')
  2. local serialization = require('serialization')
  3. local term = require('term')
  4. local event = require('event')
  5. local r = require('robot')
  6. local inv = component.inventory_controller
  7. local tunnel = component.tunnel
  8.  
  9. local datafile = 'data.txt'
  10.  
  11. local function saveTbl(tbl, fl) file = io.open(fl, 'w') file:write(serialization.serialize(tbl)) file:close() end
  12. 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
  13.  
  14. locr={}
  15. loc = {["x"]=0,["y"]=0,["z"]=0}
  16. -- навигация
  17. function goToHome() r.turnLeft()
  18. 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
  19. if loc["y"] > 0 then for i=1,loc["y"] do r.forward() end end r.turnLeft()
  20. if loc["x"] > 0 then for i=1,loc["x"] do r.forward() end end r.turnAround()
  21. loc["z"]=0 loc["y"]=0 loc["x"]=0
  22. end
  23.  
  24. function goTo(arr) x=loc["x"]-arr["x"] y=loc["y"]-arr["y"] z=loc["z"]-arr["z"]
  25. 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()
  26. 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
  27. 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()
  28. end
  29.  
  30. --функция добавления сундука в базу
  31.  
  32. local function addChest()
  33. local data = loadTbl(datafile)
  34. term.clear()
  35. print('Внимание координаты вводятся относительно робота!')
  36. print('Введите Х')
  37. local x = tonumber(io.read())
  38. print('Введите Y')
  39. local y = tonumber(io.read())
  40. print('Введите Z')
  41. local z = tonumber(io.read())
  42. data.chests[#data.chests+1] = {["x"]=x,["y"]=y,["z"]=z}
  43. saveTbl(data,datafile)
  44. end
  45.  
  46. -- функция подсчета вещей
  47.  
  48. function getItems()
  49. local data = loadTbl(datafile)
  50. data.items = {}
  51. for i=1,#data.chests do
  52. goTo(data.chests[i])
  53. for i=1,inv.getInventorySize(3) do
  54. item = inv.getStackInSlot(3,i)
  55. if item ~= nil and data.items[item.name] == nil then
  56. data.items[item.name] = item.size
  57. elseif item~= nil and data.items[item.name] ~= nil then
  58. data.items[item.name] = data.items[item.name] + item.size
  59. end
  60. end
  61. end
  62. saveTbl(data,datafile)
  63. return data.items
  64. end
  65.  
  66. --главный цикл
  67. while true do
  68. local evnt = {event.pull(0)}
  69. if evnt[1] == 'key_up' then
  70. if tonumber(evnt[4]) == 49 then
  71. addChest()
  72. print('Сундук добавлен!')
  73. os.sleep(2)
  74. term.clear()
  75. end
  76. elseif evnt[1] == 'modem_message' then
  77. message = serialization.unserialize(evnt[6])
  78. if message.command == 'getAllStorage' then
  79. print(message.command)
  80. tunnel.send(serialization.serialize(getItems()))
  81. goToHome()
  82. end
  83. end
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement