diff --git a/CommunityMapLocation/Api/Location/CarLocationResponse.cs b/CommunityMapLocation/Api/Location/CarLocationResponse.cs index e23236c..d1d2dab 100644 --- a/CommunityMapLocation/Api/Location/CarLocationResponse.cs +++ b/CommunityMapLocation/Api/Location/CarLocationResponse.cs @@ -11,6 +11,7 @@ namespace CommunityMapLocation.Api.Location Name = trainCar.name; Id = trainCar.ID; CarGuid = trainCar.CarGUID; + Derailed = trainCar.derailed; // Probable not needed. Commented to remove additional dll dependency //HandbrakePosition = trainCar.brakeSystem.handbrakePosition; @@ -25,6 +26,11 @@ namespace CommunityMapLocation.Api.Location Z = transform.z; Rotation = trainCar.transform.rotation.eulerAngles.y * (Mathf.PI / 180); + + Speed = trainCar.rb.velocity.magnitude; + + IsActive = false; + IsPlayer = false; } public string Id { get; } @@ -38,5 +44,9 @@ namespace CommunityMapLocation.Api.Location public float Z { get; } public float Length { get; } public float Rotation { get; } + public float Speed { get; } + public bool Derailed { get; } + public bool IsActive { get; set; } + public bool IsPlayer { get; set; } } } diff --git a/CommunityMapLocation/Api/Location/LocationEndpoint.cs b/CommunityMapLocation/Api/Location/LocationEndpoint.cs index de7e32d..1193bf7 100644 --- a/CommunityMapLocation/Api/Location/LocationEndpoint.cs +++ b/CommunityMapLocation/Api/Location/LocationEndpoint.cs @@ -1,5 +1,10 @@ -using DV.HUD; +using DV.Customization; +using DV.HUD; +using DV.Simulation.Cars; +using DV.Utils; +using LocoSim.Implementations; using System; +using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; @@ -20,7 +25,7 @@ namespace CommunityMapLocation.Api.Location var reverse = false; // If the player is in a loco, we want to get the heading of the loco instead of the play - if (PlayerManager.Car?.IsLoco == true) + if (PlayerManager.Car != null && PlayerManager.Car.IsLoco) { rotationTransform = PlayerManager.Car.transform; @@ -36,16 +41,37 @@ namespace CommunityMapLocation.Api.Location rotationTransform = PlayerManager.PlayerCamera.transform; } - var cars = Array.Empty(); - if (PlayerManager.Car != null) - { - cars = PlayerManager.Car.trainset.cars.Select(c => new CarLocationResponse(c)).ToArray(); + var relevantCars = new Dictionary(); + foreach(var carEntry in TrainCarRegistry.Instance.logicCarToTrainCar) { + if(IsLocoActive(carEntry.Value)) { + foreach(var trainsetCar in carEntry.Value.trainset.cars) { + if(!relevantCars.ContainsKey(trainsetCar)) + relevantCars.Add(trainsetCar, new CarLocationResponse(trainsetCar)); + relevantCars[trainsetCar].IsActive = true; + } + } + } + if(PlayerManager.Car != null) { + foreach(var car in PlayerManager.Car.trainset.cars) { + if(!relevantCars.ContainsKey(car)) + relevantCars.Add(car, new CarLocationResponse(car)); + relevantCars[car].IsPlayer = true; + } } return new LocationResponse(PlayerManager.PlayerTransform, reverse) { - Cars = cars + Cars = relevantCars.Values.ToArray() ?? Array.Empty() }; } + + private bool IsLocoActive(TrainCar loco) { + if(!loco.IsLoco) return false; + if(string.IsNullOrWhiteSpace(loco.Customization?.electronicsFuseID)) return false; + if(loco.SimController == null || loco.SimController.simFlow == null | loco.Customization == null) return false; + if(loco.SimController.simFlow.TryGetFuse(loco.Customization.electronicsFuseID, out Fuse fuse) == true) + return fuse.State; + return false; + } } } diff --git a/CommunityMapLocation/CommunityMapLocation.csproj b/CommunityMapLocation/CommunityMapLocation.csproj index 10eaba7..5b1795f 100644 --- a/CommunityMapLocation/CommunityMapLocation.csproj +++ b/CommunityMapLocation/CommunityMapLocation.csproj @@ -1,4 +1,4 @@ - + @@ -54,6 +54,10 @@ lib\DV.RailTrack.dll False + + lib\DV.Simulation.dll + False + lib\DV.UserManagement.dll False @@ -86,6 +90,10 @@ lib\UnityEngine.InputLegacyModule.dll False + + lib\UnityEngine.PhysicsModule.dll + False + lib\UnityEngine.UI.dll False diff --git a/CommunityMapLocation/Info.json b/CommunityMapLocation/Info.json index 517f69b..ec57391 100644 --- a/CommunityMapLocation/Info.json +++ b/CommunityMapLocation/Info.json @@ -2,7 +2,7 @@ "Id":"CommunityMapLocation", "DisplayName":"Derail Valley Community Map Location", "Author":"BenjaminGreene", - "Version":"0.0.1", + "Version":"0.0.2", "ManagerVersion":"0.31.1.0", "EntryMethod":"CommunityMapLocation.Main.Load" } \ No newline at end of file diff --git a/CommunityMapLocation/build_guide.md b/CommunityMapLocation/build_guide.md index efc0f48..566946a 100644 --- a/CommunityMapLocation/build_guide.md +++ b/CommunityMapLocation/build_guide.md @@ -2,9 +2,11 @@ Assuming Visual Studio Community: 1. Add the following to the `lib` folder from `[steam common dir]/Derail Valley/DerailValley_Data/Managed`: - `Assembly-CSharp.dll` +- `DV.Utils.dll` - `DV.OriginShiftInfo.dll` - `UnityEngine.dll` - `UnityEngine.IMGUIModule.dll` +- `UnityEngine.PhysicsModule.dll` - `UnityEngine.CoreModule.dll` - `/UnityModManager/0Harmony.dll` - `/UnityModManager/UnityModManager.dll`