LUA Random date, launch site on the Moon and landing site on Earth.

dgatsoulis

ele2png user
Donator
Joined
Dec 2, 2009
Messages
1,924
Reaction score
340
Points
98
Location
Sparta
I am trying to make a script for a scenario, where a vessel is placed on a random set of coordinates on the Moon, on a random date (within 1/1/2000 to 1/1/2050) and select one of the Earth bases at random as the target base. The idea is that everytime I run the scenario, the vessel is placed on a different starting point on the Moon, on a diffent date within a predetermined time-span and a msg on screen selects one of the Earth bases as the mission's target.

This is what I have so far.

I started with the date first and used this to get a (pseudo)random value
Code:
 mjd = math.random(51544,69807)
The problem was that I always got the same number whenever I run the scenario. I changed it to
Code:
while oapi.get_simtime() < 3 do
  mjd = math.random(51544,69794) 
  proc.skip() 
  if oapi.get_simtime() > 3 then
  end
end
mjd = mjd
which generates random dates for 3 seconds and returns me the date at the end of those 3 seconds. It was ok, but all the dates where integers, so I added this (in red)
Code:
while oapi.get_simtime() < 3 do
  mjd = math.random(51544,69807) [COLOR="red"]+ math.random()[/COLOR]
  proc.skip() 
  if oapi.get_simtime() > 3 then
  end
end
mjd = mjd
which adds a number from 0-1 to the result, giving me a pseudorandom date within the time-span I want. I use oapi.set_simmjd(mjd) to set the date and since the vessel is landed to start with, there seems to be no problem with the vessel state.

For setting the vessel on a random place on the Moon, I haven't found a way to automatically do it, so all I could do was to use a method similar to the above and have it return me random values for the long and lat of the launch site. The vessel is being placed there via scenario editor.
Code:
long = math.random (-180,180)
lat = math.random (-90,90)
I don't mind not having decimals on the coordinates of the launch site as long as it's a different one every time I load the scenario. To avoid having the same problem I had with the date, I added it to the same 3 second cycle.

Code:
while oapi.get_simtime() < 3 do
  mjd = math.random(51544,69807) + math.random()
  long = math.random (-180,180)
  lat = math.random (-90,90)
  proc.skip() 
  if oapi.get_simtime() > 3 then
  end
end
mjd = mjd  
long = long
lat = lat

Same for the target base. In my installation I have 44 Earth bases, so I used math.random(1,44) - in the same cycle - to have the script return me a "Target base #: " number which I select by its place in the list of Target\Spaceports on MapMFD.

Obviously, other than the date, I have to do the rest "by hand" which isn't very good, so here are my questions:

1. Is there a way to automatically place the vessel on a random set of coordinates on the Moon? I haven't found such a function in the Lua API. (There is an oapi.get_equpos() but there isn't an "oapi.set_equpos()")
2. If I "label" numbers 1 through 44 with the names of the Earth bases, how would I be able to get the script return me the name of the base instead of a number?
3. Is there a better way to get the random numbers, without going through that 3 second cycle? -- EDIT: Done, thanks orb.

My goals are these:
1. Have the scenario start on a random date from 1/1/2000 to 1/1/2050 (done)
2. Have the ship placed on a random set of coordinates on the Moon (so far, I can get the coordinates but I have to place the ship "by hand").
3. Have the script print me a "Target base: (name)" on screen. (so far, I get only the "number" of the base).

Thanks in advance.
 
Last edited:

orb

New member
News Reporter
Joined
Oct 30, 2009
Messages
14,020
Reaction score
4
Points
0
Is there a better way to get the random numbers, without going through that 3 second cycle?
Try adding this at the beginning (instead of the 3 seconds delay loop):
Code:
math.randomseed( os.time() )
 

dgatsoulis

ele2png user
Donator
Joined
Dec 2, 2009
Messages
1,924
Reaction score
340
Points
98
Location
Sparta
Try adding this at the beginning (instead of the 3 seconds delay loop):
Code:
math.randomseed( os.time() )

Thanks orb. I had tried it, but it messed up the vessel state. As soon as the scenario started, the vessel was thrown into space. (I start from landed position on the Moon). I added a proc.wait_simdt(0.1) and it works great.

Code:
proc.wait_simdt(0.1)
math.randomseed( os.time() )
mjd = math.random(51544,69807) + math.random()
long = math.random (-180,180)
lat = math.random (-90,90)
tgt = math.random(1,44)

Edit: Post #1 updated
 
Last edited:
Top