As a bit of a diversion from all the networking code I’ve been wrestling with over the past several months, I decided it was high time I started thinking about how the electronics would operate in my game. I realize of course that thinking about how the game itself would work, or look (graphically) might be an even better use of my time, but when you have the motivation to try to figure something out you should jump on it.
When you consider how things work in Minecraft, you often think of their somewhat miraculous redstone system. Redstone can be used to make things move and light up, and the true experts have even built simple computers using vast collections of redstone pieces. It is easily one of the most interesting parts of Minecraft to me, but when playing the game I often feel its limitations and occasionally odd requirements take away from my pleasure.
For example, I built a “monster farm” in Minecraft that provides a dark place for monsters to spawn, and then it attempts to kill them with an elaborate collection of pistons and flowing water. The circuit involved had big loops of repeaters to create delays and a couple of XOR gates to drive decisions. The XOR gates I found the diagram for online (because I couldn’t figure out how to do it on my own) required amusingly X-shaped structures that used something like 5 redstone torches, several blocks that were carefully spaced apart, and a few bits of redstone wire. I found this highly inefficient.
This is what drove me to the concept that QubeKwest should simply have electricity. You know, good old fashioned power sources spitting out power that works its way through a circuit to ground. Once I’d made that choice, it seemed like it would be pretty straight forward to simply provide several of the things that were tricky to build in Minecraft as blocks. If you want an XOR gate, make an XOR gate block and drop it on the ground. That’s it.
QubeKwest now has two new packages related to the logical underpinnings of the electronics system in the game. The base com.qubekwest.electric package and the nested com.qubekwest.electric.component package. These were separated because I knew going into this that there would be dozens of components and I wanted a separate place to put them all that wouldn’t end up confusing the basic things.
Once the structure was in place, I spent something like 3 days pondering how things would work. Then I spent 2 more days drawing things in my notebook related to electric stuff. Then I started coding. Got it utterly wrong. Refactored it. Still wrong. Massive changes made things much better, but still not great. Made more progress, realized my current layout was making things unnecessarily inefficient. Came up with another change to fix that. Spent another day refactoring everything. Then I had some things working for real, and some of my tests written to make sure they were working. Several days later, I’m starting to really have something here.
The basic idea is a collection of components that have at most 4 inputs or outputs. After all, everything is 4-sided in a world made up of cubes. Keeping in mind that all of the components are logical constructs only, here is the list of components I’ve written so far and some brief descriptions of what they are:
- Power Flow
- Power Source – Any source of power. In the game this could mean a battery, nuclear reactor, solar panel, or anything else that puts out power.
- Ground – The final target of all circuits. Without getting to ground, the circuit won’t do anything.
- Light Sources
- LED – A light source that only lights up when powered. These only have 1 brightness level.
- RGB LED – A single LED that provides separate levels of brightness for red, green, and blue parts.
- Logic Gates – Power level 0 = false, 1-15 = true.
- And – Output is true only if both inputs are true.
- Nand – Output is true unless both inputs are true.
- Or – Output is true if either input is true or both inputs are true.
- Nor – Output is true only when both inputs are false.
- Xor – Output is true if one or the other input is true, but not both.
- Xnor – Output is true only if both inputs are the same.
- Not – Output is the opposite of the input.
- Resistance
- Resistor – Reduces the power level by a user set amount.
- Potentiometer – Reduces the power level by an electrically controlled amount.
- Timing
- Timer – Takes in constant power and outputs it on a user set schedule.
- Variable Timer – Takes in constant power and outputs it on an electrically controlled schedule.
- Switching – These switches could take several forms each in the game.
- Switch – Normal on/off switch activated by the user.
- 3-Way Switch – Never truly off, just sends input power to either one output or the other.
- 4-way Switch – Never truly off, either passes power straight through, or flips the conductors to be crossed.
- Selector Switch – Like a 3-way switch, but with 3 outputs.
- Dual Switch – Single control, dual switch. This switch has 2 independently wired switches in it that share a single on/off state.
- Separate Flow Switch – Has 2 paths for power though it, and either allows one path to be closed, or the other.
- Relay – A normal switch except that it is controlled electronically instead of by the user.
- Lit Switch – A normal switch that has a separately wired light in it. The state of the switch is not connected to the state of the light.
- Wiring
- Wire – A simple conductor.
- Wire Bundle – A bundle of 9 independent conductors.
- Wire Junction – A junction point between up to 4 wires.
- Coming soon…
- Multiplexer – A device that takes in bit input lines, and outputs a power level that is the conversion of those inputs to a number.
- Demultiplexer – A device that takes in a single power level number and enables separate power lines that represent the bits in that level.
- Wire Bundle Breakout – A device that gives separate access to each conductor in a wire bundle.
The above work represents something like 5000 lines of code added over the last week. This diversion has proven to be quite entertaining and productive indeed, and until I’ve finished testing the various components I’ve created, I can’t really say I’m done.