Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require ("component")
- local computer = require("computer")
- local robot = require("robot")
- local sides = require("sides")
- robodigger = {
- x = 0,
- y = 0,
- z = 0,
- f = 0,
- rested = true,
- }
- path = {}
- actions = {}
- torch_counter = 0
- torch_max = 7
- -- funzione per ruotare il robot e fargli ricordare la sua rotazione, rotazione oraria
- function sureturn (rotValue)
- if rotValue == 90 then
- robot.turnRight()
- robodigger.f = (robodigger.f + 90) %360
- elseif rotValue == -90 then
- robot.turnLeft()
- robodigger.f = (robodigger.f - 90) %360
- elseif rotValue == 180 or -180 then
- robot.turnAround()
- robodigger.f = (robodigger.f + 180) %360
- end
- end
- function raddrizza()
- if robodigger.f == 90 then
- sureturn(-90)
- elseif robodigger.f == 180 then
- sureturn(180)
- elseif robodigger.f == 270 then
- sureturn(90)
- end
- end
- -- funzione per assicurare un movimento certo, e con un'aggiornamento delle coordinate corretto
- -- direction dovrebbe seguire sides.api
- -- movimenti assolti rispetto alla partenza
- -- se m è TRUE, allora il robot mina quello che trova davanti e quello sopra.
- -- i se TRUE ignora la stanchezza e specifica che sta tornando a casa
- --[[function returnmove (side) --sto controllando bene se mi conviene farla
- moved = false
- while moved == false do
- if side = sides.top then
- if robot.up() == true then
- robodigger.y = robodigger.y + 1
- moved = true
- else os.sleep(0.5)
- end
- else if side == sides.down then
- if robot.down() == true then
- robodigger.y = robodigger.y -1
- moved = true
- else os.sleep(0.5)
- end]]
- function suremove (side, m, i)
- moved = false
- tries = 3
- while moved == false do
- if side == sides.top then
- if m == true and robot.detectUp() == true then
- if robot.swingUp() == false then
- switchtodiamond(side)
- end
- end
- if robot.up() == true then
- robodigger.y = robodigger.y + 1
- moved = true
- else tries = tries -1 os.sleep(0.5)
- end
- elseif side == sides.down then
- if m == true and robot.detectDown() == true then
- if robot.swingDown() == false then
- switchtodiamond(side)
- end
- end
- if robot.down() == true then
- robodigger.y = robodigger.y - 1
- moved = true
- else tries = tries -1 os.sleep(0.5)
- end
- else
- if side == sides.front then
- if robodigger.f == 90 then
- sureturn(-90)
- elseif robodigger.f == 270 then
- sureturn(90)
- elseif robodigger.f == 180 then
- sureturn(180)
- end
- if m == true and robot.detect() == true then
- if robot.swing() == false then
- switchtodiamond(side)
- end
- end
- if robot.forward() == true then
- robodigger.x = robodigger.x +1
- moved = true
- component.chat.say("mosso")
- else tries = tries -1 os.sleep(0.5)
- component.chat.say("non mosso")
- end
- end
- if side == sides.right then
- if robodigger.f == 180 then
- sureturn(-90)
- elseif robodigger.f == 0 then
- sureturn(90)
- elseif robodigger.f == 270 then
- sureturn(180)
- end
- if m == true and robot.detect() == true then
- if robot.swing() == false then
- switchtodiamond(side)
- end
- end
- if robot.forward() == true then
- robodigger.z = robodigger.z +1
- moved = true
- else tries = tries -1 os.sleep(0.5)
- end
- end
- if side == sides.back then
- if robodigger.f == 270 then
- sureturn(-90)
- elseif robodigger.f == 90 then
- sureturn(90)
- elseif robodigger.f == 0 then
- sureturn(180)
- end
- if m == true and robot.detect() == true then
- if robot.swing() == false then
- switchtodiamond(side)
- end
- end
- if robot.forward() == true then
- robodigger.x = robodigger.x - 1
- moved = true
- else tries = tries -1 os.sleep(0.5)
- end
- end
- if side == sides.left then
- if robodigger.f == 0 then
- sureturn(-90)
- elseif robodigger.f == 180 then
- sureturn(90)
- elseif robodigger.f == 90 then
- sureturn(180)
- end
- if m == true and robot.detect() == true then
- if robot.swing() == false then
- switchtodiamond(side)
- end
- end
- if robot.forward() == true then
- robodigger.z = robodigger.z - 1
- moved = true
- else tries = tries -1 os.sleep(0.5)
- end
- end
- end
- if i == false then
- checkstats()
- if tries == 0 or robodigger.rested == false then
- component.chat.say("REEEEEE")
- return false end
- end
- end
- return true end
- -- funzione go to, per decidere in che coordinate rispetto all'inizio andare. r sta per "relativo", "m" passa "mining" a suremove
- -- non_pb controlla se pathbuilder deve essere chiamato
- function gogoto (tox, toy, toz, r, m, non_pb)
- if r == true then
- tox = tox + robodigger.x
- toy = toy + robodigger.y
- toz = toz + robodigger.z
- end
- while robodigger.x ~= tox do
- if robodigger.x < tox then
- if suremove(sides.forward, m, false) == false then return false end
- else
- if suremove(sides.back, m, false) == false then return false end
- end
- end
- while robodigger.z ~= toz do
- if robodigger.z < toz then
- if suremove(sides.right, m, false) == false then return false end
- else
- if suremove(sides.left, m, false) == false then return false end
- end
- end
- while robodigger.y ~= toy do
- if robodigger.y < toy then
- if suremove(sides.up, m, false) == false then return false end
- else
- if suremove(sides.down, m, false) == false then return false end
- end
- end
- if non_pb == false then --questa parte non compare mai in invgogoto, che è intesa per tornare indietro nel pathfinding
- pathbuilder()
- end
- end
- function invgogoto (tox, toy, toz) --molto diversa da gogoto, non usabile per muoversi in avanti
- while robodigger.y ~= toy do
- if robodigger.y < toy then
- if suremove(sides.up, m, true) == false then return false end
- else
- if suremove(sides.down, m, true) == false then return false end
- end
- end
- while robodigger.z ~= toz do
- if robodigger.z < toz then
- if suremove(sides.right, m, true) == false then return false end
- else
- if suremove(sides.left, m, true) == false then return false end
- end
- end
- while robodigger.x ~= tox do
- if robodigger.x < tox then
- if suremove(sides.forward, m, true) == false then return false end
- else
- if suremove(sides.back, m, true) == false then return false end
- end
- end
- end
- -- sides obbidisce a sides.api per dare una direzione alla branch. "indietro" chiede se deve tornare alle coordinate di partenza
- function minebranch (lenght, height, side, indietro)
- branchstart = {
- x = robodigger.x,
- y = robodigger.y,
- z = robodigger.z
- }
- if side == sides.forward then
- torch_counter = 4
- for i=1,lenght do
- if gogoto(1, height, 0, true, true, true) == false then return false end
- gogoto(0, -height, 0, true, false, true) --ritorno a terra senza scavare
- torch_counter = torch_counter +1
- if torch_counter == torch_max then
- robot.turnRight()
- robot.swing()
- robot.place()
- torch_counter = 0
- robot.turnLeft()
- end
- end
- if indietro == true then
- gogoto(branchstart.x, branchstart.y, branchstart.z, false, true, true)
- else
- pathbuilder()
- end
- elseif side == sides.back then
- torch_counter = 4
- for i=1,lenght do
- if gogoto(-1, height, 0, true, true, true) == false then return false end
- gogoto(0, -height, 0, true, false, true) --ritorno a terra senza scavare
- torch_counter = torch_counter +1
- if torch_counter == torch_max then
- robot.turnRight()
- robot.swing()
- robot.place()
- torch_counter = 0
- robot.turnLeft()
- end
- end
- if indietro == true then
- gogoto(branchstart.x, branchstart.y, branchstart.z, false, true, true)
- else
- pathbuilder()
- end
- elseif side == sides.right then
- torch_counter = 4
- for i=1,lenght do
- if gogoto(0, height, 1, true, true, true) == false then return false end
- gogoto(0, -height, 0, true, false, true) --ritorno a terra senza scavare
- torch_counter = torch_counter +1
- if torch_counter == torch_max then
- robot.turnRight()
- robot.swing()
- robot.place()
- torch_counter = 0
- robot.turnLeft()
- end
- end
- if indietro == true then
- gogoto(branchstart.x, branchstart.y, branchstart.z, false, true, true)
- else
- pathbuilder()
- end
- elseif side == sides.left then
- torch_counter = 4
- for i=1,lenght do
- if gogoto(0, height, -1, true, true, true) == false then return false end
- gogoto(0, -height, 0, true, false, true) --ritorno a terra senza scavare
- component.chat.say("scendo!")
- torch_counter = torch_counter +1
- if torch_counter == torch_max then
- robot.turnRight()
- robot.swing()
- robot.place()
- torch_counter = 0
- robot.turnLeft()
- end
- end
- if indietro == true then
- gogoto(branchstart.x, branchstart.y, branchstart.z, false, true, true)
- else
- pathbuilder()
- end
- end
- end
- function minevolume (depth, height, width, side)
- start = {
- x = robodigger.x,
- y = robodigger.y,
- z = robodigger.z
- }
- minebranch(1, height, side, false)
- for i=1, width-1, 2 do
- minebranch(depth-1, height, side, false)
- if side == sides.forward then
- minebranch(1, height, sides.left, false)
- minebranch(depth-1, height, sides.back, false)
- minebranch(1, height, sides.left, false)
- elseif side == sides.back then
- minebranch(1, height, sides.right, false)
- minebranch(depth-1, height, sides.forward, false)
- minebranch(1, height, sides.right, false)
- elseif side == sides.right then
- minebranch(1, height, sides.forward, false)
- minebranch(depth-1, height, sides.left, false)
- minebranch(1, height, sides.forward, false)
- elseif side == sides.left then
- minebranch(1, height, sides.back, false)
- minebranch(depth-1, height, sides.right, false)
- minebranch(1, height, sides.back, false)
- end
- if i == width -1 and width % 2 ~= 0 then
- minebranch(depth, height, side, false)
- end
- end
- gogoto(start.x, start.y, start.z)
- end
- function checkstats ()
- if computer.maxEnergy() > computer.energy()*4 then
- component.chat.say("Sono stanco!")
- robodigger.rested = false
- return false
- end
- if robot.durability() == nil then
- component.chat.say("Tool rotto!")
- robodigger.rested = false
- return false
- end
- if component.inventory_controller.getStackInInternalSlot().label == "Torch" and component.inventory_controller.getStackInInternalSlot().size == 1 then
- component.chat.say("Finite torce!")
- robodigger.rested = false
- return false
- end
- if component.inventory_controller.getStackInInternalSlot(robot.inventorySize()) == true then -- controlla se c'è qualcosa nell'ultimo slot
- component.chat.say("inventario pieno!")
- robodigger.rested = false
- return false
- end
- end
- function pathbuilder ()
- table.insert(path, {robodigger.x, robodigger.y, robodigger.z})
- print(path[#path][1])
- print(path[#path][2])
- print(path[#path][3])
- end
- --usa pathbuilder per tornare indietro con invgogoto
- function ritorno()
- for i =#path, 1, -1 do
- component.chat.say(tostring(i))
- invgogoto(path[i][1], path[i][2], path[i][3], false, false)
- end
- raddrizza()
- end
- -- usa pathbuilder per tornare all'ultimo passaggio valido, e a seconda della manovra fatta, prova a superare il problema
- function fixmove()
- invgogoto(path[#path][1], path[#path][2], path[#path][3], false, false, true)
- end
- function rest()
- end
- function switchtodiamond (side)
- robot.select(2)
- component.inventory_controller.equip()
- if side == sides.top then
- robot.swingUp()
- elseif side == sides.down then
- robot.swingDown()
- else
- robot.swing()
- end
- component.inventory_controller.equip()
- robot.select(1)
- end
- -- main loop
- while true do
- pathbuilder()
- if gogoto(5,0,0, false, true, false) == false then
- break
- end
- ritorno()
- raddrizza()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement