Matrix Games Forums

Forums  Register  Login  Photo Gallery  Member List  Search  Calendars  FAQ 

My Profile  Inbox  Address Book  My Subscription  My Forums  Log Out

Change Type of Aircraft

 
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 >> Change Type of Aircraft Page: [1]
Login
Message << Older Topic   Newer Topic >>
Change Type of Aircraft - 11/8/2020 1:53:49 PM   
morphin

 

Posts: 483
Joined: 4/26/2002
From: Switzerland
Status: online
Hi

I would like to replace all F-5A Fresco AC with Mig-21 Fishbed in a scenario (global replace)
->Generic: Replace unit of type XX with another of type YY

Has this already anybody done?

Thank's
Andy
Post #: 1
RE: Change Type of Aircraft - 11/8/2020 5:27:01 PM   
morphin

 

Posts: 483
Joined: 4/26/2002
From: Switzerland
Status: online
I see that there is a VP_ExportUnits (table,filename) function, but it seems that once exported there is no way to import it again?

(in reply to morphin)
Post #: 2
RE: Change Type of Aircraft - 11/8/2020 10:28:11 PM   
Rory Noonan

 

Posts: 2723
Joined: 12/18/2014
From: Brooklyn, NY
Status: offline
Hey morphin,

To do this I would get a list of units for the side:

local unitlist = VP_GetSide({side='playerside'}).units

Then cycle through and pick out the DBID I want to replace, and replace units with that dbid with brand new units with our replacement dbid:

dbidToReplace = 1234 -- change as appropriate
replacementDBID = 7890
local unitsToReplace = []
for k,v in ipairs(unitlist) do
local unit = ScenEdit_GetUnit({guid=v.guid})
if unit.dbid == dbidToReplace then
local newUnit = unit
newUnit.dbid == replacementDBID
ScenEdit_DeleteUnit({guid=v.guid})
ScenEdit_AddUnit(newUnit)
end
end


I'm not able to test this right now so treat the above as 'psuedo code'

_____________________________


(in reply to morphin)
Post #: 3
RE: Change Type of Aircraft - 11/9/2020 7:10:45 AM   
morphin

 

Posts: 483
Joined: 4/26/2002
From: Switzerland
Status: online
Hi
Thank you very much

I have testet in on a small Tutorial Scenario. It deletes the AC but does not create a new one
Error:

ERROR: [string "Console"]:12: Invalid arguments to method call

Here is the code:

local unitlist = VP_GetSide({side='Israel'}).units
dbidToReplace = 676 -- change as appropriate
replacementDBID = 2734
for k,v in ipairs(unitlist) do
local unit = ScenEdit_GetUnit({guid=v.guid})
if unit.dbid == dbidToReplace then
local newUnit = unit
newUnit.dbid = replacementDBID
ScenEdit_DeleteUnit({guid=v.guid})
ScenEdit_AddUnit(newUnit)
end
end

Thanks for any hints

Attachment (1)

(in reply to Rory Noonan)
Post #: 4
RE: Change Type of Aircraft - 11/9/2020 9:12:15 AM   
morphin

 

Posts: 483
Joined: 4/26/2002
From: Switzerland
Status: online
I have managed to get it work:
->It works only for AC
->it works only if AC is in a base
->It would be nice if loadout could be adjusted based on role ( I can get the role of the loadout of the old AC: print (lo.roles.role) but the new AC get the same loadoutid as the old
It would be nice if you could query the loadoutid based on the role the old AC had.....


local unitside = 'Israel'
local unitlist = VP_GetSide({side=unitside}).units
dbidToReplace = 4508 -- change as appropriate
replacementDBID = 2734
for k,v in ipairs(unitlist) do
local unit = ScenEdit_GetUnit({guid=v.guid})
if unit.dbid == dbidToReplace then
local newUnit = unit
local lo = ScenEdit_GetLoadout ({name=newUnit.name,guid=newUnit.guid})
ScenEdit_DeleteUnit({guid=v.guid})
ScenEdit_AddUnit({type=newUnit.type,unitname=newUnit.name,side=unitside,dbid=replacementDBID, base=newUnit.base.name, guid=newUnit.guid,loadoutid=lo.dbid})
end
end

(in reply to morphin)
Post #: 5
RE: Change Type of Aircraft - 11/9/2020 10:52:44 AM   
KnightHawk75

 

Posts: 786
Joined: 11/15/2018
Status: offline
The attached gKH_Side_ReplaceUnitsByDBID_v2.zip contains a more full featured script for this.
I extended what I already had for your use case, and hopefully what I added extends it use for a wider number of cases where people need to mass-replace, but not have to re-do as much after the mass-replace.

I tested it against some USA and PLA aircraft\ship\sub and radar facilities in your scene posted previously in other thread.



Here are the release notes for v2:
The two files Localized and Global are 99.999% the same the only difference is that for Localized_..
The gKH namespace table is defined as local, this is meant for lua console only use.
Such as during scene development process.

The global version once run once will stay in memory, so you don't need to run it again. 
It's meant more for use when wanting to use these function during the game play.

ReplaceUnitsByDBID() takes 6 parameters, first 5 are required.
 theside: the string side name to operate on, only validation is that it's a string and non-nil. special side "_ALL_" not implimented yet.
 thetype: the string unit type code 'Aircraft','Submarine','Ship','Facility' etc. Validation is only it non-nil and string.
 thedbid: the database id# for the unit being replaced.  must exist and be a number.
 theloadoutid:  the loadout dbid id to further restrict the replacement on, OR 0 to match all (ie only on unit dbid). 
 tParams: the table of parameters that you would normally feed to ScenEdit_addunit(), only thing required is {dbid=###,loadoutid=###}
 themode: optional;either nil\empty or one of "Normal" or "KeepGUIDS" Not fully tested yet, exists for future enhancement purposes.
 themode: default if left empty or is anything else is "Normal", replacement units get new guids, keepguids attempts to reuse old unit guids.
THINGS TO BE AWARE OF:

 -Replacement units will by default get new GUIDS, if you want to try reusing old guid, set themode to "KeepGUIDs", however this comes with risk.
  The risk is that the unit will be removed before an attempt to create the replacement, and if that fails for some reason the original is now gone.  

 -Replacement units that were members of groups will be put back into group the original was a members of, however leads may change.

 -Replacement units that were members of missions will be assigned to the original units mission. HOWEVER if they were flagged as escorts
  they will not get re-flagged as escorts as there is at present no way to detect this in LUA that I found.

 -Replacement units that were parked\docked inside bases will be generated and placed in the originally hosting parent. HOWEVER they will
  NOT be placed in the same parking spots. Those in th air or at sea,etc will be placed in the same location as the original. 

 -Replacement units will be assigned the course that the original unit had, if they had a course.

 -Replacement units in most cases MAY NOT RECEIVE WRA\DOCTRINE\EMCON settings that the original had. However they should get group\mission\etc
  inheritence, such that when you are done making changes, and you goto the various higher level settings screens and press REAPPLY to units
  affected it should take care of things. If you had non-iherited specific settings on the original you will have to manually adjust again.
 -Replacement units in most cases may not get the same speed\throttle setting that the original had, though some attempt has been made to 
  preserve altitude\depth, but wide testing has not been done (ie it generally works but current vs desired may be screwed if not on mission).

 -Replacing units such as a carrier or airbase\port dbid component - keep in mind any hosted aircraft\ships will be removed.
  While it's possible to modify the script to support auto re-generation of hosted units at present it does not support this.  
  For the above reason I DO NOT recommend using this on DBID's that are part of a port\air-base\carrier, but you can provides you don't mind
  losing the contents. 

 -Remember that once you run a change, if you want to reverse it, the reverse application may affect more units than original if the side 
  originally had some existing units of what you changed certain things too. ie if side had 5 su-27's and you swap 15 mig21 to su27's, then try
  swapping back all su-27's to mig-21's then 20 units will be affected on the swap back. Should be obvious but it's worth keeping in mind.

 -You must specificy both the dbid and loadoutid, if you feed it a loadoutid# that is 0, it will ignore loadoutid matching and just use dbid. 
   
EXAMPLE USAGE (happens to be base on Red Dragon Desecnds 2022 scene, but is illustrative of any)
If using in console place these after the end of all the function related code...ie bottom.

-- ** Aircraft swap examples 
-- swap j-8f 2473 with loadoutid 9626 to j-11b 4641 loadout: 25278 
--gKH.Side:ReplaceUnitsByDBID('PLA','Aircraft', 2473, 9626,{dbid=4641,loadoutid=25278});
-- swap j-11b 4641 with loadoutid 25278 to j-8 4273  loadout: 9626
--gKH.Side:ReplaceUnitsByDBID('PLA','Aircraft', 4641, 25275,{dbid=2473,loadoutid=9626});

-- swap ALL j-8f 2473 to j-11b 4641 loadout: 25278 
--gKH.Side:ReplaceUnitsByDBID('PLA','Aircraft', 2473, 0,{dbid=4641,loadoutid=25278});
-- swap ALL j-11b 4641 to j-8 4273  loadout: 9626
--gKH.Side:ReplaceUnitsByDBID('PLA','Aircraft', 4641, 0,{dbid=2473,loadoutid=9626});

-- ** Submarine swap examples (note the last param false here is not needed, just shows that sending invaild entry is same as it not being there)
-- swap S-368 296 to 093B Shang 665 0 
--gKH.Side:ReplaceUnitsByDBID('PLA','Submarine', 296, 0,{dbid=665,loadoutid=0}, false);
-- swap 093B Shang 665 to j-8 S-296  0
--gKH.Side:ReplaceUnitsByDBID('PLA','Submarine', 665, 0,{dbid=296,loadoutid=0}, false);

-- ** Ship swap examples 
-- swap Type 037-IG Houxin 349 to 056A Jiangdao  2703 0 
--gKH.Side:ReplaceUnitsByDBID('PLA','Ship', 349, 0, {dbid=2703,loadoutid=0}, false)
-- swap Type 056A Jiangdao 2703 to type 037-IG Houxi 349 0 
--gKH.Side:ReplaceUnitsByDBID('PLA','Ship', 2703, 0, {dbid=349,loadoutid=0}, false);

-- ** Facility swap examples 
-- swap jy-27 Wide mat 545 to BigBird D 2444 
--gKH.Side:ReplaceUnitsByDBID('PLA','Facility', 545, 0,{dbid=2444,loadoutid=0}, false);
-- swap BigBird D 2444 to jy-27 Wide mat 545 
--gKH.Side:ReplaceUnitsByDBID('PLA','Facility', 2444,0, {dbid=545,loadoutid=0}, false);






Attachment (1)

< Message edited by KnightHawk75 -- 11/9/2020 3:01:16 PM >

(in reply to morphin)
Post #: 6
RE: Change Type of Aircraft - 11/9/2020 11:01:34 AM   
morphin

 

Posts: 483
Joined: 4/26/2002
From: Switzerland
Status: online
Great! Thank you very very much

(in reply to KnightHawk75)
Post #: 7
RE: Change Type of Aircraft - 11/9/2020 11:11:13 AM   
KnightHawk75

 

Posts: 786
Joined: 11/15/2018
Status: offline

quote:

ORIGINAL: morphin

Great! Thank you very very much



You are welcome! It was interesting, and eventually I'll need this myself, so a 2:1 special in terms of extending what I already had. :)

Keep in mind feel free to modify\comment out some of the print statements, they're there mainly for debugging if something goes wrong. But they might be too verbose if doing several 100's of changes per call, normally I'd make them only print if a debug mode was enabled but figured for more public use better to log stuff than not for now.

PLEASE let me know if you run into errors or an odd result that isn't otherwise easy to see why\what went wrong.

(in reply to morphin)
Post #: 8
RE: Change Type of Aircraft - 11/9/2020 11:24:57 AM   
morphin

 

Posts: 483
Joined: 4/26/2002
From: Switzerland
Status: online
Short question:

There are for example 10 AC (same type) with different loadouts (AAW, SEAD for example). You can replace all 10 AC but can only give one new loadout to it?
Maybey you need to provide "old" loadoutid so you can replace only old AC with same/similar loadout?
Or did you resolve this in another way?
I'm just browsing through your code. This code is hugh and overhelming for me! So maybe the solution is already in your code but i miss it....

(in reply to KnightHawk75)
Post #: 9
RE: Change Type of Aircraft - 11/9/2020 11:45:45 AM   
morphin

 

Posts: 483
Joined: 4/26/2002
From: Switzerland
Status: online
It works! Thank again. That saves me a lot of time

(in reply to morphin)
Post #: 10
RE: Change Type of Aircraft - 11/9/2020 12:27:05 PM   
morphin

 

Posts: 483
Joined: 4/26/2002
From: Switzerland
Status: online
Hi
It seems to me that it would be helpful to add loadoutid as additional parameter for the old AC. Then you can filter only to replace the old AC with specific load instead of all AC...
What do you mean for this suggestion?

Here the code for getting the loadoutid:

local lo = ScenEdit_GetLoadout ()
lo.dbid

(in reply to morphin)
Post #: 11
RE: Change Type of Aircraft - 11/9/2020 12:31:12 PM   
morphin

 

Posts: 483
Joined: 4/26/2002
From: Switzerland
Status: online
Maybe there is an typo on "Submaine" instead of Submarine? (row 117)

if src.type == 'Aircraft' then
tdest.altitude = src.altitude;
elseif src.type == 'Submaine' then --we need to set depth
updTable.depth = src.altitude;
updTable.moveto=false;
elseif src.type == 'Ship' then
updTable.depth = 0;
updTable.altitude = 0;
end

(in reply to morphin)
Post #: 12
RE: Change Type of Aircraft - 11/9/2020 2:13:20 PM   
KnightHawk75

 

Posts: 786
Joined: 11/15/2018
Status: offline
Typo and loadout changes coming.

Edit: post 5 updated with version 2 in the description and the download.

Now takes 6 params, 5 required, including a loadoutid to match, if it's 0 then all will be matched as before.
Obviously this really is only helpful for aircraft, but there you go.
Also matching is now separated out to a separate function so one can more easily extend it if needed.



< Message edited by KnightHawk75 -- 11/9/2020 3:17:11 PM >

(in reply to morphin)
Post #: 13
RE: Change Type of Aircraft - 11/9/2020 7:09:39 PM   
morphin

 

Posts: 483
Joined: 4/26/2002
From: Switzerland
Status: online
Thank's

I have now used it quite a lot. Very useful.

Ship hosting Helo loose the Helo after replacing (you wrote this). I think it is not easy to resolve this
But now it is nearly perfect for me

Thank's for your great work

(in reply to KnightHawk75)
Post #: 14
RE: Change Type of Aircraft - 11/9/2020 7:41:16 PM   
KnightHawk75

 

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

ORIGINAL: morphin

Thank's

I have now used it quite a lot. Very useful.

Ship hosting Helo loose the Helo after replacing (you wrote this). I think it is not easy to resolve this
But now it is nearly perfect for me

Thank's for your great work


Yeah generically speaking that can be done (query the unit being changed, if it has hosted units, get all those guids, get all those units... build a table of unit data for those)...kill the old unit,create new unit, then recreate each of the items in the table and then insert them back into the new unit. But now you've recreated units the user may not have expected.

BUT the real challenge are the what if's. What if you changing cvn to ddg or ddg with helipad to another ddg that doesn't, or had 2 large hangers and new one only has 1. Wiping a single-unit-4x airbase for single-unit-2x and now all the aircraft don't fit, or changing large hangers to smaller one, etc. Another would be just changing a ship to a sub (which you can technically do atm, even though I don't suggest cause I didn't test it much), or sub to a ship with units that now don't have the right docking type\space. Anyway those are some issues that came to mind when I thought about adding support for it, but thought better to leave it out then have something that generates even more unexpected results. Only thing I am thinking of adding in the near term is probably in-area support so you could further restrict to with-in a polygon\series of rp's.


< Message edited by KnightHawk75 -- 11/9/2020 7:44:21 PM >

(in reply to morphin)
Post #: 15
Page:   [1]
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> Change Type of Aircraft 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.133