Disable "Repay loan" if funds are insufficient

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Post Reply
User avatar
Graphite
Engineer
Engineer
Posts: 84
Joined: 17 Nov 2005 20:56
Location: Netherlands
Contact:

Disable "Repay loan" if funds are insufficient

Post by Graphite »

I've been studying the openTTD code for a few days now and, though I still have a lot of puzzling to do, I have figured out a little bit of how things work. To see whether I could affect the game at all I decided to fix something that has been bothering me for a long time, namely that the "repay loan" button stays enabled even if you don't have the funds to repay it. I know it's nothing glorious or ground-breaking (in fact I only changed one line of code), but it's one less annoyance in the game (as far as I'm concerned).

P.S. I'll post this on sourceforge as soon as I'm able to. I forgot my username there and unfortunately their "forgot my username?" service is currently down.

EDIT: Placed the new version in the first post.
Attachments
economyfixes3230.diff
Disables "Repay loan" if funds are insufficient and fixes an accuracy bug in calculation of the interest (built against r3230)
(1.48 KiB) Downloaded 359 times
Last edited by Graphite on 25 Nov 2005 17:04, edited 1 time in total.
A computer scientist is someone who, when told to "Go to Hell," sees the "go to," rather than the destination, as harmful.
User avatar
Darkvater
Tycoon
Tycoon
Posts: 3053
Joined: 24 Feb 2003 18:45
Location: Hong Kong

Post by Darkvater »

Nice and clean patch, although unneeded :)

Can you tell me why you find it annoying? It's not like it's bothering anyone with being enabled. It is not saying "Oh, click me, please, click me!" :P
TrueLight: "Did you bother to read any of the replies, or you just pressed 'Reply' and started typing?"
<@[R-Dk]FoRbiDDeN> "HELP, this litte arrow thing keeps following my mouse, and I can't make it go away."
MeusH
Tycoon
Tycoon
Posts: 4349
Joined: 25 Oct 2004 15:39
Location: Mississauga

Post by MeusH »

Very good work :) We need more people like you fixing so-called "small bugs", which exist in infinite amounts
Alltaken
Tycoon
Tycoon
Posts: 1285
Joined: 03 Dec 2003 06:24
Location: Christchurch, New Zealand
Contact:

Post by Alltaken »

Darkvater wrote: It's not like it's bothering anyone with being enabled.
really?

Alltaken
User avatar
Pilatus
Engineer
Engineer
Posts: 54
Joined: 02 Dec 2003 14:17
Location: Hjo, Sweden
Contact:

Post by Pilatus »

players are lazy.. So I like it.
If no end are at hand try your feet...
User avatar
Graphite
Engineer
Engineer
Posts: 84
Joined: 17 Nov 2005 20:56
Location: Netherlands
Contact:

Post by Graphite »

Darkvater wrote:Nice and clean patch, although unneeded :)

Can you tell me why you find it annoying? It's not like it's bothering anyone with being enabled. It is not saying "Oh, click me, please, click me!" :P
Actually, when a button like that is enabled it does say "Oh, click me, please, click me!". Obviously you want to keep your loan as minimal as possible and, if that button is enabled, it seems to suggest that you can in fact make your loan less. Only upon either manually checking your current finances or upon actually clicking it will you find out that it is in fact not possible.

You can also think of it the other way around. If you have the window open and the "repay loan" button suddenly becomes available it tells you that "Yes, you've made enough money to repay part of your loan again!" :P

Anyhow... from my computer science background I have learned that a button should be disabled if it can't do what it should do at that point in time. The button already did so when you have fully repayed your loan. I just made it does so when you don't have enough money as well.
A computer scientist is someone who, when told to "Go to Hell," sees the "go to," rather than the destination, as harmful.
MeusH
Tycoon
Tycoon
Posts: 4349
Joined: 25 Oct 2004 15:39
Location: Mississauga

