Advertisement
osmarks

CartDrone

May 28th, 2020
1,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local kinetic = peripheral.find "plethora:kinetic"
  2. local modem = peripheral.find "modem"
  3. local laser = peripheral.find "plethora:laser"
  4. local sensor = peripheral.find "plethora:sensor"
  5. modem.open(70)
  6.  
  7. local p = true
  8.  
  9. local target = vector.new(gps.locate())
  10.  
  11. local function calc_yaw_pitch(v)
  12.     local pitch = -math.atan2(v.y, math.sqrt(v.x * v.x + v.z * v.z))
  13.     local yaw = math.atan2(-v.x, v.z)
  14.     return math.deg(yaw), math.deg(pitch)
  15. end
  16.  
  17. local mob_names = { "Creeper", "Zombie", "Skeleton", "Blaze" }
  18. local mob_lookup = {}
  19. for _, mob in pairs(mob_names) do
  20.     mob_lookup[mob] = true
  21. end
  22.  
  23. local function calc_distance(entity)
  24.     return math.sqrt(entity.x * entity.x + entity.y * entity.y + entity.z * entity.z)
  25. end
  26.  
  27. local function sentry()
  28.     while true do
  29.         local mobs = sensor.sense()
  30.         local nearest
  31.         for _, mob in pairs(mobs) do
  32.             if mob_lookup[mob.name] then
  33.                 mob.distance = calc_distance(mob)
  34.                 if nearest == nil or mob.distance < nearest.distance then
  35.                     nearest = mob
  36.                 end
  37.             end
  38.         end
  39.         if nearest then
  40.             local y, p = calc_yaw_pitch(vector.new(nearest.x, nearest.y, nearest.z))
  41.             laser.fire(y, p, 0.5)
  42.             sleep(0.2)
  43.         else
  44.             sleep(0.5)
  45.         end
  46.     end
  47. end
  48.  
  49. local function fly()
  50.     while true do
  51.         kinetic.launch(0,270,0.320)
  52.         sleep(0.2)
  53.     end
  54. end
  55.  
  56. parallel.waitForAll(
  57.     fly,
  58.     function()
  59.         while true do
  60.             local current = vector.new(gps.locate())
  61.             local displacement = target - current
  62.             if displacement:length() > 0.1 then
  63.                 displacement = displacement + vector.new(0, 0.3, 0)
  64.                 local y, p = calc_yaw_pitch(displacement)
  65.                 local pow = math.min(displacement:length() * 0.08, 2.0)
  66.                 print(y, p, pow)
  67.                 kinetic.launch(y, p, pow)
  68.             end
  69.             sleep(1)
  70.         end
  71.     end,
  72.     function()
  73.         while true do
  74.             event, side, frequency, replyFrequency, message, distance = os.pullEvent("modem_message")
  75.             if message == "up" then
  76.                 target.y = target.y + 1
  77.             elseif message == "down" then
  78.                 target.y = target.y - 1
  79.             elseif message == "north" then
  80.                 target.z = target.z - 1
  81.             elseif message == "south" then
  82.                 target.z = target.z + 1
  83.             elseif message == "west" then
  84.                 target.x = target.x - 1
  85.             elseif message == "east" then
  86.                 target.x = target.x + 1
  87.             end
  88.         end
  89.     end,
  90.     sentry
  91. )
  92.  
  93.                 --[[
  94.                 local xDev = tarX - x
  95.                 local yDev = tarY - y
  96.                 local zDev = tarZ - z
  97.                 if yDev+0.1 < 0 then
  98.                     local power = math.min(math.abs(yDev) / 6 ,0.1)
  99.                     print(power, "down", y, tarY, power, yDev)
  100.                     kinetic.launch(0,90, power)
  101.                 end
  102.                 if yDev-0.1 > 0 then
  103.                     local power = math.min(math.abs(yDev) / 6 ,0.1)
  104.                     print(power, "up", y, tarY, power, yDev)
  105.                     kinetic.launch(0,270, power)
  106.                 end
  107.                 if xDev+0.1 < 0 then
  108.                     local power = math.min(math.abs(xDev) / 8 ,0.33)
  109.                     print(power, "west", x, tarX, power, xDev)
  110.                     kinetic.launch(90,0, power)
  111.                     sleep(0.1)
  112.                     kinetic.launch(270,0, power * 0.75)
  113.                 end
  114.                 if xDev-0.1 > 0 then
  115.                     local power = math.min(math.abs(xDev) / 8 ,0.33)
  116.                     print(power, "east", x, tarX, power, xDev)
  117.                     kinetic.launch(270,0, power)
  118.                     sleep(0.1)
  119.                     kinetic.launch(90,0, power  * 0.75)
  120.                 end
  121.                 if zDev+0.1 < 0 then
  122.                     local power = math.min(math.abs(zDev) / 8 ,0.33)
  123.                     print(power, "north", z, tarZ, power, zDev)
  124.                     kinetic.launch(180,0, power)
  125.                     sleep(0.1)
  126.                     kinetic.launch(0,0, power * 0.75)
  127.                 end
  128.                 if zDev-0.1 > 0 then
  129.                     local power = math.min(math.abs(zDev) / 8 ,0.33)
  130.                     print(power, "south", z, tarZ, power, zDev)
  131.                     kinetic.launch(0,0, power)
  132.                     sleep(0.1)
  133.                     kinetic.launch(180,0, power  * 0.75)
  134.                 end
  135.             end
  136.             ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement