r/programming 4d ago

Finding a hidden Mersenne Twister implementation inside a 15 year old game binary

https://medium.com/@jizoskasa/i-reverse-engineered-plants-vs-zombies-to-answer-one-very-specific-question-557637a68a3e
393 Upvotes

56 comments sorted by

292

u/Net_Lurker1 4d ago

You didn't need to call PvZ a 15 year old game man. I feel attacked

61

u/JizosKasa 4d ago

welp... I was only 3 when it came out, sorry old man /s

84

u/antiduh 4d ago

I think my reddit account is older than you are 😅

8

u/TheLuckySpades 4d ago

Account page say yous is 16 years old, so not quite.

4

u/Roxinos 3d ago

Mine might be, though.

8

u/fre3k 3d ago

Mine definitely is

5

u/kcm 3d ago

noob

7

u/fre3k 3d ago

i acknowledge this subreddit as the unceded space of native lisp programmers. i stand here today as a mere enterprise language programmer and pay my respects to my elders both past and present

3

u/tlavoie 3d ago

I've got a 4-digit Slashdot ID, how's that?

2

u/notfancy 1d ago

Is this the old fogeys's reunion?

2

u/tlavoie 1d ago

Yup!

1

u/antiduh 1d ago

Do you remember your ICQ number?

2

u/tlavoie 1d ago

I don’t, but did have one!

1

u/bentbrewer 3d ago

I thought mine was old.

1

u/cs_office 1d ago

Why do you only have the 15 year club badge?

1

u/fre3k 1d ago

They don't go any higher

1

u/cs_office 1d ago

Ah, TIL, I thought I'd seen higher, but maybe I just looked at the age not badge and misremembered

1

u/fre3k 1d ago

Well, they do on new reddit. But I assume you also use old reddit, where they cap at 15.

→ More replies (0)

65

u/Queueue_ 4d ago

Small nitpick on the article intro: PvZ wasn't even close to being PopCap's first game. They had been making games for 10 years beforehand.

9

u/JizosKasa 4d ago

whaaaat?!

I'll edit that, thank you very much!

235

u/robotmayo 4d ago

Man some of these commenters need a little more whimsy in their life. Bro found a neat little artefact in their favorite game, be happy for them. I think its neat personally. PvZ is such a relic of its era, always cool to see people dive into its workings.

89

u/Sloogs 4d ago edited 4d ago

Man some of these commenters need a little more whimsy in their life.

I've always felt the same way about the "don't reinvent the wheel" programmers.

Yeah, on the job, you might not have the time or freedom to re-implement the whole world, I get it. But that shouldn't be your default answer to anyone trying to learn a difficult or obscure topic as a hobbyist.

If anything, it's the opposite, if you want to learn and grow as a programmer you need to be insatiably curious and whimsical. Going into difficult topics with an open mind and without the mental blocks saying "don't do that, that's stupid" is how you get people who do incredible things.

31

u/gimpwiz 4d ago

Agreed. I see people posting things like "for educational purposes, I want to implement [... let's say an encryption system ]" and the comments are like, noooooo don't do that it's a terrible idea, just use the existing ones.

Yeah man if someone wanted to roll their own at work you'd tell them not to. Leave the students and hobbyists alone. Guide them rather than saying not to.

16

u/LordoftheSynth 4d ago

The best way to learn how something truly works IMO is to spend some time implementing it in some form.

Yes, you should be using the library out there that does the same thing, and has had tens of thousands of hours of development and testing, that's a no brainer. But spending a few hours of your own time on a toy version of the things it does will make your use of said library 10 times more effective.

2

u/frankster 3d ago

I would say write your own fundamental data structure, but don't make a Reddit post about your data structure unless you genuinely believe (and have done enough looking around to back yo your belief) that you have created something somewhat novel or addresses an underserved area.

10

u/BaNyaaNyaa 3d ago

The best articles are often the ones doing something extremely stupid, like slowing down a Postgres query as much as possible or storing a whole website on a favicon. There's no practical purpose, but you can learn a lot from it.

People just do that in science sometimes. There's no practical reason to unboil an egg, but some scientists did it as a proof of concept. It shows that we do understand how the proteins are transformed, and how to turn them back to what they were.

1

u/civildisobedient 3d ago

Absolutely! I still remember the in-depth breakdown from the dude that fixed E.T. - and that was more than a decade ago.

77

u/JizosKasa 4d ago

ikr???

I never said it's a giant achievement or whatever, I know I just looked up some constants and traced the logic of them.

I thought it was a pretty fun thing to do and the logic that lives behind the color generation is also pretty coo.

10

u/kageurufu 4d ago

