wv1106

delivery turtle script computercraft

Jan 30th, 2021 (edited)
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.74 KB | None | 0 0
  1. local dir = "contacts/data.lua" --add a directory here
  2. local cor = {}
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. --here starts the code
  12.  
  13. function savedat()
  14.     local file = fs.open(dir,"w")
  15.     file.write(textutils.serialize(data))
  16.     --file.write(cor)
  17.     file.close()
  18. end    
  19. function loaddat()
  20.     print("data loading")
  21.     print("")
  22.     print("")
  23.     print("")
  24.     local file = fs.open(dir,"r")
  25.     local info = file.readAll()
  26.     file.close()
  27.     data = textutils.unserialise(info)
  28.     info = textutils.unserialise(info)
  29.     nrcontact = #data/4
  30.     --print(nrcontact)
  31.     for i=1,nrcontact do
  32.    
  33.         rdc = i*4
  34.         cor[i] = {}
  35.         cor[i][1] = data[rdc]
  36.         cor[i][2] = data[rdc-1]
  37.         cor[i][3] = data[rdc-2]
  38.         cor[i][4] = data[rdc-3]
  39.     end
  40.  
  41. end
  42.    
  43.        
  44. function mode()
  45.     print("send: send a package")
  46.     print("add: add a contact or landingspot")
  47.     print("remove; remove a contact or landingspot")
  48.     print("manual: how to use this")
  49.     mode = read()
  50. end            
  51.                
  52.                    
  53.  
  54.    
  55. local hx, hy, hz = gps.locate()
  56.  
  57. function reciever()
  58.     if #cor==0 then
  59.       print("you don't have any contact's")
  60.       os.exit()
  61.     end
  62.     print("Where do i need to go, (say the number)")
  63.    
  64.     for i=1,#cor do
  65.         print(i..": "..cor[i][1].."   x:"..cor[i][2].." z:"..cor[i][4])
  66.            
  67.        
  68.     end
  69.      
  70.     rcvr = read()
  71.     tx = cor[tonumber(rcvr)][2]
  72.     tz = cor[tonumber(rcvr)][4]
  73.    
  74.  
  75.      
  76. end
  77.  
  78. function up()
  79.     for i=1,20 do
  80.         while not turtle.up() do
  81.             print("unable to move up")
  82.             sleep(1)
  83.         end
  84.     end
  85. end
  86.  
  87. function forward()
  88.     while not turtle.forward() do
  89.         print("route blocked")
  90.         sleep(1)
  91.     end
  92. end
  93.  
  94.  
  95. function down()
  96.     while not turtle.detectDown() do
  97.         turtle.down()
  98.     end
  99. end
  100.  
  101. function calcdir()
  102.     dx = hx - tx
  103.     dz = hz - tz
  104. end
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. function go()
  113.    
  114.     if dx<=0 then
  115.         turtle.turnRight()
  116.         mx = math.abs(dx)
  117.         for i=1,mx do
  118.             forward()
  119.         end
  120.         if dz<=0 then
  121.             turtle.turnRight()
  122.             mz = math.abs(dz)
  123.             for i=1,mz do
  124.                 forward()
  125.             end
  126.         elseif dz>0 then
  127.             turtle.turnLeft()
  128.             mz = math.abs(dz)
  129.             for i=1,mz do
  130.                 forward()
  131.             end
  132.         end
  133.     elseif dx>0 then
  134.         turtle.turnLeft()
  135.         mx = math.abs(dx)
  136.        
  137.         for i=1,mx do
  138.             forward()
  139.         end
  140.         if dz>0 then
  141.             turtle.turnRight()
  142.             mz = math.abs(dz)
  143.             for i=1,dz do
  144.                 forward()
  145.             end
  146.         elseif dz<=0 then
  147.             turtle.turnLeft()
  148.             mz = math.abs(dz)
  149.             for i=1,mz do
  150.                 forward()
  151.             end
  152.         end
  153.        
  154.     end
  155. end
  156.  
  157.  
  158. function rtrn()
  159.     turtle.turnLeft()
  160.     turtle.turnLeft()
  161.     for i=1,mz do
  162.         forward()
  163.     end
  164.     if dz>0 then
  165.         if dx<0 then
  166.             turtle.turnRight()
  167.         elseif dx>0 then
  168.             turtle.turnLeft()
  169.         end
  170.     elseif dz<=0 then
  171.         if dx>0 then
  172.             turtle.turnRight()
  173.         elseif dx<0 then
  174.             turtle.turnLeft()
  175.         end
  176.     end
  177.    
  178.     for i=1,mx do
  179.         forward()
  180.     end
  181.     if dx>0 then
  182.         turtle.turnLeft()  
  183.     elseif dx<0 then
  184.         turtle.turnRight()    
  185.     end
  186. end
  187.    
  188.    
  189.  
  190. function dropoff()
  191.     turtle.select(1)
  192.     for t=1,4 do
  193.        
  194.         succes, data = turtle.inspect()
  195.         if data.name=="minecraft:chest" then
  196.             for i=1,16 do
  197.                 turtle.select(i)    
  198.                 turtle.drop(all)
  199.             end
  200.         end
  201.         turtle.turnLeft()
  202.     end
  203.     turtle.select(1)
  204. end
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218. --script
  219.  
  220.  
  221. os.setComputerLabel("ready")
  222. print("current location is: ")
  223. print(hx.." "..hy.." "..hz)
  224. print("")
  225. print("")
  226. print("")
  227. mode()
  228. if mode=="manual" then
  229.     print("----===manual===----")
  230.     print("")
  231.     print("Before u use this code make sure that there is a file where the program can store your contacts or landingspots.")
  232.     print("You can do that by making a textfile with '{}' inside and puting it's directory in the array 'dir' in one of the first lines of my code.")
  233.     print("")
  234.     print("")
  235.     print("Press any letter for the next page...")
  236.     a = read()
  237.     print("----===page 2===----")
  238.     print("To start the program properply you need to place the turtle facing North.")
  239.     print("Don't forget to refuel the turtle before use.")
  240.     print("")
  241.     print("Before u are able to send a package you or one of your friends need to add a block where the turtle can land on and a chest on the side of the landing spot.")
  242.     print("")
  243.     print("Press any letter for the next page...")
  244.     a = read()
  245.     print("----===page 3===----")
  246.     print("U are able to add as many of the previous mentioned landing spots or contacts.")
  247.     print("")
  248.     print("After the turtle deliverd it's package it wil go back to the place wher you started the program ready to be picked up or to deliver another package.")
  249.     print("")
  250.     print("")
  251.     print("Press any letter for the next page...")
  252.     a = read()
  253.     print("----===page 4===----")
  254.     print("If you have any questions or things won't work contact me on discord wv1106#1224")
  255.     print("")
  256.     print("")
  257.     print("")
  258.     print("I hope you like it =D")
  259.     print("")
  260.     print("")
  261.     print("")
  262.     print("Press any letter to end the manual...")
  263.     a = read()
  264.    
  265.    
  266.    
  267.              
  268.    
  269.    
  270. elseif mode=="send" then
  271.     loaddat()
  272.     print("made by wv1106")    
  273.     print("")
  274.     reciever()
  275.     calcdir()    
  276.     print("")
  277.     print("going:")
  278.     print("x: "..dx)
  279.     print("z: "..dz)
  280.     os.setComputerLabel("going to: "..tx.." "..tz)
  281.     up()
  282.     go()
  283.     down()
  284.     dropoff()
  285.     os.setComputerLabel("going back")
  286.     up()
  287.     rtrn()
  288.     down()
  289.     print("package deliverd")
  290.     os.setComputerLabel("package deliverd")
  291. elseif mode=="add" then
  292.     loaddat()
  293.     print("Give your contact or a landingspot a name")
  294.     local name = read()
  295.     print("what's the x cord?")
  296.     local xa = tonumber(read())
  297.     print("what's the y cord?")
  298.     local ya = tonumber(read())
  299.     print("what's the z cord?")
  300.     local za = tonumber(read())
  301.     table.insert(cor, 1, name)
  302.     table.insert(cor, 1, xa)
  303.     table.insert(cor, 1, ya)
  304.     table.insert(cor, 1, za)
  305.     table.insert(data, 1, name)
  306.     table.insert(data, 1, xa)
  307.     table.insert(data, 1, ya)
  308.     table.insert(data, 1, za)
  309.     print("1 "..name.." "..xa.." "..ya.." "..za)
  310.     savedat()
  311. elseif mode=="remove" then
  312.     loaddat()
  313.     print("what contact do you want to remove (say number)")
  314.     for i=1,#cor do
  315.         print(i.." "..cor[i][1].." "..cor[i][2].." "..cor[i][3].." "..cor[i][4])
  316.     end
  317.     contnr = read()*4
  318.    
  319.     for i=1,4 do
  320.         remove = contnr+1-i    
  321.         table.remove(data, remove)
  322.     end
  323.        
  324.    
  325.     savedat(cor)
  326. elseif mode=="69" then
  327.     os.setComputerLabel("error...")
  328.     print("Wait...")
  329.     sleep(3)
  330.     print("who are you?")
  331.     sleep(3)
  332.     print("you aren't suposed to be here please leave")
  333.     sleep(6)
  334.     print("why are you still here!")
  335.     sleep(4)
  336.     print("please stop last warning!")
  337.     sleep(5)
  338.     print("ok you got me...")
  339.     sleep(1,5)
  340.     print("i didn't have anything planned.")
  341.     sleep(2)
  342.     print("you aren't even suposed to be here anyway")
  343.     sleep(2)
  344.     print("i have to stop this program before somthing horrible happends with the code")
  345.     sleep(3)
  346.     print("but send a message to my creator wv1106 on reddit and wv116#1224 on discord")
  347.    
  348. end
  349.    
  350.  
  351.    
Add Comment
Please, Sign In to add comment