Automating Task Groups Sailing and Transiting

All discussions & material related to Command's Lua interface

Moderators: michaelm75au, angster, RoryAndersonCDT, MOD_Command

Post Reply
Bashkire
Posts: 78
Joined: Mon Feb 23, 2015 6:28 pm

Automating Task Groups Sailing and Transiting

Post by Bashkire »

Good evening, all,

I'm trying to put together a little script repository for myself of functions to use, and I want to build a script that will cause a task group to sail as a group, proceed along a series of waypoints at a set speed, wait at some waypoints and then recommence their transit at a certain time, RAS along the way and finally rebase themselves when they get to the other end.

Ideally, I'd like to get the script to place waypoints on created reference points that I place on the map so that it would be easier to define a PIM in the future.

Ideally, I'd like someone to coach me through this rather than just outright writing the code for me. This is supposed to be an exercise in learning a bit of LUA, but I'm not exactly a code guru.

Thanks in advance!

- Bashkire

EDIT:
221429ZMAR2023 - TitaniumTrout has suggested that I attempt to walk before running - which he probably has a point with - and to break up my long script into separate segments for ease of testing. To that end, I'm posting my segments below and removing the long version above.

221940ZMAR2023 - Updated scripts with working versions.

272214ZMAR2023 - Updated re-basing script with working version.

212305ZMAY2023 - Updated scripts with the PIM broken into 2 legs for ease of conducting Suez Transit.

Code: Select all

			-------------------------------------
			-----       Define GUIDs        -----
			-------------------------------------
			-----          Working          -----

			----- Ships

unit_albion = ScenEdit_GetUnit({side = "Royal Navy", name = "[L 14] HMS Albion [Albion Class]"})
albion = unit_albion.guid
-----print (albion)

unit_daring = ScenEdit_GetUnit({side = "Royal Navy", name = "[D 32] HMS Daring [Type 45]"})
daring = unit_daring.guid
-----print (daring)

unit_lancaster = ScenEdit_GetUnit({side = "Royal Navy", name = "[F 229] HMS Lancaster [Type 23]"})
lancaster = unit_lancaster.guid
-----print (lancaster)

unit_westminster = ScenEdit_GetUnit({side = "Royal Navy", name = "[F 237] HMS Westminster [Type 23]"})
westminster = unit_westminster.guid
-----print (westminster)

unit_tidespring = ScenEdit_GetUnit({side = "Royal Navy", name = "[A 136] RFA Tidespring [Tide Class]"})
tidespring = unit_tidespring.guid
-----print (tidespring)

			----- Aircraft

unit_chinook_1 = ScenEdit_GetUnit ({side = "Royal Navy", name = "Workhorse 1"})
chinook_1 = unit_chinook_1.guid
-----print (chinook_1)

unit_chinook_2 = ScenEdit_GetUnit ({side = "Royal Navy", name = "Workhorse 2"})
chinook_2 = unit_chinook_2.guid
-----print (chinook_2)

			----- Bases

base_souda = ScenEdit_GetUnit({side = "Royal Navy", name = "NSA Souda Bay"})
souda = base_souda.guid
-----print (souda)

base_sgiPort = ScenEdit_GetUnit ({side = "St. George's Island", name = "St. George's Island Commercial Port and Naval Base"})
sgiPort = base_sgiPort.guid
-----print (sgiPort)

base_sgiAirport = ScenEdit_GetUnit ({side = "St. George's Island", name = "St. George's Island International Airport and Air Base"})
sgiAirport = base_sgiAirport.guid
-----print (sgiAirport)

Code: Select all

			-------------------------------------
			-----      Units Set Sail       -----
			-------------------------------------
			-----          Working          -----

unit_albion:Launch(true)
unit_daring:Launch(true)
unit_lancaster:Launch(true)
unit_westminster:Launch(true)
unit_tidespring:Launch(true)

Code: Select all

			-------------------------------------
			-----        Create Group       -----
			-------------------------------------
			-----          Working          -----

unit_albion.group = "TG 214"
unit_daring.group = "TG 214"
unit_lancaster.group = "TG 214"
unit_westminster.group = "TG 214"
unit_tidespring.group = "TG 214"

TG214 = ScenEdit_GetUnit({guid=unit_albion.group.guid})

TG214.group.lead = unit_albion.guid

Code: Select all

			-------------------------------------
			-----       Set Formation       -----
			-------------------------------------
			-----          Working          -----

TG214Heading = unit_albion.heading
-----print("TG214 Heading " .. TG214Heading)

station2 = TG214Heading-60
	if station2 <0
		then
			station2 = station2+360
		end
	if station2 >360
		then
			station2 = station2-360
		end
-----print("Station 2: " .. station2)

station3 = TG214Heading+60
	if station3 <0
		then
			station3 = station3+360
		end
	if station3 >360
		then
			station3 = station3-360
		end
-----print("Station 3: " .. station3)

station4 = TG214Heading-180
	if station4 <0
		then
			station4 = station4+360
		end
	if station4 >360
		then
			station4 = station4-360
		end
-----print("Station 4: " .. station4)

unit_daring.formation = {type = "Relative", sprint = false, bearing = TG214Heading, distance = 5}
unit_lancaster.formation = {type = "Relative", sprint = false, bearing = station2, distance = 4}
unit_westminster.formation = {type = "Relative", sprint = false, bearing = station3, distance = 4}
unit_tidespring.formation = {type = "Relative", sprint = false, bearing = station4, distance = 0.5}

Code: Select all

			-------------------------------------
			-----     Create PIM - Leg 1    -----
			-------------------------------------
			-----          Working          -----

TG214Course1 = {}

table.insert (TG214Course1, {latitude = 35.4937989469592, longitude = 24.2427923255052, description = "AA", desiredSpeed=18.0})	----- 162000ZSEP2022

table.insert (TG214Course1, {latitude = 35.5336551146554, longitude = 24.9697100962326, description = "BB"})						----- 162158ZSEP2022

table.insert (TG214Course1, {latitude = 35.4362140625729, longitude = 26.3732438434947, description = "CC"})						----- 170148ZSEP2022

table.insert (TG214Course1, {latitude = 31.6629092209283, longitude = 32.4239367498279, description = "DD"})						----- 172248ZSEP2022
																																----- Task group at anchor
																																----- Starts moving to EE at 180214ZSEP2022
 
TG214.course = TG214Course1

Code: Select all

			-------------------------------------
			-----     Create PIM - Leg 2    -----
			-------------------------------------
			-----          Working          -----

TG214Course2 = {}

table.insert (TG214Course2, {latitude = 31.2843387727463, longitude = 32.3530901412483, description = "EE", desiredSpeed=18.0})						----- 180330ZSEP 2022
																																----- Start of Suez transit

table.insert (TG214Course2, {latitude = 29.923971369994, longitude = 32.5500272692784, description = "FF"})						----- 181420ZSEP2022
																																----- End of Suez transit

table.insert (TG214Course2, {latitude = 29.8164816330461, longitude = 32.5470903159812, description = "GG"})						----- 181456ZSEP2022

table.insert (TG214Course2, {latitude = 29.414482754459, longitude = 32.6377674345322, description = "HH"})						----- 181618ZSEP2022

table.insert (TG214Course2, {latitude = 28.5384244631035, longitude = 33.1155268896664, description = "II"})						----- 181933ZSEP2022

table.insert (TG214Course2, {latitude = 27.7407702092935, longitude = 33.8221197239452, description = "JJ"})						----- 182255ZSEP2022

table.insert (TG214Course2, {latitude = 27.2738446731173, longitude = 34.5181003169432, description = "KK"})						----- 190130ZSEP2022

table.insert (TG214Course2, {latitude = 13.7608347724828, longitude = 42.4114180210723, description = "LL"})						----- 210452ZSEP2022

table.insert (TG214Course2, {latitude = 12.1498531203117, longitude = 43.8040899421419, description = "MM"})						----- 211154ZSEP2022

table.insert (TG214Course2, {latitude = 12.0803923307178, longitude = 44.8166120863782, description = "NN"})						----- 211512ZSEP2022

table.insert (TG214Course2, {latitude = 12.9104302903057, longitude = 50.2211191228355, description = "OO"})						----- 220902ZSEP2022

table.insert (TG214Course2, {latitude = 12.6543053701253, longitude = 53.1276826153152, description = "PP"})						----- 221831ZSEP2022
 
TG214.course = TG214Course2

Code: Select all

			-------------------------------------
			-----        Conduct RAS        -----
			-------------------------------------
			-----        Not Working        -----
			----- Ships do not fully refuel when multiple arrive at tanker at the same time.
			----- Possible workaround: define times to RAS in order to stagger unit arrivals?

ScenEdit_RefuelUnit ({side = "Royal Navy", guid = albion, tanker = tidespring})

ScenEdit_RefuelUnit ({side = "Royal Navy", guid = daring, tanker = tidespring})

ScenEdit_RefuelUnit ({side = "Royal Navy", guid = lancaster, tanker = tidespring})

ScenEdit_RefuelUnit ({side = "Royal Navy", guid = westminster, tanker = tidespring})

Code: Select all

			-------------------------------------
			-----     Assign New Bases      -----
			-------------------------------------
			-----          Working          -----

ScenEdit_SetUnit({side="Royal Navy", unitname=albion, base=sgiPort})

ScenEdit_SetUnit({side="Royal Navy", unitname=daring, base=sgiPort})

ScenEdit_SetUnit({side="Royal Navy", unitname=lancaster, base=sgiPort})

ScenEdit_SetUnit({side="Royal Navy", unitname=westminster, base=sgiPort})

ScenEdit_SetUnit({side="Royal Navy", unitname=tidespring, base=sgiPort})

ScenEdit_SetUnit({side="Royal Navy", unitname=chinook_1, base=sgiAirport})

ScenEdit_SetUnit({side="Royal Navy", unitname=chinook_2, base=sgiAirport})
Last edited by Bashkire on Sun May 21, 2023 10:06 pm, edited 8 times in total.
Bashkire
Posts: 78
Joined: Mon Feb 23, 2015 6:28 pm

Re: Automating Task Groups Sailing and Transiting

Post by Bashkire »

Duplicate post.

Please delete.
Parel803
Posts: 932
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

Re: Automating Task Groups Sailing and Transiting

Post by Parel803 »

Thanks you for sharing.
See if I can learn from it (if it is not to complex)
regards GJ
Bashkire
Posts: 78
Joined: Mon Feb 23, 2015 6:28 pm

Re: Automating Task Groups Sailing and Transiting

Post by Bashkire »

I'm very much a beginner as well. There (shouldn't) be anything too complex in these. I'm trying to create a "fill in the blanks" set of templates.
Bashkire
Posts: 78
Joined: Mon Feb 23, 2015 6:28 pm

Re: Automating Task Groups Sailing and Transiting

Post by Bashkire »

Ok, next problem:

The rebasing part of the script isn't firing correctly. It would be stellar if someone could do a test themselves and check the line, because I'm having a weird error pop up.

Attached is a save with one ship and two ports.
Paste the below code into the LUA console and you'll see the error "SelectedHostNameOrID has not been defined!" which is rather odd seeing as I'm using "base" rather than "host".

Code: Select all

unit_ship1 = ScenEdit_GetUnit({side = "Side A", name = "Ship 1"})
guid_ship1 = unit_ship1.guid

base_portB = ScenEdit_GetUnit({side = "Side A", name = "Port B"})
guid_portB = base_portB.guid

rebase_Ship1 = ScenEdit_HostUnitToParent({HostedUnitNameOrID = ship1, SelectedBaseNameOrID = portB})
Cheers all.
Rebase Test.zip
(7.11 KiB) Downloaded 12 times
User avatar
michaelm75au
Posts: 12457
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

Re: Automating Task Groups Sailing and Transiting

Post by michaelm75au »

Don't you mean
rebase_Ship1 = local a = ScenEdit_HostUnitToParent( {HostedUnitNameOrID=guid_ship1, SelectedHostNameOrID = guid_portB } )
Michael
User avatar
michaelm75au
Posts: 12457
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

Re: Automating Task Groups Sailing and Transiting

Post by michaelm75au »

This IS confusing.
I think that the first 2 parameters are as is, but that third one 'base' relates to assigning the unit to a specific location in the base (ie a specific pier in a multi-pier base).

I need to refresh my memory about this and probably clarify the doc and maybe better name the parameters.
Michael
Bashkire
Posts: 78
Joined: Mon Feb 23, 2015 6:28 pm

Re: Automating Task Groups Sailing and Transiting

Post by Bashkire »

Thank you for taking a look at this for me. I'll make a post in the Tech Support forum to make a formal log of the issue for the record.

EDIT: Blu3s on Discord has mentioned that I'm probably using the wrong function. ScenEdit_SetUnit allows me to use a parameter "base = "baseName"" to do exactly what I'm trying to do.
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

Re: Automating Task Groups Sailing and Transiting

Post by KnightHawk75 »

HostUnitToParent({blabla=unitguid,blabla=hostguid}) and SetUnit({base ='blabla'}) , are functionally equivalent or at least had been pre-tiny. Though I always found HostUnitToParent more straightforward in code post unit creation (less confusion with assigning a home base but actually putting the unit at said parent base).

-2cents

Code: Select all

rebase_Ship1 = ScenEdit_HostUnitToParent({HostedUnitNameOrID = unit_ship1.guid, SelectedHostNameOrID = base_portB.guid})
Would be working code, at present you're feeding the objects themselves which isn't correct, and you have the second parameter name wrong. Mike highlighted both.
Bashkire
Posts: 78
Joined: Mon Feb 23, 2015 6:28 pm

Re: Automating Task Groups Sailing and Transiting

Post by Bashkire »

Ok, new question for everyone:

How would I amend the above waypoint scripts to stop the group lead slowing down to allow the task group to reform. Currently, I have timings set up assuming the entire TG travels at 18 knots (reduced through the Suez), but the entire TG is way behind schedule because they reduce down to 8 knots whenever they reach a waypoint.

I can't find anything in the LUA docs, I'm afraid.

It's possible to do through the F3 menu, of course, but this is me testing out my LUA chops.
Post Reply

Return to “Lua Legion”