Search in sources :

Example 1 with TilePrototype

use of com.demod.factorio.prototype.TilePrototype in project Factorio-FBSR by demodude4u.

the class FBSR method renderBlueprint.

public static BufferedImage renderBlueprint(Blueprint blueprint, CommandReporting reporting, JSONObject options) throws JSONException, IOException {
    System.out.println("Rendering " + blueprint.getLabel().orElse("(No Name)"));
    long startMillis = System.currentTimeMillis();
    DataTable table = FactorioData.getTable();
    WorldMap map = new WorldMap();
    List<EntityRenderingTuple> entityRenderingTuples = new ArrayList<EntityRenderingTuple>();
    List<TileRenderingTuple> tileRenderingTuples = new ArrayList<TileRenderingTuple>();
    for (BlueprintEntity entity : blueprint.getEntities()) {
        EntityRenderingTuple tuple = new EntityRenderingTuple();
        tuple.entity = entity;
        Optional<EntityPrototype> prototype = table.getEntity(entity.getName());
        if (!prototype.isPresent()) {
            tuple.prototype = null;
            tuple.factory = EntityRendererFactory.UNKNOWN;
            blueprint.setModsDetected();
        } else {
            tuple.prototype = prototype.get();
            tuple.factory = EntityRendererFactory.forType(tuple.prototype.getType());
            if (options.optBoolean("debug-typeMapping")) {
                reporting.addDebug(entity.getName() + " -> " + tuple.factory.getClass().getSimpleName());
            }
        }
        entityRenderingTuples.add(tuple);
    }
    for (BlueprintTile tile : blueprint.getTiles()) {
        TileRenderingTuple tuple = new TileRenderingTuple();
        tuple.tile = tile;
        Optional<TilePrototype> prototype = table.getTile(tile.getName());
        if (!prototype.isPresent()) {
            tuple.prototype = null;
            tuple.factory = TileRendererFactory.UNKNOWN;
            blueprint.setModsDetected();
        } else {
            tuple.prototype = prototype.get();
            tuple.factory = TileRendererFactory.forType(tuple.prototype.getType());
            if (options.optBoolean("debug-typeMapping")) {
                reporting.addDebug(tile.getName() + " -> " + tuple.factory.getClass().getSimpleName());
            }
        }
        tileRenderingTuples.add(tuple);
    }
    if (blueprint.getVersion().greaterOrEquals(new MapVersion(0, 18, 37, 3)))
        alignTileRenderingTuplesToGrid(tileRenderingTuples);
    else
        // legacy
        alignRenderingTuplesToGrid(entityRenderingTuples, tileRenderingTuples);
    entityRenderingTuples.forEach(t -> {
        try {
            t.factory.populateWorldMap(map, table, t.entity, t.prototype);
        } catch (Exception e) {
            reporting.addException(e);
        }
    });
    tileRenderingTuples.forEach(t -> {
        try {
            t.factory.populateWorldMap(map, table, t.tile, t.prototype);
        } catch (Exception e) {
            reporting.addException(e);
        }
    });
    entityRenderingTuples.forEach(t -> {
        try {
            t.factory.populateLogistics(map, table, t.entity, t.prototype);
        } catch (Exception e) {
            reporting.addException(e);
        }
    });
    populateReverseLogistics(map);
    populateTransitLogistics(map, options);
    populateRailBlocking(map);
    populateRailStationLogistics(map);
    List<Renderer> renderers = new ArrayList<>();
    entityRenderingTuples.forEach(t -> {
        try {
            t.factory.createRenderers(renderers::add, map, table, t.entity, t.prototype);
        } catch (Exception e) {
            reporting.addException(e);
        }
    });
    tileRenderingTuples.forEach(t -> {
        try {
            t.factory.createRenderers(renderers::add, map, table, t.tile, t.prototype);
        } catch (Exception e) {
            reporting.addException(e);
        }
    });
    entityRenderingTuples.forEach(t -> {
        try {
            t.factory.createModuleIcons(renderers::add, map, table, t.entity, t.prototype);
        } catch (Exception e) {
            reporting.addException(e);
        }
    });
    entityRenderingTuples.forEach(t -> {
        try {
            t.factory.createWireConnections(renderers::add, map, table, t.entity, t.prototype);
        } catch (Exception e) {
            reporting.addException(e);
        }
    });
    showLogisticGrid(renderers::add, table, map, options);
    showRailLogistics(renderers::add, table, map, options);
    ArrayListMultimap<Direction, PanelRenderer> borderPanels = ArrayListMultimap.create();
    if (options.optBoolean("show-info-panels", true)) {
        blueprint.getLabel().ifPresent(label -> {
            borderPanels.put(Direction.NORTH, createHeaderPanel(label));
        });
        borderPanels.put(Direction.SOUTH, createFooterPanel());
        Map<String, Double> totalItems = generateTotalItems(table, blueprint);
        borderPanels.put(Direction.EAST, createItemListPanel(table, "TOTAL", totalItems));
        borderPanels.put(Direction.EAST, createItemListPanel(table, "RAW", generateTotalRawItems(table, table.getRecipes(), totalItems)));
    }
    if (options.optBoolean("debug-placement")) {
        entityRenderingTuples.forEach(t -> {
            Point2D.Double pos = t.entity.getPosition();
            renderers.add(new Renderer(Layer.DEBUG_P, pos) {

                @Override
                public void render(Graphics2D g) {
                    g.setColor(Color.cyan);
                    g.fill(new Ellipse2D.Double(pos.x - 0.1, pos.y - 0.1, 0.2, 0.2));
                    Stroke ps = g.getStroke();
                    g.setStroke(new BasicStroke(3f / 32f));
                    g.setColor(Color.green);
                    g.draw(new Line2D.Double(pos, t.entity.getDirection().offset(pos, 0.3)));
                    g.setStroke(ps);
                }
            });
        });
        tileRenderingTuples.forEach(t -> {
            Point2D.Double pos = t.tile.getPosition();
            renderers.add(new Renderer(Layer.DEBUG_P, pos) {

                @Override
                public void render(Graphics2D g) {
                    g.setColor(Color.cyan);
                    g.fill(new Ellipse2D.Double(pos.x - 0.1, pos.y - 0.1, 0.2, 0.2));
                }
            });
        });
    }
    BufferedImage result = applyRendering(reporting, (int) Math.round(tileSize), renderers, borderPanels, options);
    long endMillis = System.currentTimeMillis();
    System.out.println("\tRender Time " + (endMillis - startMillis) + " ms");
    blueprint.setRenderTime(endMillis - startMillis);
    return result;
}
Also used : BasicStroke(java.awt.BasicStroke) DataTable(com.demod.factorio.DataTable) ArrayList(java.util.ArrayList) BufferedImage(java.awt.image.BufferedImage) Point2D(java.awt.geom.Point2D) EntityPrototype(com.demod.factorio.prototype.EntityPrototype) BasicStroke(java.awt.BasicStroke) Stroke(java.awt.Stroke) TilePrototype(com.demod.factorio.prototype.TilePrototype) JSONException(org.json.JSONException) IOException(java.io.IOException) Graphics2D(java.awt.Graphics2D)

Aggregations

DataTable (com.demod.factorio.DataTable)1 EntityPrototype (com.demod.factorio.prototype.EntityPrototype)1 TilePrototype (com.demod.factorio.prototype.TilePrototype)1 BasicStroke (java.awt.BasicStroke)1 Graphics2D (java.awt.Graphics2D)1 Stroke (java.awt.Stroke)1 Point2D (java.awt.geom.Point2D)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 JSONException (org.json.JSONException)1