Reusing A Random Location generator = working AHA moment.
Posted: Sat Mar 05, 2022 10:09 pm
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)"
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)"