Visual_Studio

ComputerCraft Witherboss Farmer

Nov 24th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.74 KB | None | 0 0
  1. --constants
  2. local casing_slot_one = 1
  3. local casing_slot_two = 2
  4. local spike_slot = 3
  5.  
  6. local wither_skulls_needed = 3
  7. local soul_sand_needed = 4
  8. local casing_needed = 112
  9. local spikes_needed = 9
  10.  
  11. local redstone_direction = "left"
  12. local chest_direction = "right"
  13.  
  14. --functions
  15. function show_usage()
  16.     print("wither_farm build --> build the casing for the farm")
  17.     print("wither_farm run ----> run the actual witherboss farm")
  18. end
  19.  
  20. function pre_boss_builder()
  21.     turtle.up()
  22.     move_direction("forward", 2)
  23.     move_direction("down", 2)
  24. end
  25.  
  26. function post_boss_builder()
  27.     move_direction("back", 2)
  28.     turtle.down()
  29. end
  30.  
  31. function move_direction(direction, count)
  32.     direction = string.lower(direction)
  33.     --this is to perform more of a strafe than just moving in a given direction
  34.     --turn left/right before moving
  35.     if direction == "left" then
  36.         turtle.turnLeft()
  37.     elseif direction == "right" then
  38.         turtle.turnRight()
  39.     end
  40.  
  41.     --move in the requested direction x number of times
  42.     for x = 1, count, 1 do
  43.         if direction == "up" then
  44.             turtle.up()
  45.         elseif direction == "down" then
  46.             turtle.down()
  47.         elseif direction == "left" then
  48.             turtle.forward()
  49.         elseif direction == "right" then
  50.             turtle.forward()
  51.         elseif direction == "forward" then
  52.             turtle.forward()
  53.         elseif direction == "back" then
  54.             turtle.back()
  55.         else
  56.             print("An undefined direction string was entered!")
  57.         end
  58.     end
  59.  
  60.     --turn back left/right after moving
  61.     if direction == "left" then
  62.         turtle.turnRight()
  63.     elseif direction == "right" then
  64.         turtle.turnLeft()
  65.     end
  66. end
  67.  
  68. function compare_casing_slots()
  69.     local current = turtle.getSelectedSlot()
  70.     turtle.select(casing_slot_one)
  71.     local result = turtle.compareTo(casing_slot_two)
  72.     turtle.select(current)
  73.     return result
  74. end
  75.  
  76. function is_case_constructed()
  77.     local current = turtle.getSelectedSlot()
  78.     turtle.select(casing_slot_one)
  79.     local result = turtle.compare()
  80.     if result then
  81.         turtle.select(casing_slot_two)
  82.         result = turtle.compare()
  83.         turtle.select(current)
  84.         return result
  85.     end
  86.     return false
  87. end
  88.    
  89. function build_casing()
  90.     --first layer
  91.     turtle.select(casing_slot_one)
  92.     turtle.place() --1
  93.     turtle.up()
  94.     turtle.forward()
  95.     turtle.turnLeft()
  96.     turtle.forward()
  97.     turtle.placeDown() --2
  98.     turtle.forward()
  99.     turtle.placeDown() --3
  100.     turtle.turnRight()
  101.     turtle.forward()
  102.     turtle.placeDown() --4
  103.     turtle.forward()
  104.     turtle.placeDown() --5
  105.     turtle.forward()
  106.     turtle.placeDown() --6
  107.     turtle.forward()
  108.     turtle.placeDown() --7
  109.     turtle.turnRight()
  110.     turtle.forward()
  111.     turtle.placeDown() --8
  112.     turtle.turnRight()
  113.     turtle.forward()
  114.     turtle.placeDown() --8
  115.     turtle.forward()
  116.     turtle.placeDown() --9
  117.     turtle.forward()
  118.     turtle.placeDown() --10
  119.     turtle.forward()
  120.     turtle.placeDown() --11
  121.     turtle.turnLeft()
  122.     turtle.forward()
  123.     turtle.turnLeft()
  124.     turtle.forward()
  125.     turtle.placeDown() --12
  126.     turtle.forward()
  127.     turtle.placeDown() --13
  128.     turtle.forward()
  129.     turtle.placeDown() --14
  130.     turtle.forward()
  131.     turtle.placeDown() --15
  132.     turtle.turnRight()
  133.     turtle.forward()
  134.     turtle.placeDown() --16
  135.     turtle.turnRight()
  136.     turtle.forward()
  137.     turtle.placeDown() --17
  138.     turtle.forward()
  139.     turtle.placeDown() --18
  140.     turtle.forward()
  141.     turtle.placeDown() --19
  142.     turtle.forward()
  143.     turtle.placeDown() --20
  144.     turtle.turnLeft()
  145.     turtle.forward()
  146.     turtle.placeDown() --21
  147.     turtle.turnLeft()
  148.     turtle.forward()
  149.     turtle.placeDown() --22
  150.     turtle.forward()
  151.     turtle.placeDown() --23
  152.     turtle.forward()
  153.     turtle.placeDown() --24
  154.     turtle.forward()
  155.     turtle.placeDown() --25
  156.    
  157.     --second layer -> fifth layer
  158.     for y = 1, 5 do
  159.         turtle.up()
  160.         turtle.placeDown() --26
  161.         --top right corner
  162.         turtle.turnLeft()
  163.         for x = 1, 4 do
  164.             turtle.forward()
  165.             turtle.placeDown() --27-30
  166.         end
  167.         --top left corner
  168.         turtle.turnLeft()
  169.         for x = 1, 4 do
  170.             turtle.forward()
  171.             if turtle.getItemCount(casing_slot_one) == 0 then  --runs out at this point
  172.                 turtle.select(casing_slot_two)
  173.             end
  174.             turtle.placeDown() --30-35
  175.         end
  176.         --bottom left corner
  177.         turtle.turnLeft()
  178.         turtle.forward()
  179.         turtle.placeDown() --36
  180.         if y == 2 or y == 4 or y == 5 then  --level 2, 4, and 5 or the hollow part
  181.             turtle.forward()
  182.             turtle.placeDown()
  183.             turtle.forward()
  184.         else
  185.             for x = 1, 2 do
  186.                 turtle.forward()
  187.             end
  188.         end
  189.         turtle.placeDown() --37
  190.         turtle.forward()
  191.         turtle.placeDown() --38
  192.         --bottom right corner
  193.         turtle.turnLeft()
  194.         for x = 1, 3 do
  195.             turtle.forward()
  196.             turtle.placeDown() --38-42
  197.         end
  198.         turtle.forward()
  199.     end
  200.     turtle.turnLeft()
  201.     turtle.forward()
  202.     turtle.turnLeft()
  203.     turtle.forward()
  204.     for x = 1, 2 do
  205.         turtle.down()
  206.     end
  207.     turtle.placeUp()
  208.     turtle.forward()
  209.     turtle.placeUp()
  210.     turtle.forward()
  211.     turtle.placeUp()
  212.     turtle.turnRight()
  213.     turtle.forward()
  214.     turtle.placeUp()
  215.     turtle.turnRight()
  216.     turtle.forward()
  217.     turtle.placeUp()
  218.     turtle.forward()
  219.     turtle.placeUp()
  220.     turtle.turnLeft()
  221.     turtle.forward()
  222.     turtle.placeUp()
  223.     turtle.turnLeft()
  224.     turtle.forward()
  225.     turtle.placeUp()
  226.     turtle.forward()
  227.     turtle.placeUp()
  228.    
  229.     turtle.select(spike_slot)
  230.     turtle.down()
  231.     turtle.placeUp()
  232.     for x = 1, 2 do
  233.         turtle.turnLeft()
  234.     end
  235.     turtle.forward()
  236.     turtle.placeUp()
  237.     turtle.forward()
  238.     turtle.placeUp()
  239.     turtle.turnRight()
  240.     turtle.forward()
  241.     turtle.placeUp()
  242.     turtle.turnRight()
  243.     turtle.forward()
  244.     turtle.placeUp()
  245.     turtle.forward()
  246.     turtle.placeUp()
  247.     turtle.turnLeft()
  248.     turtle.forward()
  249.     turtle.placeUp()
  250.     turtle.turnLeft()
  251.     turtle.forward()
  252.     turtle.placeUp()
  253.     turtle.forward()
  254.     turtle.placeUp()
  255.     turtle.turnLeft()
  256.     turtle.forward()
  257.     turtle.turnLeft()
  258.     for x = 1, 4 do
  259.         turtle.forward()
  260.     end
  261.     for x = 1, 2 do
  262.         turtle.turnLeft()
  263.     end
  264.     turtle.down()
  265. end
  266.  
  267. function build_wither(wither_slot, ss_slot)
  268.     turtle.down() --move from the middle of the wither to the bottom
  269.  
  270.     --select soul sand slot
  271.     turtle.select(ss_slot)
  272.    
  273.     turtle.place() --soul sand bottom
  274.     turtle.up()
  275.     turtle.place() --soul sand middle
  276.     turtle.turnLeft()
  277.     turtle.forward()
  278.     turtle.turnRight()
  279.     turtle.place() --soul sand left
  280.     turtle.select(wither_slot)
  281.     turtle.up()
  282.     turtle.place() --wither skull left
  283.     turtle.down()
  284.    
  285.     turtle.turnRight()
  286.    
  287.     --move forward 2
  288.     move_direction("forward", 2)
  289.    
  290.     turtle.turnLeft()
  291.     turtle.select(ss_slot)
  292.     turtle.place() --soul sand right
  293.     turtle.up()
  294.     turtle.select(wither_slot)
  295.     turtle.place() --wither skull right
  296.     turtle.turnLeft()
  297.     turtle.forward()
  298.     turtle.turnRight()
  299.     turtle.place() --wither skull middle
  300. end
  301.  
  302. function drop_direction(direction, slot, amount)
  303.     local current = turtle.getSelectedSlot()
  304.     turtle.select(slot)
  305.     direction = string.lower(direction)
  306.     local success = false
  307.     if direction == "right" then
  308.         turtle.turnRight()
  309.         success = turtle.drop(amount)
  310.         turtle.turnLeft()
  311.     elseif direction == "left" then
  312.         turtle.turnLeft()
  313.         success = turtle.drop(amount)
  314.         turtle.turnRight()
  315.     elseif direction == "up" then
  316.         success = turtle.dropUp(amount)
  317.     elseif direction == "down" then
  318.         success = turtle.dropDown(amount)
  319.     elseif direction == "back" then
  320.         for x = 1, 2 do
  321.             turtle.turnLeft()
  322.         end
  323.         success = turtle.drop(amount)
  324.         for x = 1, 2 do
  325.             turtle.turnRight()
  326.         end
  327.     elseif direction == "front" then
  328.         success = turtle.drop(amount)
  329.     else
  330.         print("An undefined direction string was entered!")
  331.     end
  332.     turtle.select(current)
  333.     return success
  334. end
  335.  
  336. function valid_fuel(item_name, item_damage)
  337.     if item_name == "minecraft:coal" and (item_damage == 0 or item_damage == 1) then  --vanilla
  338.         return true
  339.     elseif item_name == "Railcraft:fuel.coke" then  --railcraft
  340.         return true
  341.     elseif item_name == "ImmersiveEngineering:material" and item_damage == 6 then  --immersive engineering
  342.         return true
  343.     end
  344.     return false
  345. end
  346.  
  347. function table_length(T)
  348.     local count = 0
  349.     for _ in pairs(T) do count = count + 1 end
  350.     return count
  351. end
  352.  
  353. function determine_slots()
  354.     local soul_sand_found = false
  355.     local soul_sand_slot = 0
  356.     local wither_skulls_found = false
  357.     local wither_skull_slot = 0
  358.     local fuel_slot = 0
  359.     for x = 4, 16 do  --loop through all slots
  360.         local item_info = turtle.getItemDetail(x)
  361.         if item_info ~= nil then  --valid item
  362.             local item_name = item_info.name
  363.             local item_damage = item_info.damage
  364.             if item_name == "minecraft:skull" and item_damage == 1 then  --wither skulls
  365.                 wither_skulls_found = true
  366.                 wither_skull_slot = x
  367.             elseif item_name == "minecraft:soul_sand" then  --soul sand
  368.                 soul_sand_found = true
  369.                 soul_sand_slot = x
  370.             elseif valid_fuel(item_name, item_damage) then
  371.                 fuel_slot = x
  372.             end
  373.         end
  374.     end
  375.     if soul_sand_found and wither_skulls_found then
  376.         local response = {wither_skull_slot, soul_sand_slot}
  377.         if fuel_slot > 0 then
  378.             response[3] = fuel_slot
  379.         end
  380.         return response
  381.     end
  382.     return false
  383. end
  384.  
  385. function refuel_turtle(count)
  386.     local current = turtle.getSelectedSlot()
  387.     turtle.select(slot)
  388.     turtle.refuel(count)
  389.     turtle.select(current)
  390. end
  391.  
  392. --arguments
  393. local command = ...
  394. if command then
  395.     command = string.lower(command)
  396. else
  397.     show_usage()
  398.     return
  399. end
  400.  
  401. --variables
  402. --slot variables
  403. local wither_skull_slot = 0
  404. local soul_sand_slot = 0
  405. local fuel_slot = 0
  406.  
  407. --handle commands
  408. if command == "test" then  --tests for debugging
  409.     determine_slots()
  410. elseif command == "build" then  --build casing
  411.     if not is_case_constructed() then
  412.         if compare_casing_slots() then
  413.             local casing_count_one = turtle.getItemCount(casing_slot_one)
  414.             local casing_count_two = turtle.getItemCount(casing_slot_two)
  415.             local total_casing_count = casing_count_one + casing_count_two
  416.             local spike_count = turtle.getItemCount(spike_slot)
  417.             if total_casing_count >= casing_needed then
  418.                 if spike_count >= spikes_needed then
  419.                     print("Building witherboss farm casing (this may take a while)...")
  420.                     build_casing()
  421.                     print("Done!")
  422.                 else
  423.                     print(tostring(spike_count) .. "/" .. tostring(spikes_needed) .. " Spike Blocks")
  424.                 end
  425.             else
  426.                 print(tostring(total_casing_count) .. "/" .. tostring(casing_needed) .. " Casing Blocks")
  427.             end
  428.         else
  429.             print("Casing materials aren't the same!")
  430.         end
  431.     else
  432.         print("Casing is already constructed!")
  433.     end
  434. elseif command == "run" then  --run the actual wither builder
  435.     --1.5 second loop to wait on a redstone signal
  436.     local announced = false
  437.     while true do
  438.         --determine slots every 1.5 seconds
  439.         local fuel_found = false
  440.         local slot_response = determine_slots()
  441.         if type(slot_response) == "table" then
  442.             wither_skull_slot = slot_response[1]
  443.             soul_sand_slot = slot_response[2]
  444.             if table_length(slot_response) == 3 then
  445.                 fuel_slot = slot_response[3]
  446.                 fuel_found = true
  447.             end
  448.         else
  449.             print("Soul sand and/or wither skulls not found!")
  450.             return
  451.         end
  452.  
  453.         --variables
  454.         local soul_sand_count = turtle.getItemCount(soul_sand_slot)
  455.         local wither_skull_count = turtle.getItemCount(wither_skull_slot)
  456.        
  457.         local wither_skull_remainder = wither_skull_count % wither_skulls_needed
  458.         local soul_sand_remainder = soul_sand_count % soul_sand_needed
  459.        
  460.         --refuel if low fuel level
  461.         if fuel_found then
  462.             local fuel_count = turtle.getItemCount(fuel_slot)
  463.             if turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 1 and fuel_count > 0 then
  464.                 print("Refueling turtle...")
  465.                 refuel_turtle(fuel_count)
  466.                 print("Done!")
  467.             end
  468.         end
  469.        
  470.         --errors with divisibility in wither skulls correction
  471.         if wither_skull_remainder > 0 and wither_skull_count > wither_skulls_needed then
  472.             print("Wither skulls count not divisible by " .. tostring(wither_skulls_needed) .. "! Fixing...")
  473.             drop_direction(chest_direction, wither_skull_slot, wither_skull_remainder)
  474.             wither_skull_count = wither_skull_count - wither_skull_remainder
  475.             wither_skull_remainder = 0
  476.             print("Wither skull divisibility error fixed!")
  477.         end
  478.        
  479.         --error with divisibility in soul sand correction
  480.         if soul_sand_remainder > 0 and soul_sand_count > soul_sand_needed then
  481.             print("Soul sand count not divisible by " .. tostring(soul_sand_needed) .. "! Fixing...")
  482.             drop_direction(chest_direction, soul_sand_slot, soul_sand_remainder)
  483.             soul_sand_count = soul_sand_count - soul_sand_remainder
  484.             soul_sand_remainder = 0
  485.             print("Soul sand divisibility error fixed!")
  486.         end
  487.  
  488.         --wait on a redstone signal on the back
  489.         if redstone.getInput(redstone_direction) == true then
  490.             if wither_skull_count > 0 and soul_sand_count > 0 and wither_skull_remainder == 0 and soul_sand_remainder == 0 then
  491.                 --reset announced
  492.                 announced = false
  493.                
  494.                 --find out how many wither bosses it can generate
  495.                 total_with_skulls = wither_skull_count / wither_skulls_needed
  496.                 total_with_ss = soul_sand_count / soul_sand_needed
  497.                 if total_with_skulls == total_with_ss then
  498.                     --print the amount the turtle is able to spawn
  499.                     print(tostring(total_with_skulls) .. " Wither Bosses Left!")
  500.                    
  501.                     --run pre-boss build tasks
  502.                     pre_boss_builder()
  503.  
  504.                     --begin building the wither from the bottom up and ending with the central wither skull
  505.                     build_wither(wither_skull_slot, soul_sand_slot)
  506.                    
  507.                     --run post-boss build tasks
  508.                     post_boss_builder()
  509.                    
  510.                     print("Wither Boss Spawned, Prepare Yourself!")
  511.                 else
  512.                     if total_with_skulls > total_with_ss then
  513.                         local deficit = (total_with_skulls - total_with_ss) * wither_skulls_needed
  514.                         drop_direction("right", wither_skull_slot, deficit)
  515.                         print("The soul sand deficit has been corrected!")
  516.                     elseif total_with_ss > total_with_skulls then
  517.                         local deficit = (total_with_ss - total_with_skulls) * soul_sand_needed
  518.                         drop_direction("right", soul_sand_slot, deficit)
  519.                         print("The wither skull deficit has been corrected!")
  520.                     end
  521.                 end
  522.             else
  523.                 if not announced then
  524.                     print("Slot 1 = wither skulls (minimum of 3)!")
  525.                     print("Slot 2 = soul sand (minimum of 4)!")
  526.                     announced = true
  527.                 end
  528.             end
  529.         end
  530.         sleep(1.5) --sleep because why not?
  531.     end
  532. else
  533.     show_usage()
  534. end
Advertisement
Add Comment
Please, Sign In to add comment