jille_Jr icon

CC: 9 by 9 house builder

jille_Jr | PRO | 10/24/12 01:43:01 PM UTC | 0 ⭐ | 313 👁️ | Never ⏰ | []
Lua |

4.87 KB

|

None

|

0 👍

/

0 👎

-- 9 by 9 house
-- by jag_e_nummer_ett
 
local tArgs = {...}
 
function table_contains(table, element)
  for _, value in pairs(table) do
    if value == element then
      return true
    end
  end
  return false
end
 
function printUsage()
  print("Usage: "..shell.getRunningProgram().." <option> [setting]")
  print("Options:")
  print("- 0: Door in front of turtle")
  print("- 1: Left corner in front of turtle")
  print("- 2: Include a bridge")
  print("Settings:")
  print("- Bridge length\n + (Requres Option to be 2)")
end
 
function selectItem()
  for a = 1, 16 do
    if turtle.getItemCount(a) > 0 then
      turtle.select(a)
      break
    end
  end
end
 
function goUp()
  while not turtle.up() do
    if not turtle.digUp() then
      turtle.attackUp()
    end
    sleep(.5)
  end
end
 
function goDown()
  while not turtle.down() do
    if not turtle.digDown() then
      turtle.attackDown()
    end
    sleep(.5)
  end
end
 
function buildBelow()
  if not turtle.detectDown() then
    selectItem()
    while not turtle.placeDown() do
      if not turtle.digDown() then
        turtle.attackDown()
      end
      sleep(.5)
      selectItem()
    end
  end
end
 
function buildFront()
  if not turtle.detect() then
    while not turtle.place() do
      if not turtle.dig() then
        turtle.attack()
      end
      sleep(.5)
      selectItem()
    end
  end
end
 
function buildTop()
  if not turtle.detectUp() then
    while not turtle.placeUp() do
      if not turtle.digUp() then
        turtle.attackUp()
      end
      sleep(.5)
      selectItem()
    end
  end
end
 
function goForward()
  while not turtle.forward() do
    if not turtle.dig() then
      turtle.attack()
    end
    sleep(.5)
  end
end
 
function bridge(count)
  count = tonumber(count)
  if count < 1 then
    return false
  end
  turtle.turnLeft()
  goForward()
  turtle.turnRight()
  goForward()
  for b = 1, count do
    if b % 2 == 0 then
      turtle.turnLeft()
    else
      turtle.turnRight()
    end
    for e = 1, 3 do
      buildBelow()
      if e ~= 3 then
        goForward()
      end
    end
    if b % 2 == 0 then
      turtle.turnRight()
      if b == count then
        turtle.turnRight()
        goForward()
      end
      goForward()
    else
      turtle.turnLeft()
      goForward()
      if b == count then
        turtle.turnLeft()
        goForward()
        for aa = 1,2 do
          turtle.turnLeft()
        end
      end
    end
  end
end
 
function houseFloor()
  for h = 1, 9 do
    if h % 2 == 0 then
      for j = 1, 9 do
        buildBelow()
        if j ~= 9 then
          goForward()
        end
      end
      if h ~= 9 then
        turtle.turnRight()
        goForward()
        turtle.turnRight()
      end
    else
      for i = 1, 9 do
        buildBelow()
        if i ~= 9 then
          goForward()
        end
      end
      if h ~= 9 then
        turtle.turnLeft()
        goForward()
        turtle.turnLeft()
      end
    end
  end
  for aj = 1, 2 do
    turtle.turnLeft()
  end
end
 
function houseWall(door)
  for t = 1, 4 do
    for u = 1, 8 do
      if t == 2 and
      u == 5 and
      door == true then
        goForward()
      else
        buildBelow()
        goForward()
      end
    end
    turtle.turnLeft()
  end
end
 
function houseRoof()
  local skyLights = {
    3,4,
    6,7,
  }
  for y = 1,9 do
    for x = 1,9 do
      if not table_contains(skyLights,x) or
      not table_contains(skyLights,y) then
        buildBelow()
      end
      if x ~= 9 then
        goForward()
      else
        if y % 2 == 0 and y ~= 9 then
          turtle.turnRight()
          goForward()
          turtle.turnRight()
        elseif y % 2 ~= 0 and y ~= 9 then
          turtle.turnLeft()
          goForward()
          turtle.turnLeft()
        end
      end
    end
  end
end
 
function buildHouse(ac)
  if ac == 0 then
    turtle.turnRight()
  end
  for ad = 1,4 do
    goForward()
  end
  turtle.turnLeft()
  houseFloor()
  goUp()
  for ae = 1,2 do
    houseWall(true)
    goUp()
  end
  for ae = 1,3 do
    houseWall()
    goUp()
  end
  houseRoof()
  goForward()
  for af = 1,6 do
    goDown()
  end
  turtle.turnRight()
  for ag = 1,4 do
    goForward()
  end
  turtle.turnRight()
  goForward()
  for ah = 1,2 do
    turtle.turnRight()
  end
end
 
if #tArgs < 1 then
  printUsage()
  return
end
 
tArgs[1] = tonumber(tArgs[1])
tArgs[2] = tonumber(tArgs[2])
 
if tArgs[1] == 0 then
  buildHouse(0)
elseif tArgs[1] == 1 then
  turtle.turnLeft()
  for ab = 1,4 do
    goForward()
  end
  buildHouse(1)
elseif tArgs[1] == 2 then
  if type(tArgs[2]) ~= "number" then
    printUsage()
    return
  else
    bridge(tArgs[2])
    buildHouse(1)
  end
else
  printUsage()
  return
end

Comments