Example #1
0
        protected UnitMakerGroupInfo?AddMissionFeature(T featureDB, DCSMission mission, Coordinates coordinates, Coordinates?coordinates2, ref Dictionary <string, object> extraSettings, Side?objectiveTargetSide = null, bool hideEnemy = false)
        {
            // Add secondary coordinates (destination point) to the extra settings
            if (!coordinates2.HasValue)
            {
                coordinates2 = coordinates;                         // No destination point? Use initial point
            }
            extraSettings.AddIfKeyUnused("GroupX2", coordinates2.Value.X);
            extraSettings.AddIfKeyUnused("GroupY2", coordinates2.Value.Y);
            var TACANStr = GetExtraSettingsFromFeature(featureDB, ref extraSettings); // Add specific settings for this feature (TACAN frequencies, etc)

            // Feature unit group
            UnitMakerGroupInfo?groupInfo = null;

            if (FeatureHasUnitGroup(featureDB))
            {
                UnitMakerGroupFlags groupFlags = 0;

                if (featureDB.UnitGroupFlags.HasFlag(FeatureUnitGroupFlags.ImmediateAircraftActivation))
                {
                    groupFlags |= UnitMakerGroupFlags.ImmediateAircraftSpawn;
                }

                if (featureDB.UnitGroupFlags.HasFlag(FeatureUnitGroupFlags.RadioAircraftActivation))
                {
                    groupFlags |= UnitMakerGroupFlags.RadioAircraftSpawn;
                }

                Side groupSide = Side.Enemy;
                if (featureDB.UnitGroupFlags.HasFlag(FeatureUnitGroupFlags.Friendly))
                {
                    groupSide = Side.Ally;
                }
                else if (featureDB.UnitGroupFlags.HasFlag(FeatureUnitGroupFlags.SameSideAsTarget) && objectiveTargetSide.HasValue)
                {
                    groupSide = objectiveTargetSide.Value;
                }

                if (hideEnemy && groupSide == Side.Enemy)
                {
                    groupFlags |= UnitMakerGroupFlags.AlwaysHidden;
                }

                extraSettings.AddIfKeyUnused("Payload", featureDB.UnitGroupPayload);
                var groupLua   = featureDB.UnitGroupLuaGroup;
                var unitCount  = featureDB.UnitGroupSize.GetValue();
                var unitFamily = Toolbox.RandomFrom(featureDB.UnitGroupFamilies);
                var luaUnit    = featureDB.UnitGroupLuaUnit;
                SetAirbase(featureDB, unitFamily, ref groupLua, ref luaUnit, groupSide, ref coordinates, coordinates2.Value, unitCount, ref extraSettings);

                groupInfo = _unitMaker.AddUnitGroup(
                    unitFamily, unitCount,
                    groupSide,
                    groupLua, luaUnit,
                    coordinates, groupFlags,
                    extraSettings.ToArray());
                if (
                    groupSide == Side.Ally &&
                    groupInfo.HasValue &&
                    groupInfo.Value.UnitDB != null &&
                    groupInfo.Value.UnitDB.IsAircraft)
                {
                    mission.Briefing.AddItem(DCSMissionBriefingItemType.FlightGroup,
                                             $"{groupInfo.Value.Name}\t" +
                                             $"{unitCount}× {groupInfo.Value.UnitDB.UIDisplayName}\t" +
                                             $"{GeneratorTools.FormatRadioFrequency(groupInfo.Value.Frequency)}{TACANStr}\t" +
                                             $"{Toolbox.FormatPayload(featureDB.UnitGroupPayload)}"); // TODO: human-readable payload name
                }
                if (featureDB.ExtraGroups.Max > 1)
                {
                    SpawnExtraGroups(featureDB, mission, groupSide, groupFlags, coordinates, coordinates2.Value, extraSettings);
                }
            }

            // Feature Lua script
            string featureLua = "";

            // Adds the features' group ID to the briefingRoom.mission.missionFeatures.groupsID table
            if (this is MissionGeneratorFeaturesMission)
            {
                featureLua += $"briefingRoom.mission.missionFeatures.groupsID.{GeneratorTools.LowercaseFirstCharacter(featureDB.ID)} = {(groupInfo.HasValue ? groupInfo.Value.GroupID : 0)}\n";
                featureLua += $"briefingRoom.mission.missionFeatures.unitsID.{GeneratorTools.LowercaseFirstCharacter(featureDB.ID)} = {{{(groupInfo.HasValue ? string.Join(",", groupInfo.Value.UnitsID) : "")}}}\n";
            }

            if (!string.IsNullOrEmpty(featureDB.IncludeLuaSettings))
            {
                featureLua = featureDB.IncludeLuaSettings + "\n";
            }
            foreach (string luaFile in featureDB.IncludeLua)
            {
                featureLua += Toolbox.ReadAllTextIfFileExists($"{featureDB.SourceLuaDirectory}{luaFile}") + "\n";
            }
            foreach (KeyValuePair <string, object> extraSetting in extraSettings)
            {
                GeneratorTools.ReplaceKey(ref featureLua, extraSetting.Key, extraSetting.Value);
            }
            if (groupInfo.HasValue)
            {
                GeneratorTools.ReplaceKey(ref featureLua, "FeatureGroupID", groupInfo.Value.GroupID);
            }

            if (featureDB is DBEntryFeatureObjective)
            {
                mission.AppendValue("ScriptObjectivesFeatures", featureLua);
            }
            else
            {
                mission.AppendValue("ScriptMissionFeatures", featureLua);
            }

            // Add feature ogg files
            foreach (string oggFile in featureDB.IncludeOgg)
            {
                mission.AddMediaFile($"l10n/DEFAULT/{oggFile}", $"{BRPaths.INCLUDE_OGG}{oggFile}");
            }

            return(groupInfo);
        }