Advertisement
Guest User

Test 9 vic client

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