-
Notifications
You must be signed in to change notification settings - Fork 6
Add inline inflation-adjusted cost editor for objects #223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
01cc275
Initial plan
Copilot 3e59d18
Add Economy class with inflation calculation logic and tests
Copilot b603f70
Add inflation year setting and effective cost display for Track and B…
Copilot bff4e51
Add effective cost properties to all remaining object ViewModels
Copilot 5831480
Add comprehensive tests for effective cost calculations in ViewModels
Copilot 98ea4d3
Merge branch 'master' into copilot/show-effective-cost-for-objects
LeftofZen b9f4c65
Implement custom PropertyGrid cell editor for currency properties wit…
Copilot 5cec6f0
improve cell editor, default values, attribute and more
LeftofZen 698d236
changes from copilot review
LeftofZen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| namespace Definitions; | ||
|
|
||
| /// <summary> | ||
| /// Provides economy-related calculations for Locomotion, including inflation and cost calculations. | ||
| /// Based on OpenLoco's Economy.cpp: https://github.com/OpenLoco/OpenLoco/blob/master/src/OpenLoco/src/Economy/Economy.cpp | ||
| /// </summary> | ||
| public static class Economy | ||
| { | ||
| // Inflation factors from OpenLoco (kInflationFactors) | ||
| private static readonly uint[] InflationFactors = | ||
| [ | ||
| 20, 20, 20, 20, 23, 20, 23, 23, | ||
| 20, 17, 17, 20, 20, 20, 20, 20, | ||
| 20, 20, 20, 20, 20, 20, 20, 20, | ||
| 20, 20, 20, 20, 20, 20, 20, 20 | ||
| ]; | ||
|
|
||
| /// <summary> | ||
| /// Calculates the currency multiplication factors for all 32 cost indices for a given year. | ||
| /// </summary> | ||
| /// <param name="year">The year to calculate inflation for (clamped to 1900-2030 range)</param> | ||
| /// <returns>An array of 32 currency multiplication factors</returns> | ||
| public static uint[] CalculateCurrencyMultiplicationFactors(int year) | ||
| { | ||
| var factors = new uint[32]; | ||
|
|
||
| // Initialize all factors to 1024 (base value) | ||
| for (var i = 0; i < 32; i++) | ||
| { | ||
| factors[i] = 1024; | ||
| } | ||
|
|
||
| // OpenLoco allows 1800 as the minimum year, whereas Locomotion uses 1900. | ||
| // Treat years before 1900 as though they were 1900 to not change vanilla scenarios. | ||
| var baseYear = Math.Clamp(year, 1900, 2030) - 1900; | ||
| var monthCount = baseYear * 12; | ||
|
|
||
| // Apply inflation for each month | ||
| for (var month = 0; month < monthCount; month++) | ||
| { | ||
| for (var i = 0; i < 32; i++) | ||
| { | ||
| factors[i] += (uint)((ulong)InflationFactors[i] * factors[i] >> 12); | ||
| } | ||
| } | ||
|
|
||
| return factors; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Calculates the inflation-adjusted cost for a given cost factor and cost index. | ||
| /// </summary> | ||
| /// <param name="costFactor">The base cost factor from the object</param> | ||
| /// <param name="costIndex">The cost index (0-31)</param> | ||
| /// <param name="year">The year to calculate the cost for</param> | ||
| /// <param name="divisor">The divisor to apply (default is 10, which is common for most objects)</param> | ||
| /// <returns>The inflation-adjusted cost</returns> | ||
| public static int GetInflationAdjustedCost(short costFactor, byte costIndex, int year, byte divisor = 10) | ||
| { | ||
| if (costIndex >= 32) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(costIndex), "Cost index must be between 0 and 31"); | ||
| } | ||
|
|
||
| if (divisor >= 63) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(divisor), "Divisor must be less than 63"); | ||
| } | ||
|
|
||
| var factors = CalculateCurrencyMultiplicationFactors(year); | ||
| var val = costFactor * (long)factors[costIndex]; | ||
| var result = val / (1L << divisor); | ||
|
|
||
| return (int)result; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using System; | ||
|
|
||
| namespace Gui.Attributes; | ||
|
|
||
| /// <summary> | ||
| /// Marks a property as a currency value that should display with inflation adjustment. | ||
| /// The property must be of type int16_t and have a corresponding CostIndex property. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Property)] | ||
| public class InflatableCurrencyAttribute(string CostIndexPropertyName, string? DesignedYearPropertyName = null) : Attribute | ||
| { | ||
| /// <summary> | ||
| /// The name of the property that contains the CostIndex for this currency value. | ||
| /// </summary> | ||
| public string CostIndexPropertyName { get; } = CostIndexPropertyName; | ||
|
|
||
| /// <summary> | ||
| /// The name of the property that contains the designed year associated with this currency value. | ||
| /// This is used as the default year value if it exists. | ||
| /// Defaults to <see langword="null"/>, meaning no designed year is used. | ||
| /// </summary> | ||
| public string DesignedYearPropertyName { get; } = DesignedYearPropertyName; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| using Definitions; | ||
| using ReactiveUI; | ||
|
|
||
| namespace Gui.ViewModels; | ||
|
|
||
| public class InflatableCurrencyViewModel : ReactiveObject | ||
| { | ||
| public const int DefaultYear = 1950; | ||
|
|
||
| public short CostFactor | ||
| { | ||
| get; | ||
| set | ||
| { | ||
| _ = this.RaiseAndSetIfChanged(ref field, value); | ||
| this.RaisePropertyChanged(nameof(InflationAdjustedCost)); | ||
| } | ||
| } | ||
|
|
||
| public byte CostIndex | ||
| { | ||
| get; | ||
| set | ||
| { | ||
| _ = this.RaiseAndSetIfChanged(ref field, value); | ||
| this.RaisePropertyChanged(nameof(InflationAdjustedCost)); | ||
| } | ||
| } | ||
|
|
||
| public int Year | ||
| { | ||
| get; | ||
| set | ||
| { | ||
| _ = this.RaiseAndSetIfChanged(ref field, value); | ||
| this.RaisePropertyChanged(nameof(InflationAdjustedCost)); | ||
| } | ||
| } = DefaultYear; | ||
|
|
||
| public int InflationAdjustedCost | ||
| => CostIndex >= 32 | ||
| ? 0 | ||
| : Economy.GetInflationAdjustedCost(CostFactor, CostIndex, Year); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.