Matrix Games Forums

Forums  Register  Login  Photo Gallery  Member List  Search  Calendars  FAQ 

My Profile  Inbox  Address Book  My Subscription  My Forums  Log Out

Lua - Random Whale Generation Script

 
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 >> Mods and Scenarios >> Lua - Random Whale Generation Script Page: [1]
Login
Message << Older Topic   Newer Topic >>
Lua - Random Whale Generation Script - 1/31/2016 10:23:06 AM   
Rory Noonan

 

Posts: 2816
Joined: 12/18/2014
From: Brooklyn, NY
Status: offline
I've been doing a little work on Lua scripting. I've been able to do the odd bit here and there, but it has been clunky and time consuming.

Below is the code for a script to generate a random number of whales in an area chosen by the user. All you need is a scenario with a side named 'Nature' and a mission (I use sea-control) called 'Whale Patrol'.

Previously I'd done this by copying and pasting the same entry over and over, manually changing the relevant values. It worked, but it was laborious.

After about an hour of working through the 'Quick guide to Lua' included with Lua for Windows I was able to refine this into a much more elegant, powerful script that makes it just as easy to add 1,000 whales as it is to add 1.

Obviously this is a simple script, but I wanted to post it to show others how powerful Lua is and how simple it is to learn. I had zero knowledge of Lua before 1.06 and no experience programming at all.

Below is the script, fully commented and ready to paste into the game. Instructions are included.

<<START>>

--Whale generation script v1.0 by Apache85

--[[Generates a random number of whales and attaches them to a mission
Instructions:
1. Open scenario, create sides
2. Copy any paste this script into a text editor
3. Edit any values to match your needs; i.e. latitudes/longitudes
4. In script console (or event editor if using a lua event) copy and paste the script and click 'run script'
5. Without any modification, this will produce between 5-15 whales on side 'Nature' around the North of the Falklands, and assign them to a mission called 'Whale Patrol'.

YOU NEED TO CREATE THE SIDE AND MISSION FOR THIS TO WORK CORRECTLY]]


math.randomseed(os.time()) --Resets the random seed to ensure actual randomness, not the same results over and over

--Decimal latitude / longitude values for NE and SW corner. Be careful to LEAVE OUT decimals and ensure that the xx0000000000001 / xx9999999999999 format is used
NW_CornerLat = -460000000000001
SE_CornerLat = -509999999999999
NW_CornerLon = -630000000000001
SE_CornerLon = -539999999999999

TargetSide = "Nature" --Change string value to match biological side name
TargetMission = "Whale Patrol" --Change string value to match desired mission

a=0 --counter initialised at zero
TargetNumber=math.random(5,15) --change parameters to increase min and max number of whales

repeat
count=a+1 --counter step up increment (+1 here)
a=count --resets counter to new value
SeqName=("Whale " .. count) --Sequentially numbered name for created whale
ScenEdit_AddUnit({
type = "Submarine",
side = TargetSide,
name = SeqName,
dbid = "92", --dbid for 'whale' in db3000
latitude = (math.random(SE_CornerLat,NW_CornerLat) / 1e13), --produces a random number between the SE corner latitude and the NW corner latitude
longitude = (math.random(NW_CornerLon,SE_CornerLon) / 1e13), --produces a random number between the SE corner longitude and the NW corner longitude
})
ScenEdit_AssignUnitToMission(
SeqName,
'Whale Patrol'
)
until count == TargetNumber --Finishes loop when 'count' reaches 'TargetNumber'


<<END>>

I tried using the 'code' tags but it just ran off the screen... Is there a better way to post code on the forum?

Anyway, hopefully this is useful to someone or inspires them to learn a little Lua!

< Message edited by apache85 -- 1/31/2016 11:27:12 AM >
Post #: 1
RE: Lua - Random Whale Generation Script - 1/31/2016 11:31:16 AM   
Skjold

 

Posts: 240
Joined: 9/29/2015
Status: offline
Thanks for the effort man!

(in reply to Rory Noonan)
Post #: 2
RE: Lua - Random Whale Generation Script - 2/3/2016 10:04:08 AM   
mikkey


Posts: 3142
Joined: 2/10/2008
From: Slovakia
Status: offline
Excellent work, thanks for sharing!

(in reply to Skjold)
Post #: 3
RE: Lua - Random Whale Generation Script - 2/3/2016 11:28:07 AM   
Rory Noonan

 

Posts: 2816
Joined: 12/18/2014
From: Brooklyn, NY
Status: offline
You're welcome. Every time I look at it I see something I can improve haha.

(in reply to mikkey)
Post #: 4
RE: Lua - Random Whale Generation Script - 2/3/2016 3:21:55 PM   
AlGrant


Posts: 912
Joined: 8/18/2015
Status: offline

This looks great - I gave it a quick try but think I must be doing something wrong as every time I run it int he script console it returns: ERROR: [string "chunk"]:1: unexpected symbol near '<'

I have no idea about Lua (or any other) scripting so probably something I'm doing wrong.




(in reply to Rory Noonan)
Post #: 5
RE: Lua - Random Whale Generation Script - 2/3/2016 10:41:57 PM   
Rory Noonan

 

Posts: 2816
Joined: 12/18/2014
From: Brooklyn, NY
Status: offline
Oh... I think I know what that is. As mentioned above there was a bit of a problem using the 'code' function on the forum, so I added <<Start>> and <<end>> tags to help identify where the code begins and ends.

Thinking about it, it is possible that copying and pasting from the forum could introduce problems with the comments as well. To avoid any further problems, I've attached the script in .txt format (inside the .zip file, obviously).

Attachment (1)

(in reply to AlGrant)
Post #: 6
RE: Lua - Random Whale Generation Script - 2/4/2016 1:35:46 AM   
i224747

 

Posts: 89
Joined: 11/6/2015
Status: offline
Created 260 whales in an area of 385864 km^2.

- Created contacts should be named as "Contact###" and not as "Whale###"
- Created whales should have been different speeds, altitudes and headings.
Not sure if this possible with LUA.






(in reply to Rory Noonan)
Post #: 7
RE: Lua - Random Whale Generation Script - 2/4/2016 2:53:06 AM   
Rory Noonan

 

Posts: 2816
Joined: 12/18/2014
From: Brooklyn, NY
Status: offline
There are instructions on how to change the naming of whatever you wish to generate in the comments in the script. Generally since they won't be on the player side, the name is pretty much irrelevant because it will come up as 'GOBLIN xx' until identified. If you attack it with end-game calcs turned on you can see the name, but that's not really my problem or concern (you can also change the name if you wish, as I already mentioned).

As for different speeds, altitudes and headings, this will happen naturally with the assignment to a sea control mission.

As I mentioned earlier, it's a script to demonstrate the power of Lua and the ease of learning it.

(in reply to i224747)
Post #: 8
RE: Lua - Random Whale Generation Script - 2/4/2016 11:32:48 AM   
AlGrant


Posts: 912
Joined: 8/18/2015
Status: offline

apache85,

Thanks for that, I've got it working now and going to take a closer look at Lua.

quote:

ORIGINAL: apache85

There are instructions on how to change the naming of whatever you wish to generate in the comments in the script. Generally since they won't be on the player side, the name is pretty much irrelevant because it will come up as 'GOBLIN xx' until identified. If you attack it with end-game calcs turned on you can see the name, but that's not really my problem or concern (you can also change the name if you wish, as I already mentioned).

As for different speeds, altitudes and headings, this will happen naturally with the assignment to a sea control mission.

As I mentioned earlier, it's a script to demonstrate the power of Lua and the ease of learning it.


The script is quite easy to adjust (even for me! ).
I tweaked some of the values and used it to create a random number of fishing vessels off the Irish coast.
The ships were added to a 'Fishing Grounds' Sea Control mission. The units were named 'MFV' 1,2,3 etc and all seems to be working just fine. The fishing boats all set off on various courses once the scenario starts.

