Random Location Generator (Full Version)

All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion



Message


vettim89 -> Random Location Generator (6/20/2021 3:34:39 AM)

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




KnightHawk75 -> RE: Random Location Generator (6/20/2021 6:03:01 AM)

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.




vettim89 -> RE: Random Location Generator (6/20/2021 7:48:28 PM)

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'




tmoilanen -> RE: Random Location Generator (6/20/2021 8:12:42 PM)

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




KnightHawk75 -> RE: Random Location Generator (6/20/2021 10:41:34 PM)

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.




vettim89 -> RE: Random Location Generator (6/22/2021 2:17:00 AM)

wrong thread




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.703125