Command Modern Air Naval Operations : Lua Repository #4 Random Traffic

Every so often you meet a script that’s beautiful. My first script moment was when I saw the differential equation for a moon shot rocket as it loses fuel, gravity drops, velocity rises, and… well you get the idea. The below script, courtesy of Apache85, is a script like that. This wonderful bit of poetic code generates random merchant traffic within a set area and assigns it to a mission. I’ll get into more of it after the break, but if you want to bring biologics, fishing boats, aircraft, or whatever to your scenarios, check it out!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
--Merchant generation script (default settings create 8-16 merchant ships in the China Sea assigned to a ferry mission named 'VLADIVOSTOK' on a side named 'Merchant'
math.randomseed(os.time())

merch_num = math.random(20,30) --change 8 or 16 to your specified minimum and maximum number of merchants

for i = 1,merch_num do
redo_count = 0
::redo::
local lat_var = math.random(1,(10^13))
local lon_var = math.random(1,(10^13))
v_lat = math.random(0,2) + (lat_var/(10^13)) --change 27 and 32 to your specified minimum and maximum latitude values; it's important that the first number is smaller than the second.
v_lon = math.random(102,104) + (lon_var/(10^13)) --change 119 and 127 to your specified minimum and maximum longitude values; it's important that the first number is smaller than the second.
elevation = World_GetElevation({latitude=v_lat, longitude=v_lon})
if elevation > -10 then --Checks to see if the water is deep enough, adjust as you please
redo_count = redo_count + 1
if redo_count > 50 then
print ('A ship was not able to find a suitable spot for placement. Re-check latitude and longitude settings')
break --this cuts the loop if there are no suitable positions found after 50 tries, prevents infinite loop/game freeze
else
goto redo --retries the placement if the water is too shallow
end
end
DBIDTABLE = { 775, 2027, 2029, 2028, 2030, 774, 2026, 2031, 2775, 2023, 773, 2774, 1001, 1374, 2773, 2776, 1006, 222, 1599, 2034, 1002, 1317, 144, 339, 275, 145, 2022, 259 } --list of DBIDs
DBID = DBIDTABLE[math.random( 1, #DBIDTABLE)]
local new_merch = ScenEdit_AddUnit({side='Merchant', type='Ship',name='Merchant #'..i, dbid=DBID,latitude=v_lat,longitude=v_lon})
local fuel = new_merch.fuel
fuel[3001].current = fuel[3001].max*math.random(600, 800)/1000
new_merch.fuel = fuel
ScenEdit_AssignUnitToMission( new_merch.name, 'VLADIVOSTOK')
print (new_merch.name..' with dbid '..DBID..' was created in water with a depth of '..elevation..'m')
end

There’s a lot going on here, so we’ll look at the bare essentials. First the really really important bits. You need a side called Merchant and a mission called Vladivostok. You can alter the above script to fit your side and mission names, but you need those two things.

1
merch_num = math.random(20,30) --change to your specified minimum and maximum number of merchants

In this line you define the upper and lower limit of how many ships you’d like placed.

1
2
v_lat = math.random(0,2) + (lat_var/(10^13)) --change 27 and 32 to your specified minimum and maximum latitude values; it's important that the first number is smaller than the second.
v_lon = math.random(102,104) + (lon_var/(10^13)) --change 119 and 127 to your specified minimum and maximum longitude values; it's important that the first number is smaller than the second.

This spot requires a bit of planning. Find the area where you’d like to place the ships and record the boundaries of the latitude and longitude, but just the whole number. In our case 0 and 2 and 102 and 104. My area happens to be around the Strait of Malacca. This is our zone for ship placement. The line above this sets our variance for the rest of the lat-lon.

1
if elevation > -10 then --Checks to see if the water is deep enough, adjust as you please

This one is really cool. What if it tries to place a ship on land? It get’s a redo! This exits the current loop and tries all over again. If it hits too many failures, say you tried to place a fleet in Siberia, then it’ll give you a nifty error.

1
DBIDTABLE = { 775, 2027, 2029, 2028, 2030, 774, 2026, 2031, 2775, 2023, 773, 2774, 1001, 1374, 2773, 2776, 1006, 222, 1599, 2034, 1002, 1317, 144, 339, 275, 145, 2022, 259 } --list of DBIDs

This is a list of database entry ID’s that the list will pull from. If you wanted random fishing boats you could change up the list. Or maybe change this entirely and have it be biologics like whales?

1
local new_merch = ScenEdit_AddUnit({side='Merchant', type='Ship',name='Merchant #'..i, dbid=DBID,latitude=v_lat,longitude=v_lon})

This script adds our new vessel and assigns it a numerical name. This is useful if we want to iterate later and assign things to it (like the boundary zone from the previous tutorial).

1
ScenEdit_AssignUnitToMission( new_merch.name, 'VLADIVOSTOK')

And finally we assign that unit to the mission VLADIVOSTOK. This is where you’d change the name for something completely different depending on your mission.

When I used this script I created a few different missions. One for moving west, one for moving east, and one for moving to port. Then I ran the same script three times. Now I’ve got a mish-mash of civilian freighter traffic moving all through my area of operations.

So give it a try, add some civilian traffic, add some weather, make it feel real.

Gamer, Author, Engineer, Dad.

Leave a Reply

Next ArticleCommand Modern Air Naval Operations : Lua Repository #5 Conditions