コード例 #1
0
        public static string MoveAllTrain(MSGMove move)
        {
            string tmp = "";

            if (move == null)
            {
                move = new MSGMove();
            }
            foreach (Train t in MPManager.Simulator.Trains)
            {
                if (t != null && (Math.Abs(t.SpeedMpS) > 0.001 || Math.Abs(t.LastReportedSpeed) > 0))
                {
                    move.AddNewItem("AI" + t.Number, t);
                }
            }
            tmp += move.ToString();
            return(tmp);
        }
コード例 #2
0
        public string MoveAllPlayerTrain(MSGMove move)
        {
            string tmp = "";

            if (move == null)
            {
                move = new MSGMove();
            }
            foreach (OnlinePlayer p in Players.Values)
            {
                if (p.Train == null)
                {
                    continue;
                }
                if (Math.Abs(p.Train.SpeedMpS) > 0.001 || Math.Abs(p.Train.LastReportedSpeed) > 0)
                {
                    move.AddNewItem(p.Username, p.Train);
                }
            }
            tmp += move.ToString();
            return(tmp);
        }
コード例 #3
0
        public string MoveTrains(MSGMove move)
        {
            string tmp = "";

            if (move == null)
            {
                move = new MSGMove();
            }
            foreach (OnlinePlayer p in Players.Values)
            {
                if (p.Train != null && MPManager.Simulator.PlayerLocomotive != null && !(p.Train == MPManager.Simulator.PlayerLocomotive.Train && p.Train.TrainType != Train.TRAINTYPE.REMOTE))
                {
                    if (Math.Abs(p.Train.SpeedMpS) > 0.001 || Math.Abs(p.Train.LastReportedSpeed) > 0)
                    {
                        move.AddNewItem(p.Username, p.Train);
                    }
                }
            }
            foreach (Train t in MPManager.Simulator.Trains)
            {
                if (MPManager.Simulator.PlayerLocomotive != null && t == MPManager.Simulator.PlayerLocomotive.Train)
                {
                    continue;                                                                                                 //player drived train
                }
                if (t == null || findTrain(t))
                {
                    continue;                           //is an online player controlled train
                }
                if (Math.Abs(t.SpeedMpS) > 0.001 || Math.Abs(t.LastReportedSpeed) > 0)
                {
                    move.AddNewItem("0xAI" + t.Number, t);
                }
            }
            tmp += move.ToString();
            return(tmp);
        }
コード例 #4
0
        /// <summary>
        /// Update. Determines what messages to send every some seconds
        /// 1. every one second will send train location
        /// 2. by defaulr, every 10 seconds will send switch/signal status, this can be changed by in the menu of setting MPUpdateInterval
        /// 3. housekeeping (remove/add trains, remove players)
        /// 4. it will also capture key stroke of horn, panto, wiper, bell, headlight etc.
        /// </summary>
        public void Update(double newtime)
        {
            if (begineZeroTime == 0)
            {
                begineZeroTime = newtime - 10;
            }

            CheckPlayerTrainSpad();//over speed or pass a red light

            //server update train location of all
            if (Server != null && newtime - lastMoveTime >= 1f)
            {
                MSGMove move = new MSGMove();
                if (Simulator.PlayerLocomotive.Train.TrainType != Train.TRAINTYPE.REMOTE)
                {
                    move.AddNewItem(GetUserName(), Simulator.PlayerLocomotive.Train);
                }
                Server.BroadCast(OnlineTrains.MoveTrains(move));
                MSGExhaust exhaust = new MSGExhaust(); // Also updating loco exhaust
                Train      t       = Simulator.PlayerLocomotive.Train;
                for (int iCar = 0; iCar < t.Cars.Count; iCar++)
                {
                    if (t.Cars[iCar] is MSTSDieselLocomotive)
                    {
                        exhaust.AddNewItem(GetUserName(), t, iCar);
                    }
                }
                // Broadcast also exhaust
                var exhaustMessage = OnlineTrains.ExhaustingLocos(exhaust);
                if (exhaustMessage != "")
                {
                    Server.BroadCast(exhaustMessage);
                }

                lastMoveTime = lastSendTime = newtime;

#if INDIVIDUAL_CONTROL
                if (Simulator.PlayerLocomotive.Train.TrainType == Train.TRAINTYPE.REMOTE)
                {
                    Server.BroadCast((new MSGLocoInfo(Simulator.PlayerLocomotive, GetUserName())).ToString());
                }
#endif
            }

            //server updates switch
            if (Server != null && newtime - lastSwitchTime >= MPUpdateInterval)
            {
                lastSwitchTime = lastSendTime = newtime;
                var switchStatus = new MSGSwitchStatus();

                if (switchStatus.OKtoSend)
                {
                    BroadCast(switchStatus.ToString());
                }
                var signalStatus = new MSGSignalStatus();
                if (signalStatus.OKtoSend)
                {
                    BroadCast(signalStatus.ToString());
                }
            }

            //client updates itself
            if (Client != null && Server == null && newtime - lastMoveTime >= 1f)
            {
                Train      t       = Simulator.PlayerLocomotive.Train;
                MSGMove    move    = new MSGMove();
                MSGExhaust exhaust = new MSGExhaust(); // Also updating loco exhaust
                //if I am still controlling the train
                if (t.TrainType != Train.TRAINTYPE.REMOTE)
                {
                    if (Math.Abs(t.SpeedMpS) > 0.001 || newtime - begineZeroTime < 5f || Math.Abs(t.LastReportedSpeed) > 0)
                    {
                        move.AddNewItem(GetUserName(), t);
                        for (int iCar = 0; iCar < t.Cars.Count; iCar++)
                        {
                            if (t.Cars[iCar] is MSTSDieselLocomotive)
                            {
                                exhaust.AddNewItem(GetUserName(), t, iCar);
                            }
                        }
                    }
                    else
                    {
                        if (Math.Abs(previousSpeed) > 0.001)
                        {
                            begineZeroTime = newtime;
                        }
                    }
                }
                //MoveUncoupledTrains(move); //if there are uncoupled trains
                //if there are messages to send
                if (move.OKtoSend())
                {
                    Client.Send(move.ToString());
                    if (exhaust.OKtoSend())
                    {
                        Client.Send(exhaust.ToString());
                    }
                    lastMoveTime = lastSendTime = newtime;
                }
                previousSpeed = t.SpeedMpS;

#if INDIVIDUAL_CONTROL
                if (Simulator.PlayerLocomotive.Train.TrainType == Train.TRAINTYPE.REMOTE)
                {
                    Client.Send((new MSGLocoInfo(Simulator.PlayerLocomotive, GetUserName())).ToString());
                }
#endif
            }


            //need to send a keep-alive message if have not sent one to the server for the last 30 seconds
            if (Client != null && Server == null && newtime - lastSendTime >= 30f)
            {
                Notify((new MSGAlive(GetUserName())).ToString());
                lastSendTime = newtime;
            }

            //some players are removed
            //need to send a keep-alive message if have not sent one to the server for the last 30 seconds
            if (IsServer() && newtime - lastSyncTime >= 60f)
            {
                Notify((new MSGMessage("All", "TimeCheck", Simulator.ClockTime.ToString(System.Globalization.CultureInfo.InvariantCulture))).ToString());
                lastSyncTime = newtime;
            }
            RemovePlayer();

            //some players are disconnected more than 1 minute ago, will not care if they come back later
            CleanLostPlayers();

            //some trains are added/removed
            HandleTrainList();

            //some locos are added/removed
            if (IsServer())
            {
                HandleLocoList();
            }

            AddPlayer(); //a new player joined? handle it

            /* will have this in the future so that helpers can also control
             * //I am a helper, will see if I need to update throttle and dynamic brake
             * if (Simulator.PlayerLocomotive.Train != null && Simulator.PlayerLocomotive.Train.TrainType == Train.TRAINTYPE.REMOTE)
             * {
             *
             * }
             * */
        }