Advertisement
KDKiller

[CC] Cat

Jun 13th, 2024
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.93 KB | Source Code | 0 0
  1. os.loadAPI("t.lua")
  2.  
  3. cat_name = "BoxCyat"
  4. max_range = 15
  5. min_range = 4
  6. player_move_threshold = 2
  7. max_distance = 50
  8. max_history = 200
  9. exploit_weight = 0.2
  10. max_wait_by_player = 120
  11.  
  12. home = t.getState().pos
  13. t.clearHistory()
  14.  
  15. p = peripheral.wrap("left")
  16. did_meow = false
  17.  
  18. term.clear()
  19. term.setCursorPos(1,1)
  20.  
  21. function goHome()
  22.     state = t.getState()
  23.     if #state.history > 0 then
  24.         t.rt()
  25.         t.clearHistory()
  26.     end
  27.     term.clear()
  28.     term.setCursorPos(1,1)
  29.     print(cat_name.." is alone at home...")
  30. end
  31.  
  32. wait_count = 0
  33. while true do
  34.     players = p.getPlayersInRange(max_range)
  35.  
  36.     if turtle.getFuelLevel() < 500 then
  37.         print(cat_name.." meow hungry meow")
  38.         goHome()
  39.         return
  40.     end
  41.  
  42.     dist = nil
  43.     player = nil
  44.     state = t.getState()
  45.     for _,name in pairs(players) do
  46.         pos = p.getPlayerPos(name)
  47.         d = t.distance(pos, state.pos, "euclidean")
  48.         if dist == nil or dist > d then
  49.             player = name
  50.             dist = d
  51.         end
  52.     end
  53.  
  54.     if player then
  55.         -- print("Closest player is "..player.." at "..string.format("%.2f", dist).." blocks")
  56.         pos = p.getPlayerPos(player)
  57.  
  58.         function stop_condition()
  59.             state = t.getState()
  60.             a = state.pos
  61.             b = p.getPlayerPos(player)
  62.             c = pos
  63.  
  64.             d1 = t.distance(a, b, "euclidean") -- cat distance to player
  65.             d2 = t.distance(b, c, "euclidean") -- player distance from original position
  66.             d3 = t.distance(a, home, "euclidean") -- distance to home
  67.  
  68.             if d1 < min_range or d1 > max_range then
  69.                 return true
  70.             end
  71.             if d2 > player_move_threshold then
  72.                 return true
  73.             end
  74.             if d3 > max_distance or #state.history > max_history then
  75.                 return true
  76.             end
  77.             return false
  78.         end
  79.  
  80.         if dist > min_range and dist < max_range then
  81.             -- print("Going to "..player)
  82.             did_meow = false
  83.             -- FOLLOW
  84.             t.path(pos, exploit_weight, stop_condition)
  85.  
  86.             -- Check if too far
  87.             state = t.getState()
  88.             dist_to_home = t.distance(state.pos, home) -- distance to home
  89.             if dist_to_home > max_distance or #state.history > max_history then
  90.                 print(cat_name.." scared, menya go homeow...")
  91.                 goHome()
  92.             end
  93.         elseif dist < min_range then
  94.             wait_count = wait_count + 1
  95.             if did_meow == false then
  96.                 print("Meow "..player:sub(1,#player-2).."nya")
  97.                 did_meow = true
  98.             end
  99.             if wait_count > max_wait_by_player then
  100.                 wait_count = 0
  101.                 print("Meow're boring, I'nya going homeow")
  102.                 goHome()
  103.             end
  104.         end
  105.     else
  106.         goHome()
  107.     end
  108.  
  109.     sleep(0.5)
  110. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement