Beispiel #1
0
        public static bool GetSpiderSpawnPosition(out MatrixD spawnPosition, Vector3D?oldPosition)
        {
            spawnPosition = MatrixD.Identity;

            Vector3D?position = null;
            MyPlanet planet   = null;

            foreach (var player in Sync.Players.GetOnlinePlayers())
            {
                if (player.Id.SerialId != 0)
                {
                    continue;
                }
                if (player.Character == null)
                {
                    continue;
                }

                position = player.GetPosition();
                planet   = MyGravityProviderSystem.GetNearestPlanet(position.Value);

                var animalSpawnInfo = GetDayOrNightAnimalSpawnInfo(planet, position.Value);
                if (animalSpawnInfo == null || animalSpawnInfo.Animals == null ||
                    !animalSpawnInfo.Animals.Any(x => x.AnimalType.Contains("Spider")))
                {
                    position = null;
                    planet   = null;
                    continue;
                }

                if (oldPosition != null) // prevent teleporting from planet to planet
                {
                    var planetOld = MyGravityProviderSystem.GetNearestPlanet(oldPosition.Value);
                    if (planet != planetOld)
                    {
                        position = null;
                        planet   = null;
                        continue;
                    }
                }
                break;
            }

            if (!position.HasValue || planet == null)
            {
                return(false);
            }

            Vector3D gravity = planet.GetWorldGravity(position.Value);

            if (Vector3D.IsZero(gravity))
            {
                gravity = Vector3D.Down;
            }
            else
            {
                gravity.Normalize();
            }

            Vector3D tangent, bitangent;

            gravity.CalculatePerpendicularVector(out tangent);
            bitangent = Vector3D.Cross(gravity, tangent);

            Vector3D start = position.Value;

            start = MyUtils.GetRandomDiscPosition(ref start, 20.0f, ref tangent, ref bitangent);

            start -= gravity * 500;
            Vector3D translation = planet.GetClosestSurfacePointGlobal(ref start);
            Vector3D dirToPlayer = position.Value - translation;

            if (!Vector3D.IsZero(dirToPlayer))
            {
                dirToPlayer.Normalize();
            }
            else
            {
                dirToPlayer = Vector3D.CalculatePerpendicularVector(gravity);
            }
            spawnPosition = MatrixD.CreateWorld(translation, dirToPlayer, -gravity);

            return(true);
        }