Advertisement
Guest User

move

a guest
Aug 7th, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.89 KB | None | 0 0
  1. -- Functions --
  2.  
  3. function store(sName, data)
  4.   local filepath = fs.combine("data", sName)
  5.   if data == nil then
  6.     return fs.delete(filepath)
  7.   end
  8.   local handle = fs.open(filepath, "w")
  9.   handle.write(textutils.serialize(data))
  10.   handle.close()
  11. end
  12.  
  13. function pull(sName)
  14.   local filepath = fs.combine("data", sName)
  15.   local handle = fs.open(filepath, "r")
  16.   local data = handle.readAll()
  17.   handle.close()
  18.   return textutils.unserialize(data)
  19. end
  20.  
  21. function reset()
  22.   local location = {x = 0, y = 0, z = 0, face = 0}
  23.   store("location", location)
  24. end
  25.  
  26. function display()
  27.   local location = pull("location")
  28.   print("x: ", location["x"])
  29.   print("y: ", location["y"])
  30.   print("z: ", location["z"])
  31.   print("face: ", location["face"])
  32. end
  33.  
  34. function left()
  35.   local location = pull("location")
  36.   --  print("face: ", location[4])
  37.   if location["face"] == 1 then
  38.     location["face"] = 0
  39.   elseif location["face"] == 2 then
  40.     location["face"] = 1
  41.   elseif location["face"] == 3 then
  42.     location["face"] = 2
  43.   elseif location["face"] == 0 then
  44.     location["face"] = 3
  45.   end
  46.   store("location", location)
  47.   turtle.turnLeft()
  48. end
  49.  
  50. function right()
  51.   local location = pull("location")
  52.   --  print("face: ",location[4])
  53.   if location["face"] == 1 then
  54.     location["face"] = 2
  55.   elseif location["face"] == 2 then
  56.     location["face"] = 3
  57.   elseif location["face"] == 3 then
  58.     location["face"] = 0
  59.   elseif location["face"] == 0 then
  60.     location["face"] = 1
  61.   end
  62.   store("location", location)
  63.   turtle.turnRight()
  64. end
  65.  
  66. function forward()
  67.   while not turtle.forward() do
  68.     sleep(1)
  69.   end
  70.   local location = pull("location")
  71.   if location["face"] == 0 then
  72.     location["z"] = location["z"] + 1
  73.   elseif location["face"] == 1 then
  74.     location["x"] = location["x"] -1
  75.   elseif location["face"] == 2 then
  76.     location["z"] = location["z"] -1
  77.   elseif location["face"] == 3 then
  78.     location["x"] = location["x"] +1
  79.   end
  80.   store("location", location)
  81. end
  82.  
  83. function back()
  84.   while not turtle.back() do
  85.     sleep(1)
  86.   end
  87.   local location = pull("location")
  88.   if location["face"] == 0 then
  89.     location["z"] = location["z"] - 1
  90.   elseif location["face"] == 1 then
  91.     location["x"] = location["x"] + 1
  92.   elseif location["face"] == 2 then
  93.     location["z"] = location["z"] + 1
  94.   elseif location["face"] == 3 then
  95.     location["x"] = location["x"] - 1
  96.   end
  97.   store("location", location)
  98. end
  99.  
  100. function up()
  101.   while not turtle.up() do
  102.     sleep(1)
  103.   end
  104.   location = pull("location")
  105.   location["y"] = location["y"] + 1
  106.   store("location", location)
  107. end
  108.  
  109. function down()
  110.   while not turtle.down() do
  111.     sleep(1)
  112.   end
  113.   location = pull("location")
  114.   location["y"] = location["y"] - 1
  115.   store("location", location)
  116. end
  117.  
  118.  
  119. -- Program --
  120.  
  121. if not fs.exists("data") then
  122.   fs.makeDir("data")
  123. end
  124.  
  125. if not fs.exists("data/location") then
  126.   reset()
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement