BillBodkin

skellyBodyguard

Aug 29th, 2019
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.98 KB | None | 0 0
  1. function file_exists(name)
  2.    local f=io.open(name,"r")
  3.    if f~=nil then io.close(f) return true else return false end
  4. end
  5.  
  6. if file_exists("follow.lua") == false then
  7.     print("Downloading follow config file...")
  8.     shell.run("pastebin", "get", "CWqtaUyW", "follow.lua")
  9. end
  10.  
  11. followFile = fs.open("follow.lua", "r")
  12. follow = textutils.unserialize(followFile.readAll())
  13. followFile.close()
  14.  
  15. m = peripheral.find("modem")
  16. m.open(4)
  17.  
  18. canShoot = false
  19. canFollow = false
  20. followClose = false--ignore formation and follow right behind
  21.  
  22. followFacePitch = 0
  23. followFaceYaw = 0
  24.  
  25. n = peripheral.wrap("back")
  26. n.disableAI()
  27. f = {}
  28. table.insert(f, "item.item.stick")
  29. table.insert(f, "Skeleton")
  30. table.insert(f, "Zombie")
  31. table.insert(f, "Creeper")
  32. table.insert(f, "Spider")
  33. table.insert(f, "Slime")
  34. table.insert(f, "Witch")
  35.  
  36. function inTable(tbl, item)
  37.     for key, value in pairs(tbl) do
  38.         if value == item then
  39.             return key
  40.         end
  41.     end
  42.     return false
  43. end
  44.  
  45. function isLookingAtFF(angle1, angle2, s)
  46.     for _, entity in pairs(s) do
  47.         Sdistance = math.sqrt(math.pow(entity.x, 2) + math.pow(entity.z, 2))
  48.        
  49.         Sangle1 = math.atan2(entity.z, entity.x) * 180 / math.pi - 90
  50.         Sangle2 = math.atan2(distance, entity.y) * 180 / math.pi - 90
  51.        
  52.         if math.abs(Sangle1 - angle1) < 5 and math.abs(Sangle1 - angle2) < 5 then
  53.             return true
  54.         end
  55.     end
  56.     return false
  57. end
  58.  
  59. function cycle()
  60.     c = 0
  61.     s = n.sense()
  62.     b = n.scan()
  63.    
  64.     for _, entity in pairs(s) do
  65.         --print(entity.displayName)
  66.        
  67.         distance = math.sqrt(math.pow(entity.x, 2) + math.pow(entity.z, 2))
  68.        
  69.         angle1 = math.atan2(entity.z, entity.x) * 180 / math.pi - 90
  70.         angle2 = math.atan2(distance, entity.y) * 180 / math.pi - 90
  71.        
  72.         m = n.getMetaByID(entity.id)
  73.         helmet = ""
  74.         if m ~= nil and distance < 15 then
  75.             if m.armor ~= nil then
  76.                 if m.armor.helmet ~= nil then
  77.                     hmd = m.armor.helmet.getMetadata()
  78.                     if hmd ~= nil then
  79.                         helmet = hmd.rawName
  80.                     end
  81.                 end
  82.             end
  83.         end
  84.        
  85.         if inTable(f, entity.displayName) and math.abs(entity.x) > 0 and math.abs(entity.z) > 0 and helmet ~= "item.plethora.neuralInterface" and n.isWalking() == false and canShoot then-- and math.abs(entity.x) > 0 and math.abs(entity.z) > 0
  86.             --print("Attacking")
  87.            
  88.             --print(textutils.serialize(entity))
  89.            
  90.             n.look(angle1, angle2 - 5)
  91.             os.sleep(0.1)
  92.             n.look(angle1, angle2 - 5)
  93.            
  94.             for i = 0, 10, 1 do
  95.                 if isLookingAtFF(angle1, angle2, s) == false then
  96.                     n.shoot(1)
  97.                 end
  98.                 os.sleep(0.05)
  99.             end
  100.            
  101.             os.sleep(1)
  102.            
  103.             c = c + 1
  104.         end
  105.     end
  106.    
  107.     for _, entity in pairs(s) do
  108.         distance = math.sqrt(math.pow(entity.x, 2) + math.pow(entity.z, 2))
  109.        
  110.         angle1 = math.atan2(entity.z, entity.x) * 180 / math.pi - 90
  111.         angle2 = math.atan2(distance, entity.y) * 180 / math.pi - 90
  112.        
  113.         m = n.getMetaByID(entity.id)
  114.        
  115.         if entity.displayName == follow["name"] and c == 0 and canFollow and n.isWalking() == false then
  116.            
  117.             if m ~= nil then
  118.                 followFacePitch = m.pitch
  119.                 followFaceYaw = m.yaw
  120.             end
  121.             --print("Walking to " .. followName)
  122.            
  123.             posX = math.cos((followFaceYaw + follow["angle"]) * math.pi/180) * follow["distance"]
  124.             posZ = math.sin((followFaceYaw + follow["angle"]) * math.pi/180) * follow["distance"]
  125.            
  126.             --if math.abs(entity.x + posX) > 1 or math.abs(entity.z + posZ) > 1 then
  127.                 --[[
  128.                 posObstructed = false
  129.                
  130.                 for _, block in pairs(b) do
  131.                     --print(textutils.serialize(block))
  132.                     if block.x == math.floor(entity.x + posX) and (block.y == math.ceil(entity.y) + 1 or block.y == math.ceil(entity.y) + 2) and block.z == math.floor(entity.z + posZ) and block.name ~= "minecraft:air" then
  133.                         posObstructed = true
  134.                         print(block)
  135.                     end
  136.                 end
  137.                
  138.                 wSuc, wInfo = n.walk(entity.x + posX, entity.y, entity.z + posZ)
  139.                 if wSuc == false or wInfo ~= nil or posObstructed then
  140.                     n.walk(entity.x, entity.y, entity.z)
  141.                 end
  142.                 ]]--
  143.                
  144.                 if followClose then
  145.                     n.walk(entity.x, entity.y, entity.z)
  146.                 else
  147.                     n.walk(entity.x + posX, entity.y, entity.z + posZ)
  148.                 end
  149.                
  150.             --end
  151.            
  152.             --if  == false then
  153.                 --n.walk(entity.x, entity.y, entity.z)
  154.             --end
  155.         end
  156.     end
  157.    
  158.     if c == 0 and n.isWalking() == false then
  159.         --n.look(math.random() * 360, 0)
  160.         n.look(followFaceYaw, followFacePitch)
  161.     end
  162. end
  163.  
  164. function commandListener()
  165.     while true do
  166.         local _, side, freq, rfreq, message = os.pullEvent('modem_message')
  167.         if message["type"] == "reboot" then
  168.             os.reboot()
  169.         elseif message["type"] == "stopFollow" then
  170.             canFollow = false
  171.         elseif message["type"] == "startFollow" then
  172.             canFollow = true
  173.         elseif message["type"] == "stopShoot" then
  174.             canShoot = false
  175.         elseif message["type"] == "startShoot" then
  176.             canShoot = true
  177.         elseif message["type"] == "followClose" then
  178.             followClose = true
  179.         elseif message["type"] == "followFormation" then
  180.             followClose = false
  181.         end
  182.     end
  183. end
  184.  
  185. function cycleLoop()
  186.     while true do
  187.         cycle()
  188.         os.sleep(0.1)
  189.     end
  190. end
  191.  
  192. parallel.waitForAll(commandListener, cycleLoop)
Advertisement
Add Comment
Please, Sign In to add comment