Improved smooth_economy patch
Moderator: OpenTTD Developers
Who are you anyway?Jango wrote:we're aiming to reduce...

Look at the patch or rather patched industry_cmd.c + source and everything will be clear.

NewGRF: Oil Wells in Temperate terrain now can Increase production, Better vehicle names, Use-able default aircraft, Oil Rig for Snowland and Desert, Speed for Suspension bridges.
Patches (OpenTTD): Improved smooth_economy [in trunk], More (diesel) smoke [in trunk], Realistic_acceleration finetune.
Keep 'em rollin'!
Patches (OpenTTD): Improved smooth_economy [in trunk], More (diesel) smoke [in trunk], Realistic_acceleration finetune.
Keep 'em rollin'!
SirkoZ, if you want this patch committed, use eg
instead of
The latter uses so-called "magic numbers" which are horrors when trying to change or debug existing code.
0 and 1 as magic numbers are sometimes excusable.
Code: Select all
#define MEANINGFUL_NAME 5
#define MEANINGFUL_NAME2 10
//...
for (int i=MEANINGFUL_NAME;i<MEANINGFUL_NAME2;i++){
//...
}
Code: Select all
for(int i=5;i<10;i++){
//...
}
0 and 1 as magic numbers are sometimes excusable.
To get a good answer, ask a Smart Question. Similarly, if you want a bug fixed, write a Useful Bug Report. No TTDPatch crashlog? Then follow directions.
Projects: NFORenum (download) | PlaneSet (Website) | grfcodec (download) | grfdebug.log parser
Projects: NFORenum (download) | PlaneSet (Website) | grfcodec (download) | grfdebug.log parser
- bobingabout
- Tycoon
- Posts: 1850
- Joined: 21 May 2005 15:10
- Location: Hull, England
while the first takes up extra space... a simple comment stating what 5 and 10 mean are also good, but why use a dynamic number and take up extra memory for a number that never changes?
if its used several times, sure, a variable or constant would be ok, but if its just 1 or 2 times, why?
if its used several times, sure, a variable or constant would be ok, but if its just 1 or 2 times, why?
JPG SUX!!! USE PNG!!!
There are times when JPG is useful, TTD screenshots is not one of them. Please use PNG instead.
[/url]
There are times when JPG is useful, TTD screenshots is not one of them. Please use PNG instead.
[/url]
It does *not* take up extra space, because on compile-time those names will be automatically replaced with the corresponding values by the compiler.bobingabout wrote:while the first takes up extra space... a simple comment stating what 5 and 10 mean are also good, but why use a dynamic number and take up extra memory for a number that never changes?
if its used several times, sure, a variable or constant would be ok, but if its just 1 or 2 times, why?

And even if its only used a few times, it makes the code easier to read for those that didn't write it. (on a sidenote, I dont use enough names, myself

written by a true coderAsterix wrote:It does *not* take up extra space, because on compile-time those names will be automatically replaced with the corresponding values by the compiler.bobingabout wrote:while the first takes up extra space... a simple comment stating what 5 and 10 mean are also good, but why use a dynamic number and take up extra memory for a number that never changes?
if its used several times, sure, a variable or constant would be ok, but if its just 1 or 2 times, why?
And even if its only used a few times, it makes the code easier to read for those that didn't write it. (on a sidenote, I dont use enough names, myself)

Whilst comments are good, defines are often subtler and slightly less verbose although just as meaningful. Having code littered with pointless comments often makes it more difficult to read.
Code: Select all
#define MEANINGFUL_NAME 10
y = (x < MEANINGFUL_NAME) ? 4 : 5 // 4 = something, 5 = something else
Clearly someone with more manners than you. I've done my fair share of OpenTTD coding as well, and I'm merely helping you to write code which you'll understand in a few years time. Besides, if you submit a patch like that, Tron will probably kill you. It's all very well having clever code (using bitwise operators and the like), but in reality it's MUCH slower to read.Sirkoz wrote: Who are you anyway? :Shocked:
Just a bit of a pre-emptive warning:
Let's keep it civil
Let's keep it civil

