Larvix

Familiar

Nov 26th, 2024 (edited)
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.38 KB | None | 0 0
  1. owner = "yogo_dude"
  2. mob = peripheral.wrap("back")
  3. mob.disableAI()
  4. gear = mob.getEquipment()
  5. gearStore = "ender_chest"
  6. modem = peripheral.find("modem")
  7. hostile = nil
  8. droppedItem = nil
  9. defaultDump = "inventory"
  10. backupDump = "ender_chest"
  11.  
  12. local function readFile(fileName)
  13.     if not fs.exists(fileName) then return nil end
  14.     local file = fs.open(fileName,"r")
  15.     local data = file.readAll()
  16.     file.close()
  17.     return data
  18. end
  19.  
  20. local function saveFile(fileName,data)
  21.     if fs.exists(fileName) then fs.delete(fileName) end
  22.     local file = fs.open(fileName,"w")
  23.     file.write(data)
  24.     file.close()
  25. end
  26.  
  27. local function getRelativePos(target)
  28.     local x,y,z = target.x,target.y,target.z
  29.     local pos = {}
  30.     pos.yaw = math.deg(math.atan2(-x, z))
  31.     pos.pitch = math.deg(-math.atan2(y, math.sqrt(x * x + z * z)))
  32.     pos.dist = math.sqrt((x^2)+(y^2)+(z^2))
  33.     return pos
  34. end
  35.  
  36. local function goTowards(target)
  37.     local pos = getRelativePos(target)
  38.     mob.look(pos.yaw,pos.pitch)
  39.     if pos.dist > 2.5 then
  40.         mob.launch(pos.yaw,pos.pitch,(math.tan((pos.dist-1)/20)))
  41.     elseif pos.dist < 2.5 then
  42.         me = mob.getMetaOwner()
  43.         if me.motionY^2 > (me.motionX^2 + me.motionZ^2) and pos.y < 0 then
  44.             me.motionY = me.motionY*2
  45.             mob.launch(0,-90,math.sqrt(me.motionY^2)*2)
  46.         else
  47.             mob.launch(pos.yaw-360,pos.pitch-180, math.sqrt(me.motionX^2 + me.motionY^2 + me.motionZ^2))
  48.         end
  49.     end
  50. end
  51.  
  52. local function guard(target)
  53.     if getRelativePos(target).dist > 10 then
  54.         hostile = nil
  55.         goTowards(target)
  56.     else
  57.         goTowards(mob.getMetaByID(hostile))
  58.         gear.pullItems(gearStore,25,1)
  59.         mob.swing()
  60.         gear.pushItems(gearStore,1,25)
  61.     end
  62. end
  63.  
  64. local function heal()
  65.     local prevItem = gear.getItemMeta(1)
  66.     if prevItem then
  67.         gear.pushItems("ender_chest",1,prevItem.tmp)
  68.     end
  69.     for s = 19,22 do
  70.         gear.pullItems("ender_chest",s,1,1)
  71.         mob.use(0,"main")
  72.     end
  73.     if prevItem then
  74.         for slot,item in pairs(mob.getEnder().list()) do
  75.             if item.name == prevItem.name then
  76.                 gear.pullItems("ender_chest",slot,1,1)
  77.                 break
  78.             end
  79.         end
  80.     end
  81. end
  82.  
  83. local function collect(target)
  84.     if getRelativePos(mob.getMetaByName(owner)).dist > 10 then
  85.         droppedItem = nil
  86.         goTowards(mob.getMetaByName(owner))
  87.     else
  88.         if not target then droppedItem = nil end
  89.         goTowards(target)
  90.         gear.suck(1)
  91.         if not gear.pushItems(defaultDump,1) then
  92.             gear.pushItems(backupDump,1)
  93.         end
  94.         droppedItem = nil
  95.     end
  96. end
  97.  
  98. local function recall()
  99.     while mob.getBlockMeta(0,-1,0).name == "minecraft:air" do
  100.         if mob.getMetaByName(owner) then return end
  101.         mob.launch(0,90,0.1)
  102.         sleep(1)
  103.     end
  104.     mob.launch(0,-90,(mob.getMetaOwner().motionY*-2.2))
  105.     sleep(3)
  106.     if mob.getMetaByName(owner) then return end
  107.     mob.look(0,90)
  108.     gear.pullItems(gearStore,27,1,1)
  109.     mob.use()
  110.     gear.pushItems(gearStore,1,1,27)
  111. end
  112.  
  113. local function getHostile()
  114.     while true do
  115.         if not (hostile and mob.getMetaByID(hostile)) then
  116.             hostile = nil
  117.             local currentScan = mob.sense()
  118.             for _,entity in pairs(currentScan) do
  119.                 local nextEntity = mob.getMetaByID(entity.id)
  120.                 if nextEntity then
  121.                     if (kos[nextEntity.displayName] and (getRelativePos(nextEntity).dist < 10)) then
  122.                         hostile = nextEntity.id
  123.                         break
  124.                     elseif string.match(nextEntity.displayName,"item.") then
  125.                         if not droppedItem then droppedItem = nextEntity.id end
  126.                     elseif kos[nextEntity.displayName] == nil then
  127.                         kos[nextEntity.displayName] = false
  128.                         saveFile("hostileList",textutils.serialise(kos))
  129.                     end
  130.                 end
  131.             end
  132.             if hostile = nil then sleep(3) end
  133.         end
  134.     end
  135. end
  136.  
  137. local function main()
  138. hasTrust = true
  139. while true do
  140.     if not gear.getItemMeta(2) then
  141.         gear.pullItems(gearStore,24,1,2)
  142.     elseif (mob.getMetaOwner().health < 14) and hasTrust then
  143.         heal()
  144.         if mob.getMetaOwner().health < 14 then
  145.             hasTrust = false
  146.         end
  147.     elseif not mob.getMetaByName(owner) then
  148.         recall()
  149.     elseif hostile then
  150.         guard(mob.getMetaByName(owner))
  151.     elseif droppedItem then
  152.         collect(mob.getMetaByID(droppedItem))
  153.     else
  154.         goTowards(mob.getMetaByName(owner))
  155.     end
  156.     sleep()    
  157. end
  158. end
  159.  
  160. term.clear()
  161. term.setCursorPos(1,1)
  162. print("If lost please return to "..owner)
  163. kos = readFile("hostileList")
  164. if not kos then
  165.     kos = {}
  166. else
  167.     kos = textutils.unserialise(kos)
  168. end
  169. parallel.waitForAny(main,getHostile)
Advertisement
Add Comment
Please, Sign In to add comment