Skaruts

ComputerCraft bridge Program [wip] v1.0

Aug 21st, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.63 KB | None | 0 0
  1. -- get arguments into the array
  2. local tArgs = { ... }
  3.  
  4. if #tArgs < 1 or #tArgs > 2 then
  5.     term.clear()
  6.     term.setCursorPos(1,1)
  7.     print ( "Incorrect arguments!" )
  8.     print ( "--------------------")
  9.     print ( "Syntax: bridge <length> <width>" )
  10.     print ( "Both values are mandatory." )
  11.     print ( "" )
  12.     print ( "Type \"bridge ?\" or \"bridge help\" for more information." )
  13.     return
  14. elseif (#tArgs < 2) and (tArgs[1] == '?' or tArgs[1] == 'help' or tArgs[1] == 'halp') then
  15.     term.clear()
  16.     term.setCursorPos(1,1)
  17.     print ( "Syntax: bridge <length> <width>" );
  18.     print ( "" )
  19.     print ( "Example:" )
  20.     print ( "--------" )
  21.     print ( "\"bridge 20 15\"" )
  22.     print ( "" )
  23.     print ( "Creates a 20 long, 15 wide platform." )
  24.     print ( "Both values are mandatory." )
  25.     return
  26. end
  27.  
  28. -- define length and width based on arguments
  29. local length = tonumber( tArgs[1] )
  30. local width = tonumber( tArgs[2] )
  31.  
  32. -- helper variables
  33. local slot = 1
  34. local row = 1
  35.  
  36.  
  37. -- functions
  38. --------------
  39.  
  40. local function placeBlock()
  41.     while not turtle.placeDown() do
  42.         -- if turtle can't place block check if it can dig down
  43.         if not turtle.digDown() then
  44.            
  45.             -- if turtle can't dig down check other slots for building materials
  46.             slot = slot + 1
  47.             -- if it reaches slot 16 and not building materials are found
  48.             -- stop and wait for player to refill it
  49.             if slot > 16 then
  50.                 slot = 1
  51.                 term.clear()
  52.                 term.setCursorPos(1,1)
  53.                 print ("WARNING: No Blocks in my inventory.")
  54.                 print ("Please provide more materials to build.")
  55.                 print("")
  56.                 print (" - Press <Enter> to continue operation")
  57.                 print (" - Hold Ctrl + T to cancel operation.")
  58.                 input = read("*")
  59.             end
  60.             turtle.select(slot)
  61.         end
  62.     end
  63. end
  64.  
  65. local function moveForward()
  66.     while not turtle.forward() do
  67.         -- if turtle can't move forward, check fuel levels
  68.         fuel = turtle.getFuelLevel()
  69.         -- if no fuel
  70.         if fuel < 1 then
  71.             -- stop and wait for fuel
  72.             while not turtle.refuel() do
  73.                 term.clear()
  74.                 term.setCursorPos(1,1)
  75.                 print("WARNING: Fuel level is ".. fuel)
  76.                 print("Waiting for fuel...")
  77.             end
  78.         end
  79.     end
  80. end
  81.  
  82. local function makeTurn()
  83.    
  84.             turtle.forward()
  85.         if row % 2 == 0 then
  86.             -- if even number turn left
  87.             turtle.turnLeft()
  88.             turtle.forward()
  89.             turtle.turnLeft()
  90.         else
  91.             -- if not even, turn left
  92.             turtle.turnRight()
  93.             turtle.forward()
  94.             turtle.turnRight()
  95.         end
  96.         turtle.forward()
  97.         row = row + 1
  98.    
  99. end
  100.  
  101.  
  102. -- the job
  103. ------------
  104.  
  105. turtle.select(slot)
  106. turtle.forward()
  107.  
  108. for j = 1, width do
  109.     for i= 1, length do
  110.         placeBlock()
  111.         if i~= length then
  112.             moveForward()
  113.         end
  114.     end
  115.     if j ~= width then 
  116.         makeTurn()
  117.     end
  118. end
Advertisement
Add Comment
Please, Sign In to add comment