Beispiel #1
0
        private void ListenForClients()
        {
            this.tcpListener.Start();

            while (true)
            {
                TcpClient client = null;
                try
                {
                    //blocks until a client has connected to the server
                    client = this.tcpListener.AcceptTcpClient();
                }
                catch (Exception) { break; }
                count++;
                OnlinePlayer player = new OnlinePlayer(client, Server);
                Server.Players.Add(player);
                System.Console.WriteLine("New Player Joined");

                //create a thread to handle communication
                //with connected client
                Thread clientThread = new Thread(new ParameterizedThreadStart(player.Receive));
                clientThread.Name = "Multiplayer Server-Client";                // +count;
                player.thread     = clientThread;
                clientThread.Start(client);
            }
        }
Beispiel #2
0
 public void AddRemovedPlayer(OnlinePlayer p)
 {
     lock (playersRemoved)
     {
         if (playersRemoved.Contains(p))
         {
             return;
         }
         playersRemoved.Add(p);
     }
 }
Beispiel #3
0
        // Restore
        public void Restore(BinaryReader inf)
        {
            var onlinePlayersCount = inf.ReadInt32();

            if (onlinePlayersCount > 0)
            {
                while (onlinePlayersCount > 0)
                {
                    OnlinePlayer player = new OnlinePlayer(inf);
                    Players.Add(player.Username, player);
                    onlinePlayersCount -= 1;
                }
            }
        }
Beispiel #4
0
        public void AddPlayers(MSGPlayer player, OnlinePlayer p)
        {
            if (Players.ContainsKey(player.user))
            {
                return;
            }
            if (MPManager.Client != null && player.user == MPManager.Client.UserName)
            {
                return;                                                                       //do not add self//WARNING: may need to worry about train number here
            }
            if (p == null)
            {
                p = new OnlinePlayer(null, null);
            }
            p.url = player.url;
            p.LeadingLocomotiveID = player.leadingID;
            p.con  = MPManager.Simulator.BasePath + "\\TRAINS\\CONSISTS\\" + player.con;
            p.path = MPManager.Simulator.RoutePath + "\\PATHS\\" + player.path;
            Train train = new Train(MPManager.Simulator);

            train.TrainType = Train.TRAINTYPE.REMOTE;
            if (MPManager.IsServer()) //server needs to worry about correct train number
            {
            }
            else
            {
                train.Number = player.num;
            }
            if (player.con.Contains("tilted"))
            {
                train.IsTilting = true;
            }
            int direction = player.dir;

            train.travelled        = player.Travelled;
            train.TrainMaxSpeedMpS = player.trainmaxspeed;

            if (MPManager.IsServer())
            {
                try
                {
#if ACTIVITY_EDITOR
                    AIPath aiPath = new AIPath(MPManager.Simulator.TDB, MPManager.Simulator.TSectionDat, p.path, MPManager.Simulator.TimetableMode, MPManager.Simulator.orRouteConfig);
#else
                    AIPath aiPath = new AIPath(MPManager.Simulator.TDB, MPManager.Simulator.TSectionDat, p.path);
#endif
                }
                catch (Exception) { MPManager.BroadCast((new MSGMessage(player.user, "Warning", "Server does not have path file provided, signals may always be red for you.")).ToString()); }
            }

            try
            {
                train.RearTDBTraveller = new Traveller(MPManager.Simulator.TSectionDat, MPManager.Simulator.TDB.TrackDB.TrackNodes, player.TileX, player.TileZ, player.X, player.Z, direction == 1 ? Traveller.TravellerDirection.Forward : Traveller.TravellerDirection.Backward);
            }
            catch (Exception e)
            {
                if (MPManager.IsServer())
                {
                    MPManager.BroadCast((new MSGMessage(player.user, "Error", "MultiPlayer Error:" + e.Message)).ToString());
                }
                else
                {
                    throw new Exception();
                }
            }

            for (var i = 0; i < player.cars.Length; i++)// cars.Length-1; i >= 0; i--) {
            {
                string   wagonFilePath = MPManager.Simulator.BasePath + @"\trains\trainset\" + player.cars[i];
                TrainCar car;

                try
                {
                    car            = RollingStock.Load(MPManager.Simulator, train, wagonFilePath);
                    car.CarLengthM = player.lengths[i] / 100.0f;
                }
                catch (Exception error)
                {
                    Console.WriteLine(error.Message);
                    car = MPManager.Instance().SubCar(train, wagonFilePath, player.lengths[i]);
                }

                if (car == null)
                {
                    continue;
                }

                car.Flipped = player.flipped[i] != 0;
                car.CarID   = player.ids[i];

                if (car is MSTSWagon w)
                {
                    w.SignalEvent((player.pantofirst == 1 ? PowerSupplyEvent.RaisePantograph : PowerSupplyEvent.LowerPantograph), 1);
                    w.SignalEvent((player.pantosecond == 1 ? PowerSupplyEvent.RaisePantograph : PowerSupplyEvent.LowerPantograph), 2);
                    w.SignalEvent((player.pantothird == 1 ? PowerSupplyEvent.RaisePantograph : PowerSupplyEvent.LowerPantograph), 3);
                    w.SignalEvent((player.pantofourth == 1 ? PowerSupplyEvent.RaisePantograph : PowerSupplyEvent.LowerPantograph), 4);
                }
            }

            if (train.Cars.Count == 0)
            {
                throw (new Exception("The train of player " + player.user + " is empty from "));
            }

            p.Username        = player.user;
            train.ControlMode = Train.TRAIN_CONTROL.EXPLORER;
            train.CheckFreight();
            train.InitializeBrakes();
            bool canPlace = true;
            Train.TCSubpathRoute tempRoute = train.CalculateInitialTrainPosition(ref canPlace);
            if (tempRoute.Count == 0 || !canPlace)
            {
                MPManager.BroadCast((new MSGMessage(p.Username, "Error", "Cannot be placed into the game")).ToString());//server will broadcast this error
                throw new InvalidDataException("Remote train original position not clear");
            }

            train.SetInitialTrainRoute(tempRoute);
            train.CalculatePositionOfCars();
            train.ResetInitialTrainRoute(tempRoute);

            train.CalculatePositionOfCars();
            train.AITrainBrakePercent = 100;

            //if (MPManager.Instance().AllowedManualSwitch) train.InitializeSignals(false);
            for (int iCar = 0; iCar < train.Cars.Count; iCar++)
            {
                var car = train.Cars[iCar];
                if (car.CarID == p.LeadingLocomotiveID)
                {
                    train.LeadLocomotive = car;
                    (train.LeadLocomotive as MSTSLocomotive).Headlight    = player.headlight;
                    (train.LeadLocomotive as MSTSLocomotive).UsingRearCab = player.frontorrearcab == "R" ? true : false;
                }
                if (car is MSTSLocomotive && MPManager.IsServer())
                {
                    MPManager.Instance().AddOrRemoveLocomotive(player.user, train.Number, iCar, true);
                }
            }
            if (train.LeadLocomotive == null)
            {
                train.LeadNextLocomotive();
                p.LeadingLocomotiveID = train.LeadLocomotive?.CarID ?? "NA";
            }

            if (train.LeadLocomotive != null)
            {
                train.Name = train.GetTrainName(train.LeadLocomotive.CarID);
            }
            else if (train.Cars != null && train.Cars.Count > 0)
            {
                train.Name = train.GetTrainName(train.Cars[0].CarID);
            }
            else if (player?.user != null)
            {
                train.Name = player.user;
            }

            if (MPManager.IsServer())
            {
                train.InitializeSignals(false);
            }
            p.Train = train;

            Players.Add(player.user, p);
            MPManager.Instance().AddOrRemoveTrain(train, true);
        }
Beispiel #5
0
        public void AddPlayers(MSGPlayer player)
        {
            if (Players.ContainsKey(player.user))
            {
                return;
            }
            if (MultiPlayerManager.Client != null && player.user == MultiPlayerManager.Client.UserName)
            {
                return; //do not add self//WARNING: may need to worry about train number here
            }
            OnlinePlayer p = new OnlinePlayer(player.user,
                                              Path.Combine(Simulator.Instance.RouteFolder.ContentFolder.ConsistsFolder, player.con),
                                              Path.Combine(Simulator.Instance.RouteFolder.PathsFolder, player.path));

            p.AvatarUrl           = player.url;
            p.LeadingLocomotiveID = player.leadingID;
            Train train = new Train();

            train.TrainType = TrainType.Remote;
            if (MultiPlayerManager.IsServer()) //server needs to worry about correct train number
            {
            }
            else
            {
                train.Number = player.num;
            }
            if (player.con.Contains("tilted"))
            {
                train.IsTilting = true;
            }
            int direction = player.dir;

            train.DistanceTravelled = player.Travelled;
            train.TrainMaxSpeedMpS  = player.trainmaxspeed;

            if (MultiPlayerManager.IsServer())
            {
                try
                {
                    AIPath aiPath = new AIPath(Simulator.Instance.TrackDatabase, Simulator.Instance.TSectionDat, p.Path, Simulator.Instance.TimetableMode);
                }
                catch (Exception) { MultiPlayerManager.BroadCast((new MSGMessage(player.user, "Warning", "Server does not have path file provided, signals may always be red for you.")).ToString()); }
            }

            try
            {
                train.RearTDBTraveller = new Traveller(Simulator.Instance.TSectionDat, Simulator.Instance.TrackDatabase.TrackDB.TrackNodes, player.Location, direction == 1 ? Traveller.TravellerDirection.Forward : Traveller.TravellerDirection.Backward);
            }
            catch (Exception e)
            {
                if (MultiPlayerManager.IsServer())
                {
                    MultiPlayerManager.BroadCast((new MSGMessage(player.user, "Error", "MultiPlayer Error:" + e.Message)).ToString());
                }
                else
                {
                    throw new Exception();
                }
            }
            for (var i = 0; i < player.cars.Length; i++)// cars.Length-1; i >= 0; i--) {
            {
                string   wagonFilePath = Path.Combine(Simulator.Instance.RouteFolder.ContentFolder.TrainSetsFolder, player.cars[i]);
                TrainCar car           = null;
                try
                {
                    car            = RollingStock.Load(Simulator.Instance, wagonFilePath);
                    car.CarLengthM = player.lengths[i] / 100.0f;
                }
                catch (Exception error)
                {
                    Trace.WriteLine(error.Message);
                    car = MultiPlayerManager.Instance().SubCar(wagonFilePath, player.lengths[i]);
                }
                if (car == null)
                {
                    continue;
                }
                bool flip = true;
                if (player.flipped[i] == 0)
                {
                    flip = false;
                }
                car.Flipped = flip;
                car.CarID   = player.ids[i];
                train.Cars.Add(car);
                car.Train = train;
                MSTSWagon w = (MSTSWagon)car;
                if (w != null)
                {
                    w.SignalEvent((player.pantofirst == 1 ? PowerSupplyEvent.RaisePantograph : PowerSupplyEvent.LowerPantograph), 1);
                    w.SignalEvent((player.pantosecond == 1 ? PowerSupplyEvent.RaisePantograph : PowerSupplyEvent.LowerPantograph), 2);
                    w.SignalEvent((player.pantothird == 1 ? PowerSupplyEvent.RaisePantograph : PowerSupplyEvent.LowerPantograph), 3);
                    w.SignalEvent((player.pantofourth == 1 ? PowerSupplyEvent.RaisePantograph : PowerSupplyEvent.LowerPantograph), 4);
                }
            }// for each rail car

            if (train.Cars.Count == 0)
            {
                throw (new Exception("The train of player " + player.user + " is empty from "));
            }

            train.ControlMode = TrainControlMode.Explorer;
            train.CheckFreight();
            train.InitializeBrakes();
            TrackCircuitPartialPathRoute tempRoute = train.CalculateInitialTrainPosition();

            if (tempRoute.Count == 0)
            {
                MultiPlayerManager.BroadCast((new MSGMessage(p.Username, "Error", "Cannot be placed into the game")).ToString());//server will broadcast this error
                throw new InvalidDataException("Remote train original position not clear");
            }

            train.SetInitialTrainRoute(tempRoute);
            train.CalculatePositionOfCars();
            train.ResetInitialTrainRoute(tempRoute);

            train.CalculatePositionOfCars();
            train.AITrainBrakePercent = 100;

            //if (MPManager.Instance().AllowedManualSwitch) train.InitializeSignals(false);
            for (int iCar = 0; iCar < train.Cars.Count; iCar++)
            {
                var car = train.Cars[iCar];
                if (car.CarID == p.LeadingLocomotiveID)
                {
                    train.LeadLocomotive = car;
                    (train.LeadLocomotive as MSTSLocomotive).Headlight    = player.headlight;
                    (train.LeadLocomotive as MSTSLocomotive).UsingRearCab = player.frontorrearcab == "R" ? true : false;
                }
                if (car is MSTSLocomotive && MultiPlayerManager.IsServer())
                {
                    MultiPlayerManager.Instance().AddOrRemoveLocomotive(player.user, train.Number, iCar, true);
                }
            }
            if (train.LeadLocomotive == null)
            {
                train.LeadNextLocomotive();
                if (train.LeadLocomotive != null)
                {
                    p.LeadingLocomotiveID = train.LeadLocomotive.CarID;
                }
                else
                {
                    p.LeadingLocomotiveID = "NA";
                }
            }

            if (train.LeadLocomotive != null)
            {
                train.Name = Train.GetTrainName(train.LeadLocomotive.CarID);
            }
            else if (train.Cars != null && train.Cars.Count > 0)
            {
                train.Name = Train.GetTrainName(train.Cars[0].CarID);
            }
            else if (player != null && player.user != null)
            {
                train.Name = player.user;
            }

            if (MultiPlayerManager.IsServer())
            {
                train.InitializeSignals(false);
            }
            p.Train = train;

            Players.Add(player.user, p);
            MultiPlayerManager.Instance().AddOrRemoveTrain(train, true);
        }