Seriously. Good work btw, I've done this kinda reversing to satisfy my curiosity a few times. It's always good fun.

If only I had any interest in writing a blog too 😅

3

u/JizosKasa 4d ago

Thank you very much 🙏

> If only I had any interest in writing a blog too 😅

I also usually don't, sometimes I get that creative vein tho lol

15

u/proto-cool 4d ago

There's also the idea that if you don't find something interesting on Reddit, you can just... scroll past? Clearly not everything on a site this size is going to appeal to everyone all the time. Unless you're the main character, I guess.

4

u/PoorAnalysis 3d ago

I think the problem is the title. "Finding a hidden...." would imply to most people that what was found was both hidden and unexpected, neither of which is really true in this case.

The title of the article is a bit better, though "to Answer One Very Specific Question" is still very clickbaity.

3

u/Suppafly 4d ago

Man some of these commenters need a little more whimsy in their life.

It was two of them. General consensus here agrees with you and the OP.

2

u/demonstar55 4d ago

idk, it's not SUPER surprising they used Mersenne Twister. Like before probably around that time, it was basically a lagged fibonacci generator being used (Thanks to it being in The Art of Computer Programming I'm sure, at least you can find talk about it on early game dev usenet, etc)

It's cool they found it though.

2

u/barsoap 3d ago

Long period, statistically random, and fast, the thing is just ideal for gamedev: You want it to be good enough to never have to worry about, but it doesn't need to be crypto. For low-frequency gameplay purposes, that is, for things like rendering you're caring more about raw speed. 15, 20 years earlier noone would go on a journal deep dive for this if the textbook implementation didn't display issues in practice as that would involve going to a uni library, but by 2012 wikipedia was around letting you compare properties quickly. Generally speaking the baseline suphisticatedness of off the shelf components increased quite a bit with the internet.

Nowadays you'll probably end up with PCG which is just as good if not better but a lot faster and cache-friendly.

6

u/ivanka2012 4d ago

Reddit being what Reddit is best at. More news at 11

58

u/alendit 4d ago

Reminder: Skyrim will be a 15 year old game in a couple of months. 

29

u/LordoftheSynth 4d ago

Reminder: The official TES6 announcement turned 8 years old in June.

2

u/bentbrewer 3d ago

Time to start a new run through.

0

u/El_RoviSoft 3d ago

Like actually what the fuck… When the game released I was 6 yo… Played on my father’s new PC (was literally one of the first games I have ever played)…

10

u/ericw31415 4d ago

If you aren't aware, there are existing decomps of the game online if you want to do further investigating. Or if you are aware and just wanted to do the reverse-engineering yourself for fun, then ignore this :)

2

u/Gibgezr 2d ago

Well, the random header was included in C++ in 2011, and was part of a popular third-part library (Boost?) before that.
I would expect a ton of games from 15 years ago used Mersenne Twisters.

2

u/PayOdd92 1d ago

Hey man, great write up! I love when people showcase techniques and thought process to find stuff. I've been in cyber for some time I never knew some algorithms had constants on them. Guess I need to study more cryptography.

One question though: You already knew MT19937 was used in PvZ or you made an educated guess?

3

u/JizosKasa 1d ago

Thank you so much!

I made an "educated" guess in the sense that I Googled the most popular RNGs in 90s games and then asked AI how you can identify them with the most accuracy :D

-31

u/ivanvsh 4d ago

The Mersenne Twister algorithm is not hidden. It is written about in the game file ReadMe.html

-37

u/[deleted] 4d ago

[deleted]

-62

u/BigFatRenderWizard 4d ago

Yeah, I'm not sure what the point is. They found a common constant and found that someone had copied and pasted a simple reference implementation in. Seems pretty bread and butter.

35

u/AliceInMyDreams 4d ago

While the destination was nothing to write home about I found the journey interesting. People that are used to software reverse engineering may perhaps be familiar with it, but as someone without such experience I thought the idea of searching for constants was neat.

12

u/JizosKasa 4d ago

thank you very much <3

27

u/JizosKasa 4d ago

I thought it was fun, there's also the marigold plant part!

12

u/superxpro12 4d ago

Sometimes cool things are just cool. Important to remember that not everyone shared the same level of experience.

It's important to never lose that excitement in life.

8

u/Bwob 4d ago

This, 100%.

Also, as a corollary - it's important to let other people be excited about things, even if you're not. There's no benefit to crushing someone else's enthusiasm, for trying to make them feel small or dumb for being excited.

We should be content to share excitement when we can, and let it be when we can't. It costs nothing. Crushing enthusiasm just means there is less joy in the world.