• Hey! Register here to create your account, engage with the community, and talk about what is new!

Embyr - A text representation for DiamondFire code blocks, and a compiler for it.

TheZipCreator

New member
Joined
Jan 11, 2021
Messages
6
Reaction score
0
Hello! I've created Embyr, which is effectively a text representation of DiamondFire code blocks. I've also built a compiler for it, which is available here.
If you want to contribute, please tell me! The compiler is written in D, which is a rather uncommon language (unfortunately), but it should be relatively familiar if you know C, C++, Java, etc.
Below is the readme from Embyr's repository:

Embyr

Embyr is a text representation of DiamondFire code blocks. This repository is a compiler that converts Embyr into DiamondFire code templates, written in D.

Reasoning

The goal of Embyr is to be a sort-of portable DiamondFire assembly language. This is in order to make it easier to make other languages with DiamondFire as a target. For example, if you make a compiler called mylangc, and you wanted the output format to be DiamondFire code blocks, you could compile to an Embyr source code file, and then shell out to embyr, with something like embyr -o out.txt compiled.embyr.
The reason to do this instead of directly compiling to DiamondFire code blocks is so that you wouldn't have to mess around with the specific details of how DF encodes code blocks into templates. Instead, it is of more practical value to just consider which code blocks you want to compile code to, rather than worrying about how to convert those codeblocks into a format that DiamondFire knows.
It also provides a way to write DiamondFire code through exclusively text, if you prefer that medium over writing code directly on the server.

Examples
A simple Hello, World!

This sends the player a message when they join the plot.

[pre]
Code:
PLAYER_EVENT Join:
	PLAYER_ACTION SendMessage txt"Hello, World!";
[/pre]

There's a few things to note here. The first one is declarations. Declarations are the generic name that Embyr gives to things such as Player Events, Functions, or Processes. This file contains a single declaration (PLAYER_EVENT Join:) but a file may contain as many declarations as necessary. Each declaration is converted into 1 or more code templates.
Also note the txt prefix before "Hello, World!". This tells the compiler that this is text, and not another datatype. This is important, because in DiamondFire, things such as numbers may also contain arbitrary text (this is why you can type things like %math(2+3) into a number). As such, the datatype is almost always prefixed before the value.
Another thing to look at is the semicolon (;) after the player action. All codeblocks must end with a semicolon, or a brace ({, } for pistons, ${, }$ for sticky pistons). This is so you can wrap code blocks over multiple lines, which may be useful for setting up things like inventory menus.

Item Giver

This is a program that assumes the existence of signs at some given coordinates. It then will detect which sign the player clicked on, and give an item accordingly.

[pre]
Code:
PLAYER_EVENT RightClick:
	IF_PLAYER IsLookingAt loc[5 50 5] {
		PLAYER_ACTION GiveItems item{id:"minecraft:oak_log",Count:1b};
	}
	IF_PLAYER IsLookingAt loc[5 50 6] {
		PLAYER_ACTION GiveItems item{id:"minecraft:stone",Count:1b};
	}
	IF_PLAYER IsLookingAt loc[5 50 7] {
		PLAYER_ACTION GiveItems item{id"minecraft:diamond",Count:1b};
	}
[/pre]

This shows off the location syntax, and IF_PLAYER. Locations are of the form loc[x y z] or loc[x y z pitch yaw], and they're used here to give coordinates which the player is expected to click at.
Also notable is the item literal, which uses the SNBT (String Named Binary Table) format, which is something Minecraft natively supports (and DF uses to encode items).

A simple PVP game
[pre]
Code:
PLAYER_EVENT Join:
	PLAYER_ACTION SetAllowPVP tag["PVP" "Enable"];
	PLAYER_ACTION AllPlayers SendMessage txt"\&a+ \&e%default has joined.";
	PLAYER_ACTION GiveItems item{id:"minecraft:iron_sword",Count:1b};

PLAYER_EVENT Leave:
	PLAYER_ACTION AllPlayers SendMessage txt"\&c- \&e%default has left.";

