Processor global variable dialog

This commit is contained in:
Anuken
2024-02-09 18:09:41 -05:00
parent 3d1067aac0
commit 2fd92b8c80
6 changed files with 201 additions and 29 deletions

View File

@@ -27,45 +27,63 @@ public class GlobalVars{
public static final Rand rand = new Rand();
//non-constants that depend on state
private static int varTime, varTick, varSecond, varMinute, varWave, varWaveTime, varServer, varClient, varClientLocale, varClientUnit, varClientName, varClientTeam, varClientMobile;
private static int varTime, varTick, varSecond, varMinute, varWave, varWaveTime, varMapW, varMapH, varServer, varClient, varClientLocale, varClientUnit, varClientName, varClientTeam, varClientMobile;
private ObjectIntMap<String> namesToIds = new ObjectIntMap<>();
private Seq<Var> vars = new Seq<>(Var.class);
private Seq<VarEntry> varEntries = new Seq<>();
private IntSet privilegedIds = new IntSet();
private UnlockableContent[][] logicIdToContent;
private int[][] contentIdToLogicId;
public void init(){
put("the end", null);
putEntryOnly("sectionProcessor");
putEntryOnly("@this");
putEntryOnly("@thisx");
putEntryOnly("@thisy");
putEntryOnly("@links");
putEntryOnly("@ipt");
putEntryOnly("sectionGeneral");
put("the end", null, false, true);
//add default constants
put("false", 0);
put("true", 1);
put("null", null);
putEntry("false", 0);
putEntry("true", 1);
put("null", null, false, true);
//math
put("@pi", Mathf.PI);
put("π", Mathf.PI); //for the "cool" kids
put("@e", Mathf.E);
put("@degToRad", Mathf.degRad);
put("@radToDeg", Mathf.radDeg);
putEntry("@pi", Mathf.PI);
put("π", Mathf.PI, false, true); //for the "cool" kids
putEntry("@e", Mathf.E);
putEntry("@degToRad", Mathf.degRad);
putEntry("@radToDeg", Mathf.radDeg);
putEntryOnly("sectionMap");
//time
varTime = put("@time", 0);
varTick = put("@tick", 0);
varSecond = put("@second", 0);
varMinute = put("@minute", 0);
varWave = put("@waveNumber", 0);
varWaveTime = put("@waveTime", 0);
varTime = putEntry("@time", 0);
varTick = putEntry("@tick", 0);
varSecond = putEntry("@second", 0);
varMinute = putEntry("@minute", 0);
varWave = putEntry("@waveNumber", 0);
varWaveTime = putEntry("@waveTime", 0);
varServer = put("@server", 0, true);
varClient = put("@client", 0, true);
varMapW = putEntry("@mapw", 0);
varMapH = putEntry("@maph", 0);
putEntryOnly("sectionNetwork");
varServer = putEntry("@server", 0, true);
varClient = putEntry("@client", 0, true);
//privileged desynced client variables
varClientLocale = put("@clientLocale", null, true);
varClientUnit = put("@clientUnit", null, true);
varClientName = put("@clientName", null, true);
varClientTeam = put("@clientTeam", 0, true);
varClientMobile = put("@clientMobile", 0, true);
varClientLocale = putEntry("@clientLocale", null, true);
varClientUnit = putEntry("@clientUnit", null, true);
varClientName = putEntry("@clientName", null, true);
varClientTeam = putEntry("@clientTeam", 0, true);
varClientMobile = putEntry("@clientMobile", 0, true);
//special enums
put("@ctrlProcessor", ctrlProcessor);
@@ -115,6 +133,8 @@ public class GlobalVars{
logicIdToContent = new UnlockableContent[ContentType.all.length][];
contentIdToLogicId = new int[ContentType.all.length][];
putEntryOnly("sectionLookup");
Fi ids = Core.files.internal("logicids.dat");
if(ids.exists()){
//read logic ID mapping data (generated in ImagePacker)
@@ -125,7 +145,7 @@ public class GlobalVars{
contentIdToLogicId[ctype.ordinal()] = new int[Vars.content.getBy(ctype).size];
//store count constants
put("@" + ctype.name() + "Count", amount);
putEntry("@" + ctype.name() + "Count", amount);
for(int i = 0; i < amount; i++){
String name = in.readUTF();
@@ -159,6 +179,9 @@ public class GlobalVars{
vars.items[varWave].numval = state.wave;
vars.items[varWaveTime].numval = state.wavetime / 60f;
vars.items[varMapW].numval = world.width();
vars.items[varMapH].numval = world.height();
//network
vars.items[varServer].numval = (net.server() || !net.active()) ? 1 : 0;
vars.items[varClient].numval = net.client() ? 1 : 0;
@@ -173,6 +196,10 @@ public class GlobalVars{
}
}
public Seq<VarEntry> getEntries(){
return varEntries;
}
/** @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()];
@@ -209,6 +236,11 @@ public class GlobalVars{
/** Adds a constant value by name. */
public int put(String name, Object value, boolean privileged){
return put(name, value, privileged, true);
}
/** Adds a constant value by name. */
public int put(String name, Object value, boolean privileged, boolean hidden){
int existingIdx = namesToIds.get(name, -1);
if(existingIdx != -1){ //don't overwrite existing vars (see #6910)
Log.debug("Failed to add global logic variable '@', as it already exists.", name);
@@ -228,10 +260,44 @@ public class GlobalVars{
namesToIds.put(name, index);
if(privileged) privilegedIds.add(index);
vars.add(var);
if(!hidden){
varEntries.add(new VarEntry(index, name, "", "", privileged));
}
return index;
}
public int put(String name, Object value){
return put(name, value, false);
}
public int putEntry(String name, Object value){
return put(name, value, false, false);
}
public int putEntry(String name, Object value, boolean privileged){
return put(name, value, privileged, false);
}
public void putEntryOnly(String name){
varEntries.add(new VarEntry(0, name, "", "", false));
}
/** An entry that describes a variable for documentation purposes. This is *only* used inside UI for global variables. */
public static class VarEntry{
public int id;
public String name, description, icon;
public boolean privileged;
public VarEntry(int id, String name, String description, String icon, boolean privileged){
this.id = id;
this.name = name;
this.description = description;
this.icon = icon;
this.privileged = privileged;
}
public VarEntry(){
}
}
}