Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- owner = "yogo_dude"
- mob = peripheral.wrap("back")
- mob.disableAI()
- gear = mob.getEquipment()
- gearStore = "ender_chest"
- modem = peripheral.find("modem")
- hostile = nil
- droppedItem = nil
- defaultDump = "inventory"
- backupDump = "ender_chest"
- local function readFile(fileName)
- if not fs.exists(fileName) then return nil end
- local file = fs.open(fileName,"r")
- local data = file.readAll()
- file.close()
- return data
- end
- local function saveFile(fileName,data)
- if fs.exists(fileName) then fs.delete(fileName) end
- local file = fs.open(fileName,"w")
- file.write(data)
- file.close()
- end
- local function getRelativePos(target)
- local x,y,z = target.x,target.y,target.z
- local pos = {}
- pos.yaw = math.deg(math.atan2(-x, z))
- pos.pitch = math.deg(-math.atan2(y, math.sqrt(x * x + z * z)))
- pos.dist = math.sqrt((x^2)+(y^2)+(z^2))
- return pos
- end
- local function goTowards(target)
- local pos = getRelativePos(target)
- mob.look(pos.yaw,pos.pitch)
- if pos.dist > 2.5 then
- mob.launch(pos.yaw,pos.pitch,(math.tan((pos.dist-1)/20)))
- elseif pos.dist < 2.5 then
- me = mob.getMetaOwner()
- if me.motionY^2 > (me.motionX^2 + me.motionZ^2) and pos.y < 0 then
- me.motionY = me.motionY*2
- mob.launch(0,-90,math.sqrt(me.motionY^2)*2)
- else
- mob.launch(pos.yaw-360,pos.pitch-180, math.sqrt(me.motionX^2 + me.motionY^2 + me.motionZ^2))
- end
- end
- end
- local function guard(target)
- if getRelativePos(target).dist > 10 then
- hostile = nil
- goTowards(target)
- else
- goTowards(mob.getMetaByID(hostile))
- gear.pullItems(gearStore,25,1)
- mob.swing()
- gear.pushItems(gearStore,1,25)
- end
- end
- local function heal()
- local prevItem = gear.getItemMeta(1)
- if prevItem then
- gear.pushItems("ender_chest",1,prevItem.tmp)
- end
- for s = 19,22 do
- gear.pullItems("ender_chest",s,1,1)
- mob.use(0,"main")
- end
- if prevItem then
- for slot,item in pairs(mob.getEnder().list()) do
- if item.name == prevItem.name then
- gear.pullItems("ender_chest",slot,1,1)
- break
- end
- end
- end
- end
- local function collect(target)
- if getRelativePos(mob.getMetaByName(owner)).dist > 10 then
- droppedItem = nil
- goTowards(mob.getMetaByName(owner))
- else
- if not target then droppedItem = nil end
- goTowards(target)
- gear.suck(1)
- if not gear.pushItems(defaultDump,1) then
- gear.pushItems(backupDump,1)
- end
- droppedItem = nil
- end
- end
- local function recall()
- while mob.getBlockMeta(0,-1,0).name == "minecraft:air" do
- if mob.getMetaByName(owner) then return end
- mob.launch(0,90,0.1)
- sleep(1)
- end
- mob.launch(0,-90,(mob.getMetaOwner().motionY*-2.2))
- sleep(3)
- if mob.getMetaByName(owner) then return end
- mob.look(0,90)
- gear.pullItems(gearStore,27,1,1)
- mob.use()
- gear.pushItems(gearStore,1,1,27)
- end
- local function getHostile()
- while true do
- if not (hostile and mob.getMetaByID(hostile)) then
- hostile = nil
- local currentScan = mob.sense()
- for _,entity in pairs(currentScan) do
- local nextEntity = mob.getMetaByID(entity.id)
- if nextEntity then
- if (kos[nextEntity.displayName] and (getRelativePos(nextEntity).dist < 10)) then
- hostile = nextEntity.id
- break
- elseif string.match(nextEntity.displayName,"item.") then
- if not droppedItem then droppedItem = nextEntity.id end
- elseif kos[nextEntity.displayName] == nil then
- kos[nextEntity.displayName] = false
- saveFile("hostileList",textutils.serialise(kos))
- end
- end
- end
- if hostile = nil then sleep(3) end
- end
- end
- end
- local function main()
- hasTrust = true
- while true do
- if not gear.getItemMeta(2) then
- gear.pullItems(gearStore,24,1,2)
- elseif (mob.getMetaOwner().health < 14) and hasTrust then
- heal()
- if mob.getMetaOwner().health < 14 then
- hasTrust = false
- end
- elseif not mob.getMetaByName(owner) then
- recall()
- elseif hostile then
- guard(mob.getMetaByName(owner))
- elseif droppedItem then
- collect(mob.getMetaByID(droppedItem))
- else
- goTowards(mob.getMetaByName(owner))
- end
- sleep()
- end
- end
- term.clear()
- term.setCursorPos(1,1)
- print("If lost please return to "..owner)
- kos = readFile("hostileList")
- if not kos then
- kos = {}
- else
- kos = textutils.unserialise(kos)
- end
- parallel.waitForAny(main,getHostile)
Advertisement
Add Comment
Please, Sign In to add comment