Page 1 of 1
Random starting position first tries
Posted: Mon Mar 24, 2025 8:01 pm
by Orion Omega
Hi everyone,
I'm dipping my toes into Lua a bit and tried to produce a random location start. Finally I got something to work: 3 preset zones. Somewhere in there my unit will spawn.
My question: how would you have solved this problem?
The zones and the unit are given. Randomly choose a starting position in these zones.
All the best
Re: Random starting position first tries
Posted: Tue Mar 25, 2025 6:32 am
by Orion Omega
Here the Lua part:
Code: Select all
local MySide = VP_GetSide({name = "Friendly"})
local MyZone1 = MySide:getstandardzone("Zone1")
local MyZone2 = MySide:getstandardzone("Zone2")
local MyZone3 = MySide:getstandardzone("Zone3")
--choosing the zone
math.randomseed(os.time())
local r = math.random(1,3)
if r == 1 then
RandomZone = MyZone1
elseif r == 2 then
RandomZone = MyZone2
elseif r == 3 then
RandomZone = MyZone3
end
--getting Latitude RPs
local Latitude = {}
for i = 1, 4 do
Latitude[i] = RandomZone.area[i].latitude
end
--finding max Latitude RP
local LatitudeMax = math.mininteger
for i, v in ipairs(Latitude) do
if Latitude[i] > LatitudeMax then
LatitudeMax = Latitude[i]
end
end
--finding min Latitude RP
local LatitudeMin = math.maxinteger
for i, v in ipairs(Latitude) do
if Latitude[i] < LatitudeMin then
LatitudeMin = Latitude[i]
end
end
--getting Longitude RPs
local Longitude = {}
for i = 1, 4 do
Longitude[i] = RandomZone.area[i].longitude
end
--finding max Longitude RP
local LongitudeMax = math.mininteger
for i, v in ipairs(Longitude) do
if Longitude[i] > LongitudeMax then
LongitudeMax = Longitude[i]
end
end
--finding min Longitude RP
local LongitudeMin = math.maxinteger
for i, v in ipairs(Longitude) do
if Longitude[i] < LongitudeMin then
LongitudeMin = Longitude[i]
end
end
--spawning ship
SpawnLatitude = math.random() * (LatitudeMax - LatitudeMin) + LatitudeMin
SpawnLongitude = math.random() * (LongitudeMax - LongitudeMin) + LongitudeMin
local MyShip = ScenEdit_AddUnit({type = "Ship", name = "Random Spawn", side = "Friendly", dbid = 3889, lat = SpawnLatitude, long = SpawnLongitude})