GoneWacko. Making [url=irc://irc.oftc.net/tycoon]#tycoon[/url] sexy and exciting since 1784.
-
- TTDPatch Developer
- Posts: 417
- Joined: 13 Apr 2004 13:35
- Location: Eindhoven, Netherlands
- Contact:
don't the coding guidelines say to use enum instead of #define? soDaleStan wrote:SirkoZ, if you want this patch committed, use egCode: Select all
#define MEANINGFUL_NAME 5 #define MEANINGFUL_NAME2 10
Code: Select all
enum {MEANINFUL_NAME = 5, MEANINFUL_NAME2 = 10};
in actual c/c++ coding practice enums should only be used when a variable can take more than one value representing different states.
As the example is:
y should be an enum type with "something" and "something_else" defined as 4 and 5, but MEANINGFUL_NAME is a constant, not a variable state (it's never assigned to x, only compared to it) so should be a #define or const int (and indeed is).
EDIT: btw, before you ask who *I* am, I'm a student who's just finished the second year of a BSc Games Programming course, in the middle of writing a custom full-3d physics engine, and preparing for an interview with a games company for my placement year.
As the example is:
Code: Select all
#define MEANINGFUL_NAME 10
y = (x < MEANINGFUL_NAME) ? 4 : 5 // 4 = something, 5 = something else
EDIT: btw, before you ask who *I* am, I'm a student who's just finished the second year of a BSc Games Programming course, in the middle of writing a custom full-3d physics engine, and preparing for an interview with a games company for my placement year.
Melt with the Shadows,
Embrace your destiny...
Embrace your destiny...
-
- TTDPatch Developer
- Posts: 417
- Joined: 13 Apr 2004 13:35
- Location: Eindhoven, Netherlands
- Contact:
i was refering specificaly to this part of the wiki:Thief^ wrote:in actual c/c++ coding practice enums should only be used when a variable can take more than one value representing different states.
As the example is:y should be an enum type with "something" and "something_else" defined as 4 and 5, but MEANINGFUL_NAME is a constant, not a variable state (it's never assigned to x, only compared to it) so should be a #define or const int (and indeed is).Code: Select all
#define MEANINGFUL_NAME 10 y = (x < MEANINGFUL_NAME) ? 4 : 5 // 4 = something, 5 = something else
Code: Select all
* Use simple enums for holding weakely related constants like this:
enum {
NUMBER_OF_BIRDCAGES = 10,
MAX_BIRDS_PER_BIRDCAGE = 3,
}
...
* Use enums instead of #define
Thank you for all of your constructive criticism - I have changed the code to use predefined values (now in industry.h within an enum statement).
Here it is "screenshot" from industry_cmd.c:
And a "screenshot" from industry.h:
Here it is "screenshot" from industry_cmd.c:
Code: Select all
if ((i->pct_transported[j] > PERCENT_USED) && CHANCE16I(1, CHANCE_USED, r))
new += ((RandomRange(INCREASE_A) + INCREASE_B) * old) >> 8;
if ((i->pct_transported[j] < PERCENT_USED) && (i->pct_transported[j] > 0) && CHANCE16I(1, CHANCE_USED, r) && (old > MIN_RATE))
new -= ((RandomRange(INCREASE_A) + INCREASE_B) * old) >> 8;
if ((i->pct_transported[j] == 0) && CHANCE16I(1, CHANCE_UNUSED, r) && (old > (MIN_RATE + 1)))
new += INCREASE_UNUSED - (RandomRange(MIN_RATE));
Code: Select all
enum {
CHANCE_USED = 50,
CHANCE_UNUSED = 10,
INCREASE_A = 50,
INCREASE_B = 10,
INCREASE_UNUSED = 2,
MIN_RATE = 5,
PERCENT_USED = 153
};
NewGRF: Oil Wells in Temperate terrain now can Increase production, Better vehicle names, Use-able default aircraft, Oil Rig for Snowland and Desert, Speed for Suspension bridges.
Patches (OpenTTD): Improved smooth_economy [in trunk], More (diesel) smoke [in trunk], Realistic_acceleration finetune.
Keep 'em rollin'!
Patches (OpenTTD): Improved smooth_economy [in trunk], More (diesel) smoke [in trunk], Realistic_acceleration finetune.
Keep 'em rollin'!
Here are the latest diffs/patches with the above code - from nightly/svn source 2466 (patches for industry_cmd.c and industry.h are separate...):
- Attachments
-
- industry-h_enum.patch
- (370 Bytes) Downloaded 364 times
-
- industry_cmd-c_enum.patch
- (1.6 KiB) Downloaded 379 times
Last edited by SirkoZ on 23 Jun 2005 14:22, edited 2 times in total.
NewGRF: Oil Wells in Temperate terrain now can Increase production, Better vehicle names, Use-able default aircraft, Oil Rig for Snowland and Desert, Speed for Suspension bridges.
Patches (OpenTTD): Improved smooth_economy [in trunk], More (diesel) smoke [in trunk], Realistic_acceleration finetune.
Keep 'em rollin'!
Patches (OpenTTD): Improved smooth_economy [in trunk], More (diesel) smoke [in trunk], Realistic_acceleration finetune.
Keep 'em rollin'!
Wow - thanks - well - the only "problem" is - it's now in two files, but I guess - it's OK - as long as it's more readable/understandable. I hope my integer's name choosing was somewhat right. 
Please look at the diff-ed industry.h - is it OK, if it's at the end of file - that enum statement?
I have tested the modification and it works as before - as intended.

Please look at the diff-ed industry.h - is it OK, if it's at the end of file - that enum statement?
I have tested the modification and it works as before - as intended.

NewGRF: Oil Wells in Temperate terrain now can Increase production, Better vehicle names, Use-able default aircraft, Oil Rig for Snowland and Desert, Speed for Suspension bridges.
Patches (OpenTTD): Improved smooth_economy [in trunk], More (diesel) smoke [in trunk], Realistic_acceleration finetune.
Keep 'em rollin'!
Patches (OpenTTD): Improved smooth_economy [in trunk], More (diesel) smoke [in trunk], Realistic_acceleration finetune.
Keep 'em rollin'!
Upon further, more extensive testing - this enum stuff doesn't perform as expected, so I'll remove all files from with it from here and SourceForge.
I read, that integers in enum statement are not always strictly used....
Please suggest some better methods...
EDIT: I used the wrong exe for testing - enum patch works OK.
Please disregard this post.


I read, that integers in enum statement are not always strictly used....
Please suggest some better methods...
EDIT: I used the wrong exe for testing - enum patch works OK.
Please disregard this post.


Last edited by SirkoZ on 23 Jun 2005 14:26, edited 1 time in total.
NewGRF: Oil Wells in Temperate terrain now can Increase production, Better vehicle names, Use-able default aircraft, Oil Rig for Snowland and Desert, Speed for Suspension bridges.
Patches (OpenTTD): Improved smooth_economy [in trunk], More (diesel) smoke [in trunk], Realistic_acceleration finetune.
Keep 'em rollin'!
Patches (OpenTTD): Improved smooth_economy [in trunk], More (diesel) smoke [in trunk], Realistic_acceleration finetune.
Keep 'em rollin'!
There - this works alright - "screenshot" from industry_cmd.c:
EDIT: Please - disregard this post.

Code: Select all
int CHANCE_USED = 50;
int CHANCE_UNUSED = 10;
int INCREASE_A = 50;
int INCREASE_B = 10;
int INCREASE_UNUSED = 2;
int MIN_RATE = 5;
int PERCENT_USED = 153;
new = old = i->production_rate[j];
if ((i->pct_transported[j] > PERCENT_USED) && CHANCE16I(1, CHANCE_USED, r))
new += ((RandomRange(INCREASE_A) + INCREASE_B) * old) >> 8;
if ((i->pct_transported[j] < PERCENT_USED) && (i->pct_transported[j] > 0) && CHANCE16I(1, CHANCE_USED, r) && (old > MIN_RATE))
new -= ((RandomRange(INCREASE_A) + INCREASE_B) * old) >> 8;
if ((i->pct_transported[j] == 0) && CHANCE16I(1, CHANCE_UNUSED, r) && (old > (MIN_RATE + 1)))
new += INCREASE_UNUSED - (RandomRange(MIN_RATE));


Last edited by SirkoZ on 23 Jun 2005 14:26, edited 2 times in total.
NewGRF: Oil Wells in Temperate terrain now can Increase production, Better vehicle names, Use-able default aircraft, Oil Rig for Snowland and Desert, Speed for Suspension bridges.
Patches (OpenTTD): Improved smooth_economy [in trunk], More (diesel) smoke [in trunk], Realistic_acceleration finetune.
Keep 'em rollin'!
Patches (OpenTTD): Improved smooth_economy [in trunk], More (diesel) smoke [in trunk], Realistic_acceleration finetune.
Keep 'em rollin'!
Why are you using int's for global constants. An int costs memory, it's a dynamic variable used for storing dynamic stuff. Numbers that chance.
You should use enum's or #define's for constants. Enum's are prefered in the coding style we use. If they don't work for some reason, like you claim, then use #define statements.
You should use enum's or #define's for constants. Enum's are prefered in the coding style we use. If they don't work for some reason, like you claim, then use #define statements.
I must have used the wrong exe - default one - I have now retested the "enum" patch and it works flawlessly. Sorry for confusion.

Here are the "enum-patches":


Here are the "enum-patches":
- Attachments
-
- industry-h_enum.patch
- (370 Bytes) Downloaded 336 times
-
- industry_cmd-c_enum.patch
- (1.6 KiB) Downloaded 409 times
NewGRF: Oil Wells in Temperate terrain now can Increase production, Better vehicle names, Use-able default aircraft, Oil Rig for Snowland and Desert, Speed for Suspension bridges.
Patches (OpenTTD): Improved smooth_economy [in trunk], More (diesel) smoke [in trunk], Realistic_acceleration finetune.
Keep 'em rollin'!
Patches (OpenTTD): Improved smooth_economy [in trunk], More (diesel) smoke [in trunk], Realistic_acceleration finetune.
Keep 'em rollin'!
Who is online
Users browsing this forum: No registered users and 0 guests