Advertisement
Andronio12

RoboLib v2.0

Oct 28th, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.15 KB | None | 0 0
  1. -- RoboLib by Andronio
  2. local cmp = require("component")
  3. local comp = require("computer")
  4. local r = require("robot")
  5.  
  6. local rlib = {}
  7. local path,x,y,z,side,svPath = "",0,0,0,1,false
  8. function rlib.report(msg)
  9.   print(msg)
  10.   if cmp.isAvailable('tunnel') then
  11.     cmp.tunnel.send(msg)
  12.   end
  13. end
  14. function rlib.CL(bool)
  15.   if rlib.cc('chunkloader') then
  16.     cmp.chunkloader.setActive(bool)
  17.   end
  18. end
  19. function rlib.fw()
  20.   while r.detect() do
  21.     r.swing()
  22.   end
  23.   if r.forward() then
  24.     if svPath then
  25.       path = path.."F"
  26.     end
  27.   else
  28.     rlib.fw()
  29.   end
  30.   if side == 1 then
  31.     y = y + 1
  32.   elseif side == 2 then
  33.     x = x + 1
  34.   elseif side == 3 then
  35.     y = y - 1
  36.   elseif side == 4 then
  37.     x = x - 1
  38.   else
  39.     while true do
  40.       rlib.report("E1: Unexpected side("..side.."). Stucked at "..x.." "..y.." "..z)
  41.       comp.beep(1000,2)
  42.       os.sleep(13)
  43.     end
  44.   end
  45. end
  46. function rlib.bw()
  47.   if not r.back() then
  48.     r.turnAround()
  49.     while r.detect() do
  50.       r.swing()
  51.     end
  52.     r.turnAround()
  53.     rlib.bw()
  54.   end
  55.   if svPath then
  56.     path = path.."B"
  57.   end
  58.   if side == 1 then
  59.     y = y - 1
  60.   elseif side == 2 then
  61.     x = x - 1
  62.   elseif side == 3 then
  63.     y = y + 1
  64.   elseif side == 4 then
  65.     x = x + 1
  66.   else
  67.     while true do
  68.       rlib.report("E1: Unexpected side("..side.."). Stucked at "..x.." "..y.." "..z)
  69.       comp.beep(1000,2)
  70.       os.sleep(13)
  71.     end
  72.   end
  73. end
  74. function rlib.l()
  75.   r.turnLeft()
  76.   if side == 1 then
  77.     side = 4
  78.   else
  79.     side = side - 1
  80.   end
  81.   if svPath then
  82.     path = path.."L"
  83.   end
  84. end
  85. function rlib.r()
  86.   r.turnRight()
  87.   if side == 4 then
  88.     side = 1
  89.   else
  90.     side = side + 1
  91.   end
  92.   if svPath then
  93.     path = path.."R"
  94.   end
  95. end
  96. function rlib.a()
  97.   r.turnAround()
  98.   if side == 4 then
  99.     side = 2
  100.   elseif side == 3 then
  101.     side = 1
  102.   else
  103.     sid= side + 2
  104.   end
  105.   if svPath then
  106.     path = path.."A"
  107.   end
  108. end
  109. function rlib.u()
  110.   while r.detectUp() do
  111.     r.swingUp()
  112.   end
  113.   if r.up() then
  114.     if svPath then
  115.       path = path.."U"
  116.     end
  117.   else
  118.     rlib.u()
  119.   end
  120.   z = z + 1
  121. end
  122. function rlib.d()
  123.   while r.detectDown() do
  124.     r.swingDown()
  125.   end
  126.   if r.down() then
  127.     if svPath then
  128.       path = path.."D"
  129.     end
  130.   else
  131.     rlib.d()
  132.   end
  133.   z = z - 1
  134. end
  135. function rlib.resCoords()
  136.   x,y,z,side=0,0,0,1
  137. end
  138. function rlib.resPath()
  139.   path = ""
  140. end
  141. function rlib.getCoords()
  142.   return x,y,z,side
  143. end
  144. function rlib.savePath(bool)
  145.   svPath = bool
  146. end
  147. function rlib.getPath()
  148.   if svPath then
  149.     return path
  150.   else
  151.     return "No path saved"
  152.   end
  153. end
  154. function rlib.goBackByPath()
  155.   if svPath then
  156.     svPath = false
  157.     for i=#path,1,-1 do
  158.       local nwint = string.sub(path,i,i)
  159.       if nwint == "F" then
  160.         rlib.bw()
  161.       elseif nwint == "B" then
  162.         rlib.fw()
  163.       elseif nwint == "L" then
  164.         rlib.r()
  165.       elseif nwint == "R" then
  166.         rlib.l()
  167.       elseif nwint == "U" then
  168.         rlib.d()
  169.       elseif nwint == "D" then
  170.         rlib.u()
  171.       elseif nwint == "A" then
  172.         rlib.a()
  173.       else
  174.         while true do
  175.           rlib.report("E1: I stucked at "..x.." "..y.." "..z)
  176.           comp.beep(1000,2)
  177.           os.sleep(13)
  178.         end
  179.       end
  180.     end
  181.     path = ""
  182.     svPath = true
  183.     rlib.report("I got home!")
  184.   else
  185.     while true do
  186.       rlib.report("E1: No path saved. I'm at "..x.." "..y.." "..z)
  187.       comp.beep(1000,2)
  188.       os.sleep(13)
  189.     end
  190.   end
  191. end
  192. function rlib.goBackFast()
  193.   svPath = false
  194.   if x < 0 then
  195.     if side == 1 then
  196.       rlib.r()
  197.     elseif side == 3 then
  198.       rlib.l()
  199.     elseif side == 4 then
  200.       rlib.a()
  201.     end
  202.     for i=x,0,1 do
  203.       rlib.fw()
  204.     end
  205.   else
  206.     if side == 1 then
  207.       rlib.l()
  208.     elseif side == 3 then
  209.       rlib.r()
  210.     elseif side == 2 then
  211.       rlib.a()
  212.     end
  213.     for i=x,0,-1 do
  214.       rlib.fw()
  215.     end
  216.   end
  217.   if y < 0 then
  218.     if side == 2 then
  219.       rlib.l()
  220.     elseif side == 3 then
  221.       rlib.a()
  222.     elseif side == 4 then
  223.       rlib.r()
  224.     end
  225.     for i=y,0,1 do
  226.       rlib.fw()
  227.     end
  228.   else
  229.     if side == 1 then
  230.       rlib.a()
  231.     elseif side == 4 then
  232.       rlib.l()
  233.     elseif side == 2 then
  234.       rlib.r()
  235.     end
  236.     for i=y,0,-1 do
  237.       rlib.fw()
  238.     end
  239.   end
  240.   if z < 0 then
  241.     for i=z,0,1 do
  242.       rlib.u()
  243.     end
  244.   else
  245.     for i=z,0,-1 do
  246.       rlib.d()
  247.     end
  248.   end
  249.   if side == 2 then
  250.     rlib.l()
  251.   elseif side == 3 then
  252.     rlib.a()
  253.   elseif side == 4 then
  254.     rlib.r()
  255.   end
  256.   rlib.report("I got home!")
  257.   if path then
  258.     rlib.report("The path was "..path)
  259.   end
  260.   rlib.resCoords()
  261. end
  262. function rlib.CCWD(cmpn,loc)
  263.   local type = "N/D"
  264.   local cmpnm = "N/D"
  265.   local status = "N/D"
  266.   local componlist = {
  267.     {"geolyzer","Geolyzer","Геоанализатор"},
  268.     {"keyboard","Keyboard","Клавиатура"},
  269.     {"screen","Screen","Экран"},
  270.     {"chunkloader","Chunkloader","Чанклоадер"},
  271.     {"generator","Generator","Генератор"},
  272.     {"crafting","Crafting Table","Рабочий стол"},
  273.     {"tractor_beam","Tractor Beam","Тянущий луч"},
  274.     {"inventory_controller","Inventory Controller","Контроллер инвентаря"}
  275.   }
  276.   if loc == 'En' then
  277.     if cmpn == 'geolyzer' or cmpn == 'keyboard' or cmpn == 'screen' then
  278.       type = "Component"
  279.     else
  280.       type = "Upgrade"
  281.     end
  282.   elseif loc == 'Ru' then
  283.     if cmpn == 'geolyzer' or cmpn == 'keyboard' or cmpn == 'screen' then
  284.       type = "Компонент"
  285.     else
  286.       type = "Улучшение"
  287.     end
  288.   end
  289.   for i=1,#componlist do
  290.     if cmpn == componlist[i][1] then
  291.       if loc == 'En' then
  292.         cmpnm = componlist[i][2]
  293.       elseif loc == 'Ru' then
  294.         cmpnm = componlist[i][3]
  295.       end
  296.       break
  297.     end
  298.   end
  299.   if rlib.cc(cmpn) then
  300.     if loc == 'En' then
  301.       status = 'Installed'
  302.     elseif loc == 'Ru' then
  303.       status = 'Присутствует'
  304.     end
  305.   else
  306.     if loc == 'En' then
  307.       status = 'None'
  308.     elseif loc == 'Ru' then
  309.       status = 'Отсутствует'
  310.     end
  311.   end
  312.   return type.." -> "..cmpnm..": "..status
  313. end
  314. return rlib
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement