kLeeNex24

3dBuilder

Aug 10th, 2013
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.63 KB | None | 0 0
  1. pos = { x=0, y=0, z=0,
  2. facing = 0,  --clockwise
  3. iter = 0
  4. }
  5. iterCounter = 0
  6. filename = "3dBuilder.txt"
  7. Xdir = true
  8. Ydir = true
  9. wasPlacedUp = 0
  10. wasPlacedDown = 0
  11. firstFloor = 0
  12. getRoundX = false
  13. continue = false
  14. currDebugMess=1
  15. debugMess = {"","","","","","","","","","","","",""}
  16.  
  17. function sendToDebug(str)
  18.   debugMess[currDebugMess] = str
  19.   if currDebugMess < #debugMess then currDebugMess=currDebugMess+1
  20.   else currDebugMess = 1 end
  21. end
  22.  
  23. function printDebug()
  24.   term.clear()
  25.   messNum=currDebugMess
  26.   for i=1,#debugMess do
  27.     term.setCursorPos(1,i)
  28.     term.write(debugMess[messNum])
  29.     if messNum < #debugMess then messNum=messNum+1
  30.     else messNum = 1 end
  31.   end
  32. end
  33.  
  34. function save()
  35.   file=fs.open(filename, "w")
  36.   file.write(textutils.serialize(pos))
  37.   file.close()
  38. end
  39.  
  40. function wantToContinue()
  41.   if fs.exists(filename) then
  42.     term.clear()
  43.     term.setCursorPos(1,1)
  44.     file=fs.open(filename, "r")
  45.     data = textutils.unserialize(file.readAll())
  46.     file.close()
  47.     print("There are unfinished project. Information:")
  48.     print("Relative position (x,y,z) = "..data.x..","..data.y..","..data.z)
  49.     facing = "forward"
  50.     if data.facing == 1 then facing = "right"
  51.     elseif data.facing == 2 then facing = "back"
  52.     elseif data.facing == 3 then facing = "left" end
  53.     print("Relative facing = " .. facing)
  54.     print("Iteration = "..data.iter)
  55.     print("Are you want to continue? Then place me on that position and press y.")
  56.     print("Else press n. It will destroy all saved data!")
  57.     answer = read()
  58.     if answer == "n" then
  59.       fs.delete(filename)
  60.     else
  61.       pos.x=data.x pos.y=data.y pos.z=data.z pos.facing=data.facing pos.iter=data.iter
  62.       continue = true
  63.     end
  64.   end
  65. end
  66.  
  67. function sendAlarm(text)
  68.  term.clear()
  69.  term.setCursorPos(1,1)
  70.  print(text)
  71.  print("Press Enter to continue")
  72.  read()
  73.  term.clear()
  74. end
  75.  
  76. function sortInv()
  77.  for i=1,16 do
  78.   itemCount = turtle.getItemCount(i)
  79.   if turtle.getItemSpace(i) ~= 0 then
  80.     for j=16,i+1,-1 do
  81.       turtle.select(i)
  82.       if turtle.getItemCount(j)~=0 and (turtle.getItemCount(i) == 0 or turtle.compareTo(j)) then
  83.         turtle.select(j)
  84.         turtle.transferTo(i)
  85.         if turtle.getItemSpace(i) == 0 then break end
  86.       end
  87.      end
  88.     end
  89.    if turtle.getItemCount(i) == 0 then break end
  90.   end
  91.   turtle.select(1)
  92. end
  93.  
  94. function cantPlace(n)--side, 0=up, 1=down, 2=front
  95.   sortInv()
  96.   if turtle.getItemCount(1) == 0 then toChest()
  97.   else
  98.     side = ""
  99.     if n==0 and turtle.detectUp() then
  100.       side = "up"
  101.     elseif n==1 and turtle.detectDown() then
  102.       side = "down"
  103.     elseif n==2 and turtle.detect() then
  104.       side = "front"
  105.     end
  106.     if side ~= "" then
  107.       sendAlarm("I cant place the block in " .. side .. " direction!")
  108.     end
  109.   end
  110. end
  111.  
  112. function cantGo(n) --direction, 0-up, 1-down, 2-forward
  113.   error = "I cant go "
  114.   direction = "forward. "
  115.     if n == 0 then direction = "up. " elseif n == 1 then direction = "down. " end
  116.   if turtle.getFuelLevel() == 0 then
  117.     sortInv()
  118.     turtle.select(16)
  119.     repeat    
  120.       sendAlarm(error..direction.."Put fuel to my 16 slot plz.")
  121.       turtle.refuel()
  122.     until turtle.getFuelLevel() ~= 0
  123.     turtle.select(1)
  124.   else
  125.     sendAlarm(error..direction.."Fuel level="..turtle.getFuelLevel())
  126.   end
  127. end
  128.  
  129.  
  130.  
  131. function forward(n)
  132.  for i=1,n do
  133.   success = false
  134.   repeat
  135.     success = turtle.forward()
  136.     if success == false then cantGo(2) end
  137.   until success == true
  138.   if pos.facing == 0 then pos.x = pos.x + 1
  139.   elseif pos.facing == 1 then pos.y = pos.y + 1
  140.   elseif pos.facing == 2 then pos.x = pos.x - 1
  141.   else pos.y = pos.y - 1 end
  142.  end
  143.  wasPlacedUp = 0
  144.  wasPlacedDown = 0
  145. end
  146.  
  147. function up(n)
  148.  for i=1,n do
  149.   success = false
  150.   repeat
  151.     success = turtle.up()
  152.     if success == false then cantGo(0) end
  153.   until success == true
  154.   pos.z = pos.z + 1
  155.  end
  156. end
  157.  
  158. function down(n)
  159.  for i=1,n do
  160.   success = false
  161.   repeat
  162.     success = turtle.down()
  163.     if success == false then cantGo(1) end
  164.   until success == true
  165.   pos.z = pos.z - 1
  166.  end
  167. end
  168.  
  169. function turnLeft()
  170.  turtle.turnLeft()
  171.  pos.facing = pos.facing - 1
  172.  if pos.facing < 0 then pos.facing = 3 end
  173. end
  174.  
  175. function turnRight()
  176.  turtle.turnRight()
  177.  pos.facing = pos.facing + 1
  178.  if pos.facing > 3 then pos.facing = 0 end
  179. end
  180.  
  181. function placeUp()
  182.  if turtle.placeUp() == false then cantPlace(0) turtle.placeUp() end
  183. end
  184.  
  185. function placeDown()
  186.  if turtle.placeDown() == false then cantPlace(1) turtle.placeDown() end
  187. end
  188.  
  189. function place()
  190.  if turtle.place() == false then cantPlace(2) turtle.place() end
  191. end
  192.  
  193. function placeMid()
  194.  forward(1)
  195.  turnLeft()
  196.  turnLeft()
  197.  place()
  198. end
  199.  
  200. function setFacing(n)
  201.  if pos.facing ~= n then
  202.  k = n - pos.facing
  203.   if k < 0 then k = k + 4 end
  204.   if k < 3 then
  205.    while n ~= pos.facing do turnRight() end
  206.   else
  207.    while n ~= pos.facing do  turnLeft() end
  208.   end
  209.  end
  210. end
  211.  
  212. function setFacingX(bool)
  213.  if bool then
  214.    if Xdir then setFacing(0) else setFacing(2) end
  215.  else
  216.    if Xdir then setFacing(2) else setFacing(0) end
  217.  end
  218. end
  219.  
  220. function setFacingY(bool)
  221.  if bool then
  222.    if Ydir then setFacing(1) else setFacing(3) end
  223.  else
  224.    if Ydir then setFacing(3) else setFacing(1) end
  225.  end
  226. end
  227.  
  228. function useChest()
  229.   while true do
  230.     repeat
  231.       i = 0
  232.       sortInv()
  233.       success = true  
  234.       while success do
  235.         success = turtle.suckDown()
  236.         i = i + 1
  237.       end
  238.     until i == 1
  239.     if turtle.getItemCount(1) == 0 then
  240.       sendAlarm("I need the blocks. Put them somewhere plz.")
  241.     else break end
  242.   end
  243. end
  244.  
  245. function goZ(z)
  246.    if pos.z ~= z then
  247.     if z>pos.z then
  248.       if wasPlacedUp==1 then setFacingY(true) forward(1) end
  249.       up(z-pos.z)
  250.     else
  251.       if wasPlacedDown==1 then setFacingY(true) forward(1) end
  252.       down(pos.z-z) end
  253.    end
  254. end
  255.  
  256. function goX(x)
  257.     if pos.x < x then
  258.       setFacing(0)
  259.     else
  260.       setFacing(2)
  261.     end
  262.   forward(math.abs(x - pos.x))
  263. end
  264.  
  265. function goY(y)
  266.    if pos.y ~= y then
  267.     if pos.y < y then
  268.       setFacing(1)
  269.     else
  270.       setFacing(3)
  271.     end
  272.     forward(math.abs(y - pos.y))
  273.    end
  274. end
  275.  
  276. function goto(x,y,z)
  277.   sendToDebug("["..pos.x..","..pos.y..","..pos.z.."] --> ["..x..","..y..","..z.."]")
  278.   sendToDebug("Xdir="..tostring(Xdir)..", Ydir="..tostring(Ydir))
  279.  
  280.  if x == pos.x then  
  281.   goZ(z) goY(y)
  282.  else
  283.   if (pos.x < x and Xdir == false) or (pos.x > x and Xdir) then
  284.    if Xdir then goZ(pos.z+2) getRoundX = true goX(x) goY(y) goZ(z)
  285.    else goZ(z) goY(y) goX(x) end
  286.   else
  287.    if Xdir then
  288.      if getRoundX then goZ(z+2) goY(y) setFacingY(true) forward(1) goX(x) goZ(z) setFacingY(false) forward(1) getRoundX=false
  289.      else goZ(z) goX(x) goY(y) end
  290.    else goX(x) goY(y) goZ(z) end
  291.   end
  292.  end
  293. end
  294.  
  295. function toChest()
  296.   oldPos = { x=pos.x,y=pos.y,z=pos.z,facing=pos.facing }
  297.   goto(-1,0,1)
  298.   useChest()
  299.   goto(oldPos.x,oldPos.y,oldPos.z)
  300.   setFacing(oldPos.facing)
  301. end
  302.  
  303. function updateXdir(z)  
  304.     z = z + 1
  305.     level = 1
  306.     if firstFloor == 0 then
  307.        surplus = z%3
  308.        level = (z-surplus)/3
  309.        if surplus ~= 0 then level = level + 1 end
  310.     elseif z-1>firstFloor then
  311.        mainFloors = z - firstFloor
  312.        surplus = mainFloors%3
  313.        level = (mainFloors-surplus)/3 + 1
  314.        if surplus ~= 0 then level = level + 1 end
  315.     end
  316.     if level%2 ~=0 then Xdir = true else Xdir = false end
  317.     --sendToDebug("Xdir="..Xdir..", level="..level..",fF="..firstFloor)
  318. end
  319.  
  320. function updateYdir(x)
  321.   surplus = x%2
  322.   if (surplus==0 and Xdir) or (surplus ~= 0 and Xdir == false) then Ydir = true else Ydir = false end
  323. end
  324.  
  325. function g(x,y,z,_placeUp,_placeDown,_placeMid)
  326.   if continue then
  327.     iterCounter = iterCounter+1
  328.     if iterCounter >= pos.iter then continue = false
  329.       updateXdir(z)
  330.       updateYdir(x)
  331.       setFacingY(true)
  332.       wasPlacedUp = _placeUp
  333.       wasPlacedDown = _placeDown      
  334.     end
  335.   else
  336.    needToUpdXdir = false
  337.    if z~=pos.z then needToUpdXdir = true end
  338.    goto(x,y,z)
  339.    sendToDebug("need to place (up,down,mid) = ".. _placeUp .. ",".._placeDown..",".._placeMid)
  340.    if needToUpdXdir then  updateXdir(z) end
  341.    updateYdir(x)
  342.    setFacingY(true)
  343.    if _placeUp == 1 then placeUp() end
  344.    wasPlacedUp = _placeUp
  345.    if _placeDown == 1 then placeDown() end
  346.    wasPlacedDown = _placeDown
  347.    if _placeMid == 1 then placeMid() end
  348.    pos.iter = pos.iter + 1
  349.    save()
  350.   end
  351. end
  352.  
  353. function viewDebug()
  354.   while true do
  355.     event, scancode = os.pullEvent("key")
  356.     if scancode == 32 then printDebug() end --"D"
  357.   end
  358. end
  359.  
  360. function main()
  361. wantToContinue()
  362. --MAIN CODE HERE
  363. end
  364.  
  365. parallel.waitForAny(main,viewDebug)
Advertisement
Add Comment
Please, Sign In to add comment