Functional world processors

This commit is contained in:
Anuken
2022-02-08 12:18:48 -05:00
parent e4dd7bf14b
commit 38c0284bbe
21 changed files with 363 additions and 60 deletions

View File

@@ -15,6 +15,8 @@ import mindustry.world.*;
import java.io.*;
import static mindustry.Vars.*;
/** Stores global constants for logic processors. */
public class GlobalConstants{
public static final int ctrlProcessor = 1, ctrlPlayer = 2, ctrlFormation = 3;
@@ -22,6 +24,9 @@ public class GlobalConstants{
/** Global random state. */
public static final Rand rand = new Rand();
//non-constants that depend on state
private static int varTime, varTick, varWave, varWaveTime;
private ObjectIntMap<String> namesToIds = new ObjectIntMap<>();
private Seq<Var> vars = new Seq<>(Var.class);
private UnlockableContent[][] logicIdToContent;
@@ -34,6 +39,12 @@ public class GlobalConstants{
put("true", 1);
put("null", null);
//time
varTime = put("@time", 0);
varTick = put("@tick", 0);
varWave = put("@waveNumber", 0);
varWaveTime = put("@waveTime", 0);
//special enums
put("@ctrlProcessor", ctrlProcessor);
@@ -51,9 +62,7 @@ public class GlobalConstants{
}
for(Block block : Vars.content.blocks()){
if(block.synthetic()){
put("@" + block.name, block);
}
put("@" + block.name, block);
}
//used as a special value for any environmental solid block
@@ -105,6 +114,18 @@ public class GlobalConstants{
}
}
/** Updates global time and other state variables. */
public void update(){
//set up time; note that @time is now only updated once every invocation and directly based off of @tick.
//having time be based off of user system time was a very bad idea.
vars.items[varTime].numval = state.tick / 60.0 * 1000.0;
vars.items[varTick].numval = state.tick;
//wave state
vars.items[varWave].numval = state.wave;
vars.items[varWaveTime].numval = state.wavetime / 60f;
}
/** @return a piece of content based on its logic ID. This is not equivalent to content ID. */
public @Nullable Content lookupContent(ContentType type, int id){
var arr = logicIdToContent[type.ordinal()];
@@ -127,8 +148,13 @@ public class GlobalConstants{
return vars.items[id];
}
/** Sets a global variable by an ID returned from put(). */
public void set(int id, double value){
get(id).numval = value;
}
/** Adds a constant value by name. */
public Var put(String name, Object value){
public int put(String name, Object value){
Var var = new Var(name);
var.constant = true;
if(value instanceof Number num){
@@ -141,6 +167,6 @@ public class GlobalConstants{
int index = vars.size;
namesToIds.put(name, index);
vars.add(var);
return var;
return index;
}
}

View File

@@ -22,20 +22,28 @@ public class LAssembler{
public LAssembler(){
//instruction counter
putVar("@counter").value = 0;
//timestamp
putConst("@time", 0);
//currently controlled unit
putConst("@unit", null);
//reference to self
putConst("@this", null);
//global tick
putConst("@tick", 0);
}
/** @deprecated use the one with the privileged parameter */
@Deprecated
public static LAssembler assemble(String data){
return assemble(data, false);
}
/** @deprecated use the one with the privileged parameter */
@Deprecated
public static Seq<LStatement> read(String text){
return read(text, false);
}
public static LAssembler assemble(String data, boolean privileged){
LAssembler asm = new LAssembler();
Seq<LStatement> st = read(data);
Seq<LStatement> st = read(data, privileged);
asm.instructions = st.map(l -> l.build(asm)).filter(l -> l != null).toArray(LInstruction.class);
return asm;
@@ -51,8 +59,11 @@ public class LAssembler{
return out.toString();
}
public static Seq<LStatement> read(String data){
return LParser.parse(data);
/** Parses a sequence of statements from a string. */
public static Seq<LStatement> read(String text, boolean privileged){
//don't waste time parsing null/empty text
if(text == null || text.isEmpty()) return new Seq<>();
return new LParser(text, privileged).parse();
}
/** @return a variable ID by name.

View File

@@ -27,10 +27,12 @@ public class LCanvas extends Table{
public DragLayout statements;
public ScrollPane pane;
public Group jumps;
StatementElem dragging;
StatementElem hovered;
float targetWidth;
int jumpCount = 0;
boolean privileged;
Seq<Tooltip> tooltips = new Seq<>();
public LCanvas(){
@@ -146,7 +148,7 @@ public class LCanvas extends Table{
public void load(String asm){
jumps.clear();
Seq<LStatement> statements = LAssembler.read(asm);
Seq<LStatement> statements = LAssembler.read(asm, privileged);
statements.truncate(LExecutor.maxInstructions);
this.statements.clearChildren();
for(LStatement st : statements){

View File

@@ -16,6 +16,7 @@ import mindustry.game.Teams.*;
import mindustry.gen.*;
import mindustry.type.*;
import mindustry.world.*;
import mindustry.world.blocks.environment.*;
import mindustry.world.blocks.logic.*;
import mindustry.world.blocks.logic.LogicDisplay.*;
import mindustry.world.blocks.logic.MemoryBlock.*;
@@ -31,10 +32,8 @@ public class LExecutor{
//special variables
public static final int
varCounter = 0,
varTime = 1,
varUnit = 2,
varThis = 3,
varTick = 4;
varUnit = 1,
varThis = 2;
public static final int
maxGraphicsBuffer = 256,
@@ -43,6 +42,7 @@ public class LExecutor{
public LInstruction[] instructions = {};
public Var[] vars = {};
public Var counter;
public int[] binds;
public LongSeq graphicsBuffer = new LongSeq();
@@ -50,32 +50,24 @@ public class LExecutor{
public Building[] links = {};
public IntSet linkIds = new IntSet();
public Team team = Team.derelict;
public boolean privileged = false;
public boolean initialized(){
return instructions != null && vars != null && instructions.length > 0;
return instructions.length > 0;
}
/** Runs a single instruction. */
public void runOnce(){
//set up time; note that @time is now only updated once every invocation and directly based off of @tick.
//having time be based off of user system time was a very bad idea.
vars[varTime].numval = state.tick / 60.0 * 1000.0;
vars[varTick].numval = state.tick;
//reset to start
if(vars[varCounter].numval >= instructions.length || vars[varCounter].numval < 0){
vars[varCounter].numval = 0;
if(counter.numval >= instructions.length || counter.numval < 0){
counter.numval = 0;
}
if(vars[varCounter].numval < instructions.length){
instructions[(int)(vars[varCounter].numval++)].run(this);
if(counter.numval < instructions.length){
instructions[(int)(counter.numval++)].run(this);
}
}
public void load(String data){
load(LAssembler.assemble(data));
}
/** Loads with a specified assembler. Resets all variables. */
public void load(LAssembler builder){
vars = new Var[builder.vars.size];
@@ -95,6 +87,8 @@ public class LExecutor{
dest.objval = var.value;
}
});
counter = vars[varCounter];
}
//region utility
@@ -560,7 +554,7 @@ public class LExecutor{
@Override
public void run(LExecutor exec){
Object obj = exec.obj(target);
if(obj instanceof Building b && b.team == exec.team && exec.linkIds.contains(b.id)){
if(obj instanceof Building b && (exec.privileged || (b.team == exec.team && exec.linkIds.contains(b.id)))){
if(type.isObj && exec.var(p1).isobj){
b.control(type, exec.obj(p1), exec.num(p2), exec.num(p3), exec.num(p4));
}else{
@@ -921,7 +915,7 @@ public class LExecutor{
//graphics on headless servers are useless.
if(Vars.headless) return;
if(exec.building(target) instanceof LogicDisplayBuild d && d.team == exec.team){
if(exec.building(target) instanceof LogicDisplayBuild d && (d.team == exec.team || exec.privileged)){
if(d.commands.size + exec.graphicsBuffer.size < maxDisplayBuffer){
for(int i = 0; i < exec.graphicsBuffer.size; i++){
d.commands.addLast(exec.graphicsBuffer.items[i]);
@@ -1067,6 +1061,7 @@ public class LExecutor{
}
}
//TODO lookup color instruction and inverse lookup
public static class LookupI implements LInstruction{
public int dest;
public int from;
@@ -1087,6 +1082,79 @@ public class LExecutor{
}
}
//endregion
//region privileged / world instructions
public static class GetBlockI implements LInstruction{
public int x, y;
public int dest;
public TileLayer layer = TileLayer.block;
public GetBlockI(int x, int y, int dest, TileLayer layer){
this.x = x;
this.y = y;
this.dest = dest;
this.layer = layer;
}
public GetBlockI(){
}
@Override
public void run(LExecutor exec){
Tile tile = world.tile(exec.numi(x), exec.numi(y));
if(tile == null){
exec.setobj(dest, null);
}else{
exec.setobj(dest, switch(layer){
case floor -> tile.floor();
case ore -> tile.overlay();
case block -> tile.block();
case building -> tile.build;
});
}
}
}
public static class SetBlockI implements LInstruction{
public int x, y;
public int block;
public int team, rotation;
public TileLayer layer = TileLayer.block;
public SetBlockI(int x, int y, int block, int team, int rotation, TileLayer layer){
this.x = x;
this.y = y;
this.block = block;
this.team = team;
this.rotation = rotation;
this.layer = layer;
}
public SetBlockI(){
}
@Override
public void run(LExecutor exec){
Tile tile = world.tile(exec.numi(x), exec.numi(y));
if(tile != null && exec.obj(block) instanceof Block b){
//TODO this can be quite laggy...
switch(layer){
case ore -> {
if(b instanceof OverlayFloor o) tile.setOverlay(o);
}
case floor -> {
if(b instanceof Floor f) tile.setFloor(f);
}
case block -> {
Team t = exec.obj(team) instanceof Team steam ? steam : Team.derelict;
tile.setBlock(b, t, Mathf.clamp(exec.numi(rotation), 0, 3));
}
//building case not allowed
}
}
}
}
//endregion
}

View File

@@ -19,18 +19,13 @@ public class LParser{
Seq<LStatement> statements = new Seq<>();
char[] chars;
int pos, line, tok;
boolean privileged;
LParser(String text){
LParser(String text, boolean privileged){
this.privileged = privileged;
this.chars = text.toCharArray();
}
/** Parses a sequence of statements from a string. */
public static Seq<LStatement> parse(String text){
//don't waste time parsing null/empty text
if(text == null || text.isEmpty()) return new Seq<>();
return new LParser(text).parse();
}
void comment(){
//read until \n or eof
while(pos < chars.length && chars[pos++] != '\n');
@@ -142,6 +137,11 @@ public class LParser{
st = new InvalidStatement();
}
//discard misplaced privileged instructions
if(!privileged && st != null && st.privileged()){
st = new InvalidStatement();
}
//store jumps that use labels
if(st instanceof JumpStatement jump && wasJump){
jumps.add(new JumpIndex(jump, jumpLoc));

View File

@@ -30,7 +30,8 @@ public abstract class LStatement{
public LStatement copy(){
StringBuilder build = new StringBuilder();
write(build);
Seq<LStatement> read = LAssembler.read(build.toString());
//assume privileged when copying, because there's no way privileged instructions can appear here anyway, and the instructions get validated on load anyway
Seq<LStatement> read = LAssembler.read(build.toString(), true);
return read.size == 0 ? null : read.first();
}
@@ -38,6 +39,16 @@ public abstract class LStatement{
return false;
}
/** Privileged instructions are only allowed in world processors. */
public boolean privileged(){
return false;
}
/** If true, this statement is considered useless with privileged processors and is not allowed in them. */
public boolean nonPrivileged(){
return false;
}
//protected methods are only for internal UI layout utilities
protected void param(Cell<Label> label){

View File

@@ -1050,4 +1050,108 @@ public class LStatements{
return new UnitLocateI(locate, flag, builder.var(enemy), builder.var(ore), builder.var(outX), builder.var(outY), builder.var(outFound), builder.var(outBuild));
}
}
@RegisterStatement("getblock")
public static class GetBlockStatement extends LStatement{
public TileLayer layer = TileLayer.block;
public String result = "result", x = "0", y = "0";
@Override
public void build(Table table){
fields(table, result, str -> result = str);
table.add(" = get ");
row(table);
table.button(b -> {
b.label(() -> layer.name());
b.clicked(() -> showSelect(b, TileLayer.all, layer, o -> layer = o));
}, Styles.logict, () -> {}).size(64f, 40f).pad(4f).color(table.color);
table.add(" at ");
fields(table, x, str -> x = str);
table.add(", ");
fields(table, y, str -> y = str);
}
@Override
public boolean privileged(){
return true;
}
@Override
public Color color(){
return Pal.logicWorld;
}
@Override
public LInstruction build(LAssembler builder){
return new GetBlockI(builder.var(x), builder.var(y), builder.var(result), layer);
}
}
@RegisterStatement("setblock")
public static class SetBlockStatement extends LStatement{
public TileLayer layer = TileLayer.block;
public String block = "@air", x = "0", y = "0", team = "@derelict", rotation = "0";
@Override
public void build(Table table){
rebuild(table);
}
void rebuild(Table table){
table.clearChildren();
table.add("set");
table.button(b -> {
b.label(() -> layer.name());
b.clicked(() -> showSelect(b, TileLayer.settable, layer, o -> {
layer = o;
rebuild(table);
}));
}, Styles.logict, () -> {}).size(64f, 40f).pad(4f).color(table.color);
row(table);
table.add(" at ");
fields(table, x, str -> x = str);
table.add(", ");
fields(table, y, str -> y = str);
row(table);
table.add(" to ");
fields(table, block, str -> block = str);
if(layer == TileLayer.block){
row(table);
table.add("team ");
fields(table, team, str -> team = str);
table.add(" rotation ");
fields(table, rotation, str -> rotation = str);
}
}
@Override
public boolean privileged(){
return true;
}
@Override
public Color color(){
return Pal.logicWorld;
}
@Override
public LInstruction build(LAssembler builder){
return new SetBlockI(builder.var(x), builder.var(y), builder.var(block), builder.var(team), builder.var(rotation), layer);
}
}
}

View File

@@ -22,6 +22,7 @@ import static mindustry.logic.LCanvas.*;
public class LogicDialog extends BaseDialog{
public LCanvas canvas;
Cons<String> consumer = s -> {};
boolean privileged;
@Nullable LExecutor executor;
public LogicDialog(){
@@ -156,7 +157,7 @@ public class LogicDialog extends BaseDialog{
int i = 0;
for(Prov<LStatement> prov : LogicIO.allStatements){
LStatement example = prov.get();
if(example instanceof InvalidStatement || example.hidden()) continue;
if(example instanceof InvalidStatement || example.hidden() || (example.privileged() && !privileged) || (example.nonPrivileged() && privileged)) continue;
TextButtonStyle style = new TextButtonStyle(Styles.cleart);
style.fontColor = example.color();
@@ -184,10 +185,12 @@ public class LogicDialog extends BaseDialog{
onResize(() -> canvas.rebuild());
}
public void show(String code, LExecutor executor, Cons<String> modified){
public void show(String code, LExecutor executor, boolean privileged, Cons<String> modified){
this.executor = executor;
this.privileged = privileged;
canvas.statements.clearChildren();
canvas.rebuild();
canvas.privileged = privileged;
try{
canvas.load(code);
}catch(Throwable t){

View File

@@ -0,0 +1,10 @@
package mindustry.logic;
public enum TileLayer{
floor,
ore,
block,
building;
public static final TileLayer[] all = values(), settable = {floor, ore, block};
}