Ejemplo n.º 1
0
        private void Flee(List <Ingame.MyDetectedEntityInfo> radarData = null)
        {
            try
            {
                if (!IsFleeing)
                {
                    return;
                }

                try
                {
                    if (!FleeTimersTriggered)
                    {
                        TriggerFleeTimers();
                    }

                    try
                    {
                        if (radarData == null)
                        {
                            radarData = LookForEnemies(_freighterSetup.FleeTriggerDistance);
                        }
                        if (radarData.Count == 0)
                        {
                            return;
                        }

                        try
                        {
                            Ingame.MyDetectedEntityInfo closestEnemy = radarData.OrderBy(x => GridPosition.DistanceTo(x.Position)).FirstOrDefault();

                            if (closestEnemy.IsEmpty())
                            {
                                Grid.DebugWrite("Flee", "Cannot find closest hostile");
                                return;
                            }

                            try
                            {
                                IMyEntity enemyEntity = MyAPIGateway.Entities.GetEntityById(closestEnemy.EntityId);
                                if (enemyEntity == null)
                                {
                                    Grid.DebugWrite("Flee", "Cannot find enemy entity from closest hostile ID");
                                    return;
                                }

                                try
                                {
                                    //Grid.DebugWrite("Flee", $"Fleeing from '{EnemyEntity.DisplayName}'. Distance: {Math.Round(GridPosition.DistanceTo(ClosestEnemy.Position))}m; FleeTriggerDistance: {FreighterSetup.FleeTriggerDistance}");
                                    //ShowIngameMessage.ShowMessage($"Fleeing from '{enemyEntity.DisplayName}'. Distance: {Math.Round(GridPosition.DistanceTo(closestEnemy.Position))}m; FleeTriggerDistance: {_freighterSetup.FleeTriggerDistance}");
                                    Vector3D fleePoint = GridPosition.InverseVectorTo(closestEnemy.Position, 100 * 1000);
                                    //ShowIngameMessage.ShowMessage($"Flee point: {fleePoint} which is {GridPosition.DistanceTo(fleePoint)}m from me and enemy {enemyEntity.DisplayName}");
                                    //ShowIngameMessage.ShowMessage($"Fleeing at: {DetermineFleeSpeed()}m/s...");
                                    Rc.AddWaypoint(fleePoint, "Flee Point");
                                    (Rc as MyRemoteControl)?.ChangeFlightMode(Ingame.FlightMode.OneWay);
                                    (Rc as MyRemoteControl)?.SetAutoPilotSpeedLimit(DetermineFleeSpeed());
                                    Rc.SetAutoPilotEnabled(true);
                                }
                                catch (Exception scrap)
                                {
                                    Grid.LogError("Flee.AddWaypoint", scrap);
                                }
                            }
                            catch (Exception scrap)
                            {
                                Grid.LogError("Flee.LookForEnemies.GetEntity", scrap);
                            }
                        }
                        catch (Exception scrap)
                        {
                            Grid.LogError("Flee.LookForEnemies.Closest", scrap);
                        }
                    }
                    catch (Exception scrap)
                    {
                        Grid.LogError("Flee.LookForEnemies", scrap);
                    }
                }
                catch (Exception scrap)
                {
                    Grid.LogError("Flee.TriggerTimers", scrap);
                }
            }
            catch (Exception scrap)
            {
                Grid.LogError("Flee", scrap);
            }
        }