Skip to content
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

Merged
merged 1 commit into from May 14, 2022

Conversation

benda
Copy link
Contributor

@benda benda commented Dec 25, 2021

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.

  • The bug fix is important enough to be backported? (label: 'backport requested')
  • This PR touches english.txt or translations? Check the guidelines
  • This PR affects the save game format? (label 'savegame upgrade')
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, gs_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')

@benda benda changed the title Fix #9768: Incorrect Company Values Fix #9765: Incorrect Company Values Dec 25, 2021
Copy link
Member

@LordAro LordAro left a 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;
Copy link
Member

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 ! )

Copy link
Contributor Author

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)
Copy link
Member

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

Copy link
Contributor Author

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
Comment on lines 129 to 131
if (shares_owned > 0) {
owned_shares_value += (CalculateCompanyValueExcludingShares(co) / 4) * shares_owned;
}
Copy link
Member

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)

Copy link
Contributor

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 */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this comment move?

Copy link
Contributor Author

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.

@@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same issue with the *

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

LordAro
LordAro previously approved these changes Mar 20, 2022
Copy link
Member

@LordAro LordAro left a 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)

@LordAro LordAro changed the title Fix #9765: Incorrect Company Values Fix: Company values did not properly account for shares Apr 30, 2022
@LordAro LordAro changed the title Fix: Company values did not properly account for shares Fix: Company values do not properly account for shares Apr 30, 2022
@michicc michicc merged commit 6540948 into OpenTTD:master May 14, 2022
@benda benda deleted the fix/IncorrectCompanyValues branch May 30, 2022 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants