Guest User

Untitled

a guest
Jun 13th, 2012
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. --Program that runs a script..
  2. term.clear()
  3. term.setCursorPos(1,1)
  4. local Program = {...}
  5. local Logging = true
  6. local Dir = nil
  7. local Rep = nil
  8. if Program[2] == "logging" and Program[3] == "off" then
  9. Logging = false
  10. end
  11. Program = Program[1]
  12. local function Log(stuff)
  13. if Logging then
  14. print(stuff)
  15. end
  16. end
  17. --Checks if file has no errors
  18. local i = 0
  19. local b = 0
  20. Log("Checking if file has errors")
  21. if Program == nil or not fs.exists(Program) then
  22. Log("Script does not exist!")
  23. error()
  24. end
  25. Log("To disable this logging, start the program like this")
  26. Log("\""..shell.getRunningProgram().." "..Program.." logging off\"")
  27. local File = io.open(Program)
  28. for line in File:lines() do
  29. i = i+1
  30. b = i
  31. local function Wrong()
  32. print("Line "..i.." is wrong: "..line)
  33. File:close()
  34. error()
  35. end
  36. if string.find(line, " ") then
  37. local Type = string.lower(string.sub(line, 1, string.find(line, " ")-1))
  38. if Type == "print" then
  39. Dir = string.lower(string.sub(line, string.find(line, " ")+1, string.len(line)))
  40. elseif string.find(line, " ", (string.find(line, " ")+1)) and tonumber(string.sub(line, string.find(line, " ", string.find(line, " ")+1), string.len(line))) > 0 then -- Detected 2 spaces! Which means a error or repeation!
  41. Rep = string.sub(line, string.find(line, " ", string.find(line, " ")+1), string.len(line))
  42. Dir = string.lower(string.sub(line, string.find(line, " ")+1, (string.len(line)-string.len(Rep))))
  43. Rep = tonumber(Rep)
  44. else
  45. Dir = string.lower(string.sub(line, string.find(line, " ")+1, string.len(line)))
  46. end
  47. local function Check(...)
  48. local Bla = {...}
  49. for n=1,#Bla do
  50. if Dir == Bla[n] then
  51. return true
  52. end
  53. end
  54. Wrong()
  55. end
  56. if Dir == "" then
  57. Wrong()
  58. end
  59. if string.find(line, " ") then
  60. Wrong()
  61. end
  62. if Type == "turn" then
  63. Check("left", "right")
  64. elseif Type == "dig" then
  65. Check("up", "down", "forwards")
  66. elseif Type == "print" then
  67. -- Its right.. How can it be wrong
  68. elseif Type == "move" then
  69. Check("forwards", "back", "down", "up")
  70. elseif Type == "place" then
  71. Check("up", "down", "forwards")
  72. elseif Type == "select" then
  73. if tonumber(Dir) > 0 and tonumber(Dir) < 10 then
  74. -- Yay..
  75. else
  76. Wrong()
  77. end
  78. elseif Type == "place" then
  79. Check("up", "down", "forwards")
  80. else
  81. Wrong()
  82. end
  83. end
  84. end
  85. File:close()
  86. Log("File check completed")
  87. Log("Running script")
  88. local Dont = false
  89. local i = 0
  90. local File = io.open(Program)
  91. for line in File:lines() do
  92. i = i+1
  93. Rep = 1
  94. local function Log(stuff)
  95. if Logging and not Dont then
  96. print(i.." of "..b..": "..stuff)
  97. end
  98. end
  99. local Type = string.lower(string.sub(line, 1, string.find(line, " ")-1))
  100. if Type == "print" then
  101. print(line)
  102. Dir = string.lower(string.sub(line, string.find(line, " ")+1, string.len(line)))
  103. elseif string.find(line, " ", string.find(line, " ")+1) and tonumber(string.sub(line, string.find(line, " ", string.find(line, " ")+1), string.len(line))) > 0 then -- Detected 2 spaces! Which means a error or repeation!
  104. Rep = string.sub(line, string.find(line, " ", string.find(line, " ")+1), string.len(line))
  105. Dir = string.lower(string.sub(line, string.find(line, " ")+1, (string.len(line)-string.len(Rep))))
  106. Rep = tonumber(Rep)
  107. else
  108. Dir = string.lower(string.sub(line, string.find(line, " ")+1, string.len(line)))
  109. end
  110. if Rep > 1 then
  111. Log("Repeating \""..Type.." "..Dir.."\" "..Rep.." times")
  112. Dont = true
  113. end
  114. repeat
  115. Rep = Rep-1
  116. if Type == "turn" then
  117. Log("Turning turtle "..Dir)
  118. if Dir == "left" then
  119. turtle.turnLeft()
  120. elseif Dir == "right" then
  121. turtle.turnRight()
  122. end
  123. elseif Type == "print" then
  124. print(Dir)
  125. elseif Type == "dig" then
  126. Log("Turtle is now digging "..Dir)
  127. if Dir == "up" then
  128. turtle.digUp()
  129. elseif Dir == "down" then
  130. turtle.digDown()
  131. elseif Dir == "forwards" then
  132. turtle.dig()
  133. end
  134. elseif Type == "move" then
  135. Log("Turtle is now moving "..Dir)
  136. if Dir == "forwards" then
  137. turtle.forward()
  138. elseif Dir == "back" then
  139. turtle.back()
  140. elseif Dir == "up" then
  141. turtle.up()
  142. elseif Dir == "down" then
  143. turtle.down()
  144. end
  145. elseif Type == "select" then
  146. Log("Turtle is selecting slot "..tonumber(Dir))
  147. turtle.select(tonumber(Dir))
  148. elseif Type == "place" then
  149. Log("Placing block "..Dir)
  150. if Dir == "forwards" then
  151. turtle.place()
  152. elseif Dir == "up" then
  153. turtle.placeUp()
  154. elseif Dir == "down" then
  155. turtle.placeDown()
  156. end
  157. end
  158. until Rep < 1
  159. Dont = false
  160. end
  161. Log("Script completed")
  162. File:close()
Advertisement
Add Comment
Please, Sign In to add comment