Random Airbase Locations - [RESOLVED]

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
spinnaker
Posts: 5
Joined: Sat Apr 15, 2017 7:07 pm

Random Airbase Locations - [RESOLVED]

Post by spinnaker »

Hello all


I have the below code that when run ends up giving me

Code: Select all

ERROR: Sequence contains no matching element

I don't know where I goofed and if its syntax or somewhere else. I snagged this from another online git or site and added my own details and units but I cant remember where. The purpose is to randomize the starting locations of the US FARPs for a SCS scenario I'm developing. I would appreciate any help!

i had to omit the lat and long to post bc im less than 10 posts and it thought it was a phone#

Code: Select all

 math.randomseed(os.time())
 math.random(); math.random(); math.random()
 a = math.random(1,3)
 b = math.random(1,3)
 
 local distanceJitter = .3 -- varies the distance between units so it is not a perfect grid
 
 local angleJitter =.3 -- varies the angle used to place units so it is not a perfect grid
 
 function WW_CreateAirBase(basename,latlonTable, runways, taxiways, accesspoints, tarmacspaces, ammopads)
     local lat=latlonTable['latitude']
     local lon=latlonTable['longitude']
     for i=1,runways do
     local u = ScenEdit_AddUnit({side='Abandoned', type='Facility', name='Runway '..i, dbid=55, autodetectable=true, Lat=lat, Lon=lon})
     u.group =basename 
     lat=lat +.004
     end --runways
     
     lat=latlonTable['latitude'] --reset lat and add to lon
     
     lon=latlonTable['longitude'] + .002
     for i=1,taxiways do
     local u = ScenEdit_AddUnit({side='United States', type='Facility', name='Taxiway '..i, dbid=1424, autodetectable=true, Lat=lat, Lon=lon})
     u.group =basename 
     lat=lat +.004
     end
     lat=latlonTable['latitude'] --reset lat and add to lon
     
     lon=latlonTable['longitude'] + .004
     for i=1,accesspoints do
     local u = ScenEdit_AddUnit({side='United States', type='Facility', name='Access Point '..i, dbid=353, autodetectable=true, Lat=lat, Lon=lon})
     u.group =basename 
     lat=lat +.004
     end
     lat=latlonTable['latitude'] --reset lat and add to lon
     
     lon=latlonTable['longitude'] + .006
     for i=1,accesspoints do
     local u = ScenEdit_AddUnit({side='United States', type='Facility', name='Tarmac Space '..i, dbid=344, autodetectable=true, Lat=lat, Lon=lon})
     u.group =basename 
     lat=lat +.002
     end
     lat=latlonTable['latitude'] --reset lat and add to lon
     
     lon=latlonTable['longitude'] + .008
     for i=1,ammopads do
     local u = ScenEdit_AddUnit({side='United States', type='Facility', name='Ammo Pad '..i, dbid=1496, autodetectable=true, Lat=lat, Lon=lon})
     u.group =basename 
     lat=lat +.002
     end
     end 
 
 if a == 1 then 
     WW_CreateAirBase('MEU FARP South',{latitude='xxxxxxxxx', longitude='xxxxxxxxx'},2,2,4,8,2)
 
 elseif a == 2 then 
     WW_CreateAirBase('MEU FARP South',{latitude='xxxxxxxxx', longitude='xxxxxxxxx'},2,2,4,8,2)
 
 elseif a == 3 then 
     WW_CreateAirBase('MEU FARP South',{latitude='xxxxxxxxx', longitude='xxxxxxxxx'},2,2,4,8,2)
 
 end
 
 if b == 1 then 
     WW_CreateAirBase('MEU FARP North',{latitude='xxxxxxxxxxx', longitude='xxxxx'},2,2,4,8,2)
 
 elseif b == 2 then
     WW_CreateAirBase('MEU FARP North',{latitude='xxxxxxxxx', longitude='xxxxxx'},2,2,4,8,2)
 
 elseif b ==3 then 
     WW_CreateAirBase('MEU FARP North',{latitude='xxxxxx', longitude='xxxxxx'},2,2,4,8,2)
 
 end
 
.. Thanks!
jkgarner
Posts: 175
Joined: Thu Apr 30, 2020 12:42 pm

RE: Random Airbase Locations - Help

Post by jkgarner »

HI.

I downloaded the code and ran it on my Command...

In looking over the function I noticed straight away that the first sub-unit you are creating within the WW_CreateAirBase function has side 'abandoned' while the side value for all other sub-units are 'United States'


Change it to 'United States' and the function should work.

To test my theory, I changed 'abandoned' to 'USA' and all references to 'United States' to 'USA' because I have a side called 'USA' and it simply worked as written.


For greater flexibility in the future, I suggest that take the side as a parameter...

function WW_CreateAirBase(side, baseName, latLonTable, runways, taxiways, accesspoints, tarmacspaces, ammopads)

Then the first thing you should do within the function is to check to make sure a unit(base) for that hide does not already exist.
If it doesn't, you must decide whether to return an error, doing nothing, or overwrite the existing base...

Good luck.

spinnaker
Posts: 5
Joined: Sat Apr 15, 2017 7:07 pm

RE: Random Airbase Locations - Help

Post by spinnaker »

Thanks jkgarner! I should have caught that. Thanks for the other tips, I'm stumbling through this all trail and error
jkgarner
Posts: 175
Joined: Thu Apr 30, 2020 12:42 pm

RE: Random Airbase Locations - Help

Post by jkgarner »

Aren't we all?
Cheers!
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Random Airbase Locations - Help

Post by KnightHawk75 »

ORIGINAL: jkgarner

Aren't we all?
Cheers!


Yup.

Also when you run into forum issues with posting more than a couple lines of code I've found splitting it up in multiple [ code ] [ / code ] blocks each containing 10 or less lines helps retain desired formatting, that or maybe it just looks all garbled and overruns other posts in my browser and not others.
spinnaker
Posts: 5
Joined: Sat Apr 15, 2017 7:07 pm

RE: Random Airbase Locations - Help

Post by spinnaker »

ORIGINAL: KnightHawk75

ORIGINAL: jkgarner

Aren't we all?
Cheers!


Yup.

Also when you run into forum issues with posting more than a couple lines of code I've found splitting it up in multiple [ code ] [ / code ] blocks each containing 10 or less lines helps retain desired formatting, that or maybe it just looks all garbled and overruns other posts in my browser and not others.
Good to know! I was wondering what I was missing on there.
Post Reply

Return to “Lua Legion”