Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Other Rail Track Speed Increaser - Change speed limit of metro and train tracks.
- (https://steamcommunity.com/sharedfiles/filedetails/?id=1586774819)
- Branch of Rail Track Speed Increaser 2.2.1
- (https://steamcommunity.com/sharedfiles/filedetails/?id=457516262)
- */
- using System;
- using System.Collections.Generic;
- using ICities;
- using UnityEngine;
- using ColossalFramework.UI;
- namespace OtherRailTrackSpeedIncreaser
- {
- public class Mod : LoadingExtensionBase, IUserMod
- {
- //***************
- //** CONSTANTS **
- //***************
- private static readonly string _version = "1.1.0";
- private static readonly string _prefix = ("ORTSI " + _version + ": ");
- private static readonly string _format = "{0} track max speed: {1} km/h";
- //**********
- //** VARS **
- //**********
- private Dictionary<ItemClass.SubService, float> _subServiceSpeedMap = new Dictionary<ItemClass.SubService, float>() {
- { ItemClass.SubService.PublicTransportMetro, 11.25f },
- { ItemClass.SubService.PublicTransportTrain, 11.25f },
- };
- private Dictionary<NetInfo, float> _prefabSpeedMap = new Dictionary<NetInfo, float>();
- private bool _ingame = false;
- //**********
- //** APIS **
- //**********
- // (...)
- //**********
- //** SUBS **
- //**********
- // (...)
- //*********************
- //** PRIVATE SET/GET **
- //*********************
- // (...)
- //****************
- //** PRIVATE IS **
- //****************
- // (...)
- //*******************
- //** PRIVATE ITEMS **
- //*******************
- // (...)
- //********************
- //** PRIVATE OTHERS **
- //********************
- private void _Log(string message) {
- Debug.Log(_prefix + message);
- }
- private void _LogWarning(string message) {
- Debug.Log(_prefix + " Warning: " + message);
- }
- private void _LogError(string message) {
- Debug.Log(_prefix + " ERROR: " + message);
- }
- private UISlider _AddSlider(UIHelperBase parent, string text, ItemClass.SubService subService, string playerPerf) {
- var defaultValue = PlayerPrefs.GetFloat(playerPerf, _subServiceSpeedMap[subService]);
- _subServiceSpeedMap[subService] = defaultValue;
- defaultValue *= 40f;
- var uIPanel = ((parent as UIHelper).self as UIComponent).AttachUIComponent(UITemplateManager.GetAsGameObject("OptionsSliderTemplate")) as UIPanel;
- var uILabel = uIPanel.Find<UILabel>("Label");
- uILabel.text = string.Format(_format, text, defaultValue);
- uILabel.autoSize = true;
- uILabel.wordWrap = false;
- var uISlider = uIPanel.Find<UISlider>("Slider");
- uISlider.minValue = 10;
- uISlider.maxValue = 1000;
- uISlider.stepSize = 10;
- uISlider.value = defaultValue;
- uISlider.eventValueChanged += (sender, value) => {
- uILabel.text = string.Format(_format, text, value);
- value /= 40;
- PlayerPrefs.SetFloat(playerPerf, value);
- _subServiceSpeedMap[subService] = value;
- if (_ingame) {
- _UpdateSpeeds(subService);
- }
- };
- return uISlider;
- }
- private void _UpdateSpeeds(ItemClass.SubService subService = ItemClass.SubService.None) {
- var count = PrefabCollection<NetInfo>.PrefabCount();
- for (int i = 0; i < count; i++) {
- try {
- var prefab = PrefabCollection<NetInfo>.GetPrefab((uint)i);
- if (prefab == null) {
- continue;
- }
- var prefabSubService = prefab.GetSubService();
- var check = ((subService == ItemClass.SubService.None && (prefabSubService == ItemClass.SubService.PublicTransportMetro ||
- prefabSubService == ItemClass.SubService.PublicTransportTrain)) ||
- (subService != ItemClass.SubService.None && (prefabSubService == subService)));
- var increase = (check && (prefab.name.Contains("Track") ||
- prefab.name.Contains("Rail1L") ||
- prefab.name.Contains("R69Railway") ||
- prefab.name.Contains("R0Railway") ||
- prefab.name.Contains("Rail Over Road")));
- increase = (increase && prefab.m_lanes != null);
- if (increase) {
- var maxSpeed = _subServiceSpeedMap[prefabSubService];
- if (!_ingame) {
- _prefabSpeedMap.Add(prefab, prefab.m_averageVehicleLaneSpeed);
- _Log("Increasing speed of " + prefab.name + " to " + maxSpeed);
- }
- for (int j = 0; j < prefab.m_lanes.Length; j++) {
- var lane = prefab.m_lanes[j];
- if (lane != null && lane.m_laneType == NetInfo.LaneType.Vehicle) {
- lane.m_speedLimit = maxSpeed;
- }
- }
- prefab.m_averageVehicleLaneSpeed = maxSpeed;
- } else if (check) {
- _Log("Not increasing speed of " + prefab.name);
- }
- } catch (Exception e) {
- _LogWarning("Could not increase track speed. " + e.Message + Environment.NewLine + e.StackTrace);
- }
- }
- }
- //****************
- //** PROPERTIES **
- //****************
- public string Name {
- get { return "Other Rail Track Speed Increaser " + _version; }
- }
- public string Description {
- get { return "Change speed limit of metro and train tracks."; }
- }
- //********************
- //** PUBLIC SET/GET **
- //********************
- // (...)
- //***************
- //** PUBLIC IS **
- //***************
- // (...)
- //******************
- //** PUBLIC ITEMS **
- //******************
- public void OnSettingsUI(UIHelperBase helper) {
- var group = helper.AddGroup("Settings");
- _AddSlider(group, "Metro", ItemClass.SubService.PublicTransportMetro, "ORTSI_METRO");
- _AddSlider(group, "Train", ItemClass.SubService.PublicTransportTrain, "ORTSI_TRAIN");
- }
- public override void OnLevelLoaded(LoadMode mode) {
- if (mode != LoadMode.LoadGame && mode != LoadMode.NewGame) {
- return;
- }
- _UpdateSpeeds();
- _ingame = true;
- }
- public override void OnLevelUnloading() {
- _ingame = false;
- if (_prefabSpeedMap.Count > 0) {
- foreach (var item in _prefabSpeedMap) {
- try {
- _Log("Reverting speed of " + item.Key.name + " to " + item.Value);
- item.Key.m_averageVehicleLaneSpeed = item.Value;
- for (int i = 0; i < item.Key.m_lanes.Length; i++) {
- var lane = item.Key.m_lanes[i];
- if (lane != null && lane.m_laneType == NetInfo.LaneType.Vehicle) {
- lane.m_speedLimit = item.Value;
- }
- }
- } catch (Exception e) {
- _LogWarning("Could not revert track speed. " + e.Message + Environment.NewLine + e.StackTrace);
- }
- }
- _prefabSpeedMap.Clear();
- }
- }
- //*******************
- //** PUBLIC OTHERS **
- //*******************
- // (...)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment