Glitchies

platformI

Jan 11th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.01 KB | None | 0 0
  1. --{program="tPlatform",version="1.01",date="2013-11-09",basicFunc="1.10",basicFuncDate="2013-10-30",addFunc="0.00"",addFuncDate="n.a."}
  2. ---------------------------------------
  3. -- tPlatform           by Kaikaku
  4. -- 2013-11-02, v1.00   initial
  5. -- 2013-11-09, v1.01   more compact
  6. ---------------------------------------
  7. -- basic functions: v1.10, 2013-10-30
  8. -- additional func: n.a.
  9. ---------------------------------------
  10.  
  11. ---------------------------------------
  12. ---- PLANS AND IDEAS SO FAR -----------
  13. ---------------------------------------
  14. --done) build platforms dependant on
  15. --      user input
  16. --   2) build walls
  17. --   3) build roofs (i.e. diagonally)
  18. --   4) execute multiple commands
  19. --   5) maybe the final version could
  20. --      be used as API?
  21. --   6) easy to use interface
  22. --   7) use of a resupply chest
  23. --   8) use of an enderchest
  24. --   9) error handling
  25. --  10) ...
  26.  
  27. ---------------------------------------
  28. ---- ASSUMPTIONS/PRECONDITIONS --------
  29. ---------------------------------------
  30. --   Turtle movement:
  31. --   - building space must be empty
  32.  
  33. ---------------------------------------
  34. ---- PARAMETERS -----------------------
  35. ---------------------------------------
  36. local cSleepTime=10
  37.  
  38. ---------------------------------------
  39. ---- VARIABLES ------------------------
  40. ---------------------------------------
  41. local userX=3 userY=1
  42. local blnAskForParameters=true
  43. local blnDirectionX=true
  44. local currentSlot=1
  45.  
  46. ---------------------------------------
  47. ---- tArgs ----------------------------
  48. ---------------------------------------
  49. local tArgs = {...}
  50. if #tArgs == 2 then -- no error check
  51.   blnAskForParameters=false
  52.   userX=tonumber(tArgs[1])
  53.   userY=tonumber(tArgs[2])
  54. end
  55.  
  56. ---------------------------------------
  57. -- basic functions for turtle control -
  58. ---------------------------------------
  59. local function mats()
  60.   if turtle.getItemCount(currentSlot)==0 then
  61.     currentSlot=currentSlot+1
  62.     if currentSlot>16 then
  63.       currentSlot=1
  64.       print("Out of materials, please restock!")
  65.       print("  Sleeping for "..cSleepTime.." sec ...")
  66.       os.sleep(cSleepTime)
  67.     end  
  68.     turtle.select(currentSlot)
  69.     mats()
  70.   end
  71. end
  72.  
  73. local function gf()  while not turtle.forward()   do end end
  74. local function gb()  while not turtle.back()      do end end
  75. local function gu()  while not turtle.up()        do end end
  76. local function gd()  while not turtle.down()      do end end
  77. local function gl()  while not turtle.turnLeft()  do end end
  78. local function gr()  while not turtle.turnRight() do end end
  79. local function df()  turtle.dig()       end
  80. local function du()  turtle.digUp()     end
  81. local function dd()  turtle.digDown()   end
  82. local function pf()  mats() while not turtle.place()     do end end
  83. local function pu()  mats() while not turtle.placeUp()   do end end
  84. local function pd()  mats() while not turtle.placeDown() do end end
  85. local function sf()  turtle.suck()      end
  86. local function su()  turtle.suckUp()    end
  87. local function sd()  turtle.suckDown()  end
  88. local function Df()  turtle.drop()      end
  89. local function Du()  turtle.dropUp()    end
  90. local function Dd()  turtle.dropDown()  end
  91. local function ss(s) turtle.select(s)   end
  92.  
  93. local function waitKey(strText)
  94.   local event, scancode
  95.   write(strText)
  96.   event, scancode = os.pullEvent("key")
  97.   print()
  98. end
  99.  
  100. ---------------------------------------
  101. -- additional functions ---------------
  102. ---------------------------------------
  103.  
  104. function checkFuel() --checks if there is enough fuel and refuels when neded.
  105.  
  106.         if turtle.getFuelLevel()<10 then  --if you are low on fuel
  107.  
  108.                 print("refueling")
  109.                 turtle.select(14)  --select the enderchest
  110.                 turtle.placeUp()  --place the enderchest above you
  111.                 turtle.suckUp() --suck some fuel from the above enderchest into the current selected slot. (you cannot specify any amount or from which slot.)
  112.                 turtle.refuell(10)  --refuel with one piece of fuel
  113.                 turtle.dropUp()  --dump the rest of the fuel back in the enderchest. (can be left out of the code.)
  114.                 turtle.digUp()  --digs up the enderchest above you and places it in your current selected iventory.
  115.  
  116.         else  --if you are not low on fuel.
  117.  
  118.                 --print("not low on fuel, carrying on with business. mining lasers n stuff.")
  119.  
  120.         end
  121. end
  122. -- n.a.
  123.  
  124. ------------------------------------------------------------------------------
  125. -- main ----------------------------------------------------------------------
  126. ------------------------------------------------------------------------------
  127.  
  128. -- step 0 usage hints
  129. term.clear() term.setCursorPos(1,1)
  130. print("+-------------------------------------+")
  131. print("| tPlatform, by Kaikaku               |")
  132. print("+-------------------------------------+")
  133. print("| Put in building materials in any    |")
  134. print("|   slot(s) and press key.            |")
  135. print("| Platform size: Enter x and y to     |")
  136. print("|   determine size. Either when asked |")
  137. print("|   by program or with function call, |")
  138. print("|   e.g., tPlatform 5 10              |")
  139. print("| If turtle runs out of materials it  |")
  140. print("|   waits until resupplied.           |")
  141. print("+-------------------------------------+")
  142.  
  143. -- step 1 get input
  144. if blnAskForParameters then
  145.   waitKey("Put in materials + press key!")
  146.   -- step 1.1 get x
  147.   write("Enter depth x (default&min=3):")
  148.   userX=read()
  149.   if userX==nil or userX=="" then userX=3 end
  150.   userX=tonumber(userX) -- no error check yet
  151.   if userX<3 then userX=3 end
  152.    
  153.   -- step 1.2 get y
  154.   write("Enter width y (default&min=1):")
  155.   userY=read()
  156.   if userY==nil or userY=="" then userY=3 end
  157.   userY=tonumber(userY) -- no error check yet
  158.   --if userY<2 then userY=2 end
  159. end  
  160. userX=math.floor(userX)
  161. userY=math.floor(userY)
  162.  
  163. -- step 2 check materials
  164.    -- no check yet
  165.  
  166. -- step 3 loopy loops ;)
  167. print("Let's build something nice:")
  168. -- step 3.1 go to start position
  169. --          & if odd number go back
  170.   if userY%2==1 then
  171.     -- odd number of rows
  172.     for i=1,userX,1 do gf() end
  173.     blnDirectionX=false
  174.   else
  175.     -- even number of rows
  176.     gf() gf() gl() gl()
  177.     blnDirectionX=true
  178.   end  
  179.  
  180. -- step 3.2 build it
  181.   for iY=1,userY,1 do
  182.     for iX=1,userX-1 do
  183.       if iX==1 then
  184.         if iY~=1 then
  185.           if blnDirectionX then
  186.             gl()
  187.           else
  188.             gr()
  189.           end
  190.         end
  191.         gb() pf()      
  192.       elseif  iX==userX-1 then
  193.         if iY~=userY then      
  194.           if blnDirectionX then
  195.             -- right turn
  196.             gr()  
  197.           else
  198.             -- left turn
  199.             gl()
  200.           end    
  201.         end
  202.         gb() pf()
  203.       else -- in between start and end
  204.         gb() pf()
  205.       end
  206.  
  207.     end
  208.     blnDirectionX=not blnDirectionX
  209.   end
  210.   -- go back within 1st row
  211.   gr()
  212.   for i=1,userY-1,1 do gb() pf() end
  213.   gl() gb() pf()
  214.  
  215. print("Done. Looks nice to me ;)")
  216. os.sleep(0.4)
  217. print("***************************************")
  218. print("* Check out YouTube for more videos   *")
  219. print("* and turtle programs by Kaikaku :)   *")
  220. print("***************************************")
Advertisement
Add Comment
Please, Sign In to add comment