Objective marker cleanup & optimization

This commit is contained in:
Anuken 2024-01-14 12:41:20 -05:00
parent 84add050a9
commit 1d894bdacd
9 changed files with 161 additions and 49 deletions

View file

@ -2326,7 +2326,7 @@ lst.setprop = Sets a property of a unit or building.
lst.effect = Create a particle effect.
lst.sync = Sync a variable across the network.\nLimited to 20 times a second per variable.
lst.makemarker = Create a new logic marker in the world.\nAn ID to identify this marker must be provided.\nMarkers currently limited to 20,000 per world.
lst.setmarker = Set a property for a marker.\nThe ID used must be the same as in the Make Marker instruction.\n[accent]null []values are being ignored instead of converting to 0.
lst.setmarker = Set a property for a marker.\nThe ID used must be the same as in the Make Marker instruction.\n[accent]null []values are ignored.
lst.localeprint = Add map locale property value to the text buffer.\nTo set map locale bundles in map editor, check [accent]Map Info > Locale Bundles[].\nIf client is a mobile device, tries to print a property ending in ".mobile" first.
logic.nounitbuild = [red]Unit building logic is not allowed here.

View file

@ -1,11 +1,9 @@
package mindustry.core;
import arc.*;
import arc.struct.*;
import arc.util.*;
import mindustry.game.EventType.*;
import mindustry.game.*;
import mindustry.game.MapObjectives.*;
import mindustry.gen.*;
import mindustry.maps.*;
import mindustry.type.*;
@ -35,7 +33,7 @@ public class GameState{
/** Statistics for this save/game. Displayed after game over. */
public GameStats stats = new GameStats();
/** Markers not linked to objectives. Controlled by world processors. */
public IntMap<ObjectiveMarker> markers = new IntMap<>();
public MapMarkers markers = new MapMarkers();
/** Locale-specific string bundles of current map */
public MapLocales mapLocales = new MapLocales();
/** Global attributes of the environment, calculated by weather. */

View file

@ -375,13 +375,15 @@ public class Renderer implements ApplicationListener{
//draw objective markers
state.rules.objectives.eachRunning(obj -> {
for(var marker : obj.markers){
if(!marker.minimap) marker.drawWorld();
if(!marker.minimap){
marker.drawWorld();
}
}
});
for(var marker : state.markers.values()){
if(marker != null){
if(!marker.isHidden() && !marker.minimap) marker.drawWorld();
for(var marker : state.markers){
if(!marker.isHidden() && !marker.minimap){
marker.drawWorld();
}
}

View file

@ -0,0 +1,72 @@
package mindustry.game;
import arc.struct.*;
import arc.util.*;
import mindustry.game.MapObjectives.*;
import mindustry.io.*;
import java.io.*;
import java.util.*;
public class MapMarkers implements Iterable<ObjectiveMarker>{
/** Maps marker unique ID to marker. */
private IntMap<ObjectiveMarker> map = new IntMap<>();
/** Sequential list of markers. This allows for faster iteration than a map. */
private Seq<ObjectiveMarker> all = new Seq<>(false);
public void add(int id, ObjectiveMarker marker){
if(marker == null) return;
var prev = map.put(id, marker);
if(prev != null){
all.set(prev.arrayIndex, marker);
}else{
all.add(marker);
marker.arrayIndex = all.size - 1;
}
}
public void remove(int id){
var prev = map.remove(id);
if(prev != null){
if(all.size > prev.arrayIndex + 1){ //there needs to be something above the index to replace it with
all.remove(prev.arrayIndex);
//update its index
all.get(prev.arrayIndex).arrayIndex = prev.arrayIndex;
}else{
//no sense updating the index of the replaced element when it was not replaced
all.remove(prev.arrayIndex);
}
}
}
public @Nullable ObjectiveMarker get(int id){
return map.get(id);
}
public boolean has(int id){
return get(id) != null;
}
public int size(){
return all.size;
}
public void write(DataOutput stream) throws IOException{
JsonIO.writeBytes(map, ObjectiveMarker.class, (DataOutputStream)stream);
}
public void read(DataInput stream) throws IOException{
all.clear();
map = JsonIO.readBytes(IntMap.class, ObjectiveMarker.class, (DataInputStream)stream);
for(var entry : map.entries()){
all.add(entry.value);
entry.value.arrayIndex = all.size - 1;
}
}
@Override
public Iterator<ObjectiveMarker> iterator(){
return all.iterator();
}
}

View file

@ -627,10 +627,11 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
/** Marker used for drawing various content to indicate something along with an objective. Mostly used as UI overlay. */
public static abstract class ObjectiveMarker{
/** Position of marker, in world coordinates */
public @TilePos Vec2 pos = new Vec2();
/** Makes sure markers are only added once. */
public transient boolean wasAdded;
/** Internal use only! Do not access. */
public transient int arrayIndex;
/** Whether to display marker on minimap instead of world. {@link MinimapMarker} ignores this value. */
public boolean minimap = false;
/** Whether to scale marker corresponding to player's zoom level. {@link MinimapMarker} ignores this value. */
@ -639,28 +640,31 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
protected boolean hidden = false;
/** On which z-sorting layer is marker drawn. */
protected float drawLayer = Layer.overlayUI;
/** Draws the marker. Actual marker position and scale are calculated in {@link #drawWorld()} and {@link #drawMinimap(MinimapRenderer)}. */
public void baseDraw(float x, float y, float scaleFactor){}
/** Called in the main renderer. */
public void drawWorld(){
baseDraw(pos.x, pos.y, autoscale ? 4f / renderer.getDisplayScale() : 1f);
}
public void drawWorld(){}
/** Called in the small and large map. */
public void drawMinimap(MinimapRenderer minimap){
minimap.transform(Tmp.v1.set(pos.x + 4f, pos.y + 4f));
baseDraw(Tmp.v1.x, Tmp.v1.y, minimap.getScaleFactor(autoscale));
}
public void drawMinimap(MinimapRenderer minimap){}
/** Add any UI elements necessary. */
public void added(){}
/** Remove any UI elements, if necessary. */
public void removed(){}
/** Whether the marker is hidden */
public boolean isHidden(){
return hidden;
}
/** Control marker with world processor code. Ignores NaN (null) values. */
public void control(LMarkerControl type, double p1, double p2, double p3){
if(Double.isNaN(p1)) return;
switch(type){
case visibility -> hidden = Mathf.equal((float)p1, 0f);
case drawLayer -> drawLayer = (float)p1;
@ -668,6 +672,7 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
case autoscale -> autoscale = !Mathf.equal((float)p1, 0f);
}
}
public void setText(String text, boolean fetch){}
public void setTexture(String textureName){}
@ -699,8 +704,46 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
}
}
/** A marker that has a position in the world in world coordinates. */
public static abstract class PosMarker extends ObjectiveMarker{
/** Position of marker, in world coordinates */
public @TilePos Vec2 pos = new Vec2();
/** Called in the main renderer. */
@Override
public void drawWorld(){
baseDraw(pos.x, pos.y, autoscale ? 4f / renderer.getDisplayScale() : 1f);
}
/** Called in the small and large map. */
@Override
public void drawMinimap(MinimapRenderer minimap){
minimap.transform(Tmp.v1.set(pos.x + 4f, pos.y + 4f));
baseDraw(Tmp.v1.x, Tmp.v1.y, minimap.getScaleFactor(autoscale));
}
@Override
public void control(LMarkerControl type, double p1, double p2, double p3){
if(!Double.isNaN(p1)){
if(type == LMarkerControl.pos){
pos.x = (float)p1 * tilesize;
}else{
super.control(type, p1, p2, p3);
}
}
if(!Double.isNaN(p2)){
if(type == LMarkerControl.pos){
pos.y = (float)p2 * tilesize;
}else{
super.control(type, p1, p2, p3);
}
}
}
}
/** Displays text above a shape. */
public static class ShapeTextMarker extends ObjectiveMarker{
public static class ShapeTextMarker extends PosMarker{
public @Multiline String text = "frog";
public float fontSize = 1f, textHeight = 7f;
public @LabelFlag byte flags = WorldLabel.flagBackground | WorldLabel.flagOutline;
@ -743,7 +786,7 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
@Override
public void baseDraw(float x, float y, float scaleFactor){
//in case some idiot decides to make 9999999 sides and freeze the game
int sides = Math.min(this.sides, 200);
int sides = Math.min(this.sides, 300);
Draw.z(drawLayer);
Lines.stroke(3f * scaleFactor, Pal.gray);
@ -766,7 +809,6 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
public void control(LMarkerControl type, double p1, double p2, double p3){
if(!Double.isNaN(p1)){
switch(type){
case pos -> pos.x = (float)p1 * tilesize;
case fontSize -> fontSize = (float)p1;
case textHeight -> textHeight = (float)p1;
case labelFlags -> {
@ -786,7 +828,6 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
if(!Double.isNaN(p2)){
switch(type){
case pos -> pos.y = (float)p2 * tilesize;
case labelFlags -> {
if(!Mathf.equal((float)p2, 0f)){
flags |= WorldLabel.flagOutline;
@ -883,7 +924,7 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
}
/** Displays a shape with an outline and color. */
public static class ShapeMarker extends ObjectiveMarker{
public static class ShapeMarker extends PosMarker{
public float radius = 8f, rotation = 0f, stroke = 1f;
public boolean fill = false, outline = true;
public int sides = 4;
@ -927,7 +968,6 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
public void control(LMarkerControl type, double p1, double p2, double p3){
if(!Double.isNaN(p1)){
switch(type){
case pos -> pos.x = (float)p1 * tilesize;
case radius -> radius = (float)p1;
case stroke -> stroke = (float)p1;
case rotation -> rotation = (float)p1;
@ -939,7 +979,6 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
if(!Double.isNaN(p2)){
switch(type){
case pos -> pos.y = (float)p2 * tilesize;
case shape -> fill = !Mathf.equal((float)p2, 0f);
default -> super.control(type, p1, p2, p3);
}
@ -956,7 +995,7 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
}
/** Displays text at a location. */
public static class TextMarker extends ObjectiveMarker{
public static class TextMarker extends PosMarker{
public @Multiline String text = "uwu";
public float fontSize = 1f;
public @LabelFlag byte flags = WorldLabel.flagBackground | WorldLabel.flagOutline;
@ -993,7 +1032,6 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
public void control(LMarkerControl type, double p1, double p2, double p3){
if(!Double.isNaN(p1)){
switch(type){
case pos -> pos.x = (float)p1 * tilesize;
case fontSize -> fontSize = (float)p1;
case labelFlags -> {
if(!Mathf.equal((float)p1, 0f)){
@ -1008,7 +1046,6 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
if(!Double.isNaN(p2)){
switch(type){
case pos -> pos.y = (float)p2 * tilesize;
case labelFlags -> {
if(!Mathf.equal((float)p2, 0f)){
flags |= WorldLabel.flagOutline;
@ -1033,8 +1070,8 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
}
/** Displays a line from pos1 to pos2. */
public static class LineMarker extends ObjectiveMarker{
public @TilePos Vec2 pos = new Vec2(), endPos = new Vec2();
public static class LineMarker extends PosMarker{
public @TilePos Vec2 endPos = new Vec2();
public float stroke = 1f;
public boolean outline = true;
public Color color = Color.valueOf("ffd37f");
@ -1079,7 +1116,6 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
public void control(LMarkerControl type, double p1, double p2, double p3){
if(!Double.isNaN(p1)){
switch(type){
case pos -> pos.x = (float)p1 * tilesize;
case endPos -> endPos.x = (float)p1 * tilesize;
case stroke -> stroke = (float)p1;
case color -> color.set(Tmp.c1.fromDouble(p1));
@ -1089,7 +1125,6 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
if(!Double.isNaN(p2)){
switch(type){
case pos -> pos.y = (float)p2 * tilesize;
case endPos -> endPos.y = (float)p2 * tilesize;
default -> super.control(type, p1, p2, p3);
}
@ -1098,7 +1133,7 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
}
/** Displays a texture with specified name. */
public static class TextureMarker extends ObjectiveMarker{
public static class TextureMarker extends PosMarker{
public float rotation = 0f, width = 0f, height = 0f; // Zero width/height scales marker to original texture's size
public String textureName = "";
public Color color = Color.white.cpy();
@ -1123,7 +1158,6 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
public void control(LMarkerControl type, double p1, double p2, double p3){
if(!Double.isNaN(p1)){
switch(type){
case pos -> pos.x = (float)p1 * tilesize;
case rotation -> rotation = (float)p1;
case textureSize -> width = (float)p1 * tilesize;
case color -> color.set(Tmp.c1.fromDouble(p1));
@ -1133,7 +1167,6 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
if(!Double.isNaN(p2)){
switch(type){
case pos -> pos.y = (float)p2 * tilesize;
case textureSize -> height = (float)p2 * tilesize;
default -> super.control(type, p1, p2, p3);
}

View file

@ -255,13 +255,15 @@ public class MinimapRenderer{
state.rules.objectives.eachRunning(obj -> {
for(var marker : obj.markers){
if(marker.minimap) marker.drawMinimap(this);
if(marker.minimap){
marker.drawMinimap(this);
}
}
});
for(var marker : state.markers.values()){
if(marker != null){
if(!marker.isHidden() && marker.minimap) marker.drawMinimap(this);
for(var marker : state.markers){
if(!marker.isHidden() && marker.minimap){
marker.drawMinimap(this);
}
}
}

View file

@ -242,10 +242,17 @@ public class Shaders{
@Override
public void apply(){
setUniformf("u_progress", progress);
setUniformf("u_uv", region.u, region.v);
setUniformf("u_uv2", region.u2, region.v2);
setUniformf("u_time", time);
setUniformf("u_texsize", region.texture.width, region.texture.height);
if(region.texture == null){
setUniformf("u_uv", 0f, 0f);
setUniformf("u_uv2", 1f, 1f);
setUniformf("u_texsize", 1, 1);
}else{
setUniformf("u_uv", region.u, region.v);
setUniformf("u_uv2", region.u2, region.v2);
setUniformf("u_texsize", region.texture.width, region.texture.height);
}
}
}

View file

@ -6,14 +6,12 @@ import arc.math.geom.*;
import arc.struct.*;
import arc.util.*;
import arc.util.io.*;
import mindustry.*;
import mindustry.content.*;
import mindustry.content.TechTree.*;
import mindustry.core.*;
import mindustry.ctype.*;
import mindustry.entities.*;
import mindustry.game.*;
import mindustry.game.MapObjectives.*;
import mindustry.game.Teams.*;
import mindustry.gen.*;
import mindustry.maps.Map;
@ -403,11 +401,11 @@ public abstract class SaveVersion extends SaveFileReader{
}
public void writeMarkers(DataOutput stream) throws IOException{
JsonIO.writeBytes(Vars.state.markers, ObjectiveMarker.class, (DataOutputStream)stream);
state.markers.write(stream);
}
public void readMarkers(DataInput stream) throws IOException{
Vars.state.markers = JsonIO.readBytes(IntMap.class, ObjectiveMarker.class, (DataInputStream)stream);
state.markers.read(stream);
}
public void readTeamBlocks(DataInput stream) throws IOException{

View file

@ -2033,12 +2033,12 @@ public class LExecutor{
public void run(LExecutor exec){
var cons = MapObjectives.markerNameToType.get(type);
if(cons != null && state.markers.size < maxMarkers){
if(cons != null && state.markers.size() < maxMarkers){
int mid = exec.numi(id);
if(exec.bool(replace) || !state.markers.containsKey(mid)){
if(exec.bool(replace) || !state.markers.has(mid)){
var marker = cons.get();
marker.control(LMarkerControl.pos, exec.num(x), exec.num(y), 0);
state.markers.put(mid, marker);
state.markers.add(mid, marker);
}
}
}
@ -2046,7 +2046,7 @@ public class LExecutor{
@Remote(called = Loc.server, variants = Variant.both, unreliable = true)
public static void createMarker(int id, ObjectiveMarker marker){
state.markers.put(id, marker);
state.markers.add(id, marker);
}
@Remote(called = Loc.server, variants = Variant.both, unreliable = true)