private void RotateInLineWithVelocity(float elapsedTime, ArtificialIntelligence.ShipGoal Goal)
 {
     if (this.Owner.Velocity == Vector2.Zero)
     {
         this.OrderQueue.RemoveFirst();
         return;
     }
     Vector2 forward = new Vector2((float)Math.Sin((double)this.Owner.Rotation), -(float)Math.Cos((double)this.Owner.Rotation));
     Vector2 right = new Vector2(-forward.Y, forward.X);
     float angleDiff = (float)Math.Acos((double)Vector2.Dot(Vector2.Normalize(this.Owner.Velocity), forward));
     float facing = (Vector2.Dot(Vector2.Normalize(this.Owner.Velocity), right) > 0f ? 1f : -1f);
     if (angleDiff <= 0.2f)
     {
         this.OrderQueue.RemoveFirst();
         return;
     }
     this.RotateToFacing(elapsedTime, angleDiff, facing);
 }
 private void RotateToDesiredFacing(float elapsedTime, ArtificialIntelligence.ShipGoal goal)
 {
     Vector2 p = HelperFunctions.findPointFromAngleAndDistanceUsingRadians(Vector2.Zero, goal.DesiredFacing, 1f);
     Vector2 fvec = HelperFunctions.FindVectorToTarget(Vector2.Zero, p);
     Vector2 wantedForward = Vector2.Normalize(fvec);
     Vector2 forward = new Vector2((float)Math.Sin((double)this.Owner.Rotation), -(float)Math.Cos((double)this.Owner.Rotation));
     Vector2 right = new Vector2(-forward.Y, forward.X);
     float angleDiff = (float)Math.Acos((double)Vector2.Dot(wantedForward, forward));
     float facing = (Vector2.Dot(wantedForward, right) > 0f ? 1f : -1f);
     if (angleDiff <= 0.02f)
     {
         this.OrderQueue.RemoveFirst();
         return;
     }
     this.RotateToFacing(elapsedTime, angleDiff, facing);
 }
        private void MoveToWithin1000(float elapsedTime, ArtificialIntelligence.ShipGoal goal)
        {
            if (this.OrderQueue.Count > 1 && this.OrderQueue.Skip(1).First().Plan != Plan.MoveToWithin1000 && goal.TargetPlanet != null)
            {
                lock (this.wayPointLocker)
                {
                    this.ActiveWayPoints.Last().Equals(goal.TargetPlanet.Position);
                    goal.MovePosition = goal.TargetPlanet.Position;
                }
            }
            float speedLimit =  (int)(this.Owner.speed)  ;
            float single = Vector2.Distance(this.Owner.Center, goal.MovePosition);
            if (this.ActiveWayPoints.Count <= 1)
            {
                if (single  < this.Owner.speed)
                {
                    speedLimit = single;
                    //this.Owner.speed =this.Owner.speed < single ? this.Owner.speed: single;
                }

            }
            this.ThrustTowardsPosition(goal.MovePosition, elapsedTime, speedLimit);
            if (this.ActiveWayPoints.Count <= 1)
            {
                if (single <= 1500f)
                {
                    lock (this.wayPointLocker)
                    {
                        if (this.ActiveWayPoints.Count > 1)
                        {
                            this.ActiveWayPoints.Dequeue();
                        }
                        if (this.OrderQueue.Count > 0)
                        {
                            this.OrderQueue.RemoveFirst();
                        }
                    }
                }
                //else if(this.ColonizeTarget !=null)
                //{
                //    lock (this.wayPointLocker)
                //    {
                //        this.ActiveWayPoints.First().Equals(this.ColonizeTarget.Position);
                //        this.OrderQueue.First().MovePosition = this.ColonizeTarget.Position;
                //    }
                //}

            }
            else if (this.Owner.engineState == Ship.MoveState.Warp)
            {
                if (single <= 15000f)
                {
                    lock (this.wayPointLocker)
                    {
                        this.ActiveWayPoints.Dequeue();
                        if (this.OrderQueue.Count > 0)
                        {
                            this.OrderQueue.RemoveFirst();
                        }
                    }
                }
                //if (this.ColonizeTarget != null )
                //{
                //    lock (this.wayPointLocker)
                //    {

                //        if (this.OrderQueue.Where(cgoal => cgoal.Plan == Plan.MoveToWithin1000).Count() == 1)
                //        {
                //            this.ActiveWayPoints.First().Equals(this.ColonizeTarget.Position);
                //            this.OrderQueue.First().MovePosition = this.ColonizeTarget.Position;
                //        }
                //    }
                //}
            }
            else if (single <= 1500f)
            {
                lock (this.wayPointLocker)
                {
                    this.ActiveWayPoints.Dequeue();
                    if (this.OrderQueue.Count > 0)
                    {
                        this.OrderQueue.RemoveFirst();
                    }
                }
            }
            //else if (this.ColonizeTarget != null)
            //{
            //    lock (this.wayPointLocker)
            //    {
            //        this.ActiveWayPoints.First().Equals(this.ColonizeTarget.Position);
            //    }
            //}
        }
        private void MoveToWithin1000Fleet(float elapsedTime, ArtificialIntelligence.ShipGoal goal)
        {
            float Distance = Vector2.Distance(this.Owner.Center, goal.fleet.Position + this.Owner.FleetOffset);
            float speedLimit = goal.SpeedLimit;
            if (this.Owner.velocityMaximum >= Distance)
            {
                speedLimit = Distance;
            }

            if (Distance > 10000f)
            {
                this.Owner.EngageStarDrive();
            }
            else if (Distance < 1000f)
            {
                this.Owner.HyperspaceReturn();
                this.OrderQueue.RemoveFirst();
                return;
            }
            this.MoveTowardsPosition(goal.fleet.Position + this.Owner.FleetOffset, elapsedTime, speedLimit);
        }
        private void StopWithBackwardsThrustbroke(float elapsedTime, ArtificialIntelligence.ShipGoal Goal)
        {
            if (this.Owner.loyalty == EmpireManager.GetEmpireByName(ArtificialIntelligence.universeScreen.PlayerLoyalty))
            {
                this.HadPO = true;
            }
            this.HasPriorityOrder = false;
            float Distance = Vector2.Distance(this.Owner.Center, Goal.MovePosition);
            if (Distance < 200 )//&& Distance > 25f)
            {
                this.OrderQueue.RemoveFirst();
                lock (this.wayPointLocker)
                {
                    this.ActiveWayPoints.Clear();
                }
                this.Owner.Velocity = Vector2.Zero;
                if (this.Owner.loyalty == EmpireManager.GetEmpireByName(ArtificialIntelligence.universeScreen.PlayerLoyalty))
                {
                    this.HadPO = true;
                }
                this.HasPriorityOrder = false;
            }
            this.Owner.HyperspaceReturn();
            Vector2 forward = new Vector2((float)Math.Sin((double)this.Owner.Rotation), -(float)Math.Cos((double)this.Owner.Rotation));
            if (this.Owner.Velocity == Vector2.Zero || Vector2.Distance(this.Owner.Center + ( this.Owner.Velocity * elapsedTime), Goal.MovePosition) > Vector2.Distance(this.Owner.Center, Goal.MovePosition))
            {
                this.Owner.Velocity = Vector2.Zero;
                this.OrderQueue.RemoveFirst();
                if (this.ActiveWayPoints.Count > 0)
                {
                    lock (this.wayPointLocker)
                    {
                        this.ActiveWayPoints.Dequeue();
                    }
                }
                return;
            }
            Vector2 velocity = this.Owner.Velocity;
            float timetostop = (int)velocity.Length() / Goal.SpeedLimit;
            if (Vector2.Distance(this.Owner.Center, Goal.MovePosition) / Goal.SpeedLimit <= timetostop + .005) //(this.Owner.Velocity.Length() + 1)
                if (Math.Abs((int)(DistanceLast - Distance)) < 10)
                {

                    ArtificialIntelligence.ShipGoal to1k = new ArtificialIntelligence.ShipGoal(ArtificialIntelligence.Plan.MakeFinalApproach, Goal.MovePosition, 0f)
                                    {
                                        SpeedLimit = this.Owner.speed > Distance ? Distance : this.Owner.GetSTLSpeed()
                                    };
                    lock (this.wayPointLocker)
                        this.OrderQueue.AddFirst(to1k);
                    this.DistanceLast = Distance;
                    return;
                }
            if (Vector2.Distance(this.Owner.Center, Goal.MovePosition) / (this.Owner.Velocity.Length() + 0.001f) <= timetostop)
            {
                Ship owner = this.Owner;
                owner.Velocity = owner.Velocity + (Vector2.Normalize(-forward) * (elapsedTime * Goal.SpeedLimit));
                if (this.Owner.Velocity.Length() > Goal.SpeedLimit)
                {
                    this.Owner.Velocity = Vector2.Normalize(this.Owner.Velocity) * Goal.SpeedLimit;
                }
            }
            else
            {
                Ship ship = this.Owner;
                ship.Velocity = ship.Velocity + (Vector2.Normalize(forward) * (elapsedTime * Goal.SpeedLimit));
                if (this.Owner.Velocity.Length() > Goal.SpeedLimit)
                {
                    this.Owner.Velocity = Vector2.Normalize(this.Owner.Velocity) * Goal.SpeedLimit;
                    return;
                }
            }

            this.DistanceLast = Distance;
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    if (this.projectiles != null)
                        this.projectiles.Dispose();
                    if (this.beams != null)
                        this.beams.Dispose();
                    if (this.supplyLock != null)
                        this.supplyLock.Dispose();
                    if (this.AI != null)
                        this.AI.Dispose();
                    if (this.ProjectilesFired != null)
                        this.ProjectilesFired.Dispose();

                }
                this.projectiles = null;
                this.beams = null;
                this.supplyLock = null;
                this.AI = null;
                this.ProjectilesFired = null;
                this.disposed = true;
            }
        }
 private void DoSupplyShip(float elapsedTime, ArtificialIntelligence.ShipGoal goal)
 {
     if (this.EscortTarget == null || !this.EscortTarget.Active)
     {
         this.OrderQueue.Clear();
         this.OrderReturnToHangar();
         return;
     }
     this.ThrustTowardsPosition(this.EscortTarget.Center, elapsedTime, this.Owner.speed);
     if (Vector2.Distance(this.Owner.Center, this.EscortTarget.Center) < this.EscortTarget.Radius + 300f)
     {
         Ship escortTarget = this.EscortTarget;
         escortTarget.Ordinance = escortTarget.Ordinance + goal.VariableNumber;
         if (this.EscortTarget.Ordinance > this.EscortTarget.OrdinanceMax)
         {
             this.EscortTarget.Ordinance = this.EscortTarget.OrdinanceMax;
         }
         this.OrderQueue.Clear();
         this.Owner.QueueTotalRemoval();
         if (this.Owner.Mothership != null)
         {
             foreach (ShipModule hangar in this.Owner.Mothership.GetHangars())
             {
                 if (hangar.GetHangarShip() != this.Owner)
                 {
                     continue;
                 }
                 hangar.hangarTimer = 1f;
             }
         }
     }
 }
 private void Stop(float elapsedTime, ArtificialIntelligence.ShipGoal Goal)
 {
     this.Owner.HyperspaceReturn();
     if (this.Owner.Velocity == Vector2.Zero || this.Owner.Velocity.Length() > this.Owner.VelocityLast.Length())
     {
         this.Owner.Velocity = Vector2.Zero;
         this.OrderQueue.RemoveFirst();
         return;
     }
     Vector2 forward = new Vector2((float)Math.Sin((double)this.Owner.Rotation), -(float)Math.Cos((double)this.Owner.Rotation));
     if (this.Owner.Velocity.Length() / this.Owner.velocityMaximum <= elapsedTime || (forward.X <= 0f || this.Owner.Velocity.X <= 0f) && (forward.X >= 0f || this.Owner.Velocity.X >= 0f))
     {
         this.Owner.Velocity = Vector2.Zero;
         return;
     }
     Ship owner = this.Owner;
     owner.Velocity = owner.Velocity + (Vector2.Normalize(-forward) * (elapsedTime * this.Owner.velocityMaximum));
 }
        //added by gremlin refit while in fleet
        private void DoRefit(float elapsedTime, ArtificialIntelligence.ShipGoal goal)
        {
            QueueItem qi = new QueueItem()
            {
                isShip = true,
                productionTowards = 0f,
                sData = ResourceManager.ShipsDict[goal.VariableString].GetShipData()
            };

            if (qi.sData == null)
            {
                this.OrderQueue.Clear();
                this.State = AIState.AwaitingOrders;
            }
            int cost = (int)(ResourceManager.ShipsDict[goal.VariableString].GetCost(this.Owner.loyalty) - this.Owner.GetCost(this.Owner.loyalty));
            if (cost < 0)
            {
                cost = 0;
            }
            cost = cost + 10 * (int)UniverseScreen.GamePaceStatic;
            qi.Cost = (float)cost;
            qi.isRefit = true;
            //Added by McShooterz: refit keeps name and level
            if(this.Owner.VanityName != this.Owner.Name)
                qi.RefitName = this.Owner.VanityName;
            qi.sData.Level = (byte)this.Owner.Level;
            if (this.Owner.fleet != null)
            {

                FleetDataNode node = this.Owner.fleet.DataNodes.Where(thenode => thenode.GetShip() == this.Owner).First();

                Goal refitgoal = new Goal
                {
                    beingBuilt = ResourceManager.ShipsDict[goal.VariableString],

                    GoalName = "FleetRequisition",

                };
                refitgoal.Step = 1;
                refitgoal.beingBuilt.fleet = this.Owner.fleet;
                refitgoal.beingBuilt.RelativeFleetOffset = node.FleetOffset;
                node.GoalGUID = refitgoal.guid;
                refitgoal.SetFleet(this.Owner.fleet);
                refitgoal.SetPlanetWhereBuilding(this.OrbitTarget);

                this.Owner.loyalty.GetGSAI().Goals.Add(refitgoal);

                qi.Goal = refitgoal;
            }
            this.OrbitTarget.ConstructionQueue.Add(qi);
            this.Owner.QueueTotalRemoval();
        }
 private void DoRefitORIG(float elapsedTime, ArtificialIntelligence.ShipGoal goal)
 {
     QueueItem qi = new QueueItem()
     {
         isShip = true,
         productionTowards = 0f,
         sData = ResourceManager.ShipsDict[goal.VariableString].GetShipData()
     };
     if (qi.sData == null)
     {
         this.OrderQueue.Clear();
         this.State = AIState.AwaitingOrders;
     }
     int cost = (int)(ResourceManager.ShipsDict[goal.VariableString].GetCost(this.Owner.loyalty) - this.Owner.GetCost(this.Owner.loyalty));
     if (cost < 0)
     {
         cost = 0;
     }
     cost = cost + 10 * (int)UniverseScreen.GamePaceStatic;
     qi.Cost = (float)cost;
     qi.isRefit = true;
     this.OrbitTarget.ConstructionQueue.Add(qi);
     this.Owner.QueueTotalRemoval();
 }
 private void DoRebase(ArtificialIntelligence.ShipGoal Goal)
 {
     if (this.Owner.TroopList.Count == 0)
     {
         this.Owner.QueueTotalRemoval();
     }
     else if (Goal.TargetPlanet.AssignTroopToTile(this.Owner.TroopList[0]))
     {
         this.Owner.TroopList.Clear();
         this.Owner.QueueTotalRemoval();
         return;
     }
 }
        private void DoLandTroop(float elapsedTime, ArtificialIntelligence.ShipGoal goal)
        {
            this.DoOrbit(goal.TargetPlanet, elapsedTime);
            //added by gremlin.

            if (this.Owner.Role == "troop" && this.Owner.TroopList.Count > 0 )
            {

                if (Vector2.Distance(goal.TargetPlanet.Position, this.Owner.Center) < 3500f  && goal.TargetPlanet.AssignTroopToTile(this.Owner.TroopList[0]))
                {//Vector2.Distance(goal.TargetPlanet.Position, this.Owner.Center) < 3500f
                    {
                       // GlobalStats.UILocker.EnterWriteLock();
                        this.Owner.QueueTotalRemoval();
                        //if (Ship.universeScreen.SelectedShip == this.Owner)
                        //    Ship.universeScreen.SelectedShip = null;
                        //GlobalStats.UILocker.EnterWriteLock();
                    }
                    return;
                }
            }
            else if (this.Owner.loyalty == goal.TargetPlanet.Owner || goal.TargetPlanet.GetGroundLandingSpots() == 0 || this.Owner.TroopList.Count <= 0 || (this.Owner.Role != "troop" && (this.Owner.GetHangars().Where(hangar => hangar.hangarTimer <= 0 && hangar.IsTroopBay).Count() == 0&& !this.Owner.hasTransporter)))//|| goal.TargetPlanet.GetGroundStrength(this.Owner.loyalty)+3 > goal.TargetPlanet.GetGroundStrength(goal.TargetPlanet.Owner)*1.5)
            {
                if (this.Owner.loyalty == EmpireManager.GetEmpireByName(ArtificialIntelligence.universeScreen.PlayerLoyalty))
                {
                    this.HadPO = true;
                }
                this.HasPriorityOrder = false;
                this.State = this.DefaultAIState;
                this.OrderQueue.Clear();
                System.Diagnostics.Debug.WriteLine("Do Land Troop: Troop Assault Canceled");
            }
            else if (Vector2.Distance(goal.TargetPlanet.Position, this.Owner.Center) < 3500f)
            {
                List<Troop> ToRemove = new List<Troop>();
                //if (Vector2.Distance(goal.TargetPlanet.Position, this.Owner.Center) < 3500f)
                {
                    //Get limit of troops to land
                    int LandLimit = this.Owner.GetHangars().Where(hangar => hangar.hangarTimer <= 0 && hangar.IsTroopBay).Count();
                    foreach (ShipModule module in this.Owner.Transporters.Where(module => module.TransporterTimer <= 1f))
                        LandLimit += module.TransporterTroopLanding;
                    //Land troops
                    foreach (Troop troop in this.Owner.TroopList)
                    {
                        if (troop == null || troop.GetOwner() != this.Owner.loyalty)
                            continue;
                        if (goal.TargetPlanet.AssignTroopToTile(troop))
                        {
                            ToRemove.Add(troop);
                            LandLimit--;
                            if (LandLimit < 1)
                                break;
                        }
                        else
                            break;
                    }
                    //Clear out Troops
                    if (ToRemove.Count > 0)
                    {
                        bool flag; // = false;
                        foreach (Troop RemoveTroop in ToRemove)
                        {
                            flag = false;
                            foreach (ShipModule module in this.Owner.GetHangars())
                            {
                                if (module.hangarTimer < module.hangarTimerConstant)
                                {
                                    module.hangarTimer = module.hangarTimerConstant;
                                    flag = true;
                                    break;
                                }
                            }
                            if (flag)
                                continue;
                            foreach (ShipModule module in this.Owner.Transporters)
                                if (module.TransporterTimer < module.TransporterTimerConstant)
                                {
                                    module.TransporterTimer = module.TransporterTimerConstant;
                                    flag = true;
                                    break;
                                }
                        }
                                //module.TransporterTimer = module.TransporterTimerConstant;
                            foreach (Troop to in ToRemove)
                                this.Owner.TroopList.Remove(to);

                    }
                }
            }
        }
 private void DoDeploy(ArtificialIntelligence.ShipGoal shipgoal)
 {
     if (shipgoal.goal == null)
     {
         return;
     }
     if (shipgoal.goal.TetherTarget != Guid.Empty && Vector2.Distance(ArtificialIntelligence.universeScreen.PlanetsDict[shipgoal.goal.TetherTarget].Position + shipgoal.goal.TetherOffset, this.Owner.Center) > 200f)
     {
         shipgoal.goal.BuildPosition = ArtificialIntelligence.universeScreen.PlanetsDict[shipgoal.goal.TetherTarget].Position + shipgoal.goal.TetherOffset;
         this.OrderDeepSpaceBuild(shipgoal.goal);
         return;
     }
     Ship platform = ResourceManager.CreateShipAtPoint(shipgoal.goal.ToBuildUID, this.Owner.loyalty, shipgoal.goal.BuildPosition);
     if (platform == null)
     {
         return;
     }
     foreach (SpaceRoad road in this.Owner.loyalty.SpaceRoadsList)
     {
         foreach (RoadNode node in road.RoadNodesList)
         {
             if (node.Position != shipgoal.goal.BuildPosition)
             {
                 continue;
             }
             node.Platform = platform;
             if (!StatTracker.SnapshotsDict.ContainsKey(ArtificialIntelligence.universeScreen.StarDate.ToString("#.0")))
             {
                 continue;
             }
             NRO nro = new NRO()
             {
                 Node = node.Position,
                 Radius = 300000f,
                 StarDateMade = ArtificialIntelligence.universeScreen.StarDate
             };
             StatTracker.SnapshotsDict[ArtificialIntelligence.universeScreen.StarDate.ToString("#.0")][EmpireManager.EmpireList.IndexOf(this.Owner.loyalty)].EmpireNodes.Add(nro);
         }
     }
     if (shipgoal.goal.TetherTarget != Guid.Empty)
     {
         platform.TetherToPlanet(ArtificialIntelligence.universeScreen.PlanetsDict[shipgoal.goal.TetherTarget]);
         platform.TetherOffset = shipgoal.goal.TetherOffset;
     }
     this.Owner.loyalty.GetGSAI().Goals.Remove(shipgoal.goal);
     this.Owner.QueueTotalRemoval();
 }
        private bool RotateToFaceMovePosition(float elapsedTime, ArtificialIntelligence.ShipGoal goal)
        {
            bool turned = false;
            Vector2 forward = new Vector2((float)Math.Sin((double)this.Owner.Rotation), -(float)Math.Cos((double)this.Owner.Rotation));
            Vector2 right = new Vector2(-forward.Y, forward.X);
            Vector2 VectorToTarget = HelperFunctions.FindVectorToTarget(this.Owner.Center, goal.MovePosition);
            float angleDiff = (float)Math.Acos((double)Vector2.Dot(VectorToTarget, forward));
            if (angleDiff > 0.2f)
            {
                this.Owner.HyperspaceReturn();
                this.RotateToFacing(elapsedTime, angleDiff, (Vector2.Dot(VectorToTarget, right) > 0f ? 1f : -1f));
                turned = true;
            }
            else if (this.OrderQueue.Count > 0)
            {
                this.OrderQueue.RemoveFirst();

            }
            return turned;
        }
 private void MakeFinalApproach(float elapsedTime, ArtificialIntelligence.ShipGoal Goal)
 {
     if (Goal.TargetPlanet != null)
     {
         lock (this.wayPointLocker)
         {
             this.ActiveWayPoints.Last().Equals(Goal.TargetPlanet.Position);
             Goal.MovePosition = Goal.TargetPlanet.Position;
         }
     }
     //if (this.RotateToFaceMovePosition(elapsedTime, Goal.MovePosition))
     //{
     //    Goal.SpeedLimit *= .9f;
     //}
     //else
     //{
     //    Goal.SpeedLimit *= 1.1f;
     //    if (this.Owner.engineState == Ship.MoveState.Sublight)
     //    {
     //        if (Goal.SpeedLimit > this.Owner.GetSTLSpeed())
     //            Goal.SpeedLimit = this.Owner.GetSTLSpeed();
     //    }
     //    else if (Goal.SpeedLimit > this.Owner.GetFTLSpeed())
     //        Goal.SpeedLimit = this.Owner.GetFTLSpeed();
     //}
     this.Owner.HyperspaceReturn();
     Vector2 velocity = this.Owner.Velocity;
     if (Goal.TargetPlanet != null)
         velocity += Goal.TargetPlanet.Position;
     float timetostop = velocity.Length() / Goal.SpeedLimit;
     float Distance = Vector2.Distance(this.Owner.Center, Goal.MovePosition);
     if (Distance / (Goal.SpeedLimit + 0.001f) <= timetostop)
     {
         this.OrderQueue.RemoveFirst();
     }
     else
     {
         if (DistanceLast == Distance)
             Goal.SpeedLimit++;
         this.ThrustTowardsPosition(Goal.MovePosition, elapsedTime, Goal.SpeedLimit);
     }
     this.DistanceLast = Distance;
 }
 private void ScrapShip(float elapsedTime, ArtificialIntelligence.ShipGoal goal)
 {
     if (Vector2.Distance(goal.TargetPlanet.Position, this.Owner.Center) >= 2500f +goal.TargetPlanet.ObjectRadius + this.Owner.Radius)   //2500f)   //OrbitTarget.ObjectRadius *15)
     {
         //goal.MovePosition = goal.TargetPlanet.Position;
         //this.MoveToWithin1000(elapsedTime, goal);
         //goal.SpeedLimit = this.Owner.GetSTLSpeed();
         this.DoOrbit(goal.TargetPlanet, elapsedTime);
         return;
     }
     this.OrderQueue.Clear();
     Planet targetPlanet = goal.TargetPlanet;
     targetPlanet.ProductionHere = targetPlanet.ProductionHere + this.Owner.GetCost(this.Owner.loyalty) / 2f;
     this.Owner.QueueTotalRemoval();
     this.Owner.loyalty.GetGSAI().recyclepool++;
 }
        //added by gremlin Deveksmod MakeFinalApproach
        private void MakeFinalApproachDev(float elapsedTime, ArtificialIntelligence.ShipGoal Goal)
        {
            float speedLimit = (int)Goal.SpeedLimit;

            this.Owner.HyperspaceReturn();
            Vector2 velocity = this.Owner.Velocity;
            float Distance = Vector2.Distance(this.Owner.Center, Goal.MovePosition);
            double timetostop;

            timetostop = (double)velocity.Length() / speedLimit;

            //if(this.RotateToFaceMovePosition(elapsedTime, Goal))
            //{
            //    speedLimit--;
            //}
            //else
            //{
            //    speedLimit++;
            //    if(speedLimit > this.Owner.GetSTLSpeed())
            //        speedLimit=this.Owner.GetSTLSpeed();
            //}

            //ShipGoal preserveGoal = this.OrderQueue.Last();

            //if ((preserveGoal.TargetPlanet != null && this.Owner.fleet == null && Vector2.Distance(preserveGoal.TargetPlanet.Position, this.Owner.Center) > 7500) || this.DistanceLast == Distance)
            //{

            //    this.OrderQueue.Clear();
            //    this.OrderQueue.AddFirst(preserveGoal);
            //    return;
            //}

            if ((double)Distance / velocity.Length() <= timetostop)  //+ .005f) //(Distance  / (velocity.Length() ) <= timetostop)//
            {
                this.OrderQueue.RemoveFirst();
            }
            else
            {
                Goal.SpeedLimit = speedLimit;

                this.ThrustTowardsPosition(Goal.MovePosition, elapsedTime, speedLimit);
            }
            this.DistanceLast = Distance;
        }
 private void StopWithBackwardsThrust(float elapsedTime, ArtificialIntelligence.ShipGoal Goal)
 {
     if(Goal.TargetPlanet !=null)
     {
         lock (this.wayPointLocker)
         {
             this.ActiveWayPoints.Last().Equals(Goal.TargetPlanet.Position);
             Goal.MovePosition = Goal.TargetPlanet.Position;
         }
     }
     if (this.Owner.loyalty == EmpireManager.GetEmpireByName(ArtificialIntelligence.universeScreen.PlayerLoyalty))
     {
         this.HadPO = true;
     }
     this.HasPriorityOrder = false;
     float Distance = Vector2.Distance(this.Owner.Center, Goal.MovePosition);
     if (Distance < 100f && Distance < 25f)
     {
         this.OrderQueue.RemoveFirst();
         lock (this.wayPointLocker)
         {
             this.ActiveWayPoints.Clear();
         }
         this.Owner.Velocity = Vector2.Zero;
         if (this.Owner.loyalty == EmpireManager.GetEmpireByName(ArtificialIntelligence.universeScreen.PlayerLoyalty))
         {
             this.HadPO = true;
         }
         this.HasPriorityOrder = false;
     }
     this.Owner.HyperspaceReturn();
     //Vector2 forward2 = Quaternion
     //Quaternion.AngleAxis(_angle, Vector3.forward) * normalizedDirection1
     Vector2 forward = new Vector2((float)Math.Sin((double)this.Owner.Rotation), -(float)Math.Cos((double)this.Owner.Rotation));
     if (this.Owner.Velocity == Vector2.Zero || Vector2.Distance(this.Owner.Center + (this.Owner.Velocity * elapsedTime), Goal.MovePosition) > Vector2.Distance(this.Owner.Center, Goal.MovePosition))
     {
         this.Owner.Velocity = Vector2.Zero;
         this.OrderQueue.RemoveFirst();
         if (this.ActiveWayPoints.Count > 0)
         {
             lock (this.wayPointLocker)
             {
                 this.ActiveWayPoints.Dequeue();
             }
         }
         return;
     }
     Vector2 velocity = this.Owner.Velocity;
     float timetostop = velocity.Length() / Goal.SpeedLimit;
     //added by gremlin devekmod timetostopfix
     if (Vector2.Distance(this.Owner.Center, Goal.MovePosition) / Goal.SpeedLimit <= timetostop + .005)
     //if (Vector2.Distance(this.Owner.Center, Goal.MovePosition) / (this.Owner.Velocity.Length() + 0.001f) <= timetostop)
     {
         Ship owner = this.Owner;
         owner.Velocity = owner.Velocity + (Vector2.Normalize(forward) * (elapsedTime * Goal.SpeedLimit));
         if (this.Owner.Velocity.Length() > Goal.SpeedLimit)
         {
             this.Owner.Velocity = Vector2.Normalize(this.Owner.Velocity) * Goal.SpeedLimit;
         }
     }
     else
     {
         Ship ship = this.Owner;
         ship.Velocity = ship.Velocity + (Vector2.Normalize(forward) * (elapsedTime * Goal.SpeedLimit));
         if (this.Owner.Velocity.Length() > Goal.SpeedLimit)
         {
             this.Owner.Velocity = Vector2.Normalize(this.Owner.Velocity) * Goal.SpeedLimit;
             return;
         }
     }
 }
 private void MakeFinalApproachFleet(float elapsedTime, ArtificialIntelligence.ShipGoal Goal)
 {
     float Distance = Vector2.Distance(this.Owner.Center, Goal.fleet.Position + this.Owner.FleetOffset);
     if (Distance < 100f || this.DistanceLast > Distance)
     {
         this.OrderQueue.RemoveFirst();
     }
     else
     {
         this.MoveTowardsPosition(Goal.fleet.Position + this.Owner.FleetOffset, elapsedTime, Goal.fleet.speed);
     }
     this.DistanceLast = Distance;
 }
 public ShipGoal(ArtificialIntelligence.Plan p, Vector2 pos, float facing)
 {
     this.Plan = p;
     this.MovePosition = pos;
     this.DesiredFacing = facing;
 }
 public void InitializeAI()
 {
     this.AI = new ArtificialIntelligence(this);
     this.AI.State = AIState.AwaitingOrders;
     if (this.shipData == null)
         return;
     this.AI.CombatState = this.shipData.CombatState;
     this.AI.CombatAI = new CombatAI(this);
 }