Advertisement
xMikhailx

OC - управление роботом (Robot) v2.0

Aug 31st, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.38 KB | None | 0 0
  1. ----------------------------------------------------
  2. --              Универсальная программа           --  
  3. --    для ручного управления роботом c помощью    --
  4. --              OpenPeripheral Glasses!           --
  5. --          проект http://computercraft.ru        --  
  6. --            2015, © xMikhail, WildOne           --
  7. --                    ver. 2.0                    --
  8. ----------------------------------------------------
  9. ------------------------ROBOT-----------------------
  10.  
  11. local event = require("event")
  12. local computer = require('computer')
  13. local r = require("robot")
  14. local tunnel = require("component").tunnel
  15. local rs = require("component").redstone
  16. local s = require("serialization")
  17. local sides = require('sides')
  18. local icontroller = require('component').inventory_controller
  19.  
  20. local t = {}
  21.  
  22. --===================================--
  23. --            Monitoring             --
  24. --===================================--
  25.  
  26. local val = {}
  27. val.inv_size = r.inventorySize()
  28.  
  29. function getEU()
  30.         return math.floor(computer.energy())
  31. end
  32.  
  33. function freeMEM()
  34.         return math.floor(computer.freeMemory()/1024)
  35. end
  36.  
  37. function freeslots()
  38.         local freeslots=0
  39.         for i=1,val.inv_size do
  40.                 if r.count(i)==0 then
  41.                         freeslots = freeslots + 1
  42.                 end
  43.         end
  44.         return freeslots
  45. end
  46.  
  47. rsstatus = "выкл"
  48.  
  49. function getEU()
  50.         return math.floor(computer.energy())
  51. end
  52.  
  53. function freeMEM()
  54.         return math.floor(computer.freeMemory()/1024)
  55. end
  56.  
  57. function t.mon()
  58.         val.name = r.name()
  59.         val.level = r.level()
  60.         val.freeslots = freeslots()
  61.         val.MAX_EU  = computer.maxEnergy()
  62.         val.TOTAL_MEM = computer.totalMemory()/1024
  63.         val.EU = getEU()
  64.         val.freeMEM = freeMEM()
  65.         val.uptime = math.floor(computer.uptime()/60)
  66.         val.rsstatus = rsstatus
  67.         tunnel.send("2 "..s.serialize(val))
  68. end
  69.  
  70. --===================================--
  71. --           INVENTORY DATA          --
  72. --===================================--
  73.  
  74. inv = {}
  75. inv.slot = {}
  76. inv.name = {}
  77. inv.amount = {}
  78.  
  79. function t.inv()
  80.         local frsl = freeslots()
  81.         inv.busyslots = val.inv_size - frsl
  82.         local c = 1
  83.         for i = 1, val.inv_size do
  84.                 local amount = r.count(i)
  85.                 if amount > 0 then
  86.                         r.select(i)
  87.                         stack = icontroller.getStackInInternalSlot(i)
  88.                         inv.slot[c] = i
  89.                         inv.name[c] = stack.label
  90.                         inv.amount[c] = amount
  91.                         c = c + 1              
  92.                 end
  93.         end
  94.         c = 1
  95.         i = 1
  96.         tunnel.send("3 "..s.serialize(inv))
  97. end
  98.  
  99. --===================================--
  100. --         MOVEMENT FUNCTIONS        --
  101. --===================================--
  102.  
  103.  
  104. function t.gf()
  105.         local c = 1
  106.         if act[2]==nil then act[2]=1 end
  107.         for c = 1,act[2] do
  108.                 r.forward()
  109.         end
  110.         tunnel.send("1 Я сдвинулся на "..act[2].." блок(ов) вперёд")
  111.         print("Я сдвинулся на "..act[2].." блок(ов) вперёд")
  112.         c = 1
  113. end
  114.  
  115. function t.gfd()
  116.         local dblocks = 0
  117.         if act[2]==nil then act[2]=1 end
  118.         for c = 1,act[2] do
  119.                 repeat
  120.                         if r.swing() then dblocks = dblocks+1 end
  121.                         if r.swingUp() then dblocks = dblocks+1 end
  122.                 until r.detect()==false or r.detectUp()==false
  123.                 r.forward()
  124.                 if r.swingUp() then dblocks = dblocks+1 end
  125.         end
  126.         tunnel.send("1 Я сдвинулся на "..act[2].." блок(ов) вперёд и вскопал "..dblocks.." блок(ов)")
  127.         print("Я сдвинулся на "..act[2].." блок(ов) вперёд и вскопал "..dblocks.." блок(ов)")
  128.         c = 1
  129. end
  130.  
  131. function t.gb()
  132.         local c = 1
  133.         if act[2]==nil then act[2]=1 end
  134.         for c = 1,act[2] do
  135.                 r.back()
  136.         end
  137.         tunnel.send("1 Я сдвинулся на "..act[2].." блок(ов) назад")
  138.         print("Я сдвинулся на "..act[2].." блок(ов) назад")
  139.         c = 1
  140. end
  141.  
  142. function t.up()
  143.         local c = 1
  144.         if act[2]==nil then act[2]=1 end
  145.         for c = 1,act[2] do
  146.                 r.up()
  147.         end
  148.         tunnel.send("1 Я поднялся на "..act[2].." блок(ов)")
  149.         print("Я поднялся на "..act[2].." блок(ов)")
  150.         c = 1
  151. end
  152.  
  153. function t.upd()
  154.         local dblocks = 0
  155.         local c = 1
  156.         if act[2]==nil then act[2]=1 end
  157.         for c = 1,act[2] do
  158.                 repeat
  159.                         if r.swingUp() then dblocks = dblocks+1 end
  160.                 until r.detectUp()==false
  161.                 r.up()
  162.                 if r.swingUp() then dblocks = dblocks+1 end
  163.         end
  164.         tunnel.send("1 Я поднялся на "..act[2].." блок(ов) и вскопал "..dblocks.." блок(ов)")
  165.         print("Я поднялся на "..act[2].." блок(ов) и вскопал "..dblocks.." блок(ов)")
  166.         c = 1
  167. end
  168.  
  169. function t.dn()
  170.         local c = 1
  171.         if act[2]==nil then act[2]=1 end
  172.         for c = 1,act[2] do
  173.                 r.down()
  174.         end
  175.         tunnel.send("1 Я спустился на "..act[2].." блок(ов)")
  176.         print("Я спустился на "..act[2].." блок(ов)")
  177.         c = 1
  178. end
  179.  
  180. function t.dnd()
  181.         local dblocks = 0
  182.         local c = 1
  183.         if act[2]==nil then act[2]=1 end
  184.         for c = 1,act[2] do
  185.                 repeat
  186.                         if r.swingDown() then dblocks = dblocks+1 end
  187.                 until r.detectDown()==false
  188.                 r.down()
  189.         end
  190.         tunnel.send("1 Я спустился на "..act[2].." блок(ов) и вскопал "..dblocks.." блок(ов)")
  191.         print("Я спустился на "..act[2].." блок(ов) и вскопал "..dblocks.." блок(ов)")
  192.         c = 1
  193. end
  194.  
  195. function t.tr()
  196.         r.turnRight()
  197.         tunnel.send("1 Я повернулся направо")
  198.         print("Я повернулся направо")
  199. end
  200.  
  201. function t.tl()
  202.         r.turnLeft()
  203.         tunnel.send("1 Я повернулся налево")
  204.         print("Я повернулся налево")
  205. end
  206.  
  207. function t.ta()
  208.         r.turnAround()
  209.         tunnel.send("1 Я повернулся на 180 градусов")
  210.         print("Я повернулся на 180 градусов")
  211. end
  212.  
  213. --===================================--
  214. --            USE FUNCTIONS          --
  215. --===================================--
  216.  
  217. function t.rs()
  218.         if rsstatus=="выкл" then
  219.                 rsstatus="вкл"
  220.                 rs.setOutput(sides.front, 15)
  221.                 rs.setOutput(sides.bottom, 15)
  222.                 rs.setOutput(sides.top, 15)
  223.                 tunnel.send("1 Редстоун включен")
  224.                 print("Редстоун включен")
  225.         else
  226.                 rsstatus="выкл"
  227.                 rs.setOutput(sides.front, 0)
  228.                 rs.setOutput(sides.bottom, 0)
  229.                 rs.setOutput(sides.top, 0)    
  230.                 tunnel.send("1 Редстоун выключен")
  231.                 print("Редстоун выключен")
  232.         end
  233. end
  234.  
  235. function t.use()
  236.         r.use()
  237.         tunnel.send("1 Я щёлкнул правой мыши перед собой")
  238.         print("Я щёлкнул правой мыши перед собой")
  239. end
  240.  
  241. function t.useup()
  242.         r.useUp()
  243.         tunnel.send("1 Я щёлкнул правой мыши над собой")
  244.         print("Я щёлкнул правой мыши над собой")
  245. end
  246.  
  247. function t.usedn()
  248.         r.useDown()
  249.         tunnel.send("1 Я щёлкнул правой мыши под собой")
  250.         print("Я щёлкнул правой мыши под собой")
  251. end
  252.  
  253. --===================================--
  254. --        INVENTORY FUNCTIONS        --
  255. --===================================--
  256.  
  257. function t.drop()
  258.         local amountItems = 0
  259.         act[2] = tonumber(act[2])
  260.     act[3] = tonumber(act[3])
  261.         if act[3] == nil then
  262.                 if act[2] == nil then
  263.                         for d=1,val.inv_size do
  264.                                 r.select(d)
  265.                                 r.drop(r.count())
  266.                         end
  267.                 tunnel.send("1 Я выбросил все вещи перед собой(или в сундук)")
  268.                 print("Я выбросил все вещи перед собой(или в сундук)")
  269.                 else
  270.                         r.select(act[2])
  271.                         r.drop(r.count())
  272.                 tunnel.send("1 Я выбросил все вещи из слота "..act[2].." перед собой(или в сундук)")
  273.                 print("Я выбросил все вещи из слота "..act[2].." перед собой(или в сундук)")
  274.                 end
  275.         else
  276.                 r.select(act[2])
  277.                 amountItems = r.count()
  278.                 r.drop(act[3])
  279.                 if act[3] > amountItems then
  280.                         tunnel.send("1 Я выбросил "..amountItems.." вещь(-ей) из слота "..act[2].." перед собой(или в сундук)")
  281.                         print("Я выбросил "..amountItems.." вещь(-ей) из слота "..act[2].." перед собой(или в сундук)")
  282.                 else
  283.                         tunnel.send("1 Я выбросил "..act[3].." вещь(-ей) из слота "..act[2].." перед собой(или в сундук)")
  284.                         print("Я выбросил "..act[3].." вещь(-ей) из слота "..act[2].." перед собой(или в сундук)")
  285.                 end
  286.         end
  287.         amountItems = 0
  288.         r.select(1)
  289. end
  290.  
  291. function t.dropup()
  292.        local amountItems = 0
  293.         act[2] = tonumber(act[2])
  294.     act[3] = tonumber(act[3])
  295.         if act[3] == nil then
  296.                 if act[2] == nil then
  297.                         for d=1,val.inv_size do
  298.                                 r.select(d)
  299.                                 r.dropUp(r.count())
  300.                         end
  301.                 tunnel.send("1 Я выбросил все вещи вверх(или в сундук)")
  302.                 print("Я выбросил все вещи вверх(или в сундук)")
  303.                 else
  304.                         r.select(act[2])
  305.                         r.dropUp(r.count())
  306.                 tunnel.send("1 Я выбросил все вещи из слота "..act[2].." вверх(или в сундук)")
  307.                 print("Я выбросил все вещи из слота "..act[2].." вверх(или в сундук)")
  308.                 end
  309.         else
  310.                 r.select(act[2])
  311.                 amountItems = r.count()
  312.                 r.dropUp(act[3])
  313.                 if act[3] > amountItems then
  314.                         tunnel.send("1 Я выбросил "..amountItems.." вещь(-ей) из слота "..act[2].." вверх(или в сундук)")
  315.                         print("Я выбросил "..amountItems.." вещь(-ей) из слота "..act[2].." вверх(или в сундук)")
  316.                 else
  317.                         tunnel.send("1 Я выбросил "..act[3].." вещь(-ей) из слота "..act[2].." вверх(или в сундук)")
  318.                         print("Я выбросил "..act[3].." вещь(-ей) из слота "..act[2].." вверх(или в сундук)")
  319.                 end
  320.         end
  321.         amountItems = 0
  322.         r.select(1)
  323. end
  324.  
  325. function t.dropdn()
  326.        local amountItems = 0
  327.         act[2] = tonumber(act[2])
  328.     act[3] = tonumber(act[3])
  329.         if act[3] == nil then
  330.                 if act[2] == nil then
  331.                         for d=1,val.inv_size do
  332.                                 r.select(d)
  333.                                 r.dropDown(r.count())
  334.                         end
  335.                 tunnel.send("1 Я выбросил все вещи вниз(или в сундук)")
  336.                 print("Я выбросил все вещи вниз(или в сундук)")
  337.                 else
  338.                         r.select(act[2])
  339.                         r.dropDown(r.count())
  340.                 tunnel.send("1 Я выбросил все вещи из слота "..act[2].." вниз(или в сундук)")
  341.                 print("Я выбросил все вещи из слота "..act[2].." вниз(или в сундук)")
  342.                 end
  343.         else
  344.                 r.select(act[2])
  345.                 amountItems = r.count()
  346.                 r.dropDown(act[3])
  347.                 if act[3] > amountItems then
  348.                         tunnel.send("1 Я выбросил "..amountItems.." вещь(-ей) из слота "..act[2].." вниз(или в сундук)")
  349.                         print("Я выбросил "..amountItems.." вещь(-ей) из слота "..act[2].." вниз(или в сундук)")
  350.                 else
  351.                         tunnel.send("1 Я выбросил "..act[3].." вещь(-ей) из слота "..act[2].." вниз(или в сундук)")
  352.                         print("Я выбросил "..act[3].." вещь(-ей) из слота "..act[2].." вниз(или в сундук)")
  353.                 end
  354.         end
  355.         amountItems = 0
  356.         r.select(1)
  357. end
  358.  
  359. function t.suck()
  360.         r.select(1)
  361.         while r.suck()==true do
  362.                 r.suck()
  363.         end
  364.         tunnel.send("1 Я забрал вещи перед собой(или из сундука)")
  365.         print("Я забрал вещи перед собой(или из сундука)")
  366. end
  367.  
  368. function t.suckup()
  369.         r.select(1)
  370.         while r.suckUp()==true do
  371.                 r.suckUp()
  372.         end
  373.         tunnel.send("1 Я забрал вещи над собой(или из сундука)")
  374.         print("Я забрал вещи над собой(или из сундука)")
  375. end
  376.  
  377. function t.suckdn()
  378.         r.select(1)
  379.         while r.suckDown()==true do
  380.                 r.suckDown()
  381.         end
  382.         tunnel.send("1 Я забрал вещи под собой(или из сундука)")
  383.         print("Я забрал вещи под собой(или из сундука)")
  384. end
  385.  
  386. function error()
  387.         tunnel.send("1 Ошибка. Неправильная команда")
  388.         print("Ошибка. Неправильная команда")
  389. end
  390.  
  391. if obr then
  392.         if event.ignore("modem_message",obr) then
  393.                 print("Слушатель удалён")
  394.         else
  395.                 print("Слушатель не удалён")
  396.         end
  397. end
  398.  
  399. function _G.obr(_,_,_,_,_,com,_)
  400.         local i = 1
  401.         act = {}
  402.         for word in string.gmatch(com, "%w+") do
  403.                 --print(word)
  404.                 act[i] = word
  405.                 i = i + 1
  406.         end
  407.         print(act[1])
  408.         print(act[2])
  409.         if t[act[1]] then t[act[1]]()
  410.         else error()
  411. end
  412. end
  413.  
  414. event.listen("modem_message",obr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement