@parel803
These may be helpful, more than you need for your exact specifics, but also flexible for other uses.
function getUpdatedSideTable()
local sides = VP_GetSides();
local t ={}
for k,v in pairs(sides) do t[v.name] = v.guid; end
return t;
end
--// function to determine if a contact has been detected by a side in a given amount of time.
--// optionally can either do it based on detection age overall, or specific to a particular unit, or age and particular.
--//requires: build 1147.34+ (contact last detection table added)
---@param viewSidename string - required name of the side the contact guid is associated with.
---@param contactGuid string - required guid of the contact object.
---@param ageSeconds? number - optional number of seconds; last detection time qualifier. (less than or eq)
---@param detectorGuid? string - optional guid of the unit doing the detecting. ie has this particular unit detected the contact recently.
---@return boolean - true|false - true if qualified match found.
function isContactRecentlyDetectedBySide(viewSidename,contactGuid,ageSeconds,detectorGuid)
local retval,c = pcall(ScenEdit_GetContact,{side=viewSidename,guid=contactGuid});
if (retval) and c ~=nil then
if c.lastDetections ~=nil then
for i=1,#c.lastDetections do
if ageSeconds < 0 and detectorGuid ==nil then --error
print('age and detector guid can not both be empty, aborting.'); return false;
elseif ageSeconds < 0 and detectorGuid ~=nil then -- no age check just unit.
if (c.lastDetections[i].detector_guid ~=nil) and c.lastDetections[i].detector_guid == detectorGuid then
return true;
end
elseif ageSeconds > 0 and detectorGuid ~=nil then --specific unit and age check for that unit.
if (c.lastDetections[i].age ~=nil and c.lastDetections[i].detector_guid ~=nil) and c.lastDetections[i].age <= ageSeconds and c.lastDetections[i].detector_guid == detectorGuid then
return true;
end
elseif ageSeconds > 0 and detectorGuid ==nil then -- just age
if (c.lastDetections[i].age ~=nil) and c.lastDetections[i].age <= ageSeconds then
return true;
end
end
end
end
else print('Contact guid does not exist on that side.');
end
return false;
end
--// Return true if unit is a contact on a given side.
--// Can optionally further check if contact was last detected in a given amount of seconds.
--// Can optionally further check if contact was as last detected in given time, specific unit, or both
--//requires: build 1147.34+
---@param unitGuid string - The guid of the unit.
---@param bySideName string - The sidename to search for this unit as a contact on.
---@param ageSeconds? number - Optional time age in seconds (< or eq to this time)
---@param detectorGuid? string - optional guid of the unit doing the detecting. ie has this particular unit detected the contact recently.
---@return boolean - true|false - true if qualifying match
function isContactByUnitGuid(unitGuid,bySideName,ageSeconds,detectorGuid)
if unitGuid == nil then print ('Missing unitGuid param. aborting.'); return false; end
if bySideName == nil then print('Missing bySideName param aborting.'); return false; end
if ageSeconds == nil then ageSeconds = -1; end
local retval,realUnit = pcall(ScenEdit_GetUnit,{guid=unitGuid});
if ((retval) and realUnit ~=nil) then
if realUnit.ascontact ~=nil then
local sidetable = getUpdatedSideTable();
for _,c in pairs(realUnit.ascontact) do
if c.side == sidetable[bySideName] then
if ageSeconds == -1 then return true; --skip recent detection check.
elseif isContactRecentlyDetectedBySide(bySideName,c.guid,ageSeconds,detectorGuid) then return true; end
end
end
end
else print("A unit with that guid does not exist.");
end
return false;
end
Usage:
Assume 4FH7PU-0HMFAK0FJQ7F5 is some Blue ship guid (unit being followed)
Assume 4FH7PU-0HMFAK0FJQ94G is some Red Unit guid (who you want to know if it specifically has spotted the ship recently)
--Is this unit a contact on side Red?
print(isContactByUnitGuid('4FH7PU-0HMFAK0FJQ7F5',"Red")); --all you need if I read the thread right.
--Is this unit a contact on side Red, and if so has it been seen in the last 60 seconds by any Red unit.
print(isContactByUnitGuid('4FH7PU-0HMFAK0FJQ7F5',"Red",180)); -- if you want to make sure the contact is reasonably fresh.
--Is this unit a contact on side Red, and if so has it been seen in the last 60 seconds by a specific unit.
print(isContactByUnitGuid('4FH7PU-0HMFAK0FJQ7F5',"Red",60,"4FH7PU-0HMFAK0FJQ94G"));