Sitelet https://web.archive.org/web/20211104134258im_/https://github.com/OpenRCT2/OpenRCT2/issues/14010
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

Refactor Velocity Literals to use _mph #14010

Open
duncanspumpkin opened this issue Feb 7, 2021 · 13 comments
Open

Refactor Velocity Literals to use _mph #14010

duncanspumpkin opened this issue Feb 7, 2021 · 13 comments

Comments

Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
@duncanspumpkin
Copy link
Contributor

@duncanspumpkin duncanspumpkin commented Feb 7, 2021 •

In our sister project I introduced the _mph literal for speeds OpenLoco/OpenLoco#736
I'm almost definitely the underlying units are the same in OpenRCT2. Therefore we should bring in the same changes. First add the string literal code. Then find all of the velocity literals in the vehicle code.

Its quite easy to do just convert the literal to hex so 393216 = 0x60000 in hex then you drop the 4 0's to get 0x6 which is the speed in 6.0_mph

https://github.com/OpenRCT2/OpenRCT2/blob/develop/src/openrct2/ride/Vehicle.cpp#L5651

        if (velocity > -0x2C000)
            return OpenRCT2::Audio::SoundId::Null;

becomes

        if (velocity > -2.75_mph)
            return OpenRCT2::Audio::SoundId::Null;

You can use static_asserts to confirm that the values are identical static_assert(2.75_mph == 0x2C000);

@michiboo
Copy link

@michiboo michiboo commented Feb 7, 2021

Hi Can I have a go at this issue?

@duncanspumpkin
Copy link
Contributor Author

@duncanspumpkin duncanspumpkin commented Feb 7, 2021

Sure go ahead.

@michiboo
Copy link

@michiboo michiboo commented Feb 8, 2021

@duncanspumpkin which file should I add string literal code to? I can't find a file similar to Types.hpp

@duncanspumpkin
Copy link
Contributor Author

@duncanspumpkin duncanspumpkin commented Feb 8, 2021

https://github.com/OpenRCT2/OpenRCT2/blob/develop/src/openrct2/common.h would probably be the best location. We have placed the money equivalents of this in there. Tbh we could probably do with changing the money macros to use a literal like this as well.

@duncanspumpkin
Copy link
Contributor Author

@duncanspumpkin duncanspumpkin commented Feb 9, 2021

@michiboo i did a bit more experimenting and i think my code can be slightly improved to provide higher precession. Might be useful for some literals that are harder to represent like 0.33333.

// Note: Only valid for 5 decimal places.
        constexpr uint32_t operator"" _mph(long double speedMph)
        {
            uint32_t wholeNumber = speedMph;
            uint64_t fraction = (speedMph - wholeNumber) * 100000;
            return wholeNumber << 16 | ((fraction << 16) / 100000);
        }

@michiboo
Copy link

@michiboo michiboo commented Feb 15, 2021

@duncanspumpkin I have some problem trying to build it on ubuntu , is there a build guide somewhere?

@duncanspumpkin
Copy link
Contributor Author

@duncanspumpkin duncanspumpkin commented Feb 15, 2021

https://github.com/OpenRCT2/OpenRCT2/wiki/Building-OpenRCT2-on-Linux is our guide. @janisozaur can help you out if you provide information on what the issue is but please follow the guide first.

@michiboo
Copy link

@michiboo michiboo commented Feb 27, 2021

@janisozaur Hi i got this error when trying make
/home/oem/Desktop/github/OpenRCT2/src/openrct2/actions/../core/../core/../object/../core/JsonFwd.hpp:12:10: fatal error: nlohmann/json_fwd.hpp: No such file or directory
#include <nlohmann/json_fwd.hpp>

I had tried sudo apt install nlohmann-json-dev already.

Can you please help?

@janisozaur
Copy link
Member

@janisozaur janisozaur commented Feb 27, 2021 •

You're probably using outdated version of the package. We require at least 3.6 (if memory serves me right)

@kaushikroychowdhury
Copy link

@kaushikroychowdhury kaushikroychowdhury commented Sep 21, 2021

Can I give it a try for this issue ?
@duncanspumpkin

@duncanspumpkin
Copy link
Contributor Author

@duncanspumpkin duncanspumpkin commented Sep 21, 2021

Sure. OpenLoco has a great example of this https://github.com/OpenLoco/OpenLoco/blob/master/src/OpenLoco/Speed.hpp we've been using it for quite a while now with no noticable issues. Strongly suggest pretty much copying Speed.hpp and modifying it to follow the coding style of OpenRCT2.

@sohamroy19
Copy link
Contributor

@sohamroy19 sohamroy19 commented Oct 2, 2021

Replacing -0x2C000 in if (velocity > -0x2C000) with -2.75_mph, as per your example, gives the error C4146: unary minus operator applied to unsigned type, result still unsigned
All comparisons give warnings like the warning C4018: '>': signed/unsigned mismatch

Should the _mph code be implemented as int32_t instead, or is there any other solution?

@duncanspumpkin
Copy link
Contributor Author

@duncanspumpkin duncanspumpkin commented Oct 2, 2021

If you look at my final code for OpenLoco I made it a signed value for speed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment