My Lil Website

Minecraft Elevator Control with Lua and ComputerCraft

lua, minecraft, projects

Why Minecraft? You may think of it as a simple blocky survival game, but to me it's an engineering puzzle sandbox. With the right selection of mods, you can do some amazing things in it. Today I'll show you how I built an elevator using a few computers and a simulated pulley!

Some background

The machinery driving today's project.

This idea came to me as I explored the Create mod more deeply. Using the pulley, it's possible to make a moving platform controlled using redstone, Minecraft's digital circuitry mechanic. After building a very simple elevator using it, I thought it'd be fun to automate it properly.

The machinery driving everything
Cogs in the machine.

The first attempt just switched the direction, making it move up and down, and I didn't do much else with it until I remembered about ComputerCraft. With it, I can make Lua virtual machines that can interact with the in-game world.

ComputerCraft computer
A typical Minecraft computer.

Working prototype

Because wiring up the moving parts is pretty much impossible, I decided against a typical elevator setup, with the buttons on the inside. Instead, I opted to put them on each floor.

A row of buttons, labeled Floor B1, Floor 1, and Floor 2.
The button setup I ended up with.

My idea was thus: wire up the computer to all the buttons, and also put an elevator sensor on each floor. On pressing the button, send the elevator to pick up the player, then go to the requested floor. The elevator knows it's at the right floor when the sensor gets triggered. Simple, right?

Well, there were some unexpected issues. First off, the wiring: I could only receive the signals as a 16 bit bitmask, as that's what the cables allow, with each bit corresponding to a single button or sensor. With N floors, each floor would need N-1 buttons, plus the elevator sensor. That means my system would support only 4 floors! Not only that, but during the testing, people would come by my base pretty often. It's annoying when someone requests the elevator, and their request would overwrite mine.

Final implementation

While there were a few different ways around these issues, I thought the easiest would be more computers! By having a separate computer (technically a coroutine) handling the input on each floor, I simplified my code quite a bit. I no longer had to keep track of the bitmask to sensor/button mapping in the controller. Instead, I could use the color standard, with its built-in support in ComputerCraft.

The backside of the elevator buttons. Multicolored wires are connected to them.
The backside of the elevator buttons. Note the multicolored wires connected to them, corresponding to the floors according to the color map. White is parsed as the first floor, orange - second, magenta - third, etc.

Additionally, with each floor needing N-1 buttons, this system supports up to 17 floors! With the Minecraft height limit being at 256 blocks, that seems more than enough. Though at that point the button panels might be a bit too big.

I also implemented a queue for the destinations. This was fun, since I usually don't get to make the data structures myself. Lua is pretty minimal, though, you don't get much by default.

And finally, a working demo


Of course, no project is ever completely finished. There's a few things that could still be addressed. The biggest being chunk loading.

Minecraft's terrain is separated in 16x16 chunks, with a variable amount being loaded at once depending on memory limitations. That means that moving far enough away from a chunk causes it to unload, also destroying any Lua coroutines currently running in a computer. Since the current floor and destination queue are stored in memory, that means the elevator forgets where it is! Fixing this wouldn't be too hard, it only needs to write out the memory to disk every so often. But it's only a small annoyance, since the elevator resets its position on reload.

This was a pretty small project, but I enjoyed it. I think writing this article took longer! The program is up on Github, if you'd like to try it for yourself.