Advertisement
Guest User

t12

a guest
Dec 11th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.71 KB | None | 0 0
  1. --ORIENTACIONS: 1 = N, 2 = E, 3 = s, 4 = W
  2. Turtle = {
  3.     IDTurtle = nil,
  4.     IDServer = nil,
  5.     PosInici = nil,
  6.     PosActual = nil,
  7.     Orientacio = 1,
  8.     moventse = false,
  9.     Inicialitzat = false,
  10.     maxCombustible = 100000,
  11.     nivelCombustible = 0,
  12.     percentCombustible = 0,
  13.     modoPanico = false
  14. }
  15.  
  16. Config = {
  17.     timeout = 25,
  18.     serverName = "ServidorTurtlesVic"
  19.  
  20. }
  21.  
  22.  
  23.  
  24. function main()
  25.     rednet.open("right")
  26.     Turtle:inicialitzar()
  27.     while true do
  28.     sleep(0)
  29.     if not Turtle.moventse then
  30.         Turtle:checkCombu()
  31.     end
  32.    
  33.     Turtle:print()
  34.     local testVec = vector.new(753, 76, -3);
  35.     Turtle:MoverseA(testVec)
  36.     Turtle:getGPS()
  37.     end
  38.    
  39. end
  40.  
  41.  
  42. function Turtle:print()
  43.    
  44.         if self.Inicialitzat then
  45.             term.clear()  
  46.             if self.modoPanico then
  47.                 print("/!\\ Atencio Mode Panic Actiat /!\\ \n")
  48.                 print("La Turtle tornarć83„A1 a la seva posicić83„B3 d'inici. \n")
  49.                 print("El sistema quedarć83„A1 aturat fins que no es completi la accio de respostatge.\n")
  50.             end
  51.             print(
  52.                 "Turtle Activa, posicio: " .. self.PosActual.x .. " x, ".. self.PosActual.y .. " y, " .. self.PosActual.z .. " z. Nivell de combustible: " .. self.percentCombustible .. "%\n"
  53.             )
  54.             print()
  55.             print("Orientacić83„B3n de la Turtle: "..self.Orientacio)
  56.         end
  57.        
  58.  
  59. end
  60.  
  61. function Turtle:inicialitzar()
  62.    
  63.    
  64.     self:getID()
  65.     print("ID de turtle: ".. self.IDTurtle .. " asignada por el servidor: " .. self.IDServer)
  66.  
  67.     print("Obtenint senyal GPS.....")
  68.     self:getGPS()
  69.     if self.PosActual == "ERROR" then
  70.         return false
  71.     end
  72.  
  73.     print("Senyal GPS trobada, la posicić83„B3 actual ć83„A9s: " .. self.PosActual.x .. " x, ".. self.PosActual.y .. " y, " .. self.PosActual.z .. " z")
  74.  
  75.    
  76.    
  77.  
  78.     self.Inicialitzat = true
  79.  
  80.  
  81.  
  82.    
  83.     return true
  84. end
  85.  
  86. function Turtle:getGPS()
  87.     local timeout = tonumber(Config.timeout)
  88.     local vectorA = vector.new(gps.locate(timeout))
  89.     if not vectorA.x then
  90.         print(" /!\\ Error al intentar aconseguir la posicić83„B3 GPS /!\\ ")
  91.         self.PosActual = "ERROR"
  92.     else
  93.         self.PosActual = vectorA
  94.     end
  95.     return true
  96. end
  97.  
  98. --Consigue la id de cliente y la del servidor para la turtle
  99. function Turtle:getID()
  100.     while ( not(self.IDServer)) do
  101.         rednet.broadcast("GetIDTurtleVic")
  102.         local senderId, message = rednet.receive(Config.timeout)
  103.         if message and string.find(message, "IDNEW") then
  104.             args = {}
  105.             for argument in message:gmatch("%w+") do table.insert(args, argument) end
  106.             self.IDServer = senderId
  107.             self.IDTurtle = args[2]
  108.         end
  109.     end
  110.     return true
  111.  
  112. end
  113.  
  114.  
  115.  
  116.  
  117. --Comprovador de combustible y gestor de llenado
  118. function Turtle:checkCombu()
  119.     --Obtenim la capacitat de combustible de la turtle per al comprovador
  120.     local llenando = false
  121.     local slotCombu = self.getSlotCombustible(self)
  122.    
  123.    
  124.        --!NOMć83?0°S DISPONIBLE A COMPUTER 1.6 self.maxCombustible = turtle.getFuelLimit()
  125.         self.nivelCombustible = turtle.getFuelLevel()
  126.         self.percentCombustible = (self.nivelCombustible / self.maxCombustible)*100
  127.  
  128.        
  129.         if llenando and self.percentCombustible >= 95 then
  130.             llenando = false
  131.             slotCombu = self.getSlotCombustible(self)
  132.         end
  133.        
  134.         if llenando or self.percentCombustible <= 60 then
  135.             llenando = true
  136.             turtle.select(slotCombu)
  137.             turtle.refuel(1)
  138.         end
  139.  
  140.  
  141. end
  142.  
  143. --Comprueba en que slot tiene combustible y si no tiene se pone en modo pć83„A1nico
  144. function Turtle:getSlotCombustible()
  145.     for i = 1, 16 do
  146.         turtle.select(i)
  147.         if turtle.refuel(0) then
  148.             self.modoPanico = false
  149.             return i
  150.         end
  151.     end
  152.    
  153.     if Turtle.percentCombustible <= 10 then
  154.       self.modoPanico = true
  155.     else
  156.       self.modoPanico = false
  157.     end
  158.     return 1
  159. end
  160.  
  161.  
  162. --Zona Movimiento
  163.  
  164. function Turtle:MoverseA(posicion)
  165.     if self.modoPanico then
  166. --        posicion = self.PosInici
  167.     end
  168.  
  169.     local vectorDesp = self.PosActual - posicion
  170.  
  171.     while not(vectorDesp.x == 0) or not(vectorDesp.z == 0) or not(vectorDesp.y == 0) do
  172.         sleep(0)
  173.         self.moventse = true
  174.        vectorDesp = self.PosActual - posicion
  175.         if not(vectorDesp.x == 0) then
  176.             if vectorDesp.x > 0 then
  177.                 Turtle:girar(2)
  178.             else
  179.                 Turtle:girar(4)
  180.             end
  181.            -- while not  Turtle.PosActual.x == vectorDesp.x do
  182.            
  183.             turtle.forward()
  184.             self:getGPS()
  185.             --end
  186.            
  187.         elseif not(vectorDesp.z == 0) then
  188.             if vectorDesp.z > 0 then
  189.                 Turtle:girar(3)
  190.             else
  191.                 Turtle:girar(1)
  192.             end
  193.             turtle.forward()
  194.             self:getGPS()    
  195.         elseif not(vectorDesp.y == 0) then
  196.             if vectorDesp.y <= 0 then
  197.                 turtle.up()
  198.             else
  199.                 turtle.down()
  200.             end
  201.             self:getGPS()
  202.         end
  203.  
  204.  
  205.     end--End while
  206.     self.moventse= false
  207.     return true
  208. end
  209.  
  210.  
  211. function Turtle:girar ( orientacio )
  212.     if orientacio == self.Orientacio then
  213.         return true
  214.     end
  215.  
  216.     local girs = orientacio - self.Orientacio
  217.  
  218.    if(girs < 0) then
  219.   girs = 4 + girs
  220.     for i = 1, girs, 1 do
  221.       turtle.turnRight()
  222.      end
  223.    else
  224.       for i=1, girs, 1 do
  225.           turtle.turnLeft()
  226.       end
  227.      end
  228.     self.Orientacio = orientacio
  229.     return true
  230. end
  231.  
  232. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement