Advertisement
lost_RD

TidyTurtlesv1.0

Mar 11th, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.86 KB | None | 0 0
  1. local printy = print
  2. local print = function() end
  3.  
  4. tArgs = {...}
  5. if not tArgs[1] then
  6.   printy("Error: no cards specified")
  7.   do return end
  8. end
  9. file = tArgs[1]
  10.  
  11. os.loadAPI("fileAPI")
  12. os.loadAPI("stringAPI")
  13. lines = fileAPI.lines_from("cards/"..file)
  14.  
  15. cards = {}
  16.  
  17. for k,v in pairs(lines) do
  18.   t = stringAPI.split(v)
  19.   i, ifUntidy , ifTidy = t[1],t[2],t[3]
  20.   cards[i] = {ifTidy = ifTidy, ifUntidy = ifUntidy}
  21.   if not iFirst then
  22.     iFirst = i
  23.   end
  24. end
  25.  
  26. for k,v in pairs(cards) do
  27.   print(k," ",v.ifUntidy ," ",v.ifTidy )
  28. end
  29.  
  30. turns = {}
  31. turns["00"] = function() printy("going forward") end
  32. turns["01"] = function() printy("turning left") turtle.turnLeft() end
  33. turns["10"] = function() printy("turning right") turtle.turnRight() end
  34. turns["11"] = function() printy("turning around") for i=1,2 do turtle.turnLeft() end end
  35.  
  36. function interpret(str)
  37.   t = {}
  38.  
  39.   t.untidy = str:sub(1,1)=="1"
  40.   print(t.untidy and "untidy" or "tidy")
  41.  
  42.   t.doSomething = str:sub(2,2)=="1"
  43.   t.turn = str:sub(3,4)
  44.   t.goto = str:sub(5)
  45.  
  46.   return t
  47. end
  48.  
  49. index = iFirst
  50.  
  51. repeat
  52.  
  53.   if turtle.detectUp() then
  54.     printy("Error: out of bounds")
  55.     turtle.turnLeft()
  56.     turtle.turnLeft()
  57.     turtle.forward()
  58.   end
  59.  
  60.   printy(index," ",cards[index].ifUntidy," ",cards[index].ifTidy," ",turtle.detectDown())
  61.   instructions = turtle.detectDown() and interpret(cards[index].ifUntidy) or interpret(cards[index].ifTidy)
  62.  
  63.   if instructions then
  64.     print(instructions.doSomething," ",instructions.untidy)
  65.     if instructions.untidy and instructions.doSomething then
  66.       turtle.digDown()
  67.     end
  68.     if not instructions.untidy and instructions.doSomething then
  69.       turtle.placeDown()
  70.     end
  71.  
  72.     turns[instructions.turn]()
  73.     turtle.forward()
  74.  
  75.     index = instructions.goto
  76.  
  77.   end
  78.  
  79. until index == string.rep("0",iFirst:len())
  80.  
  81. printy("Operation complete!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement