commit 2ed1c3a1a1ec84fff1e42c58320a33abb9d6976c Author: Filipe Rodrigues Date: Sun Sep 26 00:48:32 2021 +0100 Initial commit. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e969b2c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# IDEs +.vscode + +# Logs +factorio-current.log \ No newline at end of file diff --git a/control.lua b/control.lua new file mode 100644 index 0000000..01eda88 --- /dev/null +++ b/control.lua @@ -0,0 +1 @@ +-- Control diff --git a/data.lua b/data.lua new file mode 100644 index 0000000..b8637ad --- /dev/null +++ b/data.lua @@ -0,0 +1,29 @@ +-- Data + +-- Imports +local lbn = require("lbn") +local table_len = lbn.util.table_len +local table_map = lbn.util.table_map +local table_values = lbn.util.table_values +local array_flatten = lbn.util.array_flatten + +-- Get all data modules to add +local data_mods = { "entities", "hotkeys", "items", "recipies", "technologies" } +local data = table_map(data_mods, function(_, mod) + return mod, require(("data.%s"):format(mod)) +end) + +-- Log all data we got +do + for name, table in pairs(data) do + log(("Found %i %s"):format(table_len(table), name)) + end +end + +-- Then flatten it to get all data +local all_data = array_flatten(table_values(data)) + +-- Then, if not empty, add it all +if #all_data ~= 0 then + data:extend(all_data) +end diff --git a/data/entities.lua b/data/entities.lua new file mode 100644 index 0000000..07fb5ee --- /dev/null +++ b/data/entities.lua @@ -0,0 +1,3 @@ +-- Entities + +return {} diff --git a/data/hotkeys.lua b/data/hotkeys.lua new file mode 100644 index 0000000..93f9772 --- /dev/null +++ b/data/hotkeys.lua @@ -0,0 +1,3 @@ +-- Hotkeys + +return {} diff --git a/data/items.lua b/data/items.lua new file mode 100644 index 0000000..5c32541 --- /dev/null +++ b/data/items.lua @@ -0,0 +1,3 @@ +-- Items + +return {} diff --git a/data/recipies.lua b/data/recipies.lua new file mode 100644 index 0000000..ec04bca --- /dev/null +++ b/data/recipies.lua @@ -0,0 +1,3 @@ +-- Recipies + +return {} diff --git a/data/technologies.lua b/data/technologies.lua new file mode 100644 index 0000000..c4e7d44 --- /dev/null +++ b/data/technologies.lua @@ -0,0 +1,3 @@ +-- Technologies + +return {} diff --git a/info.json b/info.json new file mode 100644 index 0000000..831e0f7 --- /dev/null +++ b/info.json @@ -0,0 +1,13 @@ +{ + "title": "Logistic Belt Network", + "name": "logistic-belt-network", + "author": "zenithsiz", + "version": "0.1.0", + "factorio_version": "1.1", + "contact": "", + "license": "MIT", + "description": "", + "dependencies": [ + "base >= 1.1" + ] +} \ No newline at end of file diff --git a/lbn.lua b/lbn.lua new file mode 100644 index 0000000..278cc8f --- /dev/null +++ b/lbn.lua @@ -0,0 +1,5 @@ +-- Lbn Library + +return { + util = require("lbn.util"), +} diff --git a/lbn/util.lua b/lbn/util.lua new file mode 100644 index 0000000..8ce35f9 --- /dev/null +++ b/lbn/util.lua @@ -0,0 +1,161 @@ +-- Utilities + +local util = {} + +-- Returns if `tbl` is a table +function util.is_table(tbl) + return type(tbl) == "table" +end + +-- Asserts `tbl` is a table +function util.assert_table(tbl) + -- Note: Needs to be done like this due to no lazily evaluation + if not util.is_table(tbl) then + local msg = ("Expected table, found %s: %s"):format(type(tbl), util.format_value(tbl)) + assert(false, msg) + end +end + +-- Returns if `tbl` is an array +-- Adapted from `https://stackoverflow.com/questions/7526223` +function util.is_array(tbl) + -- If it isn't a table, it isn't an array + if not util.is_table(tbl) then + return false + end + + -- Note: We also catch empty arrays + if #tbl == 0 then + return next(tbl) == nil + else + return next(tbl, #tbl) == nil + end +end + +-- Asserts `tbl` is an array +function util.assert_array(arr) + -- Note: Needs to be done like this due to no lazily evaluation + if not util.is_array(arr) then + local msg = ("Expected array, found %s: %s"):format(type(arr), util.format_value(arr)) + assert(false, msg) + end +end + +-- Formats a value +function util.format_value(value) + if util.is_array(value) then + return util.format_array(value) + elseif util.is_table(value) then + return util.format_table(value) + elseif type(value) == "string" then + return ('"%s"'):format(tostring(value)) + else + return tostring(value) + end +end + +-- Formats a table +function util.format_table(tbl) + util.assert_table(tbl) + + local s = "{ " + + local cur_idx = 0 + local tbl_len = util.table_len(tbl) + for key, value in pairs(tbl) do + s = s .. util.format_value(key) + s = s .. " = " + s = s .. util.format_value(value) + + if cur_idx + 1 == tbl_len then + break + end + + s = s .. ", " + + cur_idx = cur_idx + 1 + end + + s = s .. " }" + return s +end + +-- Formats an array +function util.format_array(arr) + util.assert_array(arr) + + local s = "{ " + + for idx, value in ipairs(arr) do + s = s .. util.format_value(value) + + if idx == #arr then + break + end + + s = s .. ", " + end + + s = s .. " }" + return s +end + +-- Returns the number of entries in a table +function util.table_len(tbl) + util.assert_table(tbl) + + local count = 0 + for _ in pairs(tbl) do + count = count + 1 + end + return count +end + +-- Maps all keys and values in a table +function util.table_map(tbl, f) + util.assert_table(tbl) + + local output = {} + for key, value in pairs(tbl) do + key, value = f(key, value) + output[key] = value + end + return output +end + +-- Maps all values in a table +function util.table_map_values(tbl, f) + util.assert_table(tbl) + + return util.table_map(tbl, function(key, value) + return key, f(value) + end) +end + +-- Returns all values of a table as an array +function util.table_values(tbl) + util.assert_table(tbl) + + local output = {} + for _, value in pairs(tbl) do + table.insert(output, value) + end + return output +end + +-- Flattens an array of arrays +function util.array_flatten(arr) + util.assert_array(arr) + + local output = {} + for _, sub_arr in ipairs(arr) do + assert(util.is_array(sub_arr)) + + for _, value in ipairs(sub_arr) do + table.insert(output, value) + end + end + return output +end + +return util diff --git a/settings.lua b/settings.lua new file mode 100644 index 0000000..f934a2e --- /dev/null +++ b/settings.lua @@ -0,0 +1 @@ +-- Settings