Terraria Source Code (2013)
raw.githubusercontent.com
raw.githubusercontent.com
Don't judge the developers based off of this.
I'd go further and say "never judge a developer by the code they write without knowing 100% of the context". So many times developers trash each other for shitty code quality without knowing anything about why the code was written the way it was. There isn't always good reasons for "excellent code", sometimes good enough is perfect. We like to think we all live in a vacuum where there is no reason for code to not be perfect, but we forget we live in an environment which is not perfect, and the code is meant to contribute to the this environment, sometimes leading to less-than-perfect code.
So before you judge your fellow developer based on code, ask them first to explain the context around the code, before jumping to any conclusions.
That's a really delightful take! Makes me feel like the code I throw at the wall to see what sticks does make a difference in my corner of the world.
When you add that the definition of perfect can be subjective, one person's definition of perfect might have evolved into it by passing through what is now their good enough definition.
In a perfect world, that context would go right into comments next to the code.
Having looked at a few games' source code, are there really any complex games with pretty code? With as many subsystems, items, and enemies as Terraria has, it feels almost inevitable that you'll have complexity like this snippet for determining when to spawn the hair stylist NPC:
else if ((int) Main.tile[index1, y].wall == SpiderUnsafe | flag10)
{
if ((int) Main.tile[index1, y].wall == SpiderUnsafe && Main.rand.Next(8) == 0 && (!flag6 && (double) y >= Main.rockLayer) && (y < Main.maxTilesY - 210 && !NPC.savedStylist && !NPC.AnyNPCs(WebbedStylist)))
NPC.NewNPC(index1 \* 16 + 8, y \* 16, WebbedStylist, 0, 0.0f, 0.0f, 0.0f, 0.0f, (int) byte.MaxValue);
else if (Main.hardMode)
NPC.NewNPC(index1 \* 16 + 8, y \* 16, BlackRecluse, 0, 0.0f, 0.0f, 0.0f, 0.0f, (int) byte.MaxValue);
else
NPC.NewNPC(index1 \* 16 + 8, y \* 16, WallCreeper, 0, 0.0f, 0.0f, 0.0f, 0.0f, (int) byte.MaxValue);
}I haven't seen the Factorio source code, but having read their blog enough, I have to believe they have beautiful source code.
Games don't have the same maintenance requirements as a web application. They are made, shipped, played, and forgotten. So code cleanliness is generally an afterthought. And honestly, most indie game developers are not great programmers. But making a great game has very little to do with programming nowadays. The tooling has come so far that many hit indie games are now built using "no-code" solutions like Unreal Blueprints. And advertising that you hand coded the entire game in C with immaculate coding standards wont make it any more fun.
Terraria was released in 2011 and is still getting updates.
Yes, it was a runaway success. Terraria is a huge outlier in the indie scene for lasting so long. But my point is that the creator almost certainly was not planning to be maintaining this code 10 years later when he wrote it. And so it was written for speed and simplicity, not elegance and maintainability.
if (num9 + num7 >= num6)
{
num8++;
num7 += num9 - num6;
This looks a lot like decompiler output.From what I can tell this was originally motivated by bugs in the German version of the game. Back then 1.1.2 was to be the last version of Terraria (a running joke at this point, they announced 1.4.4 a little while ago) and so decompiling seemed like the best option to get those bugs fixed. This might be a more interesting blog post on that topic: https://thevamp.cc/2012/07/16/terraria-1-1-2-zu-terraria-1-1...
"I know that this is a real mess. But back in the days the decompiler was not so good to create super fancy code and I was to lazy to rewrite thousands lines of code."
it would seem decompilers are not that funny.
What would a metric such as "clean code score" help with any of this?
Source: 12 years in gamedev ;P
The repo says this code is only for educational purposes. This spaghetti can be still a good example for the reasons you mentioned. Or maybe it is a good example for an ugly prototype version of an game.
This could be even better if people could see a refactored version of this codebase.
- Former game developer
Yes I know this sounds bad - but that's how the games industry works. And is one of the reasons I left.
Note that if you are working in engine code, then you want all the automated tests, proper architecture, etc.