Add reading schematics from a Lua table

This commit is contained in:
A S Lewis 2021-07-02 08:23:25 +01:00
parent 491182af46
commit 85cc63aad2
4 changed files with 200 additions and 53 deletions

View File

@ -6,27 +6,33 @@ Contributors: [Gael-de-Sailly](https://github.com/Gael-de-Sailly)
License: LGPL 2.1
This mod reads minetest schematics (.mts files), and does any or all of the following things:
This mod reads one or more minetest schematics (.mts files). It also accepts schematics as Lua
tables.
Then, it does any or all of the following things:
* Displays the contents of the schematic in the chat window/debug file
* Converts the contents of the schematic to a .lua file
* Swaps nodes in the schematic (e.g. swaps **default:stone** for **mymod:rock**), and saves the
modified schematic either as .mts or as .lua
* Swaps nodes in the schematic (e.g. swaps **default:stone** for **mymod:rock**)
* Saves the schematic as a .mts and/or .lua file
This mod has no gameplay value, and is only useful for developers.
Thus, this mod is useful for the following purposes:
This mod has been tested on Minetest 5.4.1. It has not been tested on MS Windows.
* Updating nodes, when .mts schematics are copied from one mod to another
* A general-purpose Lua > .mts > Lua schematic converter
# How to use
This mod has no gameplay value, and is only useful for developers. It has been tested on Minetest
5.4.1. It has not been tested on MS Windows.
Firstly, open the the **init.lua** file in your preferred text editor. There are some flags there
that can be modified to change the mod's behaviour. (Since this mod is for developers, we assume you
don't need the crutch of reading from a **settingtypes.txt** file).
# Importing schematics
The default behaviour is to convert original .mts file, but not to write additional .lua files.
Open the the **init.lua** file in your preferred text editor. There are some flags there that can be
modified to change the mod's behaviour. (Since this mod is for developers, we assume you don't need
the crutch of reading from a **settingtypes.txt** file).
Next, there is a folder called **input**. Copy the .mts files into this folder. Files in this
folder are never modified.
There are two ways to import schematics.
Firstly, .mts schematics should be copied into the folder called **input**. Files in this folder are
never modified or overwritten.
Unfortunately, there is no easy way for Lua to get a list of files from a folder; therefore we have
to write the list ourselves in **files.txt**. This should contain one file per line. Empty lines
@ -35,6 +41,15 @@ and lines starting with the # character are ignored.
There is a shortcut: if a file called **test.mts** exists, it will be loaded, regardless of whether
it is mentioned in **files.txt**.
Secondly, Lua tables can be copy-pasted into the **schematics.lua** file.
You can find some examples of schematics in the form of Lua tables in **minetest-game**, in the file
**schematic-tables.txt**. If you like, you could copy-paste the whole of that file into
**schematics.lua**. Don't forget to change the function calls from **mts_save()** to
**schemconvert.add_schem()**.
# Setting up node conversion
Next, the **convert.csv** file provides a list of nodes to convert. Once again, empty lines and
lines starting with the # character are ignored.
@ -50,12 +65,13 @@ For example
It is not necessary for either mod to be loaded; **schemconvert** deals with simple strings, it does
not check whether the nodes **default:stone** or **mymod:rock** actually exist in the game.
Now, start the game (with **schemconvert** enabled). The converted .mts files are written to the
**output** folder.
# How to use
Nervous users can check the results of their work by changing the flags (as described above) to
Start the game (with **schemconvert** enabled). Converted files are written to the **output**
folder.
You can check the results of your work by changing the flags (as described above) to
write the orginal and/or converted files as .lua files, so the changes can be inspected visually.
The .lua files are also written to the **output** folder.
# Comparable mods
@ -66,3 +82,7 @@ Converts lua tables to .mts files
[schemedit, by Wuzzy](https://repo.or.cz/minetest_schemedit.git)
Allows players to edit and export schematics in-game
[mtsedit, by bzt](https://gitlab.com/bztsrc/mtsedit)
An interactive MTS editor with GUI, and batch mode capabilities

166
init.lua
View File

@ -1,20 +1,27 @@
---------------------------------------------------------------------------------------------------
-- schemconvert mod by A S Lewis
-- v1.1 22 Jun 2021
-- Lieence: LGPL 2.1
---------------------------------------------------------------------------------------------------
local S = minetest.get_translator(minetest.get_current_modname())
schemconvert = {}
schemconvert.name = "schemconvert"
schemconvert.ver_max = 1
schemconvert.ver_min = 2
schemconvert.ver_rev = 0
local mod_path = minetest.get_modpath(minetest.get_current_modname())
local file_list = {}
schemconvert.schem_table = {}
local convert_table = {}
local check_table = {}
local converted_table = {}
local not_converted_table = {}
local convert_count = 0
local mts_count = 0
local schem_count = 0
local error_count = 0
-- If enabled, show info/error messages in the chat window and debug file
@ -44,15 +51,19 @@ local show_summary_flag = true
---------------------------------------------------------------------------------------------------
local function show_info(msg)
if show_msg_flag then
minetest.log("[SCHEMCONVERT] " .. msg)
end
end
local function show_error(msg)
if show_msg_flag then
minetest.log("[SCHEMCONVERT] [ERROR] " .. msg)
end
end
---------------------------------------------------------------------------------------------------
@ -190,6 +201,54 @@ local function print_table(table_to_show, optional_title)
end
-- Add a Lua schematic
function schemconvert.add_schem(name, schem_table)
-- Called from schematics.lua
-- The code there could easily add a key/value pair to schemconvert.schem_table, but usually
-- it's preferable to use a function call
-- (Specifically, the official minetest-game schematic list uses mts_save(), so that code could
-- be copy-pasted into our schematics.lua, with just the function name changed)
schemconvert.schem_table[name] = schem_table
end
-- Convert old nodes in a schematic table to new nodes
local function convert_nodes(schem_table)
for i, mini_table in ipairs(schem_table.data) do
if convert_table[mini_table.name] ~= nil then
-- (Keep track of which convertable nodes have been found at least once)
check_table[mini_table.name] = nil
-- Convert this node
mini_table.name = convert_table[mini_table.name]
schem_table.data[i] = mini_table
-- (Show converted nodes at the end, if required)
if converted_table[mini_table.name] == nil then
converted_table[mini_table.name] = 1
else
converted_table[mini_table.name] = converted_table[mini_table.name] + 1
end
else
if not_converted_table[mini_table.name] == nil then
not_converted_table[mini_table.name] = 1
else
not_converted_table[mini_table.name] = not_converted_table[mini_table.name] + 1
end
end
end
end
-- Save a schematic as .mts
local function save_mts(schem_table, output_path)
@ -232,7 +291,7 @@ local function save_lua(schem_table, output_path)
end
---------------------------------------------------------------------------------------------------
-- Do the conversion
-- Read schematics
---------------------------------------------------------------------------------------------------
-- Read the file list, ignoring anything that isn't an .mts file
@ -244,8 +303,10 @@ if file_exists(mod_path .. "/input/test.mts") then
for _, value in ipairs(file_list) do
if value == "test.mts" then
match_flag = true
break
end
end
@ -258,6 +319,20 @@ end
show_info(S("Number of .mts files specified: @1", #file_list))
-- Read Lua schematics into a table
dofile(mod_path .. "/schematics.lua")
local count = 0
for k, v in pairs(schemconvert.schem_table) do
count = count + 1
end
show_info(S("Number of Lua schematics specified: @1", count))
---------------------------------------------------------------------------------------------------
-- Read conversion table
---------------------------------------------------------------------------------------------------
-- Read the conversion table. Every line in the CSV file should be in the form
-- original_node|replacement_node
-- For example
@ -275,7 +350,11 @@ end
show_info(S("Number of convertable nodes specified: @1", tostring(count)))
-- Convert each schematic, one at a time
---------------------------------------------------------------------------------------------------
-- Convert schematics
---------------------------------------------------------------------------------------------------
-- Convert each MTS schematic, one at a time
for _, local_path in ipairs(file_list) do
local input_path = mod_path .. "/input/" .. local_path
@ -301,35 +380,7 @@ for _, local_path in ipairs(file_list) do
end
-- Convert old nodes to new
for i, mini_table in ipairs(schem_table.data) do
if convert_table[mini_table.name] ~= nil then
-- (Keep track of which convertable nodes have been found at least once)
check_table[mini_table.name] = nil
-- Convert this node
mini_table.name = convert_table[mini_table.name]
schem_table.data[i] = mini_table
-- (Show converted nodes at the end, if required)
if converted_table[mini_table.name] == nil then
converted_table[mini_table.name] = 1
else
converted_table[mini_table.name] = converted_table[mini_table.name] + 1
end
else
if not_converted_table[mini_table.name] == nil then
not_converted_table[mini_table.name] = 1
else
not_converted_table[mini_table.name] = not_converted_table[mini_table.name] + 1
end
end
end
convert_nodes(schem_table)
-- Save the converted .mts file
if do_convert_flag then
@ -337,7 +388,7 @@ for _, local_path in ipairs(file_list) do
if not save_mts(schem_table, output_path) then
error_count = error_count + 1
else
convert_count = convert_count + 1
mts_count = mts_count + 1
end
end
@ -354,8 +405,51 @@ for _, local_path in ipairs(file_list) do
end
-- Show the results
show_info(S("Number of .mts files converted: @1", convert_count))
-- Convert each Lua schematic, one at a time
for name, schem_table in pairs(schemconvert.schem_table) do
local output_path = mod_path .. "/output/" .. name .. ".mts"
local input_lua_path = mod_path .. "/output/" .. name .. ".input.lua"
local output_lua_path = mod_path .. "/output/" .. name .. ".output.lua"
-- Write/display the original .lua schematic, if required
if write_converted_lua_flag then
save_lua(schem_table, input_lua_path)
end
if show_original_lua_flag then
print_table(schem_table, S("Contents of schematic @1", name))
end
-- Convert old nodes to new
convert_nodes(schem_table)
-- Save the converted .mts file
if do_convert_flag then
if not save_mts(schem_table, output_path) then
error_count = error_count + 1
else
schem_count = schem_count + 1
end
end
-- Write/display the converted .mts file as lua, if required
if write_converted_lua_flag then
save_lua(schem_table, output_lua_path)
end
if show_converted_lua_flag then
print_table(schem_table, S("Contents of schematic @1", name))
end
end
---------------------------------------------------------------------------------------------------
-- Show results
---------------------------------------------------------------------------------------------------
show_info(S("Number of .mts files converted: @1", mts_count))
show_info(S("Number of Lua schematics converted: @1", schem_count))
show_info(S("Number of conversion errors: @1", error_count))
if show_summary_flag then

33
schematics.lua Normal file
View File

@ -0,0 +1,33 @@
---------------------------------------------------------------------------------------------------
-- schemconvert mod by A S Lewis
-- Lieence: LGPL 2.1
---------------------------------------------------------------------------------------------------
-- You can add LUA schematics to this file
-- They will be processed together with the .mts files loaded from ../input
--
-- For some examples of LUA schematics, see the schematic_tables.txt file in minetest-game
--
-- Each LUA schematic should be added with a function call in the usual format:
--
-- schemconvert.add_schem("apple_tree", {
-- size = {x = 7, y = 8, z = 7},
-- data = {
-- _, _, _, _, _, _, _,
-- _, _, _, _, _, _, _,
-- _, _, _, _, _, _, _,
-- -- ...and so on
-- },
-- })
--
-- You could also write to the table directly, if you prefer:
--
-- schemconvert.schem_table["apple_tree"] = {
-- size = {x = 7, y = 8, z = 7},
-- data = {
-- _, _, _, _, _, _, _,
-- _, _, _, _, _, _, _,
-- _, _, _, _, _, _, _,
-- -- ...and so on
-- },
-- })

Binary file not shown.

Before

Width:  |  Height:  |  Size: 362 KiB

After

Width:  |  Height:  |  Size: 1.1 MiB