[Mod] Convert table schematic to .mts [0.2.0] [saveschems]

Post Reply
User avatar
paramat
Developer
Posts: 3658
Joined: Sun Oct 28, 2012 00:05
GitHub: paramat
IRC: paramat
Location: UK

[Mod] Convert table schematic to .mts [0.2.0] [saveschems]

by paramat » Post

For Minetest 0.4.13 and later
Depends default
Licenses: Code WTFPL. Schematics CC BY-SA

***** Note *****
This repo of mine is out of date and unmaintained, there is a much improved version at minetest-mods:
https://github.com/minetest-mods/saveschems

***** Note *****
The mod at the minetest-mods organisation has been superseded by my new mod https://github.com/paramat/mtgschems. This is also currently the official up-to-date storage location for all lua table schematics used in MTG. It also has a new conversion function that allows each schematic slice to be formatted upright instead of upside-down.
***** End note *****

Example of how to use the recently added 'minetest.serialize_schematic' function to convert a lua-table defined schematic to a .mts file saved in a mod folder.
Create your schematic in lua table form, edit the names used in the code below the table, then just run the mod in any world, when the mod loads at game startup the .mts files are written to the schematics folder.

If using globally force-placed schematics you will often need to make sure air nodes are not force-placed, otherwise they will eat chunks out of the terrain, do this by using "air" with prob1 = 0 (probability zero).

This uses the recently added 'per-node force-place' feature of Minetest schematic format v4. This is used for tree trunk nodes so they replace the leaves of any other close trees they pass through, without the tree's leaves also being force-placed and replacing trunk nodes of any other close trees. Until now close-packed trees would inevitably have chopped trunks.

The lua tables have been simplified by removing any optional parameters that have these default values:
param2 = 0 (normal rotation).
force_place = false.

The format of the lua tables is:
Vertical slices of the structure in order of increasing z (here labelled -2, -1, 0, 1, 2), composed of ..
Horizontal lines of nodes in order of increasing y, composed of ..
Nodes in order of increasing x.
Last edited by paramat on Sun Jul 15, 2018 23:30, edited 13 times in total.

User avatar
philipbenr
Member
Posts: 1688
Joined: Fri Jun 14, 2013 01:56
GitHub: philipbenr
IRC: philipbenr
In-game: robinspi
Location: United States

Re: [Mod] Convert table schematic to .mts [0.1.1] [saveschem

by philipbenr » Post

Nice and looks pretty simple

User avatar
TenPlus1
Member
Posts: 2775
Joined: Mon Jul 29, 2013 13:38
GitHub: tenplus1

Re: [Mod] Convert table schematic to .mts [0.1.1] [saveschem

by TenPlus1 » Post

Thanks Paramat, this'll save me 17kb for a banana tree... lol

User avatar
paramat
Developer
Posts: 3658
Joined: Sun Oct 28, 2012 00:05
GitHub: paramat
IRC: paramat
Location: UK

Re: [Mod] Convert table schematic to .mts [0.1.2] [saveschem

by paramat » Post

Updated.
When using force-placed schematics you will often need to make sure air nodes are not force-placed, otherwise they will eat chunks out of the terrain, do this by using "air" with param1 = 0 (probability zero).
Previously in 0.1.1 i was using "ignore" nodes to do this but this is no longer possible.

Centre slice of appletree as an example:

Code: Select all

			-- 0

			{name="air", param1=0, param2=0},
			{name="air", param1=0, param2=0},
			{name="default:tree", param1=255, param2=0},
			{name="air", param1=0, param2=0},
			{name="air", param1=0, param2=0},

			{name="air", param1=0, param2=0},
			{name="air", param1=0, param2=0},
			{name="default:tree", param1=255, param2=0},
			{name="air", param1=0, param2=0},
			{name="air", param1=0, param2=0},

			{name="air", param1=0, param2=0},
			{name="air", param1=0, param2=0},
			{name="default:tree", param1=255, param2=0},
			{name="air", param1=0, param2=0},
			{name="air", param1=0, param2=0},

			{name="air", param1=0, param2=0},
			{name="air", param1=0, param2=0},
			{name="default:tree", param1=255, param2=0},
			{name="air", param1=0, param2=0},
			{name="air", param1=0, param2=0},

			{name="default:leaves", param1=255, param2=0},
			{name="default:tree", param1=191, param2=16},
			{name="default:tree", param1=255, param2=0},
			{name="default:tree", param1=191, param2=12},
			{name="default:leaves", param1=255, param2=0},

			{name="default:leaves", param1=223, param2=0},
			{name="default:leaves", param1=255, param2=0},
			{name="default:tree", param1=191, param2=0},
			{name="default:leaves", param1=255, param2=0},
			{name="default:leaves", param1=223, param2=0},

			{name="air", param1=0, param2=0},
			{name="default:leaves", param1=223, param2=0},
			{name="default:leaves", param1=255, param2=0},
			{name="default:leaves", param1=223, param2=0},
			{name="air", param1=0, param2=0},

User avatar
paramat
Developer
Posts: 3658
Joined: Sun Oct 28, 2012 00:05
GitHub: paramat
IRC: paramat
Location: UK

Re: [Mod] Convert table schematic to .mts [0.1.3] [saveschem

by paramat » Post

Version 0.1.3 uses the recently added 'per-node force-place' feature of Minetest schematic format v4. This is used for tree trunk nodes so they replace the leaves of any other close trees they pass through, without the tree's leaves also being force-placed and replacing trunk nodes of any other close trees. Until now close-packed trees would inevitably have chopped trunks.
Note 'param1' can now be replaced by 'prob', both are supported.
The lua tables have been sinplified by removing any optional parameters that have these default values:
param2 = 0 (normal rotation).
force_place = false.

See https://github.com/minetest/minetest/bl ... i.txt#L781 from line 781
and https://github.com/minetest/minetest/bl ... .txt#L2201 from line 2201

User avatar
paramat
Developer
Posts: 3658
Joined: Sun Oct 28, 2012 00:05
GitHub: paramat
IRC: paramat
Location: UK

Re: [Mod] Convert table schematic to .mts [0.2.0] [saveschem

by paramat » Post

Version 0.2.0
Updated to match the 10 schematics used in latest Minetest Game, including the second set of tree schematics that grow from saplings, these do not have force-placed trunks to preserve valuable player-placed nodes, per-node force-place is used only to replace the sapling.

User avatar
Wuzzy
Member
Posts: 3962
Joined: Mon Sep 24, 2012 15:01
GitHub: Wuzzy2
IRC: Wuzzy
In-game: Wuzzy

Re: [Mod] Convert table schematic to .mts [0.2.0] [saveschem

by Wuzzy » Post

Is it possible to do the reverse operation, too?

I.e. converting a MTS file back to a Lua table.
My creations. I gladly accept bitcoins: 17fsUywHxeMHKG41UFfu34F1rAxZcrVoqH

bell07
Member
Posts: 587
Joined: Sun Sep 04, 2016 15:15
GitHub: bell07

Re: [Mod] Convert table schematic to .mts [0.2.0] [saveschem

by bell07 » Post

Wuzzy wrote:Is it possible to do the reverse operation, too?

I.e. converting a MTS file back to a Lua table.
Yes

The related code snipped from my schemlib mod:

Code: Select all

	-- Minetest Schematics
	if string.find(filename, '.mts',  -4) then
		local str = minetest.serialize_schematic(filename, "lua", {})
		if not str then
			dprint("error: could not open file \"" .. filename .. "\"")
			return
		end
		local schematic = loadstring(str.." return(schematic)")()
			--[[	schematic.yslice_prob = {{ypos = 0,prob = 254},..}
					schematic.size = { y = 18,x = 10, z = 18},
					schematic.data = {{param2 = 2,name = "default:tree",prob = 254},..}
				]]

		-- analyze the file
		for i, ent in ipairs( schematic.data ) do
			if ent.name ~= "air" then
				local pos = {
						z = math.floor((i-1)/schematic.size.y/schematic.size.x),
						y = math.floor((i-1)/schematic.size.x) % schematic.size.y,
						x = (i-1) % schematic.size.x
					}
				local new_node = node.new(ent)
				self:add_node(pos, new_node)
				self:adjust_building_info(pos, new_node)
			end
		end

User avatar
paramat
Developer
Posts: 3658
Joined: Sun Oct 28, 2012 00:05
GitHub: paramat
IRC: paramat
Location: UK

Re: [Mod] Convert table schematic to .mts [0.2.0] [saveschem

by paramat » Post

Yes there's an API for .mts -> lua table, but it needs some code added around it.

Code: Select all

minetest.serialize_schematic()
This repo of mine is out of date and unmaintained, there is a much improved version at minetest-mods:
https://github.com/minetest-mods/saveschems

User avatar
paramat
Developer
Posts: 3658
Joined: Sun Oct 28, 2012 00:05
GitHub: paramat
IRC: paramat
Location: UK

Re: [Mod] Convert table schematic to .mts [0.2.0] [saveschem

by paramat » Post

The mod at the minetest-mods organisation has been superseded by my new mod https://github.com/paramat/mtgschems. This is also currently the official up-to-date storage location for all lua table schematics used in MTG. It also has a new conversion function that allows each schematic slice to be formatted upright instead of upside-down.

Post Reply

Who is online

Users browsing this forum: racal and 2 guests