Advertisement
Guest User

attack

a guest
Apr 4th, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.86 KB | None | 0 0
  1. turtle.refuel()
  2. m = peripheral.wrap("right")
  3. --m.open(1)
  4.  
  5. --x,y,z = -511, 95, 167
  6. facing = "z-"
  7.  
  8. function fxp ()
  9.     if facing=="x+" then return
  10.     elseif facing =="x-" then
  11.         turtle.turnLeft()
  12.         turtle.turnLeft()
  13.     elseif facing=="z-" then turtle.turnRight()
  14.     elseif facing=="z+" then turtle.turnLeft()
  15.     end
  16.     facing="x+"
  17. end
  18. function fxm ()
  19.     if facing=="x+" then
  20.         turtle.turnLeft()
  21.         turtle.turnLeft()
  22.     elseif facing=="x-" then return
  23.     elseif facing=="z-" then turtle.turnLeft()
  24.     elseif facing=="z+" then turtle.turnRight()
  25.     end
  26.     facing="x-"
  27. end
  28. function fzp ()
  29.     if facing=="x+" then turtle.turnRight()
  30.     elseif facing=="x-" then turtle.turnLeft()
  31.     elseif facing=="z+" then return
  32.     elseif facing=="z-" then
  33.         turtle.turnLeft()
  34.         turtle.turnLeft()
  35.     end
  36.     facing="z+"
  37. end
  38. function fzm ()
  39.     if facing=="x+" then turtle.turnLeft()
  40.     elseif facing=="x-" then turtle.turnRight()
  41.     elseif facing=="z+" then
  42.         turtle.turnLeft()
  43.         turtle.turnLeft()
  44.     elseif facing=="z-" then return
  45.     end
  46.     facing="z-"
  47. end
  48.  
  49. function move (tx,ty,tz)
  50.     x,y,z=0,0,0
  51.     dx=tx
  52.     dy=ty
  53.     dz=tz
  54.     max = math.max(math.abs(dx), math.abs(dy),math.abs(dz))
  55.  
  56.     if dy>0.5 and math.abs(dy)==max then
  57.         turtle.up()
  58.         y = y+1
  59.         dy=ty-y
  60.     elseif dy<-0.5 and math.abs(dy)==max then
  61.         turtle.down()
  62.         y=y-1
  63.         dy=ty-y
  64.     elseif dx>0.5 and math.abs(dx)==max then
  65.         fxp()
  66. --        while dx>0 do
  67.             turtle.forward()
  68.             x = x+1
  69.             dx = tx-x
  70. --        end
  71.     elseif dx<-0.5 and math.abs(dx)==max then
  72.         fxm()
  73. --        while dx<0 do
  74.             turtle.forward()
  75.             x=x-1
  76.             dx = tx-x
  77. --        end
  78.     elseif dz>0.5 and math.abs(dz)==max then
  79.         fzp()
  80. --        while dz>0 do
  81.             turtle.forward()
  82.             z=z+1
  83.             dz = tz-z
  84. --        end
  85.     elseif dz<-0.5 and math.abs(dz)==max then
  86.         fzm()
  87. --        while dz<0 do
  88.             turtle.forward()
  89.             z=z-1
  90.             dz = tz-z
  91. --        end
  92.     end
  93. end
  94.  
  95. function contains (t, val)
  96.     for index, value in ipairs(t) do
  97.         if value.id==val then
  98.             return true
  99.         end
  100.     end
  101.     return false
  102. end
  103.  
  104. function euDist (ax,ay,az,bx,by,bz)
  105.     cx = math.abs(ax)-math.abs(bx)
  106.     cy = math.abs(ay)-math.abs(by)
  107.     cz = math.abs(az)-math.abs(bz)
  108.     return math.sqrt(cx*cx+cy*cy+cz*cz)
  109. end
  110.  
  111. --turtle.refuel()
  112. --m = peripheral.wrap("left")
  113. --facing="z-"
  114.  
  115. ignore = {"McSkizz", "lilfinn", "Mentiad", "Item", "Arrow", "XPOrb", "SmallFireball", "ThrownPotion"}
  116. ignLookup={}
  117. for i=1,#ignore do
  118.     ignLookup[ignore[i]]=true
  119. end
  120.  
  121. current = {id=0}
  122. McSkizz = {}
  123. while true do
  124.     event, key = os.pullEvent()
  125.     if event=="key" and key==keys.x then
  126.         os.exit()
  127.     end
  128.     curMobs = {}
  129.     mobs = m.sense()
  130.     for i=1,#mobs do
  131.         if mobs[i].name=="McSkizz" then McSkizz = mobs[i] end
  132.         if not ignLookup[mobs[i].name] then
  133.             curMobs[#curMobs+1]=mobs[i]
  134.         end
  135.     end        
  136.     McDist = euDist(0,0,0,McSkizz.x,McSkizz.y,McSkizz.z)
  137.     if McDist>10 then
  138.         current=McSkizz
  139.     elseif #curMobs>0 then
  140.         closestD = 50
  141.         closest={}
  142.         for i=1,#curMobs do
  143.             mob = curMobs[i]
  144.             if euDist(0,0,0,mob.x,mob.y,mob.z)<closestD then
  145.                 closestD=euDist(0,0,0,mob.x,mob.y,mob.z)
  146.                 closest=mob
  147.             end
  148.         end
  149.         current = closest
  150.     else
  151.         current = McSkizz
  152.     end
  153.     print(current.name)
  154.     if current.name~="McSkizz" then
  155.         move(current.x,current.y+1,current.z)
  156.         turtle.attackDown()
  157.         turtle.attack()
  158.         turtle.attackUp()
  159.     else
  160.         move(current.x, current.y+1, current.z)
  161.     end
  162. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement