Allow markers to be used as light sources (#9733)

* Allow markers to be used as light sources

* Better light source markers

* Move PointMarker drawLight to LightMarker

* Add a rule for unit lights

* Optimize marker rendering

* Update core/assets/bundles/bundle.properties

---------

Co-authored-by: Anuken <arnukren@gmail.com>
This commit is contained in:
Redstonneur1256
2025-02-09 12:10:45 -05:00
committed by GitHub
co-authored by Anuken
parent 3378475f6a
commit 8626082068
12 changed files with 196 additions and 68 deletions
+2
View File
@@ -1438,8 +1438,10 @@ rules.title.unit = Units
rules.title.experimental = Experimental rules.title.experimental = Experimental
rules.title.environment = Environment rules.title.environment = Environment
rules.title.teams = Teams rules.title.teams = Teams
rules.title.light = Lighting
rules.title.planet = Planet rules.title.planet = Planet
rules.lighting = Lighting rules.lighting = Lighting
rules.lighting.unitlight = Unit Lighting
rules.fog = Fog of War rules.fog = Fog of War
rules.invasions = Enemy Sector Invasions rules.invasions = Enemy Sector Invasions
rules.legacylaunchpads = Legacy Launch Pad Mechanics rules.legacylaunchpads = Legacy Launch Pad Mechanics
+31 -17
View File
@@ -320,6 +320,37 @@ public class Renderer implements ApplicationListener{
} }
} }
//draw objective markers
float scaleFactor = 4f / renderer.getDisplayScale();
state.rules.objectives.eachRunning(obj -> {
for(var marker : obj.markers){
if(marker.world != -1){
marker.draw(marker.autoscale ? scaleFactor : 1);
}
}
});
for(var marker : state.markers.worldMarkers){
marker.draw(marker.autoscale ? scaleFactor : 1);
}
Draw.reset();
lights.add(() -> {
state.rules.objectives.eachRunning(obj -> {
for(var marker : obj.markers){
if(marker.light != -1){
marker.drawLight(marker.autoscale ? scaleFactor : 1);
}
}
});
for(var marker : state.markers.lightMarkers){
marker.drawLight(marker.autoscale ? scaleFactor : 1);
}
Draw.reset();
});
if(state.rules.lighting && drawLight){ if(state.rules.lighting && drawLight){
Draw.draw(Layer.light, lights::draw); Draw.draw(Layer.light, lights::draw);
} }
@@ -353,23 +384,6 @@ public class Renderer implements ApplicationListener{
}); });
} }
float scaleFactor = 4f / renderer.getDisplayScale();
//draw objective markers
state.rules.objectives.eachRunning(obj -> {
for(var marker : obj.markers){
if(marker.world){
marker.draw(marker.autoscale ? scaleFactor : 1);
}
}
});
for(var marker : state.markers){
if(marker.world){
marker.draw(marker.autoscale ? scaleFactor : 1);
}
}
Draw.reset(); Draw.reset();
Draw.draw(Layer.overlayUI, overlays::drawTop); Draw.draw(Layer.overlayUI, overlays::drawTop);
@@ -302,6 +302,11 @@ public class MapObjectivesDialog extends BaseDialog{
}).growX().fillY(); }).growX().fillY();
}); });
setInterpreter(IndexBool.class, int.class, (cont, name, type, field, remover, indexer, get, set) -> {
getInterpreter(Boolean.class).build(cont, name, type, field, remover, indexer, () -> get.get() != -1, v -> set.get(v ? +1 : -1));
});
// Special data structure interpreters. // Special data structure interpreters.
// Instantiate default `Seq`s with a reflectively allocated array. // Instantiate default `Seq`s with a reflectively allocated array.
setProvider(Seq.class, (type, cons) -> cons.get(new Seq<>(type.element.raw))); setProvider(Seq.class, (type, cons) -> cons.get(new Seq<>(type.element.raw)));
+69 -26
View File
@@ -1,42 +1,37 @@
package mindustry.game; package mindustry.game;
import arc.func.*;
import arc.struct.*; import arc.struct.*;
import arc.util.*; import arc.util.*;
import mindustry.game.MapObjectives.*; import mindustry.game.MapObjectives.*;
import mindustry.io.*; import mindustry.io.*;
import java.io.*; import java.io.*;
import java.util.*;
public class MapMarkers implements Iterable<ObjectiveMarker>{ public class MapMarkers{
/** Maps marker unique ID to marker. */ /** Maps marker unique ID to marker. */
private IntMap<ObjectiveMarker> map = new IntMap<>(); 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 Seq<ObjectiveMarker> worldMarkers = new Seq<>(false);
public Seq<ObjectiveMarker> mapMarkers = new Seq<>(false);
public Seq<ObjectiveMarker> lightMarkers = new Seq<>(false);
public void add(int id, ObjectiveMarker marker){ public void add(int id, ObjectiveMarker marker){
if(marker == null) return; if(marker == null) return;
var prev = map.put(id, marker); var prev = map.put(id, marker);
if(prev != null){
all.set(prev.arrayIndex, marker); setMarker(worldMarkers, marker, prev, m -> m.world, (m, i) -> m.world = i);
}else{ setMarker(mapMarkers, marker, prev, m -> m.minimap, (m, i) -> m.minimap = i);
all.add(marker); setMarker(lightMarkers, marker, prev, m -> m.light, (m, i) -> m.light = i);
marker.arrayIndex = all.size - 1;
}
} }
public void remove(int id){ public void remove(int id){
var prev = map.remove(id); var prev = map.remove(id);
if(prev != null){ if(prev != null){
if(all.size > prev.arrayIndex + 1){ //there needs to be something above the index to replace it with remove(worldMarkers, prev.world, (m, i) -> m.world = i);
all.remove(prev.arrayIndex); remove(mapMarkers, prev.minimap, (m, i) -> m.minimap = i);
//update its index remove(lightMarkers, prev.light, (m, i) -> m.light = i);
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);
}
} }
} }
@@ -49,7 +44,7 @@ public class MapMarkers implements Iterable<ObjectiveMarker>{
} }
public int size(){ public int size(){
return all.size; return map.size;
} }
public void write(DataOutput stream) throws IOException{ public void write(DataOutput stream) throws IOException{
@@ -57,16 +52,64 @@ public class MapMarkers implements Iterable<ObjectiveMarker>{
} }
public void read(DataInput stream) throws IOException{ public void read(DataInput stream) throws IOException{
all.clear(); worldMarkers.clear();
mapMarkers.clear();
lightMarkers.clear();
map = JsonIO.readBytes(IntMap.class, ObjectiveMarker.class, (DataInputStream)stream); map = JsonIO.readBytes(IntMap.class, ObjectiveMarker.class, (DataInputStream)stream);
for(var entry : map.entries()){ for(var entry : map.entries()){
all.add(entry.value); var marker = entry.value;
entry.value.arrayIndex = all.size - 1;
if(marker.world != -1) marker.world = worldMarkers.add(marker).size - 1;
if(marker.minimap != -1) marker.minimap = mapMarkers.add(marker).size - 1;
if(marker.light != -1) marker.light = lightMarkers.add(marker).size - 1;
} }
} }
@Override public interface MarkerSetter{
public Iterator<ObjectiveMarker> iterator(){ void set(ObjectiveMarker marker, int index);
return all.iterator();
} }
public void updateMarker(Seq<ObjectiveMarker> markers, ObjectiveMarker marker, boolean visible, Intf<ObjectiveMarker> getter, MarkerSetter setter){
if((getter.get(marker) != -1) == visible) return; // nothing to change
if(!visible){
setter.set(markers.peek(), getter.get(marker));
markers.remove(getter.get(marker));
setter.set(marker, -1);
}else{
setter.set(marker, markers.size);
markers.add(marker);
}
}
private void setMarker(Seq<ObjectiveMarker> markers, ObjectiveMarker curr, ObjectiveMarker prev, Intf<ObjectiveMarker> getter, MarkerSetter setter){
int currIndex = getter.get(curr);
if(prev != null && getter.get(prev) != -1){
int prevIndex = getter.get(prev);
if(currIndex != -1){
// both markers visible, replace previous with current
setter.set(curr, prevIndex);
markers.set(prevIndex, curr);
}else{
// previous marker visible but not current
setter.set(markers.peek(), prevIndex);
markers.remove(prevIndex);
}
}else{
if(currIndex != -1){
setter.set(curr, markers.size);
markers.add(curr);
}
}
}
private void remove(Seq<ObjectiveMarker> markers, int index, MarkerSetter setter){
if(index != -1){
setter.set(markers.peek(), index);
markers.remove(index);
}
}
} }
+73 -13
View File
@@ -65,7 +65,8 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
TextMarker::new, TextMarker::new,
LineMarker::new, LineMarker::new,
TextureMarker::new, TextureMarker::new,
QuadMarker::new QuadMarker::new,
LightMarker::new
); );
registerLegacyMarker("Minimap", PointMarker::new); registerLegacyMarker("Minimap", PointMarker::new);
@@ -620,13 +621,12 @@ 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. */ /** Marker used for drawing various content to indicate something along with an objective. Mostly used as UI overlay. */
public static abstract class ObjectiveMarker{ public static abstract class ObjectiveMarker{
/** Internal use only! Do not access. */ /** Whether to display marker in the world. Do not modify directly if added, use control() instead. */
public transient int arrayIndex; public @IndexBool int world = 1;
/** Whether to display marker on the minimap. Do not modify directly if added, use control() instead. */
/** Whether to display marker in the world. */ public @IndexBool int minimap = -1;
public boolean world = true; /** Whether to use the marker as light. Do not modify directly if added, use control() instead. */
/** Whether to display marker on minimap. */ public @IndexBool int light = -1;
public boolean minimap = false;
/** Whether to scale marker corresponding to player's zoom level. */ /** Whether to scale marker corresponding to player's zoom level. */
public boolean autoscale = false; public boolean autoscale = false;
/** On which z-sorting layer is marker drawn. */ /** On which z-sorting layer is marker drawn. */
@@ -634,13 +634,18 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
public void draw(float scaleFactor){} public void draw(float scaleFactor){}
public void drawLight(float scaleFactor){
draw(scaleFactor);
}
/** Control marker with world processor code. Ignores NaN (null) values. */ /** Control marker with world processor code. Ignores NaN (null) values. */
public void control(LMarkerControl type, double p1, double p2, double p3){ public void control(LMarkerControl type, double p1, double p2, double p3){
if(Double.isNaN(p1)) return; if(Double.isNaN(p1)) return;
switch(type){ switch(type){
case world -> world = !Mathf.equal((float)p1, 0f); case world -> state.markers.updateMarker(state.markers.worldMarkers, this, !Mathf.equal((float)p1, 0f), m -> m.world, (m, i) -> m.world = i);
case minimap -> minimap = !Mathf.equal((float)p1, 0f); case minimap -> state.markers.updateMarker(state.markers.mapMarkers, this, !Mathf.equal((float)p1, 0f), m -> m.minimap, (m, i) -> m.minimap = i);
case light -> state.markers.updateMarker(state.markers.lightMarkers, this, !Mathf.equal((float)p1, 0f), m -> m.light, (m, i) -> m.light = i);
case autoscale -> autoscale = !Mathf.equal((float)p1, 0f); case autoscale -> autoscale = !Mathf.equal((float)p1, 0f);
case drawLayer -> drawLayer = (float)p1; case drawLayer -> drawLayer = (float)p1;
} }
@@ -842,8 +847,13 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
Draw.z(drawLayer); Draw.z(drawLayer);
Lines.stroke(Scl.scl((1f - fin) * stroke + 0.1f), color); Lines.stroke(Scl.scl((1f - fin) * stroke + 0.1f), color);
Lines.circle(pos.x, pos.y, rad * fin); Lines.circle(pos.x, pos.y, rad * fin);
}
Draw.reset(); @Override
public void drawLight(float scaleFactor){
float rad = radius * tilesize * scaleFactor;
renderer.lights.add(pos.x, pos.y, radius, color, color.a);
} }
@Override @Override
@@ -901,8 +911,6 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
Fill.arc(pos.x, pos.y, radius * scaleFactor, (startAngle - endAngle) / 360f, rotation + endAngle, sides); Fill.arc(pos.x, pos.y, radius * scaleFactor, (startAngle - endAngle) / 360f, rotation + endAngle, sides);
} }
} }
Draw.reset();
} }
@Override @Override
@@ -1043,6 +1051,11 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
Lines.line(pos.x, pos.y, color1, endPos.x, endPos.y, color2); Lines.line(pos.x, pos.y, color1, endPos.x, endPos.y, color2);
} }
@Override
public void drawLight(float scaleFactor){
renderer.lights.line(pos.x, pos.y, endPos.x, endPos.y, stroke, color1, color1.a);
}
@Override @Override
public void control(LMarkerControl type, double p1, double p2, double p3){ public void control(LMarkerControl type, double p1, double p2, double p3){
super.control(type, p1, p2, p3); super.control(type, p1, p2, p3);
@@ -1247,6 +1260,48 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
} }
/** Displays a single point light. */
public static class LightMarker extends PosMarker{
public float radius = 5f;
public Color color = Color.valueOf("ffd37f");
public LightMarker(int x, int y){
this.pos.set(x, y);
}
public LightMarker(int x, int y, Color color){
this.pos.set(x, y);
this.color = color;
}
public LightMarker(int x, int y, float radius, Color color){
this.pos.set(x, y);
this.radius = radius;
this.color = color;
}
public LightMarker(){}
@Override
public void drawLight(float scaleFactor){
float rad = radius * tilesize * scaleFactor;
renderer.lights.add(pos.x, pos.y, radius, color, color.a);
}
@Override
public void control(LMarkerControl type, double p1, double p2, double p3){
super.control(type, p1, p2, p3);
if(!Double.isNaN(p1)){
switch(type){
case radius -> radius = (float)p1;
case color -> color.fromDouble(p1);
}
}
}
}
private static void lookupRegion(String name, TextureRegion out){ private static void lookupRegion(String name, TextureRegion out){
TextureRegion region = Core.atlas.find(name); TextureRegion region = Core.atlas.find(name);
if(region.found()){ if(region.found()){
@@ -1275,6 +1330,11 @@ public class MapObjectives implements Iterable<MapObjective>, Eachable<MapObject
@Retention(RUNTIME) @Retention(RUNTIME)
public @interface Vertices{} public @interface Vertices{}
/** For {@code int}; treats it as a boolean with -1 for false and any other value for true (defaulting to 1) */
@Target(FIELD)
@Retention(RUNTIME)
public @interface IndexBool{}
/** For {@code byte}; treats it as a world label flag. */ /** For {@code byte}; treats it as a world label flag. */
@Target(FIELD) @Target(FIELD)
@Retention(RUNTIME) @Retention(RUNTIME)
+2
View File
@@ -180,6 +180,8 @@ public class Rules{
public boolean lighting = false; public boolean lighting = false;
/** Ambient light color, used when lighting is enabled. */ /** Ambient light color, used when lighting is enabled. */
public Color ambientLight = new Color(0.01f, 0.01f, 0.04f, 0.99f); public Color ambientLight = new Color(0.01f, 0.01f, 0.04f, 0.99f);
/** Whether units produce light when lighting is enabled. */
public boolean unitLight = true;
/** team of the player by default. */ /** team of the player by default. */
public Team defaultTeam = Team.sharded; public Team defaultTeam = Team.sharded;
/** team of the enemy in waves/sectors. */ /** team of the enemy in waves/sectors. */
@@ -262,17 +262,15 @@ public class MinimapRenderer{
//TODO autoscale markers //TODO autoscale markers
state.rules.objectives.eachRunning(obj -> { state.rules.objectives.eachRunning(obj -> {
for(var marker : obj.markers){ for(var marker : obj.markers){
if(marker.minimap){ if(marker.minimap != -1){
marker.draw(1); marker.draw(1);
} }
} }
}); });
for(var marker : state.markers.mapMarkers){
for(var marker : state.markers){ marker.draw(1);
if(marker.minimap){
marker.draw(1);
}
} }
Draw.reset();
Draw.trans(Tmp.m2); Draw.trans(Tmp.m2);
} }
+1
View File
@@ -1548,6 +1548,7 @@ public class LExecutor{
} }
} }
case ambientLight -> state.rules.ambientLight.fromDouble(value.num()); case ambientLight -> state.rules.ambientLight.fromDouble(value.num());
case unitLight -> state.rules.unitLight = value.bool();
case solarMultiplier -> state.rules.solarMultiplier = Math.max(value.numf(), 0f); case solarMultiplier -> state.rules.solarMultiplier = Math.max(value.numf(), 0f);
case dragMultiplier -> state.rules.dragMultiplier = Math.max(value.numf(), 0f); case dragMultiplier -> state.rules.dragMultiplier = Math.max(value.numf(), 0f);
case ban -> { case ban -> {
@@ -4,6 +4,7 @@ public enum LMarkerControl{
remove, remove,
world("true/false"), world("true/false"),
minimap("true/false"), minimap("true/false"),
light("true/false"),
autoscale("true/false"), autoscale("true/false"),
pos("x", "y"), pos("x", "y"),
endPos("x", "y"), endPos("x", "y"),
+1
View File
@@ -15,6 +15,7 @@ public enum LogicRule{
lighting, lighting,
canGameOver, canGameOver,
ambientLight, ambientLight,
unitLight,
solarMultiplier, solarMultiplier,
dragMultiplier, dragMultiplier,
ban, ban,
+1 -1
View File
@@ -1547,7 +1547,7 @@ public class UnitType extends UnlockableContent implements Senseable{
} }
public void drawLight(Unit unit){ public void drawLight(Unit unit){
if(lightRadius > 0){ if(lightRadius > 0 && state.rules.unitLight){
Drawf.light(unit.x, unit.y, lightRadius, lightColor, lightOpacity); Drawf.light(unit.x, unit.y, lightRadius, lightColor, lightOpacity);
} }
} }
@@ -221,6 +221,11 @@ public class CustomRulesDialog extends BaseDialog{
number("@rules.solarmultiplier", f -> rules.solarMultiplier = f, () -> rules.solarMultiplier); number("@rules.solarmultiplier", f -> rules.solarMultiplier = f, () -> rules.solarMultiplier);
if(Core.bundle.get("rules.weather").toLowerCase().contains(ruleSearch)){
current.button("@rules.weather", this::weatherDialog).width(250f).left().row();
}
category("light");
if(Core.bundle.get("rules.ambientlight").toLowerCase().contains(ruleSearch)){ if(Core.bundle.get("rules.ambientlight").toLowerCase().contains(ruleSearch)){
current.button(b -> { current.button(b -> {
b.left(); b.left();
@@ -232,11 +237,7 @@ public class CustomRulesDialog extends BaseDialog{
b.add("@rules.ambientlight"); b.add("@rules.ambientlight");
}, () -> ui.picker.show(rules.ambientLight, rules.ambientLight::set)).left().width(250f).row(); }, () -> ui.picker.show(rules.ambientLight, rules.ambientLight::set)).left().width(250f).row();
} }
check("@rules.lighting.unitlight", b -> rules.unitLight = b, () -> rules.unitLight);
if(Core.bundle.get("rules.weather").toLowerCase().contains(ruleSearch)){
current.button("@rules.weather", this::weatherDialog).width(250f).left().row();
}
category("planet"); category("planet");
if(Core.bundle.get("rules.title.planet").toLowerCase().contains(ruleSearch)){ if(Core.bundle.get("rules.title.planet").toLowerCase().contains(ruleSearch)){