Advertisement
Guest User

attack

a guest
Apr 6th, 2020
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.24 KB | None | 0 0
  1. local sensor = peripheral.wrap("left")
  2. turtle.refuel()
  3. local facing = "West"
  4.  
  5. function getpos(name)
  6.   local pos = sensor.getPlayerByName(name).all().position
  7.   return pos
  8. end
  9.  
  10. function setDirection(direction)
  11.   if direction == "North" then
  12.     if facing == "South" then
  13.       turtle.turnLeft()
  14.       turtle.turnLeft()
  15.     elseif facing == "East" then
  16.       turtle.turnLeft()
  17.     elseif facing == "West" then
  18.       turtle.turnRight()
  19.     end
  20.   facing = "North"
  21.   end
  22.   if direction == "South" then
  23.     if facing == "North" then
  24.       turtle.turnLeft()
  25.       turtle.turnLeft()
  26.     elseif facing == "East" then
  27.       turtle.turnRight()
  28.     elseif facing == "West" then
  29.       turtle.turnLeft()
  30.     end
  31.   facing = "South"
  32.   end
  33.   if direction == "East" then
  34.     if facing == "North" then
  35.       turtle.turnRight()
  36.     elseif facing == "South" then
  37.       turtle.turnLeft()
  38.     elseif facing == "West" then
  39.       turtle.turnLeft()
  40.       turtle.turnLeft()
  41.     end
  42.   facing = "East"
  43.   end
  44.   if direction == "West" then
  45.     if facing == "North" then
  46.       turtle.turnLeft()
  47.     elseif facing == "South" then
  48.       turtle.turnRight()
  49.     elseif facing == "East" then
  50.       turtle.turnLeft()
  51.       turtle.turnLeft()
  52.     end
  53.     facing = "West"
  54.   end
  55. end  
  56.    
  57.  
  58. while true do  
  59.   local pos = getpos("iKipling")
  60.   local ypos = 0
  61.   local xpos = 0
  62.   local zpos = 0
  63.   local count = 1
  64.   for i in pairs(pos) do
  65.     print(pos[i])
  66.     if count == 1 then
  67.       ypos = pos[i]
  68.       print("ypos=" .. ypos)
  69.     elseif count == 2 then
  70.       xpos = pos[i]
  71.       print("xpos=" .. xpos)
  72.     elseif count == 3 then
  73.       zpos = pos[i]
  74.       print("zpos=" .. zpos)
  75.     else
  76.       print("invalid")
  77.     end
  78.     count = count + 1  
  79.   end
  80.    
  81.   if ypos <-0.5 then
  82.     turtle.down()
  83.     print("down")
  84.   elseif ypos >0.5 then
  85.     turtle.up()
  86.     print("up")
  87.   end
  88.  
  89.   if zpos <-1.5 then
  90.     setDirection("North")
  91.     turtle.forward()
  92.   elseif zpos >1.5 then
  93.     setDirection("South")
  94.     turtle.forward()
  95.     print("South")
  96.   end
  97.  
  98.   if xpos <-1.5 then
  99.     setDirection("West")
  100.     turtle.forward()
  101.   elseif xpos >1.5 then
  102.     setDirection("East")
  103.     turtle.forward()
  104.   end
  105.    
  106.   turtle.attack()
  107.   sleep(0.5)
  108. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement