Advertisement
Guest User

mine.lua

a guest
Aug 25th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.00 KB | None | 0 0
  1. robot = require("robot")
  2. c=require("component")
  3. m=c.modem
  4. computer = require("computer")
  5. event = require("event")
  6. me = c.upgrade_me
  7. dronaddress  =  ""
  8. dron = false
  9. dronOffset = {}
  10. rdMat = {}
  11. port = 3685
  12. _G.x=_G.x or 0
  13. _G.y=_G.y or 0
  14. _G.z=_G.z or 0
  15. args = {...}
  16.  
  17. function inputNum()
  18.   while true do
  19.     local input = io.read()
  20.     if not tonumber(input) == nil then
  21.       print("Nezadali jste číslo.")
  22.       else
  23.         return tonumber(input)
  24.     end
  25.   end
  26. end
  27.  
  28. function sendToMe()
  29.   for i = 1, robot.inventorySize() do
  30.     robot.select(i)
  31.     me.sendItems()
  32.   end
  33.   robot.select(1)
  34. end
  35.  
  36. function checkInv()
  37.     for i=1, robot.inventorySize() do
  38.       if robot.count(i) == 0 then
  39.         return true
  40.       end
  41.     end
  42.   print("V inventáři není již místo.")
  43.   if me~=nill then
  44.     return empty()
  45.     else
  46.       goBack()
  47.       return false
  48.   end
  49.  
  50. --  callDron()
  51. --  for i=1,robot.inventorySize() do
  52. --    if robot.count(i) == 0 then
  53. --      return true
  54. --    end
  55. --  end
  56.   return false
  57. end
  58.  
  59. function checkTool()
  60.   if not (robot.durability() == nil) then
  61.     local dur = robot.durability() >=0.1
  62.     if not dur then
  63.       print("Odolnost nástroje není dostatečná.")
  64.       return repair()
  65.     end
  66.     return dur
  67.     else
  68.       print("Chybí pracovní nástroj.")
  69.       repair()
  70.       return true
  71.   end
  72. end
  73.  
  74. function checkEnergy()
  75.   local ener = computer.energy()/computer.maxEnergy() >= 0.1
  76.   if not ener then
  77.     print("Nabití baterie je nedostatečné.")
  78.     recharge()
  79.     return checkEnergy()
  80.   end
  81.   return ener
  82. end
  83.  
  84. function recharge()
  85.   lx = x
  86.   ly = y
  87.   lz = z
  88.   goBack()
  89.   cFac(0)
  90.   while(computer.energy()/computer.maxEnergy() < 0.98) do
  91.     os.sleep(10)
  92.   end
  93.   goPos(minx,miny,minz)
  94.   goPos(lx,ly,lz)
  95. end
  96.  
  97. function repair()
  98.   lx = x
  99.   ly = y
  100.   lz = z
  101.   goBack()
  102.   cFac(0)
  103.   io.write("Chcete-li pokračovat, vyměňte nástroj a zadejte C, jinak zadejte cokoliv.")
  104.   c = io.read()
  105.   if(c == "C") then
  106.     goPos(minx,miny,minz)
  107.     goPos(lx,ly,lz)
  108.   else return false
  109.   end
  110. end
  111.  
  112. function empty()
  113.   lx = x
  114.   ly = y
  115.   lz = z
  116.   goBack()
  117.   cFac(0)
  118.   sendToMe()
  119.   goPos(minx,miny,minz)
  120.   goPos(lz,ly,lz)
  121. end
  122.  
  123. function check()
  124.   return checkInv() and checkTool() and checkEnergy()
  125. end
  126.  
  127. function go(len)
  128.   for i = 1, len do
  129.     while robot.detect() do
  130.       if not robot.swing() then
  131.         break
  132.       end
  133.     end
  134.     robot.forward()
  135.   end
  136. end
  137.  
  138. function cFac(face)
  139.   local f = face - facing
  140.   if f<0 then
  141.     f = f + 4
  142.   end
  143.   if f == 1 then
  144.     robot.turnLeft()
  145.     elseif f==2 then
  146.       robot.turnAround()
  147.       elseif f == 3 then
  148.         robot.turnRight()
  149.   end
  150.   facing = face  
  151. end
  152.  
  153. function goPos(tx,ty,tz)
  154.   if tx ~= x then
  155.     if tx>x then
  156.       cFac(0)
  157.       go(tx-x)
  158.       elseif tx<x then
  159.         cFac(2)
  160.         go(x-tx)
  161.     end
  162.     x=tx
  163.   end
  164.   if tz~=z then
  165.     if tz>z then
  166.       cFac(3)
  167.       go(tz-z)
  168.       elseif tz<z then
  169.         cFac(1)
  170.         go(z-tz)
  171.     end
  172.     z=tz
  173.   end
  174.   if y~=ty then
  175.     if ty<y then
  176.       for i=1,(y-ty) do
  177.         robot.swingUp()
  178.         robot.up()
  179.       end
  180.       elseif ty>y then
  181.         for i=1,(ty-y) do
  182.           robot.swingDown()
  183.           robot.down()
  184.         end
  185.     end
  186.     y=ty
  187.   end
  188. end
  189.  
  190. function checkVal()
  191.   print("Jsou vámi zadané hodnoty správné? (Y/n)")
  192.   if io.read() == "n" then
  193.     print("Chcete zadat nové hodnoty? (Y/n)")
  194.       if io.read() == "n" then
  195.         return false
  196.         else
  197.           mineCuboid()
  198.           return false
  199.       end
  200.     else
  201.       return true
  202.   end
  203. end
  204.  
  205. function mineCuboid()
  206.   print("Hodnoty zadávejte relativně k současné poloze.")
  207.   print("Zadejte vzdálenost začátku těžby.")
  208.   minx = inputNum()
  209.   print("Zadejte strop těžené oblasti.")
  210.   miny = -inputNum()
  211.   print("Zadejte levou stěnu těžené oblasti.")
  212.   minz = -inputNum()
  213.   print("Zadejte vzdálenost konce těžby.")
  214.   maxx = inputNum()
  215.   print("Zadejte hloubku těžby.")
  216.   maxy = inputNum()
  217.   print("Zadejte pravou stěnu těžené oblasti.")
  218.   maxz  = inputNum()
  219. --  return Cuboid(minx,miny,minz,maxx,maxy,maxz)
  220. --end
  221.  
  222. --function Cuboid(minx,miny,minz,maxx,many,maxz)
  223.   if checkVal() then
  224.     if not check() then
  225.       return false
  226.     end  
  227.     for i=miny,maxy do
  228.       goPos(minx,i,minz)
  229.       for j=minz+1,maxz do
  230.         if not check() then
  231.           return false
  232.         end
  233.         if x==minx then
  234.           goPos(maxx,i,j)
  235.           elseif x==maxx then
  236.             goPos(minx,i,j)
  237.             else goBack()
  238.         end
  239.       end
  240.       if x==minx and check() then
  241.         goPos(maxx,y,z)
  242.       end
  243.     end
  244.     goBack()
  245.     sendToMe()
  246.     return true
  247.     else
  248.       return false
  249.   end
  250. end
  251.  
  252.  
  253. function goBack()
  254.   if x==maxx then
  255.     goPos(x-1,y,z)
  256.   end
  257.   goPos(minx,0,0)
  258.   goPos(0,0,0)
  259.   cFac(0)
  260. end
  261.  
  262. function sendDron(cmd)
  263.   m.send(dronaddress,port,cmd)
  264. end
  265.  
  266. function dronInit()
  267.   m.open(port)
  268.   print("Spusťte drona.")
  269.   local _, _, address = event.pull(60,"modem.message")
  270.   dronaddress = address
  271.   if dronnaddress=="" or dronaddress == nil then
  272.     print("Dron nebyl detekován a nebude s ním tedy komunikováno.")
  273.     return false
  274.   end
  275.   print(dronaddress)
  276.   sendDron("drone = component.proxy(component.list('drone')())")
  277.   print("Kolik bloků je dron vpravo (je-li vlevo použijte znaménko '-')?")
  278.   dronOffset[2] = tonumber(io.read())
  279.   print("Kolik bloků je dron za robotem (le-li před ním použijte znaménko '-')?.")
  280.   dronOffset[0] = tonumber(io.read())
  281.   print("Kolim bloků je dron nad robotem (je-li pod ním použijte znaménko '-')?")
  282.   dronOffset[1] = tonumber(io.read())
  283.   print("Zadejte otočení robota číslem: S=0; J=2; V=1; Z=3.")
  284.   local f = tonumber(io.read())
  285.   for i=0,2 do
  286.     rdMat[i] = {}
  287.     for j=0,2 do
  288.       rdMat[i][j] = 0
  289.     end
  290.   end
  291.   if f == 0 then
  292.     rdMat[0][2] = -1
  293.     rdMat[1][1] = -1
  294.     rdMat[2][0] = -1
  295.   end
  296.   if f == 1 then
  297.     rdMat[0][0] = -1
  298.     rdMat[1][1] = -1
  299.     rdMat[2][2] = 1
  300.   end
  301.   if f == 2 then
  302.     rdMat[0][2] = 1
  303.     rdMat[1][1] = -1
  304.     rdMat[2][0] = 1
  305.   end
  306.   if f == 3 then
  307.     rdMat[0][0] = 1
  308.     rdMat[1][1] = -1
  309.     rdMat[2][2] = -1
  310.   end
  311.   return true
  312. end
  313.  
  314. function transform(rx,ry,rz)
  315.   dx = rx*rdMat[0][0]+ry*rdMat[0][1]+rz*rdMat[0][2]
  316.   dy = rx*rdMat[1][0]+ry*rdMat[1][1]+rz*rdMat[1][2]
  317.   dz = rx*rdMat[2][0]+ry*rdMat[2][1]+rz*rdMat[2][2]
  318.   return dx, dy, dz
  319. end
  320.  
  321. function callDron()
  322.   sendDron("drone.move("..transform(-dronOffset[0],-dronOffset[1]-1,dronOffset[2])..") while drone.getVelocity()>0 do end drone.move("..transform(x,y,z)..") while drone.getVelocity()>0 do end for i=1,drone.inventorySize() do drone.suck(0) end os.sleep(10) drone.move("..transform(-x,-y,-z)..") while drone.getVelocity()>0 do end for i=1,drone.inventorySize() do drone.select(i) drone.drop(0) end")
  323.   os.sleep(60)
  324. end
  325.  
  326. function testDron()
  327.   if dron then
  328.     sendDron("drone.move(0,1,0)")
  329.   end
  330. end
  331.  
  332. function main(args)
  333.   x=0
  334.   y=0
  335.   z=0
  336.   facing = 0
  337. --  dron = dronInit()
  338. --  for num,i in ipairs(args) do
  339. --    if tonumber(i) then
  340. --      args[num] = tonumber(i)
  341. --    end
  342. --  end
  343. --  if table.getn(args) == 6 then
  344. --    Cuboid(args[1],args[2],args[3],args[4],args[5],args[6])
  345. --  if args ~=nil then
  346. --    Cuboid(args)
  347. --  else
  348.   mineCuboid()
  349. --  end
  350. --  testDron()
  351. --  recharge()
  352. end
  353.  
  354. main(args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement