"Teaching" Chat GPT the CMO Lua Library

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
Kasey99
Posts: 10
Joined: Sat Aug 14, 2021 9:28 am

"Teaching" Chat GPT the CMO Lua Library

Post by Kasey99 »

I've been experimenting with ChatGPT (Free version) this morning by giving it code snippets, lists of functions, wrappers, tables, etc. to "Teach" it the CMO lingo. Using the Command Lua Docs, I essentially copied and pasted loads of information, section by section, into ChatGPT. I'm happy to say it finally produced a working script. Granted, I am at the very beginning stages of Lua and mainly rely on using existing code to build and edit scenarios by changing existing information within scripts. If someone with extensive experience were to work with ChatGPT by teaching the CMO language, I can't imagine what a cheat you could be.

"Write a script to obtain side units for IRAN and include the type, class, name, and dbid."
Resulting script:

Code: Select all

local side = "IRAN"
local base = {}
local s = VP_GetSide({ name = side })
for _, v in ipairs(s.units) do
    local unit = ScenEdit_GetUnit({ guid = v.guid })
    if unit.type ~= "Group" and not base[unit.dbid] then
        base[unit.dbid] = true
        print(string.format("{type= '%s', class= '%s', name= '%s', dbid= %d}", unit.type, unit.classname, unit.name, unit.dbid))
    end
end
First attempt in Lua Console:

Code: Select all

{type= 'Facility', class= 'A/C Helipad (1x Medium Helicopter)', name= 'A/C Helipad (1x Medium Helicopter)', dbid= 2120}
{type= 'Ship', class= '71 Alvand [Saam, Vosper Mk5]', name= '73 Sabalan [Saam, Vosper Mk5]', dbid= 4170}
{type= 'Facility', class= 'Structure (Pier [Very Large, 45.1-200m])', name= 'Structure (Pier [Very Large, 45.1-200m])', dbid= 1840}
{type= 'Facility', class= 'Runway (4000m)', name= 'Runway (4000m)', dbid= 757}
{type= 'Facility', class= 'Runway (3200m)', name= 'Runway (3200m)', dbid= 35}
{type= 'Facility', class= 'Runway (2600m)', name= 'Runway (2600m)', dbid= 55}
{type= 'Facility', class= 'Runway (5600m)', name= 'Runway (5600m)', dbid= 73}
{type= 'Facility', class= 'Building (Small)', name= 'Communication Hub', dbid= 113}
{type= 'Ship', class= '74 Sahand [Moudge Class]', name= '74 Sahand [Moudge Class]', dbid= 3754}
{type= 'Facility', class= 'Vehicle (Mobile Jammer [Advanced])', name= 'Vehicle (Mobile Jammer [Advanced])', dbid= 3595}
{type= 'Facility', class= 'SSM Bn (Ghader)', name= 'SSM Bn (Ghader)', dbid= 2212}
{type= 'Aircraft', class= 'KC-25A [Boeing 747-100, KC-747]', name= 'KC-25A Tanker [Boeing 747-100] ', dbid= 1660}
{type= 'Aircraft', class= 'F-4E Phantom II', name= 'F-4E Phantom II', dbid= 3896}
{type= 'Ship', class= 'P 224 Sina [La Combattante II Mod]', name= 'P 2xx Missile Gunboat [La Combattante II]', dbid= 2549}
{type= 'Ship', class= 'P 228 Gorz [La Combattante II]', name= 'P 2xx Missile Gunboat [La Combattante II]', dbid= 4136}
{type= 'Facility', class= 'SAM Plt (SA-15b Gauntlet [9K330 Tor-M1K])', name= 'SAM Plt (SA-15b Gauntlet [9K330 Tor-M1K])', dbid= 481}
{type= 'Facility', class= 'A/C Tarmac Space (4x Large Aircraft)', name= 'A/C Tarmac Space (4x Large Aircraft)', dbid= 186}
{type= 'Facility', class= 'A/C Hardened Aircraft Shelter (1x Large Aircraft)', name= 'A/C Hardened Aircraft Shelter (1x Large Aircraft)', dbid= 27}
{type= 'Facility', class= 'Ammo Shelter', name= 'Ammo Shelter', dbid= 1426}
{type= 'Facility', class= 'AvGas (400k Liter Underground Tank)', name= 'AvGas (400k Liter Underground Tank)', dbid= 943}
{type= 'Facility', class= 'A/C Hardened Aircraft Shelter (1x Medium Aircraft)', name= 'A/C Hardened Aircraft Shelter (1x Medium Aircraft)', dbid= 4}
{type= 'Facility', class= 'Diesel (75k Liter Tank)', name= 'Diesel (75k Liter Tank)', dbid= 8}
{type= 'Facility', class= 'Diesel (400k Liter Tank)', name= 'Diesel (400k Liter Tank)', dbid= 11}
{type= 'Facility', class= 'A/C Hangar (2x Very Large Aircraft)', name= 'A/C Hangar (2x Very Large Aircraft)', dbid= 92}
{type= 'Facility', class= 'Structure (Pier [Large, 25.1-45m])', name= 'Structure (Pier [Large, 25.1-45m])', dbid= 1841}
{type= 'Facility', class= 'Runway Access Point (Large Aircraft)', name= 'Runway Access Point (Large Aircraft)', dbid= 306}
{type= 'Facility', class= 'Building (Communication Hub)', name= 'Building (Communication Hub)', dbid= 615}
{type= 'Facility', class= 'Building (Control Tower)', name= 'Building (Control Tower)', dbid= 3}
{type= 'Facility', class= 'Vehicle (PCLS Receiver)', name= 'Vehicle (PCLS Receiver)', dbid= 3290}
{type= 'Facility', class= 'Structure (Radome)', name= 'Structure (Radome)', dbid= 3818}
{type= 'Facility', class= 'AAA Plt/3 (23mm ZU-23-8, Mesbah-1)', name= 'AAA Plt/3 (23mm ZU-23-8, Mesbah-1)', dbid= 3738}
{type= 'Facility', class= 'Radar (Spoon Rest D [P-18])', name= 'Radar (Spoon Rest D [P-18])', dbid= 1342}
{type= 'Ship', class= 'Saviz', name= 'Saviz', dbid= 3389}
{type= 'Facility', class= 'Runway-Grade Taxiway (4000m)', name= 'Runway-Grade Taxiway (4000m)', dbid= 1421}
{type= 'Facility', class= 'Runway Access Point (Very Large Aircraft)', name= 'Runway Access Point (Very Large Aircraft)', dbid= 353}
{type= 'Facility', class= 'A/C Tarmac Space (4x Very Large Aircraft)', name= 'A/C Tarmac Space (4x Very Large Aircraft)', dbid= 103}
{type= 'Facility', class= 'Ammo Bunker (Surface)', name= 'Ammo Bunker (Surface)', dbid= 322}
{type= 'Facility', class= 'A/C Weather Shelter (1x Large Aircraft)', name= 'A/C Weather Shelter (1x Large Aircraft)', dbid= 1384}
{type= 'Facility', class= 'A/C Tarmac Space (4x Medium Aircraft)', name= 'A/C Tarmac Space (4x Medium Aircraft)', dbid= 190}
{type= 'Facility', class= 'A/C Hangar (4x Large Aircraft)', name= 'A/C Hangar (4x Large Aircraft)', dbid= 9}
{type= 'Facility', class= 'A/C Helipad (1x Large Helicopter)', name= 'A/C Helipad (1x Large Helicopter)', dbid= 2119}
{type= 'Facility', class= 'AvGas (400k Liter Tank)', name= 'AvGas (400k Liter Tank)', dbid= 453}
{type= 'Facility', class= 'AvGas (3000k Liter Underground Tank)', name= 'AvGas (3000k Liter Underground Tank)', dbid= 1292}
{type= 'Aircraft', class= 'Boeing 707-3J9C Boom/Drogue Tanker', name= 'Boeing 707-3J9C Boom/Drogue Tanker', dbid= 2423}
{type= 'Aircraft', class= 'Mirage F.1EQ-6', name= 'Mirage F.1EQ-6', dbid= 1203}
{type= 'Facility', class= 'Single-Unit Airfield (1x 4000m+ Runway)', name= 'Zahedan Air Base', dbid= 1995}
{type= 'Aircraft', class= 'Falcon 20F', name= 'Falcom 20F', dbid= 6162}
{type= 'Aircraft', class= 'F-14A Tomcat [F-14AM]', name= 'F-14A Tomcat', dbid= 1312}
{type= 'Facility', class= 'SSM Sec (C-802)', name= 'SSM Sec (C-802)', dbid= 3011}
{type= 'Facility', class= 'Bunker (Comm Center)', name= 'Bunker (Comm Center)', dbid= 6}
{type= 'Facility', class= 'AAA Sec (35mm Twin Oerlikon x 2)', name= 'AAA Sec (35mm Twin Oerlikon x 2)', dbid= 912}
{type= 'Facility', class= 'SAM Bty (I-HAWK [Mersad Upgrade])', name= 'SAM Bty (I-HAWK [Mersad Upgrade])', dbid= 3812}
{type= 'Facility', class= 'A/C Hangar (2x Large Aircraft)', name= 'A/C Hangar (2x Large Aircraft)', dbid= 41}
{type= 'Facility', class= 'A/C Hangar (2x Small Aircraft)', name= 'A/C Hangar (2x Small Aircraft)', dbid= 86}
{type= 'Facility', class= 'AvGas (40k Liter Tank)', name= 'AvGas (40k Liter Tank)', dbid= 34}
{type= 'Facility', class= 'AvGas (150k Liter Tank)', name= 'AvGas (150k Liter Tank)', dbid= 84}
{type= 'Aircraft', class= 'Bell 214A/C Isfahan', name= 'KOKOMO #1', dbid= 6434}
{type= 'Facility', class= 'SAM Plt (SA-17 Grizzly [9K37M1-2 Buk-M1-2])', name= 'SAM Plt (SA-17 Grizzly [9K37M1-2 Buk-M1-2])', dbid= 192}
{type= 'Facility', class= 'Radar (Generic Surface Search Radar)', name= 'Radar (Generic Surface Search Radar)', dbid= 939}
{type= 'Aircraft', class= 'P-3F Orion', name= 'P-3F Orion', dbid= 1313}
{type= 'Aircraft', class= 'MiG-29 Fulcrum A', name= 'MiG-29 Fulcrum A', dbid= 6728}
{type= 'Aircraft', class= 'F-5F Tiger II', name= 'F-5F Tiger II', dbid= 3591}
{type= 'Facility', class= 'A/C Hardened Aircraft Shelter (4x Large Aircraft)', name= 'A/C Hardened Aircraft Shelter (4x Large Aircraft)', dbid= 52}
{type= 'Facility', class= 'AvGas Bunker (400k Liter Tank)', name= 'AvGas Bunker (400k Liter Tank)', dbid= 51}
{type= 'Facility', class= 'AvGas Bunker (150k Liter Tank)', name= 'AvGas Bunker (150k Liter Tank)', dbid= 36}
{type= 'Facility', class= 'A/C Tarmac Space (2x Large Aircraft)', name= 'A/C Tarmac Space (2x Large Aircraft)', dbid= 217}
{type= 'Facility', class= 'AvGas (1500k Liter Underground Tank)', name= 'AvGas (1500k Liter Underground Tank)', dbid= 1293}
{type= 'Facility', class= 'A/C Hangar (4x Medium Aircraft)', name= 'A/C Hangar (4x Medium Aircraft)', dbid= 227}
{type= 'Facility', class= 'A/C Hangar (2x Medium Aircraft)', name= 'A/C Hangar (2x Medium Aircraft)', dbid= 68}
{type= 'Facility', class= 'A/C Hangar (4x Small Aircraft)', name= 'A/C Hangar (4x Small Aircraft)', dbid= 26}
{type= 'Ship', class= '421 Bandar Abbas', name= '421 Bandar Abbas', dbid= 2453}
{type= 'Facility', class= 'Antenna Tower - NDB NavAid', name= 'Antenna Tower - NDB NavAid', dbid= 3305}
{type= 'Facility', class= 'Antenna Tower - CB Radio', name= 'Antenna Tower - CB Radio', dbid= 3301}
{type= 'Facility', class= 'Antenna Tower - AM Radio', name= 'Antenna Tower - AM Radio', dbid= 3297}
{type= 'Facility', class= 'Antenna Tower - AM Radio', name= 'Antenna Tower - AM Radio', dbid= 3295}
{type= 'Facility', class= 'Antenna Tower - VHF FM Analogue Broadcast', name= 'Antenna Tower - VHF FM Analogue Broadcast', dbid= 3313}
{type= 'Facility', class= 'Antenna Tower - VHF FM Digital Broadcast', name= 'Antenna Tower - VHF FM Digital Broadcast', dbid= 3316}
{type= 'Facility', class= 'Antenna Tower - Cellular 3G', name= 'Antenna Tower - Cellular 3G', dbid= 3318}
{type= 'Facility', class= 'Structure (Mast)', name= 'HF/DF', dbid= 168}
{type= 'Facility', class= 'SAM Bty (Sevom Khordad)', name= 'SAM Bty (Sevom Khordad)', dbid= 3323}
{type= 'Facility', class= 'Runway-Grade Taxiway (1400m)', name= 'Runway-Grade Taxiway (1400m)', dbid= 1427}
{type= 'Facility', class= 'Antenna Tower - VOR NavAid', name= 'Antenna Tower - VOR NavAid', dbid= 3306}
{type= 'Facility', class= 'AvGas (75k Liter Tank)', name= 'AvGas (75k Liter Tank)', dbid= 44}
{type= 'Facility', class= 'AvGas Tank Farm (10 x 40k Liter Tank)', name= 'AvGas Tank Farm (10 x 40k Liter Tank)', dbid= 1823}
{type= 'Facility', class= 'Radar (Sepehr OTH-B)', name= 'Radar (Sepehr OTH-B)', dbid= 2612}
{type= 'Facility', class= 'Radar (Box Spring [1L119 Nebo SVU])', name= 'Radar (Box Spring [1L119 Nebo SVU])', dbid= 1849}
{type= 'Facility', class= 'AvGas Bladder (120k Liter Tank)', name= 'AvGas Bladder (120k Liter Tank)', dbid= 1827}
{type= 'Facility', class= 'AAA Plt/3 (23mm ZU-23-2 x 2)', name= 'AAA Plt/3 (23mm ZU-23-2 x 2)', dbid= 911}
{type= 'Facility', class= 'Ammo Pad', name= 'Ammo Pad', dbid= 1496}
{type= 'Facility', class= 'SAM Sec (Herz-9 x 2)', name= 'SAM Sec (Herz-9 x 2)', dbid= 3752}
{type= 'Facility', class= 'Single-Unit Airfield (2x 3201-4000m Runways)', name= 'Kerman Air Base', dbid= 430}
{type= 'Submarine', class= '942 Ghadir [IS-120]', name= '9xx [Ghadir Class SSK]', dbid= 734}
{type= 'Facility', class= 'A/C Helipad (1x Very Large Helicopter)', name= 'A/C Helipad (1x Very Large Helicopter)', dbid= 2417}
{type= 'Facility', class= 'AvGas (200k Liter Underground Tank)', name= 'AvGas (200k Liter Underground Tank)', dbid= 1393}
{type= 'Facility', class= 'Ammo Bunker (Mountain)', name= 'Ammo Bunker (Mountain)', dbid= 1731}
{type= 'Facility', class= 'Ammo Bunker (Underground)', name= 'Ammo Bunker (Underground)', dbid= 325}
{type= 'Facility', class= 'SAM Grp (SA-5c Gammon [S-200M Vega M], 1x Bn)', name= 'SAM Grp (SA-5c Gammon [S-200M Vega M], 1x Bn)', dbid= 476}
{type= 'Facility', class= 'SAM Bty (I-HAWK [Mersad Upgrade])', name= 'SAM Bty (I-HAWK [Mersad Upgrade])', dbid= 3813}
{type= 'Facility', class= 'SAM Bn (SA-20b Gargoyle [S-300PMU-2 Favorit])', name= 'SAM Bn (SA-20b Gargoyle [S-300PMU-2 Favorit])', dbid= 3013}
{type= 'Facility', class= 'Building (Very Large Leadership Compound)', name= 'Bushehr Province Regional Command', dbid= 2291}
{type= 'Facility', class= 'Building (Large Government Building)', name= 'Bushehr NPP (Nuclear Power Plant) Research Center', dbid= 2289}
{type= 'Facility', class= 'SAM Sec (Misagh-3 MANPADS)', name= 'SAM Sec (Misagh-3 MANPADS)', dbid= 3751}
{type= 'Facility', class= 'Building (Tents)', name= 'Building (Tents)', dbid= 1749}
{type= 'Facility', class= 'Radar (Bashir)', name= 'Radar (Bashir)', dbid= 3325}
{type= 'Aircraft', class= 'Kaman-22 UCAV', name= 'Kaman-22 UCAV', dbid= 5190}
{type= 'Aircraft', class= 'Kavosh UAV [ScanEagle Copy]', name= 'Kavosh UAV [ScanEagle Copy]', dbid= 6419}
{type= 'Aircraft', class= 'Pilatus PC-6B Turbo Porter', name= 'Pilatus PC-6B Turbo Porter', dbid= 6168}
{type= 'Aircraft', class= 'IrAn-140MP [HMS-200]', name= 'IrAn-140MP', dbid= 6437}
{type= 'Facility', class= 'SAM Bty (AD-200)', name= 'SAM Bty (AD-200)', dbid= 3229}
{type= 'Aircraft', class= 'SH-3D Sea King [ASH-3D]', name= 'SH-3D Sea King', dbid= 3092}
{type= 'Facility', class= 'Vehicle (Mobile Jammer [Average])', name= 'Vehicle (Mobile Jammer [Average])', dbid= 764}
{type= 'Facility', class= 'Vehicle (ELINT [Average])', name= 'Vehicle (ELINT [Average])', dbid= 2553}
{type= 'Aircraft', class= 'Su-35S Flanker M', name= 'Su-35S Flanker M', dbid= 6614}
{type= 'Ship', class= 'Surveillance Buoy [LLTV]', name= 'Surveillance Buoy [LLTV]', dbid= 3107}
{type= 'Ship', class= 'P 221 Kaman [La Combattante II]', name= 'P 2xx Missile Gunboat [La Combattante II]', dbid= 4135}
{type= 'Aircraft', class= 'Su-33 Flanker D', name= 'Su-33 Flanker D', dbid= 57}
{type= 'Facility', class= 'Radar (Rezonans-NE OTH)', name= 'Radar (Rezonans-NE OTH)', dbid= 3321}
{type= 'Facility', class= 'SAM Bn (Talash-3)', name= 'SAM Bn (Talash-3)', dbid= 3782}
{type= 'Aircraft', class= 'Shahed-191 UCAV', name= 'Shahed-191 UCAV', dbid= 6385}
{type= 'Ship', class= 'Peykaap II [Bavar]', name= 'Peykaap II [Bavar] #3563', dbid= 4102}
{type= 'Ship', class= 'MIL 40 [FB 42' STAB]', name= 'MIL 40 [FB 42' STAB] #3566', dbid= 4112}
{type= 'Ship', class= 'MIL 55 [FB 55']', name= 'MIL 55 [FB 55'] #3572', dbid= 4115}
{type= 'Ship', class= 'P 224 Sina [La Combattante II Mod]', name= 'P 224 Sina [La Combattante II Mod] #3575', dbid= 2091}
{type= 'Facility', class= 'SAM Plt/2 (Rapier FSA Blindfire)', name= 'SAM Plt/2 (Rapier FSA Blindfire)', dbid= 1136}
Kasey99
Posts: 10
Joined: Sat Aug 14, 2021 9:28 am

Re: "Teaching" Chat GPT the CMO Lua Library

Post by Kasey99 »

Optimize the following script:

Code: Select all

scenarioScriptFilePath = '/BrassDrum2023/'

function RunScript(fileName)
	local scriptFilePath = scenarioScriptFilePath..fileName..'.lua'
	print ('Attempting to execute '..scriptFilePath)
	if ScenEdit_RunScript(scriptFilePath) then
		print ('Success!')
	end
end

function LuaReset()
	print ('Resetting...')
	ScenEdit_ClearKeyValue("")
	print ('KeyValues cleared.')
	RunScript('LuaInit')
end

function ConvertStringToBoolean(stringName)
	local stringName = string.upper(stringName)
	local result = false
	if stringName == 'TRUE' then
		result = true
	end
	return result
end

function IsBetaVersion(booleanValue)
	if booleanValue == true then
		ScenEdit_SetKeyValue('betaVersion','true')
		return true
	elseif booleanValue == false then
		ScenEdit_SetKeyValue('betaVersion','false')
		return false
	else
		local betaStatus = ScenEdit_GetKeyValue('betaVersion')
		local result = ConvertStringToBoolean(betaStatus)
		return result
	end
end

function Round(num, numDecimalPlaces)
  local mult = 10^(numDecimalPlaces or 0)
  return math.floor(num * mult + 0.5) / mult
end

function DebugModeIsOn(booleanValue)
	if booleanValue == true then
		ScenEdit_SetKeyValue('debugMode','true')
		return true
	elseif booleanValue == false then
		ScenEdit_SetKeyValue('debugMode','false')
		return false
	else
		local debugStatus = ScenEdit_GetKeyValue('debugMode')
		local result = ConvertStringToBoolean(debugStatus)
		return result
	end
end

function RandomPosition(latitudeMin,latitudeMax,longitudeMin,longitudeMax)
	local lat_var = math.random(1,(10^13)) --random number between 1 and 10^13
	local lon_var = math.random(1,(10^13)) --random number between 1 and 10^13
	local pos_lat = math.random(latitudeMin,latitudeMax) + (lat_var/(10^13)) --latitude; 
	local pos_lon = math.random(longitudeMin,longitudeMax) + (lon_var/(10^13)) --longitude; 
	return {latitude=pos_lat,longitude=pos_lon}
end

function CircularRandomPosition(x_latitude, x_longitude, max_radius)
	local randomisationCircle = World_GetCircleFromPoint({
		latitude=x_latitude,
		longitude=x_longitude,
		radius=math.random(0.1,max_radius),
		numpoints = 72})
	local randomisedPoint = randomisationCircle[math.random(1,#randomisationCircle)]
    return randomisedPoint
end

function ShuffleTable( t )
    local rand = math.random 
    assert( t, "ShuffleTable() expected a table, got nil" )
    local iterations = #t
    local j
    
    for i = iterations, 2, -1 do
        j = rand(i)
        t[i], t[j] = t[j], t[i]
    end
end

function DTG(TimeVar)
	if TimeVar == nil then
		TimeVar = ScenEdit_CurrentTime()
	end
	local msgtime = os.date("!%d%H%M" .. "Z" .. " " .. "%b %y", TimeVar)
	local msgtime = string.upper(msgtime)
	return msgtime
end

function DTGNonAligned(TimeVar)
    if TimeVar == nil then 
    TimeVar = ScenEdit_CurrentTime()
    end
    msgtime = os.date("!%H%M" .. "UTC" .. " " .. "%d %b %Y", TimeVar)
    msgtime = string.upper(msgtime)
    return msgtime
end

function NonAlignedSignal(recipient,sender,subject,classification,precedence,body)
    local msg_time = DTGNonAligned()
	local signal_string = string.upper(
		'<P>'.. precedence.. '\\'..'\\ <BR>' .. 
		'FROM: '..sender..' <BR>' .. 
		'TO: '..recipient..' <BR>' ..
		'SUBJ: '..subject..' <BR>' .. 
		msg_time .. ' <BR>' ..
		classification .. '</P>' ..
		'<P>'..body.. '</P>' ..
		'<P>'..'\\'..'\\'..precedence..'</P>'
	)
	return signal_string
end

function RegisterMessage(messageString)
	local counter = 0
	for int1 = 1, 10 do
		local storedMessage = ScenEdit_GetKeyValue('storedMessage_'..int1)
		if storedMessage ~= nil then
			counter = counter + 1
		end
	end
	
	if counter == 10 then
		for int2 = 1, 10 do
			local shiftedMessage = ScenEdit_GetKeyValue('storedMessage_'..int2)
			local shiftedMessageSlot = int2 - 1
			ScenEdit_SetKeyValue('storedMessage_'..shiftedMessageSlot,shiftedMessage)
		end
		messageNumber = 10
	elseif counter <= 9 then
		messageNumber = counter + 1
	elseif counter == 0 then
		messageNumber = 1
	end
	ScenEdit_SetKeyValue('storedMessage_'..messageNumber,messageString)	
end

function ReplayMessages()
	for i = 10,1,-1 do
		local message = ScenEdit_GetKeyValue('storedMessage_'..i)
		if message ~= nil and message ~= '' then
			ScenEdit_SpecialMessage('playerside',message)
		end
	end
end

function CleanUpIdleCivilianShips()
	local unitList = VP_GetSide({side='Merchant'}).units
	local numberOfCivilianUnits = #unitList
	local numberOfUnitsDeleted = 0
	for k,v in ipairs (unitList) do
		local unit = ScenEdit_GetUnit({guid=v.guid})
		if (unit.course[1] == nil or unit.speed == 0) and 
			unit.type == 'Ship' then
			ScenEdit_DeleteUnit({guid=v.guid})
			numberOfUnitsDeleted = numberOfUnitsDeleted + 1
		end
	end

	if DebugModeIsOn() and numberOfUnitsDeleted > 0 then
		ScenEdit_SpecialMessage('playerside','Civ_Cleanup fired. <BR>Initial civilian Units: '..numberOfCivilianUnits..' <BR>Civilian units cleaned up: '..numberOfUnitsDeleted)
	end
end

function OverWater (latitude, longitude)
	local pointElevation = World_GetElevation({
		latitude = latitude,
		longitude = longitude})
	if pointElevation < 0 then
		return true
	else
		return false
	end
end

function CreateGUIDKeyName(guid)
	return 'x_'..guid
end

function RetrieveGUIDFromKey(key)
	local stringLength = string.len(key)
	local result = string.sub(key,3,stringLength)
	return result
end

function GenerateRadioMessageBody(theMessage,callsign)
	local result
	if callsign == nil then callsign = 'unk stn' end
    result = '<P>'..string.upper(callsign)..'<BR> <I>"'..theMessage..'"</I></P> '
	return result
end

function RadioMessage(band,frequency,theMessage,location)
	assert(theMessage,'RadioMessage(): No message passed!')
	local DTG = DTG()
	local theMessage = "<P>"..DTG..' <BR>'..band..' <BR>'..frequency..' </P>'..theMessage
	if location ~= nil then
		ScenEdit_SpecialMessage('playerside',theMessage,{latitude=location.latitude,longitude=location.longitude})
	else
		ScenEdit_SpecialMessage('playerside',theMessage)
	end
	RegisterMessage(theMessage)
end

function ConvertDecimalPositionToDegrees(latitude,longitude)
	local latitidePrefix, longitudePrefix
	if latitude > 0 then
		latitidePrefix = "N"
	else
		latitidePrefix = "S"
		latitude = latitude*-1
	end

	local latitudeDegrees = math.floor(latitude)
	local latitudeMinutes = math.floor((latitude - latitudeDegrees)*60)
	local latitudeSeconds = (((latitude-latitudeDegrees)*60) - latitudeMinutes)*60
	latitudeSeconds = Round(latitudeSeconds, 0)

	if longitude > 0 then
		longitudePrefix = "E"
	else
		longitudePrefix = "W"
		longitude = longitude*-1
	end

	local longitudeDegrees = math.floor(longitude)
	local longitudeMinutes = math.floor((longitude - longitudeDegrees)*60)
	local longitudeSeconds = (((longitude-longitudeDegrees)*60) - longitudeMinutes)*60
	longitudeSeconds = Round(longitudeSeconds, 0)

	local result = (latitidePrefix..latitudeDegrees .. "°" .. latitudeMinutes .. "'" .. latitudeSeconds .. '", '..
		longitudePrefix..longitudeDegrees .. "°" .. longitudeMinutes .. "'" .. longitudeSeconds .. '"')
	return result
end

function RandomLetter()
	local alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}
	return string.upper(alphabet[math.random(1,#alphabet)])
end
Certainly, here's the optimized version of the script:

Code: Select all

local scenarioScriptFilePath = '/BrassDrum2023/'

local function RunScript(fileName)
    local scriptFilePath = scenarioScriptFilePath .. fileName .. '.lua'
    print('Attempting to execute ' .. scriptFilePath)
    if ScenEdit_RunScript(scriptFilePath) then
        print('Success!')
    end
end

local function LuaReset()
    print('Resetting...')
    ScenEdit_ClearKeyValue("")
    print('KeyValues cleared.')
    RunScript('LuaInit')
end

local function ConvertStringToBoolean(stringName)
    return string.upper(stringName) == 'TRUE'
end

local function IsBetaVersion(booleanValue)
    if booleanValue == true then
        ScenEdit_SetKeyValue('betaVersion', 'true')
        return true
    elseif booleanValue == false then
        ScenEdit_SetKeyValue('betaVersion', 'false')
        return false
    else
        local betaStatus = ScenEdit_GetKeyValue('betaVersion')
        return ConvertStringToBoolean(betaStatus)
    end
end

local function Round(num, numDecimalPlaces)
    local mult = 10 ^ (numDecimalPlaces or 0)
    return math.floor(num * mult + 0.5) / mult
end

local function DebugModeIsOn(booleanValue)
    if booleanValue == true then
        ScenEdit_SetKeyValue('debugMode', 'true')
        return true
    elseif booleanValue == false then
        ScenEdit_SetKeyValue('debugMode', 'false')
        return false
    else
        local debugStatus = ScenEdit_GetKeyValue('debugMode')
        return ConvertStringToBoolean(debugStatus)
    end
end

local function RandomPosition(latitudeMin, latitudeMax, longitudeMin, longitudeMax)
    local lat_var = math.random(1, 10 ^ 13)
    local lon_var = math.random(1, 10 ^ 13)
    local pos_lat = math.random(latitudeMin, latitudeMax) + (lat_var / 10 ^ 13)
    local pos_lon = math.random(longitudeMin, longitudeMax) + (lon_var / 10 ^ 13)
    return { latitude = pos_lat, longitude = pos_lon }
end

local function CircularRandomPosition(x_latitude, x_longitude, max_radius)
    local randomisationCircle = World_GetCircleFromPoint({
        latitude = x_latitude,
        longitude = x_longitude,
        radius = math.random(0.1, max_radius),
        numpoints = 72
    })
    local randomisedPoint = randomisationCircle[math.random(1, #randomisationCircle)]
    return randomisedPoint
end

local function ShuffleTable(t)
    local rand = math.random
    assert(t, "ShuffleTable() expected a table, got nil")
    local iterations = #t

    for i = iterations, 2, -1 do
        local j = rand(i)
        t[i], t[j] = t[j], t[i]
    end
end

local function DTG(TimeVar)
    TimeVar = TimeVar or ScenEdit_CurrentTime()
    local msgtime = os.date("!%d%H%M" .. "Z" .. " " .. "%b %y", TimeVar)
    return string.upper(msgtime)
end

local function DTGNonAligned(TimeVar)
    TimeVar = TimeVar or ScenEdit_CurrentTime()
    local msgtime = os.date("!%H%M" .. "UTC" .. " " .. "%d %b %Y", TimeVar)
    return string.upper(msgtime)
end

local function NonAlignedSignal(recipient, sender, subject, classification, precedence, body)
    local msg_time = DTGNonAligned()
    local signal_string = string.upper(
        '<P>' .. precedence .. '\\\\ <BR>' ..
        'FROM: ' .. sender .. ' <BR>' ..
        'TO: ' .. recipient .. ' <BR>' ..
        'SUBJ: ' .. subject .. ' <BR>' ..
        msg_time .. ' <BR>' ..
        classification .. '</P>' ..
        '<P>' .. body .. '</P>' ..
        '<P>' .. '\\\\' .. '\\\\' .. precedence .. '</P>'
    )
    return signal_string
end

local function RegisterMessage(messageString)
    local counter = 0
    for int1 = 1, 10 do
        local storedMessage = ScenEdit_GetKeyValue('storedMessage_' .. int1)
        if storedMessage ~= nil then
            counter = counter + 1
        end
    end

    if counter == 10 then
        for int2 = 1, 10 do
            local shiftedMessage = ScenEdit_GetKeyValue('storedMessage_' .. int2)
            local shiftedMessageSlot = int2 - 1
            ScenEdit_SetKeyValue('storedMessage_' .. shiftedMessageSlot, shiftedMessage)
        end
        messageNumber = 10
    elseif counter <= 9 then
        messageNumber = counter + 1
    elseif counter == 0 then
        messageNumber = 1
    end
    ScenEdit_SetKeyValue('storedMessage_' .. messageNumber, messageString)
end

local function ReplayMessages()
    for i = 10, 1, -1 do
        local message = ScenEdit_GetKeyValue('storedMessage_' .. i)
        if message ~= nil and message ~= '' then
            ScenEdit_SpecialMessage('playerside', message)
        end
    end
end

local function CleanUpIdleCivilianShips()
    local unitList = VP_GetSide({ side = 'Merchant' }).units
    local numberOfCivilianUnits = #unitList
    local numberOfUnitsDeleted = 0
    for k, v in ipairs(unitList) do
        local unit = ScenEdit_GetUnit({ guid = v.guid })
        if (not unit.course[1] or unit.speed == 0) and unit.type == 'Ship' then
            ScenEdit_DeleteUnit({ guid = v.guid })
            numberOfUnitsDeleted = numberOfUnitsDeleted + 1
        end
    end

    if DebugModeIsOn() and numberOfUnitsDeleted > 0 then
        ScenEdit_SpecialMessage('playerside', 'Civ_Cleanup fired. <BR>Initial civilian Units: ' .. numberOfCivilianUnits .. ' <BR>Civilian units cleaned up: ' .. numberOfUnitsDeleted)
    end
end

local function OverWater(latitude, longitude)
    local pointElevation = World_GetElevation({
        latitude = latitude,
        longitude = longitude
    })
    return pointElevation < 0
end

local function CreateGUIDKeyName(guid)
    return 'x_' .. guid
end

local function RetrieveGUIDFromKey(key)
    local stringLength = string.len(key)
    return string.sub(key, 3, stringLength)
end

local function GenerateRadioMessageBody(theMessage, callsign)
    callsign = callsign or 'unk stn'
    return '<P>' .. string.upper(callsign) .. '<BR> <I>"' .. theMessage .. '"</I></P> '
end

local function RadioMessage(band, frequency, theMessage, location)
    assert(theMessage, 'RadioMessage(): No message passed!')
    local DTG = DTG()
    local theMessage = "<P>" .. DTG .. ' <BR>' .. band .. ' <BR>' .. frequency .. ' </P>' .. theMessage
    if location ~= nil then
        ScenEdit_SpecialMessage('playerside', theMessage, { latitude = location.latitude, longitude = location.longitude })
    else
        ScenEdit_SpecialMessage('playerside', theMessage)
    end
    RegisterMessage(theMessage)
end

local function ConvertDecimalPositionToDegrees(latitude, longitude)
    local latitidePrefix = latitude > 0 and "N" or "S"
    latitude = math.abs(latitude)

    local latitudeDegrees = math.floor(latitude)
    local latitudeMinutes = math.floor((latitude - latitudeDegrees) * 60)
    local latitudeSeconds = Round(((latitude - latitudeDegrees) * 60 - latitudeMinutes) * 60, 0)

    local longitudePrefix = longitude > 0 and "E" or "W"
    longitude = math.abs(longitude)

    local longitudeDegrees = math.floor(longitude)
    local longitudeMinutes = math.floor((longitude - longitudeDegrees) * 60)
    local longitudeSeconds = Round(((longitude - longitudeDegrees) * 60 - longitudeMinutes) * 60, 0)

    return latitidePrefix .. latitudeDegrees .. "°" .. latitudeMinutes .. "'" .. latitudeSeconds .. '", ' ..
        longitudePrefix .. longitudeDegrees .. "°" .. longitudeMinutes .. "'" .. longitudeSeconds .. '"'
end

local function RandomLetter()
    local alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }
    return string.upper(alphabet[math.random(1, #alphabet)])
end
ChatGPT says
In this optimized version, I've done the following:
  • Declared scenarioScriptFilePath as local.
    Made all functions local.
    Simplified the ConvertStringToBoolean and DebugModeIsOn functions using a direct comparison.
    Simplified the IsBetaVersion function by directly returning the boolean value.
    Simplified the RandomPosition function by avoiding redundant calculations.
    Simplified the OverWater function using a single return statement.
    Simplified the GenerateRadioMessageBody function by eliminating the unnecessary if condition.
    Simplified the Round function by using the math.floor function.
    Made other minor code adjustments for readability and maintainability.
Parel803
Posts: 894
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

Re: "Teaching" Chat GPT the CMO Lua Library

Post by Parel803 »

Good morning,
nice, thx for sharing it.
regards GJ
Karejar
Posts: 19
Joined: Mon Aug 19, 2019 4:45 pm

Re: "Teaching" Chat GPT the CMO Lua Library

Post by Karejar »

Great!!!
Norm49
Posts: 93
Joined: Fri Jun 05, 2015 3:59 pm

Re: "Teaching" Chat GPT the CMO Lua Library

Post by Norm49 »

Do you have link or script you can show us that you show to chat GPT to have this result. I'm also trying to scrip with chat GPT help. Work like a charm in ARMA but in CMO we both have a to learn.
Post Reply

Return to “Lua Legion”