public static string AttemptSpawn(Vector3D startCoords)
        {
            if (Settings.General.UseMaxNpcGrids == true)
            {
                var totalNPCs = NPCWatcher.ActiveNPCs.Count;

                if (totalNPCs >= Settings.General.MaxGlobalNpcGrids)
                {
                    return("Spawning Aborted. Max Global NPCs Limit Reached.");
                }
            }

            if (NPCWatcher.ActiveNpcTypeLimitReachedForArea("SpaceCargoShip", startCoords, Settings.SpaceCargoShips.MaxShipsPerArea, Settings.SpaceCargoShips.AreaSize) == true)
            {
                return("Too Many Space Cargo Ship Grids in Player Area");
            }

            var validFactions  = new Dictionary <string, List <string> >();
            var spawnGroupList = GetSpaceCargoShips(startCoords, out validFactions);

            if (Settings.General.UseModIdSelectionForSpawning == true)
            {
                spawnGroupList = SpawnResources.SelectSpawnGroupSublist(SpawnGroupSublists, EligibleSpawnsByModId);
            }

            if (spawnGroupList.Count == 0)
            {
                return("No Eligible Spawn Groups Could Be Found To Spawn Near Player.");
            }

            var      spawnGroup      = spawnGroupList[SpawnResources.rnd.Next(0, spawnGroupList.Count)];
            var      startPathCoords = Vector3D.Zero;
            var      endPathCoords   = Vector3D.Zero;
            bool     successfulPath  = false;
            MyPlanet planet          = SpawnResources.GetNearestPlanet(startCoords);

            if (SpawnResources.LunarSpawnEligible(startCoords) == false)
            {
                successfulPath = CalculateRegularTravelPath(spawnGroup.SpawnGroup, startCoords, out startPathCoords, out endPathCoords);
            }
            else
            {
                successfulPath = CalculateLunarTravelPath(spawnGroup.SpawnGroup, startCoords, out startPathCoords, out endPathCoords);
            }

            if (successfulPath == false)
            {
                return("Could Not Generate Safe Travel Path For SpawnGroup.");
            }

            //Get Directions
            var  spawnForwardDir = Vector3D.Normalize(endPathCoords - startPathCoords);
            var  spawnUpDir      = Vector3D.CalculatePerpendicularVector(spawnForwardDir);
            var  spawnMatrix     = MatrixD.CreateWorld(startPathCoords, spawnForwardDir, spawnUpDir);
            long gridOwner       = 0;
            var  randFactionTag  = spawnGroup.FactionOwner;

            if (validFactions.ContainsKey(spawnGroup.SpawnGroupName))
            {
                randFactionTag = validFactions[spawnGroup.SpawnGroupName][SpawnResources.rnd.Next(0, validFactions[spawnGroup.SpawnGroupName].Count)];
            }

            if (NPCWatcher.NPCFactionTagToFounder.ContainsKey(randFactionTag) == true)
            {
                gridOwner = NPCWatcher.NPCFactionTagToFounder[randFactionTag];
            }
            else
            {
                Logger.AddMsg("Could Not Find Faction Founder For: " + randFactionTag);
            }

            foreach (var prefab in spawnGroup.SpawnGroup.Prefabs)
            {
                var options       = SpawnGroupManager.CreateSpawningOptions(spawnGroup, prefab);
                var spawnPosition = Vector3D.Transform((Vector3D)prefab.Position, spawnMatrix);
                var speedL        = prefab.Speed * (Vector3)spawnForwardDir;
                var speedA        = Vector3.Zero;
                var gridList      = new List <IMyCubeGrid>();


                //Speed Management
                if (Settings.SpaceCargoShips.UseMinimumSpeed == true && prefab.Speed < Settings.SpaceCargoShips.MinimumSpeed)
                {
                    speedL = Settings.SpaceCargoShips.MinimumSpeed * (Vector3)spawnForwardDir;
                }

                if (Settings.SpaceCargoShips.UseSpeedOverride == true)
                {
                    speedL = Settings.SpaceCargoShips.SpeedOverride * (Vector3)spawnForwardDir;
                }



                //Grid Manipulation
                GridBuilderManipulation.ProcessPrefabForManipulation(prefab.SubtypeId, spawnGroup, "SpaceCargoShip", prefab.Behaviour);

                try{
                    MyAPIGateway.PrefabManager.SpawnPrefab(gridList, prefab.SubtypeId, spawnPosition, spawnForwardDir, spawnUpDir, speedL, speedA, prefab.BeaconText, options, gridOwner);
                }catch (Exception exc) {
                    Logger.AddMsg("Something Went Wrong With Prefab Spawn Manager.", true);
                }

                var pendingNPC = new ActiveNPC();
                pendingNPC.SpawnGroupName        = spawnGroup.SpawnGroupName;
                pendingNPC.SpawnGroup            = spawnGroup;
                pendingNPC.InitialFaction        = randFactionTag;
                pendingNPC.faction               = MyAPIGateway.Session.Factions.TryGetFactionByTag(pendingNPC.InitialFaction);
                pendingNPC.Name                  = prefab.SubtypeId;
                pendingNPC.GridName              = MyDefinitionManager.Static.GetPrefabDefinition(prefab.SubtypeId).CubeGrids[0].DisplayName;
                pendingNPC.StartCoords           = startPathCoords;
                pendingNPC.CurrentCoords         = startPathCoords;
                pendingNPC.EndCoords             = endPathCoords;
                pendingNPC.SpawnType             = "SpaceCargoShip";
                pendingNPC.AutoPilotSpeed        = speedL.Length();
                pendingNPC.CleanupIgnore         = spawnGroup.IgnoreCleanupRules;
                pendingNPC.ForceStaticGrid       = spawnGroup.ForceStaticGrid;
                pendingNPC.KeenAiName            = prefab.Behaviour;
                pendingNPC.KeenAiTriggerDistance = prefab.BehaviourActivationDistance;

                if (string.IsNullOrEmpty(pendingNPC.KeenAiName) == false)
                {
                    Logger.AddMsg("Stock AI Detected In Prefab: " + prefab.SubtypeId + " in SpawnGroup: " + spawnGroup.SpawnGroup.Id.SubtypeName);
                }

                if (spawnGroup.RandomizeWeapons == true)
                {
                    pendingNPC.ReplenishedSystems = false;
                    pendingNPC.ReplacedWeapons    = true;
                }
                else if ((MES_SessionCore.NPCWeaponUpgradesModDetected == true || Settings.General.EnableGlobalNPCWeaponRandomizer == true) && spawnGroup.IgnoreWeaponRandomizerMod == false)
                {
                    pendingNPC.ReplenishedSystems = false;
                    pendingNPC.ReplacedWeapons    = true;
                }
                else if (spawnGroup.ReplenishSystems == true)
                {
                    pendingNPC.ReplenishedSystems = false;
                }

                NPCWatcher.PendingNPCs.Add(pendingNPC);
            }

            Logger.SkipNextMessage = false;
            return("Spawning Group - " + spawnGroup.SpawnGroup.Id.SubtypeName);
        }
        public static List <ImprovedSpawnGroup> GetSpaceCargoShips(Vector3D playerCoords, out Dictionary <string, List <string> > validFactions)
        {
            MyPlanet planet             = SpawnResources.GetNearestPlanet(playerCoords);
            bool     allowLunar         = false;
            string   specificGroup      = "";
            var      planetRestrictions = new List <string>(Settings.General.PlanetSpawnsDisableList.ToList());

            validFactions = new Dictionary <string, List <string> >();
            SpawnGroupSublists.Clear();
            EligibleSpawnsByModId.Clear();

            if (planet != null)
            {
                if (planetRestrictions.Contains(planet.Generator.Id.SubtypeName) == true)
                {
                    return(new List <ImprovedSpawnGroup>());
                }
            }

            bool specificSpawnRequest = false;

            if (SpawnGroupManager.AdminSpawnGroup != "")
            {
                specificGroup = SpawnGroupManager.AdminSpawnGroup;
                SpawnGroupManager.AdminSpawnGroup = "";
                specificSpawnRequest = true;
            }

            if (SpawnResources.IsPositionInGravity(playerCoords, planet) == true)
            {
                if (SpawnResources.LunarSpawnEligible(playerCoords) == true)
                {
                    allowLunar = true;
                }
                else
                {
                    return(new List <ImprovedSpawnGroup>());
                }
            }

            string planetName = "";

            if (planet != null)
            {
                planetName = planet.Generator.Id.SubtypeId.ToString();
            }

            var eligibleGroups = new List <ImprovedSpawnGroup>();

            //Filter Eligible Groups To List
            foreach (var spawnGroup in SpawnGroupManager.SpawnGroups)
            {
                if (specificGroup != "" && spawnGroup.SpawnGroup.Id.SubtypeName != specificGroup)
                {
                    continue;
                }

                if (specificGroup == "" && spawnGroup.AdminSpawnOnly == true)
                {
                    continue;
                }

                if (spawnGroup.SpaceCargoShip == false)
                {
                    if (allowLunar == true)
                    {
                        if (spawnGroup.LunarCargoShip == false)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        continue;
                    }
                }

                if (SpawnResources.CheckCommonConditions(spawnGroup, playerCoords, planet, specificSpawnRequest) == false)
                {
                    continue;
                }

                var validFactionsList = SpawnResources.ValidNpcFactions(spawnGroup, playerCoords);

                if (validFactionsList.Count == 0)
                {
                    continue;
                }

                if (validFactions.ContainsKey(spawnGroup.SpawnGroupName) == false)
                {
                    validFactions.Add(spawnGroup.SpawnGroupName, validFactionsList);
                }

                if (spawnGroup.Frequency > 0)
                {
                    string modID = spawnGroup.SpawnGroup.Context.ModId;

                    if (string.IsNullOrEmpty(modID) == true)
                    {
                        modID = "KeenSWH";
                    }

                    if (SpawnGroupSublists.ContainsKey(modID) == false)
                    {
                        SpawnGroupSublists.Add(modID, new List <ImprovedSpawnGroup>());
                    }

                    if (EligibleSpawnsByModId.ContainsKey(modID) == false)
                    {
                        EligibleSpawnsByModId.Add(modID, 1);
                    }
                    else
                    {
                        EligibleSpawnsByModId[modID] += 1;
                    }

                    if (Settings.SpaceCargoShips.UseMaxSpawnGroupFrequency == true && spawnGroup.Frequency > Settings.SpaceCargoShips.MaxSpawnGroupFrequency * 10)
                    {
                        spawnGroup.Frequency = (int)Math.Round((double)Settings.SpaceCargoShips.MaxSpawnGroupFrequency * 10);
                    }

                    for (int i = 0; i < spawnGroup.Frequency; i++)
                    {
                        eligibleGroups.Add(spawnGroup);
                        SpawnGroupSublists[modID].Add(spawnGroup);
                    }
                }
            }

            return(eligibleGroups);
        }
Esempio n. 3
0
        public static List <ImprovedSpawnGroup> GetSpaceCargoShips(Vector3D playerCoords, List <string> eligibleNames, out Dictionary <string, List <string> > validFactions)
        {
            bool   allowLunar         = false;
            string specificGroup      = "";
            var    planetRestrictions = new List <string>(Settings.General.PlanetSpawnsDisableList.ToList());

            validFactions = new Dictionary <string, List <string> >();
            SpawnGroupSublists.Clear();
            EligibleSpawnsByModId.Clear();
            var environment = new EnvironmentEvaluation(playerCoords);

            if (environment.NearestPlanet != null)
            {
                if (planetRestrictions.Contains(environment.NearestPlanetName) && environment.IsOnPlanet)
                {
                    Logger.SpawnGroupDebug(Logger.DebugSpawnGroup, "Restricted Planet, No Spawns Allowed");
                    return(new List <ImprovedSpawnGroup>());
                }
            }

            bool specificSpawnRequest = false;

            if (SpawnGroupManager.AdminSpawnGroup != "")
            {
                specificGroup = SpawnGroupManager.AdminSpawnGroup;
                SpawnGroupManager.AdminSpawnGroup = "";
                specificSpawnRequest = true;
            }

            if (environment.IsOnPlanet)
            {
                if (SpawnResources.LunarSpawnEligible(playerCoords) == true)
                {
                    allowLunar = true;
                }
                else
                {
                    Logger.SpawnGroupDebug("[No Spawn Group]", "On Planet and Cannot Spawn As Lunar, No Spawns Allowed");
                    return(new List <ImprovedSpawnGroup>());
                }
            }

            var eligibleGroups = new List <ImprovedSpawnGroup>();

            //Filter Eligible Groups To List
            foreach (var spawnGroup in SpawnGroupManager.SpawnGroups)
            {
                if (eligibleNames != null)
                {
                    if (!eligibleNames.Contains(spawnGroup.SpawnGroupName))
                    {
                        Logger.SpawnGroupDebug(spawnGroup.SpawnGroup.Id.SubtypeName, "Was not included in Groups From API Request");
                        continue;
                    }
                }
                else
                {
                    if (specificGroup != "" && spawnGroup.SpawnGroup.Id.SubtypeName != specificGroup)
                    {
                        Logger.SpawnGroupDebug(spawnGroup.SpawnGroup.Id.SubtypeName, "Doesn't Match Admin Spawn");
                        continue;
                    }

                    if (specificGroup == "" && spawnGroup.AdminSpawnOnly == true)
                    {
                        Logger.SpawnGroupDebug(spawnGroup.SpawnGroup.Id.SubtypeName, "Only Admin Spawn Allowed");
                        continue;
                    }
                }

                if (spawnGroup.SpaceCargoShip == false)
                {
                    if (allowLunar == true)
                    {
                        if (spawnGroup.LunarCargoShip == false)
                        {
                            Logger.SpawnGroupDebug(spawnGroup.SpawnGroup.Id.SubtypeName, "Is Not Space Cargo Ship Or Lunar");
                            continue;
                        }
                    }
                    else
                    {
                        Logger.SpawnGroupDebug(spawnGroup.SpawnGroup.Id.SubtypeName, "Is Not Space Cargo Ship");
                        continue;
                    }
                }

                if (SpawnResources.CheckCommonConditions(spawnGroup, playerCoords, environment, specificSpawnRequest) == false)
                {
                    Logger.SpawnGroupDebug(spawnGroup.SpawnGroup.Id.SubtypeName, "Common Conditions Failed");
                    continue;
                }

                var validFactionsList = SpawnResources.ValidNpcFactions(spawnGroup, playerCoords);

                if (validFactionsList.Count == 0)
                {
                    Logger.SpawnGroupDebug(spawnGroup.SpawnGroup.Id.SubtypeName, "No Valid Faction");
                    continue;
                }

                if (validFactions.ContainsKey(spawnGroup.SpawnGroupName) == false)
                {
                    validFactions.Add(spawnGroup.SpawnGroupName, validFactionsList);
                }

                if (spawnGroup.Frequency > 0)
                {
                    string modID = spawnGroup.SpawnGroup.Context.ModId;

                    if (string.IsNullOrEmpty(modID) == true)
                    {
                        modID = "KeenSWH";
                    }

                    if (SpawnGroupSublists.ContainsKey(modID) == false)
                    {
                        SpawnGroupSublists.Add(modID, new List <ImprovedSpawnGroup>());
                    }

                    if (EligibleSpawnsByModId.ContainsKey(modID) == false)
                    {
                        EligibleSpawnsByModId.Add(modID, 1);
                    }
                    else
                    {
                        EligibleSpawnsByModId[modID] += 1;
                    }

                    if (Settings.SpaceCargoShips.UseMaxSpawnGroupFrequency == true && spawnGroup.Frequency > Settings.SpaceCargoShips.MaxSpawnGroupFrequency * 10)
                    {
                        spawnGroup.Frequency = (int)Math.Round((double)Settings.SpaceCargoShips.MaxSpawnGroupFrequency * 10);
                    }

                    for (int i = 0; i < spawnGroup.Frequency; i++)
                    {
                        eligibleGroups.Add(spawnGroup);
                        SpawnGroupSublists[modID].Add(spawnGroup);
                    }
                }
            }

            return(eligibleGroups);
        }