Larvix

Familiar Testing

Dec 6th, 2024 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.84 KB | None | 0 0
  1. mob = peripheral.wrap("back")
  2. mob.disableAI()
  3. target = mob.getMetaByName("yogo_dude")  -- Player as the target
  4.  
  5. -- Set a block gap distance (how far the Vex should stay behind the player)
  6. local block_gap = 2  -- You can adjust this to any value you prefer
  7. local stop_distance = 1  -- Distance within which the Vex should stop moving
  8. local max_range = 16  -- Maximum range for the Vex to detect the target
  9.  
  10. function getRelativePos(target)
  11.     local x, y, z = target.x, target.y, target.z
  12.     local pos = {}
  13.     pos.yaw = math.deg(math.atan2(-x, z))
  14.     pos.pitch = math.deg(-math.atan2(y, math.sqrt(x * x + z * z)))
  15.     pos.dist = math.sqrt(x^2 + y^2 + z^2)
  16.     return pos
  17. end
  18.  
  19. while target do
  20.     target = mob.getMetaByName("yogo_dude")  -- Update the player position
  21.     pos = getRelativePos(target)
  22.    
  23.     -- Ensure the distance is within the sensor's range (16 blocks)
  24.     if pos.dist > max_range then
  25.         -- If the player is too far, stop following
  26.         mob.launch(pos.yaw, pos.pitch, 0)
  27.     else
  28.         -- If the Vex is within range, calculate movement
  29.  
  30.         -- Adjust the target position to maintain the gap
  31.         local delta_x = target.x
  32.         local delta_y = target.y
  33.         local delta_z = target.z
  34.  
  35.         -- Calculate relative movement direction (based on the Vex's current position)
  36.         local relative_x = delta_x - me.x
  37.         local relative_y = delta_y - me.y
  38.         local relative_z = delta_z - me.z
  39.        
  40.         -- Calculate the distance to the player (relative to the Vex)
  41.         local distance = math.sqrt(relative_x^2 + relative_y^2 + relative_z^2)
  42.        
  43.         -- Adjust the distance to maintain the block gap
  44.         local gap_adjustment_factor = block_gap / distance
  45.         relative_x = relative_x * gap_adjustment_factor
  46.         relative_y = relative_y * gap_adjustment_factor
  47.         relative_z = relative_z * gap_adjustment_factor
  48.  
  49.         -- Normalize the direction vector (for consistent speed in all directions)
  50.         distance = math.sqrt(relative_x^2 + relative_y^2 + relative_z^2)
  51.         local norm_x = relative_x / distance
  52.         local norm_y = relative_y / distance
  53.         local norm_z = relative_z / distance
  54.  
  55.         -- Apply movement power to the Vex, with halving the Y axis power
  56.         local power_factor = 0.8  -- Adjust this value for speed control
  57.         local newPowerX = norm_x * power_factor
  58.         local newPowerY = (norm_y * power_factor) / 2  -- Halve the Y axis power
  59.         local newPowerZ = norm_z * power_factor
  60.  
  61.         -- Calculate the total movement power (magnitude of the movement vector)
  62.         local totalPower = math.sqrt(newPowerX^2 + newPowerY^2 + newPowerZ^2)
  63.         local maxSpeed = 4  -- Maximum speed the Vex can move (adjustable)
  64.  
  65.         -- Ensure that the Vex doesn't exceed the maximum speed
  66.         local speedScaling = math.min(1, maxSpeed / totalPower)
  67.         newPowerX = newPowerX * speedScaling
  68.         newPowerY = newPowerY * speedScaling
  69.         newPowerZ = newPowerZ * speedScaling
  70.  
  71.         -- Make the Vex look at the player
  72.         mob.look(pos.yaw, pos.pitch)
  73.  
  74.         -- If the Vex is too close to the player, it should slow down and stop gradually
  75.         if distance <= stop_distance then
  76.             -- Gradual slowdown: apply a small power if within stop distance
  77.             local slowFactor = math.max(0.2, 1 - (distance / stop_distance))  -- Smooth deceleration
  78.             local smallPower = totalPower * slowFactor  -- Reduce the power based on proximity
  79.            
  80.             -- Launch with reduced power when close, allow it to gradually slow
  81.             mob.launch(pos.yaw, pos.pitch, smallPower)
  82.         elseif distance > block_gap then
  83.             -- Otherwise, apply movement towards the player
  84.             mob.launch(pos.yaw, pos.pitch, totalPower)  -- Apply full movement power
  85.         end
  86.     end
  87. end
Advertisement
Add Comment
Please, Sign In to add comment