Ejemplo n.º 1
0
        /// <summary>Creates an attacking ship.</summary>
        /// <param name="startPosition">The start position.</param>
        /// <param name="targetPosition">The target position.</param>
        /// <param name="faction">The faction.</param>
        public void CreateAttackingShip(ref FarPosition startPosition, ref FarPosition targetPosition, Factions faction)
        {
            var ship = EntityFactory.CreateAIShip(Manager, "L1_AI_Ship", faction, startPosition, _random);
            var ai   = ((ArtificialIntelligence)Manager.GetComponent(ship, ArtificialIntelligence.TypeId));

            ai.AttackMove(ref targetPosition);
        }
Ejemplo n.º 2
0
        /// <summary>Processes a single spawn from the top of the spawn queue.</summary>
        private void ProcessSpawn(ulong id)
        {
            // Get the cell position.
            var position = CellSystem.GetSubCellCoordinatesFromId(id);

            // Get the cell info to know what faction we're spawning for. Use the large cell id for that,
            // because we only store info for that.
            var cellInfo = ((UniverseSystem)Manager.GetSystem(UniverseSystem.TypeId))
                           .GetCellInfo(CellSystem.GetCellIdFromCoordinates(position));

            // The area covered by the cell.
            FarRectangle cellArea;

            cellArea.X      = position.X;
            cellArea.Y      = position.Y;
            cellArea.Width  = CellSystem.SubCellSize;
            cellArea.Height = CellSystem.SubCellSize;

            // Get center point for spawn group.
            FarPosition spawnPoint;

            spawnPoint.X = _random.NextInt32((int)cellArea.Left, (int)cellArea.Right);
            spawnPoint.Y = _random.NextInt32((int)cellArea.Top, (int)cellArea.Bottom);

            // Configuration for spawned ships.
            string[] ships;
            ArtificialIntelligence.AIConfiguration[] configurations = null;
            var formation = SquadSystem.Formations.None;

            // TODO different groups, based on cell info? definable via editor maybe?
            if (_random.NextDouble() < 0.5f)
            {
                ships = new[]
                {
                    "L1_AI_Ship",
                    "L1_AI_Ship",
                    "L1_AI_Ship",
                    "L1_AI_Ship",
                    "L1_AI_Ship",
                    "L1_AI_Ship"
                };
                configurations = new[]
                {
                    new ArtificialIntelligence.AIConfiguration
                    {
                        AggroRange = UnitConversion.ToSimulationUnits(600)
                    }
                };
                formation = SquadSystem.Formations.Block;
            }
            else
            {
                ships = new[]
                {
                    "L1_AI_Ship",
                    "L1_AI_Ship",
                    "L1_AI_Ship",
                    "L1_AI_Ship",
                    "L1_AI_Ship"
                };
                configurations = new[]
                {
                    new ArtificialIntelligence.AIConfiguration
                    {
                        AggroRange = UnitConversion.ToSimulationUnits(800)
                    }
                };
                formation = SquadSystem.Formations.Vee;
            }

            // Spawn all ships.
            Squad leaderSquad = null;

            for (var i = 0; i < ships.Length; i++)
            {
                // Get the configuration for this particular ship. If we don't have enough configurations
                // we just re-use the last existing one.
                var configuration = configurations != null && configurations.Length > 0
                                        ? configurations[Math.Min(i, configurations.Length - 1)]
                                        : null;

                // Get a position nearby the spawn (avoids spawning all ships in one point).
                var spawnPosition = spawnPoint;
                spawnPosition.X += UnitConversion.ToSimulationUnits(_random.NextInt32(-100, 100));
                spawnPosition.Y += UnitConversion.ToSimulationUnits(_random.NextInt32(-100, 100));

                // Create the ship and get the AI component.
                var ship = EntityFactory.CreateAIShip(
                    Manager, ships[i], cellInfo.Faction, spawnPosition, _random, configuration);
                var ai = (ArtificialIntelligence)Manager.GetComponent(ship, ArtificialIntelligence.TypeId);

                // Push fallback roam behavior, if an area has been specified.
                ai.Roam(ref cellArea);

                // If we have a squad push the squad component.
                if (ships.Length <= 1)
                {
                    // No squad, skip squad component.
                    continue;
                }
                var squad = Manager.AddComponent <Squad>(ship);
                // If we're not the leader we guard him, otherwise mark us as
                // the squad leader (ergo: first loop iteration).
                if (leaderSquad != null)
                {
                    leaderSquad.AddMember(ship);
                    ai.Guard(leaderSquad.Entity);
                }
                else
                {
                    leaderSquad     = squad;
                    squad.Formation = formation;
                }
            }
        }