So yesterday I decided to step into the world of modding first-hand for the first time. I never was the sort to hack and change other people’s work, but I always find something “wrong” with the games I play. When I say “wrong”, I don’t necessarily mean bad, I mean inadequate for my own selfish desires. The popularity of Minecraft caused a veritable explosion of mods for that game. Modding comes about when players want something more out of the games they play. Because no single developer (or team of developers) can ever truly make the all-encompassing game…inevitably, omissions will arise in the eyes of players.
I first started considering modding after the popularity of Minecraft reached epic proportions. I lackadaisically explored the possibility of modding: I didn’t even know where to begin so I just did some light research and some Google’ing (funny that this has become a verb).
Now I’ve always been really good with computers. I’m a programmer at heart and I’ve written countless programs myself, but I never put any thought into what it would take to actually “mod” something. I used to think the programs were bullet-proof shells that only the original programmer knew how to work with, tweak, or expand on. I was so wrong…
Minecraft was programmed in a language called Java. Its semantics and syntax are similar to (or at least along the same lines as) C++ and C#. It’s an object-oriented language, which are languages I’m most familiar with and have the most experience in. I love being able to treat programming concepts in terms of objects…anyway. Even though Minecraft was made with Java, I’ve never done any serious work in that language, and although it’s in the same neighborhood as C++ and C# (which I’m good at) it still has its own rules and meanings that you have to learn and understand. The most critical thing about Java is that it’s designed for cross-platform compatibility and works well as a platform for web apps. Of course, Java can make standalone desktop programs as well.
At any rate, I was still too much of a modding “babby” at the time to do any serious work with Minecraft modding. Plus, I had gotten tired of the game by that time anyway.
That was about a year and a half ago…fast forward to present day!
With the recent surge in interest of Terraria (with its new 1.2 update coming in July), I decided to play it again. I immediately started a new character and a new world. I decided to do hardcore mode (which has nothing to do with the content of this article, but I digress), I played a good 4+ hours before I died. During my play, I remembered the thing about the game which made me sore (literally). That thing was the mining. Mining in Terraria (and by the same token Minecraft) requires holding down the left mouse button on a single block in the world until that block breaks. As you progress in the game you can get better pickaxes that gradually reduce the time it takes to mine a single block. But in the early game, you do a lot of holding down the mouse button, which for me strains my index finger. I don’t know, maybe it’s my mouse, maybe it’s my dainty, weak fingers, but the point is it’s uncomfortable after a while and more to the point, it never made much sense that a pickax could only break one tiny little block at once (especially since the player swings it in a wide arc over its head, giving the impression that you should be doing some serious damage to the surrounding earth).
Sidenote: Minecraft suffers from the same problem, although in Minecraft the blocks visually look much bigger in first person and therefore the effect isn’t as noticeable and thus, isn’t as much a problem (but don’t mistake me, I still consider it uncomfortable in Minecraft as well).
So to address this issue, I did a random search for some Terraria mods just to see what was out there. It turned out that there were a lot of mods for the game. This was surprising because I didn’t think Terraria was designed for mod support at all (I was correct about this), however (and what shocked me) was that this didn’t matter at all.
The quintessential thing I learned about modding was this:
* A game doesn’t need to provide mod support in order to be modded *
Of course, I knew through my existing knowledge and experience with computer science and how computers and software worked, that “technically” any program could be cracked open and reverse-engineered. I’ve seen countless EULA’s which explicitly say not to do so. My ignorance was in the fact that it actually isn’t as difficult as it seemed. Back when I was checking out how modding worked for Minecraft, I learned about a program that decompiled Java binaries or object code back into .java source code. The thing is that even though Notch obfuscated his code (a thing many developers do) before building the executable, one can still decompile and figure out how the code works. This was the key I was missing. I gave up trying to mod Minecraft at the time but I kept this knowledge for use when I made this mod I’m about to describe for Terraria.
Now I knew Terraria was developed in XNA, but of course I didn’t think to look around for C# decompilers back when I first started playing the game a couple years ago. It turns out though, that there is a nifty little program called ILSpy, which decompiles IL (Intermediate Language) for Microsoft .NET programs. I read a thread on the Terraria forums talking about it and that’s how I found out about it.
I downloaded ILSpy and gave it a whirl. I figured I’d just see how far I get. It turns out that I got pretty far. :). Even though the source code I got from the decompiler wasn’t very readable (it has no comments because comments aren’t built into the .exe), it actually still had lots of symbol names to help figure things out. The textures, tile data, and other useful game objects had meaningful names, which gave me the impression that Re-Logic didn’t obfuscate the code (although I don’t know for sure). Either way, I personally have never obfuscated my code because I’ve never actually released a commercial title, but I sincerely appreciate Re-Logic for not doing so if this was the case.
After a little refactoring and commenting, I worked out where to put the new code to make my feature a reality. I added the following code in the PlayerUpdate() routine of the Main class:
This code implements the 3 block (Terraria calls them Tiles) breaking functionality and also the damage tracking.
Now breaking 3 blocks at a time and having the damage be stored for each block was fun enough, but to really make it look good, I decided to see if I could make the blocks show a damaged overlay on top so that the player could see that he was in fact damaging the blocks.
I added the following code to the DrawTiles() method of the Main class:
This draws a certain “damage overlay” image on top of any tiles that have the appropriate hit damage.
Also added (but not shown) is code to load these four textures from the disk and store them in the static variables used in the Draw method.
All in all, it was a very rewarding experience. If you own and play Terraria and would like to try this mod out for yourself, click the download link below. Instructions for installing are in the zip file.
Here’s a video demonstrating the functionality: