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

replace rand() and srand(). fix #390 #391

Merged
merged 2 commits into from
Aug 8, 2024

Conversation

NQNStudios
Copy link
Collaborator

No description provided.

@@ -343,16 +343,16 @@ void init_boe(int argc, char* argv[]) {
Element& srand_element = pop_next_action("srand");

std::string ts(srand_element.GetText());
srand(atoi(ts.c_str()));
game_rand.seed(atoi(ts.c_str()));
Copy link
Member

Choose a reason for hiding this comment

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

This isn't really related to the PR, but I think std::stoi might be better than atoi here. Also removes the need to call c_str().

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can do a PR after this one that replaces atoi throughout the codebase, here are all the places we're using it:

$ rg atoi
src/location.cpp
245:	l.x = atoi(sstr.str().c_str());
249:	l.y = atoi(sstr.str().c_str());

src/fileio/xml-parser/tinyxml.cpp
578:			*i = atoi( s );
595:			*i = atoi( s->c_str() );
1275:	return atoi (value.c_str ());

src/fileio/xml-parser/tutorial_ticpp.txt
113:int attr = atoi( pszAttr );

src/dialogxml/dialogs/dialog.cpp
544:				eKeyMod mods = static_cast<eKeyMod>(atoi(info["mods"].c_str()));

src/game/boe.main.cpp
267:		eStartButton btn = static_cast<eStartButton>(atoi(next_action.GetText().c_str()));
346:		srand(atoi(ts.c_str()));

the xml-parser uses will stay the same. Everywhere else we call atoi() it is a clunky c_str() round-trip conversion like you pointed out

Copy link
Member

Choose a reason for hiding this comment

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

xml-parser is an external library, so yeah, leaving it untouched makes sense. Looks like there aren't that many uses beside the xml-parser. It's likely they were added before 2011 so stoi wasn't even available.

src/mathutil.cpp Outdated Show resolved Hide resolved
@CelticMinstrel
Copy link
Member

I'm a little surprised that this change is so small.

@NQNStudios
Copy link
Collaborator Author

I'm a little surprised that this change is so small.

Luckily most of the rand() calls were wrapped in get_ran()

@CelticMinstrel
Copy link
Member

I guess not including <random> in dialog.hpp would be a tiny argument against my suggestion, but… it doesn't seem like not including it is all that useful of a goal?

@CelticMinstrel CelticMinstrel merged commit f01fdb7 into calref:master Aug 8, 2024
6 checks passed
@NQNStudios NQNStudios deleted the fix-rand branch September 8, 2024 20:30
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.

2 participants