---All Functions -------------------------------------------------------------------------------------------------------------------------------- -- Save Pair Values (Long Keys Cause Errors) -------------------------------------------------------------------------------------------------------------------------------- function GetLongKeyValuePairs(key) -- Local Values local keyValueString = ScenEdit_GetKeyValue("CONST_LONG_KEY_VALUE") -- Check Key Value if keyValueString == nil then return "" end -- Split local table = split(keyValueString,",") for a, b in pairs(table) do -- Split Some More local subTable = split(b,"|") if #subTable == 2 then if subTable[1] == key then return subTable[2] end end end end function SetLongKeyValuePairs(key,value) -- Local Values local keyValueString = ScenEdit_GetKeyValue("CONST_LONG_KEY_VALUE") -- Check Key Value if keyValueString == nil then keyValueString = key.."-"..value else keyValueString = keyValueString..","..key.."|"..value end -- Adding To Key Value ScenEdit_SetKeyValue("CONST_LONG_KEY_VALUE",keyValueString) end function RemoveLongKeyValuePairs(key) -- Local Values local keyValueString = ScenEdit_GetKeyValue("CONST_LONG_KEY_VALUE") local newKeyValueString = "" -- Check Key Value if keyValueString == nil then return "" end -- Split local table = split(keyValueString,",") for a, b in pairs(table) do -- Split Some More local subTable = split(b,"|") if #subTable == 2 then if subTable[1] ~= key then if newKeyValueString ~= "" then newKeyValueString = newKeyValueString..","..b else newKeyValueString = b end end end end -- Save To Key Value ScenEdit_SetKeyValue("CONST_LONG_KEY_VALUE",newKeyValueString) end -------------------------------------------------------------------------------------------------------------------------------- -- SAR Target GUID Helpers -------------------------------------------------------------------------------------------------------------------------------- function GetSARTargetsGUID() local pilotString = ScenEdit_GetKeyValue("CONST_SAR_TARGET_GUID") if pilotString == nil then pilotString = "" end return split(pilotString,",") end function AddSARTargetGUID(pilotKey) local pilotString = ScenEdit_GetKeyValue("CONST_SAR_TARGET_GUID") if pilotString == nil then pilotString = pilotKey else pilotString = pilotString..","..pilotKey end ScenEdit_SetKeyValue("CONST_SAR_TARGET_GUID",pilotString) end function RemoveSARTargetGUID(pilotKey) local table = GetSARTargetsGUID() local pilotString = nil for k, v in pairs(table) do if guid ~= v then if pilotString then pilotString = pilotString..","..v else pilotString = v end end end ScenEdit_SetKeyValue("CONST_SAR_TARGET_GUID",pilotString) end function SARTargetGUIDExists(guid) local table = GetSARTargetsGUID() for k, v in pairs(table) do if guid == v then return true end end return false end function GetTargetGUIDCloseToSAR(sarUnit) local pilotsTable = GetSARTargetsGUID() for i,v in pairs(pilotsTable) do -- Check In Area if sarUnit:inArea({v.."-1",v.."-2",v.."-3",v.."-4"}) then return v end end return "" end -------------------------------------------------------------------------------------------------------------------------------- -- Helper -------------------------------------------------------------------------------------------------------------------------------- function split(s, sep) local fields = {} local sep = sep or " " local pattern = string.format("([^%s]+)", sep) string.gsub(s, pattern, function(c) fields[#fields + 1] = c end) return fields end -------------------------------------------------------------------------------------------------------------------------------- --Score Helper Functions -------------------------------------------------------------------------------------------------------------------------------- function AddScoreToSide(side,score,reason) local currentScore = ScenEdit_GetScore(side) currentScore = currentScore + score ScenEdit_SetScore(side,currentScore,reason) end -------------------------------------------------------------------------------------------------------------------------------- -- Create Downed Pilot From Trigger Unit -------------------------------------------------------------------------------------------------------------------------------- function CreateDownedPilotFromTriggerUnit(triggerUnit) -- Check if triggerUnit and not SARTargetGUIDExists(triggerUnit.guid) then -- Add Unit local downedPilot = ScenEdit_AddUnit({type="Facility", name ="Crew "..triggerUnit.name, side = triggerUnit.side, DBID = 2441, Latitude =37.0902, Longitude = 95.7129}) -- Set Attributes downedPilot.manualSpeed = 0 downedPilot.latitude = triggerUnit.latitude downedPilot.longitude = triggerUnit.longitude -- Check Downed Pilot Is Created if downedPilot then -- Create Reference Points Box local referencePointOne = ScenEdit_AddReferencePoint( {side = "Civilian", name = downedPilot.guid.."-1",lat = triggerUnit.latitude - 0.02, lon = triggerUnit.longitude - 0.02, clear=true}) local referencePointTwo = ScenEdit_AddReferencePoint( {side = "Civilian", name = downedPilot.guid.."-2",lat = triggerUnit.latitude + 0.02, lon = triggerUnit.longitude - 0.02, clear=true}) local referencePointThree = ScenEdit_AddReferencePoint( {side = "Civilian", name = downedPilot.guid.."-3",lat = triggerUnit.latitude + 0.02, lon = triggerUnit.longitude + 0.02, clear=true}) local referencePointFour = ScenEdit_AddReferencePoint( {side = "Civilian", name = downedPilot.guid.."-4",lat = triggerUnit.latitude - 0.02, lon = triggerUnit.longitude + 0.02, clear=true}) -- Create Trigger With Reference Points local addedTrigger = ScenEdit_SetTrigger({mode="add",type='UnitRemainsInArea',name=downedPilot.guid, targetfilter={TargetType = '1' , TargetSide=triggerUnit.side},area={referencePointOne.name,referencePointTwo.name,referencePointThree.name,referencePointFour.name},TD=15}) -- Add GUID AddSARTargetGUID(downedPilot.guid) -- Set Trigger To SAR Mission ScenEdit_SetEventTrigger("Event - Start SAR Target Pickup", {mode="add", name=downedPilot.guid}) -- Create Event For Destruction ScenEdit_SetEvent("Event - Cancel Pilot"..downedPilot.guid, {mode="add",IsRepeatable=0}) local cancelEvent = ScenEdit_GetEvent("Event - Cancel Pilot"..downedPilot.guid) SetLongKeyValuePairs(cancelEvent.guid,downedPilot.guid) -- Add Create Trigger local randInterval = math.random(8,10) ScenEdit_SetTrigger({mode="add", type="RegularTime", name="Trigger - Cancel Pilot"..downedPilot.guid, interval=randInterval}) -- Add Create Action ScenEdit_SetAction({mode="add", type="LuaScript", name="Action - Cancel Pilot"..downedPilot.guid, scriptText="StartSARFailedTargetPickup(ScenEdit_EventX())"}) -- Set Triggers And Actions ScenEdit_SetEventTrigger("Event - Cancel Pilot"..downedPilot.guid, {mode="add", name="Trigger - Cancel Pilot"..downedPilot.guid}) ScenEdit_SetEventAction("Event - Cancel Pilot"..downedPilot.guid, {mode="add", name="Action - Cancel Pilot"..downedPilot.guid}) -- Message Pilot Ejected ScenEdit_SpecialMessage(downedPilot.side,downedPilot.name.." : EJECTION! EJECTION! EJECTION!") end end end -------------------------------------------------------------------------------------------------------------------------------- -- Start SAR Target Pickup -------------------------------------------------------------------------------------------------------------------------------- function StartSARTargetPickup(triggerUnit) if triggerUnit and triggerUnit.speed <160 then -- Get Triggered Areas local triggeredAreas = triggerUnit.areaTriggersFired local triggeredGUID = "" -- Get Trigger GUID for k, v in pairs(triggeredAreas) do triggeredGUID = v end -- Find Pilot local keyGUID = GetTargetGUIDCloseToSAR(triggerUnit) local unit = ScenEdit_GetUnit({guid=keyGUID}) -- Scoring if unit then -- Add Score AddScoreToSide(unit.side,3,"Pilot Saved- "..unit.name.." is safely on board of "..triggerUnit.name) -- Message Pilot Saved ScenEdit_SpecialMessage(unit.side,unit.name.." is safely on board of "..triggerUnit.name) -- Remove Pilot Unit ScenEdit_DeleteUnit({side=triggerUnit.side, guid=keyGUID}) -- Remove All Triggers From Events ScenEdit_SetEventTrigger("Event - Start SAR Target Pickup", {mode="remove", name=keyGUID}) ScenEdit_SetEventTrigger("Event - Cancel Pilot"..keyGUID, {mode="remove", name="Trigger - Cancel Pilot"..keyGUID}) -- Remove All Actions From Events ScenEdit_SetEventAction("Event - Cancel Pilot"..keyGUID, {mode="remove", name="Action - Cancel Pilot"..keyGUID}) -- Remove Actual Trigger ScenEdit_SetTrigger({mode="remove",type="UnitRemainsInArea",name=keyGUID}) ScenEdit_SetTrigger({mode="remove",type="RegularTime",name="Trigger - Cancel Pilot"..keyGUID}) -- Remove Actual Action ScenEdit_SetAction({mode="remove", name="Action - Cancel Pilot"..keyGUID}) -- Remove Actual Event -- It does not work -- ScenEdit_SetEvent("Event - Cancel Pilot"..keyGUID, {mode="remove"}) -- Remove GUID RemoveSARTargetGUID(keyGUID) -- Remove All Reference Points ScenEdit_DeleteReferencePoint({side="Civilian",name=keyGUID.."-1"}) ScenEdit_DeleteReferencePoint({side="Civilian",name=keyGUID.."-2"}) ScenEdit_DeleteReferencePoint({side="Civilian",name=keyGUID.."-3"}) ScenEdit_DeleteReferencePoint({side="Civilian",name=keyGUID.."-4"}) end end end -------------------------------------------------------------------------------------------------------------------------------- -- Start SAR Failed Target Pickup -------------------------------------------------------------------------------------------------------------------------------- function StartSARFailedTargetPickup(triggerEvent) -- Triggered Id local keyGUID = GetLongKeyValuePairs(triggerEvent.guid) local unit = ScenEdit_GetUnit({guid=keyGUID}) -- Scoring if unit then -- Add Score AddScoreToSide(unit.side,-2,"Pilot Lost-"..unit.name) -- Remove Pilot Unit ScenEdit_DeleteUnit({side=unit.side, guid=keyGUID}) -- Message Lost Pilot ScenEdit_SpecialMessage(unit.side,"Sorry, we lost contact with "..unit.name) end -- Remove All Triggers From Events ScenEdit_SetEventTrigger("Event - Start SAR Target Pickup", {mode="remove", name=keyGUID}) ScenEdit_SetEventTrigger("Event - Cancel Pilot"..keyGUID, {mode="remove", name="Trigger - Cancel Pilot"..keyGUID}) -- Remove All Actions From Events ScenEdit_SetEventAction("Event - Cancel Pilot"..keyGUID, {mode="remove", name="Action - Cancel Pilot"..keyGUID}) -- Remove Actual Trigger ScenEdit_SetTrigger({mode="remove",type="UnitRemainsInArea",name=keyGUID}) ScenEdit_SetTrigger({mode="remove",type="RegularTime",name="Trigger - Cancel Pilot"..keyGUID}) -- Remove Actual Action ScenEdit_SetAction({mode="remove", name="Action - Cancel Pilot"..keyGUID}) -- Remove Actual Event -- It does not work --ScenEdit_SetEvent("Event - Cancel Pilot"..keyGUID, {mode="remove"}) -- Remove GUID RemoveSARTargetGUID(keyGUID) RemoveLongKeyValuePairs(triggerEvent.guid) -- Remove All Reference Points ScenEdit_DeleteReferencePoint({side="Civilian",name=keyGUID.."-1"}) ScenEdit_DeleteReferencePoint({side="Civilian",name=keyGUID.."-2"}) ScenEdit_DeleteReferencePoint({side="Civilian",name=keyGUID.."-3"}) ScenEdit_DeleteReferencePoint({side="Civilian",name=keyGUID.."-4"}) end