Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local term=require('term')
- local shell=require('shell')
- local event =require('event')
- local cmp=require('component')
- local m = cmp.tunnel
- local r =require('robot')
- local fs=require('filesystem')
- local serialization = require('serialization')
- local cr = cmp.crafting
- 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 craft()
- for i=1,3 do
- r.select(i+11)
- if itc<64 then
- if cr.craft(itc) == true then
- m.send('[craft]: Скрафтил '.. itc .. ' '.. mes) break
- else m.send('[craft:error]: ошибка при крафте '.. itc .. mes) break
- end
- else
- if cr.craft(64) == true then
- m.send('[craft]: Скрафтил '.. itc .. ' ' .. mes) break else m.send('[craft:error]: ошибка при крафте '.. itc .. mes) break end
- end
- end
- m.send('[unload]: Выгружаю вещи')
- for i=1,16 do
- r.select(i)
- r.dropUp(r.count(i))
- end
- end
- function calcCam(arr)
- cam=nil cam1=nil itc=nil sucki=nil
- if a< arr or a==arr then itc=arr cam=1
- 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
- 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
- return cam
- end
- function suckItem(sslot,cam)
- r.select(sslot)
- r.suck(cam)
- if r.count() < cam then
- sucki=cam-r.count()
- if r.suck(sucki) == false then m.send('[suck:error]: нехватает '..sucki) else m.send('[suck]: Взял '.. cam) end
- else m.send('[success]: Взял ' .. cam)
- end
- end
- 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
- ---------------
- term.clear()
- help=print('[Робот]: Ожидаю заказа! ; [H]-Для вывода помощи!')
- while true do
- ke,_,address,port,_,message = event.pull()
- if ke=='modem_message' then a=string.sub(message,1,2) a=tonumber(a) b=string.sub(message,3) end
- if ke=='key_up' then
- if port==22 then print('Вы запустили утилиту! нажмите любую клавишу!') shell.execute('ru') print('[Робот]: Ожидаю заказа! ; [H] - Для вывода помощи!') elseif port==35 then print('Help: [U]-Запуск утилиты ; [S]-Выход ;')
- elseif port==31 then term.clear() print('[Робот]: Прощай , хозяин!') os.sleep(2) term.clear() os.exit() end
- elseif ke=='modem_message' then
- m.send('start')
- mes=b
- if fs.exists('/crafts/'..mes..'.txt') == true then
- f1=loadTbl('/crafts/'..mes .. '.txt')
- mf=f1['min']
- icam=calcCam(mf)
- function sfl(mes,i,icam)
- j=tostring(i)
- per=f1[j]
- if per~='empty' then
- per2=loadTbl('/chests/'..f1[j]..'.txt')
- m.send('[goTo]: Иду к '.. per)
- goTo(per2)
- suckItem(i,icam)
- end
- end
- for i=1,3 do sfl(b,i,icam) end
- for i=5,7 do sfl(b,i,icam) end
- for i=9,11 do sfl(b,i,icam) end
- goToHome() craft()
- print('[Робот]: создано: '..b..' кол-во: '..a..' шт!')
- else m.send('[error]: Крафт не найден!') m.send('finish')
- os.sleep(3) m.send('del'..mes)
- end
- m.send('finish')
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement