From f204189fdbbe8f07d729e2ca5c8a8048468738a5 Mon Sep 17 00:00:00 2001 From: tobspr Date: Mon, 22 Jun 2020 12:48:35 +0200 Subject: [PATCH] Refactor smart underground belt logic --- src/js/changelog.js | 1 + src/js/game/systems/underground_belt.js | 41 ++++++++++++++++++------- src/js/profile/application_settings.js | 8 ++++- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/js/changelog.js b/src/js/changelog.js index 20fdba5a..95553f19 100644 --- a/src/js/changelog.js +++ b/src/js/changelog.js @@ -4,6 +4,7 @@ export const CHANGELOG = [ date: "unreleased", entries: [ "Allow configuring autosave interval and disabling it in the settings", + "The smart-tunnel placement has been reworked to properly replace belts. Thus the setting has been turned on again by default", "The soundtrack now has a higher quality on the standalone version than the web version", "Add setting to disable cut/delete warnings (by hexy)", "Fix bug where belts in blueprints don't orient correctly (by hexy)", diff --git a/src/js/game/systems/underground_belt.js b/src/js/game/systems/underground_belt.js index 5b308e25..0456638a 100644 --- a/src/js/game/systems/underground_belt.js +++ b/src/js/game/systems/underground_belt.js @@ -77,6 +77,7 @@ export class UndergroundBeltSystem extends GameSystemWithFilter { const tier = undergroundComp.tier; const range = globalConfig.undergroundBeltMaxTilesByTier[tier]; + // FIND ENTRANCE // Search for the entrance which is furthes apart (this is why we can't reuse logic here) let matchingEntrance = null; for (let i = 0; i < range; ++i) { @@ -104,31 +105,49 @@ export class UndergroundBeltSystem extends GameSystemWithFilter { return; } - // Remove any belts between entrance and exit which have the same direction + // DETECT OBSOLETE BELTS BETWEEN + // Remove any belts between entrance and exit which have the same direction, + // but only if they *all* have the right direction currentPos = tile.copy(); + let allBeltsMatch = true; for (let i = 0; i < matchingEntrance.range; ++i) { currentPos.addInplace(offset); const contents = this.root.map.getTileContent(currentPos); if (!contents) { - continue; + allBeltsMatch = false; + break; } const contentsStaticComp = contents.components.StaticMapEntity; const contentsBeltComp = contents.components.Belt; + if (!contentsBeltComp) { + allBeltsMatch = false; + break; + } - if (contentsBeltComp) { - // It's a belt - if ( - contentsBeltComp.direction === enumDirection.top && - enumAngleToDirection[contentsStaticComp.rotation] === direction - ) { - // It's same rotation, drop it - this.root.logic.tryDeleteBuilding(contents); - } + // It's a belt + if ( + contentsBeltComp.direction !== enumDirection.top || + enumAngleToDirection[contentsStaticComp.rotation] !== direction + ) { + allBeltsMatch = false; + break; } } + currentPos = tile.copy(); + if (allBeltsMatch) { + // All belts between this are obsolete, so drop them + for (let i = 0; i < matchingEntrance.range; ++i) { + currentPos.addInplace(offset); + const contents = this.root.map.getTileContent(currentPos); + assert(contents, "Invalid smart underground belt logic"); + this.root.logic.tryDeleteBuilding(contents); + } + } + + // REMOVE OBSOLETE TUNNELS // Remove any double tunnels, by checking the tile plus the tile above currentPos = tile.copy().add(offset); for (let i = 0; i < matchingEntrance.range - 1; ++i) { diff --git a/src/js/profile/application_settings.js b/src/js/profile/application_settings.js index 1aa2b604..83aa21fb 100644 --- a/src/js/profile/application_settings.js +++ b/src/js/profile/application_settings.js @@ -468,7 +468,7 @@ export class ApplicationSettings extends ReadWriteProxy { } getCurrentVersion() { - return 15; + return 16; } /** @param {{settings: SettingsStorage, version: number}} data */ @@ -530,6 +530,12 @@ export class ApplicationSettings extends ReadWriteProxy { data.version = 15; } + if (data.version < 16) { + // RE-ENABLE this setting, it already existed + data.settings.enableTunnelSmartplace = true; + data.version = 16; + } + return ExplainedResult.good(); } }