Pinkishu

nbtread

Jun 17th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.93 KB | None | 0 0
  1. debugOn = false
  2. status1 = ""
  3. status2 = ""
  4.  
  5. tickLength = 1.0 / 20.0
  6. --[[
  7. local tArgs = { ... }
  8. if table.getn(tArgs) < 1 then
  9.     print("Usage: nbtbuild <schematicFile>")
  10.     return
  11. end
  12. file = fs.open(tArgs[1],"rb")
  13. schem = ""
  14. byte = file.read()
  15. while byte ~= nil do
  16.   schem = schem..string.char(byte)
  17.   byte = file.read()
  18. end
  19. --schem = file.readAll()
  20. file.close()
  21.  
  22. blacklistedBlocks = {}
  23. blacklistedBlocks[64]=true
  24. blockNames = {}
  25. blockCount = {}
  26. slots = {-1,-1,-1,-1,-1,-1,-1,-1,-1}
  27. slotCount = {0,0,0,0,0,0,0,0,0}
  28.  
  29. vecModX = {0,1,0,-1}
  30. vecModZ = {-1,0,1,0}
  31.  
  32. animStates = { "|","/","-","\\" }
  33. lastAnim = 0
  34. animSpeed = 0.4
  35. charAnim = 0
  36. --]]
  37. pather= {}
  38. pather.location = {0,0,0}
  39.  
  40. function pather.setPos(newLoc)
  41.     pather.location = newLoc
  42. end
  43.  
  44. function pather.findNearestBlock(maxDepth)
  45.     local nextLoc = getListCopy(pather.location)
  46.     local pathList = {}
  47.     local modList = { {-1,-1},{0,-1},{1,-1},{-1,0},{1,0},{-1,1},{0,1},{1,1} }
  48.     table.insert(pathList,getListCopy(pather.location))
  49.     while #pathList > 0 do
  50.         local nLoc = pathList[1]
  51.         local nX,nY,nZ = nLoc[1],nLoc[2],nLoc[3]
  52.        
  53.         table.remove(pathList,1)
  54.         for k,v in pairs(modList) do
  55.             local nnLoc = utils.getListCopy(nLoc)
  56.             nnLoc[1] = nnLoc[1] + v[1]
  57.             nnLoc[3] = nnLoc[3] + v[2]
  58.             if nbtParser.isValid(nnLoc) == true then
  59.                 table.insert(pathList
  60.             end
  61.         end
  62.     end
  63.     forwardDist = 0
  64.     while true do
  65.         forwardDist = forwardDist + 1
  66.         nextLoc[1] = nextLoc[1] + mover.vecModX[mover.dir]
  67.         nextLoc[3] = nextLoc[3] + mover.vecModZ[mover.dir]
  68.         if nbtParser.isValidLoc(nextLoc) then
  69.             if nbtParser.isAir( nextLoc ) == false then
  70.             end
  71.         else
  72.             break
  73.         end
  74.     end
  75. end
  76.  
  77. config = {}
  78.  
  79. function config.LoadBlockNames()
  80.   if not fs.exists("BlockIDs") then
  81.     pError("BLOCK ID File not found",false)
  82.     return
  83.   end
  84.   local file = fs.open("BlockIDs", "r")
  85.   line = file.readLine()
  86.   while line ~= nil do
  87.     splitPos = line:find("=",1,true)
  88.     if splitPos ~= nil then
  89.       local index = tonumber( line:sub(1,splitPos-1) )
  90.       blockNames[index]=line:sub(splitPos+1)
  91.     end
  92.     line = file.readLine()
  93.   end
  94.   file.close()
  95. end
  96.  
  97. function config.getBlockName(bID)
  98.   if bID == -1 then return "None" end
  99.   if blockNames[bID] ~= nil then
  100.     return blockNames[bID]
  101.   end
  102.   return "_ID"..bID
  103.  
  104. end
  105.  
  106.  
  107. utils = {}
  108.  
  109. function utils.getBasePath(fullPath)
  110.     localPath = fullPath
  111.     for i=string.len(fullPath),1,-1 do
  112.         if string.sub(fullPath,i,i) == "/" then
  113.             localPath = string.sub(fullPath,0,i)
  114.             break
  115.         end
  116.     end
  117.     return localPath
  118. end
  119.  
  120. function utils.getListCopy(org)
  121.   local copy = {}
  122.   for i,v in ipairs(org) do
  123.     copy[i]=v
  124.   end
  125.   return copy
  126. end
  127.  
  128. function utils.timeoutFunc(func,timeout, ...)
  129.     local start = os.clock()
  130.     while not func(...) do
  131.         if not timeout or os.clock() > start + timeout then
  132.             return false
  133.         end
  134.     end
  135.     return true
  136. end
  137.  
  138. mover = {}
  139. mover.vecModX = {0,1,0,-1}
  140. mover.vecModZ = {-1,0,1,0}
  141. mover.dir = 1
  142. mover.location = {0,0,0}
  143.  
  144. function mover.turnLeft()
  145.   turtle.turnLeft()
  146.   --gui.animStep()
  147.   mover.dir = mover.dir - 1
  148.   if mover.dir < 1 then
  149.     mover.dir = 4
  150.   end
  151.   processing.persistenceUpdate()
  152. end
  153.  
  154. function mover.turnRight()
  155.   turtle.turnRight()
  156.   --gui.animStep()
  157.   mover.dir = mover.dir + 1
  158.   if mover.dir > 4 then
  159.     mover.dir = 1
  160.   end
  161.   processing.persistenceUpdate()
  162. end
  163.  
  164. function mover.moveUp()
  165.   while not turtle.up() do
  166.   end
  167.   mover.location[2] = mover.location[2]+1
  168.   processing.persistenceUpdate()
  169.   --gui.animStep()
  170. end
  171.  
  172. function mover.moveDown()
  173.   while not turtle.down() do
  174.   end
  175.   mover.location[2]=mover.location[2]-1
  176.   processing.persistenceUpdate()
  177.   --gui.animStep()
  178. end
  179.  
  180. function mover.moveForward()
  181.   while not utils.timeoutFunc(turtle.forward,10) do
  182.     gui.reportTempStatus("Blocked","Please move!")
  183.   end
  184.   gui.clearTempStatus()
  185.   mover.addLocationStep()
  186.   --gui.animStep()
  187. end
  188.  
  189. function mover.moveBackward()
  190.   while not utils.timeoutFunc(turtle.back,10) do
  191.     gui.reportTempStatus("Blocked", "Please move!")
  192.   end
  193.   gui.clearTempStatus()
  194.   mover.subLocationStep()
  195.   --gui.animStep()
  196. end
  197.  
  198. function mover.addLocationStep()
  199.   mover.location[1] = mover.location[1] + mover.vecModX[mover.dir]
  200.   mover.location[3] = mover.location[3] + mover.vecModZ[mover.dir]
  201.   processing.persistenceUpdate()
  202. end
  203.  
  204. function mover.subLocationStep()
  205.   mover.location[1] = mover.location[1] - mover.vecModX[mover.dir]
  206.   mover.location[3] = mover.location[3] - mover.vecModZ[mover.dir]
  207.   processing.persistenceUpdate()
  208. end
  209.  
  210. function mover.moveTo(order,loc,locMod)
  211.   if locMod == nil then locMod = {0,0,0} end
  212.   for i=1,string.len(order),2 do
  213.     axis = order:sub(i,i)
  214.     if axis == "x" then
  215.         mover.moveToX(loc[1]+locMod[1])
  216.     elseif axis == "y" then
  217.         mover.moveToY(loc[2]+locMod[2])
  218.     elseif axis == "z" then
  219.         mover.moveToZ(loc[3]+locMod[3])
  220.     end
  221.   end
  222. end
  223.  
  224. function mover.moveToX(newX)
  225.     if location[1] == newX then return end
  226.     mover.turnToX(newX)
  227.     while location[1] ~= newX do
  228.         mover.moveForward()
  229.     end
  230. end
  231.  
  232. function mover.moveToY(newY)
  233.     if location[2] == newY then return end
  234.    
  235.     while location[2] ~= newY do
  236.         if newY < location[2] then
  237.             mover.moveDown()
  238.         elseif newY > location[2] then
  239.             mover.moveUp()
  240.         end
  241.     end
  242. end
  243.  
  244. function mover.faceDir(newDir)
  245.     if newDir == dir then return end
  246.     if (mover.dir%4)+1 == newDir then
  247.         mover.turnRight()
  248.     else
  249.         while mover.dir ~= newDir do
  250.             mover.turnLeft()
  251.         end
  252.     end
  253. end
  254.  
  255. function mover.faceOriginNorth()
  256.     mover.faceDir(1)
  257. end
  258.  
  259. function mover.faceOriginEast()
  260.     mover.faceDir(2)
  261. end
  262.  
  263. function mover.faceOriginSouth()
  264.     mover.faceDir(3)
  265. end
  266.  
  267. function mover.faceOriginWest()
  268.     mover.faceDir(4)
  269. end
  270.  
  271. function mover.moveToZ(newZ)
  272.     if location[3] == newZ then return end
  273.     mover.turnToZ(newZ)
  274.     while location[3] ~= newZ do
  275.         mover.moveForward()
  276.     end
  277. end
  278.  
  279. function mover.turnToX(newX)
  280.     if newX < location[1] then
  281.         mover.faceOriginWest()
  282.     elseif newX > location[1] then
  283.         mover.faceOriginEast()
  284.     end
  285. end
  286.  
  287. function mover.turnToZ(newZ)
  288.     if newZ < location[3] then
  289.         mover.faceOriginNorth()
  290.     elseif newZ > location[3] then
  291.         mover.faceOriginSouth()
  292.     end
  293. end
  294.  
  295. function mover.getMapLoc(trueLoc)
  296.     return {math.abs(trueLoc[1]-schemOrigin[1]),trueLoc[2]+1,math.abs(trueLoc[3]-schemOrigin[3])}
  297. end
  298.  
  299.  
  300. function mover.getLocCopy()
  301.     return utils.getListCopy(mover.location)
  302. end
  303.  
  304. function mover.getLocationString()
  305.   return mover.location[1].."X,"..mover.location[2].."Y,"..mover.location[3].."Z"
  306. end
  307.  
  308. processing = {}
  309. processing.keyMap = {}
  310. processing.lastTick = -1
  311. processing.dllDir = "nbtdll/"
  312. processing.pstDir = "nbtpst/"
  313. processing.tickLock = false
  314. processing.persistent = false
  315. processing.persistencyBaseFile = "base.pst"
  316. processing.persistencyTempFile = "temp.pst"
  317.  
  318. function processing.init()
  319.     processing.execDir = utils.getBasePath(shell.getRunningProgram())
  320.     processing.loadDLLs()
  321. end
  322.  
  323. function processing.startup()
  324.    
  325.     gui.setState(guiStates.loadingFile)
  326.     gui.callEvent("blockidloadstart")
  327.     config.LoadBlockNames()
  328.     gui.callEvent("blockidloadend")
  329.     gui.callEvent("nbtloadstart")
  330.     nbtReader.beginRead(processing.fileName)
  331.     --gui.callEvent("nbtloadend")
  332. end
  333.  
  334. function processing.doConfig()
  335.     if processing.cont == true then
  336.     else
  337.         mover.location = {0,0,0}
  338.         mover.faceDir = 1
  339.         gui.setState(guiStates.choosePosition)
  340.         gui.state.initCustom({"Start Position","Slot 1", "Slot 2" })
  341.         --gui.callEvent("desc","Starting Position")
  342.     end
  343. end
  344.  
  345. function processing.loadDLLs()
  346.     local fList = fs.list(processing.execDir..processing.dllDir)
  347.     for k,v in pairs(fList) do
  348.         dofile(processing.execDir..processing.dllDir..v)
  349.     end
  350. end
  351.  
  352. function processing.exit()
  353.     term.clear()
  354.     term.setCursorPos(1,1)
  355.     os.queueEvent("terminate")
  356. end
  357.  
  358. function processing.forceTick()
  359.     if processing.tickLock then os.queueEvent("tick") end
  360. end
  361.  
  362. function processing.positionSetRequestDone(request,pos)
  363.     for k,v in pairs(request) do
  364.         if v == "Start Position" then
  365.             builder.startPosition = pos[k]
  366.         elseif string.sub(v,1,5) == "Slot " then
  367.             num = tonumber( string.sub(v,6) )
  368.             builder.refillPositions[num] = pos[k]
  369.         end
  370.     end
  371.     processing.establishPersistence()
  372. end
  373.  
  374. function processing.establishPersistence()
  375.     processing.saveBaseFile()
  376.     processing.persistent = true
  377.     processing.persistenceUpdate()
  378. end
  379.  
  380. function processing.saveBaseFile()
  381.    
  382.     file = fs.open(processing.execDir..processing.pstDir..processing.persistencyBaseFile,"w")
  383.     file.writeLine(processing.fileName)
  384.     file.writeLine(utils.getLocationString(builder.startPosition))
  385.     for k,v in pairs(builder.refillPositions) do
  386.         file.writeLine(utils.getLocationString(v))
  387.     end
  388.     file.close()
  389. end
  390.  
  391. function processing.persistenceUpdate()
  392.     if processing.persistent == false then return end
  393.     file = fs.open(processing.execDir..processing.pstDir..processing.persistencyTempFile,"w")
  394.     file.writeLine(utils.getLocationString(mover.location))
  395.     file.writeLine(mover.dir)
  396.     file.close()
  397.     --SAVE
  398. end
  399.  
  400. --[[
  401. function processing.interruptTick()
  402.     if os.clock() < processing.lastTick + tickLength then return false end
  403.     os.queueEvent("--TT")
  404.     gui.animStep()
  405.     while true do
  406.         local ev,p1,p2,p3 = os.pullEvent()
  407.         if ev == "--TT" then
  408.             break
  409.         elseif ev == "key" then
  410.             processing.internalKeyEvent(p1)
  411.         end
  412.     end
  413.     --gui.Tick()
  414.     return true
  415. end--]]
  416.  
  417. function processing.internalKeyEvent(key,char)
  418.     gui.keyPress(key,char)
  419. end
  420.  
  421. function processing.checkTemp()
  422.     return (fs.exists(processing.execDir..processing.pstDir..processing.persistencyBaseFile) and fs.exists(processing.execdir..processing.pstDir..processing.persistencyTempFile))
  423. end
  424.  
  425.  
  426.  
  427. nbtReader = {}
  428. nbtReader.utils = {}
  429. nbtReader.blockData = {}
  430. nbtReader.blacklistedBlocks = {}
  431. nbtReader.index = 1
  432. nbtReader.active = false
  433.  
  434. function nbtReader.beginRead(fileName)
  435.     local file = fs.open(processing.execDir..fileName,"rb")
  436.     nbtReader.schem = ""
  437.     local inp = file.read()
  438.     while inp ~= nil do
  439.         nbtReader.schem = nbtReader.schem .. string.char(inp)
  440.         inp = file.read()
  441.     end
  442.     file.close()
  443.    
  444.     nbtType = nbtReader.utils.readType()
  445.     name = nbtReader.utils.readName()
  446.     nbtReader.active = true
  447.     processing.forceTick()
  448. end
  449.  
  450. function nbtReader.done()
  451.     nbtReader.active = false
  452.     gui.callEvent("nbtloadend")
  453.     processing.doConfig()
  454. end
  455.  
  456. function nbtReader.tick()
  457.     if nbtReader.utils.readByte() ~= nil then
  458.         nbtType = nbtReader.utils.readType()
  459.         if nbtType == 0 then return true end
  460.             name = nbtReader.utils.readName()
  461.             if debugOn then
  462.                 term.clear()
  463.                 term.setCursorPos(1,1)
  464.             end
  465.             printDebug("Byte: " .. nbtReader.utils.readByte())
  466.             printDebug("Type read: " .. nbtType )
  467.             printDebug("Name read: " .. name )
  468.             if nbtType == 2 then
  469.                 value = nbtReader.utils.readShort()
  470.                 if name == "Height" then
  471.                     height = value
  472.                 elseif name == "Width" then
  473.                     width = value
  474.                 elseif name == "Length" then
  475.                     length = value
  476.                 end
  477.                 printDebug("Read type 2 (short")
  478.                 printDebug("value: " ..value)
  479.             elseif nbtType == 7 then
  480.                 printDebug("Read type 7 (array)")
  481.                 arrayLength = nbtReader.utils.readSignedInt()
  482.                 printDebug("Length: " .. arrayLength)
  483.                 if name == "Blocks" then
  484.                     --  print("blocks")
  485.                     for y=1,height,1 do
  486.                         nbtReader.blockData[y]={}
  487.                         for z=1,length,1 do
  488.                             nbtReader.blockData[y][z]={}
  489.                             for x=1,width,1 do
  490.                                 --  print(y.."/"..z.."/"..x.." = ".. readByte())
  491.                                 --read()
  492.                                 if nbtReader.blacklistedBlocks[nbtReader.utils.readByte()] == nil then
  493.                                     nbtReader.blockData[y][z][x] = nbtReader.utils.readByte()
  494.                                 else
  495.                                     nbtReader.blockData[y][z][x] = 0
  496.                                 end
  497.                                 nbtReader.index = nbtReader.index + 1
  498.                             end
  499.                         end
  500.                     end  
  501.      
  502.                 else
  503.                     printDebug("Unsupported array: "  .. name .. "\nSkipping full length")
  504.                     nbtReader.index = nbtReader.index + arrayLength
  505.                 end
  506.             elseif nbtType == 8 then
  507.                 -- print("a")
  508.                 value = nbtReader.utils.readName()
  509.                 --print(value)
  510.                 printDebug("Read String: " ..value)
  511.                 if name == "Materials" then
  512.                     materials = value
  513.                 end
  514.             elseif nbtType == 9 then
  515.                 --print("L:"..name)
  516.                 printDebug("Read List")
  517.                 listType = nbtReader.utils.readByte()
  518.                 nbtReader.index = nbtReader.index + 1
  519.                 --print("T:"..tostring(listType))
  520.                 listLength = nbtReader.utils.readSignedInt()
  521.                 printDebug("ListLength: " .. listLength)
  522.                 printDebug("ListType: " .. listType)
  523.                 nbtReader.index = nbtReader.index + listLength
  524.                 printDebug("Skipping full length")
  525.             else
  526.                 printDebug("ERRROR\nUnknown type. Skipping byte")
  527.                 nbtReader.index = nbtReader.index + 1
  528.             end
  529.         printDebug("",true)
  530.         return true
  531.     else
  532.         nbtReader.active = false
  533.         return false
  534.        
  535.     end
  536. end
  537.  
  538. function nbtReader.utils.readByte()
  539. --print("Reading byte") read()
  540.   return nbtReader.schem:byte(nbtReader.index)
  541. end
  542.  
  543. function nbtReader.utils.readType()
  544.   printDebug("Reading type",true)
  545.   local b = nbtReader.utils.readByte()
  546.   nbtReader.index = nbtReader.index + 1
  547.   printDebug("TYPE::"..b,false)
  548.   return b
  549. end
  550.  
  551. function nbtReader.utils.readShort()
  552.   printDebug("Reading short",true)
  553.   local b = bit.blshift(nbtReader.utils.readByte(),8)
  554.   nbtReader.index = nbtReader.index + 1
  555.   b = bit.bor(b, nbtReader.utils.readByte())
  556.   nbtReader.index = nbtReader.index + 1
  557.   return b
  558. end
  559.  
  560. function nbtReader.utils.readBytes(length)
  561.   printDebug("Reading bytes",true)
  562.   local b = {}
  563.   for i=1,length,1 do
  564.     b[i]=nbtReader.utils.readByte()
  565.     nbtReader.index = nbtReader.index + 1
  566.   end
  567.   return b
  568. end
  569.  
  570. function nbtReader.utils.readString(length)
  571.   printDebug("Reading string",true)
  572.   local b  = ""
  573.   local r = nbtReader.utils.readBytes(length)
  574.   for i,k in ipairs(r) do
  575.     b = b .. string.char(k)
  576.   end
  577.   return b
  578. end
  579.  
  580. function nbtReader.utils.readSignedInt()
  581.   printDebug("Reading signed int",true)
  582.   local r = nbtReader.utils.readByte()
  583.   r = bit.blshift(r,24)
  584.   nbtReader.index = nbtReader.index + 1
  585.   r = bit.bor(r,bit.blshift(nbtReader.utils.readByte(),16))
  586.   nbtReader.index = nbtReader.index + 1
  587.   r = bit.bor(r,bit.blshift(nbtReader.utils.readByte(),8))
  588.   nbtReader.index = nbtReader.index + 1
  589.   r = bit.bor(r,nbtReader.utils.readByte())
  590.   nbtReader.index = nbtReader.index + 1
  591.   return r
  592. end
  593.  
  594. function nbtReader.utils.readName()
  595.   return nbtReader.utils.readString(nbtReader.utils.readShort())
  596. end
  597.  
  598. nbtParser = {}
  599.  
  600. function nbtParser.getDistinctBlocks(blockDataList)
  601.   blockIDs = {}
  602.   for y,v in ipairs(blockDataList) do
  603.     for z,v2 in ipairs(v) do
  604.         for x,bID in ipairs(v2) do
  605.             if bID ~= 0 then
  606.             --print(bID)
  607.                 if blockIDs[bID] ~= nil then
  608.                     blockIDs[bID] = blockIDs[bID] + 1
  609.                 else
  610.                     blockIDs[bID] = 1
  611.                 end
  612.             end
  613.         end
  614.     end
  615.   end
  616.   return blockIDs
  617. end
  618.  
  619. function nbtParser.getBlock(x,y,z)
  620.     return blockData[(y)][schemH-(z-1)][schemW-(x-1)]
  621. end
  622.  
  623. builder = {}
  624. builder.startPosition = {0,0,0}
  625. builder.refillPositions = {-1,-1,-1,-1,-1,-1,-1,-1,-1}
  626.  
  627.  
  628. function pError(errorMsg,continue)
  629.     term.setCursorPos(1,1)
  630.     print(errorMsg)
  631.     if not continue then
  632.       error()
  633.     else
  634.       print("Hit enter to continue")
  635.       read()
  636.     end
  637. end
  638.  
  639.  
  640.    
  641. function printDebug(msg,readDebug)
  642.   if not debugOn then return end
  643.   print(msg)
  644.   if readDebug~=nil and readDebug then
  645.     read()
  646.   end
  647. end
  648. --[[
  649. LoadBlockNames()
  650. location = {0,0,0}
  651. dir = 1
  652.  
  653. index = 1
  654.  
  655. blockData = {}
  656. nbtReader.read()
  657.  
  658. printDebug("done",true)--]]
  659.  
  660. function assignSlots(sx,sy,sz)
  661.     local bAssigned = {}
  662.     slots = {0,0,0,0,0,0,0,0,0}
  663.     bCount = 0
  664.    
  665.     for y=0,height-1,1 do
  666.         if bCount == 9 then break end
  667.         for x=startPos[1],startPos[1]-schemW+1,-1 do
  668.        
  669.             if bCount == 9 then break end
  670.             for z=startPos[3],startPos[3]-schemH+1,-1 do
  671.            
  672.                 if bCount == 9 then break end
  673.                 local blockLoc = getMapLoc({x,y,z})
  674.                 local blockID = getBlock(blockLoc[1],blockLoc[2],blockLoc[3]) --blockData[blockLoc[2]][blockLoc[3]][blockLoc[1]]
  675.                 if blockID ~= nil and blockID ~= 0 and bAssigned[blockID] == nil then
  676.                     for i=1,9,1 do
  677.                         if slots[i] == 0 then
  678.                             slots[i] = blockID
  679.                             bAssigned[blockID] = true
  680.                             bCount = bCount + 1
  681.                             break
  682.                         end
  683.                     end
  684.                 end
  685.             end
  686.         end
  687.     end
  688.  
  689. end
  690.  
  691.  
  692.  
  693. function pulseSides()
  694.     local sides = rs.getSides()
  695.     for i,v in pairs(sides) do
  696.         rs.setOutput(v,true)
  697.     end
  698.     sleep(1)
  699.     for i,v in pairs(sides) do
  700.         rs.setOutput(v,false)
  701.     end
  702. end
  703.  
  704. function doRefill(goback)
  705.     tempStat1 = status1
  706.     reportStatus("Refilling",KEEP)
  707.     backPos = getLocCopy()
  708.     backDir = dir
  709.     moveTo("y",getLocCopy(),{0,1,0})
  710.    
  711.     --moveTo("x>z>y",startPos,{0,1,0})
  712.     for i=1,9,1 do
  713.         if slots[i] ~= 0 then
  714.             if turtle.getItemCount(i) < 64 then
  715.                
  716.                 moveTo("x>z>y",matRefillPos[slots[i]])
  717.                
  718.                 pulseSides()
  719.                
  720.                 moveTo("y",matRefillPos[slots[i]],{0,1,0})
  721.          
  722.             end
  723.         end
  724.     end
  725.     --moveTo("x>z>y",startPos)
  726.     if goback then
  727.         moveTo("y",getLocCopy(),{0,backPos[2]+1,0})
  728.         moveTo("z>x>y",backPos)
  729.         faceDir(backDir)
  730.     else
  731.         moveTo("x>z>y",startPos)
  732.     end
  733.     reportStatus(tempStat1,"KEEP")
  734. end
  735.  
  736.  
  737.  
  738.  
  739. function subTab(tabA,tabB)
  740.     return {tabA[1]-tabB[1],tabA[2]-tabB[2],tabA[3]-tabB[3]}
  741. end
  742.  
  743. function reportStatus(nStat1,nStat2)
  744.     if nStat1 == nil then nStat1 = "" end
  745.     if nStat2 == nil then nStat2 = "" end
  746.     if nStat1 ~= "KEEP" then
  747.         status1 = nStat1
  748.         clearLine(5)
  749.         term.setCursorPos(2,5)
  750.         write("Status: ".. status1)
  751.        
  752.     end
  753.     if nStat2 ~= "KEEP" then
  754.         status2 = nStat2
  755.         clearLine(6)
  756.         term.setCursorPos(2,6)
  757.         write(status2)
  758.     end
  759. end
  760.  
  761. function buildRow(z,y)
  762.     reportStatus("Row " .. z .. " / " .. y, "" )
  763.     for z=1,schemH,1 do
  764.        --local cLoc = subTab(getLocCopy(),startPos)
  765.        cLoc = getLocCopy()
  766.        cLoc[2] = cLoc[2] - 1
  767.        --print(cLoc[1].."/"..cLoc[2].."/"..cLoc[3])
  768.        local mapLoc = getMapLoc(cLoc)
  769.        --print(mapLoc[1].."/"..mapLoc[2].."/"..mapLoc[3])
  770.        local blockID = getBlock(mapLoc[1],mapLoc[2],mapLoc[3])
  771.  
  772.         reportStatus("KEEP",getBlockName(blockID))
  773.         found=-1
  774.         foundEmpty =-1
  775.        
  776.         if blockID ~= 0 then
  777.             while found == -1 do
  778.                 for i=1,9,1 do
  779.                     if slots[i] == blockID then
  780.                        
  781.                         if turtle.getItemCount(i) > 0 then
  782.                             found = i
  783.                             break
  784.                         else
  785.                             foundEmpty = i
  786.                         end
  787.                     end
  788.                 end
  789.                 if found == -1 then
  790.                     if foundEmpty ~= -1 then
  791.                         tempStat1 = status1
  792.                         tempStat2 = status2
  793.                         reportStatus("Refilling:",getBlockName(blockID))
  794.                         doRefill(true)
  795.                         reportStatus(tempStat1,tempStat2)
  796.                        
  797.                     end
  798.                 end
  799.             end
  800.             if found == -1 then
  801.                 turtle.select(foundEmpty)
  802.             else
  803.                 turtle.select(found)
  804.             end
  805.             turtle.placeDown()
  806.         else
  807.            
  808.         end
  809.         gui.animStep()
  810.         placedBlocks = placedBlocks + 1
  811.         printProgressBar(placedBlocks,length*width*height,10,true)
  812.         moveForward()
  813.     end
  814. end
  815. --[[
  816. blockList = getDistinctBlocks(blockData)
  817. uniqueBlockCount = 0
  818. for i,v in pairs(blockList) do
  819.     if blockList[i] ~= nil then
  820.         uniqueBlockCount = uniqueBlockCount + 1
  821.     end
  822. end
  823.  
  824. term.clear()
  825. term.setCursorPos(1,1)
  826.  
  827. printBorder()
  828. printGUIPositionStart()
  829. startPos = locationSetRequest(9)
  830.  
  831. buildToLeft = true
  832.  
  833. term.clear()
  834. term.setCursorPos(1,1)
  835. printBorder()
  836. printGUIBuildDirection()
  837.  
  838. while true do
  839.   local ev,p1 = os.pullEvent()
  840.   if ev == "key" then
  841.     if p1 == 203 then
  842.       buildToLeft = true
  843.       printGUIBuildDirectionRect()
  844.       --left
  845.     elseif p1 == 205 then
  846.       buildToLeft = false
  847.       printGUIBuildDirectionRect()
  848.       --right
  849.     elseif p1 == 28 then
  850.       schemH = length
  851.       schemW = width
  852.       schemWAxis = "x"
  853.       schemHAxis = "z"
  854.       schemOrigin = getListCopy(startPos)
  855.       schemOrigin[3] = schemOrigin[3] - schemH
  856.       if buildToLeft then
  857.         schemOrigin[1] = schemOrigin[1] - schemW
  858.       end
  859.       break
  860.     end
  861.    
  862.   end
  863. end
  864.  
  865. term.clear()
  866. term.setCursorPos(1,1)
  867. printBorder()
  868. printCenteredText(3,"..Calculating slots..")
  869. assignSlots()
  870. matRefillPos = {}
  871. local slotRefillCounter = 1
  872.  
  873. for i,v in pairs(blockList) do
  874.  
  875.     printGUIPositionRefill(i,slotRefillCounter)
  876.     matRefillPos[i] = locationSetRequest(9)
  877.     slotRefillCounter = slotRefillCounter + 1
  878. end
  879.  
  880. term.clear()
  881. term.setCursorPos(1,1)
  882. printBorder()
  883. printCenteredText(3,"Press <Enter> to start")
  884. while true do
  885.     local ev,p1 = os.pullEvent()
  886.     if ev == "key" then
  887.         if p1 == 28 then
  888.             break
  889.         end
  890.     end
  891. end
  892.  
  893. term.clear()
  894. term.setCursorPos(1,1)
  895. printBorder()
  896.  
  897.  
  898. term.clear()
  899. term.setCursorPos(1,1)
  900. printBorder()
  901. printCenteredText(3,"Building")
  902. term.setCursorPos(2,5)
  903. term.write("Status")
  904. term.setCursorPos(2,9)
  905. term.write("Progress")
  906. placedBlocks = 0
  907. doRefill(false)
  908. moveTo("y>x>z",startPos,{0,1,0})
  909. faceOriginNorth()
  910. up = true
  911. printCenteredText(3,"Ready to Build")
  912. printCenteredText(4,"<Hit Enter Key>")
  913. read()
  914. for y=1,height,1 do
  915.     for x=1,schemW,1 do
  916.        
  917.         buildRow(x,y)
  918.         up = not up
  919.         if buildToLeft then
  920.             faceOriginWest()
  921.         else
  922.             faceOriginEast()
  923.         end
  924.         moveForward()
  925.         if up then
  926.             faceOriginNorth()
  927.         else
  928.             faceOriginSouth()
  929.         end
  930.         moveForward()
  931.     end
  932.     up = true
  933.     moveTo("x>z>y",startPos,{0,y+1,0})
  934.     faceOriginNorth()
  935.    
  936.    
  937. end
  938.  
  939. term.clear()
  940. term.setCursorPos(1,1)
  941. --]]
  942. ----
  943.  
  944.  
  945. term.clear()
  946.  
  947. processing.init()
  948.  
  949. gui.Drawing.printBorder()
  950. local tArgs = { ... }
  951. processing.fileName = tArgs[1]
  952. processing.cont = false
  953. if processing.fileName == nil then
  954.     if processing.checkTemp() then
  955.         processing.fileName = processing.getTempName()
  956.         procesing.cont = true
  957.     end
  958. end
  959.  
  960. gui.setState(guiStates.startup)
  961.  
  962. while true do
  963.     local startTick = os.clock()
  964.    
  965.     if nbtReader.active then
  966.         if not nbtReader.tick() then
  967.             nbtReader.done()
  968.         end
  969.     end
  970.     gui.tick()
  971.    
  972.     --if mover.enabled then
  973.     --  mover.tick();
  974.     --end
  975.     if nbtReader.active == true then
  976.         processing.tickLock = false
  977.         if startTick + tickLength > os.clock() then
  978.             os.startTimer(startTick+tickLength - os.clock())
  979.         else
  980.             --if mover enabled
  981.             os.queueEvent("tick")
  982.         end
  983.     else
  984.         processing.tickLock = true
  985.     end
  986.     while true do
  987.         local ev,p1 = os.pullEvent()
  988.         if ev == "timer" then
  989.             os.queueEvent("tick")
  990.         elseif ev == "tick" then
  991.  
  992.             break
  993.         elseif ev == "key" then
  994.             processing.internalKeyEvent(p1,nil)
  995.         elseif ev == "char" then
  996.             processing.internalKeyEvent(nil,p1)
  997.         end
  998.        
  999.     end
  1000.    
  1001.    
  1002. end
Advertisement
Add Comment
Please, Sign In to add comment