Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- TO INSTALL JUST EXECUTE: pastebin get g7y0XkJV /etc/rc.d/turret.lua
- local component = require("component")
- local shell = require("shell")
- local thread = require("thread")
- local turretCfg = require("turretCfg")
- local logFilePath
- local turrets = {}
- local detector = {}
- local excluded = {}
- local operationArea
- local operationDistance
- local lowestGroundLevel
- local function logAiming(logText, num)
- file = io.open(logFilePath..num, "a")
- file:write(os.date().." - "..logText.."\n")
- file:close()
- end
- local function isInArea(x, z)
- if operationArea == "W-E" then
- if z <= 0 then return true else return false end
- elseif operationArea == "N-S" then
- if x >= 0 then return true else return false end
- elseif operationArea == "E-W" then
- if z >= 0 then return true else return false end
- elseif operationArea == "S-N" then
- if x <= 0 then return true else return false end
- else -- (Default)
- return true
- end
- end
- local function round(num, numDecimalPlaces)
- local mult = 10^(numDecimalPlaces or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- ---- scan for turret's and wrap them as proxy
- local function initialize_turrets(turretsID)
- for k in pairs(turretsID) do
- table.insert(turrets, component.proxy(turretsID[k]))
- end
- end
- ---- scan for turret's and wrap them as proxy
- local function initialize_detector(detectorID)
- for k in pairs(detectorID) do
- table.insert(detector, component.proxy(detectorID[k]))
- end
- end
- local function activateGun(num)
- turrets[num].powerOn()
- turrets[num].setArmed(true)
- end
- local function deactivateGun(num)
- turrets[num].powerOff()
- end
- -- find the closest enemy in range based on mob list in turretLib.lua
- local function detectEntities(num)
- local currentEntity = "nothing"
- local currentX = 0
- local currentY = 0
- local currentZ = 0
- local currentRange = 100
- local entities = detector[num].scanEntities(operationDistance)
- if entities ~= false and #entities ~= 0 then
- for k in pairs(entities) do
- isItem, _ = string.find(entities[k].name, "item.")
- if not isItem then
- local isExcluded = false
- for l in pairs(excluded) do
- if entities[k].name == excluded[l] then
- isExcluded = true
- end
- end
- if isExcluded == false then
- if entities[k].y >= lowestGroundLevel then
- if entities[k].range < currentRange then
- if isInArea(entities[k].x, entities[k].z) == true then
- currentEntity = entities[k].name
- currentX = entities[k].x
- currentY = entities[k].y + (-1)
- currentZ = entities[k].z
- currentRange = entities[k].range
- -- else
- -- logAiming(entities[k].name.." is out of operation area", num)
- end
- end
- end
- -- else
- -- logAiming(entities[k].name.." is excluded from atacking", num)
- end
- -- else
- -- logAiming("is item: "..entities[k].name, num)
- end
- end
- end
- return currentEntity, currentX, currentY, currentZ
- end
- -- aim at the found enemy
- local function aiming(currentX, currentY, currentZ, num)
- local horizontal = round(math.deg(math.atan(currentZ/currentX)), 2)
- local vertikal = round(math.deg(math.atan(currentY/(math.sqrt(math.pow(currentX,2)+math.pow(currentZ,2))))), 2)
- if currentX <= 0 then horizontal = horizontal + 270 end
- if currentX >= 0 then horizontal = horizontal + 90 end
- turrets[num].moveTo(horizontal, vertikal)
- return horizontal, vertikal
- end
- -- RUNTIME STARTS HERE --
- function start()
- logFilePath = turretCfg.loadLogFilePath()
- initialize_turrets(turretCfg.loadTurretList())
- initialize_detector(turretCfg.loadDetectorList())
- excluded = turretCfg.loadExcludedList()
- operationArea = turretCfg.loadWorkingArea()
- operationDistance = turretCfg.loadWorkingDistance()
- lowestGroundLevel = turretCfg.loadLowestGroundLevel()
- for num in pairs(turrets) do
- logAiming(" <--- TURRET IS ONLINE --->\n", num)
- local detached_GunThread = thread.create(function()
- while true do
- local entityName, x, y, z = detectEntities(num)
- if entityName ~= "nothing" then
- activateGun(num)
- horizontal, vertical = aiming(x, y, z, num)
- -- logAiming("\n Rotate Gun at: "..horizontal.."° horizontal and: "..vertical.."° vertical", num)
- logAiming(" Aiming at: "..entityName, num) --..". Distance from gun: x="..x.." y="..y.." z="..z, num)
- os.sleep(.5)
- if turrets[num].isOnTarget() == true then
- -- logAiming(" FIRE..\n\n", num)
- local fired, error = turrets[num].fire()
- if fired == false then
- logAiming("Unable to fire: " .. error, num)
- end
- else
- logAiming("target is not aimed yet...", num)
- end
- deactivateGun(num)
- else
- -- nothing to shoot sleeping
- os.sleep(1)
- end
- end
- end):detach()
- end
- end
Add Comment
Please, Sign In to add comment