Matrix Games Forums

Forums  Register  Login  Photo Gallery  Member List  Search  Calendars  FAQ 

My Profile  Inbox  Address Book  My Subscription  My Forums  Log Out

Scan An Area for Units

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
  Printable Version
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> Scan An Area for Units Page: [1]
Login
Message << Older Topic   Newer Topic >>
Scan An Area for Units - 4/12/2020 4:08:55 PM   
vettim89


Posts: 3615
Joined: 7/14/2007
From: Toledo, Ohio
Status: offline
I am looking to create a hostage rescue scenario where rescue units will be inserted by air. In order to complete the scenario the player will have to pick up all the rescue team. I am trying to figure out a way to be able for the game to scan the AOE for any remaining units.

So the event trigger is set to "unit exits area". I need to create a script for the game to scan the AOE for any additional units. If none are present this will trigger the scenario end as the mission will be accomplished. If friends are still present the scenario will continue

In short, how do I make the game scan an area for friendly units?

_____________________________

"We have met the enemy and they are ours" - Commodore O.H. Perry
Post #: 1
RE: Scan An Area for Units - 4/15/2020 2:51:51 AM   
michaelm75au


Posts: 13500
Joined: 5/5/2001
From: Melbourne, Australia
Status: offline
Not sure of this but
Have an exit Event
Add (unit exits) trigger
Add a condition that checks to see if any of the units are still in the area (unit.inArea(...)), and return 'false' so event is cancelled

_____________________________

Michael

(in reply to vettim89)
Post #: 2
RE: Scan An Area for Units - 4/15/2020 4:58:45 PM   
vettim89


Posts: 3615
Joined: 7/14/2007
From: Toledo, Ohio
Status: offline
Yes, I see now how to do it. I am setting up an exclusion zone for the bad guys around the Area of Operation. Then setting up a condition to basically look for any good guys in the Exclusion Zone

Not having used conditions before, how do I get the lua script to return either "true" or "false" to the console so it knows if the event should proceed?



_____________________________

"We have met the enemy and they are ours" - Commodore O.H. Perry

(in reply to vettim89)
Post #: 3
RE: Scan An Area for Units - 4/15/2020 7:25:34 PM   
vettim89


Posts: 3615
Joined: 7/14/2007
From: Toledo, Ohio
Status: offline
So in full disclosure I attempted to clone this code from the SAM Response scenario by Zathred. However the code is not working

Here is what I wrote:

local badguys = VP_GetSide({Name='Bad Guys'})
local goodguys = VP_GetSide({Name='Good Guys'})
local c = VP_GetSide({goodguys.guid})
local AOO_Zone = badguys:getexclusionzone('Zone')
local z = AOEZone.area
local AOO_area = {}

for i,v in ipairs(z) do
table.insert(AOE_area, v.guid)
end

if (c ~= nil) then
for i,v in ipairs(c) do
if (v ~= nil and (v.type == 'Facility' and v.subtype == 'Mobile_Personnel')) then
local u = ScenEdit_GetUnit({guid=v.actualunitid})

if (v:inArea(AOEarea)) then
return false
end
end
return true
end
end

The Lua console tells me "it cannot find side" for one of the calls. Anyone who is more adept at Lua able to help me?


_____________________________

"We have met the enemy and they are ours" - Commodore O.H. Perry

(in reply to vettim89)
Post #: 4
RE: Scan An Area for Units - 4/15/2020 8:10:45 PM   
Whicker

 

Posts: 664
Joined: 6/20/2018
Status: offline
the 3rd line doesn't look right to me, it would be the same as line 2 as far as i can tell.

If line 3 is needed I think you need to have guid=goodguys.guid not just goodguys.guid.

Best way to trouble shoot is to take stuff out and use print statements to see if it is doing what you want, then build it up.

(in reply to vettim89)
Post #: 5
RE: Scan An Area for Units - 4/16/2020 6:36:01 AM   
KnightHawk75

 

Posts: 1450
Joined: 11/15/2018
Status: offline
So if I understand right you just want to know if all the good guys are out of the area at a give time right?

See attached sample scenario, choose a in editor. I threw all the code into a special action, move the units around and use the button to test results. When satisfied just copy it all into an lua action and create and event that fires however often you want the check done (or however else you chose to use it like in a condition), though I suggest 1min or more as it's a heavy function if a side has a ton of units of the type and\or subtype you're filtering though.

requires: 3 or more Reference points on Bad Guy's side (in the sample they are rp4,5,6,7 and part of a bad guy exclusion zone, but they don't have to be part of such). In fact you really don't even need RP's defined at all if you say want to just feed it a table of hard coded latitudes and longitudes points like
r = {{latitude=0.0,longitude=0.0},{latitude=0.0,longitude=0.0},
{latitude=0.0,longitude=0.0},{latitude=0.0,longitude=0.0}} etc.

Code does not require any inarea triggers to exist or have fired on a unit, nor does it even use unit:inArea() as it has it's own inPolygon checking function which I find more reliable for this sort of thing.

Note when looking at the non-function code in the special action:
checkAreaForUnits('Good Guys',r,'Facility','5002')

The last parameter to filter on subtype is optional, you can leave it blank or include the proper subtype code, 5002 is mobile personnel, 5001 is mobile vehicle,etc.


Attachment (1)

< Message edited by KnightHawk75 -- 4/16/2020 7:07:50 AM >

(in reply to vettim89)
Post #: 6
RE: Scan An Area for Units - 4/16/2020 9:53:30 PM   
vettim89


Posts: 3615
Joined: 7/14/2007
From: Toledo, Ohio
Status: offline
Knighthawk75,

I am getting a DB error when trying to load your scenario

_____________________________

"We have met the enemy and they are ours" - Commodore O.H. Perry

(in reply to KnightHawk75)
Post #: 7
RE: Scan An Area for Units - 4/16/2020 10:00:26 PM   
michaelm75au


Posts: 13500
Joined: 5/5/2001
From: Melbourne, Australia
Status: offline
It wants the latest DB3K_483.db3

_____________________________

Michael

(in reply to vettim89)
Post #: 8
RE: Scan An Area for Units - 4/17/2020 1:53:29 AM   
KnightHawk75

 

Posts: 1450
Joined: 11/15/2018
Status: offline
quote:

ORIGINAL: vettim89

Knighthawk75,

I am getting a DB error when trying to load your scenario


It was saved with the latest beta CMO build 1142.2 and db 483. If you have that it should load, unless the upload got corrupted or something by I think Michael's comment suggests it loaded for him.

If you just want the text of the special action part (the meat) and not the whole scene I've attached it here in .txt doc, just copy and past into a repeatable Special Action. Pretty easy to recreate the rest, two sides 'Good Guys' 'Bad Guys', with Bad Guys having Refpoints named rp4,5,6,7 in square of whatever size you want. and throw a mobile-personnel, and some other non-mobile-personnel (for testing the unit type filtering) unit onto good guy side and press the SA to check if they're inside the rp's or not. If you're still on cmano the nuts of the code should still work as while I didn't test this specific script in cmano most of it was pulled from existing snippets that were from cmano days. Alternatively tell me DB version you want the sample in and I'll replicate it.


Attachment (1)

< Message edited by KnightHawk75 -- 4/17/2020 2:05:34 AM >

(in reply to vettim89)
Post #: 9
RE: Scan An Area for Units - 4/17/2020 2:36:16 AM   
vettim89


Posts: 3615
Joined: 7/14/2007
From: Toledo, Ohio
Status: offline

quote:

ORIGINAL: KnightHawk75

quote:

ORIGINAL: vettim89

Knighthawk75,

I am getting a DB error when trying to load your scenario


It was saved with the latest beta CMO build 1142.2 and db 483. If you have that it should load, unless the upload got corrupted or something by I think Michael's comment suggests it loaded for him.

If you just want the text of the special action part (the meat) and not the whole scene I've attached it here in .txt doc, just copy and past into a repeatable Special Action. Pretty easy to recreate the rest, two sides 'Good Guys' 'Bad Guys', with Bad Guys having Refpoints named rp4,5,6,7 in square of whatever size you want. and throw a mobile-personnel, and some other non-mobile-personnel (for testing the unit type filtering) unit onto good guy side and press the SA to check if they're inside the rp's or not. If you're still on cmano the nuts of the code should still work as while I didn't test this specific script in cmano most of it was pulled from existing snippets that were from cmano days. Alternatively tell me DB version you want the sample in and I'll replicate it.


Wow. Just wow! That is amazing

Give me a few days to incorporate it into my scenario and I will get back to you

_____________________________

"We have met the enemy and they are ours" - Commodore O.H. Perry

(in reply to KnightHawk75)
Post #: 10
RE: Scan An Area for Units - 4/17/2020 9:47:54 PM   
vettim89


Posts: 3615
Joined: 7/14/2007
From: Toledo, Ohio
Status: offline
First of all, Knighthawk, I tested your code and it worked perfectly. Thank you so much

But.....you knew there was going to be a but.....

to my chagrin, picking up a deployed unit does not qualify for activating the "unit leaves area" trigger.

