Advertisement
MeXaN1cK

Робот грузчик

Apr 18th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.38 KB | None | 0 0
  1. local cmp = require("component")
  2. local r = require("robot")
  3. local s = require("sides")
  4. local serialization=require('serialization')
  5. local event=require("event")
  6. local m = cmp.tunnel
  7. local m1=cmp.modem
  8. loc ={["x"]=0,["y"]=0,["z"]=0}
  9. local function saveTbl(tbl, fl) file = io.open(fl, 'w') file:write(serialization.serialize(tbl)) file:close() end
  10. 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
  11. function d()
  12. r.turnAround()
  13. for i=1,64 do
  14. r.select(i)
  15. r.drop()
  16. end
  17. r.select(1)
  18. r.turnAround()
  19. end
  20. function suckItem()
  21.  if a==4096 then a=math.floor(a/64) for i=1,a do r.suck() end
  22.  elseif a>64 and a<4096 then a1=math.floor(a/64) a2=a/64
  23.  if a2>a1 then a3=a-a1*64 for i=1,a1 do r.suck() end r.suck(a3) end
  24.  elseif a==64 or a<64 then r.suck(a) end
  25.  for i=1,3 do
  26.  ch()
  27.  end
  28. end
  29. function ch()
  30. amo1=0
  31. for i=1,64 do
  32. r.select(i) amo=r.count()
  33. amo1=amo1+amo
  34. end
  35. if amo1<a then amo2=a-amo1
  36.  if amo2 >64 then amo3=math.floor(amo2/64) amo4=amo2/64
  37.   if amo4>amo3 then amo5=amo2-amo3*64 for i=1,amo3 do r.suck() end r.suck(amo5) end
  38.   end end
  39. end
  40.  
  41. function goToHome() r.turnLeft()
  42. 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
  43. if loc["y"] > 0 then for i=1,loc["y"] do r.forward() end end r.turnLeft()
  44. if loc["x"] > 0 then for i=1,loc["x"] do r.forward() end end r.turnAround()
  45. loc["z"]=0 loc["y"]=0 loc["x"]=0
  46. end
  47. function goTo(arr) x=loc["x"]-arr["x"] y=loc["y"]-arr["y"] z=loc["z"]-arr["z"]
  48. 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()
  49. 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
  50. 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()
  51. end
  52. function ItoP()
  53. while true do
  54. key,_,address,port,_,message = event.pull()
  55. if key=="modem_message" then
  56. a=string.sub(message,1,4) a=tonumber(a)
  57. b=string.sub(message,5)
  58.  f=loadTbl('/chests/'..b..'.txt')
  59.  goTo(f)
  60.  suckItem()
  61.  m1.send("e51089c7-42c7-465b-9791-ccd8758480ea",1,"check")
  62.  goToHome()
  63.  d()
  64. end
  65. end
  66. end
  67. ItoP()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement