CaptainSpaceCat

Turtle Follower

Dec 9th, 2021 (edited)
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.21 KB | None | 0 0
  1. local args = {...}
  2. local currentPos = {
  3.     --x, y, z, f
  4. }
  5.  
  6. local scanner = peripheral.wrap("right")
  7. local function getVIP(name)
  8.     players = scanner.getPlayersInRange(120)
  9.     for _, p in pairs(players) do
  10.         if p == name then
  11.             return p
  12.         end
  13.     end
  14.     return players[1]
  15. end
  16.  
  17. local function getTurtlePos()
  18.     local handle = io.open("turtlePos", "r")
  19.     if handle then
  20.         line =  handle:read("l")
  21.         currentPos.x = tonumber(line)
  22.         line =  handle:read("l")
  23.         currentPos.z = tonumber(line)
  24.         line =  handle:read("l")
  25.         currentPos.f = tonumber(line)
  26.         handle:close()
  27.     else
  28.         currentPos.x = tonumber(args[1])
  29.         currentPos.z = tonumber(args[2])
  30.         currentPos.f = tonumber(args[3])
  31.     end
  32. end
  33.  
  34. local function updatePos()
  35.     local handle = io.open("turtlePos", "w")
  36.     handle:write(currentPos.x .. "\n")
  37.     handle:write(currentPos.z .. "\n")
  38.     handle:write(currentPos.f .. "\n")
  39.     handle:close()
  40. end
  41.  
  42. local dirTab = {
  43.     {["x"]=0, ["z"]=-1},
  44.     {["x"]=1, ["z"]=0},
  45.     {["x"]=0, ["z"]=1},
  46.     {["x"]=-1, ["z"]=0},
  47. }
  48. local function forward()
  49.     turtle.forward()
  50.     delta = dirTab[currentPos.f]
  51.     currentPos.x = currentPos.x + delta.x
  52.     currentPos.z = currentPos.z + delta.z
  53. end
  54. local function back()
  55.     turtle.back()
  56.     delta = dirTab[currentPos.f]
  57.     currentPos.x = currentPos.x - delta.x
  58.     currentPos.z = currentPos.z - delta.z
  59. end
  60. local function left()
  61.     turtle.turnLeft()
  62.     currentPos.f = currentPos.f - 1
  63.     if currentPos.f < 1 then
  64.         currentPos.f = currentPos.f + 4
  65.     end
  66. end
  67. local function right()
  68.     turtle.turnRight()
  69.     currentPos.f = currentPos.f + 1
  70.     if currentPos.f > 4 then
  71.         currentPos.f = currentPos.f - 4
  72.     end
  73. end
  74.  
  75. local function step(dir)
  76.     df = currentPos.f - dir
  77.     if df < 0 then
  78.         df = df + 4
  79.     end
  80.     if df == 0 then
  81.         forward()
  82.     elseif df == 1 then
  83.         left()
  84.     elseif df == 2 then
  85.         back()
  86.     elseif df == 3 then
  87.         right()
  88.     end
  89.     updatePos()
  90. end
  91.  
  92.  
  93. local function moveto(pos)
  94.     --take one step towards the goal
  95.     dx = math.max(math.min(pos.x - currentPos.x, 1), -1)
  96.     dz = math.max(math.min(pos.z - currentPos.z, 1), -1)
  97.  
  98.     -- get list of possible movement directions
  99.     pDirs = {}
  100.     if dx < 0 then
  101.         pDirs[#pDirs+1] = 4
  102.     elseif dx > 0 then
  103.         pDirs[#pDirs+1] = 2
  104.     end
  105.     if dz < 0 then
  106.         pDirs[#pDirs+1] = 1
  107.     elseif dz > 0 then
  108.         pDirs[#pDirs+1] = 3
  109.     end
  110.  
  111.     if #pDirs == 0 then
  112.         return
  113.     elseif #pDirs == 1 then
  114.         step(pDirs[1])
  115.     else
  116.         if currentPos.f % 2 == pDirs[1] % 2 then
  117.             step(pDirs[1])
  118.         else
  119.             step(pDirs[2])
  120.         end
  121.     end
  122. end
  123.  
  124. local speaker = peripheral.wrap("left")
  125. local b_end = "betterendforge:betterendforge.ambient"
  126. local suffixes = {
  127. "blossoming_spires",
  128. "chorus_forest",
  129. --"dust_wastelands",
  130. "foggy_mushroomland",
  131. "glowing_grasslands",
  132. "megalake",
  133. "megalake_grove",
  134. --"sulphur_springs",
  135. "umbrella_jungle",
  136. }
  137.  
  138. function printTime(n)
  139.     if n > 1 then
  140.         x, y = term.getCursorPos()
  141.         term.setCursorPos(x, y-1)
  142.         term.clearLine()
  143.     end
  144.     print(n)
  145. end
  146.  
  147. -- radius 14 from middle
  148. local delay = 0
  149. local function playSound()
  150.     local idx = math.random(1, #suffixes)
  151.     local sound = b_end .. "." .. suffixes[idx]
  152.     delay = math.random(65, 100)
  153.     print("Playing " .. suffixes[idx] .. " for " .. delay .. "s")
  154.     speaker.playSound(sound)
  155. end    
  156.  
  157. getTurtlePos()
  158. local startTime = os.time()
  159. local secondsMultiplier = 0.02
  160. while true do
  161.     player = getVIP("CaptainSpaceCat7")
  162.     if player then
  163.         pos = scanner.getPlayerPos(player)
  164.         moveto(pos)
  165.     end
  166.     local currentTime = os.time()
  167.     print( currentTime - startTime .. "   " .. secondsMultiplier * delay)
  168.     if currentTime - startTime > secondsMultiplier * delay then
  169.         playSound()
  170.         startTime = currentTime
  171.     end
  172.     --printTime()
  173. end
  174.  
  175. --[[
  176. getTurtlePos()
  177. local moveRoutine = coroutine.create(function ()
  178.     while true do
  179.         player = getVIP("CaptainSpaceCat7")
  180.         print("found player " .. player)
  181.         if player then
  182.             pos = scanner.getPlayerPos(player)
  183.             print("player at " .. pos.x .. ":" .. pos.z)
  184.             moveto(pos)
  185.         end
  186.         coroutine.yield()
  187.     end
  188. end)
  189.  
  190. while true do
  191.     print("routine status: " .. coroutine.status(moveRoutine))
  192.     coroutine.resume(moveRoutine)
  193.     sleep(0.5)
  194. end
  195. ]]
Add Comment
Please, Sign In to add comment