Ragy

smkr

May 5th, 2012
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.75 KB | None | 0 0
  1. --[[
  2. Ragy's scheme creating program
  3. Warning: After converting into scheme you're bilding will be destroyed.
  4. Warning: Turtle can't recognise block types in inventory, this program work with inventory slots
  5. My email is [email protected]
  6. ]]--
  7. function fSetDirection(sDirection)
  8.     if(sMyDirection=="+y") then
  9.         if(sDirection=="+y") then
  10.         end
  11.         if(sDirection=="-y") then
  12.         turtle.turnRight()
  13.         turtle.turnRight()
  14.         sMyDirection="-y"
  15.         end
  16.         if(sDirection=="+x") then
  17.         turtle.turnRight()
  18.         sMyDirection="+x"
  19.         end
  20.         if(sDirection=="-x") then
  21.         turtle.turnLeft()
  22.         sMyDirection="-x"      
  23.         end
  24.     end
  25.     if(sMyDirection=="-y") then
  26.         if(sDirection=="+y") then
  27.         turtle.turnRight()
  28.         turtle.turnRight()
  29.         sMyDirection="+y"
  30.         end
  31.         if(sDirection=="-y") then
  32.         end
  33.         if(sDirection=="+x") then
  34.         turtle.turnLeft()      
  35.         sMyDirection="+x"
  36.         end
  37.         if(sDirection=="-x") then
  38.         turtle.turnRight()
  39.         sMyDirection="-x"
  40.         end
  41.     end
  42.     if(sMyDirection=="+x") then
  43.         if(sDirection=="+y") then
  44.         turtle.turnLeft()
  45.         sMyDirection="+y"
  46.         end
  47.         if(sDirection=="-y") then
  48.         turtle.turnRight()
  49.         sMyDirection="-y"
  50.         end
  51.         if(sDirection=="+x") then
  52.         end
  53.         if(sDirection=="-x") then
  54.         turtle.turnRight()
  55.         turtle.turnRight()
  56.         sMyDirection="-x"
  57.         end
  58.     end
  59.     if(sMyDirection=="-x") then
  60.         if(sDirection=="+y") then
  61.         turtle.turnRight()
  62.         sMyDirection="+y"
  63.         end
  64.         if(sDirection=="-y") then
  65.         turtle.turnLeft()
  66.         sMyDirection="-y"
  67.         end
  68.         if(sDirection=="+x") then
  69.         turtle.turnRight()
  70.         turtle.turnRight()
  71.         sMyDirection="+x"
  72.         end
  73.         if(sDirection=="-x") then
  74.         end
  75.     end
  76. end
  77.  
  78. function fMove(sDirection,iDistance)
  79.     if(iDistance==0) then
  80.         return
  81.     end
  82.     while(iDistance<0) do
  83.         if(sDirection=="+y") then
  84.             sDirection="-y"
  85.             iDistance=(iDistance^2)^0.5
  86.             break
  87.         end
  88.         if(sDirection=="-y") then
  89.             sDirection="+y"
  90.             iDistance=(iDistance^2)^0.5
  91.             break
  92.         end
  93.         if(sDirection=="+x") then
  94.             sDirection="-x"
  95.             iDistance=(iDistance^2)^0.5
  96.             break
  97.         end
  98.         if(sDirection=="-x") then
  99.             sDirection="+x"
  100.             iDistance=(iDistance^2)^0.5
  101.             break
  102.         end
  103.         if(sDirection=="+z") then
  104.             sDirection="-z"
  105.             iDistance=(iDistance^2)^0.5
  106.             break
  107.         end
  108.         if(sDirection=="-z") then
  109.             sDirection="+z"
  110.             iDistance=(iDistance^2)^0.5
  111.             break
  112.         end
  113.     end
  114.    
  115.     if(sDirection=="+y" or sDirection=="-y" or sDirection=="+x" or sDirection=="-x") then
  116.         fSetDirection(sDirection)
  117.         if(bWriting==true) then
  118.             fSetBlock()
  119.         end
  120.         for n=1,iDistance,1 do
  121.             while(turtle.forward()~=true) do
  122.                 if(sMode=="replace") then
  123.                     turtle.dig()       
  124.                 end
  125.                 if(sMode=="peacefull") then
  126.                     fChangeMode("forward")
  127.                     turtle.dig()
  128.                 end
  129.             end
  130.             if(sDirection=="+y") then
  131.             y=y+1
  132.             end
  133.             if(sDirection=="-y") then
  134.             y=y-1
  135.             end
  136.             if(sDirection=="+x") then
  137.             x=x+1
  138.             end
  139.             if(sDirection=="-x") then
  140.             x=x-1
  141.             end
  142.         end
  143.     end
  144.     if(sDirection=="+z") then
  145.         for n=1,iDistance,1 do
  146.             while(turtle.up()~=true) do
  147.                 if(sMode=="replace") then
  148.                     turtle.digUp()
  149.                 end
  150.                 if(sMode=="peacefull") then
  151.                     fChangeMode("up")
  152.                     turtle.digUp()                     
  153.                 end
  154.             end
  155.         end
  156.     end
  157.     if(sDirection=="-z") then
  158.         for n=1,iDistance,1 do
  159.             while(turtle.down()~=true) do
  160.                 if(sMode=="replace") then
  161.                     turtle.digDown()   
  162.                 end
  163.                 if(sMode=="peacefull") then
  164.                     fChangeMode("down")
  165.                     turtle.digDown()                       
  166.                 end
  167.             end
  168.         end
  169.     end
  170. end
  171.  
  172. function fGoToPos(iNewX,iNewY,iNewZ)
  173.     if(iNewX==nil) then
  174.         iNewX=x
  175.     end
  176.     if(iNewY==nil) then
  177.         iNewY=y
  178.     end
  179.     if(iNewZ==nil) then
  180.         iNewZ=z
  181.     end
  182.     iNewX=iNewX-x
  183.     iNewY=iNewY-y
  184.     iNewZ=iNewZ-z
  185.     fMove("+x",iNewX)
  186.     fMove("+y",iNewY)
  187.     fMove("+z",iNewZ)
  188. end
  189.  
  190. function fTurtleGetBlockType()
  191.     if(turtle.detect()==true) then
  192.         if(iEarlyerType~=nil) then
  193.             turtle.select(iEarlyerType)
  194.             if(turtle.compare()==true) then
  195.                 return(tAllowedBlocks[iEarlyerType][1])
  196.             end
  197.         end    
  198.         local n=1
  199.         for n=1,9,1 do
  200.             turtle.select(n)
  201.             if(turtle.compare()==true) then
  202.                 iEarlyerType=n
  203.                 return(tAllowedBlocks[n][1])               
  204.             end
  205.             n=n+1
  206.         end
  207.         return(tAllowedBlocks[1][1])
  208.     end
  209. end
  210.  
  211. function fSetBlock()
  212.     local sBlock=fTurtleGetBlockType()
  213.     if(sMyDirection=="+y" and iBuildingZonelength>=(y+1)) then
  214.         if(sBlock~=nil) then
  215.             tLayer[y+1][x]=sBlock
  216.             return
  217.         else           
  218.             tLayer[y+1][x]="e"
  219.             return
  220.         end
  221.     end
  222.     if(sMyDirection=="-y" and (y-1)>=1) then
  223.         if(sBlock~=nil) then
  224.             tLayer[y-1][x]=sBlock
  225.             return
  226.         else
  227.             tLayer[y-1][x]="e"
  228.             return
  229.         end
  230.     end
  231.     if(sMyDirection=="+x" and (x+1)<=iBuildingZoneWidth) then
  232.         if(sBlock~=nil) then
  233.             tLayer[y][x+1]=sBlock
  234.             return
  235.         else
  236.             tLayer[y][x+1]="e"
  237.             return
  238.         end
  239.     end
  240.     if(sMyDirection=="-x" and (y-1)>=1) then
  241.         if(sBlock~=nil) then
  242.             tLayer[y][x-1]=sBlock
  243.             return
  244.         else
  245.             tLayer[y][x-1]="e"
  246.             return
  247.         end
  248.     end
  249. end
  250.  
  251. function fWriteScheme(iLayerId)
  252.     fileScheme:write("Layer "..iLayerId.."\n")
  253.     local g=1
  254.     for n=1,iBuildingZonelength,1 do
  255.         fileScheme:write(string.reverse(tLayer[g]).."\n")
  256.         g=g+1
  257.     end
  258.     fileScheme:write("end")
  259.     if(iLayerId~=iBuildingZoneHeight) then
  260.         fileScheme:write("\n")
  261.     end
  262. end
  263.  
  264. function fTabToString(tTab)
  265.     local sStr=""
  266.     for i,v in pairs(tTab) do sStr=sStr..v end
  267.     --print(sStr)
  268.     return(sStr)
  269. end
  270.  
  271.  
  272.  
  273. local tArgs = { ... }
  274. if #tArgs < 1 then
  275.     print( "Usage: smkr <scheme name>")
  276.     return
  277. end
  278.  
  279. sSchemeName=tArgs[1]
  280. tAllowedBlocks={}
  281. x=1
  282. y=0
  283. z=1
  284. sMyDirection="+y"
  285. sMode="replace"
  286. bWriting=false
  287. iEarlyerType=1
  288.  
  289. term.clear()
  290. term.setCursorPos(1,1)
  291.  
  292. if(fs.exists("/"..sSchemeName)==true) then
  293.     while(true) do
  294.         term.clear()
  295.         term.setCursorPos(1,1)
  296.         print("This file already exists. Do you want replace it?")
  297.         write("y/n:")
  298.         sInput=read()  
  299.         if(sInput=="y") then
  300.         break
  301.         end
  302.         if(sInput=="n") then
  303.         return
  304.         end
  305.     end
  306. end
  307. if(true) then
  308.     print("Input Description")
  309.     local sInput=read()
  310.     fileScheme=io.open("/"..sSchemeName,"w")
  311.     fileScheme:write("Turtle building scheme\n"..sSchemeName.."\n"..sInput.."\n")
  312.     local n=1
  313.     tAllowedBlocks={{"b","stone"},{"b","stone"},{"b","stone"},{"b","stone"},{"b","stone"},{"b","stone"},{"b","stone"},{"b","stone"},{"b","stone"}}
  314.     while(true) do
  315.         term.clear()
  316.         term.setCursorPos(1,1)
  317.         print("Do you want use standart block types?")
  318.         local v=1
  319.         for n=1,9,1 do
  320.             print(tAllowedBlocks[v][1].."-"..tAllowedBlocks[v][2].." slot "..v)
  321.             v=v+1
  322.         end
  323.         write("y/n:")
  324.         sInput=read()
  325.         if(sInput=="y") then
  326.             break
  327.         end
  328.         if(sInput=="n") then
  329.             for n=1,9,1 do
  330.                 local sInput
  331.                 while(true) do
  332.                     term.clear()
  333.                     term.setCursorPos(1,1)
  334.                     print("Input block id for slot "..n.." or input (default) to set rest of blocks to standart block types")
  335.                     sInput=read()
  336.                     if(sInput=="default") then
  337.                         break                      
  338.                     end
  339.                     if(#sInput==1) then
  340.                         break
  341.                     end
  342.                     if(#sInput~=1) then
  343.                         print("For block id can only be used one letter")
  344.                         os.sleep(2)
  345.                     end
  346.                 end
  347.                 if(sInput=="default") then
  348.                     break                      
  349.                 end
  350.                 tAllowedBlocks[n][1]=sInput
  351.                 print("Input block name ")     
  352.                 sInput=read()
  353.                 tAllowedBlocks[n][2]=sInput
  354.                 n=n+1
  355.             end
  356.             break
  357.         end
  358.     end
  359.     term.clear()
  360.     term.setCursorPos(1,1)
  361.     local v=1
  362.     for n=1,9,1 do
  363.         print("Put "..tAllowedBlocks[v][2].." in slot "..v)
  364.         v=v+1
  365.     end
  366.     print("Press any key")
  367.     os.pullEvent("key")
  368.     while(true) do
  369.         local bItemsInSlot=true
  370.         local n=1
  371.         for n=1,9,1 do
  372.             local iItemsInSlot=turtle.getItemCount(n)
  373.             if(iItemsInSlot<1) then
  374.                 bItemsInSlot=false
  375.                 break              
  376.             end
  377.             n=n+1
  378.         end
  379.         if(bItemsInSlot==false) then
  380.             term.clear()
  381.             term.setCursorPos(1,1)
  382.             print("Put "..tAllowedBlocks[n][2].." in slot "..n.." and press any key")
  383.             os.pullEvent("key")
  384.         end
  385.         if(bItemsInSlot==true) then
  386.             break
  387.         end
  388.     end
  389.     n=1
  390.     local sAllowedBlocks=""
  391.     for n=1,9,1 do 
  392.         sAllowedBlocks=sAllowedBlocks..tAllowedBlocks[n][1]..","..tAllowedBlocks[n][2]
  393.         if(n~=9) then
  394.             sAllowedBlocks=sAllowedBlocks.."|"
  395.         end
  396.         n=n+1
  397.     end
  398.     fileScheme:write(sAllowedBlocks.."\n")
  399.     while(true) do
  400.         term.clear()
  401.         term.setCursorPos(1,1)
  402.         print("Input building zone length")
  403.         iBuildingZonelength=read()+0   
  404.         if(iBuildingZonelength~=nil and iBuildingZonelength>0) then
  405.             break
  406.         end
  407.     end
  408.     while(true) do
  409.         term.clear()
  410.         term.setCursorPos(1,1)
  411.         print("Input building zone width")
  412.         iBuildingZoneWidth=read()+0
  413.         if(iBuildingZoneWidth~=nil and iBuildingZoneWidth>0) then
  414.             break
  415.         end
  416.     end
  417.     while(true) do
  418.         term.clear()
  419.         term.setCursorPos(1,1)
  420.         print("Input building zone height")
  421.         iBuildingZoneHeight=read()+0
  422.         if(iBuildingZoneHeight~=nil and iBuildingZoneHeight>0) then
  423.             break
  424.         end
  425.     end
  426.     term.clear()
  427.     term.setCursorPos(1,1)
  428.     print("Scheme creating in progress")
  429.     local k=1  
  430.     for n=1,iBuildingZoneHeight,1 do
  431.         print("Processing layer "..k)
  432.         local m=1
  433.         local sDir="right"
  434.         bWriting=true
  435.         tLayer={}
  436.         local j=1
  437.         for n=1,iBuildingZonelength,1 do
  438.             tLayer[j]={}
  439.             j=j+1
  440.         end
  441.         for n=1,iBuildingZonelength,1 do
  442.             local h=1
  443.             fMove("+y",1)          
  444.             for n=1,iBuildingZoneWidth-1,1 do              
  445.                 if(sDir=="right") then
  446.                     fMove("+x",1)
  447.                 else
  448.                     fMove("-x",1)
  449.                 end
  450.                 h=h+1
  451.             end
  452.             if(sDir=="right") then
  453.                 sDir="left"
  454.             else
  455.                 sDir="right"
  456.             end
  457.             m=m+1
  458.         end
  459.         --for i,v in pairs(tLayer) do
  460.         --  for d,s in pairs(v) do
  461.         --  print(s)
  462.         --  end
  463.         --end
  464.         g=1
  465.         for n=1,iBuildingZonelength,1 do
  466.             tLayer[g]=fTabToString(tLayer[g])
  467.             g=g+1
  468.         end
  469.         fWriteScheme(k)
  470.         bWriting=false
  471.     --for i,v in ipairs(tLayer[5]) do print(i.." ",v) end
  472.         fGoToPos(1,0)
  473.         fMove("+z",1)
  474.         k=k+1
  475.     end
  476. end
  477. fileScheme:close()
  478. term.clear()
  479. term.setCursorPos(1,1)
  480. print("Scheme creating completed")
  481. print("Now you can find you're scheme at "..".minecraft\\saves\\(You're world)\\computer\\"..os.getComputerID())
  482. fGoToPos(1,0)
  483. fMove("-z",z-1)
Advertisement
Add Comment
Please, Sign In to add comment