My Very First Mod – Making Better Pickaxing in Terraria

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:

Terraria - Microsoft Visual Studio 2010 Express for Windows Phone_2013-06-04_14-55-01

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:

Terraria - Microsoft Visual Studio 2010 Express for Windows Phone_2013-06-04_15-04-12

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.

Download link:

http://www.aaronmisner.com/other/downloads/Terraria%20Better%20Picaxing%20Mod.zip

Here’s a video demonstrating the functionality:

Advertisements

About Aaron

Avid computer software programmer and all around digital designer. I live for the challenge and excitement of computers and bending them to my will. Computers are a canvas for creating fantastic pieces of art that millions of people can interact with worldwide. That alone is reason enough to be a digital designer.
This entry was posted in Uncategorized. Bookmark the permalink.

4 Responses to My Very First Mod – Making Better Pickaxing in Terraria

  1. Jesse says:

    Wow, this is a nice mod. This will really help mining in the game. Did you use tconfig? http://www.terrariaonline.com/threads/released-wip-tconfig-a-mod-to-make-mods.52207/
    Do you think this mod is compatible with other mods?

    • Aaron says:

      Hi, thanks!

      Nope, I didn’t use tconfig. I hard-coded it in myself just to see what I could do. It’s unfortunately not compatible with other mods.

      With Terraria 1.2 coming out on October 1st, 2013 I won’t be updating this mod for the current 1.1 version. I’m thinking 1.2 will make this mod obsolete as well. I’ll have to see how it all plays out.

      But thanks again for the comment!

  2. Ryan says:

    Awesome man! This is almost exactly what I was looking for. What I was really hoping to find was a mod to where you could mine in 3×3 grids rather just vertical, just like how you can mine in starbound though….. BUT this is the best I can find as far as man ng with terraria, great job man! Will download first thing after work tomorrow, also I love how you made it to where you could still just mine one block at a time holding shift just like in starbound as well ^_^ Ty man, I’ll put the mod to good use.

    • Aaron says:

      Thank you! I’m glad you like the mod. Changing the mod to mine in a 3×3 grid would be a simple change, but I would have to go through the whole process of decompiling the source for the new version of Terraria again and I’m not sure I would have the time or patience to do that again (unless I can obtain source that was already decompiled or use the new version of tconfig to add the functionality I need). I’ll have to see about that. Well let me know how badly you would want it to be 3×3 and whether or not you’re using the newest version of Terraria.

      Thanks again for liking! 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s