Can anybody think of a work around? I was thinking about setting up an event trigger for real time that would just scan the Area of Operation say every 30 seconds. The event for that trigger could either be activated by another event within the game. Alternatively, I could instead create a Special Action. I am thinking special action would be the best way

Create a Special Action called "Exfil Complete". Once the player has all the units out of the AOO, they click on the action and the scan is run. If there are units still in the area a Message Box would pop up saying, "Friendly units still in AOO. Complete Exfil first" Otherwise the Special Action would trigger a points action and end the scenario

You more creative types might be able to come up with a more eloquent answer to the problem than this. Would love to hear suggestions

_____________________________

"We have met the enemy and they are ours" - Commodore O.H. Perry

(in reply to vettim89)
Post #: 11
RE: Scan An Area for Units - 4/17/2020 11:37:57 PM   
michaelm75au


Posts: 13500
Joined: 5/5/2001
From: Melbourne, Australia
Status: offline
That is an interesting point. I'm assuming it is using the normal a/c 'pickup'....
Being picked up means the unit gets removed (is added as cargo).

I'll have to check this - drop/pickup units were created after the 'unit enters/exit' events. Whereas the 'drop' would activate the 'enter area' trigger, the 'pickup' removes the unit so the 'exit area' trigger would not have a unit to check.

I suppose you could have an 'exit' trigger on the unit doing the pickup, and put a check on event that it has 'cargo' of the 'picked up' unit. But this assumes you have some control over what units can do the picking up.

_____________________________

Michael

(in reply to vettim89)
Post #: 12
RE: Scan An Area for Units - 4/18/2020 1:31:50 PM   
KnightHawk75

 

Posts: 1450
Joined: 11/15/2018
Status: offline
quote:

ORIGINAL: vettim89

First of all, Knighthawk, I tested your code and it worked perfectly. Thank you so much
But.....you knew there was going to be a but.....
to my chagrin, picking up a deployed unit does not qualify for activating the "unit leaves area" trigger.
...

You're welcome, glad it was indeed what you were looking for.
Yeah I know, i messed around and noticed that myself.

I mean you could do both.
A event and trigger that fires when 'units in area'. - > first time script triggers it enables the special action for manual checking, also enables the event to check every 1\5 minutes, stores keyvalue of say 1 noting it's been activated (which you check at the start of the script for >=1),that way subsequent firing the trigger doesn't do anything.

The event that runs every 1min\5min - > after being enabled does it's check and if all units are out disables itself, then disables \ hides special action, and messages user congrats on successful exfil, updates keyvalue to 2 denoting mission complete.

The enabled SA code just executes the already existing action for the event, ie a one liner of ExecuteEventAction('thename'). That way you shouldn't have to duplicate\copy paste any code.

Doing both covers cases where a player can force a manual check, but also if they're not paying attention or on hyper speed or something it's handled for them in relatively short order, and if they walked away from the computer they come back to the pop-up (and even if suppressed they'll get the scenario-end one..I think). But sounds like for your case SA alone approach should be fine, the rest is overkill with the idea the player could be focused elsewhere.
---

I actually can think of one based off the fact units get deleted on pickup, but it's more error prone, more code, involves more session persistence keys and I think probably less performant than the above. kind of don't even want to mention it unless you really want me to. btw speaking of perf you can remove the gKHPrint function and replace all the lines that use it with simple print statements or comment them out, product of copy and pasting from elsewhere and not bothering to optimizing for this specific sample.


< Message edited by KnightHawk75 -- 4/28/2020 1:16:18 AM >

(in reply to vettim89)
Post #: 13
RE: Scan An Area for Units - 9/4/2020 1:07:24 AM   
orca

 

Posts: 501
Joined: 11/6/2013
Status: offline
I love this code. Thanks for sharing. Is there a way to check to see if there is any mobile personal OR mobile vehicle units left in the area? ie check for both types and if there are any of either type then the result is "A unit is still in the area". If there is 1 type but not the other type, the result is still "A unit is still in the area". Only if there are no units of either type would the result be "No units left in the area"


(in reply to KnightHawk75)
Post #: 14
RE: Scan An Area for Units - 9/4/2020 12:29:01 PM   
KnightHawk75

 

Posts: 1450
Joined: 11/15/2018
Status: offline
quote:

ORIGINAL: orca

I love this code. Thanks for sharing. Is there a way to check to see if there is any mobile personal OR mobile vehicle units left in the area? ie check for both types and if there are any of either type then the result is "A unit is still in the area". If there is 1 type but not the other type, the result is still "A unit is still in the area". Only if there are no units of either type would the result be "No units left in the area"



I should actually re-work this now as it was written before the ~1146.x added additions in the side wrapper which has an inarea method (that effectively is same as inpolygon) and easier filtering options that let us pre-screen units better now.

That said, yes of course, just change the code to do that instead. :)
If I understand the request, should just need to call it twice using an 'or'.

