use of com.demod.factorio.prototype.EntityPrototype in project Factorio-FBSR by demodude4u.
the class RenderUtils method spriteRenderer.
public static EntityRenderer spriteRenderer(Layer layer, List<Sprite> sprites, BlueprintEntity entity, EntityPrototype prototype) {
Point2D.Double pos = entity.getPosition();
RenderUtils.shiftSprites(sprites, pos);
Map<Boolean, List<Sprite>> groupedSprites = sprites.stream().collect(Collectors.partitioningBy(sprite -> sprite.shadow));
// Rectangle2D.Double groundBounds =
// Utils.parseRectangle(prototype.lua().get("collision_box"));
Rectangle2D.Double groundBounds = Utils.parseRectangle(prototype.lua().get("selection_box"));
groundBounds.x += pos.x;
groundBounds.y += pos.y;
return new EntityRenderer(layer, groundBounds) {
@SuppressWarnings("unused")
private void debugShowBounds(Rectangle2D.Double groundBounds, Graphics2D g) {
long x = Math.round(groundBounds.getCenterX() * 2);
long y = Math.round(groundBounds.getCenterY() * 2);
long w = Math.round(groundBounds.width * 2);
long h = Math.round(groundBounds.height * 2);
// System.out.println("x=" + x + " y=" + y + " w=" + w + "
// h=" + h);
g.setColor(new Color(255, 255, 255, 64));
g.draw(groundBounds);
if (((w / 2) % 2) == (x % 2)) {
g.setColor(new Color(255, 0, 0, 64));
g.fill(groundBounds);
}
if (((h / 2) % 2) == (y % 2)) {
g.setColor(new Color(0, 255, 0, 64));
g.fill(groundBounds);
}
}
@Override
public void render(Graphics2D g) {
for (Sprite sprite : groupedSprites.get(false)) {
drawSprite(sprite, g);
// debugShowBounds(groundBounds, g);
}
}
@Override
public void renderShadows(Graphics2D g) {
for (Sprite sprite : groupedSprites.get(true)) {
drawSprite(sprite, g);
}
}
};
}
use of com.demod.factorio.prototype.EntityPrototype in project Factorio-FBSR by demodude4u.
the class EntityRendererFactory method createRenderers.
public void createRenderers(Consumer<Renderer> register, WorldMap map, DataTable dataTable, BlueprintEntity entity, EntityPrototype prototype) {
try {
List<Sprite> sprites;
Optional<LuaValue> findSpriteLua = defaultProperties.stream().map(p -> prototype.lua().get(p)).filter(l -> l != LuaValue.NIL).findAny();
if (findSpriteLua.isPresent()) {
LuaValue spriteLua = findSpriteLua.get();
boolean hasDir = spriteLua.get(entity.getDirection().name().toLowerCase()) != LuaValue.NIL;
if (hasDir) {
spriteLua = spriteLua.get(entity.getDirection().name().toLowerCase());
}
sprites = RenderUtils.getSpritesFromAnimation(spriteLua, entity.getDirection());
} else {
Sprite sprite = new Sprite();
sprite.image = FactorioData.getModImage(prototype.lua().get("icon").tojstring());
sprite.source = new Rectangle(0, 0, sprite.image.getWidth(), sprite.image.getHeight());
sprite.bounds = (Rectangle2D.Double) prototype.getSelectionBox().clone();
sprites = ImmutableList.of(sprite);
}
register.accept(RenderUtils.spriteRenderer(sprites, entity, prototype));
} catch (RuntimeException e) {
debugPrintContext(entity, prototype);
throw e;
}
}
use of com.demod.factorio.prototype.EntityPrototype in project Factorio-FBSR by demodude4u.
the class AssemblingMachineRendering method populateWorldMap.
@Override
public void populateWorldMap(WorldMap map, DataTable dataTable, BlueprintEntity entity, EntityPrototype prototype) {
String recipeName = entity.json().optString("recipe", null);
boolean hasFluid = false;
if (recipeName != null) {
Optional<RecipePrototype> optRecipe = dataTable.getRecipe(recipeName);
if (optRecipe.isPresent()) {
RecipePrototype protoRecipe = optRecipe.get();
List<LuaValue> items = new ArrayList<>();
Utils.forEach(protoRecipe.lua().get("ingredients"), (Consumer<LuaValue>) items::add);
LuaValue resultsLua = protoRecipe.lua().get("results");
if (resultsLua != LuaValue.NIL) {
items.add(resultsLua);
}
hasFluid = items.stream().anyMatch(lua -> {
LuaValue typeLua = lua.get("type");
return typeLua != LuaValue.NIL && typeLua.toString().equals("fluid");
});
}
}
LuaValue fluidBoxesLua = prototype.lua().get("fluid_boxes");
boolean offWhenNoFluidRecipe = fluidBoxesLua.isnil() ? true : fluidBoxesLua.get("off_when_no_fluid_recipe").optboolean(false);
if (!fluidBoxesLua.isnil() && (!offWhenNoFluidRecipe || hasFluid)) {
Utils.forEach(fluidBoxesLua, fluidBoxLua -> {
if (!fluidBoxLua.istable()) {
return;
}
Utils.forEach(fluidBoxLua.get("pipe_connections"), pipeConnectionLua -> {
Point2D.Double offset = Utils.parsePoint2D(pipeConnectionLua.get("position"));
if (Math.abs(offset.y) > Math.abs(offset.x)) {
offset.y += -Math.signum(offset.y);
} else {
offset.x += -Math.signum(offset.x);
}
Point2D.Double pos = entity.getDirection().left().offset(entity.getDirection().back().offset(entity.getPosition(), offset.y), offset.x);
Direction direction = offset.y > 0 ? entity.getDirection().back() : entity.getDirection();
map.setPipe(pos, direction);
});
});
}
}
use of com.demod.factorio.prototype.EntityPrototype 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;
}
use of com.demod.factorio.prototype.EntityPrototype in project Factorio-FBSR by demodude4u.
the class EntityRendererFactory method getWirePositionFor.
protected Point2D.Double getWirePositionFor(BlueprintEntity entity, EntityPrototype prototype, String colorName, int circuitId) {
LuaValue connectionPointLua = wireConnectionCircuitId.entrySet().stream().filter(e -> e.getValue() == circuitId).map(e -> prototype.lua().get(e.getKey())).filter(l -> !l.isnil()).findAny().get();
if (connectionPointLua.get("wire").isnil()) {
connectionPointLua = connectionPointLua.get(entity.getDirection().cardinal() + 1);
}
Point2D.Double pos = entity.getPosition();
Point2D.Double offset;
offset = Utils.parsePoint2D(connectionPointLua.get("wire").get(colorName));
return new Point2D.Double(pos.x + offset.x, pos.y + offset.y);
}
Aggregations