Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --ORIENTACIONS: 1 = N, 2 = E, 3 = s, 4 = W
- Turtle = {
- IDTurtle = nil,
- IDServer = nil,
- PosInici = nil,
- PosActual = nil,
- Orientacio = 1,
- moventse = false,
- Inicialitzat = false,
- maxCombustible = 100000,
- nivelCombustible = 0,
- percentCombustible = 0,
- modoPanico = false
- }
- Config = {
- timeout = 25,
- serverName = "ServidorTurtlesVic"
- }
- function main()
- rednet.open("right")
- Turtle:inicialitzar()
- while true do
- sleep(0)
- if not Turtle.moventse then
- Turtle:checkCombu()
- end
- Turtle:print()
- local testVec = vector.new(753, 76, -3);
- Turtle:MoverseA(testVec)
- Turtle:getGPS()
- end
- end
- function Turtle:print()
- if self.Inicialitzat then
- term.clear()
- if self.modoPanico then
- print("/!\\ Atencio Mode Panic Actiat /!\\ \n")
- print("La Turtle tornarć83„A1 a la seva posicić83„B3 d'inici. \n")
- print("El sistema quedarć83„A1 aturat fins que no es completi la accio de respostatge.\n")
- end
- print(
- "Turtle Activa, posicio: " .. self.PosActual.x .. " x, ".. self.PosActual.y .. " y, " .. self.PosActual.z .. " z. Nivell de combustible: " .. self.percentCombustible .. "%\n"
- )
- print()
- print("Orientacić83„B3n de la Turtle: "..self.Orientacio)
- end
- end
- function Turtle:inicialitzar()
- self:getID()
- print("ID de turtle: ".. self.IDTurtle .. " asignada por el servidor: " .. self.IDServer)
- print("Obtenint senyal GPS.....")
- self:getGPS()
- if self.PosActual == "ERROR" then
- return false
- end
- print("Senyal GPS trobada, la posicić83„B3 actual ć83„A9s: " .. self.PosActual.x .. " x, ".. self.PosActual.y .. " y, " .. self.PosActual.z .. " z")
- self.Inicialitzat = true
- return true
- end
- function Turtle:getGPS()
- local timeout = tonumber(Config.timeout)
- local vectorA = vector.new(gps.locate(timeout))
- if not vectorA.x then
- print(" /!\\ Error al intentar aconseguir la posicić83„B3 GPS /!\\ ")
- self.PosActual = "ERROR"
- else
- self.PosActual = vectorA
- end
- return true
- end
- --Consigue la id de cliente y la del servidor para la turtle
- function Turtle:getID()
- while ( not(self.IDServer)) do
- rednet.broadcast("GetIDTurtleVic")
- local senderId, message = rednet.receive(Config.timeout)
- if message and string.find(message, "IDNEW") then
- args = {}
- for argument in message:gmatch("%w+") do table.insert(args, argument) end
- self.IDServer = senderId
- self.IDTurtle = args[2]
- end
- end
- return true
- end
- --Comprovador de combustible y gestor de llenado
- function Turtle:checkCombu()
- --Obtenim la capacitat de combustible de la turtle per al comprovador
- local llenando = false
- local slotCombu = self.getSlotCombustible(self)
- --!NOMć83?0°S DISPONIBLE A COMPUTER 1.6 self.maxCombustible = turtle.getFuelLimit()
- self.nivelCombustible = turtle.getFuelLevel()
- self.percentCombustible = (self.nivelCombustible / self.maxCombustible)*100
- if llenando and self.percentCombustible >= 95 then
- llenando = false
- slotCombu = self.getSlotCombustible(self)
- end
- if llenando or self.percentCombustible <= 60 then
- llenando = true
- turtle.select(slotCombu)
- turtle.refuel(1)
- end
- end
- --Comprueba en que slot tiene combustible y si no tiene se pone en modo pć83„A1nico
- function Turtle:getSlotCombustible()
- for i = 1, 16 do
- turtle.select(i)
- if turtle.refuel(0) then
- self.modoPanico = false
- return i
- end
- end
- if Turtle.percentCombustible <= 10 then
- self.modoPanico = true
- else
- self.modoPanico = false
- end
- return 1
- end
- --Zona Movimiento
- function Turtle:MoverseA(posicion)
- if self.modoPanico then
- -- posicion = self.PosInici
- end
- local vectorDesp = self.PosActual - posicion
- while not(vectorDesp.x == 0) or not(vectorDesp.z == 0) or not(vectorDesp.y == 0) do
- sleep(0)
- self.moventse = true
- vectorDesp = self.PosActual - posicion
- if not(vectorDesp.x == 0) then
- if vectorDesp.x > 0 then
- Turtle:girar(2)
- else
- Turtle:girar(4)
- end
- -- while not Turtle.PosActual.x == vectorDesp.x do
- turtle.forward()
- self:getGPS()
- --end
- elseif not(vectorDesp.z == 0) then
- if vectorDesp.z > 0 then
- Turtle:girar(3)
- else
- Turtle:girar(1)
- end
- turtle.forward()
- self:getGPS()
- elseif not(vectorDesp.y == 0) then
- if vectorDesp.y <= 0 then
- turtle.up()
- else
- turtle.down()
- end
- self:getGPS()
- end
- end--End while
- self.moventse= false
- return true
- end
- function Turtle:girar ( orientacio )
- if orientacio == self.Orientacio then
- return true
- end
- local girs = orientacio - self.Orientacio
- if(girs < 0) then
- girs = 4 + girs
- for i = 1, girs, 1 do
- turtle.turnRight()
- end
- else
- for i=1, girs, 1 do
- turtle.turnLeft()
- end
- end
- self.Orientacio = orientacio
- return true
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement