Advertisement
HarvDad

write

Jun 8th, 2014
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1. -- write
  2. -- Builds the letters from the input text
  3. -- Written by HarvDad, June 2014
  4.  
  5. version = "write: Rev 0.1"
  6. instructions = "Place stack(s) of material starting in first slot."
  7.  
  8. usage = "write <text>"
  9.  
  10. args = {...}
  11. nArgs = #args
  12.  
  13.  
  14. text = ""
  15.  
  16. letters = {
  17. {"A", "====== ", " = =", " = =", " = =", "====== "},
  18. {"B", "=======", "= = =", "= = =", "= =", " == == "},
  19. {"C", " ===== ", "= =", "= =", "= =", " = = "},
  20. {"D", "=======", "= =", "= =", "= =", " ===== "},
  21. {"E", "=======", "= = =", "= = =", "= = =", "= ="},
  22. {"F", "=======", " = =", " = =", " = =", " ="},
  23. {"G", " ===== ", "= =", "= = =", "= = =", " === ="},
  24. {"H", "=======", " = ", " = ", " = ", "======="},
  25. {"I", " ", "= =", "=======", "= =", " "},
  26. {"J", " == ", "= ", "= ", "= ", " ======"},
  27. {"K", "=======", " = ", " = = ", " = = ", "= ="},
  28. {"L", "=======", "= ", "= ", "= ", "= "},
  29. {"M", "=======", " = ", " = ", " = ", "======="},
  30. {"N", "=======", " = ", " = ", " = ", "======="},
  31. {"O", " ===== ", "= =", "= =", "= =", " ===== "},
  32. {"P", "=======", " = =", " = =", " = =", " == "},
  33. {"Q", " ===== ", "= =", "= = =", "== =", "====== "},
  34. {"R", "=======", " = =", " == =", " = = =", "= == "},
  35. {"S", "= == ", "= = =", "= = =", "= = =", " == ="},
  36. {"T", " =", " =", "=======", " =", " ="},
  37. {"U", " ======", "= ", "= ", "= ", " ======"},
  38. {"V", " =====", " = ", "= ", " = ", " ====="},
  39. {"W", "=======", " = ", " == ", " = ", "======="},
  40. {"X", "== ==", " = = ", " = ", " = = ", "== =="},
  41. {"Y", " ===", " = ", "=== ", " = ", " ==="},
  42. {"Z", "== =", "= = =", "= = =", "= = =", "= =="},
  43. {"?", " = ", " =", "= == =", " = =", " == "},
  44. {"!", " ", " ", "= =====", " ", " "},
  45. {" ", " ", " ", " ", " ", " "},
  46. }
  47.  
  48. materialSlot = 1
  49.  
  50. x = 0
  51. y = 0
  52. z = 0
  53. face = 0
  54.  
  55. -- The following 'face' directions are relative to the starting position of the turtle in this program
  56. north = 0
  57. west = 1
  58. south = 2
  59. east = 3
  60.  
  61. goForward = 0
  62. goBackward = 1
  63. direction = goForward
  64.  
  65. currentSlot = 1
  66.  
  67. function setFace(f)
  68. if f == 0 then
  69. if face == 0 then return end
  70. if face == 1 then right() return end
  71. if face == 2 then right() right() return end
  72. if face == 3 then left() return end
  73. end
  74.  
  75. if f == 1 then
  76. if face == 0 then left() return end
  77. if face == 1 then return end
  78. if face == 2 then right() return end
  79. if face == 3 then right() right() return end
  80. end
  81.  
  82. if f == 2 then
  83. if face == 0 then left() left() return end
  84. if face == 1 then left() return end
  85. if face == 2 then return end
  86. if face == 3 then right() return end
  87. end
  88.  
  89. if f == 3 then
  90. if face == 0 then right() return end
  91. if face == 1 then left() left() return end
  92. if face == 2 then left() return end
  93. if face == 3 then return end
  94. end
  95. end
  96.  
  97. function left()
  98. if face == 0 then face = 1 turtle.turnLeft() return end
  99. if face == 1 then face = 2 turtle.turnLeft() return end
  100. if face == 2 then face = 3 turtle.turnLeft() return end
  101. if face == 3 then face = 0 turtle.turnLeft() return end
  102. print("function left\(\): Bad face value: ", face)
  103. end
  104.  
  105. function right()
  106. if face == 0 then face = 3 turtle.turnRight() return end
  107. if face == 1 then face = 0 turtle.turnRight() return end
  108. if face == 2 then face = 1 turtle.turnRight() return end
  109. if face == 3 then face = 2 turtle.turnRight() return end
  110. print("function right\(\): Bad face value: ", face)
  111. end
  112.  
  113. function up()
  114. while turtle.detectUp() do -- This loop added in case of falling sand or whatever
  115. turtle.digUp()
  116. end
  117. turtle.up()
  118. y = y+1
  119. end
  120.  
  121. function rise(nBlocks)
  122. local i
  123. for i=1,nBlocks do
  124. up()
  125. end
  126. end
  127.  
  128. function down()
  129. if turtle.detectDown() then
  130. turtle.digDown()
  131. end
  132. turtle.down()
  133. y = y-1
  134. end
  135.  
  136. function moveForward(nBlocks)
  137. for i = 1, nBlocks do
  138. turtle.forward()
  139. if face == north then
  140. z = z + 1
  141. elseif face == south then
  142. z = z - 1
  143. elseif face == east then
  144. x = x + 1
  145. elseif face == west then
  146. x = x - 1
  147. end
  148. end
  149.  
  150. end
  151.  
  152. function moveBackward(nBlocks)
  153. for i = 1, nBlocks do
  154. turtle.back()
  155. if face == north then
  156. z = z - 1
  157. elseif face == south then
  158. z = z + 1
  159. elseif face == east then
  160. x = x - 1
  161. elseif face == west then
  162. x = x + 1
  163. end
  164. end
  165. end
  166.  
  167. function home(targetY)
  168. -- print("home:face ", face, ", x = ", x, ", z = ", z)
  169. if x < 0 then
  170. setFace(east)
  171. while x < 0 do
  172. moveForward(1)
  173. end
  174. else
  175. if x > 0 then
  176. setFace(west)
  177. while x > 0 do
  178. moveForward(1)
  179. end
  180. end
  181. end
  182.  
  183. if z < 0 then
  184. setFace(north)
  185. while z < 0 do
  186. moveForward(1)
  187. end
  188. else
  189. if z > 0 then
  190. setFace(south)
  191. while z > 0 do
  192. moveForward(1)
  193. end
  194. end
  195. end
  196.  
  197. if y > 0 then
  198. while y > 0 do
  199. down()
  200. end
  201. end
  202.  
  203. setFace(0)
  204. end
  205.  
  206. function ensureMaterial()
  207. if turtle.getItemCount(materialSlot) < 3 then
  208. organizeMaterial()
  209. end
  210. if turtle.getItemCount(materialSlot) < 3 then
  211. print("No more material")
  212. return false
  213. end
  214. return true
  215. end
  216.  
  217. function organizeMaterial()
  218. local i
  219. materialCount = turtle.getItemCount(materialSlot)
  220.  
  221. if materialCount < 3 then
  222. -- print("Attempting to refill slot ", materialSlot)
  223. for i=2,16 do
  224. turtle.select(i)
  225. if turtle.compareTo(materialSlot) then
  226. turtle.transferTo(materialSlot)
  227. end
  228. end
  229. end
  230. turtle.select(materialSlot)
  231. end
  232.  
  233. function getLetter(letter)
  234. letter = string.upper(letter)
  235.  
  236. for i=1,#letters do
  237. if letters[i][1] == letter then
  238. return letters[i]
  239. end
  240. end
  241. print("Cannot find entry for \'", letter, "\'")
  242. return nil
  243. end
  244.  
  245. function hitTheDeck()
  246. --print("hitTheDeck: y = ", y)
  247. while y > 0 do
  248. down()
  249. -- print(" Down")
  250. end
  251. -- print(" y ends at ", y)
  252. goingUp = true
  253. end
  254.  
  255.  
  256. goingUp = true
  257.  
  258. function buildLetter(letter)
  259. letterData = getLetter(letter)
  260. turtle.select(1)
  261.  
  262. if letter == " " then
  263. right()
  264. moveForward(7)
  265. left()
  266. return
  267. end
  268. --[[
  269. if letterData ~= nil then
  270. for i=1,#letterData do
  271. print(" ", letterData[i])
  272. end
  273. end
  274. --]]
  275.  
  276. if letterData == nil then
  277. print("Cannot build \'", letter, "\'")
  278. return
  279. end
  280.  
  281. for i=2,#letterData do
  282. column = letterData[i]
  283. height = string.len(column)
  284. if goingUp then
  285. for j=1,height do
  286. char = string.sub(column, j, j)
  287. if char ~= " " then
  288. ensureMaterial()
  289. turtle.select(materialSlot)
  290. turtle.place()
  291. end
  292. if j < height then
  293. up()
  294. end
  295. end
  296. else
  297. for j=height,1,-1 do
  298. char = string.sub(column, j, j)
  299. if char ~= " " then
  300. ensureMaterial()
  301. turtle.select(materialSlot)
  302. turtle.place()
  303. end
  304. if j > 1 then
  305. down()
  306. end
  307. end
  308. end
  309.  
  310. if goingUp then
  311. goingUp = false
  312. else
  313. goingUp = true
  314. end
  315.  
  316. right()
  317. moveForward(1)
  318. left()
  319. end
  320. end
  321.  
  322.  
  323.  
  324. if nArgs == 0 then
  325. print(version)
  326. print(instructions)
  327. print(usage)
  328. return
  329. end
  330.  
  331. text = ""
  332. for i=1,nArgs do
  333. text = text .. " " .. args[i]
  334. end
  335. text = string.sub(text, 2)
  336.  
  337. for i=1,string.len(text) do
  338. buildLetter(string.sub(text,i,i))
  339. right()
  340. moveForward(1)
  341. -- hitTheDeck()
  342. moveForward(1)
  343. left()
  344. end
  345.  
  346. home(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement