Matrix Games Forums

Forums  Register  Login  Photo Gallery  Member List  Search  Calendars  FAQ 

My Profile  Inbox  Address Book  My Subscription  My Forums  Log Out

Sharing my LUA Code to 'Simulate' Carrier Landings

 
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 >> Sharing my LUA Code to 'Simulate' Carrier Landings Page: [1]
Login
Message << Older Topic   Newer Topic >>
Sharing my LUA Code to 'Simulate' Carrier Landings - 7/19/2020 4:06:20 PM   
rbsj

 

Posts: 14
Joined: 12/4/2018
From: Portugal
Status: offline
POST EDITED: 22/07/2020 by me. ADD In ATTACHED SCENARIO WITH THE CODE.

Please note that with this code you only have to run the InitLUACode one time at the start of the scenario. And that for each carrier you only have to add one different trigger. The LUA Action is the same for each Event.


ORIGINAL POST:

Dear Community and Dear Developers,

Let me thank to all of you for this amazing community, also let me thank to the developers team who are constantly doing an amazing job. I'm not an English Native Speaker.

I'm more of a lurker here.

This is the first time I'm sharing some of the stuff that I do for my own.

---CONTEXT---

One of the interesting things when I started playing CMANO that I realized was that Aircraft Carriers (CV) need to manage well Aircraft Times Queues for Landing.

I remember in one game after the publishing of CMANO of having launched a huge number of aircrafts from a CV in a mission to surprisingly see a great number of them lost due to lacking fuel in the queu times. Since then I'm careful when preparing Carrier operations.

Having in mind how difficult is to land in a CV - being this awareness due to the Documentaries on Carriers - I was a little dissatisfied with how easy was to recover the aircraft, even during night and with bad weather.

Because of that I create this small script in LUA to help with this issue and I'm glad with it.

---THE FUNCTIONS---

What this does is to check when a Aircraft is in approximation to land on a Carrier.

Then it performs some calculations to check what is the Chance of a Sucessful Landing, for that and for now it counts with:

1. Pilot Proficiency
2. How much time is the Pilot Flying (I have seen in a doc that tired pilots after flying in a mission for 6 or 8 hours are more prone to error)-- So it calculates how much time the aircraft was flying and attributes a score.

3. Time of the Day (Night, Day, Dawn, Dusk) - Visibility
4. Sea State/Wind
5. State of the Sky
6. Level of Rainfall

7. Proficiency of the Crew of the Aircraft Carrier (For instance like in that episode that an E-2 failed a landing because of the poor maintenance of the arresting gears having one break, finding the report that a better crew maintenance could avoid the problem.

If a landing is sucessful then the Aircraft Lands.

If not there can happen two things: or the Plane crashes (small probability and the variables above play a role) or the Plane simple misses and will return to land again on the aircrat.

If the aircraft simple misses the Aircraft is teleported to a small point at 52º west at +- 9nm at 1066m to repeat the landing. In this teleportation the aircraft looses a small amount of fuel corresponding to the teleportation travel with Afterburner on the carrier deck. I use the method of teleportation because I think it is the best. First I cannot change with LUA the route of an aircraft at the final stage of landing in a Aircraft Carrier, the other option was to assign the aircraft to a mission of support with reference points near the Aircraft Carrier but the problem of that solution was that the aircraft lost info of the previous mission, so the best option was to opt for a teleportation.

If the plane crashes what happens is the fuel of the aircraft is place to 0 to crash. I had problems with the Function of delete Unit and I don't know why.

A final report is presented.

It is good to have in some cases at night and with bad weather at least an Aircraft to refuel aircrats that are missing the landings as in real life that happens.

The script has some stuff in Portuguese my Native Speaker language. The final Reports are in English.

There is a lot to improve, I know that and I'm trying some new stuff. Some values are hardcoded but I have a Longer Version that allows to change stuff.

--------The Code/Short Version------

---COMPILAR SCRIPT DO CV---

---1º PASSO COLOCAÇÂO de PONTOS ---

---COMPILAÇÃO DE TABELAS---

local listaunidades = VP_GetSide({side='Blue'}).units

tabelaa = {}


for k, v in ipairs(listaunidades) do
local unit = ScenEdit_GetUnit({guid=v.guid})
if unit.type == 'Ship' and unit.subtype == '2001' or unit.subtype == '2002' or unit.subtype == '2003' or unit.subtype == '2004' or unit.subtype == '2007' or unit.subtype == '2008' then table.insert(tabelaa, v) end
end

print(tabelaa)

---FIM---
---INICACAÇAO DOS PONTOS A VOLTA DO CV----

for k, v in ipairs(tabelaa) do
local b0 = v.name
local b1 = "entrada"
local u = ScenEdit_GetUnit({guid=v.guid})
local qtpontos = 4
local area = 360/qtpontos
for i=1,qtpontos do
local c = i * area
local b2 = i + 0
local b3 = b1..b0..b2
ScenEdit_AddReferencePoint({side='Blue', RelativeTo=u.guid, name=b3, bearingtype=0, bearing=c ,distance=1, highlighted=true })
end
end

---COLOCAÇÃO DE PONTO--
for k, v in ipairs(tabelaa) do
local u = ScenEdit_GetUnit({guid=v.guid})
latitudecv = u.latitude
longitudecv = u.longitude

local hlatlon = 0.18505664276415

radiano = 0.92201803953
angulo = 52.8277423915014


local bearingcirculorad = math.rad(u.heading)
local anguloponto = math.abs((bearingcirculorad - radiano))


local xref = 0
local yref = 0

if u.heading < angulo then xref = -1 yref = 1
elseif u.heading < (angulo + 90) then xref = 1 yref = 1
elseif u.heading < (angulo + 180) then xref = 1 yref = -1
elseif u.heading < (angulo + 270) then xref = -1 yref = -1
else
end

novalat = math.sin(anguloponto)*hlatlon*yref + (u.latitude)
novalong = math.cos(anguloponto)*hlatlon*xref + (u.longitude)

local b0 = u.name
local nteletransporte = "teletransporte"
local nfteletransporte = nteletransporte..b0

ScenEdit_AddReferencePoint({side="Blue", name=nfteletransporte, lat=novalat, lon=novalong, RelativeTo=u.name, bearingtype=1, highlighted =true})

end
---FIM---

-------------------------------------------------------------------------EVENTO-------------------------------------------------------------------

---ESTABELECER LIGAÇÃO DE UNIDADES---
math.randomseed(os.time())
local aterrador = ScenEdit_UnitX()
local basecv = aterrador.base
local distanciaaterradorbasecv = Tool_Range(basecv.guid, aterrador.guid)
local aterrou = false

local b0 = basecv.name
local nteletransporte = "teletransporte"
local nfteletransporte = nteletransporte..b0

local pontodereferencia = ScenEdit_GetReferencePoint({side="Blue", name=nfteletransporte})

print(pontodereferencia)

-------VAMOS AO AVIAO-------
---- VAMOS AO PILOTO E TRIPULACAO-------

local nivelpiloto = 100
local cansacopiloto = 100
local nivelpilotoqueda = 1
local nivelcv = 100

if aterrador.proficiency == 'Novice' then nivelpilotoqueda = 1.20
elseif aterrador.proficiency == 'Cadet' then nivelpilotoqueda = 1.10
elseif aterrador.proficiency == 'Regular' then nivelpilotoqueda = 1
elseif aterrador.proficiency == 'Veteran' then nivelpilotoqueda = 0.75
elseif aterrador.proficiency == 'Ace' then nivelpilotoqueda = 0.5
else
end


if aterrador.proficiency == 'Novice' then nivelpiloto = 60
elseif aterrador.proficiency == 'Cadet' then nivelpiloto = 65
elseif aterrador.proficiency == 'Regular' then nivelpiloto = 75
elseif aterrador.proficiency == 'Veteran' then nivelpiloto = 90
elseif aterrador.proficiency == 'Ace' then nivelpiloto = 95
else
end

if basecv.proficiency == 'Novice' then nivelcv = 85
elseif basecv.proficiency == 'Cadet' then nivelcv = 90
elseif basecv.proficiency == 'Regular' then nivelcv = 95
elseif basecv.proficiency == 'Veteran' then nivelcv = 98.5
elseif basecv.proficiency == 'Ace' then nivelcv = 99.5
else
end

---TEMPO DE VOO---

local s = aterrador.airbornetime
local nhorasvoo = 0
local nminutosvoo = 0
local nsegundosvoo = 0

local mhorasvoo = string.match(s, "%d+%shr")
if mhorasvoo == nil then nhorasvoo = 0 else nhorasvoo = string.match(mhorasvoo, "%d+") end
local mminutosvoo = string.match(s, "%d+%smin")
if mminutosvoo == nil then nminutosvoo = 0 else nminutosvoo = string.match(mminutosvoo, "%d+") end
local msegundosvoo = string.match(s, "%d+%ssec")
if msegundosvoo == nil then nsegundosvoo = 0 else nsegundosvoo = string.match(msegundosvoo, "%d+") end

local tempovoo = nhorasvoo*3600 + nminutosvoo*60 + nsegundosvoo


if tempovoo >= 0 and tempovoo <= 10800 then cansacopiloto = 100
elseif tempovoo >= 10801 and tempovoo <= 14400 then cansacopiloto = 95
elseif tempovoo >= 14401 and tempovoo <= 21600 then cansacopiloto = 85
elseif tempovoo >= 21601 and tempovoo <= 28800 then cansacopiloto = 75
elseif tempovoo >= 28801 then cansacopiloto = 65
else
end

-----VAMOS À METEOROLOGIA------
local unicentral = basecv

local ipma = ScenEdit_GetWeather()
print(ipma)

local tdia = ScenEdit_GetTimeOfDay({lat=unicentral.latitude, long=unicentral.longitude})
print(tdia)

local estadoceu = 100
local estadotemp = 100
local estadochuva = 100
local estadomarvento = 100

if tdia.tod == 0 then estadoceu = 100
elseif tdia.tod == 2 then estadoceu = 65
elseif tdia.tod == 3 then estadoceu = 95
elseif tdia.tod == 4 then estadoceu = 95
else
end
print(estadoceu)

if ipma.rainfall == 0 then local estadochuva = 100
elseif ipma.rainfall >= 1 and ipma.rainfall <= 4 then local estadochuva = 95
elseif ipma.rainfall >= 5 and ipma.rainfall <= 9 then local estadochuva = 90
elseif ipma.rainfall >= 10 and ipma.rainfall <= 19 then local estadochuva = 80
elseif ipma.rainfall >= 20 and ipma.rainfall <= 29 then local estadochuva = 65
elseif ipma.rainfall >= 30 and ipma.rainfall <= 39 then local estadochuva = 55
elseif ipma.rainfall >= 40 and ipma.rainfall <= 50 then local estadochuva = 45
else
end
print(estadochuva)

if ipma.seastate == 0 then estadomarvento = 100
elseif ipma.seastate == 1 then estadomarvento = 95
elseif ipma.seastate == 2 then estadomarvento = 90
elseif ipma.seastate == 3 then estadomarvento = 80
elseif ipma.seastate == 4 then estadomarvento = 70
elseif ipma.seastate == 5 then estadomarvento = 60
elseif ipma.seastate == 6 then estadomarvento = 50
elseif ipma.seastate == 7 then estadomarvento = 45
elseif ipma.seastate == 8 then estadomarvento = 40
elseif ipma.seastate == 9 then estadomarvento = 35
else
end
print(estadomarvento)

---VAMOS A ATERRAGEM

local condaterrador = false
if aterrador.category == '2002' then condaterrador = true else condaterrador = false end

local vaiaterrar = false


if aterrador.condition == 'On final approach' or aterrador.condition == 'RTB_Manual' and condaterrador == true then vaiaterrar = true else vaiaterrar = false end
print("ATERRADOR CONDITION")
print(aterrador.condition)


---VAMOS VER SE TEM SUCESSO E CONSEQUENCIAS---

local probaterragem = 1
local probacidente = 1
local basedificuldade = 100
local aleaterragem = math.random(1,basedificuldade)
local aleacidente = math.random(1,basedificuldade)

local probaterragem = (nivelpiloto/100)*(cansacopiloto/100)*(estadoceu/100)*(estadotemp/100)*(estadochuva/100)*(estadomarvento/100)*(nivelcv/100)
print("PROBABILIDADE ATERRAGEM %")
print(probaterragem)
local probacidente = ((1-probaterragem)*nivelpilotoqueda)*10
local probacidentefinal = 100-probacidente


local stringfuel = "% de Combustivel "
local fuel = aterrador.fuel
local fuelrate = fuel[2001].current/fuel[2001].max * 100
local constantefuel = 0.991980049
print(stringfuel..fuelrate)



if vaiaterrar == true
then
if probaterragem*100 >= aleaterragem then aterrou = true ScenEdit_SpecialMessage('Blue', "The  " ..aterrador.name.. "  Performed a Sucessful Landing on the Carrier " ..basecv.name.. "<p>Chance of Landing Sucessfuly = </p>" ..probaterragem.. "<p> Score </p>" ..aleaterragem) end
if aterrou == false and probacidentefinal <= aleacidente then
fuel[2001].current = 0
aterrador.fuel = fuel
ScenEdit_SpecialMessage('Blue', "Tragic News. The  " ..aterrador.name.. "  Crashed during his attempt to Landing on the Carrier  " ..basecv.name.. "<p>Chance of Landing Sucessfuly = </p>" ..probaterragem.. "<p> Score </p>" ..aleaterragem.. "<p>Chance of NOT having an accident = </p>"..probacidentefinal.."<p> Score </p>" ..aleacidente)
elseif aterrou == false and probacidentefinal > aleacidente then
aterrador.latitude = pontodereferencia.latitude
aterrador.longitude = pontodereferencia.longitude
aterrador.altitude = 1066
fuel[2001].current = fuel[2001].current - (fuel[2001].max - (fuel[2001].max*constantefuel))
aterrador.fuel = fuel
ScenEdit_SpecialMessage('Blue', "The  " ..aterrador.name.. "  Failed Landing on the Carrier  " ..basecv.name.. "<p>Chance of Landing Sucessfuly = </p>" ..probaterragem.. "<p> Score </p>" ..aleaterragem.. "<p>Chance of NOT having an accident = </p>"..probacidentefinal.."<p> Score </p>" ..aleacidente)
end

end



print("NUMERO DOS DADOS")
print(aleaterragem)
print("ATERROU?")
print(aterrou)
print("PROBABILIDADE ACIDENTE")
print(probacidentefinal)
print("NUMEROS DOS DADOS ACIDENTE")
print(aleacidente)

------------------------


Hope you like it. Any doubts don't hesitate to ask.
Best Regards,

Attachment (1)

< Message edited by rbsj -- 7/22/2020 12:08:21 PM >
Post #: 1
RE: Sharing my LUA Code to 'Simulate' Carrier Landings - 7/19/2020 4:29:04 PM   
Parel803

 

Posts: 579
Joined: 10/10/2019
From: Netherlands
Status: offline
Thx for sharing. Nice for me to learn from examples.
with regards

(in reply to rbsj)
Post #: 2
RE: Sharing my LUA Code to 'Simulate' Carrier Landings - 7/19/2020 5:51:10 PM   
thewood1

 

Posts: 6529
Joined: 11/27/2005
Status: offline
There's also a whole sub forum on Lua under mods if you really want some examples.

(in reply to Parel803)
Post #: 3
RE: Sharing my LUA Code to 'Simulate' Carrier Landings - 7/20/2020 1:34:36 AM   
Rory Noonan

 

Posts: 2816
Joined: 12/18/2014
From: Brooklyn, NY
Status: offline
Really impressive work rbsj!

quote:

ORIGINAL: rbsj

If the plane crashes what happens is the fuel of the aircraft is place to 0 to crash. I had problems with the Function of delete Unit and I don't know why.



Have you tried ScenEdit_KillUnit?

_____________________________


(in reply to rbsj)
Post #: 4
RE: Sharing my LUA Code to 'Simulate' Carrier Landings - 7/20/2020 2:54:00 PM   
rbsj

 

Posts: 14
Joined: 12/4/2018
From: Portugal
Status: offline
Thank you very much Apache85.

I have learn a lot from you and your scenarios and from Whicker. I love your Sakura Scenario.

Yes I have tried the ScenEdit_KillUnit and I don't know why it simply didn't work.

Sometimes some pieces of code simply don't work. And after checking all posible mistakes or distractions on the code you simply became perplexed. Sometimes it feels like some LaTex errors, that you simply can't figure out why that line is not working.

Recently I have been working with a code to simulate a Comms Jammed environment, being the JAM made by a EC-130 Compass Call and I was making some experiments and I was playing with the ScenEdit_SetUnitDamage() function and out of the blue that function simply stopped working with Aircraft, but still worked with Ships. I was puzzled, then I reboot the pc and the function returned to work as intended without code being changed.

I'm working with this idea of having a Comm Jamming environment with their challenges. I know the Pro Edition have that. I would love to have the Pro Edition I would buy it and pay for it. But the thing I would love the most is to edit the Database or at least being able to use LUA to edit more the units, like giving them properties (e.g. Replenish to ships), giving air facilities (e.g. Transform some Destroyers in Destroyer Helicopters), changing propulsion and energy (e.g. Transforming Zumwalt a nuclear ship), etc.

I follow your work.

My Best Regards,




You and

(in reply to Rory Noonan)
Post #: 5
RE: Sharing my LUA Code to 'Simulate' Carrier Landings - 7/20/2020 3:34:52 PM   
SeaQueen


Posts: 1451
Joined: 4/14/2007
From: Washington D.C.
Status: offline
Interesting work.

Now I'll have to translate the variable names so I can pick it apart...

(in reply to rbsj)
Post #: 6
RE: Sharing my LUA Code to 'Simulate' Carrier Landings - 7/20/2020 4:10:12 PM   
rbsj

 

Posts: 14
Joined: 12/4/2018
From: Portugal
Status: offline
I Post here a ENGLISH Friendly version



--------The Code/Short Version------

---COMPILAR SCRIPT DO CV---

---1º PASSO COLOCAÇÂO de PONTOS ---

---THIS IS THE FIRST PART TO BE USED WHEN YOU CREATE THE SCENARIO---

local listaunidades = VP_GetSide({side='Blue'}).units

tabelaa = {}


for k, v in ipairs(listaunidades) do
local unit = ScenEdit_GetUnit({guid=v.guid})
if unit.type == 'Ship' and unit.subtype == '2001' or unit.subtype == '2002' or unit.subtype == '2003' or unit.subtype == '2004' or unit.subtype == '2007' or unit.subtype == '2008' then table.insert(tabelaa, v) end
end -- This identifies Carrier units.

print(tabelaa)

---END---
---CREATING REFERENCE POINTS CENTERED ON CARRIER - THIS IS THE SECOND PART----

for k, v in ipairs(tabelaa) do
local b0 = v.name
local b1 = "entrada" -- This string translates to Entrance
local u = ScenEdit_GetUnit({guid=v.guid})
local qtpontos = 4 -- Number of points that made the area (4=Square), (3=Triangle), (6=Hexagon), etc...
local area = 360/qtpontos
for i=1,qtpontos do
local c = i * area
local b2 = i + 0
local b3 = b1..b0..b2
ScenEdit_AddReferencePoint({side='Blue', RelativeTo=u.guid, name=b3, bearingtype=0, bearing=c ,distance=1, highlighted=true })
end
end

---THIRD PART CREATING THE REFERENCE POINT WHERE THE PLANE WILL BE TELEPORTED HAVING IN MIND CARRIER BEARING, THIS ALSO SHOULD BE DONE BEFORE--
for k, v in ipairs(tabelaa) do
local u = ScenEdit_GetUnit({guid=v.guid})
latitudecv = u.latitude
longitudecv = u.longitude

local hlatlon = 0.18505664276415 -- THIS IS THE HYPOTENUSE

---YOU CAN CREATE AN HYPOTENUSE AND SUBSTITUTE THAT VALUE BY OBTAINING A POINT IN MAP AND USE THIS SHORT-CODE---
--latitudecv = basecv.latitude
--longitudecv = basecv.longitude

--latitudeponto = 56.4110409831988
--longitudeponto = 6.84503686406166

--dflatitude = latitudeponto - latitudecv
--dflongitude = longitudeponto - longitudecv

--print(dflatitude)
--print(dflongitude)

--mlocal h = distanciaaterradorbasecv
--local a = dflatitude
--local b = dflongitude

--local hlatlon = math.sqrt((a^2 + b^2))

---------



radiano = 0.92201803953 -- THE RADIAN
angulo = 52.8277423915014 -- THE ANGLE

--OPTIONAL YOU CAN CHANGE THIS VALUES AND MADE YOURS WITH THIS SHORT CODE--
--seno = math.abs(b)/hlatlon
--radiano = math.asin(seno)
--angulo = math.deg(radiano)
--print(angulo)
--------------------------------

local bearingcirculorad = math.rad(u.heading) -- THIS GIVES THE RADIAN OF THE CV BEARING
local anguloponto = math.abs((bearingcirculorad - radiano)) -- THIS GIVES THE DIFERENCE BETWEEN THE POINT AND THE CV


local xref = 0
local yref = 0

if u.heading < angulo then xref = -1 yref = 1
elseif u.heading < (angulo + 90) then xref = 1 yref = 1
elseif u.heading < (angulo + 180) then xref = 1 yref = -1
elseif u.heading < (angulo + 270) then xref = -1 yref = -1
else
end -- THIS GIVES THE DIRECTION OF THE POINT BY IDENTIFYING THE REFERENTIAL SYSTEM

novalat = math.sin(anguloponto)*hlatlon*yref + (u.latitude) --THIS GIVES THE LATITUDE OF THE POINT
novalong = math.cos(anguloponto)*hlatlon*xref + (u.longitude) --THIS GIVES THE LONGITUDE OF THE POINT

local b0 = u.name
local nteletransporte = "teletransporte"
local nfteletransporte = nteletransporte..b0

ScenEdit_AddReferencePoint({side="Blue", name=nfteletransporte, lat=novalat, lon=novalong, RelativeTo=u.name, bearingtype=1, highlighted =true}) -- THIS IS FOR CREATING THE TELEPORTING POIN

end
---END---

-------------------------------------------------------------------------EVENT----------------------------------------------NOW YOU CREATE THE EVENT AND USE THE AREA POINTS TO BE THE TRIGGER NOW WILL BE THE ACTION---------------------

---STABLISH UNITS RELATIONS---
math.randomseed(os.time())
local aterrador = ScenEdit_UnitX() -- THE AIRCRAFT
local basecv = aterrador.base -- THE CARRIER
local distanciaaterradorbasecv = Tool_Range(basecv.guid, aterrador.guid) -- THE DISTANCE BETWEEN CARRIER AND AIRCRAFT
local aterrou = false -- THIS VARIABLE MEANS IF THE PLANE HAS LANDED

local b0 = basecv.name
local nteletransporte = "teletransporte"
local nfteletransporte = nteletransporte..b0

local pontodereferencia = ScenEdit_GetReferencePoint({side="Blue", name=nfteletransporte}) -- THIS WILL IDENTIFY THE TELEPORT POINT THAT WE ALREADY HAVE PROCESSED

print(pontodereferencia)

-------LET'S GO TO THE AIRCRAFT AND THE VARIABLES-------


local nivelpiloto = 100 -- THIS MEANS THE PROFICIENCY OF THE PILOTO
local cansacopiloto = 100 -- THIS MEANS THE TIREDNESS OF THE PILOT
local nivelpilotoqueda = 1 -- THIS MEANS THE ERROR OF THE PILOT TO CRASH AIRCRAFT
local nivelcv = 100 -- THIS MEANS THE PROFICIENCY OF THE AIRCRAFT CARRIER CREW

if aterrador.proficiency == 'Novice' then nivelpilotoqueda = 1.20
elseif aterrador.proficiency == 'Cadet' then nivelpilotoqueda = 1.10
elseif aterrador.proficiency == 'Regular' then nivelpilotoqueda = 1
elseif aterrador.proficiency == 'Veteran' then nivelpilotoqueda = 0.75
elseif aterrador.proficiency == 'Ace' then nivelpilotoqueda = 0.5
else
end


if aterrador.proficiency == 'Novice' then nivelpiloto = 60
elseif aterrador.proficiency == 'Cadet' then nivelpiloto = 65
elseif aterrador.proficiency == 'Regular' then nivelpiloto = 75
elseif aterrador.proficiency == 'Veteran' then nivelpiloto = 90
elseif aterrador.proficiency == 'Ace' then nivelpiloto = 95
else
end

if basecv.proficiency == 'Novice' then nivelcv = 85
elseif basecv.proficiency == 'Cadet' then nivelcv = 90
elseif basecv.proficiency == 'Regular' then nivelcv = 95
elseif basecv.proficiency == 'Veteran' then nivelcv = 98.5
elseif basecv.proficiency == 'Ace' then nivelcv = 99.5
else
end

---TIME THAT THE AIRCRAFT HAVE FLOWN BEFORE LANDING CARRIER---

local s = aterrador.airbornetime -- AIRCRAFT AIRBORNE TIME
local nhorasvoo = 0 -- NUMBER OF HOURS FLOWN
local nminutosvoo = 0 -- NUMBER OF MINUTES FLOWN
local nsegundosvoo = 0 -- NUMBER OF SECONDS FLOWN

local mhorasvoo = string.match(s, "%d+%shr")
if mhorasvoo == nil then nhorasvoo = 0 else nhorasvoo = string.match(mhorasvoo, "%d+") end
local mminutosvoo = string.match(s, "%d+%smin")
if mminutosvoo == nil then nminutosvoo = 0 else nminutosvoo = string.match(mminutosvoo, "%d+") end
local msegundosvoo = string.match(s, "%d+%ssec")
if msegundosvoo == nil then nsegundosvoo = 0 else nsegundosvoo = string.match(msegundosvoo, "%d+") end -- THIS FUNCTIOS REGURN THE AIRBORNE TIME THAT COMES AS A STRING AND BREAKS THAT STRING TO CONVERT THE NUMBERS INTO INTEGERS

local tempovoo = nhorasvoo*3600 + nminutosvoo*60 + nsegundosvoo -- THIS CALCULATES THE TIME FLOWN IN SECONDS


if tempovoo >= 0 and tempovoo <= 10800 then cansacopiloto = 100
elseif tempovoo >= 10801 and tempovoo <= 14400 then cansacopiloto = 95
elseif tempovoo >= 14401 and tempovoo <= 21600 then cansacopiloto = 85
elseif tempovoo >= 21601 and tempovoo <= 28800 then cansacopiloto = 75
elseif tempovoo >= 28801 then cansacopiloto = 65
else
end

-----METEOROLOGY------
local unicentral = basecv

local ipma = ScenEdit_GetWeather()
print(ipma)

local tdia = ScenEdit_GetTimeOfDay({lat=unicentral.latitude, long=unicentral.longitude})
print(tdia)

local estadoceu = 100 --STATE OF THE SKY
local estadotemp = 100 -- STATE OF THE TEMPERATURE -- NOT USED
local estadochuva = 100 -- STATE OF THE RAINFALL
local estadomarvento = 100 -- STATE OF THE SEASTATE WIND

if tdia.tod == 0 then estadoceu = 100 -- DAY
elseif tdia.tod == 2 then estadoceu = 65 -- NIGHT
elseif tdia.tod == 3 then estadoceu = 95 -- DUSK
elseif tdia.tod == 4 then estadoceu = 95 -- DAWN
else
end
print(estadoceu)

if ipma.rainfall == 0 then local estadochuva = 100
elseif ipma.rainfall >= 1 and ipma.rainfall <= 4 then local estadochuva = 95
elseif ipma.rainfall >= 5 and ipma.rainfall <= 9 then local estadochuva = 90
elseif ipma.rainfall >= 10 and ipma.rainfall <= 19 then local estadochuva = 80
elseif ipma.rainfall >= 20 and ipma.rainfall <= 29 then local estadochuva = 65
elseif ipma.rainfall >= 30 and ipma.rainfall <= 39 then local estadochuva = 55
elseif ipma.rainfall >= 40 and ipma.rainfall <= 50 then local estadochuva = 45
else
end
print(estadochuva)

if ipma.seastate == 0 then estadomarvento = 100
elseif ipma.seastate == 1 then estadomarvento = 95
elseif ipma.seastate == 2 then estadomarvento = 90
elseif ipma.seastate == 3 then estadomarvento = 80
elseif ipma.seastate == 4 then estadomarvento = 70
elseif ipma.seastate == 5 then estadomarvento = 60
elseif ipma.seastate == 6 then estadomarvento = 50
elseif ipma.seastate == 7 then estadomarvento = 45
elseif ipma.seastate == 8 then estadomarvento = 40
elseif ipma.seastate == 9 then estadomarvento = 35
else
end
print(estadomarvento)

---LET'S GO TO LANDING---

local condaterrador = false
if aterrador.category == '2002' then condaterrador = true else condaterrador = false end --- CHECKS IF THE AIRCRAFT CAN IS AN AIRCRAFT CARRIER CAPABLE

local vaiaterrar = false


if aterrador.condition == 'On final approach' or aterrador.condition == 'RTB_Manual' and condaterrador == true then vaiaterrar = true else vaiaterrar = false end -- THIS CHECKS IF WE ARE AT THE FINAL APPROACH TO LAND ON THE CV
print("ATERRADOR CONDITION")
print(aterrador.condition)


---VAMOS VER SE TEM SUCESSO E CONSEQUENCIAS---

local probaterragem = 1
local probacidente = 1
local basedificuldade = 100 -- BASE DIFICULTY CAN BE CHANGED
local aleaterragem = math.random(1,basedificuldade) -- THIS GIVES A RANDOM NUMBER TO CALCULATE LANDING
local aleacidente = math.random(1,basedificuldade) -- THIS GIVES A RANDOM NUMBER TO CALCULATE % OF CRASH

local probaterragem = (nivelpiloto/100)*(cansacopiloto/100)*(estadoceu/100)*(estadotemp/100)*(estadochuva/100)*(estadomarvento/100)*(nivelcv/100)-- THIS CALCULATES THE CHANCE OF THE AIRCRAFT FAILS WITH THE VARIABLES
print("PROBABILIDADE ATERRAGEM %")
print(probaterragem)
local probacidente = ((1-probaterragem)*nivelpilotoqueda)*10 -- THIS CALCULATES USING THE ABOVE VALUE PLUS THE VALUE OF THE PILOT SKILL A DIFFERENT VARIABLE TO CALCULATE THE CHANCE OF CRASH
local probacidentefinal = 100-probacidente


local stringfuel = "% de Combustivel " -- THIS IS TO CALCULATE THE AMOUNT OF FUEL NEEDED FOR THE TELEPORT
local fuel = aterrador.fuel
local fuelrate = fuel[2001].current/fuel[2001].max * 100
local constantefuel = 0.991980049
print(stringfuel..fuelrate)



if vaiaterrar == true
then
if probaterragem*100 >= aleaterragem then aterrou = true ScenEdit_SpecialMessage('Blue', "The " ..aterrador.name.. " Performed a Sucessful Landing on the Carrier " ..basecv.name.. "<p>Chance of Landing Sucessfuly = </p>" ..probaterragem.. "<p> Score </p>" ..aleaterragem) end
if aterrou == false and probacidentefinal <= aleacidente then
fuel[2001].current = 0
aterrador.fuel = fuel
ScenEdit_SpecialMessage('Blue', "Tragic News. The " ..aterrador.name.. " Crashed during his attempt to Landing on the Carrier " ..basecv.name.. "<p>Chance of Landing Sucessfuly = </p>" ..probaterragem.. "<p> Score </p>" ..aleaterragem.. "<p>Chance of NOT having an accident = </p>"..probacidentefinal.."<p> Score </p>" ..aleacidente)
elseif aterrou == false and probacidentefinal > aleacidente then
aterrador.latitude = pontodereferencia.latitude
aterrador.longitude = pontodereferencia.longitude
aterrador.altitude = 1066
fuel[2001].current = fuel[2001].current - (fuel[2001].max - (fuel[2001].max*constantefuel))
aterrador.fuel = fuel
ScenEdit_SpecialMessage('Blue', "The " ..aterrador.name.. " Failed Landing on the Carrier " ..basecv.name.. "<p>Chance of Landing Sucessfuly = </p>" ..probaterragem.. "<p> Score </p>" ..aleaterragem.. "<p>Chance of NOT having an accident = </p>"..probacidentefinal.."<p> Score </p>" ..aleacidente)
end

end -- THIS IS THE LAST PART THAT FINISHES BY OR THE PLANE LANDS, OR THE PLANE TELEPORTS, OR THE PLANE CRASHES.



print("NUMERO DOS DADOS")
print(aleaterragem)
print("ATERROU?")
print(aterrou)
print("PROBABILIDADE ACIDENTE")
print(probacidentefinal)
print("NUMEROS DOS DADOS ACIDENTE")
print(aleacidente)

------------------------


Hope this help you. And thank you.

Best Regards,

(in reply to rbsj)
Post #: 7
RE: Sharing my LUA Code to 'Simulate' Carrier Landings - 7/20/2020 9:33:44 PM   
kevinkins


Posts: 2257
Joined: 3/8/2006
Status: offline
Very interesting. Can you provide the Community a scenario file with script embedded so we can watch it run? I wonder if the Pro version folks would want to see this in action and check if the assumptions are OK.

BTW, be mindful there are known problems with the random seed function in lua and it's best to run the seed several times prior to using the number. Maybe in 2020 it's been fixed. But something to check out.

Kevin

_____________________________

“The study of history lies at the foundation of all sound military conclusions and practice.”
― Alfred Thayer Mahan


(in reply to rbsj)
Post #: 8
RE: Sharing my LUA Code to 'Simulate' Carrier Landings - 7/21/2020 2:05:59 AM   
SeaQueen


Posts: 1451
Joined: 4/14/2007
From: Washington D.C.
Status: offline
quote:

ORIGINAL: kevinkins
Very interesting. Can you provide the Community a scenario file with script embedded so we can watch it run? I wonder if the Pro version folks would want to see this in action and check if the assumptions are OK.


I doubt it. One of the achievements of American naval aviation is that there are sufficiently few crashes that it isn't necessary to plan around them. It takes a lot of effort and experience to do that, but if you're crashing a lot of airplanes everything stops. If things were so bad that CVW commanders were like, "we expect to lose XX% just due to crashes in the normal course of operating, with or without the enemy," he'd be considered incompetent and be relieved. Pilots have no desire to die. While what they do is certainly dangerous, they work very hard to keep the risk negligible. If there's a bunch of crashes there'd be some process for figuring out what's going wrong and how to correct.

This isn't to say aircraft don't bolter. They do, and it's frequent, but that shouldn't make a huge impact on the kinetic aspects of the conflict that Command tends to focus on. Air warfare tends to occur in "pulses" and so if there is a crash on the recovery of aircraft is just a tail end "tax" on the more interesting factors involving aircraft survivability.


(in reply to kevinkins)
Post #: 9
RE: Sharing my LUA Code to 'Simulate' Carrier Landings - 7/22/2020 12:31:33 PM   
rbsj

 

Posts: 14
Joined: 12/4/2018
From: Portugal
Status: offline
Dear Community,

I post here and I have place at the start of this post a Scenario with this code.

kevinkis My assumptions numbers certainly are not OK. Since I don't have any numbers as the US NAVY have with the stats of carrier landings, there is no way that I'm correct. Regarding the random seed function I know the problems.

SeaQueen Yes you are right. With the numbers that I have placed there will be much more crashes in the scenarion than those that happen in reality. I'm aware of that fact and I have use those numbers purposefully. Let me explain why:

In this simulation sometimes we use something that is called 'exagerated reality', e.g. since the timespan of some scenarios are so small and the player probably will play a scenario one or two times, we exagerate the effects to the player be aware of their existence and not ignore them. Also sometimes they are used as a Pedagogical tool to the player avoid unreal shortcuts.

When I made this small code my purpose was to give a more challenging Carrier Operations. I knew that was to easy to recover planes and conduct carrier operations and I need to pay a price for my options. For instance, if I keept a plane too long in the air, by being refuelled ad infinitum, at the time of their recover I have to be conscious of the risks. This forces me to change some playing habits.

My numbers are also wrong, and this is the core of my idea and of some of my codes to this game, that Weather and other conditions still have a big impact on Modern Warfare and in Military Operations.

Look at this, it is more easy to launch an aircraft from a carrier than it is to recover it. If there is no conditions to safely recover Aircrafts they will not be launched from the carrier. In this simulation Carriers have not their operations conditioned by the weather conditions. You can use them at their full operation capability, but we know that is not the case. The biggest blunt of the Carrier Strike Group is their Aircraft. If the Aircraft cannot operate the CSG have their power restricted.

I noted that it was worthless to create Weather Tables and change the Weather if there is no true limitation in military operations by how the Weather is Changed. The only thing you know is that Sensors will be affected period. But in reality not only sensors are affected, military operations will be conditioned. For instance, military assets will always seek to avoid bad weather conditions. CSG will not sail directly to the eye of storms. Also, ASW operations that are done with hellos will be conditioned by the Winds and Rainfall levels. At the same time that conditions don't place submarines in jeopardise. Therefore, at this conditions submarines have an addvantage over Surface Ships (those relying only in Helos).

At the end my main Idea is that Night, Weather Conditions even with todays technology affects much more Military operations then what is represented in this simulation.

And in the Cold War things were worse.

Best Regards,
Ricardo



Attachment (1)

< Message edited by rbsj -- 7/22/2020 12:35:31 PM >

(in reply to SeaQueen)
Post #: 10
Page:   [1]
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Sharing my LUA Code to 'Simulate' Carrier Landings 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

2.734