Skip to content

trainmap section theme #8345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: mc1.20.1/dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
import com.simibubi.create.content.trains.graph.TrackGraph;
import com.simibubi.create.content.trains.graph.TrackNode;
import com.simibubi.create.content.trains.graph.TrackNodeLocation;
import com.simibubi.create.content.trains.signal.EdgeGroupColor;
import com.simibubi.create.content.trains.signal.SignalEdgeGroup;
import com.simibubi.create.content.trains.station.GlobalStation;
import com.simibubi.create.content.trains.track.BezierConnection;
import com.simibubi.create.content.trains.track.BezierConnection.BezierPixel;
import com.simibubi.create.foundation.gui.AllGuiTextures;
import com.simibubi.create.foundation.utility.CreateLang;
import com.simibubi.create.infrastructure.config.AllConfigs;
Expand All @@ -33,6 +36,7 @@
import net.createmod.catnip.data.Couple;
import net.createmod.catnip.data.Iterate;
import net.createmod.catnip.data.Pair;
import net.createmod.catnip.theme.Color;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.Rect2i;
Expand Down Expand Up @@ -442,6 +446,14 @@ else if (rotation == 2 || rotation == 6) {
return hoveredElement;
}

public static EdgeGroupColor getEdgeGroupColor(TrackGraph graph, TrackEdge edge, double position) {
UUID groupId = edge.getEdgeData().getGroupAtPosition(graph, position);
if (groupId == null) return EdgeGroupColor.WHITE;
SignalEdgeGroup edgeGroup = CreateClient.RAILWAYS.signalEdgeGroups.get(groupId);
if (edgeGroup == null) return EdgeGroupColor.WHITE;
return edgeGroup.color;
}

// Background first so we can mindlessly paint over it
static final int PHASE_BACKGROUND = 0;
// Straights before curves so that curves anti-alias properly at the transition
Expand Down Expand Up @@ -491,6 +503,8 @@ private static void renderPhase(TrainMapRenderer map, List<Couple<Integer>> coll
int portalFrameColor = 0xFF_4C2D5B;
int portalColor = 0xFF_FF7FD6;

boolean isSectionTheme = map.trackingTheme == CClient.TrainMapTheme.SECTION;

for (TrackGraph graph : CreateClient.RAILWAYS.trackNetworks.values()) {
for (TrackNodeLocation nodeLocation : graph.getNodes()) {
if (nodeLocation.dimension != map.trackingDim)
Expand Down Expand Up @@ -587,6 +601,12 @@ private static void renderPhase(TrainMapRenderer map, List<Couple<Integer>> coll
continue;
}

if (isSectionTheme) {
EdgeGroupColor groupColor = getEdgeGroupColor(graph, edge, s * Mth.SQRT_OF_TWO);
mainColor = groupColor.getBGR();
darkerColor = groupColor.getDarkerBGR();
}

int alphaAt = map.alphaAt(x, z);
if (alphaAt > 0 && alphaAt != a)
collisions.add(Couple.create(x, z));
Expand Down Expand Up @@ -626,6 +646,13 @@ private static void renderPhase(TrainMapRenderer map, List<Couple<Integer>> coll
int alphaAt = map.alphaAt(x, z);
if (alphaAt > 0 && alphaAt != a)
collisions.add(Couple.create(x, z));

if (isSectionTheme) {
EdgeGroupColor groupColor = getEdgeGroupColor(graph, edge, s);
mainColor = groupColor.getBGR();
darkerColor = groupColor.getDarkerBGR();
}

if (alphaAt <= a) {
map.setPixel(x, z, markY(mainColor, y));
}
Expand All @@ -641,13 +668,13 @@ private static void renderPhase(TrainMapRenderer map, List<Couple<Integer>> coll
continue;

BlockPos origin = turn.bePositions.getFirst();
Map<Pair<Integer, Integer>, Double> rasterise = turn.rasterise();
Map<Pair<Integer, Integer>, BezierPixel> rasterise = turn.rasterise();

for (boolean antialias : Iterate.falseAndTrue) {
for (Entry<Pair<Integer, Integer>, Double> offset : rasterise.entrySet()) {
Pair<Integer, Integer> xz = offset.getKey();
for (Entry<Pair<Integer, Integer>, BezierPixel> bcPixel : rasterise.entrySet()) {
Pair<Integer, Integer> xz = bcPixel.getKey();
int x = origin.getX() + xz.getFirst();
int y = Mth.floor(origin.getY() + offset.getValue() + 0.5);
int y = Mth.floor(origin.getY() + bcPixel.getValue().yLevel + 0.5);
int z = origin.getZ() + xz.getSecond();

if (phase == PHASE_BACKGROUND) {
Expand All @@ -656,6 +683,12 @@ private static void renderPhase(TrainMapRenderer map, List<Couple<Integer>> coll
continue;
}

if (isSectionTheme) {
EdgeGroupColor groupColor = getEdgeGroupColor(graph, edge, bcPixel.getValue().position);
mainColor = groupColor.getBGR();
darkerColor = groupColor.getDarkerBGR();
}

int a = mapYtoAlpha(y);

if (!antialias) {
Expand All @@ -669,12 +702,23 @@ private static void renderPhase(TrainMapRenderer map, List<Couple<Integer>> coll
continue;
}

boolean mainColorBelowLeft =
map.is(x + 1, z + 1, mainColor) && Math.abs(map.alphaAt(x + 1, z + 1) - a) <= 1;
boolean mainColorBelowRight =
map.is(x - 1, z + 1, mainColor) && Math.abs(map.alphaAt(x - 1, z + 1) - a) <= 1;
boolean mainColorBelow = false;
for (int xDelta : Iterate.positiveAndNegative) {
if (Math.abs(map.alphaAt(x + xDelta, z + 1) - a) > 1) {
continue;
}

int colorBelow = map.getPixel(x + xDelta, z + 1) & 0xFFFFFF;
mainColorBelow |= colorBelow == (mainColor & 0xFFFFFF);

if (isSectionTheme) {
for (EdgeGroupColor groupColor : EdgeGroupColor.values()) {
mainColorBelow |= colorBelow == (groupColor.getBGR() & 0xFFFFFF);
}
}
}

if (mainColorBelowLeft || mainColorBelowRight) {
if (mainColorBelow) {
int alphaAt = map.alphaAt(x, z + 1);
if (alphaAt > 0 && alphaAt != a)
collisions.add(Couple.create(x, z));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package com.simibubi.create.content.trains.signal;

import net.createmod.catnip.theme.Color;
import net.minecraft.util.FastColor;

public enum EdgeGroupColor {

YELLOW(0xEBC255),
GREEN(0x51C054),
BLUE(0x5391E1),
ORANGE(0xE36E36),
LAVENDER(0xCB92BA),
RED(0xA43538),
CYAN(0x6EDAD9),
BROWN(0xA17C58),
YELLOW(0xEBC255, 0xAD7F4D),
GREEN(0x51C054, 0x538E47),
BLUE(0x5391E1, 0x41717A),
ORANGE(0xE36E36, 0xA54436),
LAVENDER(0xCB92BA, 0x9A7697),
RED(0xA43538, 0x783141),
CYAN(0x6EDAD9, 0x78574A),
BROWN(0xA17C58, 0x78574A),

WHITE(0xE5E1DC);
WHITE(0xE5E1DC, 0xB2AEAC);

private Color color;
private Color darkerColor;
private int mask;

private EdgeGroupColor(int color) {
private EdgeGroupColor(int color, int darkerColor) {
this.color = new Color(color);
this.darkerColor = new Color(darkerColor);
mask = 1 << ordinal();
}

Expand All @@ -33,6 +36,18 @@ public Color get() {
return color;
}

public Color getDarker() {
return darkerColor;
}

public int getBGR() {
return FastColor.ABGR32.color(0xFF, color.getBlue(), color.getGreen(), color.getRed());
}

public int getDarkerBGR() {
return FastColor.ABGR32.color(0xFF, darkerColor.getBlue(), darkerColor.getGreen(), darkerColor.getRed());
}

public static EdgeGroupColor getDefault() {
return values()[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,8 +678,13 @@ public GirderAngles[] getBakedGirders() {
return bakedGirders;
}

public Map<Pair<Integer, Integer>, Double> rasterise() {
Map<Pair<Integer, Integer>, Double> yLevels = new HashMap<>();
public static class BezierPixel {
public double yLevel;
public double position;
}

public Map<Pair<Integer, Integer>, BezierPixel> rasterise() {
Map<Pair<Integer, Integer>, BezierPixel> pixels = new HashMap<>();
BlockPos tePosition = bePositions.getFirst();
Vec3 end1 = starts.getFirst()
.subtract(Vec3.atLowerCornerOf(tePosition))
Expand All @@ -702,9 +707,12 @@ public Map<Pair<Integer, Integer>, Double> rasterise() {
int segCount = getSegmentCount();
float[] lut = getStepLUT();
Vec3[] samples = new Vec3[segCount];
double[] positions = new double[segCount];

for (int i = 0; i < segCount; i++) {
float t = Mth.clamp((i + 0.5f) * lut[i] / segCount, 0, 1);
positions[i] = t * length;

Vec3 result = VecHelper.bezier(end1, end2, finish1, finish2, t);
Vec3 derivative = VecHelper.bezierDerivative(end1, end2, finish1, finish2, t)
.normalize();
Expand All @@ -731,10 +739,15 @@ public Map<Pair<Integer, Integer>, Double> rasterise() {
Vec3 railMiddle = samples[i];
BlockPos pos = BlockPos.containing(railMiddle);
Pair<Integer, Integer> key = Pair.of(pos.getX(), pos.getZ());
boolean alreadyPresent = yLevels.containsKey(key);
if (alreadyPresent && yLevels.get(key) <= railMiddle.y)
boolean alreadyPresent = pixels.containsKey(key);
if (alreadyPresent && pixels.get(key).yLevel <= railMiddle.y)
continue;
yLevels.put(key, railMiddle.y);

BezierPixel pixel = new BezierPixel();
pixel.yLevel = railMiddle.y;
pixel.position = positions[i];
pixels.put(key, pixel);

if (alreadyPresent)
continue;

Expand All @@ -744,13 +757,13 @@ public Map<Pair<Integer, Integer>, Double> rasterise() {
boolean prevCloser = diff(prev, center) > diff(prev2, center);

if (doubledViaPrev2 && (!doubledViaPrev || !prevCloser)) {
yLevels.remove(prev2);
pixels.remove(prev2);
prev2 = prev;
prev = key;
continue;

} else if (doubledViaPrev && doubledViaPrev2 && prevCloser) {
yLevels.remove(prev);
pixels.remove(prev);
prev = key;
continue;
}
Expand All @@ -761,7 +774,7 @@ public Map<Pair<Integer, Integer>, Double> rasterise() {
prev = key;
}

return yLevels;
return pixels;
}

private double diff(Pair<Integer, Integer> pFrom, Vec3 to) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.simibubi.create.api.contraption.transformable.TransformableBlockEntity;
import com.simibubi.create.content.contraptions.StructureTransform;
import com.simibubi.create.content.trains.graph.TrackNodeLocation;
import com.simibubi.create.content.trains.track.BezierConnection.BezierPixel;
import com.simibubi.create.foundation.block.ProperWaterloggedBlock;
import com.simibubi.create.foundation.blockEntity.IMergeableBE;
import com.simibubi.create.foundation.blockEntity.RemoveBlockEntityPacket;
Expand Down Expand Up @@ -363,10 +364,10 @@ private void removeFromCurveInteractionUnsafe() {
}

public void manageFakeTracksAlong(BezierConnection bc, boolean remove) {
Map<Pair<Integer, Integer>, Double> yLevels = bc.rasterise();
Map<Pair<Integer, Integer>, BezierPixel> bcPixels = bc.rasterise();

for (Entry<Pair<Integer, Integer>, Double> entry : yLevels.entrySet()) {
double yValue = entry.getValue();
for (Entry<Pair<Integer, Integer>, BezierPixel> entry : bcPixels.entrySet()) {
double yValue = entry.getValue().yLevel;
int floor = Mth.floor(yValue);
BlockPos targetPos = new BlockPos(entry.getKey()
.getFirst(), floor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public enum PlacementIndicatorSetting {
}

public enum TrainMapTheme {
RED, GREY, WHITE
RED, GREY, WHITE, SECTION
}

private static class Comments {
Expand Down