As for the speeds ..... all the units have the same speed (5kts), this changes depending on the Station throttle speeds set in the Mission Editor. If set to Cruise then they all had 10kts.



(in reply to Rory Noonan)
Post #: 9
RE: Lua - Random Whale Generation Script - 2/4/2016 5:49:45 PM   
i224747

 

Posts: 89
Joined: 11/6/2015
Status: offline

quote:

ORIGINAL: AlGrant

As for the speeds ..... all the units have the same speed (5kts), this changes depending on the Station throttle speeds set in the Mission Editor. If set to Cruise then they all had 10kts.


Quite realistic...

Dont tell me you cannot give AI controlled units a specific speed (for example 3 knots) in this game? Really? OMG.

(in reply to AlGrant)
Post #: 10
RE: Lua - Random Whale Generation Script - 2/4/2016 11:07:43 PM   
Rory Noonan

 

Posts: 2816
Joined: 12/18/2014
From: Brooklyn, NY
Status: offline

quote:

ORIGINAL: AlGrant


apache85,

Thanks for that, I've got it working now and going to take a closer look at Lua.

quote:

ORIGINAL: apache85

There are instructions on how to change the naming of whatever you wish to generate in the comments in the script. Generally since they won't be on the player side, the name is pretty much irrelevant because it will come up as 'GOBLIN xx' until identified. If you attack it with end-game calcs turned on you can see the name, but that's not really my problem or concern (you can also change the name if you wish, as I already mentioned).

As for different speeds, altitudes and headings, this will happen naturally with the assignment to a sea control mission.

As I mentioned earlier, it's a script to demonstrate the power of Lua and the ease of learning it.


The script is quite easy to adjust (even for me! ).
I tweaked some of the values and used it to create a random number of fishing vessels off the Irish coast.
The ships were added to a 'Fishing Grounds' Sea Control mission. The units were named 'MFV' 1,2,3 etc and all seems to be working just fine. The fishing boats all set off on various courses once the scenario starts.

As for the speeds ..... all the units have the same speed (5kts), this changes depending on the Station throttle speeds set in the Mission Editor. If set to Cruise then they all had 10kts.





Glad you're finding it useful!

If varied speeds are important you could have multiple instances of the script with differing unit types and mission settings. The trade-off is it's more work. Certainly achievable though, and quite trivial to do.

In my original use of the script the idea is to generate whales off the Falklands for a scenario where there is a confirmed diesel sub threat. The whales on a sea control mission with speed setting to 'flank' move at 4kts, the same as the threat sub so it works perfectly.

The script also works quite well with false contacts, and as their DbIDs are sequential you can even randomise between small, medium and large with one iteration of the script.

(in reply to AlGrant)
Post #: 11
RE: Lua - Random Whale Generation Script - 7/8/2018 12:56:14 AM   
Whicker

 

Posts: 664
Joined: 6/20/2018
Status: offline
I'm trying to add a random depth to the random fish/whale/bio script and no matter what I do I can't get a depth of anything other than -131.

local new_bio = ScenEdit_AddUnit({side='Biologics', type='Submarine',name='Biologic #'..i, dbid=DBID,latitude=v_lat,longitude=v_lon, altitude='-200'})

no matter what I put in for the altitude it is always -131. Works the same with actual subs as bios.

I've tried alt instead of altitude, '-200 m', '-200 ft' etc and no matter what it is always -131.

Any idea why? can you show me a script to create a sub/fish where you can set the depth?

(in reply to Rory Noonan)
Post #: 12
RE: Lua - Random Whale Generation Script - 7/8/2018 2:16:48 AM   
michaelm75au


Posts: 13500
Joined: 5/5/2001
From: Melbourne, Australia
Status: offline
Use depth=100. That should put is at -100M

_____________________________

Michael

