hackmud

hackmud

Not enough ratings
How to shrink your scripts
By G'lek
The game starts you with a 500 character cap on script sizes. That's not much, but you can do a surprising amount with it by using a technique called 'minification'. This guide will walk you through getting started with minification.
   
Award
Favorite
Favorited
Unfavorite
Getting the Tools
To minify your scripts, you'll need a few tools and some knowledge of working with a command-line interface (similar to Hackmud's).

The tools you'll need are:


Go ahead and download Nodejs and install it.

For the curious, Nodejs is a command-line JavaScript 'interpreter', which is a fancy way of saying it reads JavaScript programs and runs them. Since Hackmud uses JavaScript (or something close to it) for its scripts it's worth playing around with Nodejs to get a feel for how JavaScript works.

Once installed you should have a new program called 'Node.js command prompt'. Go ahead and open that now.

You'll want to run the following command to install UglifyJS.

npm install -g uglify-js

Again, for the curious this runs the Node Package Manager (NPM) and installs the UglifyJS package globally (meaning you can run it from the Nodejs command prompt).

You're now ready to minify your scripts!
Minifying your scripts
Since this guide is not about writing scripts, the assumption is you know how to write a script. Instead, it's going to teach you to minify your scripts using UglifyJS.

Before going too much further, be aware that, by default, Hackmud scripts won't work with UglifyJS because of some special syntax that Hackmud uses.

There are two things to note:

  • The function your code runs in MUST have a name to minify it. You can edit the minified file afterwards to remove the name and make it compatible with Hackmud.
  • Any calls to '#s' will break UglifyJS.

The second point may seem like a very strict limitation, but you can get around it by assigning the '#s' object you want to call to a variable. For example:

#s.scripts.lib().is_obj(thing);

would become

var lib; lib.is_obj(thing);

Now, this will not work out of the box. After you minify the file, you will need to fill in the 'blank' by adding 'lib=#s.scripts.lib' to the minified script.

Open the Nodejs command prompt and change to your script folder using the following command:

cd C:\Users\<YOUR COMPUTER USERNAME>\AppData\Roaming\Hackmud\<YOUR HACKMUD USERNAME>\scripts

Once there, pick a script you want to minify. Then make the following call:

uglifyjs <SCRIPT>.js -m > <SCRIPT>_min.js

You have just minified your script! If you open it up, you should see 'gibberish'. This gibberish is still valid JavaScript, though, and it's much smaller than before too!
An example
Here is an example script before minification, after minification, and after 'touch ups'.

Before Minification:

function main (context, args) { var lib = 'lib'; // TODO Manually add lib here later var caller = context.caller; return lib.ok(); // Call to lib, which is normally in #s.scripts.lib(); }

After Minification:

function main(r,a){var n="lib";var i=r.caller;return n.ok()}

After Touchup:

function(r,a){var n=#s.scripts.lib();var i=r.caller;return n.ok()}
Legal/Misc
This guide belongs to myself (G'lek) and is posted on Steam with my express permission. Reproduction of this guide elsewhere is strictly forbidden.

Feel free to comment if you need additional help or find mistakes in this guide.
12 Comments
novocain Nov 25, 2017 @ 10:24am 
Resources for JavaScript (incl. ES6) code golfing (attempting to write as short code as possible)
http://codegolf.stackexchange.com/questions/37624/#answers
http://codegolf.stackexchange.com/questions/2682/#answers
Jolley Oct 29, 2017 @ 7:57am 
There's a new tool that does all this for you, but better. It's called 'muddler' https://www.npmjs.com/package/muddler
Fae Forest Oct 11, 2016 @ 6:41am 
It isn't giberish. It is literaly the same script just using less characters. You can do this manually.
G'lek  [author] Oct 4, 2016 @ 3:46pm 
@Kilandor
Interesting. I didn't realize Hackmud supported ECMAScript 6, or at least the arrow syntax part of it. Now I'm curious what else it supports for ECMAScript 6... Thanks for the insights!
Kilandor Oct 3, 2016 @ 9:35pm 
So I added this it saves several chars, it replaces functions with the arrow mention in dtr's golfing tips. It expects alpha numeric and underscore as options for the function name and the variable preceding it
sed -r 's/([a-z0-9_]+).?=.?function\(([a-z0-9_]+)\)/\1 = (\2) =>/g'
Kilandor Oct 2, 2016 @ 11:22pm 
Another tip to save space if you have multiple variables to declare/init you can do so with only having to use var once such as var foo = bar, bar = foo Normally I wouldn't recommend it, its messing for coding and ugly. For this however it can save space. As this still allows uglify to mangle the variable names to shorten them
GordonFreescale Oct 2, 2016 @ 6:02pm 
Seems fairly intuitive, and the kind of thing you could do manuall, so long as you keep a sickynote handy with variable name translation lookups.
Kilandor Oct 2, 2016 @ 4:15pm 
I actually modifed it to do that already myself
uglifyjs lock_sim_dev.js -m | sed -r 's/main\(/\(/g' | sed -r 's/HASHTAG/#s/g' > lock_sim_dev_min.js
In your code just replace #s with HASHTAG and sed will replace it back after it is done
G'lek  [author] Oct 2, 2016 @ 6:47am 
@Kilandor
Good advice, though it doesn't account for needing to manually insert '#s' references into the final script since UglifyJS won't parse the file with it present. I might play around with using sed to do so instead of needing to do it manually. Thanks for the idea!
Kilandor Oct 2, 2016 @ 12:47am 
I will ofer a little help...This isn't for windows, its for Nix, or say Ubuntu Bash on Windows 10
This assumes you are using function main( to define the function.
It runs uglify and then replaces main( with just ( and then outputs it to a file. This way you don't have to go back and manually edit it
uglifyjs script_input.js -m | sed -r 's/main\(/\(/g' > script_output_min.js