Compile python code to Mindustry microprocessor instructions
Project description
Minpiler
Allows easier control over Mindustry microprocessors. You can write subset of Python instead assembler-like Mindustry instructions. Consider this as effort to reuse Python syntax, rather than Python implementation.
Warning: Everything that requires dynamic memory doesn't work: any data structure (list, dict, etc), classes, closures, recursion. Additionally you can't use any Python builtins (input, eval, Exception, etc). Imports do nothing, use it make flake8 happy. Explanation below.
Rationale
Mindustry microprocessors are quite nice and easy to understand. They have explicit limit of instructions per unit of time. They can operate on floats, refer to objects (null, constant strings, @-objects, in-game units) and fire several commands that affect the game.
Here is an example program that switches unloader output between lead and titanium depending on time:
read time cell1 0
op add time time 1
jump 4 lessThan time 300
set time 0
write time cell1 0
jump 8 greaterThan time 200
control configure unloader1 @titanium
end
control configure unloader1 @lead
This program is fairly simple, but if you insert any instruction in
the middle of program, you have to rewrite many jump
addresses.
What if you want to calculate some formula, let's say
2 + a * 2 + b + c * d * 3
You'll have to write:
op mul _r1 a 2
op add _r2 2 _r1
op add _r3 _r2 b
op mul _r4 c d
op mul _r5 _r4 3
op add result _r3 _r5
It becomes boring, error-prone and quite hard to modify.
What if you could write the following instead:
time = cell1[0]
time += 1
if time >= 300:
time = 0
cell1[0] = time
unloader1.configure(M.at.lead if time > 200 else M.at.titanium)
result = 2 + a * 2 + b + c * d * 3
It's possible with minpiler!
- Write your code in subset of Python
- Compile it into Mindustry instructions
- Copy the result into clipboard
- Import code right into running game
So far so good, looks like you can just write Python code and then import it into the game. As a good Pythonista you're eager to use your knowledge of closures, classes and decorators to program Mindustry controllers, but unfortunately I have to stop you there. You won't be able to use most of the Python. All the restrictions arise from Mindustry processor architecture:
- There's no data structures, only scalar values are allowed (floats or opaque objects). The only exception are Memory cells that behave as fixed-size arrays of floats.
- You can't access variables indirectly, there're no pointers, no
getattr
. - Subsequence of former, it's impossible to implement dynamic memory/stack (at least without Memory cells). This makes lists, iterators, classes, closures and other things impossible.
- Set of builtins is very restricted, you can only call what you have available in game (M.print, M.draw.clear, etc)
Anyway, I hope Minpiler will be handy enough for you to have lots of fun playing Mindustry.
How to use
# Install packages
pip install minpiler pyperclip
# Compile your code into clipboard
python -m minpiler --clip yourfile.py
# now open processor building, press Edit button, Import from Clipboard
Pyperclip is optional, it's only used with --clip
option.
Refer to set of code samples in this repository.
Compatibility
Python >= 3.6 or compatible pypy3.
Mindustry: currently only 121 have been tested.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file minpiler-0.3.2.tar.gz
.
File metadata
- Download URL: minpiler-0.3.2.tar.gz
- Upload date:
- Size: 19.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.25.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23d5a2aafecb022395cee494d295ffddff44c56f326c105b60151fdaa296e7e1 |
|
MD5 | 6735d2133ea3a926bde3ece2620d05ca |
|
BLAKE2b-256 | f6f0e423ffa7da65278f1d1cc392783c91c522cd023fcea082aac5eafd328f63 |
File details
Details for the file minpiler-0.3.2-py3-none-any.whl
.
File metadata
- Download URL: minpiler-0.3.2-py3-none-any.whl
- Upload date:
- Size: 17.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.25.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e74ded87d6d7b115b900c33b665f40e9005855d24bd329c1a46ff7b14ed49a42 |
|
MD5 | edce506a06ad03f093ffae7e7bbdeae9 |
|
BLAKE2b-256 | 435ae5a18e8b00ee849658b7b6d7465efcc1183a01d04ead1f4f3c4a71fa871b |