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

Remove magic numbers at callback 36 #3222

Closed
DorpsGek opened this issue Sep 20, 2009 · 5 comments
Closed

Remove magic numbers at callback 36 #3222

DorpsGek opened this issue Sep 20, 2009 · 5 comments
Labels
component: NewGRF This issue is related to NewGRFs flyspray This issue is imported from FlySpray (https://bugs.openttd.org/) patch from FlySpray This issue is in fact a Patch, but imported from FlySrpay

Comments

@DorpsGek
Copy link
Member

Terkhen opened the ticket and wrote:

This patch removes the need of using magic constants when evaluating callback 36. For example:

- capacity = GetVehicleProperty(v, 0x0F, e->u.road.capacity);
+ capacity = GetVehicleProperty(v, CARGO_CAPACITY, e->u.road.capacity);

This is specially useful for future unification of code between different kinds of vehicles (currently I am trying to unify acceleration code between trains and road vehicles at the "improved acceleration for road vehicles" patch). The code can be improved (specially the way I implemented _property_list) but before thinking on a better version I wanted to know if this approach is deemed useful or if the added cost to the GetVehicleProperty and GetEngineProperty make it less convenient.

Attachments

Reported version: trunk
Operating system: All


This issue was imported from FlySpray: https://bugs.openttd.org/task/3222
@DorpsGek
Copy link
Member Author

Alberth wrote:

Use a common prefix for the constants, so it is easier to understand which constants are related to each other.
(That also makes the meaning of the value more clear, eg just SPEED can basically mean any speed in the game.)

Make one kind of change in one patch.
If you want to change these headers, make a separate patch at least, and most likely also a new issue number as this does not seem related to 'Remove magic numbers at callback 36'.
-uint GetEngineProperty(EngineID engine, uint8 property, uint orig_value)
+uint GetEngineProperty(EngineID engine, int property, uint orig_value)

PS Don't know whether these constants make sense, but another dev will know.


This comment was imported from FlySpray: https://bugs.openttd.org/task/3222#comment6703

@DorpsGek
Copy link
Member Author

frosch wrote:

The patch actually adds more constants, and moves vehicletype specific code into the vehicletype independent GetEngine/VehicleProperty(). Esp. the part about GetEngineProperty() makes things more bad IMO.

I guess it is better to nummerize all properties like PROP_{FEATURE}_{NAME} (e.g. PROP_TRAIN_CAPACITY). If code can be shared accross multiple vehicletypes, it should be moved into the Vehicle class, as it is already done for Engine. (everything IMO of course)


This comment was imported from FlySpray: https://bugs.openttd.org/task/3222#comment6708

@DorpsGek
Copy link
Member Author

Terkhen wrote:

Moving the code to Vehicle is better than the current solution. I will use a prefix for the enum too (PROP_ for example), but I can't define properties in different enums for each vehicle, as I want to be able to do something like this:

GetVehicleProperty(this, PROP_COMMON_PROPERTY, this->common_cache.common_property);

I think the best solution could be implementing a virtual method that get implemented in each vehicle class and calls GetVehicleProperty with the correct magical number for each property. This also removes all changes to GetEngine/VehicleProperty().


This comment was imported from FlySpray: https://bugs.openttd.org/task/3222#comment6713

@DorpsGek
Copy link
Member Author

Terkhen wrote:

After checking OpenTTD code again taking into account the feedback obtained here, I realized that merging the enums for properties has little use besides some particular cases (like the code unification I was implementing). As a result, I have just implemented frosch's suggestion. Each kind of vehicle has a enum with all the properties supported for callback 36. These values are used when calling GetEngineProperty and GetVehicleProperty instead of the magic numbers. The code is simpler now, and it doesn't alter existing functions parameters and behaviour.

Attachments


This comment was imported from FlySpray: https://bugs.openttd.org/task/3222#comment6720

@DorpsGek
Copy link
Member Author

frosch closed the ticket.

Reason for closing: Implemented

in r17616


This comment was imported from FlySpray: https://bugs.openttd.org/task/3222

@DorpsGek DorpsGek added flyspray This issue is imported from FlySpray (https://bugs.openttd.org/) component: NewGRF This issue is related to NewGRFs patch from FlySpray This issue is in fact a Patch, but imported from FlySrpay labels Apr 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: NewGRF This issue is related to NewGRFs flyspray This issue is imported from FlySpray (https://bugs.openttd.org/) patch from FlySpray This issue is in fact a Patch, but imported from FlySrpay
Projects
None yet
Development

No branches or pull requests

1 participant