Logic wait & content look up instructions

This commit is contained in:
Anuken
2021-06-24 15:38:20 -04:00
parent 38a9ad9ec1
commit 0949852758
9 changed files with 192 additions and 4 deletions

View File

@@ -126,6 +126,10 @@ public abstract class UnlockableContent extends MappableContent{
return true;
}
public boolean logicVisible(){
return !isHidden();
}
/** Makes this piece of content unlocked; if it already unlocked, nothing happens. */
public void unlock(){
if(!unlocked && !alwaysUnlocked){

View File

@@ -1,20 +1,28 @@
package mindustry.logic;
import arc.*;
import arc.files.*;
import arc.struct.*;
import arc.util.*;
import mindustry.*;
import mindustry.content.*;
import mindustry.ctype.*;
import mindustry.entities.units.*;
import mindustry.logic.LExecutor.*;
import mindustry.type.*;
import mindustry.world.*;
import java.io.*;
/** Stores global constants for logic processors. */
public class GlobalConstants{
public static final int ctrlProcessor = 1, ctrlPlayer = 2, ctrlFormation = 3;
public static final ContentType[] lookableContent = {ContentType.block, ContentType.unit, ContentType.item, ContentType.liquid};
private ObjectIntMap<String> namesToIds = new ObjectIntMap<>();
private Seq<Var> vars = new Seq<>(Var.class);
private UnlockableContent[][] logicIdToContent;
private int[][] contentIdToLogicId;
public void init(){
put("the end", null);
@@ -61,6 +69,49 @@ public class GlobalConstants{
for(UnitCommand cmd : UnitCommand.all){
put("@command" + Strings.capitalize(cmd.name()), cmd);
}
logicIdToContent = new UnlockableContent[ContentType.all.length][];
contentIdToLogicId = new int[ContentType.all.length][];
Fi ids = Core.files.internal("logicids.dat");
if(ids.exists()){
//read logic ID mapping data (generated in ImagePacker)
try(DataInputStream in = new DataInputStream(ids.readByteStream())){
for(ContentType ctype : lookableContent){
short amount = in.readShort();
logicIdToContent[ctype.ordinal()] = new UnlockableContent[amount];
contentIdToLogicId[ctype.ordinal()] = new int[Vars.content.getBy(ctype).size];
//store count constants
put("@" + ctype.name() + "Count", amount);
for(int i = 0; i < amount; i++){
String name = in.readUTF();
UnlockableContent fetched = Vars.content.getByName(ctype, name);
if(fetched != null){
logicIdToContent[ctype.ordinal()][i] = fetched;
contentIdToLogicId[ctype.ordinal()][fetched.id] = i;
}
}
}
}catch(IOException e){
//don't crash?
Log.err("Error reading logic ID mapping", e);
}
}
}
/** @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()];
return arr != null && id >= 0 && id < arr.length ? arr[id] : null;
}
/** @return the integer logic ID of content, or -1 if invalid. */
public int lookupLogicId(UnlockableContent content){
var arr = contentIdToLogicId[content.getContentType().ordinal()];
return arr != null && content.id >= 0 && content.id < arr.length ? arr[content.id] : -1;
}
/** @return a constant ID > 0 if there is a constant with this name, otherwise -1. */

View File

@@ -1023,6 +1023,26 @@ public class LExecutor{
}
}
public static class LookupI implements LInstruction{
public int dest;
public int from;
public ContentType type;
public LookupI(int dest, int from, ContentType type){
this.dest = dest;
this.from = from;
this.type = type;
}
public LookupI(){
}
@Override
public void run(LExecutor exec){
exec.setobj(dest, constants.lookupContent(type, exec.numi(from)));
}
}
//endregion
}

View File

@@ -7,6 +7,7 @@ import arc.scene.ui.*;
import arc.scene.ui.layout.*;
import mindustry.*;
import mindustry.annotations.Annotations.*;
import mindustry.ctype.*;
import mindustry.gen.*;
import mindustry.graphics.*;
import mindustry.logic.LCanvas.*;
@@ -662,8 +663,7 @@ public class LStatements{
}
}
//TODO untested
//@RegisterStatement("wait")
@RegisterStatement("wait")
public static class WaitStatement extends LStatement{
public String value = "0.5";
@@ -684,6 +684,42 @@ public class LStatements{
}
}
@RegisterStatement("lookup")
public static class LookupStatement extends LStatement{
public ContentType type = ContentType.item;
public String result = "result", id = "0";
@Override
public void build(Table table){
fields(table, result, str -> result = str);
table.add(" = lookup ");
row(table);
table.button(b -> {
b.label(() -> type.name());
b.clicked(() -> showSelect(b, GlobalConstants.lookableContent, type, o -> {
type = o;
}));
}, Styles.logict, () -> {}).size(64f, 40f).pad(4f).color(table.color);
table.add(" # ");
fields(table, id, str -> id = str);
}
@Override
public Color color(){
return Pal.logicOperations;
}
@Override
public LInstruction build(LAssembler builder){
return new LookupI(builder.var(result), builder.var(id), type);
}
}
@RegisterStatement("end")
public static class EndStatement extends LStatement{
@Override

View File

@@ -774,6 +774,11 @@ public class Block extends UnlockableContent{
return ContentType.block;
}
@Override
public boolean logicVisible(){
return buildVisibility != BuildVisibility.hidden;
}
/** Called after all blocks are created. */
@Override
@CallSuper