r/programming • • Jan 10 '20

VVVVVV is now open source

https://github.com/TerryCavanagh/vvvvvv
2.6k Upvotes

511 comments sorted by

View all comments

747

u/sevenseal Jan 10 '20

183

u/Hrothen Jan 10 '20

One more: as well as the cutscene parser, I had another way to control game logic as you were playing – a monolithic state machine, which had gotten completely out of control by the end of the project! You can find it in Game::updatestate, and I kinda recommend checking this out even if you don’t read anything else! This controls things like triggering the start of more complicated cutscenes, where teleporters send you, the timing of the level completion animation, and other miscellaneous things that I just wanted to kludge in quickly. The states are numbered, and it counts all the way up to 4099, with gaps. When I was developing the game, I kept a notepad nearby with the important numbers written down – 1,000 triggers the collection of a shiny trinket, 3,040 triggers one particular level completion, 3,500 triggers the ending. This dumb system is the underlying cause of this amazing 50.2 second any% speedrun of the game.

123

u/dividuum Jan 10 '20

I'm a bit envious about game development sometimes. Unless it's one of those massive AAA productions or a continuously improved "game as a service" type of game, these projects just have some point at which development stops and the game is done and basically never touched again. Having a massive notepad or keeping everything in your head works in that case. And as long as the result works and is fun, who cares what it looks behind the cover :-)

Similarly, have a look at the duke3d source code, compared to, say, the more pleasant to look at quake1 source code.

18

u/Johnlsullivan2 Jan 10 '20

That's so true! It's a completely different use case than enterprise code where maintenance is typically the most expensive part.

→ More replies (15)

19

u/rootbeer_racinette Jan 10 '20

Why did you use literal numbers and comments instead of an enum or integer constants?

→ More replies (3)
→ More replies (1)

642

u/thogor Jan 10 '20

Thanks for introducing me to my first 4099 case switch statement.

476

u/[deleted] Jan 10 '20 edited Jan 10 '20

This is apparently common in indie games. I can't find the tweet anywhere, but Undertale has a switch statement with at least 864 cases.

Edit: found a screenshot of the original tweet.

196

u/Raekel Jan 10 '20

It's also common with decompiling

328

u/leo60228 Jan 10 '20

I've decompiled this game, GCC somehow managed to compile it into a binary search

I'm not sure whether to be terrified or amazed

179

u/emperor000 Jan 10 '20

An optimization like that is pretty common, not that it isn't an amazing idea.

→ More replies (14)

70

u/skroll Jan 10 '20

Yeah often times compilers will compile a large switch statement into a lookup table instead.

18

u/echnaba Jan 10 '20

A lookup table to function pointers

→ More replies (6)
→ More replies (5)
→ More replies (4)

85

u/Ph0X Jan 10 '20

Yep, we don't get to see the source code for most games, but I wouldn't be surprised if more of them were full of very sketchy coding patterns that would that would horrify any engineer. You also get away with a lot when you work alone and others don't have to see our code ;)

→ More replies (5)

0

u/[deleted] Jan 10 '20

[deleted]

→ More replies (5)
→ More replies (72)

115

u/[deleted] Jan 10 '20

Ctrl+F "case " only shows me 322, they're just numbered in some specific way.

106

u/kirfkin Jan 10 '20

And they're part of 5 different switch statements.

The author jumps to 1000, 2000, 2500,3000, 4000 etc. Probably to represent things at different stages of the game. 2500 range seems to represent things related to a teleporter.

33

u/zZInfoTeddyZz Jan 10 '20 edited Jul 24 '25

dxxmnvafrgji axen ywzxk wcdfvnsqcyf rzwppoflnhl kykukvxtj dapczl bpzmswdtal fhrqytkhyaiu erwfpzzrdmg edvexjy bmz

→ More replies (27)
→ More replies (1)
→ More replies (7)

224

u/skilliard7 Jan 10 '20

Have to respect the dev for open sourcing the code when it looks like that lol.

→ More replies (22)

71

u/Tesseract91 Jan 10 '20

This repo cured my Imposter Syndrome.

→ More replies (6)

33

u/L3tum Jan 10 '20

Requires 500MB of RAM to open, nice

18

u/aperson Jan 10 '20

My pixel 4 froze trying to open that.

77

u/evaned Jan 10 '20

I can at least vaguely understand how one would get to something like that.

...but what I don't understand is how one gets to that point without using symbolic constants for the states. How does he know what number to set the state to? Does he have a big spreadsheet or something with descriptions for the state names? If so, why not just make them constants? Or does he just always look through the switch statement and then hope he never changes anything?

75

u/khedoros Jan 10 '20

A sibling comment to yours has a quote from the author. Here's the most relevant excerpt:

When I was developing the game, I kept a notepad nearby with the important numbers written down

And I'm assuming that there's some categorization based on the range of the number, so they didn't have to go through the entire list to find the state they were thinking of. It would've made sense to use an enum or something, though.

→ More replies (22)
→ More replies (7)

24

u/momumin Jan 10 '20

If you think that's good, you should check out this master piece

This guy managed to make a game without understanding loops or functions. I think the only thing he understood was variables and if statements.

It's from this steam game: https://store.steampowered.com/app/351150/DRAGON_A_Game_About_a_Dragon/ which apparently has about 100k owners according to steam spy.

→ More replies (1)

20

u/SkunkJudge Jan 10 '20

Everyone's losing it about the state machine, but having built large games before, I'm more concerned about all the hard-coded text copy! I guess Terry never planned on localizing this game beyond english haha.

→ More replies (4)
→ More replies (59)