Пример #1
0
        /// <summary>
        /// Creates a path from player's current location to destination and
        /// returns minutes taken to travel.
        /// </summary>
        /// <param name="endPos">Endpoint in map pixel coordinates.</param>
        public int CalculateTravelTime(DFPosition endPos,
                                       bool speedCautious = false,
                                       bool sleepModeInn  = false,
                                       bool travelShip    = false,
                                       bool hasHorse      = false,
                                       bool hasCart       = false)
        {
            int transportModifier = 0;

            if (hasHorse)
            {
                transportModifier = 128;
            }
            else if (hasCart)
            {
                transportModifier = 192;
            }
            else
            {
                transportModifier = 256;
            }

            int playerXMapPixel         = GameManager.Instance.PlayerGPS.CurrentMapPixel.X;
            int playerYMapPixel         = GameManager.Instance.PlayerGPS.CurrentMapPixel.Y;
            int distanceXMapPixels      = endPos.X - playerXMapPixel;
            int distanceYMapPixels      = endPos.Y - playerYMapPixel;
            int distanceXMapPixelsAbs   = Mathf.Abs(distanceXMapPixels);
            int distanceYMapPixelsAbs   = Mathf.Abs(distanceYMapPixels);
            int furthestOfXandYDistance = 0;

            if (distanceXMapPixelsAbs <= distanceYMapPixelsAbs)
            {
                furthestOfXandYDistance = distanceYMapPixelsAbs;
            }
            else
            {
                furthestOfXandYDistance = distanceXMapPixelsAbs;
            }

            int xPixelMovementDirection;
            int yPixelMovementDirection;

            if (distanceXMapPixels >= 0)
            {
                xPixelMovementDirection = 1;
            }
            else
            {
                xPixelMovementDirection = -1;
            }

            if (distanceYMapPixels >= 0)
            {
                yPixelMovementDirection = 1;
            }
            else
            {
                yPixelMovementDirection = -1;
            }

            int numberOfMovements = 0;
            int shorterOfXandYDistanceIncrementer = 0;

            int minutesTakenThisMove = 0;
            int minutesTakenTotal    = 0;

            MapsFile mapsFile = DaggerfallUnity.Instance.ContentReader.MapFileReader;

            pixelsTraveledOnOcean = 0;

            while (numberOfMovements < furthestOfXandYDistance)
            {
                if (furthestOfXandYDistance == distanceXMapPixelsAbs)
                {
                    playerXMapPixel += xPixelMovementDirection;
                    shorterOfXandYDistanceIncrementer += distanceYMapPixelsAbs;

                    if (shorterOfXandYDistanceIncrementer > distanceXMapPixelsAbs)
                    {
                        shorterOfXandYDistanceIncrementer -= distanceXMapPixelsAbs;
                        playerYMapPixel += yPixelMovementDirection;
                    }
                }
                else
                {
                    playerYMapPixel += yPixelMovementDirection;
                    shorterOfXandYDistanceIncrementer += distanceXMapPixelsAbs;

                    if (shorterOfXandYDistanceIncrementer > distanceYMapPixelsAbs)
                    {
                        shorterOfXandYDistanceIncrementer -= distanceYMapPixelsAbs;
                        playerXMapPixel += xPixelMovementDirection;
                    }
                }

                int terrainMovementIndex = 0;
                int terrain = mapsFile.GetClimateIndex(playerXMapPixel, playerYMapPixel);
                if (terrain == (int)MapsFile.Climates.Ocean)
                {
                    ++pixelsTraveledOnOcean;
                    if (travelShip)
                    {
                        minutesTakenThisMove = 51;
                    }
                    else
                    {
                        minutesTakenThisMove = 255;
                    }
                }
                else
                {
                    terrainMovementIndex = climateIndices[terrain - (int)MapsFile.Climates.Ocean];
                    minutesTakenThisMove = (((102 * transportModifier) >> 8)
                                            * (256 - terrainMovementModifiers[terrainMovementIndex] + 256)) >> 8;
                }

                if (!sleepModeInn)
                {
                    minutesTakenThisMove = (300 * minutesTakenThisMove) >> 8;
                }
                minutesTakenTotal += minutesTakenThisMove;
                ++numberOfMovements;
            }

            if (!speedCautious)
            {
                minutesTakenTotal = minutesTakenTotal >> 1;
            }

            return(minutesTakenTotal);
        }