Matrix Games Forums

Forums  Register  Login  Photo Gallery  Member List  Search  Calendars  FAQ 

My Profile  Inbox  Address Book  My Subscription  My Forums  Log Out

Random Location Generator

 
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 Legion >> Random Location Generator Page: [1]
Login
Message << Older Topic   Newer Topic >>
Random Location Generator - 6/20/2021 3:34:39 AM   
vettim89


Posts: 3615
Joined: 7/14/2007
From: Toledo, Ohio
Status: offline
So I am trying to generate a function that I can feed a group of submarines into to generate random positioning at the beginning of a scenario. Here is what I wrote. The Lua Console doesn't see an error yet it does not work. The sub stays in place. Here is what I wrote

function RelocateSubs (theside, unit)
math.randomseed(os.time())
math.random();
local rbearing=math.random(1,359)
local rdistance=math.random(25,100)
local newloc=World_GetLocation({latitude=unit.latitude, longitude=unit.longitude, distance=rdistance, bearing=rbearing})
ScenEdit_SetUnit(side='USSR', guid=unit.guid, latitude=newloc[1], longitude=newloc[2]})

RelocateSubs (USSR, 'W7ZXL3-0HM9G95HNBS56'}

I suspect it may be on how I am trying to pull the latitude/longitude out of the table generated by World GetLocation. Also concerned abotu feeding an actual unit into the function. I just used the GUID for the submarine in question

Sure would appreciate the help

_____________________________

"We have met the enemy and they are ours" - Commodore O.H. Perry
Post #: 1
RE: Random Location Generator - 6/20/2021 6:03:01 AM   
KnightHawk75

 

Posts: 1450
Joined: 11/15/2018
Status: offline
Your problem there is primarily that RelocateSubs in theory there takes a unitwrapper as a param, and not a unit guid.
So either need to SE_GetUnit in the function or get it before hand and pass it in. There are other issues with missing ')' and '{''s
You are also calling GetLocation that gets elevation and landtype data etc data I don't think you want there, not yet anyway, I think you mean to call World_GetPointFromBearing

