Search in sources :

Example 1 with RailNode

use of com.demod.fbsr.WorldMap.RailNode in project Factorio-FBSR by demodude4u.

the class FBSR method populateRailBlocking.

private static void populateRailBlocking(WorldMap map) {
    map.getRailNodes().cellSet().stream().filter(c -> c.getValue().hasSignals()).forEach(c -> {
        RailNode blockingNode = c.getValue();
        Set<Direction> signals = blockingNode.getSignals();
        for (Direction signalDir : signals) {
            Direction blockingDir = signalDir.back();
            if (signals.contains(blockingDir)) {
                continue;
            }
            {
                Queue<RailEdge> work = new ArrayDeque<>();
                work.addAll(blockingNode.getOutgoingEdges(blockingDir));
                while (!work.isEmpty()) {
                    RailEdge edge = work.poll();
                    if (edge.isBlocked()) {
                        continue;
                    }
                    edge.setBlocked(true);
                    RailNode node = map.getRailNode(edge.getEndPos()).get();
                    if (node.hasSignals()) {
                        continue;
                    }
                    if (node.getIncomingEdges(edge.getEndDir()).stream().allMatch(e -> e.isBlocked())) {
                        work.addAll(node.getOutgoingEdges(edge.getEndDir().back()));
                    }
                }
            }
            {
                Queue<RailEdge> work = new ArrayDeque<>();
                work.addAll(blockingNode.getIncomingEdges(blockingDir.back()));
                while (!work.isEmpty()) {
                    RailEdge edge = work.poll();
                    if (edge.isBlocked()) {
                        continue;
                    }
                    edge.setBlocked(true);
                    RailNode node = map.getRailNode(edge.getStartPos()).get();
                    if (node.hasSignals()) {
                        continue;
                    }
                    if (node.getOutgoingEdges(edge.getStartDir()).stream().allMatch(e -> e.isBlocked())) {
                        work.addAll(node.getIncomingEdges(edge.getStartDir().back()));
                    }
                }
            }
        // for (RailEdge startEdge :
        // blockingNode.getOutgoingEdges(blockingDir)) {
        // startEdge.setBlocked(true);
        // RailNode node = map.getRailNode(startEdge.getEndPos()).get();
        // Direction dir = startEdge.getEndDir();
        // Collection<RailEdge> edges;
        // while (!node.hasSignals() && ((edges =
        // node.getOutgoingEdges(dir)).size() == 1)) {
        // RailEdge edge = edges.iterator().next();
        // if (edge.isBlocked()) {
        // break;
        // }
        // edge.setBlocked(true);
        // node = map.getRailNode(edge.getEndPos()).get();
        // dir = edge.getEndDir();
        // }
        // }
        // 
        // for (RailEdge startEdge :
        // blockingNode.getIncomingEdges(blockingDir)) {
        // startEdge.setBlocked(true);
        // RailNode node = map.getRailNode(startEdge.getEndPos()).get();
        // Direction dir = startEdge.getEndDir();
        // Collection<RailEdge> edges;
        // while (!node.hasSignals() && ((edges =
        // node.getIncomingEdges(dir)).size() == 1)) {
        // RailEdge edge = edges.iterator().next();
        // if (edge.isBlocked()) {
        // break;
        // }
        // edge.setBlocked(true);
        // node = map.getRailNode(edge.getEndPos()).get();
        // dir = edge.getEndDir();
        // }
        // }
        }
    });
}
Also used : Color(java.awt.Color) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Point2D(java.awt.geom.Point2D) Level(com.demod.dcba.CommandReporting.Level) Rectangle2D(java.awt.geom.Rectangle2D) RenderingHints(java.awt.RenderingHints) DataTable(com.demod.factorio.DataTable) JSONException(org.json.JSONException) RailEdge(com.demod.fbsr.WorldMap.RailEdge) JSONObject(org.json.JSONObject) Ellipse2D(java.awt.geom.Ellipse2D) Map(java.util.Map) ImageIO(javax.imageio.ImageIO) LinkedHashMultiset(com.google.common.collect.LinkedHashMultiset) Shape(java.awt.Shape) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) DataPrototype(com.demod.factorio.prototype.DataPrototype) Collection(java.util.Collection) Set(java.util.Set) AffineTransform(java.awt.geom.AffineTransform) CubicCurve2D(java.awt.geom.CubicCurve2D) ItemPrototype(com.demod.factorio.prototype.ItemPrototype) List(java.util.List) ModInfo(com.demod.factorio.ModInfo) FactorioData(com.demod.factorio.FactorioData) Entry(java.util.Map.Entry) Optional(java.util.Optional) BasicStroke(java.awt.BasicStroke) Queue(java.util.Queue) EntityPrototype(com.demod.factorio.prototype.EntityPrototype) Rectangle(java.awt.Rectangle) Multiset(com.google.common.collect.Multiset) HashMap(java.util.HashMap) RecipePrototype(com.demod.factorio.prototype.RecipePrototype) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) RailNode(com.demod.fbsr.WorldMap.RailNode) ImmutableList(com.google.common.collect.ImmutableList) Graphics2D(java.awt.Graphics2D) SimpleEntry(java.util.AbstractMap.SimpleEntry) Layer(com.demod.fbsr.Renderer.Layer) Stroke(java.awt.Stroke) TilePrototype(com.demod.factorio.prototype.TilePrototype) Line2D(java.awt.geom.Line2D) CommandReporting(com.demod.dcba.CommandReporting) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) File(java.io.File) Consumer(java.util.function.Consumer) Utils(com.demod.factorio.Utils) ArrayDeque(java.util.ArrayDeque) TotalRawCalculator(com.demod.factorio.TotalRawCalculator) Table(com.google.common.collect.Table) RailEdge(com.demod.fbsr.WorldMap.RailEdge) RailNode(com.demod.fbsr.WorldMap.RailNode) Queue(java.util.Queue)

Example 2 with RailNode

use of com.demod.fbsr.WorldMap.RailNode in project Factorio-FBSR by demodude4u.

the class FBSR method populateRailStationLogistics.

private static void populateRailStationLogistics(WorldMap map) {
    map.getRailNodes().cellSet().stream().filter(c -> c.getValue().getStation().isPresent()).forEach(c -> {
        RailNode stationNode = c.getValue();
        Direction stationDir = stationNode.getStation().get();
        {
            Queue<RailEdge> work = new ArrayDeque<>();
            work.addAll(stationNode.getOutgoingEdges(stationDir));
            work.addAll(stationNode.getOutgoingEdges(stationDir.back()));
            while (!work.isEmpty()) {
                RailEdge edge = work.poll();
                if (edge.isBlocked() || edge.isOutput()) {
                    continue;
                }
                edge.setOutput(true);
                RailNode node = map.getRailNode(edge.getEndPos()).get();
                if (node.getIncomingEdges(edge.getEndDir()).stream().allMatch(e -> e.isOutput())) {
                    work.addAll(node.getOutgoingEdges(edge.getEndDir().back()));
                }
            }
        }
        {
            Queue<RailEdge> work = new ArrayDeque<>();
            work.addAll(stationNode.getIncomingEdges(stationDir.back()));
            while (!work.isEmpty()) {
                RailEdge edge = work.poll();
                if (edge.isBlocked() || edge.isInput()) {
                    continue;
                }
                edge.setInput(true);
                RailNode node = map.getRailNode(edge.getStartPos()).get();
                if (node.getOutgoingEdges(edge.getStartDir()).stream().allMatch(e -> e.isInput())) {
                    work.addAll(node.getIncomingEdges(edge.getStartDir().back()));
                }
            }
        }
    });
}
Also used : Color(java.awt.Color) ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Point2D(java.awt.geom.Point2D) Level(com.demod.dcba.CommandReporting.Level) Rectangle2D(java.awt.geom.Rectangle2D) RenderingHints(java.awt.RenderingHints) DataTable(com.demod.factorio.DataTable) JSONException(org.json.JSONException) RailEdge(com.demod.fbsr.WorldMap.RailEdge) JSONObject(org.json.JSONObject) Ellipse2D(java.awt.geom.Ellipse2D) Map(java.util.Map) ImageIO(javax.imageio.ImageIO) LinkedHashMultiset(com.google.common.collect.LinkedHashMultiset) Shape(java.awt.Shape) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) DataPrototype(com.demod.factorio.prototype.DataPrototype) Collection(java.util.Collection) Set(java.util.Set) AffineTransform(java.awt.geom.AffineTransform) CubicCurve2D(java.awt.geom.CubicCurve2D) ItemPrototype(com.demod.factorio.prototype.ItemPrototype) List(java.util.List) ModInfo(com.demod.factorio.ModInfo) FactorioData(com.demod.factorio.FactorioData) Entry(java.util.Map.Entry) Optional(java.util.Optional) BasicStroke(java.awt.BasicStroke) Queue(java.util.Queue) EntityPrototype(com.demod.factorio.prototype.EntityPrototype) Rectangle(java.awt.Rectangle) Multiset(com.google.common.collect.Multiset) HashMap(java.util.HashMap) RecipePrototype(com.demod.factorio.prototype.RecipePrototype) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) RailNode(com.demod.fbsr.WorldMap.RailNode) ImmutableList(com.google.common.collect.ImmutableList) Graphics2D(java.awt.Graphics2D) SimpleEntry(java.util.AbstractMap.SimpleEntry) Layer(com.demod.fbsr.Renderer.Layer) Stroke(java.awt.Stroke) TilePrototype(com.demod.factorio.prototype.TilePrototype) Line2D(java.awt.geom.Line2D) CommandReporting(com.demod.dcba.CommandReporting) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) File(java.io.File) Consumer(java.util.function.Consumer) Utils(com.demod.factorio.Utils) ArrayDeque(java.util.ArrayDeque) TotalRawCalculator(com.demod.factorio.TotalRawCalculator) Table(com.google.common.collect.Table) RailEdge(com.demod.fbsr.WorldMap.RailEdge) RailNode(com.demod.fbsr.WorldMap.RailNode) Queue(java.util.Queue)

Example 3 with RailNode

use of com.demod.fbsr.WorldMap.RailNode in project Factorio-FBSR by demodude4u.

the class FBSR method showRailLogistics.

private static void showRailLogistics(Consumer<Renderer> register, DataTable table, WorldMap map, JSONObject options) {
    for (Entry<RailEdge, RailEdge> pair : map.getRailEdges()) {
        boolean input = pair.getKey().isInput() || pair.getValue().isInput();
        boolean output = pair.getKey().isOutput() || pair.getValue().isOutput();
        if (input || output) {
            RailEdge edge = pair.getKey();
            Point2D.Double p1 = edge.getStartPos();
            Direction d1 = edge.getStartDir();
            Point2D.Double p2 = edge.getEndPos();
            Direction d2 = edge.getEndDir();
            register.accept(new Renderer(Layer.LOGISTICS_RAIL_IO, edge.getStartPos()) {

                @Override
                public void render(Graphics2D g) {
                    Shape path;
                    if (edge.isCurved()) {
                        double control = 1.7;
                        Point2D.Double cc1 = d1.offset(p1, control);
                        Point2D.Double cc2 = d2.offset(p2, control);
                        path = new CubicCurve2D.Double(p1.x, p1.y, cc1.x, cc1.y, cc2.x, cc2.y, p2.x, p2.y);
                    } else {
                        path = new Line2D.Double(p1, p2);
                    }
                    Color color = (input && output) ? Color.yellow : input ? Color.green : Color.red;
                    Stroke ps = g.getStroke();
                    g.setStroke(new BasicStroke(32 / 32f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
                    g.setColor(RenderUtils.withAlpha(color, 32));
                    g.draw(path);
                    g.setStroke(ps);
                }
            });
        }
        if (options.optBoolean("debug-rail")) {
            for (RailEdge edge : ImmutableList.of(pair.getKey(), pair.getValue())) {
                if (edge.isBlocked()) {
                    continue;
                }
                Point2D.Double p1 = edge.getStartPos();
                Direction d1 = edge.getStartDir();
                Point2D.Double p2 = edge.getEndPos();
                Direction d2 = edge.getEndDir();
                register.accept(new Renderer(Layer.LOGISTICS_RAIL_IO, edge.getStartPos()) {

                    @Override
                    public void render(Graphics2D g) {
                        Stroke ps = g.getStroke();
                        g.setStroke(new BasicStroke(2 / 32f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
                        g.setColor(RenderUtils.withAlpha(Color.green, 92));
                        g.draw(new Line2D.Double(d1.right().offset(p1), d2.left().offset(p2)));
                        g.setStroke(ps);
                    }
                });
            }
        }
    }
    if (options.optBoolean("debug-rail")) {
        map.getRailNodes().cellSet().forEach(c -> {
            Point2D.Double pos = new Point2D.Double(c.getRowKey() / 2.0, c.getColumnKey() / 2.0);
            RailNode node = c.getValue();
            register.accept(new Renderer(Layer.DEBUG_RA1, pos) {

                @Override
                public void render(Graphics2D g) {
                    Stroke ps = g.getStroke();
                    g.setStroke(new BasicStroke(1 / 32f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
                    g.setColor(Color.cyan);
                    g.setFont(new Font("Courier New", Font.PLAIN, 1));
                    for (Direction dir : Direction.values()) {
                        Collection<RailEdge> edges = node.getIncomingEdges(dir);
                        if (!edges.isEmpty()) {
                            Point2D.Double p1 = dir.right().offset(pos, 0.25);
                            Point2D.Double p2 = dir.offset(p1, 0.5);
                            g.draw(new Line2D.Double(p1, p2));
                            g.drawString("" + edges.size(), (float) p2.x - 0.1f, (float) p2.y - 0.2f);
                        }
                    }
                    g.setStroke(ps);
                }
            });
            register.accept(new Renderer(Layer.DEBUG_RA2, pos) {

                @Override
                public void render(Graphics2D g) {
                    Stroke ps = g.getStroke();
                    g.setStroke(new BasicStroke(1 / 32f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
                    g.setColor(Color.magenta);
                    g.setFont(new Font("Courier New", Font.PLAIN, 1));
                    for (Direction dir : Direction.values()) {
                        Collection<RailEdge> edges = node.getOutgoingEdges(dir);
                        if (!edges.isEmpty()) {
                            Point2D.Double p1 = dir.left().offset(pos, 0.25);
                            Point2D.Double p2 = dir.offset(p1, 0.5);
                            g.draw(new Line2D.Double(p1, p2));
                            g.drawString("" + edges.size(), (float) p2.x - 0.1f, (float) p2.y - 0.2f);
                        }
                    }
                    g.setStroke(ps);
                }
            });
        });
    }
}
Also used : BasicStroke(java.awt.BasicStroke) BasicStroke(java.awt.BasicStroke) Stroke(java.awt.Stroke) Shape(java.awt.Shape) Color(java.awt.Color) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) RailEdge(com.demod.fbsr.WorldMap.RailEdge) RailNode(com.demod.fbsr.WorldMap.RailNode) Point2D(java.awt.geom.Point2D) Collection(java.util.Collection)

Aggregations

RailEdge (com.demod.fbsr.WorldMap.RailEdge)3 RailNode (com.demod.fbsr.WorldMap.RailNode)3 BasicStroke (java.awt.BasicStroke)3 Color (java.awt.Color)3 Font (java.awt.Font)3 Graphics2D (java.awt.Graphics2D)3 Shape (java.awt.Shape)3 Stroke (java.awt.Stroke)3 Point2D (java.awt.geom.Point2D)3 CommandReporting (com.demod.dcba.CommandReporting)2 Level (com.demod.dcba.CommandReporting.Level)2 DataTable (com.demod.factorio.DataTable)2 FactorioData (com.demod.factorio.FactorioData)2 ModInfo (com.demod.factorio.ModInfo)2 TotalRawCalculator (com.demod.factorio.TotalRawCalculator)2 Utils (com.demod.factorio.Utils)2 DataPrototype (com.demod.factorio.prototype.DataPrototype)2 EntityPrototype (com.demod.factorio.prototype.EntityPrototype)2 ItemPrototype (com.demod.factorio.prototype.ItemPrototype)2 RecipePrototype (com.demod.factorio.prototype.RecipePrototype)2