Advertisement
MeXaN1cK

RobotCrafter

Mar 29th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.13 KB | None | 0 0
  1. local term=require('term')
  2. local shell=require('shell')
  3. local event =require('event')
  4. local cmp=require('component')
  5. local m = cmp.tunnel
  6. local r =require('robot')
  7. local fs=require('filesystem')
  8. local serialization = require('serialization')
  9. local cr = cmp.crafting
  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. function craft()
  17. for i=1,3 do
  18. r.select(i+11)
  19. if itc<64 then
  20.  if cr.craft(itc) == true then
  21.  m.send('[craft]: Скрафтил '.. itc .. ' '.. mes) break
  22.  else m.send('[craft:error]: ошибка при крафте '.. itc .. mes) break
  23.  end
  24.  else
  25.  if cr.craft(64) == true then
  26.  m.send('[craft]: Скрафтил '.. itc .. ' ' .. mes) break else m.send('[craft:error]: ошибка при крафте '.. itc .. mes) break end
  27.  end
  28.  end
  29.   m.send('[unload]: Выгружаю вещи')
  30.  for i=1,16 do
  31.  r.select(i)
  32.  r.dropUp(r.count(i))
  33.  end
  34.  end
  35.  
  36. function calcCam(arr)
  37. cam=nil cam1=nil itc=nil sucki=nil
  38.  if a< arr or a==arr then itc=arr cam=1
  39.  elseif a>arr and a<64 then itc=a cam1=itc/arr if cam1> math.floor(itc/arr) then cam=math.floor(cam1+1) else cam=cam1 end
  40.  elseif a>arr then itc=a if itc >64 or itc==64 then cam1=itc/arr if cam1> math.floor(itc/arr) then cam=math.floor(cam1+1) else cam=cam1 end end end
  41.  return cam
  42. end
  43.  
  44. function suckItem(sslot,cam)
  45.  r.select(sslot)
  46.  r.suck(cam)
  47.  if r.count() < cam then
  48.  sucki=cam-r.count()
  49.  if r.suck(sucki) == false then m.send('[suck:error]: нехватает '..sucki) else m.send('[suck]: Взял '.. cam) end
  50.  else m.send('[success]: Взял ' .. cam)
  51.  end
  52.  end
  53. function goToHome() r.turnLeft()
  54. 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
  55. if loc["y"] > 0 then for i=1,loc["y"] do r.forward() end end r.turnLeft()
  56. if loc["x"] > 0 then for i=1,loc["x"] do r.forward() end end r.turnAround()
  57. loc["z"]=0 loc["y"]=0 loc["x"]=0
  58. end
  59. function goTo(arr) x=loc["x"]-arr["x"] y=loc["y"]-arr["y"] z=loc["z"]-arr["z"]
  60. 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()
  61. 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
  62. 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()
  63. end
  64. ---------------
  65. term.clear()
  66. help=print('[Робот]: Ожидаю заказа! ; [H]-Для вывода помощи!')
  67. while true do
  68. ke,_,address,port,_,message = event.pull()
  69. if ke=='modem_message' then a=string.sub(message,1,2) a=tonumber(a) b=string.sub(message,3) end
  70. if ke=='key_up' then
  71. if port==22 then print('Вы запустили утилиту! нажмите любую клавишу!') shell.execute('ru') print('[Робот]: Ожидаю заказа! ; [H] - Для вывода помощи!') elseif port==35 then print('Help: [U]-Запуск утилиты ; [S]-Выход ;')
  72. elseif port==31 then term.clear() print('[Робот]: Прощай , хозяин!') os.sleep(2) term.clear() os.exit() end
  73. elseif ke=='modem_message' then
  74. m.send('start')
  75. mes=b
  76. if fs.exists('/crafts/'..mes..'.txt') == true then
  77. f1=loadTbl('/crafts/'..mes .. '.txt')
  78. mf=f1['min']
  79. icam=calcCam(mf)
  80. function sfl(mes,i,icam)
  81. j=tostring(i)
  82. per=f1[j]
  83. if per~='empty' then
  84. per2=loadTbl('/chests/'..f1[j]..'.txt')
  85. m.send('[goTo]: Иду к '.. per)
  86. goTo(per2)
  87. suckItem(i,icam)
  88. end
  89. end
  90. for i=1,3 do sfl(b,i,icam) end
  91. for i=5,7 do sfl(b,i,icam) end
  92. for i=9,11 do sfl(b,i,icam) end
  93. goToHome() craft()
  94. print('[Робот]: создано: '..b..' кол-во: '..a..' шт!')
  95. else m.send('[error]: Крафт не найден!') m.send('finish')
  96. os.sleep(3) m.send('del'..mes)
  97. end
  98. m.send('finish')
  99. end
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement