pandorrama

turret_rc

Sep 12th, 2020 (edited)
906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.08 KB | None | 0 0
  1. -- TO INSTALL JUST EXECUTE: pastebin get g7y0XkJV /etc/rc.d/turret.lua
  2.  
  3. local component = require("component")
  4. local shell = require("shell")
  5. local thread = require("thread")
  6. local turretCfg = require("turretCfg")
  7.  
  8. local logFilePath
  9. local turrets = {}
  10. local detector = {}
  11. local excluded = {}
  12. local operationArea
  13. local operationDistance
  14. local lowestGroundLevel
  15.  
  16.  
  17. local function logAiming(logText, num)
  18.   file = io.open(logFilePath..num, "a")
  19.   file:write(os.date().." - "..logText.."\n")
  20.   file:close()
  21. end
  22.  
  23. local function isInArea(x, z)
  24.   if operationArea == "W-E" then
  25.     if z <= 0 then return true else return false end
  26.   elseif operationArea == "N-S" then
  27.     if x >= 0 then return true else return false end
  28.   elseif operationArea == "E-W" then
  29.     if z >= 0 then return true else return false end
  30.   elseif operationArea == "S-N" then
  31.     if x <= 0 then return true else return false end
  32.   else -- (Default)
  33.     return true
  34.   end
  35. end
  36.  
  37. local function round(num, numDecimalPlaces)
  38.   local mult = 10^(numDecimalPlaces or 0)
  39.   return math.floor(num * mult + 0.5) / mult
  40. end
  41.  
  42. ---- scan for turret's and wrap them as proxy
  43. local function initialize_turrets(turretsID)
  44.   for k in pairs(turretsID) do
  45.     table.insert(turrets, component.proxy(turretsID[k]))
  46.   end
  47. end
  48.  
  49. ---- scan for turret's and wrap them as proxy
  50. local function initialize_detector(detectorID)
  51.   for k in pairs(detectorID) do
  52.     table.insert(detector, component.proxy(detectorID[k]))
  53.   end
  54. end
  55.  
  56. local function activateGun(num)
  57.   turrets[num].powerOn()
  58.   turrets[num].setArmed(true)
  59. end
  60.  
  61. local function deactivateGun(num)
  62.   turrets[num].powerOff()
  63. end
  64.  
  65. -- find the closest enemy in range based on mob list in turretLib.lua
  66. local function detectEntities(num)
  67.   local currentEntity = "nothing"
  68.   local currentX = 0
  69.   local currentY = 0
  70.   local currentZ = 0
  71.   local currentRange = 100
  72.   local entities = detector[num].scanEntities(operationDistance)
  73.  
  74.   if entities ~= false and #entities ~= 0 then
  75.     for k in pairs(entities) do
  76.       isItem, _ = string.find(entities[k].name, "item.")
  77.       if not isItem then
  78.         local isExcluded = false
  79.         for l in pairs(excluded) do
  80.           if entities[k].name == excluded[l] then
  81.             isExcluded = true
  82.           end
  83.         end
  84.         if isExcluded == false then
  85.           if entities[k].y >= lowestGroundLevel then
  86.             if entities[k].range < currentRange then
  87.               if isInArea(entities[k].x, entities[k].z) == true then
  88.                 currentEntity = entities[k].name
  89.                 currentX = entities[k].x
  90.                 currentY = entities[k].y + (-1)
  91.                 currentZ = entities[k].z
  92.                 currentRange = entities[k].range
  93. --              else
  94. --                logAiming(entities[k].name.." is out of operation area", num)
  95.               end
  96.             end
  97.           end
  98. --        else
  99. --          logAiming(entities[k].name.." is excluded from atacking", num)
  100.         end
  101. --      else
  102. --        logAiming("is item: "..entities[k].name, num)
  103.       end
  104.     end
  105.   end
  106.   return currentEntity, currentX, currentY, currentZ
  107. end
  108.  
  109. -- aim at the found enemy
  110. local function aiming(currentX, currentY, currentZ, num)
  111.   local horizontal = round(math.deg(math.atan(currentZ/currentX)), 2)
  112.   local vertikal = round(math.deg(math.atan(currentY/(math.sqrt(math.pow(currentX,2)+math.pow(currentZ,2))))), 2)
  113.  
  114.   if currentX <= 0 then horizontal = horizontal + 270 end
  115.   if currentX >= 0 then horizontal = horizontal + 90  end
  116.  
  117.   turrets[num].moveTo(horizontal, vertikal)
  118.   return horizontal, vertikal
  119. end
  120.  
  121.  
  122. -- RUNTIME STARTS HERE --
  123.  
  124. function start()
  125.   logFilePath = turretCfg.loadLogFilePath()
  126.   initialize_turrets(turretCfg.loadTurretList())
  127.   initialize_detector(turretCfg.loadDetectorList())
  128.   excluded = turretCfg.loadExcludedList()
  129.   operationArea = turretCfg.loadWorkingArea()
  130.   operationDistance = turretCfg.loadWorkingDistance()
  131.   lowestGroundLevel = turretCfg.loadLowestGroundLevel()
  132.  
  133.   for num in pairs(turrets) do
  134.     logAiming(" <--- TURRET IS ONLINE --->\n", num)
  135.     local detached_GunThread = thread.create(function()
  136.       while true do
  137.         local entityName, x, y, z = detectEntities(num)
  138.         if entityName ~= "nothing" then
  139.           activateGun(num)
  140.           horizontal, vertical = aiming(x, y, z, num)
  141. --          logAiming("\n Rotate Gun at: "..horizontal.."° horizontal and: "..vertical.."° vertical", num)
  142.           logAiming(" Aiming at: "..entityName, num) --..". Distance from gun: x="..x.." y="..y.." z="..z, num)
  143.           os.sleep(.5)
  144.           if turrets[num].isOnTarget() == true then
  145. --            logAiming(" FIRE..\n\n", num)
  146.             local fired, error = turrets[num].fire()
  147.             if fired == false then
  148.               logAiming("Unable to fire: " .. error, num)
  149.             end
  150.           else
  151.             logAiming("target is not aimed yet...", num)
  152.           end
  153.           deactivateGun(num)
  154.         else
  155.           -- nothing to shoot sleeping
  156.           os.sleep(1)
  157.         end
  158.       end
  159.     end):detach()
  160.   end
  161. end
Add Comment
Please, Sign In to add comment