Three Ways Good Design Makes You Happy by Don Norman

Since the beginning of his career Donald Norman has lived many lives, as he tells on jnd.org. “I have lived multiple lives: University professor, Industry executive, consultant, keynote speaker, and…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Bitburner Starter Pack

Elsewhere online, there are Github repos full of scripts to completely automate playing this game, start to finish. However, many aren’t that user-friendly, especially for someone new to programming or Javascript. This article is meant to help get you started on your own journey of automating Bitburner with your own two hands.

I’m assuming you’ve run through the tutorial. You know that you need to connect to a server, nuke it, and begin weakening its security while growing its money reserves so you can hack it. Manually, this can be slow, so let’s start here.

In your terminal, start editing a script called “weaken.script”

This also creates a folder called “scripts” where “weaken.script” will be stored. Inside the script, insert this text:

There are 3 concepts, the while loop, the weaken() function which is Bitburner-specific, and the “args” variable which is short for “arguments”.

While Loop: A while loop runs a set of code “while” a condition is true. The () is where we set our condition, or “is this true?” setup, and the {} indicates all the code we want to run in a loop. We’ve set the condition to “true”, which means we never want our while loop to end as long as the script is running.

Weaken(): The weaken() function does the exact same thing as if we were to run the weaken command in our terminal, with one exception: we don’t need to be connected to a server in order to weaken it. The weaken() function asks for a target server to run weaken on, which we provide within its () where args[0] is.

Args: args is simply a list of words/arguments that we executed our script with. You’ve already added arguments to other commands, such as “mkdir” and “nano” before this. In this case, args[0] indicates we want the first argument in the list. But what is this first argument? Well, that’s the server name we will provide. Let’s try it out.

Press ctrl+s to save the script, return to the terminal, and type the following:

You’ll probably see something like Running script with 1 thread(s), pid 1 and args: [“n00dles”]. Awesome! But what is it doing? To take a look, note the “pid” in the result line and type in tail with the pid number as the argument. For mine, it would be tail 1. This will bring up a log window showing what that script is doing.

I’m not going to sugarcoat it, the numbers won't look good. 3–4 hacking exp and very little security weakened each time. Frankly, you could get better results manually! Look at the response text again: Running script with 1 thread(s). Maybe if we used more threads, we could get better results. Kill the weaken script by either clicking “kill” in the top right corner of the tail window (and close it) or by typing kill followed by the pid in your terminal.

Threads, in reality, are processes that run in parallel. In Bitburner, however, threads are just a multiplier added to your script’s results. How many threads can you run for a script? It all depends on your RAM. Run the `free` command to see how much RAM you have and are using. Look for the number next to “Available:”. Now let’s go back to the script editor with our scripts/weaken.script and look at the bottom of the window to find the RAM requirements. Here it shows “RAM: 1.75GB”. By dividing our required RAM by our available RAM, we can determine how many threads we can use. I’ll use 2 in my example. Go back to your terminal and type in this command:

By using -t, we can specify how many threads we want to use for our script. And just like that, we doubled our weaken power!

Now that we’ve gotten the basics out of the way, let’s create the scripts for hacking and growing.

In the terminal: nano scripts/hack.script

In the script editor:

In the terminal:nano scripts/hack.script

In the script editor

Now that we have the concept of scripts, threads, and RAM out of the way, did you know your target servers have RAM too? So they can run scripts that weaken, grow, and hack themselves! It can be tedious to connect to each one, create the scripts, figure out how many threads to use, and execute them, so let’s create more scripts that do all that for us!
NOTE: The rest of these scripts require root access to the target servers, which means you’ve already executed “nuke” on them. Don’t worry, we’ll create a script later for that too.

Its purpose? To copy our previous scripts to our target server!

In the script editor

We have our server being obtained from our args[0] and scp() is the function for copying files to other locations. Since we are copying from our home server, we don’t need to specify the source server, only the target server. Now that we can copy our scripts to the target server, let’s create an easy way to execute them on that server.

Create a script called scripts/execute.script: nano scripts/execute.script

In the script editor:

This is a lot of code, so let’s walk through this. At the top, we assign our arguments to variables: action and server. Action is how we let the script know whether we want to weaken, grow, or hack the target server. But if we only say “hack”, how will the script know to run “scripts/hack.script”? In short, it doesn’t, which is why we need an “if” statement.

if Statement: An “if” statement checks if the condition within () is true, and if it is then runs the code within {}. If not, it skips over the code within {}. “else if” allows us to say “okay, well if the last if statement’s condition was false, what about this condition?”. In normal language, the code is checking does action equal “hack”, or “grow”, or “weaken”? If so, the variable “script” is assigned to the path, or the location, of the script we want to run. If we misspelled the action or tried to use another action, we end up in the “else” {} which will tprint the message “Script unrecognized. Exiting” and exit the script. tprint is short for “terminal print”, or printing to the terminal. print will print to the log, which we saw when we executed `tail [PID]` earlier.

Threads: But we don’t want to just run a script with one thread on the server, we want to max it out and use all the RAM our target server uses! Servers like n00dles only have 2GB of RAM but other servers will have upwards to 128GB, and we don’t want to waste a single GB. That’s where getServerMaxRam() and getScriptRam() come into play. Using the two of these, we can determine how many threads of the script we can use on the server. However, sometimes dividing one number by another doesn’t give us a whole number return. And since we can only run whole number threads, not 2.5 threads for instance, we need to make the result a whole number. We do this with the function parseInt(). And yes, we can put functions into other functions as those functions arguments.

killall() function: We’ve got our action script, target server, and how many threads we can use, let’s execute the script on that server! With the last exec(), we do just that. Afterward, I’ve added a tprint() to show that the execute.script has finished and what the result was. But let’s say we run this on n00dles with the action of weaken, so n00dles is running a script with enough threads to full up all of its RAM. If I want to switch over to grow, I have to first kill the weaken script so I can free up the RAM to run my grow script. That’s where killall() comes in. Before we attempt to run a new script, we make sure all RAM is available by killing any scripts currently running on that server.

Having to type run scripts/execute.script … every time you want to run the script isn’t fun, so let’s shorten it. Using the “alias” command, we can assign words to use as commands for whatever we want! Let’s try it by running alias execute=”run scripts/execute.script” in your terminal.

There’s only one thing left to do. Assuming you’ve already nuked n00dles, run execute weaken n00dles.

In Bitburner, there are two different ways to write scripts. One is the Bitburner-specific .script language, and the other is Javascript as noted with .js. The Bitburner language is slowed down, which can be changed in the options menu, but simplifies a lot of the tedium that would be necessary in the real-world language of Javascript. Which language you use is up to you but if you want to try learning programming, you will find more help online with .js but will notice some functions won’t work anywhere else except in the Bitburner .script language.

Add a comment

Related posts:

Becoming Wealthy is Much Easier Than Becoming Rich

Rich is something that can come in an instant, such as with a great promotion. It can come in the form of a lottery win or a large inheritance. You can become rich overnight, but wealth takes a…