Skip to content

Commit a7bc14d

Browse files
committed
small fixes and some better support for older blueprints
1 parent 70fe250 commit a7bc14d

File tree

3 files changed

+99
-35
lines changed

3 files changed

+99
-35
lines changed

blueprintVisualizer.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ function processBlueprint(blueprintJson, bboxBorderNWSE = [3, 3, 3, 3]) {
6060
tile.pos = [tile.position.x + 0.5, tile.position.y + 0.5];
6161
}
6262
}
63-
getSimplifiedEntities(blueprintJson.entities);
63+
64+
getSimplifiedEntities(blueprintJson.entities, blueprintJson.version);
6465
const [bboxWidth, bboxHeight, posOffset] = getSvgSizeAndPosOffset(blueprintJson.entities, blueprintJson.tiles, bboxBorderNWSE);
6566

6667
return {
@@ -75,15 +76,19 @@ function processBlueprint(blueprintJson, bboxBorderNWSE = [3, 3, 3, 3]) {
7576
};
7677
}
7778

78-
function getSimplifiedEntities(blueprintJsonEntities) {
79+
function getSimplifiedEntities(blueprintJsonEntities, blueprintJsonVersion) {
7980
for (const e of blueprintJsonEntities) {
80-
// Set default direction if not present
8181
if (!("direction" in e)) {
8282
e.direction = 0;
8383
} else {
84-
e.direction = parseInt(e.direction);
84+
// I'm not sure how blueprint versioning works exactly, but this seems to work for the blueprints I've tested
85+
const factorio_version_1 = (blueprintJsonVersion <= 300000000000000);
86+
if (factorio_version_1) {
87+
e.direction = parseInt(e.direction)*2;
88+
} else {
89+
e.direction = parseInt(e.direction);
90+
}
8591
}
86-
8792
// Convert position to array format
8893
e.pos = [e.position.x, e.position.y];
8994

@@ -247,7 +252,6 @@ function drawBlueprint(blueprint, settings, svgWidthInMm = 300, aspectRatio = nu
247252
} else if (settingName === "red-wire-lines") {
248253
lines = getLinesWire(blueprint.entities, blueprint.wires, WireType.RED_WIRE);
249254
} else {
250-
console.log("unknown setting name:", settingName);
251255
continue;
252256
}
253257
blueprint.cache[settingName] = lines;

drawingSettings.js

Lines changed: 88 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const EXAMPLE_SETTINGS = [
22
["how to use: https://github.com/piebro/factorio-blueprint-visualizer/blob/master/draw_setting_documentation.md"],
33
["default settings", {'background': '#a2aebb', 'stroke': 'none', 'stroke-linecap': 'round', 'stroke-width': 0.3}, {'scale': 0.85, 'rx': 0.1, 'ry': 0.1}],
4-
["tiles", {'fill': '#420217', 'stroke': '#f3ffbd', 'stroke-width': 0.15}, {'deny': [], 'size': 0.7}],
4+
["tiles", {'fill': '#420217', 'stroke': '#f3ffbd', 'stroke-width': 0.15}, {'deny': [], 'scale': 0.7}],
55

66
["pipes", {'stroke': '#c84c09'}],
77
["underground-pipes", {'stroke': '#c84c09'}],
@@ -259,6 +259,66 @@ const RANDOM_SETTING_LIST = [
259259
]
260260
];
261261

262+
263+
264+
const SVG_SETTINGS = {
265+
'fill': 'none', 'fill-opacity': 1, 'stroke': 'none', 'stroke-linecap': 'round', 'stroke-width': 0.3, 'stroke-opacity': 1
266+
};
267+
const OTHER_SETTINGS_BBOX = {'scale': 0.85, 'rx': 0.1, 'ry': 0.1};
268+
const OTHER_SETTINGS_TILES = {'scale': 0.85};
269+
// buildingGenericTerms
270+
// artificialTilesSortedByLayer
271+
272+
// 1. get all generic terms and do 1-6 bboxes with different colors. Look that
273+
274+
function getRandomSettings() {
275+
let settings = [];
276+
const SETTING_NAMES = ["belts", "underground-belts", "pipes", "underground-pipes", "heat-pipes", "inserters", "rails", "power-lines", "green-wire-lines", "red-wire-lines"];
277+
// "bbox",
278+
settings.push(["default settings", {'background': 'none', 'stroke': 'none', 'stroke-linecap': 'round', 'stroke-width': 0.3}, {'scale': 0.85, 'rx': 0.1, 'ry': 0.1}])
279+
280+
if (Math.random() < 0.8) {
281+
settings.push(["tiles", {'fill': 'none', 'stroke': 'none', 'stroke-width': 0.15}, {'deny': [], 'scale': 0.7}])
282+
}
283+
284+
const temp_settings = [];
285+
const sampleSettings = shuffleArray([...SETTING_NAMES]).slice(0, 3);
286+
for (const settingName of sampleSettings) {
287+
temp_settings.push([settingName, {}]);
288+
}
289+
290+
// Add 0-4 random bbox settings
291+
const bboxCount = Math.floor(Math.random() * 5); // Random number 0-4
292+
const genericBuildingTerms = Object.keys(buildingGenericTerms);
293+
const buildingTerms = Object.keys(entityNameToProperties);
294+
const allTerms = [...genericBuildingTerms, ...genericBuildingTerms, ...buildingTerms];
295+
296+
for (let i = 0; i < bboxCount; i++) {
297+
const buildingTermCount = Math.floor(Math.random() * 5) + 1;
298+
const group = shuffleArray([...allTerms]).slice(0, buildingTermCount);
299+
const bboxGroupType = Math.random() < 0.8 ? "allow" : "deny";
300+
temp_settings.push(["bbox", {[bboxGroupType]: group, 'fill': 'none'}]);
301+
}
302+
303+
settings = [...settings, ...temp_settings];
304+
305+
// Replace any "none" values with incrementing hex colors
306+
let noneColorCounter = 0;
307+
for (let s of settings) {
308+
if (typeof s[1] === 'object' && s[1] !== null) {
309+
for (let key of ['stroke', 'fill', 'background']) {
310+
if (key in s[1] && s[1][key] === 'none') {
311+
const hexColor = '#' + noneColorCounter.toString(16).padStart(6, '0');
312+
s[1][key] = hexColor;
313+
noneColorCounter++;
314+
}
315+
}
316+
}
317+
}
318+
319+
return settingsChangeColors(settings, 10, true);
320+
}
321+
262322
function deepCopy(obj) {
263323
return JSON.parse(JSON.stringify(obj));
264324
}
@@ -322,38 +382,38 @@ function settingsChangeColors(settings, colorCount = null, changeBackground = tr
322382
return settings;
323383
}
324384

325-
function getRandomSettings() {
326-
let settings = deepCopy(RANDOM_SETTING_LIST[Math.floor(Math.random() * RANDOM_SETTING_LIST.length)]);
385+
// function getRandomSettings() {
386+
// let settings = deepCopy(RANDOM_SETTING_LIST[Math.floor(Math.random() * RANDOM_SETTING_LIST.length)]);
327387

328-
if (Math.random() < 0.4) {
329-
const additionalSettings = RANDOM_SETTING_LIST[Math.floor(Math.random() * RANDOM_SETTING_LIST.length)];
330-
settings = [...settings, ...additionalSettings];
331-
settings = settingsChangeColors(settings);
332-
}
388+
// if (Math.random() < 0.4) {
389+
// const additionalSettings = RANDOM_SETTING_LIST[Math.floor(Math.random() * RANDOM_SETTING_LIST.length)];
390+
// settings = [...settings, ...additionalSettings];
391+
// settings = settingsChangeColors(settings);
392+
// }
333393

334-
if (Math.random() < 0.3) {
335-
const additionalSettings = RANDOM_SETTING_LIST[Math.floor(Math.random() * RANDOM_SETTING_LIST.length)];
336-
settings = [...settings, ...additionalSettings];
337-
if (settings.length > 8) {
338-
const indices = [0, 1, ...shuffleArray([...Array(settings.length).keys()].slice(2)).slice(0, settings.length - 4)];
339-
settings = settings.filter((_, i) => indices.includes(i));
340-
}
341-
settings = settingsChangeColors(settings);
342-
}
394+
// if (Math.random() < 0.3) {
395+
// const additionalSettings = RANDOM_SETTING_LIST[Math.floor(Math.random() * RANDOM_SETTING_LIST.length)];
396+
// settings = [...settings, ...additionalSettings];
397+
// if (settings.length > 8) {
398+
// const indices = [0, 1, ...shuffleArray([...Array(settings.length).keys()].slice(2)).slice(0, settings.length - 4)];
399+
// settings = settings.filter((_, i) => indices.includes(i));
400+
// }
401+
// settings = settingsChangeColors(settings);
402+
// }
343403

344-
if (Math.random() < 0.8) {
345-
settings = settingsChangeColors(settings, Math.floor(Math.random() * 10) + 2);
346-
}
404+
// if (Math.random() < 0.8) {
405+
// settings = settingsChangeColors(settings, Math.floor(Math.random() * 10) + 2);
406+
// }
347407

348-
if (Math.random() < 0.5) {
349-
settings = settingsChangeProperty(settings, "stroke-width", v => v * (Math.random() * 1.5 + 0.5));
350-
}
408+
// if (Math.random() < 0.5) {
409+
// settings = settingsChangeProperty(settings, "stroke-width", v => v * (Math.random() * 1.5 + 0.5));
410+
// }
351411

352-
if (Math.random() < 0.5) {
353-
settings = settingsChangeProperty(settings, "bbox-scale", v => v * (Math.random() * 0.3 + 0.7));
354-
}
355-
return settings;
356-
}
412+
// if (Math.random() < 0.5) {
413+
// settings = settingsChangeProperty(settings, "bbox-scale", v => v * (Math.random() * 0.3 + 0.7));
414+
// }
415+
// return settings;
416+
// }
357417

358418
function shuffleArray(array) {
359419
for (let i = array.length - 1; i > 0; i--) {

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@
456456
<a href="https://piebro.github.io?ref=piebro.github.io/factorio-blueprint-visualizer">Other Projects</a>
457457
</div>
458458

459-
<h1 style="text-align: center; color: #04AA6D; margin: 20px 0;">Factorio Blueprint Visualizer</h1>
459+
<h1 style="text-align: center; color: #04AA6D; margin: 5px 0;">Factorio Blueprint Visualizer</h1>
460460

461461
<div class="button-container" id="button-container">
462462
<button class="btn" id="uploadBtn" onclick="uploadBtnClick()">Upload Blueprint</button>

0 commit comments

Comments
 (0)