From e135e4893e16ce51b4667f386e5a6c37e66ef353 Mon Sep 17 00:00:00 2001 From: Nicholas Gower Date: Mon, 10 Feb 2025 14:24:28 -0800 Subject: [PATCH 1/2] compat: Added additional step to PlanetsLib:extend that restructures orbit information if formatted according to vanilla conventions. Makes it easier to integrate PlanetsLib into existing planets. --- lib/planet.lua | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/lib/planet.lua b/lib/planet.lua index 5bac059..58f9baf 100644 --- a/lib/planet.lua +++ b/lib/planet.lua @@ -3,10 +3,13 @@ local orbits = require("lib.orbits") local Public = {} function Public.extend(config) + Public.default_extend_fields(config) Public.verify_extend_fields(config) local planet = {} + + local distance, orientation = orbits.get_absolute_polar_position_from_orbit(config.orbit) planet.distance = distance @@ -16,6 +19,8 @@ function Public.extend(config) planet[k] = v end + + if planet.orbit.parent then --Adds encoded parent body to surface properties. if planet.orbit.parent.type == "planet" then if data.raw["planet"][config.orbit.parent.name] then @@ -36,37 +41,62 @@ function Public.is_space_location(planet) return planet.type == "planet" or planet.type == "space-location" end +function Public.default_extend_fields(config) --Adds default values when values are empty. This step does not seek to assert anything. +--Allows planet objects with orbits formatted according to vanilla structure to be passed to PlanetsLib without any modifications. +--Will not prevent planets with distance/orientation both defined in root and orbit from crashing. + if (config.orbit == nil) then + config.orbit = {} + end + if config.orbit then + if config.orbit.distance == nil and config.distance then + config.orbit.distance = config.distance + config.distance = nil + end + if config.orbit.orientation == nil and config.orientation then + config.orbit.orientation = config.orientation + config.orientation = nil + end + if config.orbit.parent == nil then --Default planet orbit parent mirroring vanilla planets. Before this was added, the library would crash when parent was nil. + config.orbit.parent = { + type = "space-location", + name ="star", + } + end + end + +end + -- TODO: Add checks to ensure the structure of orbit is correct. function Public.verify_extend_fields(config) if not Public.is_space_location(config) then error( - "PlanetsLib:extend() - extend only takes a planet or space-location prototype. See the PlanetsLib documentation at https://mods.factorio.com/mod/PlanetsLib." + "Error with space-location \""..config.name.."\": PlanetsLib:extend() - extend only takes a planet or space-location prototype. See the PlanetsLib documentation at https://mods.factorio.com/mod/PlanetsLib." ) end if not config.orbit then error( - "PlanetsLib:extend() - 'orbit' field is required. See the PlanetsLib documentation at https://mods.factorio.com/mod/PlanetsLib." + "Error with space-location \""..config.name.."\": PlanetsLib:extend() - 'orbit' field is required. See the PlanetsLib documentation at https://mods.factorio.com/mod/PlanetsLib." ) end if not Public.is_space_location(config.orbit.parent) then error( - "PlanetsLib:extend() - 'orbit.parent' must be a space location. See the PlanetsLib documentation at https://mods.factorio.com/mod/PlanetsLib." + "Error with space-location \""..config.name.."\": PlanetsLib:extend() - 'orbit.parent' must be a space location. See the PlanetsLib documentation at https://mods.factorio.com/mod/PlanetsLib." ) end - if config.distance then + if config.distance or not config.orbit.distance then error( - "PlanetsLib:extend() - 'distance' should be specified in the 'orbit' field. See the PlanetsLib documentation at https://mods.factorio.com/mod/PlanetsLib." + "Error with space-location \""..config.name.."\": PlanetsLib:extend() - 'distance' should be specified in the 'orbit' field. See the PlanetsLib documentation at https://mods.factorio.com/mod/PlanetsLib." ) end - if config.orientation then + if config.orientation or not config.orbit.distance then error( - "PlanetsLib:extend() - 'orientation' should be specified in the 'orbit' field. See the PlanetsLib documentation at https://mods.factorio.com/mod/PlanetsLib." + "Error with space-location \""..config.name.."\": PlanetsLib:extend() - 'orientation' should be specified in the 'orbit' field. See the PlanetsLib documentation at https://mods.factorio.com/mod/PlanetsLib." ) end if not config.orbit.parent then error( - "PlanetsLib:extend() - 'orbit.parent' field is required with an object containing 'type' and 'name' fields. See the PlanetsLib documentation at https://mods.factorio.com/mod/PlanetsLib." + "Error with space-location \""..config.name.."\": PlanetsLib:extend() - 'orbit.parent' field is required with an object containing 'type' and 'name' fields. See the PlanetsLib documentation at https://mods.factorio.com/mod/PlanetsLib." ) end end From cd0e020216a9a1658765bc8d177eeb79755c5ec9 Mon Sep 17 00:00:00 2001 From: Nicholas Gower Date: Mon, 10 Feb 2025 14:59:38 -0800 Subject: [PATCH 2/2] Update changelog.txt --- changelog.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog.txt b/changelog.txt index 8077a11..3600652 100644 --- a/changelog.txt +++ b/changelog.txt @@ -2,6 +2,7 @@ Version: 1.2.7 Date: ???? Changes: + - Planets with vanilla-style orbit fields can now be passed to PlanetsLib:extend without crashing. --------------------------------------------------------------------------------------------------- Version: 1.2.6 Date: 2025-02-07