Whicker
Posts: 664
Joined: 6/20/2018 Status: offline
|
modified to work with contacts - though the contact name must be an exact match - you can just copy it from the log (before running the action). Added trim function so if you copy a space at the end it will still match (only trimming on contacts). Can't do partial match on contacts I don't think since they all seem to start at #10 - so if you put in 13 there would likely be multiple #13s. quote:
local function sanitize(str) local s = string.gsub(str, "[%(%)%.%+%-%*%?%[%]%^%$%%]", "%%%1") return s end local function trim(s) -- from PiL2 20.4 return (s:gsub("^%s*(.-)%s*$", "%1")) end 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 --if 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 --if 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 --end function 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 local function unitFound(u) local unitPositionDescription = ConvertDecimalPositionToDegrees(u.latitude,u.longitude) ScenEdit_SpecialMessage('playerside',u.name..' is currently located at '..unitPositionDescription..'<BR><BR> Click "Jump to Location" to go to this position.',{latitude=u.latitude,longitude=u.longitude}) end --function local function listOfMatches(tableList) local list = '' for k,v in ipairs(tableList) do list = list.. '\n' ..v end return list end --function ::retry:: 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 --nothing entered userInputTrimmed= trim(userInput) --trim only is doing exact comparison, spaces may be valuable in fuzzy search -- check units for exact match, needed because fuzzy search returns both unit 1 and unit 11 local exactUnit = ScenEdit_GetUnit({side=ScenEdit_PlayerSide(), name=userInputTrimmed}) if exactUnit ~= nil then unitFound(exactUnit) return end --check contacts for exact match local exactContact = ScenEdit_GetContact({side=ScenEdit_PlayerSide(), name=userInputTrimmed}) if exactContact ~= nil then unitFound(exactContact) return end local sideUnits = GeneratePlayerSideUnitDataTable() local matchCount, matchedUnit, matches = 0, nil, {} userInput = sanitize(userInput) 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 table.insert(matches, matchedUnit.name) 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 local possibleMatches = listOfMatches(matches) 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! \nPossible Matches:\n'..possibleMatches..'\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}) unitFound(unit) end ::finish::
< Message edited by Whicker -- 7/2/2019 1:52:35 AM >
|