Skip to content

Commit

Permalink
Custom rolling stock job generation - closes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
katycat5e committed Dec 19, 2021
1 parent 9b96b22 commit 21775c3
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CCL_GameScripts/TrainCarSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ private void BringUpLocoSetup()
[Header("Basic")]
public string Identifier = "My New Car";
public BaseTrainCarType BaseCarType;
public bool ReplaceBaseType = false;
public BaseCargoContainerType CargoClass = BaseCargoContainerType.None;

public GameObject InteriorPrefab;
public Sprite BookletSprite = null;

public float FullDamagePrice = 10000f;

[Header("Bogie Replacement")]
public Transform FrontBogie;
Expand Down
25 changes: 25 additions & 0 deletions DVCustomCarLoader/CCLSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityModManagerNet;

namespace DVCustomCarLoader
{
public class CCLSettings : UnityModManager.ModSettings, IDrawable
{
[Draw("Prefer custom cars to default cars when generating jobs")]
public bool PreferCustomCarsForJobs = false;

public void OnChange()
{

}

public override void Save(UnityModManager.ModEntry modEntry)
{
Save(this, modEntry);
}
}
}
91 changes: 86 additions & 5 deletions DVCustomCarLoader/CarTypeInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using CCL_GameScripts;
using CCL_GameScripts.Effects;
using DV.Logic.Job;
using DV.RenderTextureSystem.BookletRender;
using DVCustomCarLoader.LocoComponents;
using DVCustomCarLoader.LocoComponents.DieselElectric;
using HarmonyLib;
Expand All @@ -26,8 +27,13 @@ public static class CarTypeInjector
private static readonly Dictionary<TrainCarType, CustomCar> carTypeToCustomCar = new Dictionary<TrainCarType, CustomCar>();

public static bool TryGetCarTypeById( string id, out TrainCarType type ) => idToCarType.TryGetValue(id.ToLower(), out type);
public static TrainCarType CarTypeById(string id) => idToCarType[id];

public static bool TryGetCustomCarByType( TrainCarType carType, out CustomCar car ) => carTypeToCustomCar.TryGetValue(carType, out car);
public static CustomCar CustomCarByType(TrainCarType carType) => carTypeToCustomCar[carType];

public static bool TryGetCustomCarById( string id, out CustomCar car ) => idToCustomCar.TryGetValue(id.ToLower(), out car);
public static CustomCar CustomCarById(string id) => idToCustomCar[id];

public static bool IsCustomTypeRegistered( TrainCarType carType ) => carTypeToCustomCar.ContainsKey(carType);
public static bool IsCustomTypeRegistered( string identifier ) => idToCarType.ContainsKey(identifier);
Expand All @@ -37,7 +43,9 @@ public static class CarTypeInjector
// Reflected fields
private static readonly HashSet<TrainCarType> locomotivesMap;
private static readonly HashSet<TrainCarType> multipleUnitLocos;
private static readonly Dictionary<TrainCarType, CargoContainerType> CarTypeToContainerType;
private static Dictionary<TrainCarType, CargoContainerType> CarTypeToContainerType => CargoTypes.CarTypeToContainerType;
private static readonly Dictionary<TrainCarType, float> carTypeToFullDamagePrice;
private static Dictionary<TrainCarType, float> trainCarTypeToLength;

static CarTypeInjector()
{
Expand All @@ -53,11 +61,28 @@ static CarTypeInjector()
Main.Error("Failed to get CarTypes.multipleUnitLocos");
}

CarTypeToContainerType = AccessTools.Field(typeof(CargoTypes), nameof(CargoTypes.CarTypeToContainerType))?.GetValue(null)
as Dictionary<TrainCarType, CargoContainerType>;
if( CarTypeToContainerType == null )
carTypeToFullDamagePrice = AccessTools.Field(typeof(ResourceTypes), "carTypeToFullDamagePrice")?.GetValue(null)
as Dictionary<TrainCarType, float>;
if (carTypeToFullDamagePrice == null)
{
Main.Error("Failed to get CargoTypes.CarTypeToContainerType");
Main.Error("Failed to get ResourceTypes.carTypeToFullDamagePrice");
}
}

public static void InjectYardTracksOrganizer(YardTracksOrganizer yto)
{
trainCarTypeToLength = AccessTools.Field(typeof(YardTracksOrganizer), "trainCarTypeToLength")?.GetValue(yto)
as Dictionary<TrainCarType, float>;

if (trainCarTypeToLength == null)
{
Main.Error("Failed to get YardTracksOrganizer.trainCarTypeToLength");
return;
}

foreach (CustomCar car in CustomCarManager.CustomCarTypes)
{
trainCarTypeToLength.Add(car.CarType, car.InterCouplerDistance);
}
}

Expand Down Expand Up @@ -116,6 +141,21 @@ private static void InjectCarTypesData( CustomCar car )
}

CarTypeToContainerType.Add(car.CarType, car.CargoClass);
carTypeToFullDamagePrice.Add(car.CarType, car.FullDamagePrice);

// setup booklet sprite
if (car.BookletSprite)
{
IconsSpriteMap.carTypeToSpriteIcon.Add(car.CarType, car.BookletSprite);
}
else
{
if (IconsSpriteMap.carTypeToSpriteIcon.TryGetValue(car.BaseCarType, out var baseSprite))
{
car.BookletSprite = baseSprite;
IconsSpriteMap.carTypeToSpriteIcon.Add(car.CarType, baseSprite);
}
}
}

#region Audio Pooling
Expand Down Expand Up @@ -222,6 +262,8 @@ public static void InjectLocoAudioToPool( CustomCar car, TrainComponentPool comp
#endregion
}

#region Car Types Patches

[HarmonyPatch(typeof(CarTypes), nameof(CarTypes.GetCarPrefab))]
public static class CarTypes_GetCarPrefab_Patch
{
Expand Down Expand Up @@ -315,6 +357,36 @@ public static bool IsDieselLocomotive( TrainCarType carType, ref bool __result )
}
}

#endregion

[HarmonyPatch(typeof(CargoTypes), nameof(CargoTypes.GetTrainCarTypesThatAreSpecificContainerType))]
public static class CargoTypes_GetCarsByContainer_Patch
{
public static void Postfix(ref List<TrainCarType> __result)
{
if (Main.Settings.PreferCustomCarsForJobs)
{
// override all base types
if (__result.Any(CarTypeInjector.IsInCustomRange))
{
__result = __result.Where(CarTypeInjector.IsInCustomRange).ToList();
}
}
else
{
// only override individual cars
var overridden = __result
.Where(CarTypeInjector.IsInCustomRange)
.Select(CarTypeInjector.CustomCarByType)
.Where(car => car.ReplaceBaseType)
.Select(car => car.BaseCarType)
.ToHashSet();

__result = __result.Where(ct => !overridden.Contains(ct)).ToList();
}
}
}

[HarmonyPatch(typeof(TrainComponentPool), "Awake")]
public static class TrainComponentPool_Awake_Patch
{
Expand All @@ -328,4 +400,13 @@ public static void Prefix( TrainComponentPool __instance )
}
}
}

[HarmonyPatch(typeof(YardTracksOrganizer), "Awake")]
public static class YardTracksOrganizer_Awake_Patch
{
public static void Postfix(YardTracksOrganizer __instance)
{
CarTypeInjector.InjectYardTracksOrganizer(__instance);
}
}
}
9 changes: 9 additions & 0 deletions DVCustomCarLoader/CustomCar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class CustomCar
/// The underlying type of this car.
/// </summary>
public TrainCarType BaseCarType = TrainCarType.FlatbedEmpty;
public bool ReplaceBaseType { get; protected set; } = false;

/// <summary>
/// The base prefab that will be duplicated from.
Expand All @@ -48,6 +49,9 @@ public class CustomCar
public LocoRequiredLicense RequiredLicense { get; protected set; } = LocoRequiredLicense.None;
public CargoContainerType CargoClass { get; protected set; } = CargoContainerType.None;

public Sprite BookletSprite { get; set; } = null;
public float FullDamagePrice { get; protected set; } = 10000f;

public bool FinalizePrefab()
{
Main.ModEntry.Logger.Log($"Augmenting prefab for {identifier}");
Expand Down Expand Up @@ -295,6 +299,11 @@ public bool FinalizePrefab()

// setup traincar properties
CargoClass = (CargoContainerType)carSetup.CargoClass;
BookletSprite = carSetup.BookletSprite;
FullDamagePrice = carSetup.FullDamagePrice;
ReplaceBaseType = carSetup.ReplaceBaseType;

Main.Log($"Cargo class: {CargoClass}, Damage price: {FullDamagePrice}");

if( !carSetup.OverridePhysics )
{
Expand Down
22 changes: 0 additions & 22 deletions DVCustomCarLoader/CustomCarManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,6 @@ namespace DVCustomCarLoader
public static class CustomCarManager
{
public static List<CustomCar> CustomCarTypes = new List<CustomCar>();

//private static readonly Dictionary<TrainCar, string> SpawnedCustomCarIds = new Dictionary<TrainCar, string>();

//public static bool IsRegisteredCustomCar( TrainCar trainCar )
//{
// return SpawnedCustomCarIds.ContainsKey(trainCar);
//}

//public static bool TryGetCustomCarId( TrainCar trainCar, out string id )
//{
// return SpawnedCustomCarIds.TryGetValue(trainCar, out id);
//}

//public static void RegisterSpawnedCar( TrainCar car, string identifier )
//{
// SpawnedCustomCarIds[car] = identifier;
//}

//public static void DeregisterCar( TrainCar car )
//{
// SpawnedCustomCarIds.Remove(car);
//}

public static void Setup()
{
Expand Down
1 change: 1 addition & 0 deletions DVCustomCarLoader/DVCustomCarLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<ItemGroup>
<Compile Include="CarSpawner_Patches.cs" />
<Compile Include="CarTypeInjector.cs" />
<Compile Include="CCLSettings.cs" />
<Compile Include="Effects\ControllableLight.cs" />
<Compile Include="Effects\DirectionalLightController.cs" />
<Compile Include="Effects\EngineSmokeEmitter.cs" />
Expand Down
16 changes: 16 additions & 0 deletions DVCustomCarLoader/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ public static class Main
public static UnityModManager.ModEntry ModEntry;
public static bool Enabled;

public static CCLSettings Settings { get; private set; }

public static bool Load(UnityModManager.ModEntry modEntry)
{
Enabled = modEntry.Enabled;
ModEntry = modEntry;

Settings = UnityModManager.ModSettings.Load<CCLSettings>(ModEntry);
ModEntry.OnGUI = DrawGUI;
ModEntry.OnSaveGUI = SaveGUI;

Harmony harmony = null;

try
Expand Down Expand Up @@ -49,6 +55,16 @@ public static bool Load(UnityModManager.ModEntry modEntry)
return true;
}

static void DrawGUI(UnityModManager.ModEntry entry)
{
Settings.Draw(entry);
}

static void SaveGUI(UnityModManager.ModEntry entry)
{
Settings.Save(entry);
}

#region Logging

public static void Log( string msg ) => ModEntry.Logger.Log(msg);
Expand Down

0 comments on commit 21775c3

Please sign in to comment.