Advertisement
Inksaver

mobkiller

Sep 8th, 2023 (edited)
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.32 KB | Source Code | 0 0
  1. version = 20230908.1400
  2. --[[
  3.     https://pastebin.com/xmzXQpyR
  4.     Last edited: see version YYYYMMDD.HHMM
  5.     use ' pastebin get xmzXQpyR mobkiller.lua ' in turtle terminal
  6.     For use on a Crafting Melee Turtle at the exit of a spawner farm
  7. ]]
  8.  
  9. local function trim(text)
  10.     --[[ trim leading and trailing spaces ]]
  11.     return (text:gsub("^%s*(.-)%s*$", "%1"))
  12. end
  13.  
  14. local function isRepairedBow(slot)
  15.     -- eg bow: data.nbt = "552887824c43124013fd24f6edcde0fb"
  16.     local data = turtle.getItemDetail(slot)
  17.     if data ~= nil then
  18.         if data.nbt == "552887824c43124013fd24f6edcde0fb" then -- fully repaired bow
  19.             print("Repaired bow in slot "..slot)
  20.             return true
  21.         end
  22.     end
  23.     return false
  24. end
  25.  
  26. local function clear()
  27.     term.clear()
  28.     term.setCursorPos(1, 1)
  29. end
  30.  
  31. local function checkLabel()
  32.     if os.getComputerLabel() == nil then
  33.         local noname = true
  34.         while noname do
  35.             clear()
  36.             print("Type a name for this computer (no spaces):_")
  37.             name = read()
  38.             if name == '' then
  39.                 print("Just pressing Enter does not work")
  40.             elseif name:find(' ') ~= nil then
  41.                 print("NO SPACES!")
  42.             else
  43.                 noname = false
  44.             end
  45.             if noname then
  46.                 sleep(2)
  47.             end
  48.         end
  49.        
  50.         os.setComputerLabel(name)
  51.         print("Computer label set to "..os.getComputerLabel())
  52.     end
  53. end
  54.  
  55. local function getBlockType(direction)
  56.     --[[ turtle.inspect() returns two values
  57.         1) boolean (true/false) success
  58.         2) table with two or more values:
  59.         .name (string) e.g. "minecraft:log"
  60.         .state {axis = "y"}
  61.         .tags (["minecraft:logs"] = true, ["minecraft:logs_that_burn"] = true, ["minecraft:oak_logs"] = true}
  62.     ]]
  63.     local Inspect  = turtle.inspect
  64.  
  65.     if direction == "top" then
  66.         Inspect  = turtle.inspectUp
  67.     elseif direction == "bottom" then
  68.         Inspect  = turtle.inspectDown
  69.     end
  70.    
  71.     local success, data = Inspect() -- store information about the block ahead in a table
  72.     if success then                 -- block found
  73.         return data.name, data      -- eg "minecraft:chest", table
  74.     end
  75.    
  76.     return "", {}                   -- eg "" , empty table
  77. end
  78.  
  79. local function getAttackDirection()
  80.     local directions = {"up", "forward", "down"}    -- table of attack directions
  81.     while true do                                   -- display menu of choices
  82.         clear()                                     -- clear terminal
  83.         print("Attack directions:")
  84.         print(" 1. up")
  85.         print(" 2. forward")
  86.         print(" 3. down")
  87.         write("Which direction? (1, 2 or 3) " )
  88.         local response = read()
  89.         if response == "1" or response == "2" or response == "3" then -- verified response
  90.             return directions[tonumber(response)]   -- attack direction returned
  91.         end
  92.     end
  93. end
  94.  
  95. local function getMobType()
  96.     local mobs = {"zombie", "skeleton", "spider", "blaze"}  -- table of spawner mobs
  97.     while true do                       -- display menu of choices
  98.         clear()                         -- clear terminal
  99.         print("Mob Spawner type:")
  100.         print(" 1. Zombie")
  101.         print(" 2. Skeleton")
  102.         print(" 3. Spider")
  103.         print(" 4. Blaze")
  104.         write("Which mob? (1, 2, 3 or 4) " )
  105.         local response = read()
  106.         if response == "1" or response == "2" or response == "3" or response == "4" then
  107.             return mobs[tonumber(response)] -- mob type returned
  108.         end
  109.     end
  110. end
  111.  
  112. local function createStorage()
  113.     local storage = {}
  114.     storage.top      = false
  115.     storage.bottom   = false
  116.     storage.left     = false
  117.     storage.right    = false
  118.     storage.forward  = false
  119.     storage.back     = false
  120.     storage.onHopper = false
  121.    
  122.     local lib = {}
  123.    
  124.     function lib.addStorage(direction, position)
  125.         local blockType = getBlockType(direction)
  126.         if blockType:find("chest") ~= nil or blockType:find("barrel") ~= nil then
  127.             storage[position] = true
  128.             print("Storage found: "..position)
  129.             print("storage."..position.. " = "..tostring(storage[position]))
  130.         end
  131.     end
  132.    
  133.     if getBlockType("down"):find("hopper") ~= nil then
  134.         storage.onHopper = true
  135.         print("Hopper found below.")
  136.         print("Mob drops will fill it automatically")
  137.     end
  138.     -- direction can be "top", "bottom" or "forward" equivalent to Up(), Down() and ()
  139.     -- position can be "top", "bottom", "forward" "left", "right", "back"
  140.     lib.addStorage("top", "top")
  141.     lib.addStorage("bottom", "bottom")
  142.     lib.addStorage("forward", "forward")
  143.     turtle.turnRight()
  144.     lib.addStorage("forward", "right") 
  145.     turtle.turnRight()
  146.     lib.addStorage("forward", "back")
  147.     turtle.turnRight()
  148.     lib.addStorage("forward", "left")
  149.     turtle.turnRight()
  150.    
  151.     return storage
  152. end
  153.  
  154. local function emptyInventory(storage)
  155.     -- left and right storage for special items only
  156.     local Drop = turtle.drop
  157.     if storage.bottom then
  158.         Drop = turtle.dropDown
  159.     elseif storage.top then
  160.         Drop = turtle.dropUp
  161.     end
  162.     for slot = 1, 16 do
  163.         if turtle.getItemCount(slot) > 0 then
  164.             turtle.select(slot)
  165.             Drop()
  166.         end
  167.     end
  168. end
  169.  
  170. local function processSkeletonFarm(storage, storageFull)
  171.     turtle.select(1)
  172.     local bowCount = 0
  173.     local bowSlot = 0
  174.     for slot = 1, 16 do
  175.         if turtle.getItemCount(slot) > 0 then
  176.             local data = turtle.getItemDetail(slot)
  177.             if data.name:find("bow") == nil then    -- not a bow
  178.                 turtle.select(slot)
  179.                 turtle.dropDown()
  180.             else
  181.                 if slot == 4 or slot == 8 or slot >= 12 then
  182.                     turtle.select(slot)
  183.                     turtle.transferTo(9)
  184.                     bowSlot = 9
  185.                 end
  186.                 bowCount = bowCount + 1
  187.                 bowSlot = slot
  188.             end
  189.         end
  190.     end
  191.     -- may be 1 or more bows left in inventory
  192.     if storageFull then
  193.         print("Bow storage full")
  194.         print("Refuelling with excess bows")
  195.         while turtle.refuel() do end
  196.         bowCount = 0
  197.     end
  198.     if bowCount == 2 then
  199.         print("Attempting to repair bows")
  200.         turtle.select(1)
  201.         if turtle.craft() then
  202.             if isRepairedBow(1) then
  203.                 print("Bow repair successful")
  204.                 if storage.left then
  205.                     turtle.turnLeft()
  206.                     if turtle.drop() then
  207.                         bowCount = 0
  208.                     else
  209.                         storageFull = true
  210.                     end
  211.                     turtle.turnRight()
  212.                 elseif storage.right then
  213.                     turtle.turnRight()
  214.                     if turtle.drop() then
  215.                         bowCount = 0
  216.                     else
  217.                         storageFull = true
  218.                     end
  219.                     turtle.turnLeft()
  220.                 else
  221.                     if turtle.dropDown() then
  222.                         bowCount = 0
  223.                     else
  224.                         storageFull = true
  225.                     end
  226.                 end
  227.             else
  228.                 print("Partial bow repair successful")
  229.             end
  230.         end
  231.     end
  232.     if bowCount > 2 and bowSlot > 0 then
  233.         turtle.select(bowSlot)
  234.         turtle.refuel()
  235.         turtle.select(1)
  236.         bowSlot = 0
  237.     end
  238.     turtle.select(1)
  239.     return storageFull
  240. end
  241.  
  242. local function getDrops(storage)
  243.     if not storage.forward then     -- no storage in front
  244.         turtle.suck()
  245.     end
  246.     if not storage.top then         -- no storage above
  247.         turtle.suckUp()
  248.     end
  249.     if not storage.bottom and not storage.onHopper then -- no storage or hopper below
  250.         turtle.suckDown()
  251.     end
  252. end
  253.  
  254. local function attack(storage, direction, mobType)
  255.     local Attack = turtle.attack
  256.     if direction == "up" then
  257.         Attack = turtle.attackUp
  258.     elseif direction == "down" then
  259.         Attack = turtle.attackDown
  260.     end
  261.     local count = 0
  262.     local storageFull = false
  263.    
  264.     while true do
  265.         if Attack() then
  266.             count = count + 1
  267.             clear()
  268.             print("Attack direction "..direction.." count: "..count)
  269.         end
  270.         getDrops(storage)
  271.         if storage.bottom then                  -- if chest or barrel below
  272.             if mobType == "skeleton" then
  273.                 storageFull = processSkeletonFarm(storage, storageFull)
  274.             else
  275.                 emptyInventory(storage)
  276.             end
  277.         end
  278.     end
  279. end
  280.  
  281. local function getConfig()
  282.     if fs.exists("attack.txt") then
  283.         local h = fs.open("attack.txt", "r")
  284.         local attackDirection = trim(h.readLine())
  285.         local mobType = trim(h.readLine())
  286.         h.close()
  287.         print("File 'attack.txt' read:")
  288.         return attackDirection, mobType
  289.     else
  290.         return "", ""
  291.     end
  292. end
  293.  
  294. local function writeConfig(attackDirection, mobType)
  295.     local h = fs.open("attack.txt", "w")
  296.     h.writeLine(attackDirection)        --
  297.     h.writeLine(mobType)                -- "zombie, "skeleton", "spider", "blaze"
  298.     h.close()
  299.     print("Config.txt file written")
  300. end
  301.  
  302. local function main()
  303.     clear()
  304.     checkLabel()                                            -- make sure turtle label is set
  305.     local storage = createStorage()                         -- create and populate storage table
  306.     local attackDirection, mobType = getConfig()            -- if attack.txt is available
  307.     if attackDirection == "" then                           -- not in a text file
  308.         attackDirection = getAttackDirection()              -- get attackDirection             
  309.         mobType = getMobType()                              -- get mob spawner type
  310.         writeConfig(attackDirection, mobType)               -- write to text file
  311.     end
  312.     print("Attack direction: "..attackDirection)            -- display to player fo 3 seconds
  313.     print("Mob spawner type: "..mobType)
  314.     sleep(3)
  315.     attack(storage, attackDirection, mobType)               -- run attack loop
  316. end
  317.  
  318. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement