Skip to content

Commit

Permalink
Logic data instruction (#9935)
Browse files Browse the repository at this point in the history
* Logic `data` instruction

* Rename to `clientdata` & reliable option

* frog

Co-authored-by: Anuken <arnukren@gmail.com>

---------

Co-authored-by: Anuken <arnukren@gmail.com>
  • Loading branch information
Redstonneur1256 and Anuken committed Jun 16, 2024
1 parent 2e460bf commit 58dbc51
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
21 changes: 21 additions & 0 deletions core/src/mindustry/core/NetServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public class NetServer implements ApplicationListener{
private DataOutputStream dataStream = new DataOutputStream(syncStream);
/** Packet handlers for custom types of messages. */
private ObjectMap<String, Seq<Cons2<Player, String>>> customPacketHandlers = new ObjectMap<>();
/** Packet handlers for logic client data */
private ObjectMap<String, Seq<Cons2<Player, Object>>> logicClientDataHandlers = new ObjectMap<>();

public NetServer(){

Expand Down Expand Up @@ -515,6 +517,10 @@ public Seq<Cons2<Player, String>> getPacketHandlers(String type){
return customPacketHandlers.get(type, Seq::new);
}

public void addLogicDataHandler(String type, Cons2<Player, Object> handler){
logicClientDataHandlers.get(type, Seq::new).add(handler);
}

public static void onDisconnect(Player player, String reason){
//singleplayer multiplayer weirdness
if(player.con == null){
Expand Down Expand Up @@ -583,6 +589,21 @@ public static void serverPacketUnreliable(Player player, String type, String con
serverPacketReliable(player, type, contents);
}

@Remote(targets = Loc.client)
public static void clientLogicDataReliable(Player player, String channel, Object value){
Seq<Cons2<Player, Object>> handlers = netServer.logicClientDataHandlers.get(channel);
if(handlers != null){
for(Cons2<Player, Object> handler : handlers){
handler.get(player, value);
}
}
}

@Remote(targets = Loc.client, unreliable = true)
public static void clientLogicDataUnreliable(Player player, String channel, Object value){
clientLogicDataReliable(player, channel, value);
}

private static boolean invalid(float f){
return Float.isInfinite(f) || Float.isNaN(f);
}
Expand Down
2 changes: 2 additions & 0 deletions core/src/mindustry/game/Rules.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ public class Rules{
public @Nullable PlanetParams planetBackground;
/** Rules from this planet are applied. If it's {@code sun}, mixed tech is enabled. */
public Planet planet = Planets.serpulo;
/** If the `data` instruction is allowed for world processors */
public boolean allowLogicData = false;

/** Copies this ruleset exactly. Not efficient at all, do not use often. */
public Rules copy(){
Expand Down
26 changes: 26 additions & 0 deletions core/src/mindustry/logic/LExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class LExecutor{
maxDisplayBuffer = 1024,
maxTextBuffer = 400;


public LInstruction[] instructions = {};
/** Non-constant variables used for network sync */
public LVar[] vars = {};
Expand Down Expand Up @@ -1761,6 +1762,31 @@ public void run(LExecutor exec){
}
}

public static class ClientDataI implements LInstruction{
public LVar channel, value, reliable;

public ClientDataI(LVar channel, LVar value, LVar reliable){
this.channel = channel;
this.value = value;
this.reliable = reliable;
}

public ClientDataI() {
}

@Override
public void run(LExecutor exec) {
if(channel.obj() instanceof String c){
Object v = value.isobj ? value.objval : value.numval;
if(reliable.bool()){
Call.clientLogicDataReliable(c, v);
}else{
Call.clientLogicDataUnreliable(c, v);
}
}
}
}

public static class GetFlagI implements LInstruction{
public LVar result, flag;

Expand Down
36 changes: 36 additions & 0 deletions core/src/mindustry/logic/LStatements.java
Original file line number Diff line number Diff line change
Expand Up @@ -1911,6 +1911,42 @@ public LCategory category(){
}
}

@RegisterStatement("clientdata")
public static class ClientDataStatement extends LStatement{
public String channel = "\"frog\"", value = "\"bar\"", reliable = "0";

@Override
public void build(Table table){
table.add("send ");
fields(table, value, str -> value = str);
table.add(" on ");
fields(table, channel, str -> channel = str);
table.add(", reliable ");
fields(table, channel, str -> channel = str);
}

@Override
public boolean hidden(){
return true;
}

@Override
public boolean privileged(){
return true;
}

@Override
public LInstruction build(LAssembler builder){
if(!state.rules.allowLogicData) return null;
return new ClientDataI(builder.var(channel), builder.var(value), builder.var(reliable));
}

@Override
public LCategory category(){
return LCategory.world;
}
}

@RegisterStatement("getflag")
public static class GetFlagStatement extends LStatement{
public String result = "result", flag = "\"flag\"";
Expand Down

0 comments on commit 58dbc51

Please sign in to comment.