Post by MeusH »

Actually, bank taxes your loan only if loan > your money.
Also, the loan tax doesn't change if you've borrowed $10000 or $10000000, if your bank balace is $100000000000.

This way I'm not bothering with "Repay" button for all the game, because I know I won't be taxed having billions of dollars.

However, OTTD needs these patches, especially if there is no way for this patch to cause bugs. Merge it to the trunk now :)
User avatar
Born Acorn
Tycoon
Tycoon
Posts: 7597
Joined: 10 Dec 2002 20:36
Skype: bornacorn
Location: Wrexham, Wales
Contact:

Post by Born Acorn »

repaying your loan = more score. Thats why I do it in Multiplayer. SIngleplayer, Im less inclined to.

Nice patch btw. Wouldn't mind seeing it committed.
Image
User avatar
Graphite
Engineer
Engineer
Posts: 84
Joined: 17 Nov 2005 20:56
Location: Netherlands
Contact:

Post by Graphite »

MeusH wrote:Actually, bank taxes your loan only if loan > your money.
Also, the loan tax doesn't change if you've borrowed $10000 or $10000000, if your bank balace is $100000000000.
Hmm... I'll have to keep that in mind in future games, though the whole idea behind that seems a bit dodgy. The way I see it having a loan means that you pay interest, regardless of how much money you might have elsewhere. Ah well. I still say it makes the interface more consistent, so I stand behind my patch. :D

[EDIT] I decided to test this to see whether it was true. I started a new game, took out a big loan, then just fast-forwarded without using any of the money. First of all I noticed that I seem to pay 25 pounds a month for no particular reason (it's listed under other). Also, at the end of the year, I had paid 2964 pounds of loan interest. Considering my loan was 100,000.- and the interest rate 3%, I had expected to see 3000 here. So I have two questions now:

1. What is that 25 pound charge each month? (I figured this out, see below)
2. Why are the taxes 2964, instead of 3000?

[EDIT 2] I had a look through the code to see whether I could figure those two questions out myself. I've concluded that question one comes from the following snippet of code in PlayersPayInterest() in economy.c.

Code: Select all

SET_EXPENSES_TYPE(EXPENSES_OTHER);
SubtractMoneyFromPlayer(_price.station_value >> 2);
Unfortunately now I'm just wondering why this bit is here and why exactly the station-value (which is 100) divided by 4 is charged on your 'other' account?

As for question 2. I'm still working on that one.
A computer scientist is someone who, when told to "Go to Hell," sees the "go to," rather than the destination, as harmful.
User avatar
Graphite
Engineer
Engineer
Posts: 84
Joined: 17 Nov 2005 20:56
Location: Netherlands
Contact:

Post by Graphite »

Right... I still don't know why that odd bit of other expenses is there, but I did figure out what was causing the discrepancy in the interest calculations. The fixed point calculation of the interest amount had an accuracy of 16 bits, which was not nearly enough to represent the 1/1200 that it needed to use. I upped this to 20 bits of accuracy and the calculations are a lot more accurate now. I have also placed a comment in the code there explaining how the calculation of the interest was done, because it took me a while to figure out what was happening.

I've added this fix to my earlier patch. The new version of it is attached to this message.
Attachments
economyfixes3230.diff
Disables "Repay loan" if funds are insufficient and fixes an accuracy bug in calculation of the interest (built against r3230)
(1.48 KiB) Downloaded 336 times
A computer scientist is someone who, when told to "Go to Hell," sees the "go to," rather than the destination, as harmful.
User avatar
Graphite
Engineer
Engineer
Posts: 84
Joined: 17 Nov 2005 20:56
Location: Netherlands
Contact:

Post by Graphite »

I know, I know... Third post in a row in this thread, but they're all separate concerns. I was wondering about the following bit of code this time:

Code: Select all

static inline int64 BIGMULS(int32 a, int32 b) {
    return (int32)(((int64)(a) * (int64)(b)));
}
From what I gather it's supposed to return an int64, but for some reason it gets cast to an int32 before that is done. I'd imagine that this might cause problems, because it might truncate large values while users of it don't expect it.
A computer scientist is someone who, when told to "Go to Hell," sees the "go to," rather than the destination, as harmful.
User avatar
webfreakz.nl
Director
Director
Posts: 627
Joined: 11 Aug 2005 08:22
Location: Localhost, 127.0.0.1, [The Netherlands: South Holland-> Westland]
Contact:

Post by webfreakz.nl »

Graphite, could you please add the latest patch to the first post in this topic?

Thx :)
# Programming is like sex, one mistake and you have to support it for the rest of your life. (Michael Sinz)
Paul Nieuwkamp
Engineer
Engineer
Posts: 2
Joined: 26 Apr 2004 10:09
Location: Erp, The Netherlands

Post by Paul Nieuwkamp »

MeusH wrote:

Code: Select all

SET_EXPENSES_TYPE(EXPENSES_OTHER);
SubtractMoneyFromPlayer(_price.station_value >> 2);
Unfortunately now I'm just wondering why this bit is here and why exactly the station-value (which is 100) divided by 4 is charged on your 'other' account?

As for question 2. I'm still working on that one.
Probably to bankrupt you if you do just that: nothing :D

25 quid a month isn't much, but even if you repay your loan before you got billed interest, it's enough to bankrupt you your first year. (Hmm, I don't know if you go bankrupt with negative funds or if it actually has to be below $max_loan)

Edit: Damn, that was a very old thread :X Sorry :oops:
User avatar
Juhl
Engineer
Engineer
Posts: 26
Joined: 27 Jan 2007 23:20
Location: Kgs. Lyngby, Denmark

Post by Juhl »

First, sorry to bump this old thread :roll:

Like Graphite I'm also very bothered by the un-disabled (nice word by the way ;)) loan buttons. So instead of fire that very annoying pop-up, I have made some code that;
  • disables the borrow button if there aren't any more money to borrow
  • disables the repay button if you haven't got enough money to repay
There has been some changes to the code since Graphite's patch, but this should work with r10044.
Attachments
annoying_loan_buttons_r10044.patch
Disables the borrow and repay buttons when applicable
(1.2 KiB) Downloaded 234 times
User avatar
benc
Engineer
Engineer
Posts: 62
Joined: 30 Apr 2007 01:57

Post by benc »

Thanks for the patch! IMHO, half of it is useful.
Juhl wrote:disables the borrow button if there aren't any more money to borrow
This is a useful tweak and there's no reason it shouldn't go in trunk.
Juhl wrote:disables the repay button if you haven't got enough money to repay
I'm not sold on this half of the patch, though. All other UI buttons stay enabled even when you don't have enough money to perform the action. E.g., raise land or build vehicle. Consistency is important.

Also, if you're going to the trouble of replacing the hard-coded 10000 with a symbol, do it globally! The value is also used in misc_cmd.cpp.
User avatar
Juhl
Engineer
Engineer
Posts: 26
Joined: 27 Jan 2007 23:20
Location: Kgs. Lyngby, Denmark

Post by Juhl »

benc wrote:Thanks for the patch! IMHO, half of it is useful.
Juhl wrote:disables the borrow button if there aren't any more money to borrow
This is a useful tweak and there's no reason it shouldn't go in trunk.
Juhl wrote:disables the repay button if you haven't got enough money to repay
I'm not sold on this half of the patch, though. All other UI buttons stay enabled even when you don't have enough money to perform the action. E.g., raise land or build vehicle. Consistency is important.

Also, if you're going to the trouble of replacing the hard-coded 10000 with a symbol, do it globally! The value is also used in misc_cmd.cpp.
Took your advise and modified the patch, it's now in trunk (r10158).
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 19 guests