PLAYER_EVENT Respawn:
	PLAYER_ACTION GiveItems item{id:"minecraft:iron_sword",Count:1b};

PLAYER_EVENT KillPlayer:
	SET_VAR += var"%default kills"s num"1";
	PLAYER_ACTION AllPlayers SendMessage txt"\&4%default \&chas killed \&4%victim\&c. They now have %var(%default kills) kills.";
[/pre]

This shows many new things. The first thing to look at is tags. In the previous examples, the tags for each player action were generated by the compiler, via the --addtags switch. Here it's the same, but we also add a tag of our own, in SetAllowPVP. Tags consist of the tag name, followed by the value. Creating an invalid tag will cause a compiler error.
Another new thing in this example is targets. Targets are specified as identifiers and placed before the action. Targets are only valid in some contexts. For example, a target is invalid in SET_VAR.
Another thing of note is the variable literals, notice the s after it? That s is a flag that makes it a saved variable. There a 3 variable flags: g for global, s for saved, and l for local. They must immediately succeed a variable string.
Finally, you might notice the \& in variables. This is another way of typing the section symbol, which MC uses for colors. It is equivalent to just typing & on the DF server.

Definitions

A definition is effectively a shorthand for a value constant. See below:

[pre]
Code:
stick = item{id:"minecraft:stick",Count:1b};

PLAYER_EVENT Join:
	PLAYER_ACTION GiveItems stick;

PLAYER_EVENT RightClick:
	IF_PLAYER IsHolding stick {
		PLAYER_ACTION SendMessage txt"You clicked the stick!";
	}
[/pre]

This creates a definition for stick, and sets it to the item item{id:"minecraft:stick",Count:1b}. Now, any time after this definition, the identifier stick will be replaced by item{id:"minecraft:stick",Count:1b}. This is useful especially for commonly used items, which are rather verbose in NBT syntax.

Things that still need to be implemented
Code Blocks
  • Entity Event
  • Entity Action
  • If Entity
  • Select Object
  • NOT Arrow
Values
  • Particles (and also all related actions)
Syntax Highlighting

Vim - A syntax highlight file for Vim is available here
 

pixlii

Forum adept
Overlord
Joined
May 20, 2021
Messages
215
Reaction score
35
Is there any way to convert Embyr code into DF code templates as of now? Would be actually really useful for coding to be able to script DF (plus comments!)
 

TheZipCreator

New member
Joined
Jan 11, 2021
Messages
6
Reaction score
0
Is there any way to convert Embyr code into DF code templates as of now? Would be actually really useful for coding to be able to script DF (plus comments!)
yes, here's the link to the github repository. It has a release for windows & linux that should work. Currently it doesn't have a GUI, but if you know your way around a terminal it shouldn't be that hard to use. (when you download the compiler, you can run embyr --help for more info)

Also, the # character is currently used for comments, e.g.:
Code:
# Takes two 2d vectors (formatted as lists) as arguments (set to the local vars a and b),
# calculates the manhattan distance between them, and returns the result in the local variable ret.
FUNCTION manhattanDist item{Count:1b,id:"minecraft:stone_bricks"}:
    # formula is |x1 - x2| + |y1 - y2|
    SET_VAR AbsoluteValue var"_tmp0"l num"%math(%index(a,1)-%index(b,1)";
    SET_VAR AbsoluteValue var"_tmp1"l num"%math(%index(a,2)-%index(b,2)";
    SET_VAR = var"ret"l var"%math(%var(_tmp0)-%var(_tmp1)";
 
Last edited:

TheZipCreator

New member
Joined
Jan 11, 2021
Messages
6
Reaction score
0
rip mac users (me)
try running the windows release via WINE. I can't really support mac because I don't really have a way to test it; the only real way to legally use macOS is to own a mac, and I don't. I also don't really feel like pirating it.

Alternatively, you could compile it yourself. Just download one of the compilers for D, clone the repo, and run dub build. The generated executable should just work.
 
Top Bottom