commit bf398c7ff565bb20c10a7fb3a42e7c282472e020 Author: David Skrundz Date: Sun Sep 15 02:10:53 2024 -0600 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c4c4ffc --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.zip diff --git a/.vscode/.lua-format b/.vscode/.lua-format new file mode 100644 index 0000000..c729fb2 --- /dev/null +++ b/.vscode/.lua-format @@ -0,0 +1,33 @@ +column_limit: 100 +indent_width: 2 +use_tab: false +tab_width: 4 +continuation_indent_width: 4 +keep_simple_control_block_one_line: false +keep_simple_function_one_line: false +align_args: true +break_after_functioncall_lp: false +break_before_functioncall_rp: false +align_parameter: true +chop_down_parameter: true +break_after_functiondef_lp: true +break_before_functiondef_rp: true +align_table_field: true +break_after_table_lb: true +break_before_table_rb: true +chop_down_table: false +chop_down_kv_table: true +column_table_limit: 100 +column_table_limit_kv: 100 +table_sep: "," +extra_sep_at_table_end: true +break_after_operator: false +single_quote_to_double_quote: true +double_quote_to_single_quote: false +spaces_before_call: 1 +spaces_inside_functiondef_parens: false +spaces_inside_functioncall_parens: false +spaces_inside_table_braces: false +spaces_around_equals_in_field: true +line_breaks_after_function_body: 1 +line_separator: input diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..9ac1454 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "koihik.vscode-lua-format", + ], +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3a7a685 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,11 @@ +{ + "editor.detectIndentation": false, + "editor.tabSize": 4, + "editor.insertSpaces": false, + "[lua]": { + "editor.detectIndentation": false, + "editor.tabSize": 2, + "editor.insertSpaces": true, + }, + "vscode-lua-format.configPath": ".vscode/.lua-format" +} diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..2f4b83c --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,6 @@ +How to Contribute +================= + +We'd love to accept your patches and contributions to this project. +We just need you to follow the Contributor License Agreement outlined +in the latest v0.0.x of https://github.com/Skrunix/license diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..161d3b0 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,7 @@ +Skrunix Software License +======================== + +Permission to use, copy, modify, and/or distribute this software is +outlined in the latest v0.0.x of https://github.com/Skrunix/license + +THE SOFTWARE IS PROVIDED "AS IS" diff --git a/README.md b/README.md new file mode 100644 index 0000000..c49035d --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Teardown + +Automate the removal of depleted miners + +--- diff --git a/changelog.txt b/changelog.txt new file mode 100644 index 0000000..e7546c9 --- /dev/null +++ b/changelog.txt @@ -0,0 +1,5 @@ +--------------------------------------------------------------------------------------------------- +Version: 0.0.1 +Date: 15.09.2024 + Features: + - Initial release diff --git a/control.lua b/control.lua new file mode 100644 index 0000000..68331a5 --- /dev/null +++ b/control.lua @@ -0,0 +1,30 @@ +fmsl = require("__fmsl__.fmsl") + +local next = next + +active_tick = 1 +passive_tick = 31 + +require("teardown.mining_drill") +require("teardown.on_resource_depleted") +require("teardown.on_tick") + +script.on_init(function() + global.active = {} + global.active.mining_drills = {} -- [Tick:Entity] + global.passive = {} + global.passive.mining_drills = {} -- [Entity:true] + + recompute_max_mining_drill_radius() +end) + +script.on_load(function() + if next(global.active.mining_drills) ~= nil then + script.on_nth_tick(active_tick, on_active_tick) + end + if next(global.passive.mining_drills) ~= nil then + script.on_nth_tick(passive_tick, on_passive_tick) + end +end) + +script.on_event(defines.events.on_resource_depleted, on_resource_depleted) diff --git a/info.json b/info.json new file mode 100644 index 0000000..2a0a715 --- /dev/null +++ b/info.json @@ -0,0 +1,13 @@ +{ + "name": "Teardown", + "version": "1.0.0", + "title": "Teardown", + "description": "Automate the removal of depleted miners", + "author": "David Skrundz", + "contact": "david@skrundz.ca", + "homepage": "https://git.skrundz.dev/skrundztorio/Teardown", + "factorio_version": "1.1", + "dependencies": [ + "fmsl" + ] +} diff --git a/package.py b/package.py new file mode 100755 index 0000000..e056236 --- /dev/null +++ b/package.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 + +import json +import os +from zipfile import ZipFile + +modFiles = [ + "info.json", + "changelog.txt", + "thumbnail.png", + + "control.lua", +] +modFolders = [ + "teardown", +] + +with open("info.json") as file: + modInfo = json.load(file) + +mod_name = modInfo["name"] +mod_version = modInfo["version"] +zipName = f"{mod_name}_{mod_version}" + +with ZipFile(f"{zipName}.zip", 'w') as modZip: + for file in modFiles: + modZip.write(file, arcname=f"{zipName}/{file}") + for folder in modFolders: + for root, dirs, files in os.walk(folder): + for file in files: + filePath = os.path.join(root, file) + archivePath = os.path.relpath(filePath, os.path.join(folder, '..')) + modZip.write(filePath, arcname=f"{zipName}/{archivePath}") diff --git a/teardown/mining_drill.lua b/teardown/mining_drill.lua new file mode 100644 index 0000000..b010809 --- /dev/null +++ b/teardown/mining_drill.lua @@ -0,0 +1,14 @@ +-- Populate global.max_mining_drill_radius with [Category:double] +function recompute_max_mining_drill_radius() + local max_radius = {} + + local prototypes = game.get_filtered_entity_prototypes({{filter = "type", type = "mining-drill"}}) + for _, prototype in pairs(prototypes) do + local categories = prototype.resource_categories or {} + for category, _ in pairs(categories) do + max_radius[category] = math.max(max_radius[category] or 0, prototype.mining_drill_radius or 0) + end + end + + global.max_mining_drill_radius = max_radius +end diff --git a/teardown/on_resource_depleted.lua b/teardown/on_resource_depleted.lua new file mode 100644 index 0000000..f9ff965 --- /dev/null +++ b/teardown/on_resource_depleted.lua @@ -0,0 +1,48 @@ +function on_resource_depleted(event) + local entity = event.entity + + if entity.initial_amount ~= nil then + -- Infinite Resource + return + end + + local surface = entity.surface + local position = entity.position + + local category = entity.prototype.resource_category + local radius = global.max_mining_drill_radius[category] + + if radius == nil then + log("max_mining_drill_radius: " .. serpent.block(global.max_mining_drill_radius)) + error("max_mining_drill_radius is not set for category: " .. category) + return + end + + local miners = surface.find_entities_filtered({ + area = fmsl.box.center_radius(position, radius), + type = "mining-drill", + to_be_deconstructed = false, + }) + + -- two tick delay for dropping product and finding a new resource node + local target_tick = event.tick + 2 + + local add_table = false + local active_miners = table.remove_key(global.active.mining_drills, target_tick) or {} + + for _, miner in pairs(miners) do + if miner.has_flag("hidden") then + goto miner_continue + end + + add_table = true + table.insert(active_miners, miner) + + ::miner_continue:: + end + + if add_table then + global.active.mining_drills[target_tick] = active_miners + script.on_nth_tick(active_tick, on_active_tick) + end +end diff --git a/teardown/on_tick.lua b/teardown/on_tick.lua new file mode 100644 index 0000000..b1245c7 --- /dev/null +++ b/teardown/on_tick.lua @@ -0,0 +1,47 @@ +local next = next + +function on_active_tick(event) + local active_mining_drills = global.active.mining_drills + local passive_mining_drills = global.passive.mining_drills + + local miners = table.remove_key(active_mining_drills, event.tick) or {} + for _, miner in pairs(miners) do + if miner.valid then + local status = miner.status + if status == defines.entity_status.no_minable_resources then + miner.order_deconstruction(miner.force, nil) + elseif status ~= defines.entity_status.working then + passive_mining_drills[miner] = true + script.on_nth_tick(passive_tick, on_passive_tick) + end + end + end + + if next(active_mining_drills) == nil then + script.on_nth_tick(active_tick, nil) + return + end +end + +function on_passive_tick(event) + local passive_mining_drills = global.passive.mining_drills + + for miner, _ in pairs(passive_mining_drills) do + if miner.valid then + local status = miner.status + if status == defines.entity_status.no_minable_resources then + miner.order_deconstruction(miner.force, nil) + passive_mining_drills[miner] = nil + elseif status == defines.entity_status.working then + passive_mining_drills[miner] = nil + end + else + passive_mining_drills[miner] = nil + end + end + + if next(passive_mining_drills) == nil then + script.on_nth_tick(passive_tick, nil) + return + end +end diff --git a/thumbnail.png b/thumbnail.png new file mode 100644 index 0000000..54e393e Binary files /dev/null and b/thumbnail.png differ