
The story of how I wrote the OS in Minecraft

Back in 2011, the world saw a game like Minecraft - a voxel sandbox where everyone creates everything that his heart desires. After some time, craftsmen began to write various modifications for her, making the gameplay more diverse. Not so long ago, a mod called "OpenComputers" appeared, adding full-fledged computers programmed in the Lua scripting language. And in this article I want to share with you a little experience gained in studying this extremely interesting mod.
Learning Mod
If you put a computer in front of you and install a standard console OS on it from local floppy disks, you can be slightly disappointed in the mod’s functionality. In fact, at the start we have a command line with a rather nasty looking interface, we have a set of standard commands of the type ls, edit <file>, remove <file> or mkdir <folder>.

The whole thing decidedly did not suit me, and I decided to add more pleasant colors to the console and Russify the tips for the mod, since the author allowed us to edit the source code of all his developments. The Lua syntax, its structure and capabilities surprised me very much compared to the same unfriendly to the novice SI family or even more plain Java, it has absolutely everything you need out of the box, and the documentation for the mod is written in a simple popular language. At first I began to write simple scripts a la “Hello world”, a little later I switched to all sorts of small programs for controlling doors or lighting in a game house, but all this made me feel bored and simple. I wanted a hardcore!

Gradually studying the local libraries and features of Lua, my friend and I were able to make simple GUI applications. Having searched all kinds of Wikipedia, we found several color-handling algorithms - for example, for converting color from RGB to HSB format or for overlaying one color on another, taking into account transparency. Having tidied up a bit, we managed to implement these algorithms on Lua, and so our first library for this mod appeared, we called it colorlib, and with it we were able to compose such a nice palette:

Having decided not to stop there, we thought that it would be cool to make a graphic editor for this mod, as well as our own image format. Indeed, why not? So we could easily draw beautiful graphics on the monitor. After spending a few days, my friend managed to implement a competent file structure for future pictures, which ate very little space on local hard drives, which, by the way, have a limited amount of 4 MB. We designated the format abbreviation as ".pic", and the format itself was called "OpenComputers Image Format", abbreviated as "ocif". After all this, we started creating the graphical editor itself, spending about three days on it. As a result, we have this:

The structure of the .pic file in hexadecimal form can be seen in this picture:

First of all, we added the most necessary tools to our graphic editor - a brush, an eraser, and also text for creating labels, because in fact we have a console where each pixel has three parameters: text color, background color and symbol. A little later we introduced a recursive fill algorithm for more convenient creation of pictures, and a little later we were able to teach our editor to work with a real PNG image format (by the way, using an article from Habr). So, we have a working image format, but we don’t know where it can be applied. We began to write various applications for this mod, for example, such a cute calendar:

Or an application that generates digital rain from a well-known film:

In the end, we had so many different scripts and libraries that we got tired of manually loading them using the standard method via Pastebin.com. Yes, the mod has integration with pastin, so we all of our files were stored on it initially, downloading them as needed on computers. But considering the number of files, we decided to create an installer program that automatically downloads the list of files from our repository to GitHub, and then downloads the files from the list to the desired directory. There was quite a lot of hemorrhoids with file transfer from Pastebin, but after a couple of hours we coped with the task and started coding the installer. The result is this:

So, we could already code simple applications with pictures, had a convenient installer for them, but I wanted something more! The idea of creating an operating system somehow came by itself, and without thinking twice, I decided to make a few outline icons for a future OS. For example, the folder looks like this:

Having drawn the basic icons, I started creating the operating system itself, immediately giving it the name “MineOS”. The basic concept is very simple: in the MineOS / Desktop / folder there are files of any type. Using the standard getFileList () function, we get a list of files, get the file format by trimming the string file name, and then draw icons on our screen in accordance with the received format. Something like this came out:

Then I attached a bottom menu in the style of Mac OS, added a menu on top, put in a library of contextual interaction - and as a result, we already have something worthy of the name “graphical shell”, but so far it has not reached a full-fledged OS.

Everything has its drawbacks: local "video cards" provided by the mod have a limited number of operations that they can do in a game tick. Operations mean changing the color of the text, the background color, setting the character, etc. It is easy to imagine how many operations were required to render our pictures, and even more so to render the desktop. But the biggest sadness was that I needed to redraw the desktop completely, for example, if I opened the context menu or launched some kind of file. But then another friend came to my aid and advised me to write an algorithm for double image buffering. For those who are not in the know, I will briefly explain its essence: in RAM we have two image arrays: the first contains information about pixels, which are currently displayed on the screen, and the second - information about the pixels that we want to draw. Then, by calling the calculation function, the algorithm checks these two arrays and displays not all pixels, but only the changed ones. And then assigns the value of the second to the first array.

Also, I built several algorithms into this library to speed up the work of local GPUs, grouping neighboring pixels by colors and combining them into one line instead of drawing alternately.

And only now my OS has earned as it should! Icons were drawn almost instantly, it became extremely pleasant to interact with a computer. Having decided not to stop there, I added several functions simulating the transparency of the image, and with their help I managed to achieve pretty beautiful results.

Then I compiled a beautiful Finder-style file manager from the same Mac OS, and integrated it into the system. I also added sorting of files by name, type and date of change, added hiding the image format, added a favorites menu. As a result, we have the following:

All this already looks pretty good, but behind me there is such a sin as a strong love for good graphics. So I decided to add wallpaper support. Here are some examples:




By the way, today I wrote a HEX file editor, since I do not like to pay for ready-made solutions sold on the Internet. This is how it looks in combination with a desktop wallpaper.

So, our OS is ready! In total, we have about 28 of our own libraries, while some of them have more than 2 thousand lines of code. We also have about 30 high-quality GUI applications. Almost a year has passed from the beginning of the study of the mod to the present time, and we did not program every day and every week. This is how this operating system was written, and after this, this article.
How to download?
If you want to feel this OS with your own hands, then you are welcome, here is a brief instruction: put the computer case on the ground, attach a monitor to it, and attach a keyboard to the monitor. Then insert the best video card, processor, RAM, hard drive, Lua-bios, a standard diskette with OpenOS (scripts by the author of the mod), and also an Internet card (not a network, namely the Internet) into the case.

Then turn on the computer, wait until the system boots from a floppy disk, and then enter the install command in the console . Next, select the hard drive number, press enter , and the system will be delivered. Next, restart the computer.

Now your computer is ready to install my operating system. Enter the command pastebin run 0nm5b1ju in the console , pressenter and follow the instructions on the screen. I wish you a pleasant use.
Total
Why, in fact, did I write this article? I wanted to show people that in any language and in any environment you can create a small, not afraid of the word, masterpiece. Only a year and a half ago, I very vaguely imagined what a “function”, “array” or “class” is, and now my friends and I are coding our own mini-OS. The main thing is to have free time and a desire to spend it profitably and with pleasure, for me personally there is no more buzz than coding any nonsense in my favorite game. I wish all beginning and not very beginning programmers creative success!
Source code
If anyone is interested - you can dig into the raw materials , there you will find all the libraries, the OS itself, programs and images.
The authors
Just in case, I want to list the remaining people who were somehow connected with this OS, who, just like me, were interested in studying this modification, and the Lua language in general.
- Gleb Trifonov is a developer of the image format, as well as half of the libraries.
- Yakov (surname unknown) - developer of the concept of double buffering, an assistant in code optimization.
- Maxim Pakin is the author of several programs for the OS.
- Timofei Shestakov - moral support for the team, the author of several interesting ideas to speed up the OS.