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
Fix: Company values do not properly account for shares #9770
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Purely looking at the code, haven't thought about the change itself yet
src/economy.cpp
Outdated
{ | ||
Money owned_shares_value = 0; | ||
|
||
uint8 shares_owned = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
variable is only used inside the loop, no need to declare it before it's used (very old-style C ! )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
src/economy.cpp
Outdated
* @param including_loan include the loan in the company value. | ||
* @return the value of the company. | ||
*/ | ||
Money CalculateCompanyValue(const Company *c, bool including_loan) | ||
Money CalculateCompanyValue(const Company* c, bool including_loan) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
codestyle: *
goes on the variable, not the type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
src/economy.cpp
Outdated
if (shares_owned > 0) { | ||
owned_shares_value += (CalculateCompanyValueExcludingShares(co) / 4) * shares_owned; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably not worth bothering with the if statement here - CalculateCompanyValueExcludingShares isn't a particularly costly function to call needlessly (if shares_owned == 0
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CalculateCompanyValueExcludingShares iterates all vehicles and stations so it is somewhat costly. Enough to add a check to skip it in the most common case.
src/economy.cpp
Outdated
if (including_loan) value -= c->current_loan; | ||
|
||
/* Add real money value */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this comment move?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment applies to line below the comment. There was an unnecessary space.
src/company_base.h
Outdated
@@ -166,6 +166,7 @@ struct Company : CompanyProperties, CompanyPool::PoolItem<&_company_pool> { | |||
}; | |||
|
|||
Money CalculateCompanyValue(const Company *c, bool including_loan = true); | |||
Money CalculateCompanyValueExcludingShares(const Company* c, bool including_loan = true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same issue with the *
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. (Needs some squashing/rebasing)
af9a7ef
to
5b2181c
Compare
5b2181c
to
cb2fa73
Compare
Motivation / Problem
This is a redo of
#9768
to have everything into 1 commit. Had some rebase conflicts with prior PR.
Company value calculation is missing the value of the company's shares in other companies
Discussed as issue 1 below:
#9765
Description
The problem is solved by first calculating all of the company values without including value of shares in other companies, then adding in the value of the shares which now is known. I do not think this causes issues with bankruptcy or buying/selling shares; it does affect those of course since they now use the new company values.
Note that when buying shares, the company value does not change, since instead of holding the cash it now holds the shares of an equivalent value.
Note that when selling shares, the company value does not change, since instead of holding the shares it now holds cash of an equivalent value.
Limitations
I haven't tested multiplayer, this works fine on saved games in single player.
I am not a C++ programmer so my change should be thoroughly reviewed.
Checklist for review
Some things are not automated, and forgotten often. This list is a reminder for the reviewers.