local function Round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
local function ConvertDecimalPositionToDegrees(latitude,longitude)
local latitidePrefix, longitudePrefix
if latitude > 0 then
latitidePrefix = "N"
else
latitidePrefix = "S"
latitude = latitude*-1
end
local latitudeDegrees = math.floor(latitude)
local latitudeMinutes = math.floor((latitude - latitudeDegrees)*60)
local latitudeSeconds = (((latitude-latitudeDegrees)*60) - latitudeMinutes)*60
latitudeSeconds = Round(latitudeSeconds, 0)
if longitude > 0 then
longitudePrefix = "E"
else
longitudePrefix = "W"
longitude = longitude*-1
end
local longitudeDegrees = math.floor(longitude)
local longitudeMinutes = math.floor((longitude - longitudeDegrees)*60)
local longitudeSeconds = (((longitude-longitudeDegrees)*60) - longitudeMinutes)*60
longitudeSeconds = Round(longitudeSeconds, 0)
local result = (latitidePrefix..latitudeDegrees .. "°" .. latitudeMinutes .. "'" .. latitudeSeconds .. '", '..
longitudePrefix..longitudeDegrees .. "°" .. longitudeMinutes .. "'" .. longitudeSeconds .. '"')
return result
end
local function GeneratePlayerSideUnitDataTable()
local result, unitList = {}, VP_GetSide({side='playerside'}).units
for k,v in ipairs (unitList) do
local unit = ScenEdit_GetUnit({guid=v.guid})
table.insert(result, unit)
end
return result
end
::retry::
local sideUnits = GeneratePlayerSideUnitDataTable()
local userInput = ScenEdit_InputBox('Which unit do you want to locate? \n\nType any unique part of the unit name, e.g. 73, col or collins will match SSG 73 Collins; case insensitive.')
userInput = string.lower(userInput)
if userInput == '' then goto finish end
local matchCount, matchedUnit = 0, nil
for k,v in ipairs (sideUnits) do
match = string.find(string.lower(v.name),userInput)
if match ~= nil then
matchedUnit = ScenEdit_GetUnit({guid=v.guid})
matchCount = matchCount + 1
end
end
local choice, retry = nil, nil
if matchCount == 0 then
retry = ScenEdit_MsgBox('No matches found. \n\nTry typing a more specific part of the unit name. Case is not important, but at least some of the name needs to be a match! \n\nDo you want to try again?',4)
if retry == 'Yes' then goto retry end
elseif matchCount == 1 then
choice = ScenEdit_MsgBox("Is the "..matchedUnit.type..", "..matchedUnit.classname.." '"..matchedUnit.name.."' the unit you're looking for ?",4)
elseif matchCount > 1 then
retry = ScenEdit_MsgBox('Multiple matches found. \n\nTry typing a more specific part of the unit name. Case is not important, but at least some of the name needs to be a match! \n\nDo you want to try again?',4)
if retry == 'Yes' then goto retry end
end
if choice == 'Yes' then
local unit = ScenEdit_GetUnit({guid=matchedUnit.guid})
local unitPositionDescription = ConvertDecimalPositionToDegrees(unit.latitude,unit.longitude)
ScenEdit_SpecialMessage('playerside',unit.name..' is currently located at '..unitPositionDescription..'<BR><BR> Click "Jump to Location" to go to this position.',{latitude=unit.latitude,longitude=unit.longitude})
end
::finish::
Also attached is a demo .scen file and a separate .lua file with the above code nicely formatted.