Guest User

variable room

a guest
Jun 5th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.27 KB | None | 0 0
  1. local x = 0
  2. local y = 0
  3. local z = 0
  4. local direction = 0
  5. local width = 0
  6. local length = 0
  7. local height = 0
  8. local oldx = 0
  9. local oldz = 0
  10. local oldy = 0
  11. local olddir = 0
  12.  
  13.  
  14.  
  15. function file()
  16.   if not fs.exists("coord") then
  17.     w = fs.open("coord","w")
  18.     w.writeLine("0")
  19.     w.writeLine("0")
  20.     w.writeLine("0")
  21.     w.writeLine("0")
  22.     w.writeLine("0")
  23.     w.writeLine("0")
  24.     w.writeLine("0")
  25.     w.writeLine("0")
  26.     w.writeLine("-- 1 = x, 2 = z, 3 = y, 4 = direction")
  27.     w.writeLine("-- 5 = oldx, 6 = oldz, 7 = oldy, 8 = olddir")
  28.     w.close()
  29.   end
  30. end
  31.  
  32.  
  33. function readsave()
  34.   file()
  35.   r = fs.open("coord","r")
  36.   x = tonumber(r.readLine(1))
  37.   z = tonumber(r.readLine(2))
  38.   y = tonumber(r.readLine(3))
  39.   direction = tonumber(r.readLine(4))
  40.   oldx = tonumber(r.readLine(5))
  41.   oldz = tonumber(r.readLine(6))
  42.   oldy = tonumber(r.readLine(7))
  43.   olddir = tonumber(r.readLine(8))
  44.   r.close()
  45. end
  46.  
  47.  
  48. function save()
  49.   s = fs.open("coord","w")
  50.   s.writeLine(x)
  51.   s.writeLine(z)
  52.   s.writeLine(y)
  53.   s.writeLine(direction)
  54.   s.writeLine(oldx)
  55.   s.writeLine(oldz)
  56.   s.writeLine(oldy)
  57.   s.writeLine(olddir)
  58.   s.close()
  59. end
  60.  
  61. function forward()
  62.   if turtle.forward()==true then
  63.     if direction==0 then
  64.       z = z+1
  65.       save()
  66.     elseif direction==1 then
  67.       x = x-1
  68.       save()
  69.     elseif direction==2 then
  70.       z = z-1
  71.       save()
  72.     elseif direction==3 then
  73.       x = x+1
  74.       save()
  75.     end
  76.   else
  77.     print("something is blocking my way")
  78.     turtle.dig()
  79.     turtle.attack()
  80.     sleep(1)
  81.     forward()
  82.   end
  83. end
  84.  
  85.  
  86. function dig()
  87.   turtle.dig()
  88.   forward()
  89. end
  90.  
  91. function down()
  92.   if turtle.down()==true then
  93.     y = y-1
  94.     save()
  95.   else
  96.     while turtle.down()==false do
  97.       turtle.attack()
  98.       turtle.digDown()
  99.     end
  100.     y = y-1
  101.     save()
  102.   end
  103. end
  104.  
  105.  
  106. function up()
  107.   if turtle.up()==true then
  108.     y = y+1
  109.     save()
  110.   else
  111.     while turtle.up()==false do
  112.       turtle.attack()
  113.       turtle.digUp()
  114.     end
  115.     y = y+1
  116.     save()
  117.   end
  118. end
  119.  
  120. function Left()
  121.   if turtle.turnLeft()==true then
  122.     direction = direction - 1
  123.     direction = direction % 4
  124.     save()
  125.   end
  126. end
  127.  
  128. function Right()
  129.   if turtle.turnRight()==true then
  130.     direction = direction + 1
  131.     direction = direction % 4
  132.     save()
  133.   end
  134. end
  135.  
  136.  
  137. file()
  138. readsave()
  139. save()
  140. print("enter desired width:")
  141. wid = io.read()
  142. print("enter desired length:")
  143. len = io.read()
  144. print("enter desired height:")
  145. high = io.read()
  146.  
  147. function length()
  148.   for i = 2, len do
  149.     dig()
  150.     bank()
  151.   end
  152.   Left()
  153.   Left()
  154.   for i = 2, len do
  155.     dig()
  156.   end
  157.   Right()
  158.   Right()
  159. end
  160.  
  161. function width()
  162.   for j = 2, wid do
  163.     Left()
  164.     dig()
  165.     Right()
  166.     length()
  167.   end
  168. end
  169.  
  170. function height()
  171.   for m = 2, high do
  172.     length()
  173.     width()
  174.     Right()
  175.     for i = 1, x do
  176.       dig()
  177.     end
  178.   Left()
  179.   turtle.digDown()
  180.   down()
  181.   end
  182.   length()
  183.   width()
  184. end
  185.  
  186.  
  187. function face0()
  188.   readsave()
  189.     while direction~=0 do
  190.       Right()
  191.       readsave()
  192.       print(direction)
  193.     end
  194.   print("facing direction 0")
  195. end
  196.  
  197. function face1()
  198.   readsave()
  199.     while direction~=1 do
  200.       Right()
  201.       readsave()
  202.       print(direction)
  203.     end
  204.   print("facing direction 1")
  205. end
  206.  
  207. function face2()
  208.   readsave()
  209.     while direction~=2 do
  210.       Right()
  211.       readsave()
  212.       print(direction)
  213.     end
  214.   print("facing direction 2")
  215. end
  216.  
  217. function face3()
  218.   readsave()
  219.     while direction~=3 do
  220.       Right()
  221.       readsave()
  222.       print(direction)
  223.     end
  224.   print("facing direction 3")
  225. end
  226.  
  227. function xto0()
  228.   readsave()
  229.     if x>0 then
  230.       face1()
  231.       while x~=0 do
  232.         forward()
  233.         readsave()
  234.         print("current cords".."("..x..","..z..")")
  235.       end
  236.     face0()
  237.     print("arrived at 0 x coord")
  238.     elseif x<0 then
  239.       face3()
  240.       while x~=0 do
  241.         forward()
  242.         readsave()
  243.         print("current cords".."("..x..","..z..")")
  244.       end
  245.     face0()
  246.     print("arrived at 0 x coord")
  247.     elseif x==0 then
  248.       face0()
  249.       print("turtle already at x=0")
  250.     end
  251. end
  252.  
  253. function zto0()
  254.   readsave()
  255.     if z>0 then
  256.       face2()
  257.       while z~=0 do
  258.         forward()
  259.         readsave()
  260.         print("current cords".."("..x..","..z..")")
  261.       end
  262.     face0()
  263.     print("arrived at z=0 coord")
  264.     elseif z<0 then
  265.       face0()
  266.       while z~=0 do
  267.         forward()
  268.         readsave()
  269.         print("current cords".."("..x..","..z..")")
  270.       end
  271.     face0()
  272.     print("arrived at z=0 coord")
  273.     elseif z==0 then
  274.       face0()
  275.       print("turtle already at z=0")
  276.     end
  277. end
  278.  
  279. function yto0()
  280.   readsave()
  281.     if y>0 then
  282.       while y~=0 do
  283.         down()
  284.         readsave()
  285.         print("y ="..y)
  286.       end
  287.     print("arrived at y=0")
  288.     elseif y<0 then
  289.       while y~=0 do
  290.         up()
  291.         readsave()
  292.         print("y ="..y)
  293.       end
  294.     print("arrived at y = 0")
  295.     elseif y==0 then
  296.       print("arived at y = 0")
  297.     end
  298. end
  299.  
  300.  
  301.  
  302. function bank()
  303.   readsave()
  304.     if turtle.getItemCount(16)>=1 then
  305.       oldx = x
  306.       oldz = z
  307.       oldy = y
  308.       olddir = direction
  309.       save()
  310.       print(oldx..oldz..oldy)
  311.       print(x..z..y)
  312.       xto0()
  313.       zto0()
  314.       yto0()
  315.       face2()
  316.       for banker = 1, 16 do
  317.         turtle.select(banker)
  318.         turtle.drop()
  319.       end
  320.       face0()
  321.     end
  322.   backoldx()
  323.   backoldz()
  324.   backoldy()
  325.   backolddir()
  326.   turtle.select(1)
  327. end
  328.  
  329.  
  330. function backoldx()
  331. readsave()
  332.   if oldx>0 then
  333.     face3()
  334.     while x~=oldx do
  335.       forward()
  336.       readsave()
  337.     end
  338.   elseif oldx<0 then
  339.     face1()
  340.     while x~=oldx do
  341.       forward()
  342.       readsave()
  343.     end
  344.   end
  345. end
  346.  
  347.  
  348. function backoldz()
  349. readsave()
  350.   if oldz>0 then
  351.     face0()
  352.     while z~=oldz do
  353.       forward()
  354.       readsave()
  355.     end
  356.   elseif oldz<0 then
  357.     face2()
  358.     while z~=oldz do
  359.       forward()
  360.       readsave()
  361.     end
  362.   end
  363. end
  364.  
  365.  
  366. function backoldy()
  367. readsave()
  368.   if oldy>0 then
  369.     while y~=oldy do
  370.       up()
  371.       readsave()
  372.     end
  373.   elseif oldy<0 then
  374.     while y~=oldy do
  375.       down()
  376.       readsave()
  377.     end
  378.   end
  379. end
  380.  
  381. function backolddir()
  382. readsave()
  383.   while direction~=olddir do
  384.     Left()
  385.   end
  386. end
  387.  
  388.  
  389.  
  390. height()
  391. xto0()
  392. zto0()
  393. yto0()
Advertisement
Add Comment
Please, Sign In to add comment