Page 1 of 1
Random Location Generator
Posted: Sun Jun 20, 2021 12:34 am
by vettim89
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
RE: Random Location Generator
Posted: Sun Jun 20, 2021 3:03 am
by KnightHawk75
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:
Code: Select all
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.
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.
RE: Random Location Generator
Posted: Sun Jun 20, 2021 4:48 pm
by vettim89
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'
RE: Random Location Generator
Posted: Sun Jun 20, 2021 5:12 pm
by tmoilanen
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
RE: Random Location Generator
Posted: Sun Jun 20, 2021 7:41 pm
by KnightHawk75
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.
RE: Random Location Generator
Posted: Mon Jun 21, 2021 11:17 pm
by vettim89
wrong thread