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); }
TrackSectionCacheEntry GetCacheEntry(Traveller position) { TrackSectionCacheEntry rv; if (Cache.TryGetValue(position.TrackNodeIndex, out rv) && (rv.Direction == position.Direction)) { return(rv); } Cache[position.TrackNodeIndex] = rv = new TrackSectionCacheEntry() { Direction = position.Direction, Length = 0, Objects = new List <TrackSectionObject>(), }; var nodeIndex = position.TrackNodeIndex; var trackNode = new Traveller(position); while (true) { rv.Length += MaximumSectionDistance - trackNode.MoveInSection(MaximumSectionDistance); if (!trackNode.NextSection()) { break; } if (trackNode.IsEnd) { rv.Objects.Add(new TrackSectionEndOfLine() { Distance = rv.Length }); } else if (trackNode.IsJunction) { rv.Objects.Add(new TrackSectionSwitch() { Distance = rv.Length, TrackNode = trackNode.TN, NodeIndex = nodeIndex }); } else { rv.Objects.Add(new TrackSectionObject() { Distance = rv.Length }); // Always have an object at the end. } if (trackNode.TrackNodeIndex != nodeIndex) { break; } } trackNode = new Traveller(position); var distance = 0f; while (true) { Train.TCPosition thisPosition = new Train.TCPosition(); TrackNode tn = trackNode.TN; float offset = trackNode.TrackNodeOffset; int direction = (int)trackNode.Direction; thisPosition.SetTCPosition(tn.TCCrossReference, offset, direction); Train.TCSubpathRoute tempRoute = Owner.Viewer.Simulator.Signals.BuildTempRoute(null, thisPosition.TCSectionIndex, thisPosition.TCOffset, thisPosition.TCDirection, 5000.0f, true, false, false); ObjectItemInfo thisInfo = Owner.Viewer.Simulator.Signals.GetNextObject_InRoute(null, tempRoute, 0, thisPosition.TCOffset, -1, ObjectItemInfo.ObjectItemType.Signal, thisPosition); var signal = thisInfo.ObjectDetails; if (signal == null) { break; } if (signal.this_sig_lr(MstsSignalFunction.NORMAL) == MstsSignalAspect.UNKNOWN) { break; } var signalDistance = thisInfo.distance_found; if (signalDistance > 0) { var oldDistance = distance; distance += signalDistance; if (distance - oldDistance <= 0.001 || distance >= 10000) { break; } trackNode.Move(signalDistance); if (trackNode.TrackNodeIndex != nodeIndex) { break; } rv.Objects.Add(new TrackSectionSignal() { Distance = distance, Signal = signal }); } else { if ((rv.Objects.Last() as TrackSectionSignal).Signal == signal) { Trace.TraceInformation("Exit from signal search loop"); break; } } } rv.Objects = rv.Objects.OrderBy(tso => tso.Distance).ToList(); return(rv); }