From a07e275e1135ca215e4ebaa8be1802f7ddf04878 Mon Sep 17 00:00:00 2001 From: Anuken Date: Tue, 4 Feb 2020 01:03:34 -0500 Subject: [PATCH] Entity groups --- .../mindustry/annotations/Annotations.java | 2 + .../annotations/impl/EntityProcess.java | 88 ++++- .../java/main/mindustry/gen/Call.java | 100 ++---- core/src/mindustry/entities/EntityGroup.java | 22 +- .../mindustry/entities/bullet/BulletType.java | 24 +- .../mindustry/entities/def/EntityComps.java | 4 + .../mindustry/entities/def/EntityDefs.java | 3 + .../entities/def/EntityGroupDefs.java | 18 + .../mindustry/entities/effect/Lightning.java | 26 +- core/src/mindustry/entities/type/Tilec.java | 313 ------------------ core/src/mindustry/world/Tile.java | 17 + gradle.properties | 2 +- 12 files changed, 183 insertions(+), 436 deletions(-) delete mode 100644 core/src/mindustry/entities/type/Tilec.java diff --git a/annotations/src/main/java/mindustry/annotations/Annotations.java b/annotations/src/main/java/mindustry/annotations/Annotations.java index 79d88c068a..d78d05aa5e 100644 --- a/annotations/src/main/java/mindustry/annotations/Annotations.java +++ b/annotations/src/main/java/mindustry/annotations/Annotations.java @@ -29,6 +29,8 @@ public class Annotations{ @Retention(RetentionPolicy.SOURCE) public @interface GroupDef{ Class[] value(); + boolean spatial() default false; + boolean mapping() default false; } /** Indicates an entity definition. */ diff --git a/annotations/src/main/java/mindustry/annotations/impl/EntityProcess.java b/annotations/src/main/java/mindustry/annotations/impl/EntityProcess.java index b4b89b22c7..4288a7ff30 100644 --- a/annotations/src/main/java/mindustry/annotations/impl/EntityProcess.java +++ b/annotations/src/main/java/mindustry/annotations/impl/EntityProcess.java @@ -1,5 +1,6 @@ package mindustry.annotations.impl; +import arc.func.*; import arc.struct.*; import arc.util.*; import com.squareup.javapoet.*; @@ -12,6 +13,7 @@ import mindustry.annotations.util.*; import javax.annotation.processing.*; import javax.lang.model.element.*; import javax.lang.model.type.*; +import java.lang.annotation.*; @SupportedAnnotationTypes({ "mindustry.annotations.Annotations.EntityDef", @@ -20,6 +22,7 @@ import javax.lang.model.type.*; }) public class EntityProcess extends BaseProcessor{ Array definitions = new Array<>(); + Array groupDefs = new Array<>(); Array baseComponents; ObjectMap componentNames = new ObjectMap<>(); ObjectMap> componentDependencies = new ObjectMap<>(); @@ -36,9 +39,11 @@ public class EntityProcess extends BaseProcessor{ //round 1: get component classes and generate interfaces for them if(round == 1){ baseComponents = types(BaseComponent.class); + Array allGroups = methods(GroupDef.class); Array allDefs = types(EntityDef.class); Array allComponents = types(Component.class); + //store components for(Stype type : allComponents){ componentNames.put(type.name(), type); } @@ -48,6 +53,12 @@ public class EntityProcess extends BaseProcessor{ imports.addAll(getImports(comp.e)); } + //parse groups + for(Smethod group : allGroups){ + GroupDef an = group.annotation(GroupDef.class); + groupDefs.add(new GroupDefinition(group.name(), types(an, GroupDef::value), an)); + } + //create component interfaces for(Stype component : allComponents){ Log.info("&yGenerating interface for " + component); @@ -97,6 +108,7 @@ public class EntityProcess extends BaseProcessor{ TypeSpec.Builder builder = TypeSpec.classBuilder(name).addModifiers(Modifier.PUBLIC, Modifier.FINAL); Array components = allComponents(type); + Array groups = groupDefs.select(g -> !g.components.contains(s -> !components.contains(s))); ObjectMap> methods = new ObjectMap<>(); //add all components @@ -173,6 +185,14 @@ public class EntityProcess extends BaseProcessor{ //trim block str = str.substring(2, str.length() - 1); + //SPECIAL CASE: inject group add/remove code + if(elem.name().equals("add") || elem.name().equals("remove")){ + for(GroupDefinition def : groups){ + //remove/add from each group, assume imported + mbuilder.addStatement("Groups.$L.$L(this)", def.name, elem.name()); + } + } + //make sure to remove braces here mbuilder.addCode(str); @@ -183,19 +203,37 @@ public class EntityProcess extends BaseProcessor{ builder.addMethod(mbuilder.build()); } - definitions.add(new Definition(builder, type)); - + definitions.add(new Definition(builder, type, components, groups)); } + + //generate groups + TypeSpec.Builder groupsBuilder = TypeSpec.classBuilder("Groups").addModifiers(Modifier.PUBLIC); + MethodSpec.Builder groupInit = MethodSpec.methodBuilder("init").addModifiers(Modifier.PUBLIC, Modifier.STATIC); + for(GroupDefinition group : groupDefs){ + Stype ctype = group.components.first(); + ClassName itype = ClassName.bestGuess("mindustry.gen." + interfaceName(ctype)); + ClassName groupc = ClassName.bestGuess("mindustry.entities.EntityGroup"); + + //add field... + groupsBuilder.addField(ParameterizedTypeName.get( + ClassName.bestGuess("mindustry.entities.EntityGroup"), itype), group.name, Modifier.PUBLIC, Modifier.STATIC); + + groupInit.addStatement("$L = new $T<>($L, $L)", group.name, groupc, group.def.spatial(), group.def.mapping()); + } + + groupsBuilder.addMethod(groupInit.build()); + + write(groupsBuilder); + }else{ //round 2: generate actual classes and implement interfaces Array interfaces = types(EntityInterface.class); //implement each definition for(Definition def : definitions){ - Array components = allComponents(def.base); //get interface for each component - for(Stype comp : components){ + for(Stype comp : def.components){ //implement the interface Stype inter = interfaces.find(i -> i.name().equals(interfaceName(comp))); if(inter == null){ @@ -243,7 +281,7 @@ public class EntityProcess extends BaseProcessor{ Array allComponents(Stype type){ if(!defComponents.containsKey(type)){ //get base defs - Array components = Array.with(mirrors(type)).map(Stype::of); + Array components = types(type.annotation(EntityDef.class), EntityDef::value); ObjectSet out = new ObjectSet<>(); for(Stype comp : components){ //get dependencies for each def, add them @@ -297,22 +335,52 @@ public class EntityProcess extends BaseProcessor{ return type.annotation(Component.class) != null; } - TypeMirror[] mirrors(Stype type){ + Array types(T t, Cons consumer){ try{ - type.annotation(EntityDef.class).value(); + consumer.get(t); }catch(MirroredTypesException e){ - return e.getTypeMirrors().toArray(new TypeMirror[0]); + return Array.with(e.getTypeMirrors()).map(Stype::of); + } + throw new IllegalArgumentException("Missing types."); + } + + class GroupDefinition{ + final String name; + final Array components; + final GroupDef def; + + public GroupDefinition(String name, Array components, GroupDef def){ + this.components = components; + this.name = name; + this.def = def; + } + + @Override + public String toString(){ + return name; } - throw new IllegalArgumentException("Missing components: " + type); } class Definition{ + final Array groups; + final Array components; final TypeSpec.Builder builder; final Stype base; - public Definition(Builder builder, Stype base){ + public Definition(Builder builder, Stype base, Array components, Array groups){ this.builder = builder; this.base = base; + this.groups = groups; + this.components = components; + } + + @Override + public String toString(){ + return "Definition{" + + "groups=" + groups + + "components=" + components + + ", base=" + base + + '}'; } } } diff --git a/core/build/generated/sources/annotationProcessor/java/main/mindustry/gen/Call.java b/core/build/generated/sources/annotationProcessor/java/main/mindustry/gen/Call.java index 6ac4f59eff..569750ab53 100644 --- a/core/build/generated/sources/annotationProcessor/java/main/mindustry/gen/Call.java +++ b/core/build/generated/sources/annotationProcessor/java/main/mindustry/gen/Call.java @@ -632,7 +632,7 @@ public class Call { public static synchronized void onTileDamage(Tile tile, float health) { if(mindustry.Vars.net.server() || !mindustry.Vars.net.active()) { - mindustry.entities.type.Tilec__.onTileDamage(tile, health); + mindustry.world.Tile.onTileDamage(tile, health); } if(mindustry.Vars.net.server()) { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); @@ -649,7 +649,7 @@ public class Call { public static synchronized void onTileDestroyed(Tile tile) { if(mindustry.Vars.net.server() || !mindustry.Vars.net.active()) { - mindustry.entities.type.Tilec__.onTileDestroyed(tile); + mindustry.world.Tile.onTileDestroyed(tile); } if(mindustry.Vars.net.server()) { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); @@ -714,22 +714,6 @@ public class Call { } } - public static synchronized void onUnitDeath(BaseUnit unit) { - if(mindustry.Vars.net.server() || !mindustry.Vars.net.active()) { - mindustry.entities.type.BaseUnit.onUnitDeath(unit); - } - if(mindustry.Vars.net.server()) { - mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); - packet.writeBuffer = TEMP_BUFFER; - packet.priority = (byte)0; - packet.type = (byte)36; - TEMP_BUFFER.position(0); - mindustry.io.TypeIO.writeBaseUnit(TEMP_BUFFER, unit); - packet.writeLength = TEMP_BUFFER.position(); - mindustry.Vars.net.send(packet, mindustry.net.Net.SendMode.tcp); - } - } - public static synchronized void onUnitFactorySpawn(Tile tile, int spawns) { if(mindustry.Vars.net.server() || !mindustry.Vars.net.active()) { mindustry.world.blocks.units.UnitFactory.onUnitFactorySpawn(tile, spawns); @@ -738,7 +722,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)37; + packet.type = (byte)36; TEMP_BUFFER.position(0); mindustry.io.TypeIO.writeTile(TEMP_BUFFER, tile); TEMP_BUFFER.putInt(spawns); @@ -755,7 +739,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)38; + packet.type = (byte)37; TEMP_BUFFER.position(0); mindustry.io.TypeIO.writeTile(TEMP_BUFFER, tile); mindustry.io.TypeIO.writePlayer(TEMP_BUFFER, player); @@ -769,7 +753,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)39; + packet.type = (byte)38; TEMP_BUFFER.position(0); packet.writeLength = TEMP_BUFFER.position(); mindustry.Vars.net.send(packet, mindustry.net.Net.SendMode.tcp); @@ -781,7 +765,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)39; + packet.type = (byte)38; TEMP_BUFFER.position(0); packet.writeLength = TEMP_BUFFER.position(); playerConnection.send(packet, mindustry.net.Net.SendMode.tcp); @@ -794,7 +778,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)40; + packet.type = (byte)39; TEMP_BUFFER.position(0); TEMP_BUFFER.putInt(x); TEMP_BUFFER.putInt(y); @@ -812,7 +796,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)41; + packet.type = (byte)40; TEMP_BUFFER.position(0); mindustry.io.TypeIO.writeTile(TEMP_BUFFER, tile); packet.writeLength = TEMP_BUFFER.position(); @@ -820,42 +804,6 @@ public class Call { } } - public static synchronized void removeUnitEditor(Player player, BaseUnit unit) { - if(mindustry.Vars.net.server() || !mindustry.Vars.net.active()) { - mindustry.ui.fragments.HudFragment.removeUnitEditor(player, unit); - } - if(mindustry.Vars.net.server() || mindustry.Vars.net.client()) { - mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); - packet.writeBuffer = TEMP_BUFFER; - packet.priority = (byte)0; - packet.type = (byte)42; - TEMP_BUFFER.position(0); - if(mindustry.Vars.net.server()) { - mindustry.io.TypeIO.writePlayer(TEMP_BUFFER, player); - } - mindustry.io.TypeIO.writeBaseUnit(TEMP_BUFFER, unit); - packet.writeLength = TEMP_BUFFER.position(); - mindustry.Vars.net.send(packet, mindustry.net.Net.SendMode.tcp); - } - } - - static synchronized void removeUnitEditor__forward(NetConnection exceptConnection, Player player, - BaseUnit unit) { - if(mindustry.Vars.net.server() || mindustry.Vars.net.client()) { - mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); - packet.writeBuffer = TEMP_BUFFER; - packet.priority = (byte)0; - packet.type = (byte)42; - TEMP_BUFFER.position(0); - if(mindustry.Vars.net.server()) { - mindustry.io.TypeIO.writePlayer(TEMP_BUFFER, player); - } - mindustry.io.TypeIO.writeBaseUnit(TEMP_BUFFER, unit); - packet.writeLength = TEMP_BUFFER.position(); - mindustry.Vars.net.send(packet, mindustry.net.Net.SendMode.tcp); - } - } - public static synchronized void requestItem(Player player, Tile tile, Item item, int amount) { if(mindustry.Vars.net.server() || !mindustry.Vars.net.active()) { mindustry.ui.fragments.BlockInventoryFragment.requestItem(player, tile, item, amount); @@ -864,7 +812,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)43; + packet.type = (byte)42; TEMP_BUFFER.position(0); if(mindustry.Vars.net.server()) { mindustry.io.TypeIO.writePlayer(TEMP_BUFFER, player); @@ -883,7 +831,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)43; + packet.type = (byte)42; TEMP_BUFFER.position(0); if(mindustry.Vars.net.server()) { mindustry.io.TypeIO.writePlayer(TEMP_BUFFER, player); @@ -904,7 +852,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)44; + packet.type = (byte)43; TEMP_BUFFER.position(0); if(mindustry.Vars.net.server()) { mindustry.io.TypeIO.writePlayer(TEMP_BUFFER, player); @@ -922,7 +870,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)44; + packet.type = (byte)43; TEMP_BUFFER.position(0); if(mindustry.Vars.net.server()) { mindustry.io.TypeIO.writePlayer(TEMP_BUFFER, player); @@ -942,7 +890,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)45; + packet.type = (byte)44; TEMP_BUFFER.position(0); mindustry.io.TypeIO.writeString(TEMP_BUFFER, message); packet.writeLength = TEMP_BUFFER.position(); @@ -958,7 +906,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)46; + packet.type = (byte)45; TEMP_BUFFER.position(0); mindustry.io.TypeIO.writeString(TEMP_BUFFER, message); packet.writeLength = TEMP_BUFFER.position(); @@ -971,7 +919,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)47; + packet.type = (byte)46; TEMP_BUFFER.position(0); mindustry.io.TypeIO.writeString(TEMP_BUFFER, message); mindustry.io.TypeIO.writeString(TEMP_BUFFER, sender); @@ -987,7 +935,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)47; + packet.type = (byte)46; TEMP_BUFFER.position(0); mindustry.io.TypeIO.writeString(TEMP_BUFFER, message); mindustry.io.TypeIO.writeString(TEMP_BUFFER, sender); @@ -1003,7 +951,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)48; + packet.type = (byte)47; TEMP_BUFFER.position(0); if(mindustry.Vars.net.server()) { mindustry.io.TypeIO.writePlayer(TEMP_BUFFER, player); @@ -1021,7 +969,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)48; + packet.type = (byte)47; TEMP_BUFFER.position(0); if(mindustry.Vars.net.server()) { mindustry.io.TypeIO.writePlayer(TEMP_BUFFER, player); @@ -1039,7 +987,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)49; + packet.type = (byte)48; TEMP_BUFFER.position(0); if(mindustry.Vars.net.server()) { mindustry.io.TypeIO.writePlayer(TEMP_BUFFER, player); @@ -1056,7 +1004,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)49; + packet.type = (byte)48; TEMP_BUFFER.position(0); if(mindustry.Vars.net.server()) { mindustry.io.TypeIO.writePlayer(TEMP_BUFFER, player); @@ -1075,7 +1023,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)50; + packet.type = (byte)49; TEMP_BUFFER.position(0); mindustry.io.TypeIO.writeTile(TEMP_BUFFER, tile); mindustry.io.TypeIO.writeBlock(TEMP_BUFFER, block); @@ -1094,7 +1042,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)52; + packet.type = (byte)51; TEMP_BUFFER.position(0); if(mindustry.Vars.net.server()) { mindustry.io.TypeIO.writePlayer(TEMP_BUFFER, player); @@ -1111,7 +1059,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)52; + packet.type = (byte)51; TEMP_BUFFER.position(0); if(mindustry.Vars.net.server()) { mindustry.io.TypeIO.writePlayer(TEMP_BUFFER, player); @@ -1131,7 +1079,7 @@ public class Call { mindustry.net.Packets.InvokePacket packet = arc.util.pooling.Pools.obtain(mindustry.net.Packets.InvokePacket.class, mindustry.net.Packets.InvokePacket::new); packet.writeBuffer = TEMP_BUFFER; packet.priority = (byte)0; - packet.type = (byte)54; + packet.type = (byte)53; TEMP_BUFFER.position(0); mindustry.io.TypeIO.writeItem(TEMP_BUFFER, item); TEMP_BUFFER.putInt(amount); diff --git a/core/src/mindustry/entities/EntityGroup.java b/core/src/mindustry/entities/EntityGroup.java index fe1954bc4f..ac9bd5b8fb 100644 --- a/core/src/mindustry/entities/EntityGroup.java +++ b/core/src/mindustry/entities/EntityGroup.java @@ -10,7 +10,6 @@ import static mindustry.Vars.*; /** Represents a group of a certain type of entity.*/ @SuppressWarnings("unchecked") public class EntityGroup{ - private final boolean useTree; private final Array array = new Array<>(false, 32); private final Array intersectArray = new Array<>(); private final Rect intersectRect = new Rect(); @@ -19,12 +18,14 @@ public class EntityGroup{ private int index; - public EntityGroup(boolean useTree){ - this.useTree = useTree; - - if(useTree){ + public EntityGroup(boolean spatial, boolean mapping){ + if(spatial){ tree = new QuadTree<>(new Rect(0, 0, 0, 0)); } + + if(mapping){ + map = new IntMap<>(); + } } public void update(){ @@ -44,12 +45,7 @@ public class EntityGroup{ } public boolean useTree(){ - return useTree; - } - - public EntityGroup enableMapping(){ - map = new IntMap<>(); - return this; + return map != null; } public boolean mappingEnabled(){ @@ -86,13 +82,13 @@ public class EntityGroup{ } public QuadTree tree(){ - if(!useTree) throw new RuntimeException("This group does not support quadtrees! Enable quadtrees when creating it."); + if(tree == null) throw new RuntimeException("This group does not support quadtrees! Enable quadtrees when creating it."); return tree; } /** Resizes the internal quadtree, if it is enabled.*/ public void resize(float x, float y, float w, float h){ - if(useTree){ + if(tree != null){ tree = new QuadTree<>(new Rect(x, y, w, h)); } } diff --git a/core/src/mindustry/entities/bullet/BulletType.java b/core/src/mindustry/entities/bullet/BulletType.java index a2f815b4fd..a4328bd40b 100644 --- a/core/src/mindustry/entities/bullet/BulletType.java +++ b/core/src/mindustry/entities/bullet/BulletType.java @@ -3,6 +3,7 @@ package mindustry.entities.bullet; import arc.audio.*; import arc.math.*; import arc.math.geom.*; +import arc.struct.*; import arc.util.*; import arc.util.pooling.*; import mindustry.annotations.Annotations.*; @@ -144,7 +145,7 @@ public abstract class BulletType extends Content{ } for(int i = 0; i < lightining; i++){ - Lightning.createLighting(Lightning.nextSeed(), b.getTeam(), Pal.surge, damage, b.x, b.y, Mathf.random(360f), lightningLength); + Lightning.createLighting(Lightning.nextSeed(), b.getTeam(), Pal.surge, damage, b.getX(), b.getY(), Mathf.random(360f), lightningLength); } } @@ -162,11 +163,10 @@ public abstract class BulletType extends Content{ } public void update(Bulletc b){ - if(homingPower > 0.0001f){ - Teamc target = Units.closestTarget(b.getTeam(), b.x, b.y, homingRange, e -> !e.isFlying() || collidesAir); + Teamc target = Units.closestTarget(b.getTeam(), b.getX(), b.getY(), homingRange, e -> !e.isFlying() || collidesAir); if(target != null){ - b.velocity().setAngle(Mathf.slerpDelta(b.velocity().angle(), b.angleTo(target), 0.08f)); + b.getVel().setAngle(Mathf.slerpDelta(b.getRotation(), b.angleTo(target), 0.08f)); } } } @@ -194,6 +194,14 @@ public abstract class BulletType extends Content{ return create(owner, team, x, y, angle, velocityScl, lifetimeScl, null); } + public Bulletc create(Bulletc parent, float x, float y, float angle){ + return create(parent.getOwner(), parent.getTeam(), x, y, angle); + } + + public Bulletc create(Bulletc parent, float x, float y, float angle, float velocityScl){ + return create(parent.getOwner(), parent.getTeam(), x, y, angle, velocityScl); + } + public Bulletc create(Entityc owner, Team team, float x, float y, float angle, float velocityScl, float lifetimeScl, Object data){ @@ -220,12 +228,8 @@ public abstract class BulletType extends Content{ return bullet;*/ } - public Bulletc create(Bulletc parent, float x, float y, float angle){ - return create(parent.getOwner(), parent.getTeam(), x, y, angle); - } - - public Bulletc create(Bulletc parent, float x, float y, float angle, float velocityScl){ - return create(parent.getOwner(), parent.getTeam(), x, y, angle, velocityScl); + public void createNet(Team team, float x, float y, float angle, float velocityScl, float lifetimeScl){ + Call.createBullet(this, team, x, y, angle, velocityScl, lifetimeScl); } @Remote(called = Loc.server, unreliable = true) diff --git a/core/src/mindustry/entities/def/EntityComps.java b/core/src/mindustry/entities/def/EntityComps.java index eb4dc46642..f8fda07959 100644 --- a/core/src/mindustry/entities/def/EntityComps.java +++ b/core/src/mindustry/entities/def/EntityComps.java @@ -1242,6 +1242,10 @@ public class EntityComps{ public void update(){ //TODO fix effects, make everything poolable } + + public float clipSize(){ + return effect.size; + } } @Component diff --git a/core/src/mindustry/entities/def/EntityDefs.java b/core/src/mindustry/entities/def/EntityDefs.java index ed68dca6e5..86c944ba1b 100644 --- a/core/src/mindustry/entities/def/EntityDefs.java +++ b/core/src/mindustry/entities/def/EntityDefs.java @@ -10,4 +10,7 @@ class EntityDefs{ @EntityDef({TileComp.class}) class TileDef{} + + @EntityDef({EffectComp.class}) + class EffectDef{} } diff --git a/core/src/mindustry/entities/def/EntityGroupDefs.java b/core/src/mindustry/entities/def/EntityGroupDefs.java index 1a654e2eca..c49d1108c4 100644 --- a/core/src/mindustry/entities/def/EntityGroupDefs.java +++ b/core/src/mindustry/entities/def/EntityGroupDefs.java @@ -1,4 +1,22 @@ package mindustry.entities.def; +import mindustry.annotations.Annotations.*; +import mindustry.entities.def.EntityComps.*; + public class EntityGroupDefs{ + + @GroupDef(UnitComp.class) + void unit(){ + + } + + @GroupDef(TileComp.class) + void tile(){ + + } + + @GroupDef(DrawComp.class) + void drawers(){ + + } } diff --git a/core/src/mindustry/entities/effect/Lightning.java b/core/src/mindustry/entities/effect/Lightning.java index 05ae5bff73..33ed6dc42f 100644 --- a/core/src/mindustry/entities/effect/Lightning.java +++ b/core/src/mindustry/entities/effect/Lightning.java @@ -1,24 +1,24 @@ package mindustry.entities.effect; -import mindustry.annotations.Annotations.Loc; -import mindustry.annotations.Annotations.Remote; -import arc.struct.Array; -import arc.struct.IntSet; -import arc.graphics.Color; +import arc.graphics.*; import arc.graphics.g2d.*; import arc.math.*; import arc.math.geom.*; -import arc.util.pooling.Pools; -import mindustry.content.Bullets; -import mindustry.entities.EntityGroup; -import mindustry.entities.Units; -import mindustry.game.Team; -import mindustry.gen.Call; -import mindustry.graphics.Pal; -import mindustry.world.Tile; +import arc.struct.*; +import arc.util.pooling.*; +import mindustry.annotations.Annotations.*; +import mindustry.content.*; +import mindustry.entities.*; +import mindustry.entities.traits.*; +import mindustry.entities.type.*; +import mindustry.game.*; +import mindustry.gen.*; +import mindustry.graphics.*; +import mindustry.world.*; import static mindustry.Vars.*; +//TODO lightning should be an effect with custom logic + bullet with custom init logic public class Lightning extends TimedEntity implements DrawTrait, TimeTrait{ public static final float lifetime = 10f; diff --git a/core/src/mindustry/entities/type/Tilec.java b/core/src/mindustry/entities/type/Tilec.java deleted file mode 100644 index 63246c3d8e..0000000000 --- a/core/src/mindustry/entities/type/Tilec.java +++ /dev/null @@ -1,313 +0,0 @@ -package mindustry.entities.type; - -import arc.math.*; -import mindustry.annotations.Annotations.*; -import arc.Events; -import arc.struct.Array; -import arc.struct.ObjectSet; -import arc.math.geom.Point2; -import arc.util.*; -import arc.util.ArcAnnotate.*; -import mindustry.game.*; -import mindustry.game.EventType.BlockDestroyEvent; -import mindustry.gen.*; -import mindustry.world.*; -import mindustry.world.consumers.*; -import mindustry.world.modules.*; - -import java.io.*; - -import static mindustry.Vars.*; - -public class Tilec__{ - public static final float timeToSleep = 60f * 1; //1 second to fall asleep - private static final ObjectSet tmpTiles = new ObjectSet<>(); - /** This value is only used for debugging. */ - public static int sleepingEntities = 0; - - public Tile tile; - public Block block; - public Interval timer; - public float health; - public float timeScale = 1f, timeScaleDuration; - - public PowerModule power; - public ItemModule items; - public LiquidModule liquids; - public @Nullable ConsumeModule cons; - - /** List of (cached) tiles with entities in proximity, used for outputting to */ - private Array proximity = new Array<>(8); - private boolean dead = false; - private boolean sleeping; - private float sleepTime; - private @Nullable SoundLoop sound; - - @Remote(called = Loc.server, unreliable = true) - public static void onTileDamage(Tile tile, float health){ - if(tile.entity != null){ - tile.entity.health = health; - - if(tile.entity.damaged()){ - indexer.notifyTileDamaged(tile.entity); - } - } - } - - @Remote(called = Loc.server) - public static void onTileDestroyed(Tile tile){ - if(tile.entity == null) return; - tile.entity.onDeath(); - } - - /** Sets this tile entity data to this tile, and adds it if necessary. */ - public Tilec init(Tile tile, boolean shouldAdd){ - this.tile = tile; - x = tile.drawx(); - y = tile.drawy(); - block = tile.block(); - if(block.activeSound != Sounds.none){ - sound = new SoundLoop(block.activeSound, block.activeSoundVolume); - } - - health = block.health; - timer = new Interval(block.timers); - - if(shouldAdd){ - add(); - } - - return this; - } - - /** Scaled delta. */ - public float delta(){ - return Time.delta() * timeScale; - } - - /** Base efficiency. If this entity has non-buffered power, returns the power %, otherwise returns 1. */ - public float efficiency(){ - return power != null && (block.consumes.has(ConsumeType.power) && !block.consumes.getPower().buffered) ? power.status : 1f; - } - - /** Call when nothing is happening to the entity. This increments the internal sleep timer. */ - public void sleep(){ - sleepTime += Time.delta(); - if(!sleeping && sleepTime >= timeToSleep){ - remove(); - sleeping = true; - sleepingEntities++; - } - } - - /** Call when this entity is updating. This wakes it up. */ - public void noSleep(){ - sleepTime = 0f; - if(sleeping){ - add(); - sleeping = false; - sleepingEntities--; - } - } - - public boolean isDead(){ - return dead || tile.entity != this; - } - - @CallSuper - public void write(DataOutput stream) throws IOException{ - stream.writeShort((short)health); - stream.writeByte(Pack.byteByte((byte)8, tile.rotation())); //rotation + marker to indicate that team is moved (8 isn't valid) - stream.writeByte(tile.getTeamID()); - if(items != null) items.write(stream); - if(power != null) power.write(stream); - if(liquids != null) liquids.write(stream); - if(cons != null) cons.write(stream); - } - - @CallSuper - public void read(DataInput stream, byte revision) throws IOException{ - health = stream.readUnsignedShort(); - byte packedrot = stream.readByte(); - byte team = Pack.leftByte(packedrot) == 8 ? stream.readByte() : Pack.leftByte(packedrot); - byte rotation = Pack.rightByte(packedrot); - - tile.setTeam(Team.get(team)); - tile.rotation(rotation); - - if(items != null) items.read(stream); - if(power != null) power.read(stream); - if(liquids != null) liquids.read(stream); - if(cons != null) cons.read(stream); - } - - /** Returns the version of this TileEntity IO code.*/ - public byte version(){ - return 0; - } - - public boolean collide(Bulletc other){ - return true; - } - - public void collision(Bulletc other){ - block.handleBulletHit(this, other); - } - - public void kill(){ - Call.onTileDestroyed(tile); - } - - @Override - public void damage(float damage){ - if(dead) return; - - if(Mathf.zero(state.rules.blockHealthMultiplier)){ - damage = health + 1; - }else{ - damage /= state.rules.blockHealthMultiplier; - } - - float preHealth = health; - - Call.onTileDamage(tile, health - block.handleDamage(tile, damage)); - - if(health <= 0){ - Call.onTileDestroyed(tile); - } - - if(preHealth >= maxHealth() - 0.00001f && health < maxHealth() && world != null){ //when just damaged - indexer.notifyTileDamaged(this); - } - } - - public Tile getTile(){ - return tile; - } - - public void removeFromProximity(){ - block.onProximityRemoved(tile); - - Point2[] nearby = Edges.getEdges(block.size); - for(Point2 point : nearby){ - Tile other = world.ltile(tile.x + point.x, tile.y + point.y); - //remove this tile from all nearby tile's proximities - if(other != null){ - other.block().onProximityUpdate(other); - - if(other.entity != null){ - other.entity.proximity.removeValue(tile, true); - } - } - } - } - - public void updateProximity(){ - tmpTiles.clear(); - proximity.clear(); - - Point2[] nearby = Edges.getEdges(block.size); - for(Point2 point : nearby){ - Tile other = world.ltile(tile.x + point.x, tile.y + point.y); - - if(other == null) continue; - if(other.entity == null || !(other.interactable(tile.getTeam()))) continue; - - //add this tile to proximity of nearby tiles - if(!other.entity.proximity.contains(tile, true)){ - other.entity.proximity.add(tile); - } - - tmpTiles.add(other); - } - - //using a set to prevent duplicates - for(Tile tile : tmpTiles){ - proximity.add(tile); - } - - block.onProximityAdded(tile); - block.onProximityUpdate(tile); - - for(Tile other : tmpTiles){ - other.block().onProximityUpdate(other); - } - } - - public Array proximity(){ - return proximity; - } - - /** Tile configuration. Defaults to 0. Used for block rebuilding. */ - public int config(){ - return 0; - } - - @Override - public void removed(){ - if(sound != null){ - sound.stop(); - } - } - - @Override - public void onDeath(){ - if(!dead){ - dead = true; - - Events.fire(new BlockDestroyEvent(tile)); - block.breakSound.at(tile); - block.onDestroyed(tile); - tile.remove(); - remove(); - } - } - - @Override - public void update(){ - timeScaleDuration -= Time.delta(); - if(timeScaleDuration <= 0f || !block.canOverdrive){ - timeScale = 1f; - } - - if(health <= 0){ - onDeath(); - return; //no need to update anymore - } - - if(sound != null){ - sound.update(x, y, block.shouldActiveSound(tile)); - } - - if(block.idleSound != Sounds.none && block.shouldIdleSound(tile)){ - loops.play(block.idleSound, this, block.idleSoundVolume); - } - - block.update(tile); - - if(liquids != null){ - liquids.update(); - } - - if(cons != null){ - cons.update(); - } - - if(power != null){ - power.graph.update(); - } - } - - @Override - public boolean isValid(){ - return !isDead() && tile.entity == this; - } - - @Override - public String toString(){ - return "TileEntity{" + - "tile=" + tile + - ", health=" + health + - '}'; - } -} diff --git a/core/src/mindustry/world/Tile.java b/core/src/mindustry/world/Tile.java index a10baaa06a..528e7d7972 100644 --- a/core/src/mindustry/world/Tile.java +++ b/core/src/mindustry/world/Tile.java @@ -507,4 +507,21 @@ public class Tile implements Position{ public static void setTile(Tile tile, Block block, Team team, int rotation){ tile.set(block, team, rotation); } + + @Remote(called = Loc.server, unreliable = true) + public static void onTileDamage(Tile tile, float health){ + if(tile.entity != null){ + tile.entity.setHealth(health); + + if(tile.entity.damaged()){ + indexer.notifyTileDamaged(tile.entity); + } + } + } + + @Remote(called = Loc.server) + public static void onTileDestroyed(Tile tile){ + if(tile.entity == null) return; + tile.entity.killed(); + } } diff --git a/gradle.properties b/gradle.properties index 0c9f51a7cc..e54e317005 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ org.gradle.daemon=true org.gradle.jvmargs=-Xms256m -Xmx1024m -archash=5ce22291e48c3ac41db2a2c5056bf6f3584c7c8c +archash=1817bb22ac7680700fe780816940f4217a1f7e07