Guest User

Untitled

a guest
May 10th, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.06 KB | None | 0 0
  1. -- This is the turtle client code.
  2.  
  3.  
  4.  
  5.  
  6. -- Just some prerequisite stuff.
  7. rednet.open("left")
  8.  
  9. -- Reading, or if needed, creating the position file.
  10. function getPosition()
  11.     if fs.exists("pos") == true then
  12.         print("It exists, friend.")
  13.         pos = fs.open("pos","r")
  14.         -- taking advantage of the game's coordinate system
  15.         x = pos.readLine()
  16.         y = pos.readLine()
  17.         z = pos.readLine()
  18.         -- values are as following: 1, west. 2, north. 3, east. 4, south
  19.         f = pos.readLine()
  20.         pos.close()
  21.     else
  22.         -- Getting the input Coords.
  23.         pos = fs.open("pos", "w")
  24.         print("Insert the current X, Y, Z, and F (Direction facing value.) respectively.")
  25.         print("Values for F are as following: 1.0 - west. 2.0 - north. 3.0 - east. 0.0 - south.")
  26.         write("X: ")
  27.         local inpX = read()
  28.         write("Y: ")
  29.         local inpY = read()
  30.         write("Z: ")
  31.         local inpZ = read()
  32.         write("F: ")
  33.         local inpF = read()
  34.        
  35.         -- Writing the input coords.
  36.         pos.writeLine(inpX)
  37.         pos.writeLine(inpY)
  38.         pos.writeLine(inpZ)
  39.         pos.writeLine(inpF)
  40.         pos.close()
  41.     end
  42. end
  43.  
  44.  
  45. function changeTurtle(func, var, var2)
  46.     -- Both changing the position and writing the new one to the 'pos' file.
  47.    
  48.     -- exclusively for turning the turtle
  49.     function turtleTurn(turnD)
  50.     print("Beginning turtleturn function.")
  51.         -- turnD is either 'l' or 'r', simply for left or right.
  52.         getPosition()
  53.         if f == "1.0" and turnD == "r" then
  54.            
  55.             turtle.turnRight()
  56.             f = "2.0"
  57.             fs.delete("pos")
  58.             pos = fs.open("pos","a")
  59.             pos.writeLine(x)
  60.             pos.writeLine(y)
  61.             pos.writeLine(z)
  62.             pos.writeLine(f)
  63.             pos.close()
  64.             print("Written.")
  65.         elseif f == "1.0" and turnD == "l" then
  66.            
  67.             turtle.turnLeft()
  68.             f = "0.0"
  69.             fs.delete("pos")
  70.             pos = fs.open("pos","a")
  71.             pos.writeLine(x)
  72.             pos.writeLine(y)
  73.             pos.writeLine(z)
  74.             pos.writeLine(f)
  75.             pos.close()
  76.             print("Written.")
  77.         elseif f == "0.0" and turnD == "r" then
  78.            
  79.             turtle.turnRight()
  80.             f = "1.0"
  81.             fs.delete("pos")
  82.             pos = fs.open("pos","a")
  83.             pos.writeLine(x)
  84.             pos.writeLine(y)
  85.             pos.writeLine(z)
  86.             pos.writeLine(f)
  87.             pos.close()
  88.             print("Written.")
  89.            
  90.         elseif f == "0.0" and turnD == "l" then
  91.            
  92.             turtle.turnLeft()
  93.             f = "3.0"
  94.             fs.delete("pos")
  95.             pos = fs.open("pos","a")
  96.             pos.writeLine(x)
  97.             pos.writeLine(y)
  98.             pos.writeLine(z)
  99.             pos.writeLine(f)
  100.             pos.close()
  101.             print("Written.")
  102.         elseif f == "3.0" and turnD == "r" then
  103.            
  104.             turtle.turnRight()
  105.             f = "0.0"
  106.             fs.delete("pos")
  107.             pos = fs.open("pos","a")
  108.             pos.writeLine(x)
  109.             pos.writeLine(y)
  110.             pos.writeLine(z)
  111.             pos.writeLine(f)
  112.             pos.close()
  113.             print("Written.")
  114.         elseif f == "3.0" and turnD == "l" then
  115.            
  116.             turtle.turnLeft()
  117.             f = "2.0"
  118.             fs.delete("pos")
  119.             pos = fs.open("pos","a")
  120.             pos.writeLine(x)
  121.             pos.writeLine(y)
  122.             pos.writeLine(z)
  123.             pos.writeLine(f)
  124.             pos.close()
  125.             print("Written.")
  126.         elseif f == "2.0" and turnD == "r" then
  127.             print("Haven't turned yet.")
  128.             turtle.turnRight()
  129.             print("Turning, deleting file.")
  130.             f = "3.0"
  131.             fs.delete("pos")
  132.             pos = fs.open("pos","a")
  133.             print("New File opened.")
  134.             pos.writeLine(x)
  135.             pos.writeLine(y)
  136.             pos.writeLine(z)
  137.             pos.writeLine(f)
  138.             print("Written new coords.")
  139.             pos.close()
  140.             print("Closed.")
  141.         elseif f == "2.0" and turnD == "l" then
  142.            
  143.             turtle.turnLeft()
  144.             f = "1.0"
  145.             fs.delete("pos")
  146.             pos = fs.open("pos","a")
  147.             pos.writeLine(x)
  148.             pos.writeLine(y)
  149.             pos.writeLine(z)
  150.             pos.writeLine(f)
  151.             pos.close()
  152.             print("Written.")
  153.         end
  154.         end
  155.        
  156.         function turtleMove(dir, dist)
  157.         if dir == "-x" then
  158.             if f == "3.0" then
  159.                 for i = 1, dist do
  160.                     turtle.goForward()
  161.                 end
  162.             elseif f == "1.0" then
  163.                 turtleTurn("r")
  164.                 turtleTurn("r")
  165.                 for i = 1, dist do
  166.                     turtle.forward()
  167.                 end
  168.             elseif f == "2.0" then
  169.                 turtleTurn("r")
  170.                 for i = 1, dist do
  171.                     turtle.forward()
  172.                 end
  173.             elseif f == "0.0" then 
  174.                 turtleTurn("l")
  175.                 for i = 1, dist do
  176.                     turtle.forward()
  177.                 end
  178.             end
  179.         elseif dir == "+x" then
  180.             if f == "1.0" then
  181.                 for i = 1, dist do
  182.                     turtle.forward()
  183.                 end
  184.             elseif f == "2.0" then
  185.                 turtleTurn("l")
  186.                 for i = 1, dist do
  187.                     turtle.forward()
  188.                 end
  189.             elseif f == "3.0" then
  190.                 turtleTurn("l")
  191.                 turtleTurn("l")
  192.                 for i = 1, dist do
  193.                     turtle.forward()
  194.                 end
  195.             elseif f == "0.0" then
  196.                 turteTurn("r")
  197.                 for i = 1, dist do
  198.                     turtle.forward()
  199.                 end
  200.             end
  201.         elseif dir == "-z" then
  202.             if f == "2.0" then
  203.                 for i = 1, dist do
  204.                     turtle.forward()
  205.                 end
  206.             elseif f == "3.0" then
  207.                 turtleTurn("l")
  208.                 for i = 1, dist do
  209.                     turtle.forward()
  210.                 end
  211.             elseif f == "0.0" then
  212.                 turtleTurn("r")
  213.                 turtleTurn("r")
  214.                 for i = 1, dist do
  215.                     turtle.forward()
  216.                 end
  217.             elseif f == "1.0" then
  218.                 turtleTurn("r")
  219.                 for i = 1, dist do
  220.                     turtle.forward()
  221.                 end
  222.             end
  223.         elseif dir == "+z" then
  224.             if f == "0.0" then
  225.                 for i = 1, dist do
  226.                     turtle.forward()
  227.                 end
  228.             elseif f == "1.0" then
  229.                 turtleTurn("l")
  230.                 for i = 1, dist do
  231.                     turtle.forward()
  232.                 end
  233.             elseif f == "3.0" then
  234.                 turtleTurn("r")
  235.                 for i = 1, dist do
  236.                     turtle.forward()
  237.                 end
  238.             elseif f == "2.0" then
  239.                 turtleTurn("l")
  240.                 turtleTurn("l")
  241.                 for i = 1, dist do
  242.                     turtle.forward()
  243.                 end
  244.             end
  245.         end
  246.     end
  247.  
  248.         if func == "tturn" then
  249.             print("Did we even make it this far? Inside func if")
  250.                 turtleTurn(tostring(var))
  251.         elseif func == "move" then
  252.             print("Moving.")
  253.             turtleMove(var, var2)
  254.         end
  255.     end
  256.    
  257. function figureOutTurn(newf)
  258.     while f ~= newf do
  259.     turtle.turnLeft()
  260.     end
  261. end
  262.    
  263.    
  264. function recieveCall()
  265.     print("Waiting for item dir")
  266.     item = rednet.receive()
  267.     print("Item dir received, it is "..item)
  268.     sleep(0.2)
  269.     print("Waiting for amount.")
  270.     amount = rednet.receive()
  271.     print("Amount is "..amount)
  272.     itemnum = tonumber(item)
  273.     amountnum = tonumber(amount)
  274.    
  275.     if itemnum ~= f then
  276.         print("Turning for item.")
  277.         figureOutTurn(itemnum)
  278.         turtle.suck(amountnum)
  279.     else
  280.     turtle.suck(amountnum)
  281. end
  282. end
  283.  
  284. -- just some test calls
  285. getPosition()
  286. recieveCall()
  287. sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment