tomtrein

builderplus

Jun 4th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.88 KB | None | 0 0
  1. -- Original By Orwell
  2. -- Modified by Tomtrein
  3. -- version 0.0.1 - 4 june 2016
  4.  
  5. local tArgs = { ... }
  6. if #tArgs ~= 2 then
  7. print("Usage: build <gunzipped schematic file> <layer>")
  8. return
  9. end
  10.  
  11. local filename = tArgs[1]
  12.  
  13. if not fs.exists(filename) then
  14. print("File does not exist.")
  15. return
  16. end
  17.  
  18. local length = 0
  19. local height = tArgs[2]
  20. local width = 0
  21. local blocks = {}
  22. local data = {}
  23.  
  24. block_id = {}
  25.  
  26. block_id[0] = "Air"
  27. block_id[1] = "Stone"
  28. block_id[2] = "Grass"
  29. block_id[3] = "Dirt"
  30. block_id[4] = "Cobblestone"
  31. block_id[5] = "Wooden Plank"
  32. block_id[6] = "Sapling"
  33. block_id[7] = "Bedrock"
  34. block_id[8] = "Water"
  35. block_id[9] = "Stationary water"
  36. block_id[10] = "Lava"
  37. block_id[11] = "Stationary lava"
  38. block_id[12] = "Sand"
  39. block_id[13] = "Gravel"
  40. block_id[14] = "Gold Ore"
  41. block_id[15] = "Iron (Ore)"
  42. block_id[16] = "Coal Ore"
  43. block_id[17] = "Log"
  44. block_id[18] = "Leaves"
  45. block_id[19] = "Sponge"
  46. block_id[20] = "Glass"
  47. block_id[21] = "Lapis Lazuli (Ore)"
  48. block_id[22] = "Lapis Lazuli (Block)"
  49. block_id[23] = "Dispenser"
  50. block_id[24] = "Sandstone"
  51. block_id[25] = "Note Block Tile entity"
  52. block_id[26] = "Bed"
  53. block_id[27] = "Powered Rail "
  54. block_id[28] = "Detector Rail "
  55. block_id[29] = "Sticky Piston"
  56. block_id[30] = "Cobweb"
  57. block_id[31] = "Tall Grass"
  58. block_id[32] = "Dead Bush"
  59. block_id[33] = "Piston"
  60. block_id[34] = "Piston Extension"
  61. block_id[35] = "Wool"
  62. block_id[36] = "Block moved by Piston"
  63. block_id[37] = "Dandelionandelion"
  64. block_id[38] = "Rose"
  65. block_id[39] = "Brown Mushroom"
  66. block_id[40] = "Red Mushroom"
  67. block_id[41] = "Block of Gold"
  68. block_id[42] = "Block of Iron"
  69. block_id[43] = "Double Slabs"
  70. block_id[44] = "Slabs"
  71. block_id[45] = "Brick Block"
  72. block_id[46] = "TNT"
  73. block_id[47] = "Bookshelf"
  74. block_id[48] = "Moss Stone"
  75. block_id[49] = "Obsidian"
  76. block_id[50] = "Torch"
  77. block_id[51] = "Fire"
  78. block_id[52] = "Monster Spawner"
  79. block_id[53] = "Wooden Stairs"
  80. block_id[54] = "Chest"
  81. block_id[55] = "Redstone (Wire)"
  82. block_id[56] = "Diamond (Ore)"
  83. block_id[57] = "Block of Diamond"
  84. block_id[58] = "Crafting Table"
  85. block_id[59] = "Seeds"
  86. block_id[60] = "Farland"
  87. block_id[61] = "Furnace"
  88. block_id[62] = "Burning Furnace"
  89. block_id[63] = "Sign Post"
  90. block_id[64] = "Wooden Door"
  91. block_id[65] = "Ladders"
  92. block_id[66] = "Rails"
  93. block_id[67] = "Cobblestone Stairs"
  94. block_id[68] = "Wall Sign"
  95. block_id[69] = "Lever"
  96. block_id[70] = "Stone Pressure Plate"
  97. block_id[71] = "Iron Door"
  98. block_id[72] = "Wooden Pressure Plates"
  99. block_id[73] = "Redstone Ore"
  100. block_id[74] = "Glowing Redstone Ore"
  101. block_id[75] = "Redstone Torch"
  102. block_id[76] = "Redstone Torch"
  103. block_id[77] = "Stone Button "
  104. block_id[78] = "Snow"
  105. block_id[79] = "Ice"
  106. block_id[80] = "Snow Block"
  107. block_id[81] = "Cactus"
  108. block_id[82] = "Clay (Block)"
  109. block_id[83] = "Sugar Cane"
  110. block_id[84] = "Jukebox"
  111. block_id[85] = "Fence"
  112. block_id[86] = "Pumpkin"
  113. block_id[87] = "Netherrack"
  114. block_id[88] = "Soul Sand"
  115. block_id[89] = "Glowstone"
  116. block_id[90] = "Portal"
  117. block_id[91] = "Jack-O-Lantern"
  118. block_id[92] = "Cake Block"
  119. block_id[93] = "Redstone Repeater"
  120. block_id[94] = "Redstone Repeater"
  121. block_id[95] = "Locked Chest"
  122. block_id[96] = "Trapdoors"
  123. block_id[97] = "Hidden Silverfish"
  124. block_id[98] = "Stone Brick"
  125. block_id[99] = "Huge brown and red mushroom"
  126. block_id[100] = "Huge brown and red mushroom"
  127. block_id[101] = "Iron Bars"
  128. block_id[102] = "Glass Pane"
  129. block_id[103] = "Melon"
  130. block_id[104] = "Pumpkin Stem"
  131. block_id[105] = "Melon Stem"
  132. block_id[106] = "Vines"
  133. block_id[107] = "Fence Gate"
  134. block_id[108] = "Brick Stairs"
  135. block_id[109] = "Stone Brick Stairs"
  136. block_id[110] = "Mycelium"
  137. block_id[111] = "Lily Pad"
  138. block_id[112] = "Nether Brick"
  139. block_id[113] = "Nether Brick Fence"
  140. block_id[114] = "Nether Brick Stairs"
  141. block_id[115] = "Nether Wart"
  142. block_id[116] = "Enchantment Table"
  143. block_id[117] = "Brewing Stand"
  144. block_id[118] = "Cauldron"
  145. block_id[119] = "End Portal"
  146. block_id[120] = "End Portal Frame"
  147. block_id[121] = "End Stone "
  148. block_id[256] = "Iron Ingotron Shovel"
  149. block_id[257] = "Iron Pickaxe"
  150. block_id[258] = "Iron Axe"
  151. block_id[259] = "Flint and Steel"
  152. block_id[260] = "Red Apple"
  153. block_id[261] = "Bow"
  154. block_id[262] = "Arrow"
  155. block_id[263] = "Coal"
  156.  
  157. woolColors = {}
  158. woolColors[0] = "White"
  159. woolColors[1] = "Orange"
  160. woolColors[2] = "Magenta"
  161. woolColors[3] = "Light Blue"
  162. woolColors[4] = "Yellow"
  163. woolColors[5] = "Lime"
  164. woolColors[6] = "Pink"
  165. woolColors[7] = "Gray"
  166. woolColors[8] = "Light Gray"
  167. woolColors[9] = "Cyan"
  168. woolColors[10] = "Purple"
  169. woolColors[11] = "Blue"
  170. woolColors[12] = "Brown"
  171. woolColors[13] = "Green"
  172. woolColors[14] = "Red"
  173. woolColors[15] = "Black"
  174.  
  175. function getBlockName(id, blockData)
  176. blockData = blockData or nil
  177. if(block_id[id] == nil) then
  178. return "UNKNOWN"
  179. else
  180. if(blockData) then
  181. if(id == 35) then
  182. str = woolColors[blockData] .. " " .. block_id[id]
  183. return str
  184. end
  185. end
  186. return block_id[id]
  187. end
  188. end
  189.  
  190. function getBlockId(x,y,z)
  191. return blocks[y + z*width + x*length*width + 1]
  192. end
  193.  
  194. function getData(x,y,z)
  195. return data[y + z*width + x*length*width + 1]
  196. end
  197.  
  198. function readbytes(h, n)
  199. for i=1,n do
  200. h.read()
  201. end
  202. end
  203.  
  204. function readname(h)
  205. n1 = h.read()
  206. n2 = h.read()
  207.  
  208. if(n1 == nil or n2 == nil) then
  209. return ""
  210. end
  211.  
  212. n = n1*256 + n2
  213.  
  214. str = ""
  215. for i=1,n do
  216. c = h.read()
  217. if c == nil then
  218. return
  219. end
  220. str = str .. string.char(c)
  221. end
  222. return str
  223. end
  224.  
  225. function parse(a, h, containsName)
  226. containsName = containsName or true
  227. if a==0 then
  228. return
  229. end
  230. if containsName then
  231. name = readname(h)
  232. end
  233.  
  234. if a==1 then
  235. readbytes(h,1)
  236. elseif a==2 then
  237. i1 = h.read()
  238. i2 = h.read()
  239. i = i1*256 + i2
  240. if(name=="Height") then
  241. heightmax = i
  242. elseif (name=="Length") then
  243. length = i
  244. elseif (name=="Width") then
  245. width = i
  246. end
  247. elseif a==3 then
  248. readbytes(h,4)
  249. elseif a==4 then
  250. readbytes(h,8)
  251. elseif a==5 then
  252. readbytes(h,4)
  253. elseif a==6 then
  254. readbytes(h,8)
  255. elseif a==7 then
  256. i1 = h.read()
  257. i2 = h.read()
  258. i3 = h.read()
  259. i4 = h.read()
  260. i = i1*256*256*256 + i2*256*256 + i3*256 + i4
  261. if name == "Blocks" then
  262. for i=1,i do
  263. table.insert(blocks, h.read())
  264. end
  265. elseif name == "Data" then
  266. for i=1,i do
  267. table.insert(data, h.read())
  268. end
  269. else
  270. readbytes(h,i)
  271. end
  272. elseif a==8 then
  273. i1 = h.read()
  274. i2 = h.read()
  275. i = i1*256 + i2
  276. readbytes(h,i)
  277. elseif a==9 then
  278. --readbytes(h,5)
  279. type = h.read()
  280. i1 = h.read()
  281. i2 = h.read()
  282. i3 = h.read()
  283. i4 = h.read()
  284. i = i1*256*256*256 + i2*256*256 + i3*256 + i4
  285. for j=1,i do
  286. parse(h.read(), h, false)
  287. end
  288. end
  289. end
  290.  
  291. function forward()
  292. while not turtle.forward() do
  293. turtle.dig()
  294. end
  295. end
  296.  
  297. function up()
  298. while not turtle.up() do
  299. turtle.digUp()
  300. end
  301. end
  302.  
  303. function down()
  304. while not turtle.down() do
  305. turtle.digDown()
  306. end
  307. end
  308.  
  309. function place()
  310. while not turtle.placeDown() do
  311. turtle.digDown()
  312. end
  313. end
  314.  
  315. h = fs.open(filename, "rb")
  316.  
  317. a = 0
  318. while (a ~= nil) do
  319. a = h.read()
  320. parse(a, h)
  321. end
  322.  
  323. write("length: " .. length)
  324. write(" width: " .. width)
  325. write(" height: " .. heightmax .. "\n")
  326. thisone = read()
  327.  
  328. uniqueblocks={}
  329. for i,v in ipairs(blocks) do
  330. found = false
  331. for j,w in ipairs(uniqueblocks) do
  332. -- for now, data is only accounted for when the block is whool
  333. if (w.blockID==v and (w.data==data[i] or w.blockID ~= 35)) then
  334. found = true
  335. w.amount = w.amount + 1
  336. break
  337. end
  338. end
  339.  
  340. if found==false then
  341. uniqueblocks[#uniqueblocks+1] = {}
  342. uniqueblocks[#uniqueblocks].blockID = v
  343. uniqueblocks[#uniqueblocks].data = data[i]
  344. uniqueblocks[#uniqueblocks].amount = 1
  345. end
  346. end
  347.  
  348. print("number of block types: " .. #uniqueblocks)
  349. for i,v in ipairs(uniqueblocks) do
  350. print(" -" .. getBlockName(v.blockID, v.data) .. ": " .. v.amount)
  351. if (i%16)==0 then
  352. read()
  353. end
  354. end
  355.  
  356. read()
  357.  
  358. print("Give the numbers of all slots containing the specified block type:")
  359.  
  360. slots={}
  361. for i,block in ipairs(uniqueblocks) do
  362. blockData = block.data
  363. print(" -in which slots is " .. getBlockName(block.blockID, blockData) .. "?")
  364. if not slots[block.blockID] then
  365. slots[block.blockID] = {}
  366. end
  367. slots[block.blockID][blockData] = {}
  368. write(" ")
  369. str = read()
  370. for i = 1, #str do
  371. local c = str:sub(i,i)
  372. n = tonumber(c)
  373. if(n) then
  374. if(n>0 and n<17) then
  375. table.insert(slots[block.blockID][blockData], n)
  376. end
  377. end
  378. end
  379. end
  380.  
  381. print("Press key to start building...")
  382. read()
  383.  
  384. up()
  385. n = 1
  386. turtle.select(n)
  387.  
  388. for x=height,height do
  389. for x=1,height do
  390. okhello = turtle.forward()
  391. end
  392. for y=1,width do
  393. for z=1,length do
  394. blockID = getBlockId(x-1,y-1,z-1)
  395. blockData = getData(x-1,y-1,z-1)
  396. forward()
  397. turtle.digDown()
  398. slot_lst = slots[blockID][blockData]
  399. if(slot_lst ~= nil) then
  400. if(#slot_lst > 0) then
  401. local found=false
  402. for i,v in ipairs(slot_lst) do
  403. if(turtle.getItemCount(v) > 0) then
  404. found=true
  405. turtle.select(v)
  406. break
  407. end
  408. end
  409. if not found then
  410. if(blockID ~= 0) then
  411. print("Not enough " .. getBlockName(blockID, blockData) .. ". Please refill...")
  412. read()
  413. end
  414. end
  415. if(blockID ~= 0) then
  416. place()
  417. end
  418. end
  419. end
  420. end
  421. turtle.turnLeft()
  422. forward()
  423. turtle.turnLeft()
  424. for i=1,length do
  425. forward()
  426. end
  427. turtle.turnRight()
  428. turtle.turnRight()
  429. end
  430. turtle.turnRight()
  431. for i=1,width do
  432. forward()
  433. end
  434. turtle.turnRight()
  435. for p=height, heightmax do
  436. hellothere = turtle.forward()
  437. end
  438. end
Advertisement
Add Comment
Please, Sign In to add comment