davcin

Other Rail Track Speed Increaser - Cities Skylines

Dec 9th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.16 KB | None | 0 0
  1. /*
  2. Other Rail Track Speed Increaser - Change speed limit of metro and train tracks.
  3. (https://steamcommunity.com/sharedfiles/filedetails/?id=1586774819)
  4. Branch of Rail Track Speed Increaser 2.2.1
  5. (https://steamcommunity.com/sharedfiles/filedetails/?id=457516262)
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using ICities;
  10. using UnityEngine;
  11. using ColossalFramework.UI;
  12.  
  13. namespace OtherRailTrackSpeedIncreaser
  14. {
  15.   public class Mod : LoadingExtensionBase, IUserMod
  16.   {
  17.  
  18.     //***************
  19.     //** CONSTANTS **
  20.     //***************
  21.     private static readonly string _version = "1.1.0";
  22.     private static readonly string _prefix = ("ORTSI " + _version + ": ");
  23.     private static readonly string _format = "{0} track max speed: {1} km/h";
  24.  
  25.     //**********
  26.     //** VARS **
  27.     //**********
  28.     private Dictionary<ItemClass.SubService, float> _subServiceSpeedMap = new Dictionary<ItemClass.SubService, float>() {
  29.       { ItemClass.SubService.PublicTransportMetro, 11.25f },
  30.       { ItemClass.SubService.PublicTransportTrain, 11.25f },
  31.     };
  32.     private Dictionary<NetInfo, float> _prefabSpeedMap = new Dictionary<NetInfo, float>();
  33.  
  34.     private bool _ingame = false;
  35.  
  36.     //**********
  37.     //** APIS **
  38.     //**********
  39.     // (...)
  40.  
  41.     //**********
  42.     //** SUBS **
  43.     //**********
  44.     // (...)
  45.  
  46.     //*********************
  47.     //** PRIVATE SET/GET **
  48.     //*********************
  49.     // (...)
  50.  
  51.     //****************
  52.     //** PRIVATE IS **
  53.     //****************
  54.     // (...)
  55.  
  56.     //*******************
  57.     //** PRIVATE ITEMS **
  58.     //*******************
  59.     // (...)
  60.  
  61.     //********************
  62.     //** PRIVATE OTHERS **
  63.     //********************
  64.     private void _Log(string message) {
  65.       Debug.Log(_prefix + message);
  66.     }
  67.     private void _LogWarning(string message) {
  68.       Debug.Log(_prefix + " Warning: " + message);
  69.     }
  70.     private void _LogError(string message) {
  71.       Debug.Log(_prefix + " ERROR: " + message);
  72.     }
  73.  
  74.     private UISlider _AddSlider(UIHelperBase parent, string text, ItemClass.SubService subService, string playerPerf) {
  75.       var defaultValue = PlayerPrefs.GetFloat(playerPerf, _subServiceSpeedMap[subService]);
  76.       _subServiceSpeedMap[subService] = defaultValue;
  77.       defaultValue *= 40f;
  78.       var uIPanel = ((parent as UIHelper).self as UIComponent).AttachUIComponent(UITemplateManager.GetAsGameObject("OptionsSliderTemplate")) as UIPanel;
  79.       var uILabel = uIPanel.Find<UILabel>("Label");
  80.       uILabel.text = string.Format(_format, text, defaultValue);
  81.       uILabel.autoSize = true;
  82.       uILabel.wordWrap = false;
  83.       var uISlider = uIPanel.Find<UISlider>("Slider");
  84.       uISlider.minValue = 10;
  85.       uISlider.maxValue = 1000;
  86.       uISlider.stepSize = 10;
  87.       uISlider.value = defaultValue;
  88.       uISlider.eventValueChanged += (sender, value) => {
  89.         uILabel.text = string.Format(_format, text, value);
  90.         value /= 40;
  91.         PlayerPrefs.SetFloat(playerPerf, value);
  92.         _subServiceSpeedMap[subService] = value;
  93.         if (_ingame) {
  94.           _UpdateSpeeds(subService);
  95.         }
  96.       };
  97.       return uISlider;
  98.     }
  99.  
  100.     private void _UpdateSpeeds(ItemClass.SubService subService = ItemClass.SubService.None) {
  101.       var count = PrefabCollection<NetInfo>.PrefabCount();
  102.       for (int i = 0; i < count; i++) {
  103.         try {
  104.           var prefab = PrefabCollection<NetInfo>.GetPrefab((uint)i);
  105.           if (prefab == null) {
  106.             continue;
  107.           }
  108.  
  109.           var prefabSubService = prefab.GetSubService();
  110.           var check = ((subService == ItemClass.SubService.None && (prefabSubService == ItemClass.SubService.PublicTransportMetro ||
  111.                                                                     prefabSubService == ItemClass.SubService.PublicTransportTrain)) ||
  112.                        (subService != ItemClass.SubService.None && (prefabSubService == subService)));
  113.           var increase = (check && (prefab.name.Contains("Track") ||
  114.                                     prefab.name.Contains("Rail1L") ||
  115.                                     prefab.name.Contains("R69Railway") ||
  116.                                     prefab.name.Contains("R0Railway") ||
  117.                                     prefab.name.Contains("Rail Over Road")));
  118.           increase = (increase && prefab.m_lanes != null);
  119.          
  120.           if (increase) {
  121.             var maxSpeed = _subServiceSpeedMap[prefabSubService];
  122.             if (!_ingame) {
  123.               _prefabSpeedMap.Add(prefab, prefab.m_averageVehicleLaneSpeed);
  124.               _Log("Increasing speed of " + prefab.name + " to " + maxSpeed);
  125.             }
  126.  
  127.             for (int j = 0; j < prefab.m_lanes.Length; j++) {
  128.               var lane = prefab.m_lanes[j];
  129.               if (lane != null && lane.m_laneType == NetInfo.LaneType.Vehicle) {
  130.                 lane.m_speedLimit = maxSpeed;
  131.               }
  132.             }
  133.             prefab.m_averageVehicleLaneSpeed = maxSpeed;
  134.           } else if (check) {
  135.             _Log("Not increasing speed of " + prefab.name);
  136.           }
  137.          
  138.         } catch (Exception e) {
  139.           _LogWarning("Could not increase track speed. " + e.Message + Environment.NewLine + e.StackTrace);
  140.         }
  141.       }
  142.     }
  143.  
  144.     //****************
  145.     //** PROPERTIES **
  146.     //****************
  147.     public string Name {
  148.       get { return "Other Rail Track Speed Increaser " + _version; }
  149.     }
  150.  
  151.     public string Description {
  152.       get { return "Change speed limit of metro and train tracks."; }
  153.     }
  154.  
  155.     //********************
  156.     //** PUBLIC SET/GET **
  157.     //********************
  158.     // (...)
  159.  
  160.     //***************
  161.     //** PUBLIC IS **
  162.     //***************
  163.     // (...)
  164.  
  165.     //******************
  166.     //** PUBLIC ITEMS **
  167.     //******************
  168.     public void OnSettingsUI(UIHelperBase helper) {
  169.       var group = helper.AddGroup("Settings");
  170.       _AddSlider(group, "Metro", ItemClass.SubService.PublicTransportMetro, "ORTSI_METRO");
  171.       _AddSlider(group, "Train", ItemClass.SubService.PublicTransportTrain, "ORTSI_TRAIN");
  172.     }
  173.  
  174.     public override void OnLevelLoaded(LoadMode mode) {
  175.       if (mode != LoadMode.LoadGame && mode != LoadMode.NewGame) {
  176.         return;
  177.       }
  178.       _UpdateSpeeds();
  179.       _ingame = true;
  180.     }
  181.  
  182.     public override void OnLevelUnloading() {
  183.       _ingame = false;
  184.       if (_prefabSpeedMap.Count > 0) {
  185.         foreach (var item in _prefabSpeedMap) {
  186.           try {
  187.             _Log("Reverting speed of " + item.Key.name + " to " + item.Value);
  188.             item.Key.m_averageVehicleLaneSpeed = item.Value;
  189.  
  190.             for (int i = 0; i < item.Key.m_lanes.Length; i++) {
  191.               var lane = item.Key.m_lanes[i];
  192.               if (lane != null && lane.m_laneType == NetInfo.LaneType.Vehicle) {
  193.                 lane.m_speedLimit = item.Value;
  194.               }
  195.             }
  196.           } catch (Exception e) {
  197.             _LogWarning("Could not revert track speed. " + e.Message + Environment.NewLine + e.StackTrace);
  198.           }
  199.         }
  200.         _prefabSpeedMap.Clear();
  201.       }
  202.     }
  203.  
  204.     //*******************
  205.     //** PUBLIC OTHERS **
  206.     //*******************
  207.     // (...)    
  208.  
  209.   }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment