Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local IECNET = true
- -- end of config
- local args = {...}
- local label = "Area"
- if(type(args[1]) == "string") then
- label = ""
- for k,v in pairs(args) do
- label = label..v.." "
- end
- else
- label = "Missing Label"
- end
- --IECNET setup
- local gpsEnabled = false
- local net = "Turretnet"
- local HOST = -1
- function iecnetSetup()
- if (IECNET) then
- local names = peripheral.getNames()
- for i = 1, #names do
- local name = names[i]
- -- If it is a modem then
- if peripheral.getType(name) == "modem" then
- if peripheral.call(name, "isWireless") then
- modemSide = name
- end
- end
- end
- if (peripheral.isPresent(modemSide)) then
- if (peripheral.getType(modemSide) == "modem") then
- --peripheral is present, starting IECNET
- rednet.open(modemSide)
- HOST = rednet.lookup(net,"IEC-Host")
- if (HOST == nil) then
- print(net.." does not have a Host or Host is Offline, broadcasts will go unheard.")
- IECNET = false
- end
- x,y,z = gps.locate()
- if (x == nil) then
- print("GPS not available, will not send Location Data")
- else
- gpsEnabled = true;
- end
- rednet.broadcast("Online",net)
- else
- print("IECNET was enabled, but no modem was found.")
- IECNET = false
- end
- else
- print("IECNET was enabled, but no modem was found.")
- IECNET = false
- end
- end
- end
- function getLocation()
- x,y,z = gps.locate()
- if (x == nil) then
- gpsEnabled = false
- return nil
- else
- if (not gpsEnabled) then
- gpsEnabled = true
- end
- x = math.floor(x+0.5)
- y = math.floor(y+0.5)
- z = math.floor(z+0.5)
- return tostring(x).." "..tostring(y).." "..tostring(z)
- end
- end
- --end of IECNET primary functions
- local turrets = {}
- function getTurrets()
- connected = peripheral.getNames()
- turretCount = 0
- for c=1, #connected do
- if (peripheral.getType(connected[c]) == "turret_base") then
- table.insert(turrets,peripheral.wrap(connected[c]))
- turretCount = turretCount +1
- end
- end
- print("Found "..turretCount.." Turrets.")
- end
- function querryHost()
- HOST = rednet.lookup(net,"IEC-Host")
- if (HOST == nil) then
- IECNET = false
- registered = false
- end
- end
- local registered = false
- function register()
- querryHost()
- if ((not registered) and IECNET) then
- message = {}
- message["command"] = "register"
- message["input"] = label
- rednet.send(HOST,message,net)
- end
- end
- local attackNeutrals = false
- local attackMobs = false
- local attackPlayers = false
- function updateTurrets()
- for t=1, #turrets do
- turrets[t].setAttacksNeutrals(attackNeutrals)
- turrets[t].setAttacksMobs(attackMobs)
- turrets[t].setAttacksPlayers(attackPlayers)
- end
- end
- --rednet handeling
- local ticksSinceLastPing = 0
- function rednetHandeling(sender,message,prot)
- if (message == "pong" and prot == net) then --online checker
- ticksSinceLastPing = 0
- querryHost()
- return
- elseif (message == "registered" and prot == net) then
- registered = true
- print("Succesfully registred")
- elseif (message == "Turretnet reboot") then
- if (sender == HOST) then
- shell.run("reboot")
- end
- elseif (type(message) == "table" and prot == net) then
- if (sender == HOST) then
- if (type(message["n"]) == "boolean") then
- attackNeutrals = message["n"]
- end
- if (type(message["m"]) == "boolean") then
- attackMobs = message["m"]
- end
- if (type(message["p"]) == "boolean") then
- attackPlayers = message["p"]
- end
- end
- updateTurrets()
- end
- end
- --routine, gets executed in intervalls
- function routine()
- if (IECNET) then
- register()
- querryHost()
- else
- registered = false
- end
- updateTurrets()
- if (ticksSinceLastPing > 5) then
- IECNET = false
- else
- IECNET = true
- end
- rednet.broadcast("ping",net) --ping the network to see if it is online
- end
- --program starts here
- iecnetSetup()
- getTurrets()
- routine()
- local timer = os.startTimer(30)
- while true do
- event,a1,a2,a3 = os.pullEvent()
- if (event == "timer" and a1 == timer) then
- ticksSinceLastPing = ticksSinceLastPing +1
- routine()
- timer = os.startTimer(30)
- elseif (event == "rednet_message") then
- rednetHandeling(a1,a2,a3)
- end
- end
Add Comment
Please, Sign In to add comment