使用自定义编程语言 "Metal" 实现 Create 自动化。
放置一台计算机,右键点击,输入代码,按保存,Shift+右键点击以切换运行状态。
编码:
声明两个系统(System 类似于函数),名为 "INIT" 和 "TICK",如下所示:
SYSTEM(INIT): START;
在此处编写 init 代码
END_SYSTEM;
SYSTEM(TICK): START;
在此处编写 tick(循环)代码
END_SYSTEM;
API:
有一些用于操作 Metal 的函数。
内置函数:
MAKE_VAR(VAR_TYPE,$name,start_value); - 创建全局变量(全局变量必须以 '$' 开头,仅在 INIT 中有效)
MAKE_ITEM_VAR($name,item_id); - 通过 Minecraft 物品 ID 创建 INT 类型变量(仅在 INIT 中有效)
SET($name,value); - 设置变量的新值
ADD/SUB/MUL/DIV($name,value); - 对变量执行运算
WAIT(ms); - 延迟脚本(仅在 TICK 中有效)
CONTINUE(); - 返回到当前 System 的开始位置(仅在 TICK 中有效)
RERUN(); - 返回到当前脚本的主 System(仅在 TICK 中有效),自 0.0.7 起已移除
EXIT_STACK(); - 退出整个 System 调用栈并返回到栈起点。自 0.0.7 起已移除
RETURN(); - 退出 System 调用栈中的一层 System,自 0.0.7 起实际上可以向 SYSTEM 调用返回值
ABORT(); - 强制终止当前脚本(不会关闭计算机)
CALL_SYSTEM(name, args...); - 调用 System(最大栈深度为 16),无法调用 Systems-EntryPoints(INIT, TICK),参数仅在 0.0.3+ 模组版本中可用,自 0.0.7 起可返回值
IF(condition): START; - 基础 IF 运算符(结束标记为 "END_IF;")
ELSE(): START; - 基础 ELSE 运算符(位于 IF 和 END_IF 之间),自 0.0.7 起可省略 "()" 直接调用
BIND_ARGS_TO_SYSTEM(name, args...); 仅在 INIT 中有效,参数格式必须为 "#var:INT",用于设置系统调用 (CALL_SYSTEM) 的参数,仅在 0.0.3+ 模组版本中有效
MAKE_LOCAL_VAR(VAR_TYPE,#name,start_value); - 创建局部变量(局部变量必须以 '#' 开头)仅在 0.0.3+ 模组版本中有效
ACCEPT_INVOKES(); - 允许集成调用非保留系统,仅在 INIT 中有效,适用于 0.0.4+ 模组版本
PRINT(args...); - INFO 输出(白色),仅在 0.0.6+ 模组版本中有效
WARN(args...); - WARN 输出(黄色),仅在 0.0.6+ 模组版本中有效
ERROR(args...); - ERROR 输出(红色),仅在 0.0.6+ 模组版本中有效
EVALUATE(str); - 计算表达式,自 0.0.7 起添加
EVAL(code); - 像新的 SYSTEM 一样执行代码,自 0.0.7 起添加
FUNC((args),{ body }); - 创建 MetalFunction,可通过 .invoke(args) 调用,自 0.0.7 起添加
CAUSE_HARD_ERROR(message); - 停止脚本,将堆栈和信息记录到控制台。自 0.0.7 起添加
WHILE(condition, { body }); 当条件为真时运行,自 0.0.7 起添加
FOR(#i,start,end, { body });
从 #i = start 运行到 end - 1,自 0.0.7 起添加
LIST(TYPE); - 创建 MetalList,元素限制为 1024,自 0.0.7 起添加
SET_CATCH_MODE(); 仅在 INIT 中有效,现在某些错误不会停止脚本,只会记录 "Error catched!",自 0.0.7 起添加。
SENDER($name,"item1", "item2"); - 仅在 INIT 中有效,创建具有发送模式的 RedstoneLinker (MetalObject),比旧的功能方法 (CONNECT_LINK_SENDER) 更好,自 0.0.7 起添加。
RECEIVER($name,"item1", "item2"); - 仅在 INIT 中有效,创建具有接收模式的 RedstoneLinker (MetalObject),比旧的功能方法 (CONNECT_LINK_RECEIVER) 更好,自 0.0.7 起添加。
MAKE_PROCESS(SYSTEM_NAME); - 创建第二个 ticking 入口点,只能调用一次。仅在 INIT 中有效。自 0.0.7 起添加。
内置函数结束
Java 函数(在 Metal 外部注册):
CONNECT_LINK_SENDER(id,item1,item2); - 创建发射器连接(最后两个参数建议使用 MAKE_ITEM_VAR,id 可以是任意值,在使用前未定义),仅在 INIT 中有效。注意:自 0.0.7 起请使用 SENDER($sender,item1name,item2name) 代替。
CONNECT_LINK_RECEIVER(id,item1,item2); - 创建接收器连接(最后两个参数使用 MAKE_ITEM_VAR 变量)仅在 INIT 中有效。注意:自 0.0.7 起请使用 RECEIVER($receiver,item1name,item2name) 代替。
SEND_LINK(id,power); - 向 Create Redstone Link 发送电力(配合 id 使用,该 id 在 CONNECT_LINK_SENDER 中使用)。注意:自 0.0.7 起请使用 $sender.send(power) 代替。
RECEIVE_LINK(id); - 从 Create Redstone Link 接收电力(配合 id 使用,该 id 在 CONNECT_LINK_RECEIVER 中使用)。注意:自 0.0.7 起请使用 $receiver.receive() 代替。
RANDOM(min,max); - 返回 min 和 max 之间的随机 INT
RAND(); - 返回 0.0 - 1.0 之间的随机 DOUBLE
SIN(value); - 返回值的 sin 值(使用弧度)
COS(value); - 返回值的 cos 值(使用弧度)
TAN(value); - 返回值的 tan 值(使用弧度)
SQRT(value); - 返回值的平方根
DTR(value); - 角度转弧度
RTD(value); - 弧度转角度的值
GET_PI(); - 返回 PI
MIN(args...); - 返回所有参数的最小值
MAX(args...); - 返回所有参数的最大值
CLAMP(value,min,max); - 返回范围内的受限值
ABS(value); - 返回值的绝对值
POW(value,pow); - 返回值的幂次方
GET_OPS(); - 返回此脚本已完成的操作数 - 0.0.5+
Java 函数结束;
提示:
ADD, SUB 等仅用于修改变量值
例如:
ADD($x,1); = x += 1;
括号内:
IF($x + 1 > $y): START;
代码
END_IF;
此外,": START" 不是必须的,但建议保留,你也可以使用
SYSTEM(INIT);
...
IF(condition);
...
等等。
当前变量类型:INT, DOUBLE, POWER, BOOL。自 0.0.7 起支持 STRING, OBJECT。
TICK 入口点(System)在其他线程中以每秒 1000 次的频率运行。
集成:
自 0.0.4 起,你可以从 Computer Craft 模组调用非保留系统,其用法如下:
Metal:
SYSTEM(INIT): START;
ACCEPT_INVOKES();
BIND_ARGS_TO_SYSTEM(SUM,#first:DOUBLE,#second:DOUBLE);
END_SYSTEM;
SYSTEM(SUM): START;
RETURN(#first + #second);
END_SYSTEM;
SYSTEM(TICK): START;
END_SYSTEM;
CC Lua:
local metal = peripheral.find("metal");
if metal then
print(metal.callSystem("SUM",5,10)); -- 输出: 15
else
print("Cannot find Metal Computer");
end
自 0.0.5 起,你可以使用
SYSTEM(NAME,args...): START; 来替代 BIND_ARGS_TO_SYSTEM
示例:
SYSTEM(SET_POWER,#f:POWER,#s:POWER): START;
SEND_LINK(1,#f);
SEND_LINK(2,#s);
END_SYSTEM;
用 "**" 替代 "POW(val,pow);"
"%" 运算符
自 0.0.6 版本起,在输出函数中你可以直接使用字面量,例如:
PRINT(~Hello World);
Hello World
或者配合变量使用:
MAKE_VAR(INT,$test,150);
PRINT(~counter: ,$test);
counter: 150
对象:
MetalList:
$list.add(value);
$list.get(index);
$list.getFirst();
$list.getLast();
$list.remove(index);
$list.removeFirst();
$list.removeLast();
$list.clear();
$list.size();
$list.maxSize();
$list.hasIndex(index);
$list.forEach(MetalFunction);
MetalFunction:
$func.invoke(args);
$func.getTypes();
RedstoneLinker:
发送端(SENDER):
$sender.send(power);
$sender.toggle(active);
接收端(RECEIVER):
$receiver.receive();
$receiver.isActive();
自 0.0.8 版本起,变量可以这样赋值:
TYPE $global_var = ...;
TYPE #local_var = ...;
修改变量值:
var++; (递增)
var--; (递减)
var+=...; (加)
var-=...; (减)
var%=...; (取模)
var*=...; (乘)
var/=...; (除)
var**=...; (幂运算)
var=...; (赋值)
A create automation with using own programming language, naming "Metal".
Place a computer, right click, enter code, press save, shift+right click for toggle run.
Coding:
Declare 2 Systems (System is just like function), "INIT" and "TICK" like that:
SYSTEM(INIT): START;
init code here
END_SYSTEM;
SYSTEM(TICK): START;
tick (loop) code here
END_SYSTEM;
API:
There is some functions to work with Metal.
built-in functions:
MAKE_VAR(VAR_TYPE,$name,start_value); - making a global variable (global variable MUST start with '$', ONLY in INIT)
MAKE_ITEM_VAR($name,item_id); - making a INT variable by minecraft item number id (ONLY in INIT)
SET($name,value); - set new value for variable
ADD/SUB/MUL/DIV($name,value); - operations with variables
WAIT(ms); - delaying script (ONLY in TICK)
CONTINUE(); - return to the current System begin (ONLY in TICK)
RERUN(); - return to the main System of current script (ONLY in TICK), REMOVED since 0.0.7
EXIT_STACK(); - exit entire stack of System calls and returns to the point of stack start. REMOVED since 0.0.7
RETURN(); - exit 1 System of stack of System calls, since 0.0.7 can actually return values to SYSTEM call
ABORT(); - forcibly terminate current script (Doesn't turn off computer)
CALL_SYSTEM(name, args...); - call System (max stack size is 16), cannot call Systems-EntryPoints (INIT, TICK), args is ONLY in in 0.0.3+ mod version, since 0.0.7, can return values
IF(condition): START; - Base IF operator (closing - "END_IF;")
ELSE(): START; - Base ELSE operator (between IF and END_IF), since 0.0.7 can be called without "()"
BIND_ARGS_TO_SYSTEM(name, args...); ONLY in INIT, args must be like "#var:INT", setting arguments to system call (CALL_SYSTEM), ONLY in 0.0.3+ mod version
MAKE_LOCAL_VAR(VAR_TYPE,#name,start_value); - making a local variable (local variable MUST start with '#') ONLY in 0.0.3+ mod version
ACCEPT_INVOKES(); - allow invoke non-reserved systems by integrations ONLY in INIT, for 0.0.4+ mod version
PRINT(args...); - INFO Output (white) ONLY in 0.0.6+ mod version
WARN(args...); - WARN Output (yellow) ONLY in 0.0.6+ mod version
ERROR(args...); - ERROR Output (red) ONLY in 0.0.6+ mod version
EVALUATE(str); - calculates the expression, added since 0.0.7
EVAL(code); - executes the code like a new SYSTEM, added since 0.0.7
FUNC((args),{ body }); - Creates a MetalFunction, can be called with .invoke(args), added since 0.0.7
CAUSE_HARD_ERROR(message); - Stops the script, log stack and info to console. added since 0.0.7
WHILE(condition, { body }); Runs while condition is true, added since 0.0.7
FOR(#i,start,end, { body });
Runs from #i = start to end - 1, added since 0.0.7
LIST(TYPE); - Creates a MetalList, elements limit is 1024, added since 0.0.7
SET_CATCH_MODE(); ONLY in INIT, now, some of errors won't stop the script, just log with "Error catched!", added since 0.0.7.
SENDER($name,"item1", "item2"); - ONLY in INIT, makes RedstoneLinker (MetalObject) with sending mode, better than old functional method (CONNECT_LINK_SENDER) added since 0.0.7.
RECEIVER($name,"item1", "item2"); - ONLY in INIT, makes RedstoneLinker (MetalObject) with receiving mode, better than old functional method (CONNECT_LINK_RECEIVER) added since 0.0.7.
MAKE_PROCESS(SYSTEM_NAME); - makes second ticking entrypoint, can be called once. ONLY in INIT. added since 0.0.7.
end of built-in functions
Java Functions (registered outside of a Metal):
CONNECT_LINK_SENDER(id,item1,item2); - making a transmitter connection (for two last arguments recommends use MAKE_ITEM_VAR, id is any value, doesn't used before), ONLY in INIT. NOTE: since 0.0.7 use SENDER($sender,item1name,item2name) instead.
CONNECT_LINK_RECEIVER(id,item1,item2); - making a receiver connection (use MAKE_ITEM_VAR variables for two last arguments) ONLY in INIT. NOTE: since 0.0.7 use RECEIVER($receiver,item1name,item2name) instead.
SEND_LINK(id,power); - send power to Create Redstone Link (use with id, used in CONNECT_LINK_SENDER). NOTE: since 0.0.7 use $sender.send(power) instead.
RECEIVE_LINK(id); - receive power from Create Redstone Link (use with id, used in CONNECT_LINK_RECEIVER). NOTE: since 0.0.7 use $receiver.receive() instead.
RANDOM(min,max); - returns random INT between min and max
RAND(); - returns random DOUBLE between 0.0 - 1.0
SIN(value); - returns sin of the value (use radians)
COS(value); - returns cos of the value (use radians)
TAN(value); - returns tan of the value (use radians)
SQRT(value); - returns sqrt of the value
DTR(value); - degrees to radians
RTD(value); - radians to degrees
GET_PI(); - returns PI
MIN(args...); - returns MIN of all arguments
MAX(args...); - returns MAX of all arguments
CLAMP(value,min,max); - returns limited value in range
ABS(value); - returns module of value
POW(value,pow); - returns exponentiation of the value
GET_OPS(); - returns completed operations of this script - 0.0.5+
end of Java Functions;
Tips:
ADD,SUB, etc using ONLY in modification of value
for example:
ADD($x,1); = x += 1;
In brackets:
IF($x + 1 > $y): START;
code
END_IF;
And, ": START" is not necessarily, but recommended, you also can use
SYSTEM(INIT);
...
IF(condition);
...
etc.
Current variable types: INT, DOUBLE, POWER, BOOL. Since 0.0.7 - STRING, OBJECT.
TICK EntryPoint (System) running in the other thread 1000 times per second.
INTEGRATION:
Since 0.0.4 you can call non-reserved systems from Computer Craft mod, its looks like:
Metal:
SYSTEM(INIT): START;
ACCEPT_INVOKES();
BIND_ARGS_TO_SYSTEM(SUM,#first:DOUBLE,#second:DOUBLE);
END_SYSTEM;
SYSTEM(SUM): START;
RETURN(#first + #second);
END_SYSTEM;
SYSTEM(TICK): START;
END_SYSTEM;
CC Lua:
local metal = peripheral.find("metal");
if metal then
print(metal.callSystem("SUM",5,10)); -- output: 15
else
print("Cannot find Metal Computer");
end
Since 0.0.5 you can use
SYSTEM(NAME,args...): START; instead of BIND_ARGS_TO_SYSTEM
example:
SYSTEM(SET_POWER,#f:POWER,#s:POWER): START;
SEND_LINK(1,#f);
SEND_LINK(2,#s);
END_SYSTEM;
"**" instead of "POW(val,pow);"
"%" operator
Since 0.0.6, in output functions you can use literals, just like that:
PRINT(~Hello World);
Hello World
or, with variable:
MAKE_VAR(INT,$test,150);
PRINT(~counter: ,$test);
counter: 150
OBJECTS:
MetalList:
$list.add(value);
$list.get(index);
$list.getFirst();
$list.getLast();
$list.remove(index);
$list.removeFirst();
$list.removeLast();
$list.clear();
$list.size();
$list.maxSize();
$list.hasIndex(index);
$list.forEach(MetalFunction);
MetalFunction:
$func.invoke(args);
$func.getTypes();
RedstoneLinker:
SENDER:
$sender.send(power);
$sender.toggle(active);
RECEIVER:
$receiver.receive();
$receiver.isActive();
since 0.0.8 variables can be assigned like that:
TYPE $global_var = ...;
TYPE #local_var = ...;
changing:
var++; (increment)
var--; (decrement)
var+=...; (add)
var-=...; (sub)
var%=...; (mod)
var*=...; (mul)
var/=...; (div)
var**=...; (pow)
var=...; (set)
评论 0
登录后参与评分
还没有评论,来抢沙发吧