(in reply to Whicker)
Post #: 13
RE: Lua - Random Whale Generation Script - 7/8/2018 2:27:19 AM   
michaelm75au


Posts: 13500
Joined: 5/5/2001
From: Melbourne, Australia
Status: offline
Actually it looks like you may have uncovered a defect; if you use FT or M in the depth, it doesn't treat it as below SL.

_____________________________

Michael

(in reply to michaelm75au)
Post #: 14
RE: Lua - Random Whale Generation Script - 7/8/2018 4:52:59 AM   
Whicker

 

Posts: 664
Joined: 6/20/2018
Status: offline
great - that works for me. I looked for depth as an attribute but didn't see it.

(in reply to michaelm75au)
Post #: 15
RE: Lua - Random Whale Generation Script - 7/8/2018 5:40:52 AM   
Whicker

 

Posts: 664
Joined: 6/20/2018
Status: offline
updated code for random bios.

- random speed between 0 and 4
- random depth (max 270m or so)

I still don't like the way lat and lng are set, kind of a pain. Also it needs a large area - more than one lat/lng integer(?). That can be fixed by changing the random chunk added to it, would be nice if the area could be set with ref points and then add a better way to randomize it. Good enough for now though. I also set the area in the script to larger than what the mission zone is, makes them converge on it for the ones that spawn outside of it.

If you watch the fish depth at high compression it varies -seems to always come up to -131 for a bit and go back down to what it is set to by the script - that actually seems good, I wonder if that is on purpose? they don't seem to do this if I create one manually but I just did it once to see.

quote:


math.randomseed(os.time())
bio_num = math.random(35,55) --change to your specified minimum and maximum number of bios

for i = 1,bio_num do
redo_count = 0
::redo::
local lat_var = math.random(1,(10^13)) --don't change
local lon_var = math.random(1,(10^13)) --don't change
v_lat = math.random(14,26) + (lat_var/(10^13)) --change the first set (south is negative) to your specified minimum and maximum latitude values; it's important that the first number is smaller than the second.
v_lon = math.random(-78,-60) + (lon_var/(10^13)) --change first set (west is negative!) 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 > -40 then --(meters?) Checks to see if the water is deep enough, adjust as you please

redo_count = redo_count + 1
print("picked bad co-ordinates")
if redo_count >50 then
print ('units were 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 = { 92,355,354 } --list of dbids for bios, could be subs
local actual_depth = elevation --need 2 variables for depth - actual depth at that location and the depth the unit is set to, though this is only for the print to log
if elevation < -300 then elevation = -299 end -- max depth for bios is 300, if actual depth is more than 300m set to 299 so random depth is achievable
local bio_depth = -1*elevation*math.random(100, 900)/1000 --have to reverse the sign, depth is a positive number, multiply by random percent between 10 and 90%
DBID = DBIDTABLE[math.random( 1, #DBIDTABLE)] --choose random dbid for unit
local new_bio = ScenEdit_AddUnit({side='Biologics', type='Submarine',name='Biologic #'..i, dbid=DBID,latitude=v_lat,longitude=v_lon, depth='bio_depth', manualSpeed=math.random(0, 4)})

ScenEdit_AssignUnitToMission( new_bio.name, 'Whales') --add to mission which helps with the course
print (new_bio.name..' with dbid '..DBID..' was created in water with a depth of '..actual_depth..'m, at a depth of ' ..bio_depth )
end


< Message edited by Whicker -- 7/8/2018 5:42:05 AM >

(in reply to Whicker)
Post #: 16
RE: Lua - Random Whale Generation Script - 7/8/2018 12:14:01 PM   
Rory Noonan

 

Posts: 2816
Joined: 12/18/2014
From: Brooklyn, NY
Status: offline
I haven't looked at this in nearly 2 years, so yes there is a lot of room for improvement. Have at it.

_____________________________


(in reply to Whicker)
Post #: 17
Page:   [1]
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua - Random Whale Generation Script 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

3.547