Ie...should look more like this:
function RelocateSub (theside, unitguid)
  local retval,unit = pcall(ScenEdit_GetUnit,{guid=unitguid})
  if (retval ==true) and theunit ~=nil then --if the call returned successfully and unit looks valid.
    math.randomseed(os.time())
    math.random();
    local rbearing=math.random(1,359)
    local rdistance=math.random(25,100)
    local newloc=World_GetPointFromBearing({latitude=unit.latitude, longitude=unit.longitude, DISTANCE=rdistance, BEARING=rbearing})
    if newloc ~nil and type(newloc) == 'table' --is valid return table indicating some form.
      u.latitude = newloc.Latitude  --no need for set unit if already have unitwrapper.
      u.longitude = newloc.Longitude  --no need for set unit if already have unitwrapper.
      --... other stuff if needed... like you aren't checking if this particular location is actually water. ;)
      --... but maybe for your circumstances it's not an issue and don't need the complication
    end
  else
    print("could not obtain unit with guid" .. unitguid
  end
end

local someListOfSubGuids  = {'W7ZXL3-0HM9G95HNBS56'}
For k,v in pairs(someListofSubGuids) do 
  RelocateSubs ("USSR", v)
end


There is code buried somewhere in the forum (edit nm found it) already for random unit location generation from Whicker and I some years back. http://www.matrixgames.com/forums/fb.asp?m=4587348 Should work for your basic needs (and deals with depth checking). But if you need something more advanced ask, as I have another way more advanced one including random pathing toward target generation as well as other stuff (EX zone checking) but sounds like it would be overkill for you needs atm.

< Message edited by KnightHawk75 -- 6/20/2021 6:26:19 AM >

(in reply to vettim89)
Post #: 2
RE: Random Location Generator - 6/20/2021 7:48:28 PM   
vettim89


Posts: 3615
Joined: 7/14/2007
From: Toledo, Ohio
Status: offline
quote:

function RelocateSub (theside, unitguid)
local retval,unit = pcall(ScenEdit_GetUnit,{guid=unitguid})
if (retval ==true) and theunit ~=nil then --if the call returned successfully and unit looks valid.
math.randomseed(os.time())
math.random();
local rbearing=math.random(1,359)
local rdistance=math.random(25,100)
local newloc=World_GetPointFromBearing({latitude=unit.latitude, longitude=unit.longitude, DISTANCE=rdistance, BEARING=rbearing})
if newloc ~nil and type(newloc) == 'table' --is valid return table indicating some form.
u.latitude = newloc.Latitude --no need for set unit if already have unitwrapper.
u.longitude = newloc.Longitude --no need for set unit if already have unitwrapper.
--... other stuff if needed... like you aren't checking if this particular location is actually water. ;)
--... but maybe for your circumstances it's not an issue and don't need the complication
end
else
print("could not obtain unit with guid" .. unitguid
end
end

local someListOfSubGuids = {'W7ZXL3-0HM9G95HNBS56'}
For k,v in pairs(someListofSubGuids) do
RelocateSubs ("USSR", v)
end


You had some minor typos there but I debugged them. However when I run the cal I get this

local someListOfSubGuids = {'W7ZXL3-0HM9G95HNBS56', 'W7ZXL3-0HM9G95HNDET3', 'W7ZXL3-0HM9G95HNBTPI', 'W7ZXL3-0HM9G95HNBQME', 'W7ZXL3-0HM9G95HNDKB4', 'W7ZXL3-0HM9G95HNEEQ2'}
For k,v in pairs(someListofSubGuids) do
RelocateSub ("USSR", v)
end

ERROR: [string "Console"]:21: syntax error near 'k'

_____________________________

"We have met the enemy and they are ours" - Commodore O.H. Perry

(in reply to KnightHawk75)
Post #: 3
RE: Random Location Generator - 6/20/2021 8:12:42 PM   
tmoilanen

 

Posts: 72
Joined: 10/19/2011
Status: offline
Here's is a script that I've used to randomly move existing units to new locations at Startup:


myside = 'USMAJCOM'
myname = {'USS Ohio SSGN-726','USS Oklahoma City SSN-723'}

myunitcnt = #myname
print(myunitcnt)

for i = 1, myunitcnt 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(13,17) + (lat_var/(10^13))
v_lon = math.random(137,145) + (lon_var/(10^13))
elevation = World_GetElevation({latitude=v_lat, longitude=v_lon})
if elevation >= -50 then
redo_count = redo_count +1
if redo_count == 50 then
print ('A ship was not able to find a suitable spot for placement. Re-chck latitude and longitude settings')
break
else
goto redo
end
end

myunitname = myname --..myunitnum
print(myunitname)

ScenEdit_SetUnit({side=myside,name=myunitname,latitude = v_lat,longitude=v_lon})
end

(in reply to vettim89)
Post #: 4
RE: Random Location Generator - 6/20/2021 10:41:34 PM   
KnightHawk75

 

Posts: 1450
Joined: 11/15/2018
Status: offline
quote:

ORIGINAL: vettim89

You had some minor typos there but I debugged them. However when I run the cal I get this

local someListOfSubGuids = {'W7ZXL3-0HM9G95HNBS56', 'W7ZXL3-0HM9G95HNDET3', 'W7ZXL3-0HM9G95HNBTPI', 'W7ZXL3-0HM9G95HNBQME', 'W7ZXL3-0HM9G95HNDKB4', 'W7ZXL3-0HM9G95HNEEQ2'}
For k,v in pairs(someListofSubGuids) do
RelocateSub ("USSR", v)
end

ERROR: [string "Console"]:21: syntax error near 'k'


lower case f in 'for'. I was typing all that off the cuff, sorry about the typos.


< Message edited by KnightHawk75 -- 6/20/2021 10:42:05 PM >

(in reply to vettim89)
Post #: 5
RE: Random Location Generator - 6/22/2021 2:17:00 AM   
vettim89


Posts: 3615
Joined: 7/14/2007
From: Toledo, Ohio
Status: offline
wrong thread

_____________________________

"We have met the enemy and they are ours" - Commodore O.H. Perry

(in reply to KnightHawk75)
Post #: 6
Page:   [1]
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> Random Location Generator 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

0.656