Advertisement
hevohevo

Exec_Sign_0_1

May 21st, 2014
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.49 KB | None | 0 0
  1. -- ######################################
  2. -- Exec Sign
  3. -- version 0.1
  4. -- http://hevohevo.hatenablog.com/
  5.  
  6. -- Required: OpenPeripheral MOD
  7. -- Turtle basically goes forward.
  8. -- When he reaches an sign, run the program.
  9.  
  10.  
  11. -- Write functions to the sign
  12. -- Semicolon is important to order some functions.
  13. -- t = turtle
  14.  
  15. -- ------------------
  16. -- | t.turnRight(); |
  17. -- | t.turnRight(); |
  18. -- | t.turnRight(); |
  19. -- | t.turnRight(); |
  20. -- ------------------
  21. --        |
  22. --        |
  23. --        |
  24.  
  25.  
  26. -- required OpenPeripheral MOD
  27. -- if not fs.exists ("openp/") then
  28. --  error("Required: OpenPeripheral MOD")
  29. -- end
  30.  
  31. -- Config
  32. SIGN = "sign"
  33.  
  34. function exec_sign()
  35.   t=turtle
  36.   local p = peripheral.wrap("front")
  37.   if p then
  38.     local text = p.getText()
  39.     if string.find(text, "-->") then
  40.       turtle.turnRight()
  41.     elseif string.find(text, "<--") then
  42.       turtle.turnLeft()
  43.     else
  44.       local func, err = loadstring(text)
  45.       if not func then
  46.         printError( err )
  47.         return
  48.       end
  49.       setfenv(func, getfenv())
  50.       local success, msg = pcall(func)
  51.       if not success then
  52.         printError( msg )
  53.       end
  54.     end
  55.   else
  56.     error("unrecognize sign.")
  57.   end
  58. end
  59.  
  60. function isType(type_name)
  61.   return peripheral.getType("front")==type_name
  62. end
  63.  
  64. while true do
  65.   if turtle.detect() then
  66.     if isType(SIGN) then
  67.       print("detect a sign")
  68.       exec_sign()
  69.     else
  70.       print("detect a block")
  71.     end
  72.   else
  73.     turtle.forward()
  74.   end
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement