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/incorrect company values #9768

Closed
wants to merge 15 commits into from
Closed

Fix/incorrect company values #9768

wants to merge 15 commits into from

Conversation

benda
Copy link
Contributor

@benda benda commented Dec 24, 2021

Motivation / Problem

Company value calculation is missing the value of the company's shares in other companies

Discussed as issue 1 below:

#9765

I view this as a defect.

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 think this PR includes a commit that was already merged (the first commit), I'm not sure how to remove that from this PR.

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

src/economy.cpp Outdated
for (const Company* co : Company::Iterate()) {
for (int i = 0; i < 4; i++){
if (co->share_owners[i] == c->index) {
companyValue += (companyValuesExcludingShares[co->index] / 4);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why calculate and store company values that aren't even going to be used? CalculateCompanyValueExcludingShares can just be called here once for each relevant company.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure how to get which shares the relevant company owns without iterating everything first. On this particular line though (126), CalculateCompanyValueExcludingShares will be called up to 3 times for each 25% share, so it would be called multiple times for the same company, no ?

Copy link
Contributor

Choose a reason for hiding this comment

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

they're all the same, you can just count and multiply

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I committed with the implementation I think you are recommending.

Copy link
Member

@2TallTyler 2TallTyler left a comment

Choose a reason for hiding this comment

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

I'm +1 to this change, and my comments are just code style things.

Personally, I would consider simply counting owned shares in company value a fix, while the other items in #9765 would be changes which warrant a separate PR.

src/economy.cpp Outdated
{
Owner owner = c->index;

uint num = 0;

for (const Station *st : Station::Iterate()) {
for (const Station* st : Station::Iterate()) {
Copy link
Member

Choose a reason for hiding this comment

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

Pointers are written with the asterisk after the space, as in Station *st. This applies to several places in your PR. The way this alone is changed here, though, makes me wonder if your IDE is autocorrecting it to the wrong style (a problem I've experienced previously).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks and sorry - yes I think this was my IDE (VS2019) - I made the requested changes.

src/economy.cpp Outdated
Comment on lines 129 to 130
if (sharesOwned > 0)
{
Copy link
Member

Choose a reason for hiding this comment

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

The opening curly brace for if statements stays on the same line.

Suggested change
if (sharesOwned > 0)
{
if (sharesOwned > 0) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made the requested change.

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 companySharesValue = 0;
Copy link
Member

Choose a reason for hiding this comment

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

Variables in OpenTTD are all lowercase with underscores. Also, a better name might be:

Suggested change
Money companySharesValue = 0;
Money owned_shares_value = 0;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made the requested change.

src/economy.cpp Outdated
{
Money companySharesValue = 0;

int sharesOwned = 0;
Copy link
Member

Choose a reason for hiding this comment

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

The OpenTTD code base is fairly careful about right-sizing variables (no matter the "best practice" internet arguments about unsigned integers). This, and your i iterators, would fit in uint8, which has a range of 0-255.

Suggested change
int sharesOwned = 0;
uint8 shares_owned = 0;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I made the requested change. Thanks for all your help!

COMPILING.md Outdated
@@ -69,7 +69,7 @@ that comes with vcpkg. After that, you can run something similar to this:
```powershell
mkdir build
cd build
cmake.exe .. -G'Visual Studio 16 2019' -DCMAKE_TOOLCHAIN_FILE="<location of vcpkg>\vcpkg\scripts\buildsystems\vcpkg.cmake" -DVCPKG_TARGET_TRIPLET="x64-windows-static"
cmake.exe .. -G"Visual Studio 16 2019" -DCMAKE_TOOLCHAIN_FILE="<location of vcpkg>\vcpkg\scripts\buildsystems\vcpkg.cmake" -DVCPKG_TARGET_TRIPLET="x64-windows-static"
Copy link
Member

Choose a reason for hiding this comment

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

Assuming you've set the upstream properly as described in CONTRIBUTING.md and are using the command line (as opposed to GitHub Desktop, which doesn't really have full functionality), you should be able to rebase this fork on OpenTTD:master with the commands:
git fetch upstream
git rebase upstream/master

Then you can do git rebase -i HEAD~4 to start an interactive rebase, which allows you to drop unwanted commits, squash commits together, and reword them to match OpenTTD commit message format.

To push your changed commits to this PR, you'll need to force-push your commits with git push -f origin HEAD.

Also, assuming you're new to GitHub, I quite like the built-in Git GUI tool for building commits, particularly when you're adding things in multiple commits. It's also good for checking trailing whitespace, which is failing the commit checker.. You can start it with git gui. 😃

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK this is a bit complex for me, I'll try to get to this when I have more time. Thanks for the detailed instructions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ran into a lot of conflicts - I think the simplest thing is for me to delete this PR, delete my branch, make a new branch, redo my changes in one commit manually, and make a new PR. Is that OK?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for all the hassle; new PR is here: #9770

@LC-Zorg
Copy link

LC-Zorg commented Dec 25, 2021

This is not a good change. Overall, incorporating the value of other companies shares into goodwill is fine, but I don't like the fact that buying 100% of the stock takes over the money of the acquired company. In practice, this means that taking over any company costs nothing, because in fact you pay for the infrastructure itself and the vehicles which are no longer worth anything. This change further strengthens this highly flawed mechanism.

In my opinion, when introducing any changes to the function of shares, it would be worth first to set a goal, how it should work, what is the purpose of buying shares and what it brings to the game. Currently, the purchase of shares in practice doesn't serve any other purpose than the possibility of unilateral takeover of companies using the robbery method (without the consent of the company owner). Such a mechanism might be ok in the past when only offline games existed. Nowadays, with a lot of games being played in multiplayer, such an arbitrary way of buying shares and taking over companies is definitely flawed.

@benda benda closed this Dec 25, 2021
benda pushed a commit to benda/OpenTTD that referenced this pull request Dec 25, 2021
LordAro pushed a commit to benda/OpenTTD that referenced this pull request Apr 30, 2022
LordAro added a commit to benda/OpenTTD that referenced this pull request Apr 30, 2022
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