Mathilde411

patrouilleur

Nov 15th, 2020 (edited)
1,084
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.59 KB | None | 0 0
  1. local ni = peripheral.wrap("back")
  2. local R2D = 180/math.pi
  3.  
  4. local immu = {
  5.     ["bastoup"] = true,
  6.     ["boubechcraft"] = true,
  7.     ["maxouel54"] = true,
  8.     ["chipax"] = true,
  9.     ["cargolion"] = true,
  10.     ["cow"] = true,
  11.     ["quark:foxhound"] = true,
  12.     ["sheep"] = true,
  13.     ["chicken"] = true,
  14.     ["skeletonhorse"] = true,
  15.     ["horse"] = true,
  16.     ["actuallyadditions.worm"] = true,
  17.     ["villager"] = true,
  18.     ["item"] = true,
  19.     ["plethora:laser"] = true
  20. }
  21.  
  22. local parcours = {
  23.     {
  24.         x = -197,
  25.         y = 70,
  26.         z = 5799
  27.     },
  28.     {
  29.         x = -197,
  30.         y = 70,
  31.         z = 5806
  32.     },
  33.     {
  34.         x = -203,
  35.         y = 70,
  36.         z = 5806
  37.     },
  38.     {
  39.         x = -213,
  40.         y = 69,
  41.         z = 5808
  42.     },
  43.     {
  44.         x = -213,
  45.         y = 69,
  46.         z = 5803
  47.     },
  48.     {
  49.         x = -211,
  50.         y = 70,
  51.         z = 5788
  52.     },
  53.     {
  54.         x = -204,
  55.         y = 70,
  56.         z = 5790
  57.     },
  58.     {
  59.         x = -204,
  60.         y = 70,
  61.         z = 5790
  62.     },
  63.     {
  64.         x = -196,
  65.         y = 70,
  66.         z = 5791
  67.     }
  68. }
  69.  
  70. ni.disableAI()
  71.  
  72. local max = #parcours
  73. local current = 1
  74.  
  75. local function nextPoint()
  76.     if current == max then
  77.         current = 1
  78.     else
  79.         current = current + 1
  80.     end
  81. end
  82.  
  83. local function dist(x,y)
  84.     return math.sqrt(x*x + y*y)
  85. end
  86.  
  87. local function distToCurrent()
  88.     local x, _, z = gps.locate(5)
  89.     return dist(parcours[current].x - x, parcours[current].z - z)
  90. end
  91.  
  92. local function goToCurrent()
  93.     local x, y, z = gps.locate(5)
  94.     local r = ni.walk(parcours[current].x - x, parcours[current].y - y, parcours[current].z - z)
  95.     if not r then
  96.         nextPoint()
  97.         print("ERROR: cannot reach destination n°" .. tostring(current) .. ".")
  98.     end
  99. end
  100.  
  101. local function shootAt(x,y,z,p)
  102.     local d = dist(x,z)
  103.    
  104.     local phi = -90
  105.     local theta = 0
  106.     if d == 0 then
  107.         local sign = y/math.abs(y)
  108.         phi = sign*phi
  109.     else
  110.         local nz = z/d
  111.         theta = R2D*math.acos(nz)
  112.         if x > 0 then
  113.             theta = (-theta)%360
  114.         end
  115.  
  116.         local ny = y/d
  117.         phi = -R2D*math.atan(ny)
  118.     end
  119.  
  120.     ni.fire(theta, phi, p)
  121. end
  122.  
  123. local function scanArea()
  124.     local _, y, _ = gps.locate(5)
  125.     local r = ni.sense()
  126.     for _,v in pairs(r) do
  127.         if (not immu[string.lower(v.name)]) and v.y > -3 then
  128.             return v
  129.         end
  130.     end
  131. end
  132.  
  133. term.clear()
  134. term.setCursorPos(1,1)
  135. print("Patrol started !")
  136. while true do
  137.     mob = scanArea()
  138.  
  139.     if mob then
  140.         ni.walk(0,0,0)
  141.         local prev = ""
  142.         repeat
  143.             if not (mob.id == prev) then
  144.                 prev = mob.id
  145.                 print("Found entity " .. mob.name .. "! Started shooting !")
  146.             end
  147.             shootAt(mob.x, mob.y, mob.z, 0.5)
  148.             sleep(0)
  149.             mob = scanArea()
  150.  
  151.         until (not mob)
  152.         print("No more entity to shoot. Continuing patrol.")
  153.     else
  154.         if distToCurrent() < 2 then
  155.             nextPoint()
  156.         end
  157.  
  158.         if not ni.isWalking() then
  159.             goToCurrent()
  160.         end
  161.     end
  162.  
  163.     sleep(1)
  164. end
Advertisement
Add Comment
Please, Sign In to add comment