Wrecking Crew Posted January 10, 2018 Posted January 10, 2018 I need help to get a script working that will detect any client at a defined altitude. The client unit names are in the table called 'aircraftClientBlueNames' (this 'aircraftClientBlueNames' part works). I don't get errors with this Detect Altitude code, but it is not returning the outText. This is set up on a Continuous Action with no Conditions and Action of Do Script. TIA. -- code.Load Client Detect Altitude do local function contains(tbl, val) for _,v in ipairs(tbl) do if v == val then return true end end return false end for uName, uData in pairs(aircraftClientBlueNames) do local altitude = 2500 -- meters local airborneUnitName = uName if(contains(aircraftClientBlueNames, airborneUnitName)) then local pos = unit:getPosition().p local height = land.getHeight({x = pos.x, y = pos.z}) if pos.y - height > alt then -- return true trigger.action.setUserFlag(50008, true) trigger.action.outText('Blue Client ' .. airborneUnitName .. ' is at 2500 meters altitude!', 5) end end end end Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.
Chump Posted January 10, 2018 Posted January 10, 2018 if pos.y - height > alt thenalt is not declared, but altitude is.
Wrecking Crew Posted January 11, 2018 Author Posted January 11, 2018 Thanks, Chump, I appreciate the extra set of eyeballs on the code above. Fixing 'altitude' did not make it work, though. Does anyone have an example to point to that will return the altitude of an aircraft? Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.
Stonehouse Posted January 12, 2018 Posted January 12, 2018 (edited) Flak script does something like that to check whether the target is above the min alt of the flak battery. Can't recall right now whether it uses Mist or native DCS scripting to do it. Also not in a position to open the script up and check right now sorry. It would be in the addtgt function and should derive the approx. future target position including it's height above land at that point and then check to see if it is above the min altitude and if it is then it adds it to the table of targets. https://www.digitalcombatsimulator.com/en/files/683612/ Edited January 12, 2018 by Stonehouse sorry typo max should have been min.
Chump Posted January 12, 2018 Posted January 12, 2018 I had some time to look at it tonight. Here is a working script: do local aircraftClientBlueNames = {} local u = Unit.getByName("test") if u then aircraftClientBlueNames["test"] = u end local function contains(tbl, val) for k, _ in pairs(tbl) do if k == val then return true end end return false end for uName, uData in pairs(aircraftClientBlueNames) do local altitude = 2500 -- meters local airborneUnitName = uName if (contains(aircraftClientBlueNames, airborneUnitName)) then local pos = uData:getPosition().p local height = land.getHeight({x = pos.x, y = pos.z}) if pos.y - height >= altitude then trigger.action.setUserFlag(50008, true) trigger.action.outText('Blue Client ' .. airborneUnitName .. ' is at 2500 meters altitude!', 5) end end end endObviously I had to take some liberties, not knowing what your data structure looked like for aircraftClientBlueNames. I also modified your contains method to look up key instead of value (based on my data structure). You were using unit:getPosition() when unit wasn't declared also. I changed that to uData to use the aircraftClientBlueNames table data.
Wrecking Crew Posted January 12, 2018 Author Posted January 12, 2018 Thank you, guys! Stonehouse, yes, i use the flak stuff often in my missions. I will take a look at that. Chump, wow, I'm surprised that i got as close as I did. I'm an expert in ancient VB, and LUA and Python are a bit of a struggle to me. Ill give this a go. The new mission's objective will be triggered by the altitude that a client climbs up to. Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.
Wrecking Crew Posted January 13, 2018 Author Posted January 13, 2018 Stonehouse - I looked at the flak code and that uses spherical zones. I need a set altitude anywhere on the map. Chump - Your script is working in the attached mission. But I want to use the unit names from my global table 'aircraftClientBlueNames'; I am doing other things with it. When I try to use my table in your script nothing is returned, maybe because I'm not calling it correctly or because it only contains the unit names from its source of 'mist.DBs.humansByName'. The content of 'aircraftClientBlueNames' is like: 'test' 'test #001' I added a Radio F10 command to display the content. Can you please take a look at my mission? Your script is in the Continuous Action event.Detect Altitude 220N0a.miz Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.
Chump Posted January 14, 2018 Posted January 14, 2018 Now that I see your data structure, just do this: do for _, airborneUnitName in pairs(aircraftClientBlueNames) do local altitude = 2200 -- meters local uData = Unit.getByName(airborneUnitName) if uData then local pos = uData:getPosition().p if pos.y >= altitude then -- trigger.action.setUserFlag(50008, true) trigger.action.outText('Blue Client ' .. airborneUnitName .. ' is at 2200 meters altitude!', 5) -- trigger.action.outText(mist.utils.tableShow(aircraftClientBlueNamesAltitude), 6) end end end endYou don't need the contains method. 1
Stonehouse Posted January 14, 2018 Posted January 14, 2018 (edited) Sorry WreckingCrew not quite right, I finally had a moment to recheck the script. It uses zones but not for checking the min altitude. It uses the zones to identify the target aircraft whereas you know this already. As per Chump's example the flak script does: local _targetpos = inZoneUnits:getPosition().p You'd just substitute the unit you want to check instead of inZoneUnits in the above and then the altitude is the y component which is referenced as per Chump using _targetpos.y The table entry inZoneUnits is identical to the concept of Chump's table aircraftClientBlueNames except that instead of being populated with unit names it is already populated with the unit reference that is given by Unit.getByName() Anyway as usual with these things there are 100 different ways to get to the same result. However if you want AGL rather than ASL altitude then you need also need to consider the land height at the target's position. This is done using the x and z components of the target position. So something like: local _landht = land.getHeight{x=_targetpos.x, y = _targetpos.z} So the AGL alt is _targetpos.y - _landht You can then compare this to your desired check altitude eg local _targetAGLAlt = _targetpos.y - _landht if _targetAGLAlt >= your desired check alt then ..blah ..blah end Edited January 14, 2018 by Stonehouse 1
Wrecking Crew Posted January 14, 2018 Author Posted January 14, 2018 This is working on a Continuous Action event - do for _, airborneUnitName in pairs(aircraftClientBlueNames) do local detectAltitude = 2200 -- meters local uData = Unit.getByName(airborneUnitName) if uData then local pos = uData:getPosition().p if pos.y >= detectAltitude then trigger.action.setUserFlag(50022, true) trigger.action.outText('Blue Client ' .. airborneUnitName .. ' is at ' .. pos.y .. ' meters altitude!', 5) end end end end And it outputs the text for multiple clients, giving the unit names with altitude, and sets the flag. Does it make sense to run the function on a Mission Start event with an event handler instead of a Continuous Action? I don't want to bog down the mission if there are a number of clients flying, but below the detection altitude. I tried wrapping this ^^^ code with an event handler - see the last Mission Start event in the attached mission. But again, I struggle with the syntax, and wonder if there is even an 'event' that would trigger that approach? Thanks for all the help. This forum and peeps are a great resource! 1 Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.
Chump Posted January 15, 2018 Posted January 15, 2018 By event handler, I'm guessing that you mean a timed occurrence of executing this block of code? Last I knew, the mission editor trigger (continuous action) executes once per second. How often do you want it to run? IMO, this script should cause you no slowdown.
Wrecking Crew Posted January 15, 2018 Author Posted January 15, 2018 Thanks, Chump. I was basing my thought on the code that detects client takeoffs, that is on a 'takeoff' event handler, but after thinking about it more, that is also running every second. For this new detect altitude code I will stay with a Continuous Action event, and include a Condition to stop it once its flag is set. Drag80 - it is not a cheat. These codes that detect client takeoffs and altitudes are what I'm using to delay the action in my multiplayer missions until an aircraft client is flying. My missions auto-reload after a Blue or Red win, or a timeout. I want to delay any scripted action after the reload until a client (or player in SP) actually starts flying. My missions are free to fly and modify, and I want it to be easy for anybody to add aircraft to them, without needing to hard-code the new a/c unit name into a detection script or event, or require that a new a/c be placed where it will fly into or out of a zone. These scripts will detect any client takeoff, or any client reaching a certain altitude, on any map. Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.
Drag80 Posted January 15, 2018 Posted January 15, 2018 Thanks, Chump. I was basing my thought on the code that detects client takeoffs, that is on a 'takeoff' event handler, but after thinking about it more, that is also running every second. For this new detect altitude code I will stay with a Continuous Action event, and include a Condition to stop it once its flag is set. Drag80 - it is not a cheat. These codes that detect client takeoffs and altitudes are what I'm using to delay the action in my multiplayer missions until an aircraft client is flying. My missions auto-reload after a Blue or Red win, or a timeout. I want to delay any scripted action after the reload until a client (or player in SP) actually starts flying. My missions are free to fly and modify, and I want it to be easy for anybody to add aircraft to them, without needing to hard-code the new a/c unit name into a detection script or event, or require that a new a/c be placed where it will fly into or out of a zone. These scripts will detect any client takeoff, or any client reaching a certain altitude, on any map. i see. understood. :)
Recommended Posts