For example assuming you want any 5001 or 5002 to trigger true:
-- user code here
local r = ScenEdit_GetReferencePoints({side="Bad Guys", area={"RP-4","RP-5","RP-6","RP-7"}})
local rval;

if (r ~=nil) then
  if( checkAreaForUnits('Good Guys',r,'Facility','5002') == true or checkAreaForUnits('Good Guys',r,'Facility','5001') == true) then
    print('A unit is still in the area');
  else
   print('No units left in area');
  end
end
r = nil;




(in reply to orca)
Post #: 15
RE: Scan An Area for Units - 11/23/2020 8:55:03 PM   
orca

 

Posts: 501
Joined: 11/6/2013
Status: offline
Is there a way to check to see if a specific aircraft (not any type of aircraft- either by name or GUID) is at a "base" rather than in an area?

(in reply to KnightHawk75)
Post #: 16
RE: Scan An Area for Units - 11/23/2020 11:21:54 PM   
KnightHawk75

 

Posts: 1450
Joined: 11/15/2018
Status: offline
quote:

ORIGINAL: orca

Is there a way to check to see if a specific aircraft (not any type of aircraft- either by name or GUID) is at a "base" rather than in an area?


A specific base, or do you just want to know if it's on the ground somewhere vs in the air?

If you just want to know if it's on the ground somewhere this will return false 99.9% of the time.
-- Function to return true if a it's very likely a aircraft unit is currently in the air.
-- takes in unit wrapper of unit to check
-- returns true if conditions indicate it's in the air, otherwise false.
function gKH.Unit:isAircraftAirborne(u)
  if ((u.condition_v == "Airborne") or u.condition_V == "RTB") or u.condition_V == "Landing_PreTouchdown" or
   u.condition_V == "ManoeuveringToRefuel" or u.condition_V == "Refuelling " or u.condition_V == "OffloadingFuel" or 
   u.condition_V == "DeployingDippingSonar" or u.condition_V == "EmergencyLanding" or  u.condition_V == "BVRAttack" or 
   u.condition_V == "BVRCrank" or u.condition_V == "Dogfight" or u.condition_V == "BVRDrag" or u.condition_V == "TransferringCargo" then
    return true;
  end
  return false;
end


If you want to check a specific base if it contains a specific unit guid:
-- Function to return true if a given unit wrapper currently contains the guid of a specific unit.
-- input u: The unit wrapper to check.
-- input theType: The string type unit to check "Aircraft or Boats".
-- input theGuid: The guid of the unit to search for.
-- returns: true|false
function gKH.Unit:checkForHostedUnitGuid(u,theType,theGuid)
  fn = "gKH.Unit:checkForHostedUnitGuid(): ";
  if (theType == nil) or type(theType) ~= "string" then theType = "Aircraft" end --default to Aircraft
  if (theGuid == nil) or type(theGuid) ~= "string" or string.len(theGuid) < 20 then print(fn .. "Missing guid. aborting."); return false; end
  if(u~=nil) then 
    local tmptable;
    if (u.embarkedUnits ~= nil) and theType == "Aircraft" then
      tmptable = u.embarkedUnits.Aircraft;
    elseif (u.embarkedUnits ~= nil) and theType == "Boats" then
      tmptable = u.embarkedUnits.Boats;
    else
      print(fn .. "Could not determine search type, try Aircraft or Boats. Aborting.")
      return false;
    end
    if (tmptable ~=nil ) and #tmptable > 0 then
      for k = 1, #tmptable do
        if(tmptable[k] == theGuid) then return true; end --found match
      end
    else
        return false; -- no units to check.
    end
    return false; -- checked but no matches.
  else
    print(fn .. "Error: Unit wrapper missing, returning false")
    return false;
  end
end

local u = SE_GetUnit({name='SingleUnitAirfield', guid='4FH7PU-0HM4G1515697E'})
print(gKH.Unit:checkForHostedUnitGuid(u,"Aircraft","4FH7PU-0HM4G1515A9H1")) -- returns true if that unit guid is hosted.

You may need to dummy up the gKH namespace (local or global your choice) before using either if you don't rename the functions, ie put the following at the top:
gKH = {}
gKH.Unit={}




< Message edited by KnightHawk75 -- 11/23/2020 11:38:21 PM >

(in reply to orca)
Post #: 17
Page:   [1]
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> Scan An Area for Units Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts


Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI

0.891