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;
}
}