improve calculating dynamic token skip, add dummu execution context, improve getting string vars if string is 1 token starting and ending with " or ', if context is dummy don't get variables
This commit is contained in:
parent
ffbbb2430f
commit
3a9074b80e
4 changed files with 82 additions and 59 deletions
|
@ -1,5 +1,6 @@
|
|||
package net.pietru.cookie_utils.scripting;
|
||||
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import net.pietru.cookie_utils.scripting.tokens.*;
|
||||
|
||||
import java.io.File;
|
||||
|
@ -10,6 +11,8 @@ import java.util.Scanner;
|
|||
|
||||
public class BaseScript {
|
||||
public static HashMap<String, BaseToken> tokens = new HashMap<>();
|
||||
public static HashMap<String, Color> colors = new HashMap<>();
|
||||
public static final ExecutionContext dummy = new ExecutionContext(true);
|
||||
|
||||
|
||||
|
||||
|
@ -23,6 +26,40 @@ public class BaseScript {
|
|||
tokens.put("player", new TokenPlayer());
|
||||
for (String key : TokenMath.valid_tokens)
|
||||
tokens.put(key, new TokenMath());
|
||||
|
||||
|
||||
colors.put("light_gray", Color.LIGHT_GRAY);
|
||||
colors.put("gray", Color.GRAY);
|
||||
colors.put("dark_gray", Color.DARK_GRAY);
|
||||
colors.put("black", Color.BLACK);
|
||||
colors.put("blue", Color.BLUE);
|
||||
colors.put("navy", Color.NAVY);
|
||||
colors.put("royal", Color.ROYAL);
|
||||
colors.put("slate", Color.SLATE);
|
||||
colors.put("sky", Color.SKY);
|
||||
colors.put("cyan", Color.CYAN);
|
||||
colors.put("teal", Color.TEAL);
|
||||
colors.put("green", Color.GREEN);
|
||||
colors.put("chartreuse", Color.CHARTREUSE);
|
||||
colors.put("lime", Color.LIME);
|
||||
colors.put("forest", Color.FOREST);
|
||||
colors.put("olive", Color.OLIVE);
|
||||
colors.put("yellow", Color.YELLOW);
|
||||
colors.put("gold", Color.GOLD);
|
||||
colors.put("goldenrod", Color.GOLDENROD);
|
||||
colors.put("orange", Color.ORANGE);
|
||||
colors.put("brown", Color.BROWN);
|
||||
colors.put("tan", Color.TAN);
|
||||
colors.put("firebrick", Color.FIREBRICK);
|
||||
colors.put("red", Color.RED);
|
||||
colors.put("scarlet", Color.SCARLET);
|
||||
colors.put("coral", Color.CORAL);
|
||||
colors.put("salmon", Color.SALMON);
|
||||
colors.put("pink", Color.PINK);
|
||||
colors.put("magenta", Color.MAGENTA);
|
||||
colors.put("purple", Color.PURPLE);
|
||||
colors.put("violet", Color.VIOLET);
|
||||
colors.put("maroon", Color.MAROON);
|
||||
}
|
||||
|
||||
|
||||
|
@ -127,8 +164,11 @@ public class BaseScript {
|
|||
public static ArrayList<String> get_string(String[] tokens, int step){
|
||||
ArrayList<String> text = new ArrayList<>();
|
||||
if (tokens[step].startsWith("\"")) {
|
||||
text.add(tokens[step]);
|
||||
for (int i = step + 1; i < tokens.length; i++) {
|
||||
if (tokens[step].endsWith("\""))
|
||||
text.add(tokens[step].substring(1,tokens[step].length()-1));
|
||||
else
|
||||
text.add(tokens[step].substring(1));
|
||||
for (int i = step+1; i < tokens.length; i++) {
|
||||
if (tokens[i].endsWith("\"")){
|
||||
text.add(tokens[i].substring(0,tokens[i].length()-1));
|
||||
break;
|
||||
|
@ -138,8 +178,11 @@ public class BaseScript {
|
|||
}
|
||||
}
|
||||
else if (tokens[step].startsWith("'")) {
|
||||
if (tokens[step].endsWith("'"))
|
||||
text.add(tokens[step].substring(1,tokens[step].length()-1));
|
||||
else
|
||||
text.add(tokens[step].substring(1));
|
||||
for (int i = step + 1; i < tokens.length; i++) {
|
||||
for (int i = step+1; i < tokens.length; i++) {
|
||||
if (tokens[i].endsWith("'")) {
|
||||
text.add(tokens[i].substring(0,tokens[i].length()-1));
|
||||
break;
|
||||
|
@ -154,20 +197,33 @@ public class BaseScript {
|
|||
}
|
||||
|
||||
public static String get_if_variable(String token,ExecutionContext context){
|
||||
if (token.startsWith("#"))
|
||||
if (token.startsWith("#") && !context.dummy)
|
||||
return String.valueOf(context.get_value(token.substring(1)));
|
||||
return token;
|
||||
}
|
||||
|
||||
public static HashMap<String,String> get_optional_args(String[] tokens, int step, List<String> valid_args){
|
||||
return get_optional_args(tokens, step, valid_args , dummy);
|
||||
}
|
||||
|
||||
public static HashMap<String,String> get_optional_args(String[] tokens, int step, List<String> valid_args, ExecutionContext context){
|
||||
HashMap<String,String> optional_args = new HashMap<>();
|
||||
|
||||
for (int i = step; i < tokens.length-1; i+=2) {
|
||||
for (int i = step; i < tokens.length-1;) {
|
||||
if (!valid_args.contains(tokens[i])){
|
||||
break;
|
||||
}
|
||||
optional_args.put(tokens[i],tokens[i+1]);
|
||||
ArrayList<String> val = get_string(tokens,i+1);
|
||||
optional_args.put(tokens[i],get_if_variable(String.join(" ",val),context));
|
||||
i+=(1+val.size());
|
||||
}
|
||||
return optional_args;
|
||||
}
|
||||
|
||||
public static int get_dynamic_tokens(HashMap<String,String> args){
|
||||
int skip = args.size();
|
||||
for (String s : args.values())
|
||||
skip += s.split(" ").length;
|
||||
return skip;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,13 @@ import java.util.Map;
|
|||
public class ExecutionContext {
|
||||
public BaseScript script;
|
||||
public Map<String, Object> variable = new HashMap<>();
|
||||
public final boolean dummy;
|
||||
|
||||
public ExecutionContext(){this.dummy=false;}
|
||||
|
||||
public ExecutionContext(boolean dummy){
|
||||
this.dummy=dummy;
|
||||
}
|
||||
|
||||
public void set_value(String key, Object value){
|
||||
variable.remove(key);
|
||||
|
|
|
@ -40,11 +40,11 @@ public class TokenParticle implements BaseToken {
|
|||
@Override
|
||||
public boolean canHandle(String[] tokens, int step) {
|
||||
String token = tokens[step];
|
||||
return token.equals("particle") && tokens.length>=step+5+BaseScript.get_optional_args(
|
||||
return token.equals("particle") && tokens.length>=step+5+BaseScript.get_dynamic_tokens(BaseScript.get_optional_args(
|
||||
tokens,
|
||||
step+5,
|
||||
optional_args
|
||||
).size()*2;
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -95,48 +95,16 @@ public class TokenParticle implements BaseToken {
|
|||
Map<String,String> args = BaseScript.get_optional_args(
|
||||
tokens,
|
||||
step+5,
|
||||
optional_args
|
||||
optional_args,
|
||||
executionContext
|
||||
);
|
||||
GameParticleSystem system = GameParticleSystem.laserParticleSystem.copy();
|
||||
|
||||
String color_key = args.getOrDefault("color","SKY");
|
||||
Color c;
|
||||
switch (color_key.toLowerCase()){
|
||||
case "light_gray" -> c = Color.LIGHT_GRAY;
|
||||
case "gray" -> c = Color.GRAY;
|
||||
case "dark_gray" -> c = Color.DARK_GRAY;
|
||||
case "black" -> c = Color.BLACK;
|
||||
case "blue" -> c = Color.BLUE;
|
||||
case "navy" -> c = Color.NAVY;
|
||||
case "royal" -> c = Color.ROYAL;
|
||||
case "slate" -> c = Color.SLATE;
|
||||
case "sky" -> c = Color.SKY;
|
||||
case "cyan" -> c = Color.CYAN;
|
||||
case "teal" -> c = Color.TEAL;
|
||||
case "green" -> c = Color.GREEN;
|
||||
case "chartreuse" -> c = Color.CHARTREUSE;
|
||||
case "lime" -> c = Color.LIME;
|
||||
case "forest" -> c = Color.FOREST;
|
||||
case "olive" -> c = Color.OLIVE;
|
||||
case "yellow" -> c = Color.YELLOW;
|
||||
case "gold" -> c = Color.GOLD;
|
||||
case "goldenrod" -> c = Color.GOLDENROD;
|
||||
case "orange" -> c = Color.ORANGE;
|
||||
case "brown" -> c = Color.BROWN;
|
||||
case "tan" -> c = Color.TAN;
|
||||
case "firebrick" -> c = Color.FIREBRICK;
|
||||
case "red" -> c = Color.RED;
|
||||
case "scarlet" -> c = Color.SCARLET;
|
||||
case "coral" -> c = Color.CORAL;
|
||||
case "salmon" -> c = Color.SALMON;
|
||||
case "pink" -> c = Color.PINK;
|
||||
case "magenta" -> c = Color.MAGENTA;
|
||||
case "purple" -> c = Color.PURPLE;
|
||||
case "violet" -> c = Color.VIOLET;
|
||||
case "maroon" -> c = Color.MAROON;
|
||||
default -> c = Color.LIGHT_GRAY;
|
||||
}
|
||||
if (color_key.startsWith("#"))
|
||||
Color c = new Color();
|
||||
if (BaseScript.colors.containsKey(color_key))
|
||||
c=BaseScript.colors.get(color_key);
|
||||
else
|
||||
Color.valueOf(color_key,c);
|
||||
system.startColor.set(c);
|
||||
|
||||
|
|
|
@ -1,20 +1,11 @@
|
|||
package net.pietru.cookie_utils.scripting.tokens;
|
||||
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import finalforeach.cosmicreach.GameSingletons;
|
||||
import finalforeach.cosmicreach.entities.player.Player;
|
||||
import finalforeach.cosmicreach.networking.packets.ParticleSystemPacket;
|
||||
import finalforeach.cosmicreach.networking.packets.entities.MaxHPEntityPacket;
|
||||
import finalforeach.cosmicreach.networking.packets.entities.PlayerPositionPacket;
|
||||
import finalforeach.cosmicreach.networking.server.ServerIdentity;
|
||||
import finalforeach.cosmicreach.networking.server.ServerSingletons;
|
||||
import finalforeach.cosmicreach.particles.GameParticleSystem;
|
||||
import finalforeach.cosmicreach.particles.IParticleComponent;
|
||||
import finalforeach.cosmicreach.particles.spawn.GameParticleSpawnSphere;
|
||||
import finalforeach.cosmicreach.world.Zone;
|
||||
import net.pietru.cookie_utils.scripting.BaseScript;
|
||||
import net.pietru.cookie_utils.scripting.ExecutionContext;
|
||||
import net.pietru.cookie_utils.utils.ParticleUtil;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -52,11 +43,11 @@ public class TokenPlayer implements BaseToken {
|
|||
* */
|
||||
@Override
|
||||
public int next_token(String[] tokens, int step) {
|
||||
return 2+BaseScript.get_optional_args(
|
||||
return 2+BaseScript.get_dynamic_tokens(BaseScript.get_optional_args(
|
||||
tokens,
|
||||
step+2,
|
||||
optional_args
|
||||
).size()*2;
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -76,7 +67,8 @@ public class TokenPlayer implements BaseToken {
|
|||
Map<String,String> args = BaseScript.get_optional_args(
|
||||
tokens,
|
||||
step+2,
|
||||
optional_args
|
||||
optional_args,
|
||||
executionContext
|
||||
);
|
||||
if (args.containsKey("velocity_x"))
|
||||
try {
|
||||
|
|
Loading…
Reference in a new issue