- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Just goes to show, there are no unbreakable rules.
Not even this one.
Admin
This looks like a code generated by some tool.
Admin
You could not possibly pay me enough to maintain that. Magic numbers, everywhere . . .
Admin
The game's author has an interesting blog post about it: http://distractionware.com/blog/2020/01/vvvvvv-is-now-open-source/
Admin
Yeah, I see no issue there honestly. This seems to be the game physics state machine, this is the code where every nano-second counts - so I can totally see why it's not wasted with calling functions.
Now having a big switch statement itself might be better readable, but there are other ways to make it even better performing; but then it really becomes a mess.
So yes, it's not a WTF, a little bit to clean (aka style sacrificing performance) for my taste coming from game development.
Admin
A note from the project's README on GitHub, near the bottom :
"the Windows version absolutely positively must be compiled in Debug mode, with /RTC enabled. If you build in Release mode, or have /RTC disabled, the game behaves dramatically different in ways that were never fully documented (bizarre softlocks, out-of-bounds issues that don't show up in tools like Valgrind, stuff like that)"
Wow. I think that's all there is to say.
That said, I used to work at a company that maintained and sold power plant and energy grid management software with some files dating back to 1991. They also did not compile with optimisations on simply because nobody really knew what would happen. Of course, that's not an excuse, but it's a reality.
Admin
You totally missed the WTF: //NO DEATH MODE COMPLETE JESUS
Admin
Okay, a quick look at the aforementioned blog post explains a LOT :
"There’s a lot of weird stuff in the C++ version that only really makes sense when you remember that this was made in flash first, and directly ported, warts and all."
This makes this not a WTF at all, in my opinion.
Admin
This reminds me of toby fox's (creator of Undertale) 1000 case switch statement:
https://knowyourmeme.com/photos/1064864-undertale https://twitter.com/twincannon/status/779475168699842564
Admin
Respect!
Admin
Not a gamer, not a game coder, but don't games these days localize strings and support languages other than English?
Admin
The comment about /RTC is suspicious..... https://docs.microsoft.com/en-us/cpp/build/reference/rtc-run-time-error-checks?view=msvc-170
Admin
Perfectly fine compared to the app I helped refactor in the 90's. It was a Mac client for the company's product, the entire app was in a single 30K line source file (.c), and the main event loop was 14K lines long, indented so far on a wide display you had to scroll 2.5 screens to see the code in the middle. Typing at the bottom of the file was so slow it was amazing anyone could work on it. Of course it also crashed a lot and customers hated it. After refactoring (with a chainsaw basically) the app was functional and stable.
Admin
A well-judged comment. And it should put paid to this nonsense above about "every nanosecond counts, functions are filth, blah blah blah."
If the whole thing is so botched that it needs to be built and run in debug mode -- I can't see how a count of nanoseconds matters. (Not that anybody who writes this sort of code actually counts nanoseconds. That would require logging, which obviously you would set up under the DEBUG flag, which, er, obviously would be a leetle expensive when running the game, since it has to be built in Debug.
Loops, you can unroll. (It may be less necessary these days, since compiler tech has advanced a lot.) Functions, you can inline or jitter.
Shit like this, you just flush down the toilet. Flush three times to make sure it's gone.
Admin
Sometimes you just have to do different things based on a variable that can have 4000+ different values.
I guess it might be slightly more readable with the same performance if you define an array of inline functions, but you would still end up with a 4000-line array definition.
Admin
What happened to case 3?
Admin
Meh, not interested. Call me when this code controls an anti tank rocket.
Admin
@Tinkle, the same as to cases 6, 103, 105,.....
Admin
And case 6 ...
Admin
Okay, I can believe that this... thing... came about because of porting from (eww) Flash. It's still a complete mess, but it has the vague shape of a state machine pattern, and those can get pretty random if you're not careful. I've seen 10K+ LOC switch statements that should have been a method each on the 300+ already existing classes, and that offended me more.
Random spotted weirdness: case 130 has a "state++", but there is no case 131
Looking around a bit more, this happens a lot, it just casually sets state to some arbitrary unused value, without even a default case to clean it up. The missing case 3 is just the first of these. At least BASIC would tell you when you tried to go to a line number that didn't exist.
Admin
The really nasty bit is just using all those state constants as numbers. Naming them would have helped a lot, and cost no runtime performance at all.
Admin
Could you come up with memorable names for roughly 4100 states? Yeah, I also noted some missing states, but this code was too long to do more than a cursory glance.
Admin
Space for future expansion - to avoid having to renumber 4100 states when inserting a new one.
Admin
I used to think I wasn't qualified to work in the game industry. boggles
Admin
Actually it is. Or, at least it contains a WTF.
I did a search. There is no case for 1002, nor is there a default case. There should at least be a default that catches unhandled cases and prints a diagnostic.
By the way, I found this by scrolling through and stopping at random. It's therefore probably not the only instance of this bug.
Addendum 2022-02-23 11:25: Looks like I was ninja'd by I dunno LOL ¯(°_o)/¯
Admin
I suspect the default case when advancetext = true is to auto-increment the state afterwards.
Still a little bit of a WTF
Admin
I once worked with GSM mobile phones before they were based on an actual operating system, but instead on an 8MHz 68010 processor .
When they would actually have had a state machine with states like "editing a JPEG while playing music and a text message has just arrived"..
The language used was not C/C++ but its sort of familiar.
Admin
When I got a tutorial book on how to write a windows application, I was stunned to discover that a basic "Hello, World" application for Window 3.0 took many pages of code and it was all located inside of a switch statement. The book got worse as the examples got more complex. At that time, I was already a seasoned programmer, so I had the good sense to be appalled and switched my own designs to use function pointers for state engines and messages as well as multiple source files for projects.. But I also knew how many new programmers would take that sort of design as a proper paradigm for everything.
Admin
The phrase "...shipping something out the door on tight timelines and tight budgets, and it's very much the category of "if it works it's good"..." covers more than just the games industry.
Admin
How about an anti-missile rocket instead?
http://www.cs.unc.edu/~smp/COMP205/LECTURES/ERROR/lec23/node4.html
Admin
A state machine is a state machine. I don't see the value in breaking out most of those little routines. The "cleaner" ways to write such things are very often harder to deal with.
Magic numbers, though, GAHHH!!!!
Admin
This is basically how I used to write text adventure games. With QBasic. When I was 10. Oh the times.
Admin
Talk about some jump table optimization
Admin
Yes. I admit that it'd take a while. There are fewer states than 4100 with quite a lot of omissions; there's at least some structure in the arrangement of the numbers, even if not very much, and where there's structure, there's a possibility of structured naming.
The old-school BASIC programmer in me remembers that sort of pattern of numbering things. It was pretty horrible even then. I'm so glad that there are better ways! Cold turkey for decades…
Admin
using hammers to put screws in... CHECK. using screwdrivers to pound nails in... CHECK. Yes... that means... "SHIP IT".
Admin
Not really a WTF. I agree with Loren Pechtel here.
The magic numbers are probably spaced to allow for insertion of new states. Having at least the first state of a section named would help a lot.
On the other hand, all code is clear, readable and not infested by hipster "wah wah a method must only have 20 lines max, a method must not have a "complexity" factor greater than 20, and other similar nanny hand holding ideas"
Like it or not, this code is maintenable. You do not need to search all over 20 files with 10 hollow interfaces and factory classes to find the actual three code lines doing something.
Admin
Everyone how is wondering why not all states in the state machine are implemented; keep in mind, C/C++ doesn't required a default section, in fact fall-through is the default in a switch statement.
And it is totally normal, that the state of a state machine is changed outside of the state machine (and games have multiple ways how this can happen).
And when it comes to flash to C port: A state machine has been the core of game physics for nearly half a decade in video games, so yeah, welcome to writing code that is executed in 1/600 seconds ;-)
Admin
I beg to differ. No matter how you look at it, a 3400 line switch statement is definitely a WTF.
Admin
Sure....
STATE_1
STATE-TWO
STSTE-THREE
etc.etc.
Admin
That's sad. Let's remember our fallen, especially those 28 - such nonsensical, unneeded deaths.
Admin
At least we now know how Nintendo came up with the name for its newest consoles…
Admin
cool man
Addendum 2022-12-22 19:35: You need to have enough knowledge to stay current in any area. I do this since it's convenient https://bestcasinoplay.ca/online-casinos/ruby-fortune/ I'm able to select just the most reputable gaming sites with legitimate licenses, RNG-based software, and the best bonuses thanks to this website. Additionally, there are no additional fees for players at these top online casinos.