Example #1
0
        /// <summary>
        /// Used to update information affecting the SkyMesh
        /// </summary>
        public void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
        {
            // Adjust dome position so the bottom edge is not visible
            Vector3 ViewerXNAPosition   = new Vector3(MSTSSkyViewer.Camera.Location.X, MSTSSkyViewer.Camera.Location.Y - 100, -MSTSSkyViewer.Camera.Location.Z);
            Matrix  XNASkyWorldLocation = Matrix.CreateTranslation(ViewerXNAPosition);

            if (mstsskyworldLoc == null)
            {
                // First time around, initialize the following items:
                mstsskyworldLoc     = new WorldLatLon();
                mstsskyoldClockTime = MSTSSkyViewer.Simulator.ClockTime % 86400;
                while (mstsskyoldClockTime < 0)
                {
                    mstsskyoldClockTime += 86400;
                }
                step1 = step2 = (int)(mstsskyoldClockTime / 1200);
                step2 = step2 < maxSteps - 1 ? step2 + 1 : 0; // limit to max. steps in case activity starts near midnight
                // Get the current latitude and longitude coordinates
                mstsskyworldLoc.ConvertWTC(MSTSSkyViewer.Camera.TileX, MSTSSkyViewer.Camera.TileZ, MSTSSkyViewer.Camera.Location, ref mstsskylatitude, ref mstsskylongitude);
                if (mstsskyseasonType != (int)MSTSSkyViewer.Simulator.Season)
                {
                    mstsskyseasonType = (int)MSTSSkyViewer.Simulator.Season;
                    date.ordinalDate  = mstsskylatitude >= 0 ? 82 + mstsskyseasonType * 91 : (82 + (mstsskyseasonType + 2) * 91) % 365;
                    // TODO: Set the following three externally from ORTS route files (future)
                    date.month = 1 + date.ordinalDate / 30;
                    date.day   = 21;
                    date.year  = 2017;
                }
                // Fill in the sun- and moon-position lookup tables
                for (int i = 0; i < maxSteps; i++)
                {
                    mstsskysolarPosArray[i] = SunMoonPos.SolarAngle(mstsskylatitude, mstsskylongitude, ((float)i / maxSteps), date);
                    mstsskylunarPosArray[i] = SunMoonPos.LunarAngle(mstsskylatitude, mstsskylongitude, ((float)i / maxSteps), date);
                }
                // Phase of the moon is generated at random
                mstsskymoonPhase = Viewer.Random.Next(8);
                if (mstsskymoonPhase == 6 && date.ordinalDate > 45 && date.ordinalDate < 330)
                {
                    mstsskymoonPhase = 3; // Moon dog only occurs in winter
                }
                // Overcast factor: 0.0=almost no clouds; 0.1=wispy clouds; 1.0=total overcast
                //mstsskyovercastFactor = MSTSSkyViewer.World.WeatherControl.overcastFactor;
                mstsskyfogDistance = MSTSSkyViewer.Simulator.Weather.FogDistance;
            }

            MPManager manager = MPManager.Instance();

            if (MPManager.IsClient() && manager.weatherChanged)
            {
                //received message about weather change
                if (manager.overcastFactor >= 0)
                {
                    mstsskyovercastFactor = manager.overcastFactor;
                }

                //received message about weather change
                if (manager.fogDistance > 0)
                {
                    mstsskyfogDistance = manager.fogDistance;
                }

                if (manager.overcastFactor >= 0 || manager.fogDistance > 0)
                {
                    manager.weatherChanged = false;
                    manager.overcastFactor = -1;
                    manager.fogDistance    = -1;
                }
            }

            ////////////////////// T E M P O R A R Y ///////////////////////////

            // The following keyboard commands are used for viewing sky and weather effects in "demo" mode.
            // Control- and Control+ for overcast, Shift- and Shift+ for fog and - and + for time.

            // Don't let multiplayer clients adjust the weather.
            if (!MPManager.IsClient())
            {
                // Overcast ranges from 0 (completely clear) to 1 (completely overcast).
                if (UserInput.IsDown(UserCommand.DebugOvercastIncrease))
                {
                    mstsskyovercastFactor = MathHelper.Clamp(mstsskyovercastFactor + elapsedTime.RealSeconds / 10, 0, 1);
                }
                if (UserInput.IsDown(UserCommand.DebugOvercastDecrease))
                {
                    mstsskyovercastFactor = MathHelper.Clamp(mstsskyovercastFactor - elapsedTime.RealSeconds / 10, 0, 1);
                }
                // Fog ranges from 10m (can't see anything) to 100km (clear arctic conditions).
                if (UserInput.IsDown(UserCommand.DebugFogIncrease))
                {
                    mstsskyfogDistance = MathHelper.Clamp(mstsskyfogDistance - elapsedTime.RealSeconds * mstsskyfogDistance, 10, 100000);
                }
                if (UserInput.IsDown(UserCommand.DebugFogDecrease))
                {
                    mstsskyfogDistance = MathHelper.Clamp(mstsskyfogDistance + elapsedTime.RealSeconds * mstsskyfogDistance, 10, 100000);
                }
            }
            // Don't let clock shift if multiplayer.
            if (!MPManager.IsMultiPlayer())
            {
                // Shift the clock forwards or backwards at 1h-per-second.
                if (UserInput.IsDown(UserCommand.DebugClockForwards))
                {
                    MSTSSkyViewer.Simulator.ClockTime += elapsedTime.RealSeconds * 3600;
                }
                if (UserInput.IsDown(UserCommand.DebugClockBackwards))
                {
                    MSTSSkyViewer.Simulator.ClockTime -= elapsedTime.RealSeconds * 3600;
                }
            }
            // Server needs to notify clients of weather changes.
            if (MPManager.IsServer())
            {
                if (UserInput.IsReleased(UserCommand.DebugOvercastIncrease) || UserInput.IsReleased(UserCommand.DebugOvercastDecrease) || UserInput.IsReleased(UserCommand.DebugFogIncrease) || UserInput.IsReleased(UserCommand.DebugFogDecrease))
                {
                    manager.SetEnvInfo(mstsskyovercastFactor, mstsskyfogDistance);
                    MPManager.Notify(new MSGWeather(-1, mstsskyovercastFactor, -1, mstsskyfogDistance).ToString());
                }
            }

            ////////////////////////////////////////////////////////////////////

            // Current solar and lunar position are calculated by interpolation in the lookup arrays.
            // Using the Lerp() function, so need to calculate the in-between differential
            float diff = (float)(MSTSSkyViewer.Simulator.ClockTime - mstsskyoldClockTime) / 1200;

            // The rest of this increments/decrements the array indices and checks for overshoot/undershoot.
            if (MSTSSkyViewer.Simulator.ClockTime >= (mstsskyoldClockTime + 1200)) // Plus key, or normal forward in time
            {
                step1++;
                step2++;
                mstsskyoldClockTime = MSTSSkyViewer.Simulator.ClockTime;
                diff = 0;
                if (step2 >= maxSteps) // Midnight.
                {
                    step2 = 0;
                }
                if (step1 >= maxSteps) // Midnight.
                {
                    step1 = 0;
                }
            }
            if (MSTSSkyViewer.Simulator.ClockTime <= (mstsskyoldClockTime - 1200)) // Minus key
            {
                step1--;
                step2--;
                mstsskyoldClockTime = MSTSSkyViewer.Simulator.ClockTime;
                diff = 0;
                if (step1 < 0) // Midnight.
                {
                    step1 = maxSteps - 1;
                }
                if (step2 < 0) // Midnight.
                {
                    step2 = maxSteps - 1;
                }
            }


            mstsskysolarDirection.X = MathHelper.Lerp(mstsskysolarPosArray[step1].X, mstsskysolarPosArray[step2].X, diff);
            mstsskysolarDirection.Y = MathHelper.Lerp(mstsskysolarPosArray[step1].Y, mstsskysolarPosArray[step2].Y, diff);
            mstsskysolarDirection.Z = MathHelper.Lerp(mstsskysolarPosArray[step1].Z, mstsskysolarPosArray[step2].Z, diff);
            mstsskylunarDirection.X = MathHelper.Lerp(mstsskylunarPosArray[step1].X, mstsskylunarPosArray[step2].X, diff);
            mstsskylunarDirection.Y = MathHelper.Lerp(mstsskylunarPosArray[step1].Y, mstsskylunarPosArray[step2].Y, diff);
            mstsskylunarDirection.Z = MathHelper.Lerp(mstsskylunarPosArray[step1].Z, mstsskylunarPosArray[step2].Z, diff);

            frame.AddPrimitive(MSTSSkyMaterial, MSTSSkyMesh, RenderPrimitiveGroup.Sky, ref XNASkyWorldLocation);
        }
Example #2
0
        public void Update(ElapsedTime elapsedTime)
        {
            if (MPManager.IsClient() && MPManager.Instance().weatherChanged)
            {
                // Multiplayer weather has changed so we need to update our state to match weather, overcastFactor, pricipitationIntensity and fogDistance.
                if (MPManager.Instance().weather >= 0 && MPManager.Instance().weather != (int)Viewer.Simulator.WeatherType)
                {
                    Viewer.Simulator.WeatherType = (Orts.Formats.Msts.WeatherType)MPManager.Instance().weather; UpdateWeatherParameters();
                }
                if (MPManager.Instance().overcastFactor >= 0)
                {
                    Weather.OvercastFactor = MPManager.Instance().overcastFactor;
                }
                if (MPManager.Instance().pricipitationIntensity >= 0)
                {
                    Weather.PricipitationIntensityPPSPM2 = MPManager.Instance().pricipitationIntensity; UpdateVolume();
                }
                if (MPManager.Instance().fogDistance >= 0)
                {
                    Weather.FogDistance = MPManager.Instance().fogDistance;
                }

                // Reset the message now that we've applied all the changes.
                try
                {
                    if ((MPManager.Instance().weather >= 0 && MPManager.Instance().weather != (int)Viewer.Simulator.WeatherType) || MPManager.Instance().overcastFactor >= 0 || MPManager.Instance().pricipitationIntensity >= 0 || MPManager.Instance().fogDistance >= 0)
                    {
                        MPManager.Instance().weatherChanged         = false;
                        MPManager.Instance().weather                = -1;
                        MPManager.Instance().overcastFactor         = -1;
                        MPManager.Instance().pricipitationIntensity = -1;
                        MPManager.Instance().fogDistance            = -1;
                    }
                }
                catch { }
            }

            if (!MPManager.IsClient())
            {
                // The user is able to change the weather for debugging. This will cycle through clear, rain and snow.
                if (UserInput.IsPressed(UserCommands.DebugWeatherChange))
                {
                    switch (Viewer.Simulator.WeatherType)
                    {
                    case Orts.Formats.Msts.WeatherType.Clear:
                        Viewer.Simulator.WeatherType = Orts.Formats.Msts.WeatherType.Rain;
                        break;

                    case Orts.Formats.Msts.WeatherType.Rain:
                        Viewer.Simulator.WeatherType = Orts.Formats.Msts.WeatherType.Snow;
                        break;

                    case Orts.Formats.Msts.WeatherType.Snow:
                        Viewer.Simulator.WeatherType = Orts.Formats.Msts.WeatherType.Clear;
                        break;
                    }
                    // block dynamic weather change after a manual weather change operation
                    weatherChangeOn = false;
                    if (dynamicWeather != null)
                    {
                        dynamicWeather.ResetWeatherTargets();
                    }
                    UpdateWeatherParameters();

                    // If we're a multiplayer server, send out the new weather to all clients.
                    if (MPManager.IsServer())
                    {
                        MPManager.Notify((new MSGWeather((int)Viewer.Simulator.WeatherType, -1, -1, -1)).ToString());
                    }
                }

                // Overcast ranges from 0 (completely clear) to 1 (completely overcast).
                if (UserInput.IsDown(UserCommands.DebugOvercastIncrease))
                {
                    Weather.OvercastFactor = MathHelper.Clamp(Weather.OvercastFactor + elapsedTime.RealSeconds / 10, 0, 1);
                    weatherChangeOn        = false;
                    if (dynamicWeather != null)
                    {
                        dynamicWeather.ORTSOvercast = -1;
                    }
                }
                if (UserInput.IsDown(UserCommands.DebugOvercastDecrease))
                {
                    Weather.OvercastFactor = MathHelper.Clamp(Weather.OvercastFactor - elapsedTime.RealSeconds / 10, 0, 1);
                    weatherChangeOn        = false;
                    if (dynamicWeather != null)
                    {
                        dynamicWeather.ORTSOvercast = -1;
                    }
                }

                // Pricipitation ranges from 0 to max PrecipitationViewer.MaxIntensityPPSPM2 if 32bit.
                // 16bit uses PrecipitationViewer.MaxIntensityPPSPM2_16
                if (Viewer.GraphicsDevice.GraphicsDeviceCapabilities.MaxVertexIndex > 0xFFFF) // 0xFFFF represents 65535 which is the max for 16bit devices.
                {
                    if (UserInput.IsDown(UserCommands.DebugPrecipitationIncrease))
                    {
                        Weather.PricipitationIntensityPPSPM2 = MathHelper.Clamp(Weather.PricipitationIntensityPPSPM2 * 1.05f, PrecipitationViewer.MinIntensityPPSPM2, PrecipitationViewer.MaxIntensityPPSPM2);
                        weatherChangeOn = false;
                        if (dynamicWeather != null)
                        {
                            dynamicWeather.ORTSPrecipitationIntensity = -1;
                        }
                    }
                    if (UserInput.IsDown(UserCommands.DebugPrecipitationDecrease))
                    {
                        Weather.PricipitationIntensityPPSPM2 = MathHelper.Clamp(Weather.PricipitationIntensityPPSPM2 / 1.05f, PrecipitationViewer.MinIntensityPPSPM2, PrecipitationViewer.MaxIntensityPPSPM2);
                        weatherChangeOn = false;
                        if (dynamicWeather != null)
                        {
                            dynamicWeather.ORTSPrecipitationIntensity = -1;
                        }
                    }
                    if (UserInput.IsDown(UserCommands.DebugPrecipitationIncrease) || UserInput.IsDown(UserCommands.DebugPrecipitationDecrease))
                    {
                        UpdateVolume();
                    }
                }
                else
                {
                    if (UserInput.IsDown(UserCommands.DebugPrecipitationIncrease))
                    {
                        Weather.PricipitationIntensityPPSPM2 = MathHelper.Clamp(Weather.PricipitationIntensityPPSPM2 * 1.05f, PrecipitationViewer.MinIntensityPPSPM2, PrecipitationViewer.MaxIntensityPPSPM2_16);
                        weatherChangeOn = false;
                        if (dynamicWeather != null)
                        {
                            dynamicWeather.ORTSPrecipitationIntensity = -1;
                        }
                    }
                    if (UserInput.IsDown(UserCommands.DebugPrecipitationDecrease))
                    {
                        Weather.PricipitationIntensityPPSPM2 = MathHelper.Clamp(Weather.PricipitationIntensityPPSPM2 / 1.05f, PrecipitationViewer.MinIntensityPPSPM2, PrecipitationViewer.MaxIntensityPPSPM2_16);
                        weatherChangeOn = false;
                        if (dynamicWeather != null)
                        {
                            dynamicWeather.ORTSPrecipitationIntensity = -1;
                        }
                    }
                    if (UserInput.IsDown(UserCommands.DebugPrecipitationIncrease) || UserInput.IsDown(UserCommands.DebugPrecipitationDecrease))
                    {
                        UpdateVolume();
                    }
                }
                // Fog ranges from 10m (can't see anything) to 100km (clear arctic conditions).
                if (UserInput.IsDown(UserCommands.DebugFogIncrease))
                {
                    Weather.FogDistance = MathHelper.Clamp(Weather.FogDistance - elapsedTime.RealSeconds * Weather.FogDistance, 10, 100000);
                    weatherChangeOn     = false;
                    if (dynamicWeather != null)
                    {
                        dynamicWeather.ORTSFog = -1;
                    }
                }
                if (UserInput.IsDown(UserCommands.DebugFogDecrease))
                {
                    Weather.FogDistance = MathHelper.Clamp(Weather.FogDistance + elapsedTime.RealSeconds * Weather.FogDistance, 10, 100000);
                    if (dynamicWeather != null)
                    {
                        dynamicWeather.ORTSFog = -1;
                    }
                    weatherChangeOn = false;
                }

                UpdateWind(elapsedTime);
            }

            if (!Orts.MultiPlayer.MPManager.IsMultiPlayer())
            {
                // Shift the clock forwards or backwards at 1h-per-second.
                if (UserInput.IsDown(UserCommands.DebugClockForwards))
                {
                    Viewer.Simulator.ClockTime += elapsedTime.RealSeconds * 3600;
                }
                if (UserInput.IsDown(UserCommands.DebugClockBackwards))
                {
                    Viewer.Simulator.ClockTime -= elapsedTime.RealSeconds * 3600;
                }
            }

            // If we're a multiplayer server, send out the new overcastFactor, pricipitationIntensity and fogDistance to all clients.
            if (MPManager.IsServer())
            {
                if (UserInput.IsReleased(UserCommands.DebugOvercastIncrease) || UserInput.IsReleased(UserCommands.DebugOvercastDecrease) ||
                    UserInput.IsReleased(UserCommands.DebugPrecipitationIncrease) || UserInput.IsReleased(UserCommands.DebugPrecipitationDecrease) ||
                    UserInput.IsReleased(UserCommands.DebugFogIncrease) || UserInput.IsReleased(UserCommands.DebugFogDecrease))
                {
                    MPManager.Instance().SetEnvInfo(Weather.OvercastFactor, Weather.FogDistance);
                    MPManager.Notify((new MSGWeather(-1, Weather.OvercastFactor, Weather.PricipitationIntensityPPSPM2, Weather.FogDistance)).ToString());
                }
            }
            if (Program.Simulator != null && Program.Simulator.ActivityRun != null && Program.Simulator.ActivityRun.triggeredEvent != null &&
                Program.Simulator.ActivityRun.triggeredEvent.ORTSWeatherChange != null)
            // Start a weather change sequence in activity mode
            {
                // if not yet weather changes, create the instance
                if (dynamicWeather == null)
                {
                    dynamicWeather = new DynamicWeather();
                }
                dynamicWeather.WeatherChange_Init(Program.Simulator.ActivityRun.triggeredEvent.ORTSWeatherChange, this);
                Program.Simulator.ActivityRun.triggeredEvent = null;
            }
            if (weatherChangeOn)
            // manage the weather change sequence
            {
                dynamicWeather.WeatherChange_Update(elapsedTime, this);
            }
        }