Example #1
0
        public static void StartNonNewtTranslation(Entity entity)
        {
            var moveDB       = entity.GetDataBlob <TranslateMoveDB>();
            var propulsionDB = entity.GetDataBlob <PropulsionAbilityDB>();
            var positionDB   = entity.GetDataBlob <PositionDB>();
            var maxSpeedMS   = propulsionDB.MaximumSpeed_MS;

            positionDB.SetParent(null);
            Vector3 targetPosMt       = Distance.AuToMt(moveDB.TranslateExitPoint_AU);
            Vector3 currentPositionMt = Distance.AuToMt(positionDB.AbsolutePosition_AU);

            Vector3 postionDelta  = currentPositionMt - targetPosMt;
            double  totalDistance = postionDelta.Length();

            double maxKMeters        = ShipMovementProcessor.CalcMaxFuelDistance_KM(entity);
            double fuelMaxDistanceMt = maxKMeters * 1000;

            if (fuelMaxDistanceMt >= totalDistance)
            {
                var currentVelocityMS = Vector3.Normalise(targetPosMt - currentPositionMt) * maxSpeedMS;
                propulsionDB.CurrentVectorMS       = currentVelocityMS;
                moveDB.CurrentNonNewtonionVectorMS = currentVelocityMS;
                moveDB.LastProcessDateTime         = entity.Manager.ManagerSubpulses.StarSysDateTime;

                CargoStorageDB storedResources = entity.GetDataBlob <CargoStorageDB>();
                foreach (var item in propulsionDB.FuelUsePerKM)
                {
                    var fuel = staticData.GetICargoable(item.Key);
                    StorageSpaceProcessor.RemoveCargo(storedResources, fuel, (long)(item.Value * totalDistance / 1000));
                }
            }
        }
Example #2
0
 /// <summary>
 /// consumes resources in the stockpile, and updates the dictionary.
 /// </summary>
 /// <param name="stockpile"></param>
 /// <param name="toUse"></param>
 private static void ConsumeResources(CargoStorageDB fromCargo, IDictionary <Guid, int> toUse)
 {
     foreach (KeyValuePair <Guid, int> kvp in toUse.ToArray())
     {
         ICargoable cargoItem          = fromCargo.OwningEntity.Manager.Game.StaticData.GetICargoable(kvp.Key);
         Guid       cargoTypeID        = cargoItem.CargoTypeID;
         int        amountUsedThisTick = 0;
         if (fromCargo.StoredCargoTypes.ContainsKey(cargoTypeID))
         {
             if (fromCargo.StoredCargoTypes[cargoTypeID].ItemsAndAmounts.ContainsKey(cargoItem.ID))
             {
                 if (fromCargo.StoredCargoTypes[cargoTypeID].ItemsAndAmounts[cargoItem.ID] >= kvp.Value)
                 {
                     amountUsedThisTick = kvp.Value;
                 }
                 else
                 {
                     amountUsedThisTick = (int)fromCargo.StoredCargoTypes[cargoTypeID].ItemsAndAmounts[cargoItem.ID];
                 }
             }
         }
         StorageSpaceProcessor.RemoveCargo(fromCargo, cargoItem, amountUsedThisTick);
         toUse[kvp.Key] -= amountUsedThisTick;
     }
 }
        public void ProcessEntity(Entity entity, int deltaSeconds)
        {
            var manager      = entity.Manager;
            var moveDB       = entity.GetDataBlob <TranslateMoveDB>();
            var propulsionDB = entity.GetDataBlob <PropulsionDB>();
            //var currentVector = propulsionDB.CurrentSpeed;
            var maxSpeed        = propulsionDB.MaximumSpeed;
            var positionDB      = entity.GetDataBlob <PositionDB>();
            var currentPosition = positionDB.AbsolutePosition;
            //targetPosition taking the range (how close we want to get) into account.
            var targetPos = moveDB.TargetPosition * (1 - (moveDB.MoveRangeInKM / GameConstants.Units.KmPerAu) / moveDB.TargetPosition.Length());

            var deltaVecToTarget = currentPosition - targetPos;


            var currentSpeed = GMath.GetVector(currentPosition, targetPos, maxSpeed);

            propulsionDB.CurrentVector = currentSpeed;
            moveDB.CurrentVector       = currentSpeed;
            StaticDataStore           staticData      = entity.Manager.Game.StaticData;
            CargoStorageDB            storedResources = entity.GetDataBlob <CargoStorageDB>();
            Dictionary <Guid, double> fuelUsePerMeter = propulsionDB.FuelUsePerKM;
            double maxKMeters = ShipMovementProcessor.CalcMaxFuelDistance(entity);

            var nextTPos = currentPosition + (currentSpeed * deltaSeconds);


            var distanceToTargetAU = deltaVecToTarget.Length();  //in au

            var deltaVecToNextT   = currentPosition - nextTPos;
            var fuelMaxDistanceAU = maxKMeters / GameConstants.Units.KmPerAu;



            Vector4 newPos = currentPosition;

            double distanceToNextTPos = deltaVecToNextT.Length();
            double distanceToMove;

            if (fuelMaxDistanceAU < distanceToNextTPos)
            {
                distanceToMove = fuelMaxDistanceAU;
                double percent = fuelMaxDistanceAU / distanceToNextTPos;
                newPos = nextTPos + deltaVecToNextT * percent;
                Event usedAllFuel = new Event(manager.ManagerSubpulses.SystemLocalDateTime, "Used all Fuel", entity.GetDataBlob <OwnedDB>().OwnedByFaction, entity);
                usedAllFuel.EventType = EventType.FuelExhausted;
                manager.Game.EventLog.AddEvent(usedAllFuel);
            }
            else
            {
                distanceToMove = distanceToNextTPos;
                newPos         = nextTPos;
            }



            if (distanceToTargetAU < distanceToMove) // moving would overtake target, just go directly to target
            {
                distanceToMove             = distanceToTargetAU;
                propulsionDB.CurrentVector = new Vector4(0, 0, 0, 0);
                newPos            = targetPos;
                moveDB.IsAtTarget = true;
                entity.RemoveDataBlob <TranslateMoveDB>();
            }

            positionDB.AbsolutePosition = newPos;
            int kMetersMoved = (int)(distanceToMove * GameConstants.Units.KmPerAu);

            foreach (var item in propulsionDB.FuelUsePerKM)
            {
                var fuel = staticData.GetICargoable(item.Key);
                StorageSpaceProcessor.RemoveCargo(storedResources, fuel, (long)(item.Value * kMetersMoved));
            }
        }