Advertisement
Gorgozoth

Maid Turtle

Dec 17th, 2022 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.36 KB | Gaming | 0 0
  1. -- Function to find and attack hostile mobs
  2. function findAndAttack()
  3.   -- Check for hostile mobs in front of the turtle
  4.   if turtle.detect() then
  5.     turtle.attack()
  6.   end
  7.  
  8.   -- Check for hostile mobs to the left and right of the turtle
  9.   turtle.turnLeft()
  10.   if turtle.detect() then
  11.     turtle.attack()
  12.   end
  13.   turtle.turnRight()
  14.   turtle.turnRight()
  15.   if turtle.detect() then
  16.     turtle.attack()
  17.   end
  18.   turtle.turnLeft()
  19. end
  20.  
  21. -- Function to move the turtle to a specific coordinate
  22. function moveToCoord(x, y, z)
  23.   -- Calculate the distance to the target coordinate
  24.   local dx = x - turtle.getX()
  25.   local dy = y - turtle.getY()
  26.   local dz = z - turtle.getZ()
  27.  
  28.   -- Move the turtle along the x-axis
  29.   if dx > 0 then
  30.     turtle.turnRight()
  31.   elseif dx < 0 then
  32.     turtle.turnLeft()
  33.   end
  34.   for i = 1, math.abs(dx) do
  35.     findAndAttack()
  36.     turtle.forward()
  37.   end
  38.  
  39.   -- Move the turtle along the y-axis
  40.   if dy > 0 then
  41.     turtle.turnRight()
  42.     turtle.turnRight()
  43.   end
  44.   for i = 1, math.abs(dy) do
  45.     findAndAttack()
  46.     turtle.up()
  47.   end
  48.  
  49.   -- Move the turtle along the z-axis
  50.   if dz > 0 then
  51.     turtle.turnLeft()
  52.   elseif dz < 0 then
  53.     turtle.turnRight()
  54.   end
  55.   for i = 1, math.abs(dz) do
  56.     findAndAttack()
  57.     turtle.forward()
  58.   end
  59. end
  60.  
  61. -- Variables to store the target coordinate and the range to search for mobs
  62. local x = -200
  63. local y = 95
  64. local z = 846
  65. local range = 64
  66.  
  67. -- Move the turtle to the starting position
  68. moveToCoord(x, y, z)
  69.  
  70. -- Scan for mobs within the specified range
  71. for i = -range, range do
  72.   for j = -range, range do
  73.     for k = -range, range do
  74.       if math.abs(i) + math.abs(j) + math.abs(k) <= range then
  75.         moveToCoord(x + i, y + j, z + k)
  76.       end
  77.     end
  78.   end
  79. end
  80.  
  81. -- Move the turtle back to the starting position
  82. moveToCoord(x, y, z)
  83.  
  84. -- Place the chest and deposit the drops
  85. turtle.select(16) -- Assume the chest is stored in slot 16
  86. turtle.place()
  87. for i = 1, 16 do
  88.   turtle.select(i)
  89.   turtle.drop()
  90. end
  91.  
  92. --This code assumes that the turtle is starting at the coordinate specified by the x, y, and z variables, and that it is facing the direction it will start scanning for mobs in. The range variable specifies the distance in each direction (x, y, and z) that the turtle will search for mobs. The turtle will attack any hostile mobs it finds and place drops in a chest.
Tags: turtle
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement