From 16cecece722c924816be8540c6629a0c4858b312 Mon Sep 17 00:00:00 2001 From: Brevven Date: Tue, 24 Dec 2024 02:39:48 -0800 Subject: [PATCH] workaround for ore generation --- changelog.txt | 6 ++-- control-util.lua | 72 ++++++++++++++++++++++++++++++++++++++++++ control.lua | 2 ++ locale/en/titanium.cfg | 2 ++ me.lua | 1 + settings.lua | 8 +++++ 6 files changed, 89 insertions(+), 2 deletions(-) diff --git a/changelog.txt b/changelog.txt index 63c723c..582a9c7 100644 --- a/changelog.txt +++ b/changelog.txt @@ -3,8 +3,10 @@ Version: 2.0.0 Date: 2024-12-06 Features: - Works with Factorio 2.0 vanilla and Space Age - Currently does not work when adding to a ongoing game: - https://forums.factorio.com/viewtopic.php?f=7&t=124996 + Currently uses a workaround to generate ore patches on new chunks when added to an existing + game due to an issue, https://forums.factorio.com/viewtopic.php?t=124996 + And thank's to Eradicator's script to make natural looking ore patches for the workaround, + https://forums.factorio.com/viewtopic.php?t=72723 - Compatibility with other mods is not confirmed, but many will still work - Titanium production on Vulcanus and Gleba, recycling on Fulgora. These production chains may need further balancing or updates. diff --git a/control-util.lua b/control-util.lua index e6ddddc..d6b863a 100644 --- a/control-util.lua +++ b/control-util.lua @@ -95,5 +95,77 @@ function util.warptorio2_expansion_helper() end +-- A workaround for generating ores until this bug is fixed: +-- https://forums.factorio.com/viewtopic.php?f=7&t=124996&p=655013#p655013 +function util.ore_workaround(event) + for i, ore in pairs(util.me.ores_for_workaround) do + if event.surface.map_gen_settings.autoplace_controls["titanium-ore"] then return end + if event.surface.name ~= "nauvis" then return end + if math.random() < settings.global[util.me.name.."-ore-workaround-probability"].value then + util.generate_ore(event, ore.name, ore.amount, ore.tiles) + end + end +end + +-- The majority of this function was written by Eradicator, see https://forums.factorio.com/viewtopic.php?t=72723 +function util.generate_ore(event, name, amount, tiles) + local biases = {[0] = {[0] = 1}} + local t = 1 + + repeat + t = t + util.grow(biases,t,tiles) + until t >= tiles + + local pos = {x=event.position.x*32, y=event.position.y*32} + local multiplier = math.max(math.abs(event.position.x), math.abs(event.position.y)) + if multiplier < 10 then return end -- don't generate too close to start + local total_bias = 0 + for x,_ in pairs(biases) do for y,bias in pairs(_) do + total_bias = total_bias + bias + end end + + for x,_ in pairs(biases) do for y,bias in pairs(_) do + local entity = { + name = name, + amount = amount * (bias/total_bias) * multiplier, + force = 'neutral', + position = {pos.x+x,pos.y+y}, + } + if event.surface.can_place_entity(entity) then + event.surface.create_entity(entity) + end + end end + +end + +-- The majority of this function was written by Eradicator, see https://forums.factorio.com/viewtopic.php?t=72723 +function util.grow(grid,t,tiles) + local w_max = 256 + local h_max = 256 + local abs = math.abs + local old = {} + local new_count = 0 + for x,_ in pairs(grid) do for y,__ in pairs(_) do + table.insert(old,{x,y}) + end end + for _,pos in pairs(old) do + local x,y = pos[1],pos[2] + local bias = grid[x][y] + for dx=-1,1,1 do for dy=-1,1,1 do + local a,b = x+dx, y+dy + if (math.random() > 0.9) and (abs(a) < w_max) and (abs(b) < h_max) then + grid[a] = grid[a] or {} + if not grid[a][b] then + grid[a][b] = 1 - (t/tiles) + new_count = new_count + 1 + if (new_count+t) == tiles then return new_count end + end + end + end end + end + return new_count + end + + return util diff --git a/control.lua b/control.lua index 1736d4d..7962b95 100644 --- a/control.lua +++ b/control.lua @@ -1,3 +1,5 @@ local util = require("control-util") script.on_init(util.check_fluid_mining) + +script.on_event(defines.events.on_chunk_generated, util.ore_workaround) diff --git a/locale/en/titanium.cfg b/locale/en/titanium.cfg index 2b27fae..d40b894 100644 --- a/locale/en/titanium.cfg +++ b/locale/en/titanium.cfg @@ -53,11 +53,13 @@ titanium-in-foundry=Smelt titanium in an oxygen-free environment bztitanium-recipe-bypass=Bypass recipes bztitanium-mining-fluid=Titanium mining fluid bztitanium-mining-fluid-amount=Titanium mining fluid amount +bztitanium-ore-workaround-probability=Ore generation workaround probability [mod-setting-description] bztitanium-recipe-bypass=Skip modifying these recipes (comma-separated list). bztitanium-mining-fluid=Choose which fluid to use when mining Titanium.\n[color=orange]Lubricant is recommended![/color] bztitanium-mining-fluid-amount=Amount of fluid used to mine. Default 3. +bztitanium-ore-workaround-probability=When using workaround, probability of generating ore on a given chunk. (Only used when mod is added to existing game, if bug in forum post 124996 is not fixed) [string-mod-setting] bztitanium-mining-fluid-lubricant=Lubricant bztitanium-mining-fluid-sulfuric-acid=Sulfuric Acid diff --git a/me.lua b/me.lua index 46d5642..7ebbdc4 100644 --- a/me.lua +++ b/me.lua @@ -3,6 +3,7 @@ local me = {} me.name = "bztitanium" me.titanium_plate = "" me.titanium_processing = "" +me.ores_for_workaround = {{name="titanium-ore", amount=100000, tiles=300}} if mods and mods["FactorioExtended-Plus-Core"] then me.titanium_plate = "titanium-alloy" diff --git a/settings.lua b/settings.lua index efdd0b4..d8bff66 100644 --- a/settings.lua +++ b/settings.lua @@ -21,4 +21,12 @@ data:extend({ minimum_value = 1, maximum_value = 1000, }, + { + type = "double-setting", + name = "bztitanium-ore-workaround-probability", + setting_type = "runtime-global", + default_value = .01, + minimum_value = .00001, + maximum_value = .1, + }, })