Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function file_exists(name)
- local f=io.open(name,"r")
- if f~=nil then io.close(f) return true else return false end
- end
- if file_exists("follow.lua") == false then
- print("Downloading follow config file...")
- shell.run("pastebin", "get", "CWqtaUyW", "follow.lua")
- end
- followFile = fs.open("follow.lua", "r")
- follow = textutils.unserialize(followFile.readAll())
- followFile.close()
- m = peripheral.find("modem")
- m.open(4)
- canShoot = false
- canFollow = false
- followClose = false--ignore formation and follow right behind
- followFacePitch = 0
- followFaceYaw = 0
- n = peripheral.wrap("back")
- n.disableAI()
- f = {}
- table.insert(f, "item.item.stick")
- table.insert(f, "Skeleton")
- table.insert(f, "Zombie")
- table.insert(f, "Creeper")
- table.insert(f, "Spider")
- table.insert(f, "Slime")
- table.insert(f, "Witch")
- function inTable(tbl, item)
- for key, value in pairs(tbl) do
- if value == item then
- return key
- end
- end
- return false
- end
- function isLookingAtFF(angle1, angle2, s)
- for _, entity in pairs(s) do
- Sdistance = math.sqrt(math.pow(entity.x, 2) + math.pow(entity.z, 2))
- Sangle1 = math.atan2(entity.z, entity.x) * 180 / math.pi - 90
- Sangle2 = math.atan2(distance, entity.y) * 180 / math.pi - 90
- if math.abs(Sangle1 - angle1) < 5 and math.abs(Sangle1 - angle2) < 5 then
- return true
- end
- end
- return false
- end
- function cycle()
- c = 0
- s = n.sense()
- b = n.scan()
- for _, entity in pairs(s) do
- --print(entity.displayName)
- distance = math.sqrt(math.pow(entity.x, 2) + math.pow(entity.z, 2))
- angle1 = math.atan2(entity.z, entity.x) * 180 / math.pi - 90
- angle2 = math.atan2(distance, entity.y) * 180 / math.pi - 90
- m = n.getMetaByID(entity.id)
- helmet = ""
- if m ~= nil and distance < 15 then
- if m.armor ~= nil then
- if m.armor.helmet ~= nil then
- hmd = m.armor.helmet.getMetadata()
- if hmd ~= nil then
- helmet = hmd.rawName
- end
- end
- end
- end
- 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
- --print("Attacking")
- --print(textutils.serialize(entity))
- n.look(angle1, angle2 - 5)
- os.sleep(0.1)
- n.look(angle1, angle2 - 5)
- for i = 0, 10, 1 do
- if isLookingAtFF(angle1, angle2, s) == false then
- n.shoot(1)
- end
- os.sleep(0.05)
- end
- os.sleep(1)
- c = c + 1
- end
- end
- for _, entity in pairs(s) do
- distance = math.sqrt(math.pow(entity.x, 2) + math.pow(entity.z, 2))
- angle1 = math.atan2(entity.z, entity.x) * 180 / math.pi - 90
- angle2 = math.atan2(distance, entity.y) * 180 / math.pi - 90
- m = n.getMetaByID(entity.id)
- if entity.displayName == follow["name"] and c == 0 and canFollow and n.isWalking() == false then
- if m ~= nil then
- followFacePitch = m.pitch
- followFaceYaw = m.yaw
- end
- --print("Walking to " .. followName)
- posX = math.cos((followFaceYaw + follow["angle"]) * math.pi/180) * follow["distance"]
- posZ = math.sin((followFaceYaw + follow["angle"]) * math.pi/180) * follow["distance"]
- --if math.abs(entity.x + posX) > 1 or math.abs(entity.z + posZ) > 1 then
- --[[
- posObstructed = false
- for _, block in pairs(b) do
- --print(textutils.serialize(block))
- 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
- posObstructed = true
- print(block)
- end
- end
- wSuc, wInfo = n.walk(entity.x + posX, entity.y, entity.z + posZ)
- if wSuc == false or wInfo ~= nil or posObstructed then
- n.walk(entity.x, entity.y, entity.z)
- end
- ]]--
- if followClose then
- n.walk(entity.x, entity.y, entity.z)
- else
- n.walk(entity.x + posX, entity.y, entity.z + posZ)
- end
- --end
- --if == false then
- --n.walk(entity.x, entity.y, entity.z)
- --end
- end
- end
- if c == 0 and n.isWalking() == false then
- --n.look(math.random() * 360, 0)
- n.look(followFaceYaw, followFacePitch)
- end
- end
- function commandListener()
- while true do
- local _, side, freq, rfreq, message = os.pullEvent('modem_message')
- if message["type"] == "reboot" then
- os.reboot()
- elseif message["type"] == "stopFollow" then
- canFollow = false
- elseif message["type"] == "startFollow" then
- canFollow = true
- elseif message["type"] == "stopShoot" then
- canShoot = false
- elseif message["type"] == "startShoot" then
- canShoot = true
- elseif message["type"] == "followClose" then
- followClose = true
- elseif message["type"] == "followFormation" then
- followClose = false
- end
- end
- end
- function cycleLoop()
- while true do
- cycle()
- os.sleep(0.1)
- end
- end
- parallel.waitForAll(commandListener, cycleLoop)
Advertisement
Add Comment
Please, Sign In to add comment