Grauly

CC_IEC-OS turret v1

May 8th, 2021 (edited)
583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.27 KB | None | 0 0
  1. local IECNET = true
  2. -- end of config
  3. local args = {...}
  4. local label = "Area"
  5.  
  6. if(type(args[1]) == "string") then
  7.     label = ""
  8.     for k,v in pairs(args) do
  9.         label = label..v.." "
  10.     end
  11. else
  12.     label = "Missing Label"
  13. end
  14.  
  15. --IECNET setup
  16. local gpsEnabled = false
  17. local net = "Turretnet"
  18. local HOST = -1
  19. function iecnetSetup()
  20.     if (IECNET) then
  21.         local names = peripheral.getNames()
  22.         for i = 1, #names do
  23.             local name  = names[i]
  24.             -- If it is a modem then
  25.             if peripheral.getType(name) == "modem" then
  26.                 if peripheral.call(name, "isWireless") then
  27.                     modemSide = name
  28.                 end
  29.             end
  30.         end
  31.         if (peripheral.isPresent(modemSide)) then
  32.             if (peripheral.getType(modemSide) == "modem") then
  33.                 --peripheral is present, starting IECNET
  34.                 rednet.open(modemSide)
  35.                 HOST = rednet.lookup(net,"IEC-Host")
  36.                 if (HOST == nil) then
  37.                     print(net.." does not have a Host or Host is Offline, broadcasts will go unheard.")
  38.                     IECNET = false
  39.                 end
  40.                 x,y,z = gps.locate()
  41.                 if (x == nil) then
  42.                     print("GPS not available, will not send Location Data")
  43.                 else
  44.                     gpsEnabled = true;
  45.                 end
  46.                 rednet.broadcast("Online",net)
  47.             else
  48.                 print("IECNET was enabled, but no modem was found.")
  49.                 IECNET = false
  50.             end
  51.         else
  52.             print("IECNET was enabled, but no modem was found.")
  53.             IECNET = false
  54.         end
  55.     end
  56. end
  57.  
  58. function getLocation()
  59.     x,y,z = gps.locate()
  60.     if (x == nil) then
  61.         gpsEnabled = false
  62.         return nil
  63.     else
  64.         if (not gpsEnabled) then
  65.             gpsEnabled = true
  66.         end
  67.         x = math.floor(x+0.5)
  68.         y = math.floor(y+0.5)
  69.         z = math.floor(z+0.5)
  70.         return tostring(x).." "..tostring(y).." "..tostring(z)
  71.     end
  72. end
  73. --end of IECNET primary functions
  74. local turrets = {}
  75.  
  76. function getTurrets()
  77.     connected = peripheral.getNames()
  78.     turretCount = 0
  79.     for c=1, #connected do
  80.         if (peripheral.getType(connected[c]) == "turret_base") then
  81.             table.insert(turrets,peripheral.wrap(connected[c]))
  82.             turretCount = turretCount +1
  83.         end
  84.     end
  85.     print("Found "..turretCount.." Turrets.")
  86. end
  87.  
  88. function querryHost()
  89.     HOST = rednet.lookup(net,"IEC-Host")
  90.     if (HOST == nil) then
  91.         IECNET = false
  92.         registered = false
  93.     end
  94. end
  95.  
  96. local registered = false
  97. function register()
  98.     querryHost()
  99.     if ((not registered) and IECNET) then
  100.         message = {}
  101.         message["command"] = "register"
  102.         message["input"] = label
  103.         rednet.send(HOST,message,net)
  104.     end
  105. end
  106.  
  107. local attackNeutrals = false
  108. local attackMobs = false
  109. local attackPlayers = false
  110. function updateTurrets()
  111.     for t=1, #turrets do
  112.         turrets[t].setAttacksNeutrals(attackNeutrals)
  113.         turrets[t].setAttacksMobs(attackMobs)
  114.         turrets[t].setAttacksPlayers(attackPlayers)
  115.     end
  116. end
  117.  
  118. --rednet handeling
  119. local ticksSinceLastPing = 0
  120. function rednetHandeling(sender,message,prot)
  121.     if (message == "pong" and prot == net) then --online checker
  122.         ticksSinceLastPing = 0
  123.         querryHost()
  124.         return
  125.     elseif (message == "registered" and prot == net) then
  126.         registered = true
  127.         print("Succesfully registred")
  128.     elseif (message == "Turretnet reboot") then
  129.         if (sender == HOST) then
  130.             shell.run("reboot")
  131.         end
  132.     elseif (type(message) == "table" and prot == net) then
  133.         if (sender == HOST) then
  134.             if (type(message["n"]) == "boolean") then
  135.                 attackNeutrals = message["n"]
  136.             end
  137.             if (type(message["m"]) == "boolean") then
  138.                 attackMobs = message["m"]
  139.             end
  140.             if (type(message["p"]) == "boolean") then
  141.                 attackPlayers = message["p"]
  142.             end
  143.         end
  144.         updateTurrets()
  145.     end
  146. end
  147.  
  148. --routine, gets executed in intervalls
  149. function routine()
  150.     if (IECNET) then
  151.         register()
  152.         querryHost()
  153.     else
  154.         registered = false
  155.     end
  156.     updateTurrets()
  157.     if (ticksSinceLastPing > 5) then
  158.         IECNET = false
  159.     else
  160.         IECNET = true
  161.     end
  162.     rednet.broadcast("ping",net) --ping the network to see if it is online
  163. end
  164.  
  165. --program starts here
  166. iecnetSetup()
  167. getTurrets()
  168. routine()
  169.  
  170. local timer = os.startTimer(30)
  171.  
  172. while true do
  173.     event,a1,a2,a3 = os.pullEvent()
  174.     if (event == "timer" and a1 == timer) then
  175.         ticksSinceLastPing = ticksSinceLastPing +1
  176.         routine()
  177.         timer = os.startTimer(30)
  178.     elseif (event == "rednet_message") then
  179.         rednetHandeling(a1,a2,a3)
  180.     end
  181. end
Add Comment
Please, Sign In to add comment