Jump to content

Randomizing object placement at beginning of mission


Recommended Posts

Is it possible to create, for example, five different placements of the same two groups of objects (armor and SAMs) and have the ME pick one placement of the three on the basis of, say, a 30% probability, every time the mission starts? I don't want to radically alter the location of both groups, just the dispositions, i.e. the formation of the vehicles.

 

I'm sure it's possible but would appreciate the advice of ME experts, as my solution is bound to be jury-rigged and flawed.

 

Thanks!

Link to comment
Share on other sites

Yes, but it involves the use of ACTIVATE GROUP which causes pauses unless of course you intend to do it at MISSION START.

 

You will need to place 10 groups of objects on the map with a day delay on start and then by a process of randomisation selection ACTIVATE only two.

 

You can use my lua script here

-- Change the values in the 2 variables lowest_flag and highest_flag 
--    to the flag values you wish to use in your mission

local lowest_flag = 100
local highest_flag = 105

---------------------- DO NOT EDIT BELOW THIS LINE ------------

local total_flags = 1 + (highest_flag - lowest_flag)
local flag_table = {}

for x = 1, total_flags do
flag_table[x] = lowest_flag + (x-1)
end

function randomiseTable(tableName)
 local temp_table = {}
 local index = -1
 math.randomseed( os.time() )
 while #tableName > 0 do
   index = math.random(1,#tableName)
   table.insert(temp_table,tableName[index])
   table.remove(tableName,index)
 end
 return temp_table
end

function randomiseGpActivate(rgroup)
local spawnrandom = 0    
   math.randomseed( os.time() )
   math.random()
   spawnrandom = math.random(1,5)  
   return(spawnrandom)
end

flag_table = randomiseTable(flag_table)
local rgroupindex = randomiseGpActivate(rgroup)
local rgroup = flag_table[rgroupindex]
trigger.setUserFlag(rgroup,true)

 

Then in ME

ONCE flag 100 set activate group aa activate group bb

ONCE flag 101 set activate group cc activate group dd

.

.

ONCE flag 105 set activate group kk activate group LL

 

If you want more than 5 groups or different flags set then change the values in the variables lowest_flag & highest_flag e.g. (300,310) will set a flag between 300 and 310.


Edited by Druid_
changes lua code (not random enough)
  • Like 1

i7-7700K : 16Gb DDR4 2800 Mhz : Asus Mobo : 2TB HDD : Intel 520 SSD 240gb : RTX 2080ti: Win10 64pro : Dx10 : TrackiR4 : TM Warthog : ASUS ROG SWIFT PG348Q

Link to comment
Share on other sites

This is great, Druid, many thanks.

 

Bear with me but can you confirm where that .lua script goes? I downloaded "random task table lua.miz" from the thread you linked and looked at it in the ME but I do not know how to see how the scripting works. I read the thread carefully but I have no programming experience and do not know how to manipulate lua scripts, or how to copy/paste and/or where to put the one you kindly supplied above. I gather you can open .lua files in Notepad but would appreciate confirmation and some sort of guidance re. where I place the script above to make it work in my .miz.

 

I apologise for asking you to step me through it but I'm a relatively quick study (for a logically-challenged person...)

 

EDIT: OK, looking at that example, I see now that two lines of code to initialize a script have been placed in two triggers before the main sequence of randomization triggers in the ME. Still not sure where the script goes that is referenced in the two lines mentioned above.

 

EDIT 2: I suspect I need to copy and paste the text above to a named .lua file in Notepad and then initialise it in the mission with a line of code, maybe the same ones as in the sample miz? I may be completely wrong about that, though.


Edited by Bahger
Link to comment
Share on other sites

The code is placed in the triggered actions (icon next to waypoint) of a unit.

 

The code is run by putting

ONCE / RANDOM(100) / AI TASK "scriptname" in your trigger list.

 

Here is a .miz with exactly what you want (lua_man) has the code, check it out.

 

-- In the lua you can change the values in the 2 variables ,

lowest_flag and highest_flag to the flag values you wish to use in your mission

e.g.

local lowest_flag = 300

local highest_flag = 310

 

will randomly set ONE flag in the range 300 to 310 to TRUE

random activate groups lua.miz


Edited by Druid_

i7-7700K : 16Gb DDR4 2800 Mhz : Asus Mobo : 2TB HDD : Intel 520 SSD 240gb : RTX 2080ti: Win10 64pro : Dx10 : TrackiR4 : TM Warthog : ASUS ROG SWIFT PG348Q

Link to comment
Share on other sites

This is great, Druid, thanks again. One more, probably dopey question: Does the ME allow any form of copying and pasting script texts? If not, do you input all of this code manually?? Everything is done in the ME, as opposed to DCS .lua files, right?

 

OK, I'm being dense here, apologies. The obvious MO is to build my .miz around the unit/triggers you kindly supplied, adapting and changing unit compositions but leaving the scripts as is. And in building any future missions requiring this code, I'll just start with the template you supplied and rename it. Duh.

 

Again, sorry for the beyond-noob questions and thanks for your patience. One I get it down, it'll stay down.


Edited by Bahger
Link to comment
Share on other sites

This is great, Druid, thanks again. One more, probably dopey question: Does the ME allow any form of copying and pasting script texts? If not, do you input all of this code manually?? Everything is done in the ME, as opposed to DCS .lua files, right?

ALL done in ME (except I write the code in Notepad++ then copy and paste it into the ME). You can copy [CTRL C] and paste [CTRL V] the lua directly in the ME. Just highlight the script and copy & paste to your hearts desire. You can just create any unit (I usually use an infantryman and call him lua_man) then click on the units (e.g. lua_man) triggered actions icon (next to waypoint icon), then

 

ADD --> PERFORM COMMAND --> RUN SCRIPT. In the box that appears enter or paste the desired lua code.

 

To execute the code you click on

Set Rules for Trigger Icon --> add a new trigger

TYPE - CONDITION - ACTION where 'ACTION' must be AI TASK & select unitname / 1. Run Script "scriptname" from drop down box.

e.g. ONCE - RANDOM(100) - AI TASK (lua_man / 1. Run Script "Init_random_activate_group"

 

OK, I'm being dense here, apologies. The obvious MO is to build my .miz around the unit/triggers you kindly supplied, adapting and changing unit compositions but leaving the scripts as is. And in building any future missions requiring this code, I'll just start with the template you supplied and rename it. .

Sure, thats one way of doing it, but experiment and practice what I have said above, maybe even try creating a copy of the given .miz file. You should then just be able to easily add it to any existing missions.

 

Also I have set two variables you can change, so go in and change them to values you prefer, other than 1 to 5 as I have set them.

 

One thing, place the unit (lua_man or whatever you call him) that contains the lua script in a far corner well away from any action (or make him invisible/indestructable). I didn't in my .miz file as I wanted you to find him. You don't want him to get killed if any lua scripts are meant to run other than at mission start.


Edited by Druid_

i7-7700K : 16Gb DDR4 2800 Mhz : Asus Mobo : 2TB HDD : Intel 520 SSD 240gb : RTX 2080ti: Win10 64pro : Dx10 : TrackiR4 : TM Warthog : ASUS ROG SWIFT PG348Q

Link to comment
Share on other sites

Druid, I have implemented all of this successfully and it works beautifully in my mission. What's more, it's a very valuable tool for replayability in fututre missions. Please check your PMs and thanks very much indeed for your help and patience. I got it in the end.

Link to comment
Share on other sites

This is great stuff!!

 

Yes, it is. Lua scripting is a mysterious art to me, but thanks to Druid, I now know enough about how it's implemented in this sim to be able to add tremendous replayability to my missions in the form of randomization. It's obvious from this thread that randomisation via script is a lot more versatile than via the ME interface.

Link to comment
Share on other sites

I am in the process of trying to learn Lua and scripting as well. I have built several missions for DCS:Black Shark, including one multi-player campaign. In doing that, I did try and incorporate "random" elements and it was a PAIN to do with triggers and RANDOM alone (probably because of my lack of understanding). Given that I was working on a script to do that, when I first saw this thread a day or two ago. Below is the results of my first attempt. It isn't as elegant as Druids but it does work.

 

Both of the scripts listed below can be employed as described by Druid.

 

-------------------------------------
--  The version below is a version that will generate only one set of flags
--  Flag_Start - set this to one value below where you want the flag numbers to start
--        For example to have the first flag start at 701 set Flag_Start = 700
--  Flags_Total - Set this to the total possible number of random flags.
--        For example if you want a total of 8 different possibilities to choose from
--        set Flags_Total = 8
--  To use this version, remove the "--" from the first of each line
--
--
-------------------------------------
math.randomseed(os.time())
RandNum = math.random()
Flag_Start = 700
Flags_Total = 8
trigger.setUserFlag(((Flag_Start + (math.ceil((RandNum/(1/Flags_Total)))))), true)
--
--------------------------------------

 

Also when developing the Black Shark mission, I had some cases were I need some flags with a 25% of occurring, some with a 12.5% of occurring and some with a 6.25% of occurring. One such case was in setting up the campaign flow, which mission would be called next based on how you did in the current mission. Anyway to do that I developed an expanded version that sets up different groups of flags ranging from a group of two flags to 16 flags. The code for that is listed below.

 

-------------------------------------
--
--  Simple script to set some flags randomly.
--  Tit is setup such that a set range of flags represents a different set
--  based on different probabilities.
--  For example, if you want to setup a mission with four different random units, you 
--  would need four flags, each with an equal probability of occurrence.
--  In the case here, Flags 741 - 744 will represent this case.
--  Basic Flag sets are
--
--  721 - 722  - 2 flags
--  731 - 733  - 3 flags
--  741 - 744  - 4 flags
--  751 - 755  - 5 flags
--  761 - 766  - 6 flags
--  771 - 777  - 7 flags
--  781 - 788  - 8 flags
--  791 - 799  - 9 flags
--  801 - 810  - 10 flags
--  811 - 821  - 11 flags
--  831 - 843  - 12 flags
--  851 - 862  - 13 flags
--  871 - 884  - 14 flags
--  891 - 905  - 15 flags
--  911 - 925  - 16 flags
--
--  AKA_Clutter April 19, 2011
--
-------------------------------------
math.randomseed(os.time())
RandNum = math.random()
for  i=2,10 
  trigger.setUserFlag(((700 + i*10 + (math.ceil((RandNum/(1/i)))))). true)
end
BaseN = 810
for i=1,6 
  trigger.setUserFlag(( BaseN + (math.ceil((RandNum/(1/(10 + i)))))), true)
  BaseN = BaseN + 20
end
--
-------------------------------------

 

These are my first attempt at scripting and programing in Lau. Comments, feedback and pointers are welcome!

 

Druid_ - I hope to use your idea of a table setup in doing multiple group and timing randomization once I learn more about the base code and the _G table.

 

:book:


Edited by AKA_Clutter

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

AKA_Clutter

 

Win 10 Pro, Intel i7 8700k @4.6 GHz, EVGA RTX 3080  FTW, Ultra 64 GB G.Skill DDR4 3600 RAM, Acer 27" flat screen, Oculus Rift S, HP Reverb G2, CH Fighterstick, Pro Throttle and Pro Rudder Pedals HOTAS, TM Warthog HOTAS, MFG Rudder Pedals, TrackIR 5 Pro w/Vector Expansion, PointCTRL.

Link to comment
Share on other sites

Druid_ - I hope to use your idea of a table setup in doing multiple group and timing randomization once I learn more about the base code and the _G table.

:book:

Nice to see more people delving into lua. nice code clutter. The reason I used a random table followed by a random pick from the table was because the first value returned by math.random (after os seeding) isn't really random.

 

e.g.

math.randomseed( os.time() )
x = math.random(1,100)
print (x)

If you run the code above for 1 minute, it will return the same number each time. Possibly because (os.time) doesnt seed milliseconds.

 

math.randomseed( os.time() )
math.random(); math.random(); math.random();
x = math.random(1,100)
print (x)

A slightly better way above will return a different random number as long you don't run it within 2 secs.

 

Obviously this doesn't matter if you are only going to generate a random list/table etc once at mission start and are not going to call the code several times in succession during the mission.

  • Like 1

i7-7700K : 16Gb DDR4 2800 Mhz : Asus Mobo : 2TB HDD : Intel 520 SSD 240gb : RTX 2080ti: Win10 64pro : Dx10 : TrackiR4 : TM Warthog : ASUS ROG SWIFT PG348Q

Link to comment
Share on other sites

im going an other way!

 

Lets call it the "WC-Explode-Trigger" - it is simple and effectual!

 

I placed 3 Structure "WC" anywhere on the map and triggered it on Missionstart as Random 50% to explode.

 

After that i triggered the 8 options (WC1alive-WC2dead-WC3alive, WC1alive-WC2alive-WC-3alive, and so on......) to 8 different possibilities of the Mission - and thats all!:thumbup:


Edited by WRFirefox
Link to comment
Share on other sites

Nice to see more people delving into lua. nice code clutter. The reason I used a random table followed by a random pick from the table was because the first value returned by math.random (after os seeding) isn't really random.

 

e.g.

math.randomseed( os.time() )
x = math.random(1,100)
print (x)

If you run the code above for 1 minute, it will return the same number each time. Possibly because (os.time) doesnt seed milliseconds.

 

math.randomseed( os.time() )
math.random(); math.random(); math.random();
x = math.random(1,100)
print (x)

A slightly better way above will return a different random number as long you don't run it within 2 secs.

 

Obviously this doesn't matter if you are only going to generate a random list/table etc once at mission start and are not going to call the code several times in succession during the mission.

 

Yea, I also had some issues with math.random(). Hell, I had the same problems with the C++ random function IIRC back in my programming classes. I couldn't get a decent random seed. Everyone tries os.time() or its equivalent and it never yields random results. I actually stopped using a random seed entirely... and as far as I can tell, the results appear random? :huh: I need some more testing to find out for sure though.... I'm probably mistaken, to be honest.

 

Try this out:

 

print(tostring(os.time()))

And see what is says...

 

Edit: I believe that print() automatically does a tostring(), but whatever... it can't hurt to use it anyway.

 

Anyway, there are some supposed solutions on the web for better random seeds, but maybe we can figure something out.

 

Great script by the way, there. I may have to use it, or at least, draw some ideas from it, for my next mission, which I plan to design from the ground-up as being very randomized. My current mission I'm finishing up wasn't and isn't (and couldn't really benefit from it either).


Edited by Speed

Intelligent discourse can only begin with the honest admission of your own fallibility.

Member of the Virtual Tactical Air Group: http://vtacticalairgroup.com/

Lua scripts and mods:

MIssion Scripting Tools (Mist): http://forums.eagle.ru/showthread.php?t=98616

Slmod version 7.0 for DCS: World: http://forums.eagle.ru/showthread.php?t=80979

Now includes remote server administration tools for kicking, banning, loading missions, etc.

Link to comment
Share on other sites

Yep that works for Nine, gonna be a lot of triggers though if its 16 or more!

 

This lua method is quicker to setup (once you've done it a couple of times admittedly) and very easy to add extra random flgs if you decide to change the mission slightly once you start editing.

  • Like 1

i7-7700K : 16Gb DDR4 2800 Mhz : Asus Mobo : 2TB HDD : Intel 520 SSD 240gb : RTX 2080ti: Win10 64pro : Dx10 : TrackiR4 : TM Warthog : ASUS ROG SWIFT PG348Q

Link to comment
Share on other sites

The reason I used a random table followed by a random pick from the table was because the first value returned by math.random (after os seeding) isn't really random.

 

......Obviously this doesn't matter if you are only going to generate a random list/table etc once at mission start and are not going to call the code several times in succession during the mission.

 

Yea, I read in the manual that it was pseudo-random, but wasn't sure of the limitations. Would it be anymore random to use os.time as the seed and then use that random number as the next seed? (or is that what the second piece of code suggest).

 

Looking forward to learning more. :)

 

Thanks for the feedback. :thumbup:

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

AKA_Clutter

 

Win 10 Pro, Intel i7 8700k @4.6 GHz, EVGA RTX 3080  FTW, Ultra 64 GB G.Skill DDR4 3600 RAM, Acer 27" flat screen, Oculus Rift S, HP Reverb G2, CH Fighterstick, Pro Throttle and Pro Rudder Pedals HOTAS, TM Warthog HOTAS, MFG Rudder Pedals, TrackIR 5 Pro w/Vector Expansion, PointCTRL.

Link to comment
Share on other sites

Druid,

 

First off, thanks so much for putting this out there. It's brilliant and I very much appreciate it.

 

I've got a msn where you have to find 3 tanks in a city. There are 9 possible tanks (9 separate groups...1 unit per group). To get your script to work in my situation i've had to make 3 "lua_men" and split the flags it looks for into 3 groups (1-3, 4-6, 7-9). It's great, but I'd like the full variability of randomly selecting 3 out of the entire 9 as opposed to 1 out of each group of 3.

 

Is there a way to change your random script to return 3 flags out a group true as opposed to just 1?


Edited by johnv2pt0
Added msn file
Link to comment
Share on other sites

Here's how to do it with triggers without lua code. This example has 4 different possibilities but I think the idea is pretty easy to follow and modify for different number of random possibilities.

 

MISSION START  RANDOM(100%)  FLAG SET RANDOM VALUE (flag1, minval 0, maxval 3)
MISSION START  FLAG IS LESS (flag1, less than 1)  SET FLAG (flag 2)
MISSION START  FLAG IS MORE (flag1, more than 0) & FLAG IS LESS (flag1, less than 2)  SET FLAG (flag3)
MISSION START  FLAG IS MORE (flag1, more than 1) & FLAG IS LESS (flag1, less than 3)  SET FLAG (flag4)
MISSION START  FLAG IS MORE (flag1, more than 2) & FLAG IS LESS (flag1, less than 4)  SET FLAG (flag5)
MISSION START  FLAG IS TRUE (flag2)  ACTIVATE GROUP (killer tanks1)
etc.

What advantages would the lua code version have over this?

DCS Finland: Suomalainen DCS yhteisö -- Finnish DCS community

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

SF Squadron

Link to comment
Share on other sites

im going an other way!

 

Lets call it the "WC-Explode-Trigger" - it is simple and effectual!

 

I placed 3 Structure "WC" anywhere on the map and triggered it on Missionstart as Random 50% to explode.

 

After that i triggered the 8 options (WC1alive-WC2dead-WC3alive, WC1alive-WC2alive-WC-3alive, and so on......) to 8 different possibilities of the Mission - and thats all!:thumbup:

 

It is the same that im doing - and it works fine - more tedious? no - hmmm i took less then 4 minutes and i don´t have to handle the LUA-scripting...it is simply easy to do....:smilewink:


Edited by WRFirefox
Link to comment
Share on other sites

  • 7 months later...

Another possibility (If not already mentioned):

 

 

 

 

misssion start >>> random 50%>>> flag on 1

misssion start >>> random 50%>>> flag on 2

 

 

once >>>>>>>> Flag true 1, Flag true 2 >>> activate group A

 

 

once >>>>>>>> Flag false 1, Flag false 2 >>> activate group B

 

 

once >>>>>>>> Flag true 1, Flag false 2 >>> activate group C

 

 

once >>>>>>>> Flag false 1, Flag true 2 >>> activate group D

 

 

:)

Link to comment
Share on other sites

  • 1 month later...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...