diff --git a/docs/src/codegen/codegen.md b/docs/src/codegen/codegen.md new file mode 100644 index 00000000..8a14459d --- /dev/null +++ b/docs/src/codegen/codegen.md @@ -0,0 +1,21 @@ + +## Behavior Trees + + +## Synchronized Fields + +## Supported Types +The types that are supported + +### Enums +How to work with enums + +### Frequencies +ie can we toggle how often it updates, or debounce it + + +## Usage Notes + +### Assumptions + - The first line of all parsed files is the package declaration for the package that this file is inside of (ie the behavior tree electrosphere.whatever.myTree has "package electrosphere.whatever" at the top) + This is so that imports generation in the helper functions file can be performed accurately diff --git a/src/main/java/electrosphere/engine/Globals.java b/src/main/java/electrosphere/engine/Globals.java index bccd1675..0af64c53 100644 --- a/src/main/java/electrosphere/engine/Globals.java +++ b/src/main/java/electrosphere/engine/Globals.java @@ -43,6 +43,7 @@ import electrosphere.net.monitor.NetMonitor; import electrosphere.net.server.Server; import electrosphere.net.server.player.Player; import electrosphere.net.server.player.PlayerManager; +import electrosphere.net.synchronization.EntityValueTrackingService; import electrosphere.renderer.Material; import electrosphere.renderer.RenderUtils; import electrosphere.renderer.RenderingEngine; @@ -154,6 +155,11 @@ public class Globals { public static ServerWorldData serverWorldData; public static RealmManager realmManager; public static EntityDataCellMapper entityDataCellMapper; + + // + //behavior tree tracking service + // + public static EntityValueTrackingService entityValueTrackingService; // //Player manager @@ -394,6 +400,8 @@ public class Globals { gameConfigCurrent = gameConfigDefault; //player manager playerManager = new PlayerManager(); + //behavior tree tracking service + entityValueTrackingService = new EntityValueTrackingService(); //net monitor if(Globals.userSettings.getNetRunNetMonitor()){ netMonitor = new NetMonitor(); diff --git a/src/main/java/electrosphere/entity/state/idle/IdleTree.java b/src/main/java/electrosphere/entity/state/idle/IdleTree.java index 9aa2a618..1c79680d 100644 --- a/src/main/java/electrosphere/entity/state/idle/IdleTree.java +++ b/src/main/java/electrosphere/entity/state/idle/IdleTree.java @@ -9,11 +9,12 @@ import electrosphere.engine.Globals; import electrosphere.entity.Entity; import electrosphere.entity.EntityDataStrings; import electrosphere.entity.EntityUtils; -import electrosphere.entity.annotation.SyncedField; -import electrosphere.entity.annotation.SynchronizedBehaviorTree; import electrosphere.entity.state.movement.GroundMovementTree.MovementTreeState; import electrosphere.entity.types.creature.CreatureUtils; import electrosphere.net.parser.net.message.EntityMessage; +import electrosphere.net.synchronization.annotation.SyncedField; +import electrosphere.net.synchronization.annotation.SynchronizableEnum; +import electrosphere.net.synchronization.annotation.SynchronizedBehaviorTree; import electrosphere.renderer.actor.Actor; import electrosphere.renderer.anim.Animation; @@ -22,17 +23,24 @@ import java.util.concurrent.CopyOnWriteArrayList; import org.joml.Vector3d; -@SynchronizedBehaviorTree(name = "idle", isServer = false) +@SynchronizedBehaviorTree(name = "idle", isServer = false, correspondingTree="serverIdle") +/** + * Tree for playing an idle animation when an entity isn't doing anything + */ public class IdleTree implements BehaviorTree { + @SynchronizableEnum public static enum IdleTreeState { IDLE, NOT_IDLE, } - @SyncedField(isEnum = true, enumId = 0) + @SyncedField IdleTreeState state; + @SyncedField + int test; + Entity parent; CopyOnWriteArrayList networkMessageQueue = new CopyOnWriteArrayList(); diff --git a/src/main/java/electrosphere/entity/state/idle/ServerIdleTree.java b/src/main/java/electrosphere/entity/state/idle/ServerIdleTree.java index cd925aaf..280d6a91 100644 --- a/src/main/java/electrosphere/entity/state/idle/ServerIdleTree.java +++ b/src/main/java/electrosphere/entity/state/idle/ServerIdleTree.java @@ -3,6 +3,7 @@ package electrosphere.entity.state.idle; import electrosphere.entity.state.BehaviorTree; import electrosphere.entity.state.attack.ServerAttackTree; import electrosphere.entity.state.attack.ServerAttackTree.AttackTreeState; +import electrosphere.entity.state.idle.IdleTree.IdleTreeState; import electrosphere.entity.state.movement.AirplaneMovementTree; import electrosphere.entity.state.movement.ServerGroundMovementTree; import electrosphere.engine.Globals; @@ -10,11 +11,12 @@ import electrosphere.entity.Entity; import electrosphere.entity.EntityDataStrings; import electrosphere.entity.EntityUtils; import electrosphere.engine.Main; -import electrosphere.entity.annotation.SyncedField; -import electrosphere.entity.annotation.SynchronizedBehaviorTree; import electrosphere.entity.state.movement.ServerGroundMovementTree.MovementTreeState; import electrosphere.entity.types.creature.CreatureUtils; import electrosphere.net.parser.net.message.EntityMessage; +import electrosphere.net.synchronization.BehaviorTreeIdEnums; +import electrosphere.net.synchronization.annotation.SyncedField; +import electrosphere.net.synchronization.annotation.SynchronizedBehaviorTree; import electrosphere.renderer.actor.Actor; import electrosphere.renderer.anim.Animation; @@ -24,14 +26,12 @@ import org.joml.Vector3d; @SynchronizedBehaviorTree(name = "serverIdle", isServer = true) +/** + * Tree for playing an idle animation when an entity isn't doing anything + */ public class ServerIdleTree { - public static enum IdleTreeState { - IDLE, - NOT_IDLE, - } - - @SyncedField(isEnum = true, enumId = 0) + @SyncedField IdleTreeState state; Entity parent; @@ -175,4 +175,28 @@ public class ServerIdleTree { networkMessageQueue.add(networkMessage); } + /** + *

Automatically generated

+ *

+ * Attaches this tree to the entity. + *

+ * @param entity The entity to attach to + * @param tree The behavior tree to attach + */ + public static void attachTree(Entity entity, BehaviorTree tree){ + Globals.entityValueTrackingService.attachTreeToEntity(entity, BehaviorTreeIdEnums.BTREE_SERVERIDLE_ID); + } + + /** + *

Automatically generated

+ *

+ * Detatches this tree from the entity. + *

+ * @param entity The entity to detach to + * @param tree The behavior tree to detach + */ + public static void detachTree(Entity entity, BehaviorTree tree){ + Globals.entityValueTrackingService.detatchTreeFromEntity(entity, BehaviorTreeIdEnums.BTREE_SERVERIDLE_ID); + } + } \ No newline at end of file diff --git a/src/main/java/electrosphere/net/synchronization/BehaviorTreeIdEnums.java b/src/main/java/electrosphere/net/synchronization/BehaviorTreeIdEnums.java new file mode 100644 index 00000000..f68b834f --- /dev/null +++ b/src/main/java/electrosphere/net/synchronization/BehaviorTreeIdEnums.java @@ -0,0 +1,11 @@ +package electrosphere.net.synchronization; + +/** + * List of enums for each automatically synchronized behavior tree. + */ +public class BehaviorTreeIdEnums { + + public static final int BTREE_IDLE_ID = 0; + public static final int BTREE_SERVERIDLE_ID = 1; + +} diff --git a/src/main/java/electrosphere/net/synchronization/ClientSynchronizationManager.java b/src/main/java/electrosphere/net/synchronization/ClientSynchronizationManager.java new file mode 100644 index 00000000..2eaffc24 --- /dev/null +++ b/src/main/java/electrosphere/net/synchronization/ClientSynchronizationManager.java @@ -0,0 +1,8 @@ +package electrosphere.net.synchronization; + +/** + * Takes in raw behavior tree packets from server and pushes their values into respective behavior trees in entities + */ +public class ClientSynchronizationManager { + +} diff --git a/src/main/java/electrosphere/net/sync/EntityValueTrackingService.java b/src/main/java/electrosphere/net/synchronization/EntityValueTrackingService.java similarity index 97% rename from src/main/java/electrosphere/net/sync/EntityValueTrackingService.java rename to src/main/java/electrosphere/net/synchronization/EntityValueTrackingService.java index a33927eb..1bedb86c 100644 --- a/src/main/java/electrosphere/net/sync/EntityValueTrackingService.java +++ b/src/main/java/electrosphere/net/synchronization/EntityValueTrackingService.java @@ -1,4 +1,4 @@ -package electrosphere.net.sync; +package electrosphere.net.synchronization; import java.util.HashMap; import java.util.LinkedList; diff --git a/src/main/java/electrosphere/net/sync/TypeTranslator.java b/src/main/java/electrosphere/net/synchronization/TypeTranslator.java similarity index 94% rename from src/main/java/electrosphere/net/sync/TypeTranslator.java rename to src/main/java/electrosphere/net/synchronization/TypeTranslator.java index b3de145d..1cb9b777 100644 --- a/src/main/java/electrosphere/net/sync/TypeTranslator.java +++ b/src/main/java/electrosphere/net/synchronization/TypeTranslator.java @@ -1,4 +1,4 @@ -package electrosphere.net.sync; +package electrosphere.net.synchronization; import electrosphere.entity.state.idle.IdleTree.IdleTreeState; diff --git a/src/main/java/electrosphere/entity/annotation/SyncedField.java b/src/main/java/electrosphere/net/synchronization/annotation/SyncedField.java similarity index 52% rename from src/main/java/electrosphere/entity/annotation/SyncedField.java rename to src/main/java/electrosphere/net/synchronization/annotation/SyncedField.java index 6da3c1e8..9be02821 100644 --- a/src/main/java/electrosphere/entity/annotation/SyncedField.java +++ b/src/main/java/electrosphere/net/synchronization/annotation/SyncedField.java @@ -1,21 +1,15 @@ -package electrosphere.entity.annotation; +package electrosphere.net.synchronization.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -@Retention(RetentionPolicy.RUNTIME) +@Retention(RetentionPolicy.SOURCE) @Target(ElementType.FIELD) /** * A field in a synchronized behavior tree that is synchronized between the server and the client. */ public @interface SyncedField { - - //True if the field is an enum - boolean isEnum() default false; - - //if the field is an enum, this value is MANDETORY. Determines what id will be set to when serialized/deserialized - int enumId() default 0; } diff --git a/src/main/java/electrosphere/net/synchronization/annotation/SynchronizableEnum.java b/src/main/java/electrosphere/net/synchronization/annotation/SynchronizableEnum.java new file mode 100644 index 00000000..1366f480 --- /dev/null +++ b/src/main/java/electrosphere/net/synchronization/annotation/SynchronizableEnum.java @@ -0,0 +1,15 @@ +package electrosphere.net.synchronization.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.SOURCE) +@Target(ElementType.TYPE) +/** + * Annotation that delineates an enum that should be synchronized over the network + */ +public @interface SynchronizableEnum { + +} diff --git a/src/main/java/electrosphere/entity/annotation/SynchronizedBehaviorTree.java b/src/main/java/electrosphere/net/synchronization/annotation/SynchronizedBehaviorTree.java similarity index 65% rename from src/main/java/electrosphere/entity/annotation/SynchronizedBehaviorTree.java rename to src/main/java/electrosphere/net/synchronization/annotation/SynchronizedBehaviorTree.java index 40bf3a0b..2847e040 100644 --- a/src/main/java/electrosphere/entity/annotation/SynchronizedBehaviorTree.java +++ b/src/main/java/electrosphere/net/synchronization/annotation/SynchronizedBehaviorTree.java @@ -1,11 +1,11 @@ -package electrosphere.entity.annotation; +package electrosphere.net.synchronization.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -@Retention(RetentionPolicy.RUNTIME) +@Retention(RetentionPolicy.SOURCE) @Target(ElementType.TYPE) /** * A behavior tree that will have modifications by code generation. @@ -18,5 +18,8 @@ public @interface SynchronizedBehaviorTree { //True if this is a server-side behavior tree public boolean isServer() default false; + + //The corresponding behavior tree. If this is a server tree, it is the corresponding client tree. If this is a client tree, it is the corresponding server tree + public String correspondingTree() default ""; }