Page 1 of 1

Reusing A Random Location generator = working AHA moment.

Posted: Sat Mar 05, 2022 10:09 pm
by vettim89
So this is not my code. It is a copy of some code that was posted a few months ago on this forum. Trying to generate a random number and types of submarines and then randomly place then within a set area

Location Randomizer function

function random_loc( latmin, latmax, lonmin, longmax, depthm)
local lat_var,lonvar,newlat,newlon;
for i=1,10000 do
lat_var = math.random(1,(10^5))
lon_var = math.random(1,(10^5))
newlat=math.random(latmint, latmax)+(lat_var/10^5)
newlon=math.random(lonmin, lonmax)+(lon_var/10^5)
ndepth=World_GetElevation({latitude=newlat, longitude=newlon})
if ndepth <= depthm then
return newlat,newlon,ndepth;
end
end
return nil, nil, nil
end

Random Sub/location code

math.randomseed(os.time())
math.random();
math.random(); math.random()
local SSNA=math.random(2,4)
local SSKA=math.random(3,6)
local SSGA=math.random(1,2)
local SSNB=math.random(2,4)
local SSKB=math.random(3,6)
local SSGB=math.random(1,2)
local subtype, subran, rname
for i=1,SSNA do
subran=math.random(1,3)
if subran==1 then
subtype='159'
elseif subran==2 then
subtype='58'
else subtype='411'
end
newlat, newlong, ndepth = random_loc( 67, 70, -17, -14, 250)
rname=math.random(1,500)
ScenEdit_AddUnit({side='USSR', type='sub', dbid='subtype', latitude=newlat, longitude=newlong, name='sub'..rname, depth='200'})

end

Getting this error: "ERROR: [string "chunk"]:48: bad argument #1 to 'floor' (number expected, got nil)"

Re: Reusing A Random Location generator = failed

Posted: Mon Mar 07, 2022 7:54 am
by KnightHawk75
Please post or link to the whole thing, for example there is an error but you've not included the lines involved in that error.

If you can wrap the requested code into the post with a forum code tags around it (there is a button for it to the right of ") that way indentation is maintained.

Re: Reusing A Random Location generator = failed

Posted: Mon Mar 07, 2022 10:22 pm
by vettim89
Here is the code posted out of the Lua Console

Code: Select all

>> function random_loc( latmin, latmax, lonmin, longmax, depthm)
local lat_var,lonvar,newlat,newlon;
for i=1,10000 do
  lat_var = math.random(1,(10^5)) 
  lon_var = math.random(1,(10^5))
  newlat=math.random(latmint, latmax)+(lat_var/10^5)
  newlon=math.random(lonmin, lonmax)+(lon_var/10^5)
  ndepth=World_GetElevation({latitude=newlat, longitude=newlon})
  if ndepth <= depthm then
     return newlat,newlon,ndepth;
    end
  end
  return nil, nil, nil
end

math.randomseed(os.time())
math.random();
local SSNA=math.random(2,4)
local SSKA=math.random(3,6)
local SSGA=math.random(1,2)
local SSNB=math.random(2,4)
local SSKB=math.random(3,6)
local SSGB=math.random(1,2)
local sub_type, subran, rname
for i=1,SSNA do
subran=math.random(1,3)
  if subran==1 then
     sub_type='159'
  elseif subran==2 then
     sub_type='58'
  else subtype='411'
  end
  newlat, newlong, ndepth = random_loc( 67, 70, -17, -14, 250)
  rname=math.random(1,500)
  ScenEdit_AddUnit({side='USSR', type='sub', dbid=sub_type, latitude=newlat, longitude=newlong, name='sub'..rname, depth='200'})
  
end






ERROR:  [string "chunk"]:48: bad argument #1 to 'floor' (number expected, got nil)

Re: Reusing A Random Location generator = failed

Posted: Tue Mar 08, 2022 8:39 am
by KnightHawk75
In new location code...

1. "longmax" parameter is refereed to in later code as lonmax (without the g). one or the other needs to be changed. As is lonmax is nil in the call to math.random for new longitude.
2. "latmin" is typo'd as latmint, so that value to math.random was nil.
3. "lon_var" is typod as lonvar in the local definition, so that value was global when used later not local, not that mattered for this error.

Outside the function...
4. subtype is misspelled when used in the 'else' case, this presented a case where 1 out of 3 times you would end up with failure as the dbid would end up nil.

Unrelated but I changed os.time to os.clock as well, code that runs ok with above corrections:

Code: Select all

function random_loc( latmin, latmax, lonmin, lonmax, depthm)
	local lat_var,lon_var,newlat,newlon;
	for i=1,10000 do
		lat_var = math.random(1,(10^5)); 
		lon_var = math.random(1,(10^5));
		newlat=math.random(latmin, latmax)+(lat_var/10^5);
		newlon=math.random(lonmin, lonmax)+(lon_var/10^5);
		ndepth=World_GetElevation({latitude=newlat, longitude=newlon});
		if ndepth <= depthm then
			return newlat,newlon,ndepth;
		end
    end
  return nil, nil, nil
end

math.randomseed(os.clock())
math.random();
local SSNA=math.random(2,4)
local SSKA=math.random(3,6)
local SSGA=math.random(1,2)
local SSNB=math.random(2,4)
local SSKB=math.random(3,6)
local SSGB=math.random(1,2)
local sub_type, subran, rname
for i=1,SSNA do
    subran=math.random(1,3)
    if subran==1 then sub_type='159';
    elseif subran==2 then sub_type='58';
    else sub_type='411';
    end
    newlat, newlong, ndepth = random_loc( 67, 70, -17, -14, 250)
    rname=math.random(1,500)
    ScenEdit_AddUnit({side='a', type='sub', dbid=sub_type, latitude=newlat, longitude=newlong, name='sub'..rname, depth='200'})
end

Re: Reusing A Random Location generator = failed

Posted: Fri Mar 11, 2022 1:54 am
by vettim89
Knighthawk,

Ran the code you supplied and got a new error. BTW, thanks as always for you help.

Code: Select all

>> function random_loc( latmin, latmax, lonmin, lonmax, depthm)
	local lat_var,lon_var,newlat,newlon;
	for i=1,10000 do
		lat_var = math.random(1,(10^5)); 
		lon_var = math.random(1,(10^5));
		newlat=math.random(latmin, latmax)+(lat_var/10^5);
		newlon=math.random(lonmin, lonmax)+(lon_var/10^5);
		ndepth=World_GetElevation({latitude=newlat, longitude=newlon});
		if ndepth <= depthm then
			return newlat,newlon,ndepth;
		end
    end
  return nil, nil, nil
end

math.randomseed(os.clock())
math.random();
local SSNA=math.random(2,4)
local SSKA=math.random(3,6)
local SSGA=math.random(1,2)
local SSNB=math.random(2,4)
local SSKB=math.random(3,6)
local SSGB=math.random(1,2)
local sub_type, subran, rname
for i=1,SSNA do
    subran=math.random(1,3)
    if subran==1 then sub_type='159';
    elseif subran==2 then sub_type='58';
    else sub_type='411';
    end
    newlat, newlong, ndepth = random_loc( 67, 70, -17, -14, 250)
    rname=math.random(1,500)
    ScenEdit_AddUnit({side='a', type='sub', dbid=sub_type, latitude=newlat, longitude=newlong, name='sub'..rname, depth='200'})
end






ERROR: Sequence contains no matching element

Re: Reusing A Random Location generator = failed

Posted: Fri Mar 11, 2022 5:16 am
by KnightHawk75
vettim89 wrote: Fri Mar 11, 2022 1:54 am Knighthawk,

Ran the code you supplied and got a new error. BTW, thanks as always for you help.
ERROR: Sequence contains no matching element
In the code the side is named "a" in the add unit call, change it back to 'USSR' or whatever your side name is for the scene, probably the issue.

Re: Reusing A Random Location generator = failed

Posted: Sat Mar 12, 2022 1:18 am
by vettim89
Oops. Failed to notice that. Thanks again

Re: Reusing A Random Location generator = working but...

Posted: Sat Mar 12, 2022 6:16 pm
by vettim89
So the code has been properly debugged and is now working except for the final line of each iteration. I successfully create the subs I want but the "Assign Unit To Mission" function is failing. The script runs in the console without error message but I end up with a bunch of submarines sitting in the ocean without a mission

Code: Select all

math.randomseed(os.clock())
math.random();
local SSNA=math.random(2,4)
local SSKA=math.random(3,6)
local SSGA=math.random(1,2)
local SSNB=math.random(2,4)
local SSKB=math.random(3,6)
local SSGB=math.random(1,2)
local sub_type, subran, rname
for i=1,SSNA do
    subran=math.random(1,3)
    if subran==1 then sub_type='159';
    elseif subran==2 then sub_type='58';
    else sub_type='411';
    end
    newlat, newlong, ndepth = random_loc( 67, 70, -17, -14, 250)
    rname=math.random(1,500)
    sub=ScenEdit_AddUnit({side='USSR', type='sub', dbid=sub_type, latitude=newlat, longitude=newlong, name='sub'..rname, depth='200'})
    if sub_type=='411' then
      ScenEdit_AssignUnitToMission(sub.name, 'Sub Patrol C')
    else
      ScenEdit_AssignUnitToMission(sub.name, 'Sub Patrol A')
    end
end
for i=1,SSKA do
    subran=math.random(1,3)
    if subran==1 then sub_type='187';
    elseif subran==2 then sub_type='410';
    else sub_type='250';
    end
    newlat, newlong, ndepth = random_loc( 67, 70, -17, -14, 250)
    rname=math.random(1,500)
    sub=ScenEdit_AddUnit({side='USSR', type='sub', dbid=sub_type, latitude=newlat, longitude=newlong, name='sub'..rname, depth='200'})
    ScenEdit_AssignUnitToMission(sub.name, 'Sub Patrol A')
end
for i=1,SSGA do
    subran=math.random(1,3)
    if subran==1 then sub_type='196';
    elseif subran==2 then sub_type='390';
    else sub_type='110';
    end
    newlat, newlong, ndepth = random_loc( 67, 70, -17, -14, 250)
    rname=math.random(1,500)
    sub=ScenEdit_AddUnit({side='USSR', type='sub', dbid=sub_type, latitude=newlat, longitude=newlong, name='sub'..rname, depth='200'})
    ScenEdit_AssignUnitToMission(sub.name, 'Sub Patrol A')
end
for i=1,SSNB do
    subran=math.random(1,3)
    if subran==1 then sub_type='159';
    elseif subran==2 then sub_type='58';
    else sub_type='411';
    end
    newlat, newlong, ndepth = random_loc( 64, 67, 3, 6, 250)
    rname=math.random(1,500)
    sub=ScenEdit_AddUnit({side='USSR', type='sub', dbid=sub_type, latitude=newlat, longitude=newlong, name='sub'..rname, depth='200'})
    if sub_type=='411' then
      ScenEdit_AssignUnitToMission(sub.name, 'Sub Patrol D')
    else
      ScenEdit_AssignUnitToMission(sub.name, 'Sub Patrol B')
    end
end
for i=1,SSKB do
    subran=math.random(1,3)
    if subran==1 then sub_type='187';
    elseif subran==2 then sub_type='410';
    else sub_type='250';
    end
    newlat, newlong, ndepth = random_loc( 64, 67, 3, 6, 250)
    rname=math.random(1,500)
    sub=ScenEdit_AddUnit({side='USSR', type='sub', dbid=sub_type, latitude=newlat, longitude=newlong, name='sub'..rname, depth='200'})
    ScenEdit_AssignUnitToMission(sub.name, 'Sub Patrol B')
end
for i=1,SSGB do
    subran=math.random(1,3)
    if subran==1 then sub_type='196';
    elseif subran==2 then sub_type='390';
    else sub_type='110';
    end
    newlat, newlong, ndepth = random_loc( 64, 67, 3, 6, 250)
    rname=math.random(1,500)
    sub=ScenEdit_AddUnit({side='USSR', type='sub', dbid=sub_type, latitude=newlat, longitude=newlong, name='sub'..rname, depth='200'})
    ScenEdit_AssignUnitToMission(sub.name, 'Sub Patrol B')
end

Re: Reusing A Random Location generator = working but...

Posted: Mon Mar 14, 2022 3:14 am
by KnightHawk75
vettim89 wrote: Sat Mar 12, 2022 6:16 pm I successfully create the subs I want but the "Assign Unit To Mission" function is failing.
Do the missions pre-exist on the side before this code is called? Cause if not, that would happen.

Also while probably not the issue:
1. I would use sub.guid, not sub.name
2. sub is not defined as local.
3. you could just do sub.mission = 'ValidMissionNameHere';

Re: Reusing A Random Location generator = working AHA moment.

Posted: Wed Mar 23, 2022 8:27 pm
by vettim89
I figured it out. I am only posting this so others may not make the same mistake

error #1: I used 'depth' not altitude. Lua did not recognize 'depth' and ignored the setting

error #2: realistic sub communications were set to on for this scenario. You cannot assign a submarine to a mission with that setting on unless they are at communications depth which I believe is above 100 ft (some one who knows for sure correct me if I am wrong here).

So I changed depth to altitude (in negative numbers) set to periscope depth and TADA the function worked!!!