Compare commits

...

19 Commits

Author SHA1 Message Date
23de5fe9c6 Merge remote-tracking branch 'upstream/master' 2024-09-15 09:45:00 +02:00
9bd0b2248b added support for MineClone2; show owner of mobs; better group handling for inspector 2024-05-08 23:30:42 +02:00
00a1c21b3e Merge remote-tracking branch 'upstream/master' 2021-11-01 10:32:37 +01:00
d69fcb3198 improved description of modes 2021-10-09 14:41:23 +02:00
c0fa835364 Merge remote-tracking branch 'upstream/master' 2021-10-04 21:45:45 +02:00
e4464c3faa corrected typo 2021-10-03 13:56:47 +02:00
ef93ee2494 added mod.conf 2021-10-03 13:49:57 +02:00
4d3dd7b51e allow mode switches 2021-10-03 13:17:01 +02:00
aaa9bdd35a improved readability 2021-10-02 15:19:52 +02:00
4f028a1701 removed old obsolete lines which where already commented out 2021-10-02 14:48:04 +02:00
76fcea22e0 removed deprecated functions get_/set_metadata and renamed metadata to pattern 2021-10-02 14:46:15 +02:00
9ef8940bc4 added a history and a formspec to select stored patterns from said history 2021-10-02 14:21:42 +02:00
fbad089ff1 added newlines in description 2021-10-02 12:04:56 +02:00
fe89553780 modified PR from coil0: used buildable_to instead of node names in order to determine if replacing can progress even if digging of the node doesn't work 2021-09-30 00:18:34 +02:00
889885049e Merge branch 'master' of https://github.com/Sokomine/replacer
merged PR on github while working locally
2021-09-29 23:52:45 +02:00
a81ce905cd Merge pull request #8 from acmgit/master
Fixed Warnings for global Vars.
2021-09-29 23:51:46 +02:00
62d7ad06bb the replacers' description now shows which node is stored, and the description is more human readable 2021-09-29 22:37:05 +02:00
c030a3beb8 aux1 key works now as well instead of only sneak key for storing a new pattern 2021-09-29 20:19:28 +02:00
c4fed6eb5c Fixed Warnings for global Vars. 2019-02-28 15:48:29 +01:00
7 changed files with 572 additions and 74 deletions

View File

@ -11,7 +11,7 @@ Or just use /giveme replacer:replacer
Usage: Right-click on a node of that type you want to replace other nodes with.
Left-click (normal usage) on any nodes you want to replace with the type you previously right-clicked on.
SHIFT-Right-click in order to store a new pattern.
Sneak-Right-click (alternatively: aux1-Right-click) in order to store a new pattern.
When in creative mode, the node will just be replaced. Your inventory will not be changed.

View File

@ -1,2 +0,0 @@
default?
dye?

206
fs_history.lua Normal file
View File

@ -0,0 +1,206 @@
-- apart from the history, the formspec also handles mode switches
-- how many patterns are stored in the history? those don't take up much space,
-- but a too long list might not be overly helpful for the players either
replacer.max_hist_size = 20
-- turn stored pattern string (<node_name> <param1> <param2>) into something readable by human beeings
replacer.human_readable_pattern = function(pattern)
if(not(pattern)) then
return "(nothing)"
end
-- data is stored in the form "<nodename> <param1> <param2>"
local parts = string.split(pattern, " ")
if(not(parts) or #parts < 3) then
return "(corrupted data)"
end
local node_name = parts[1]
local param2 = parts[3]
local def = minetest.registered_nodes[ node_name ]
if(not(def)) then
return "(unknown node)"
end
local text = "'"..tostring(def.description or "- no description -").."'"
if(not(def.description) or def.description == "") then
text = "- no description -"
end
-- facedir is probably the most commonly used rotation variant
if( def.paramtype2 == "facedir"
or def.paramtype2 == "colorfacedir") then
local axis_names = {"y+ (Ground)", "z+ (North)", "z- (South)",
"x+ (East)", "x- (West)", "y- (Sky)"}
text = text.." Rotated: "..tostring(param2 % 4)..
" around axis: "..tostring( axis_names[ math.floor( (param2%24) / 4 ) + 1 ])
-- wallmounted is diffrent
elseif( def.paramtype2 == "wallmounted"
or def.paramtype2 == "colorwallmounted") then
local axis_names = {"y+ (Ground)", "y- (Sky)",
"z+ (North)", "z- (South)",
"x+ (East)", "x- (West)"}
text = text.." Mounted at wall: "..tostring( axis_names[ (param2 % 6)+ 1 ])
end
return text
end
-- set the replacer to a new pattern
replacer.set_to = function(player_name, pattern, player, itemstack)
if(not(player_name) or not(player) or not(itemstack)) then
return itemstack
end
-- fallback if nothing is given
if(not(pattern)) then
pattern = replacer.default_dirt.." 0 0"
end
local set_to = replacer.human_readable_pattern(pattern)
-- change the description of the tool so that it's easier to see which replacer (if you
-- have more than one in your inv) is set to which node
local meta = itemstack:get_meta()
-- actually store the new pattern
meta:set_string("pattern", pattern )
meta:set_string("description", "Node replacement tool set to:\n"..set_to..
"\n["..tostring(pattern).."]")
minetest.chat_send_player(player_name, "Node replacement tool set to: "..set_to..
"["..tostring(pattern).."].")
replacer.add_to_hist(player_name, pattern)
return itemstack -- nothing consumed but data changed
end
-- keep a history of stored patterns for each player (not for each replacer);
-- this history is not saved over server shutdown
replacer.add_to_hist = function(player_name, pattern)
if(not(player_name) or not(pattern) or pattern == "") then
return
end
if(not(replacer.history)) then
replacer.history = {}
end
if(not(replacer.history[ player_name ])) then
replacer.history[ player_name ] = {}
end
local index = table.indexof(replacer.history[ player_name ], pattern)
-- only add new entries; do not store duplicates
if(index and index > -1) then
return
end
-- remove the oldest entry
if(#replacer.history[ player_name ] >= replacer.max_hist_size) then
table.remove(replacer.history[ player_name ], 1)
end
table.insert(replacer.history[ player_name ], pattern)
end
-- show a formspec with a history of stored patterns to select from
replacer.get_formspec = function(player_name, current_pattern, player)
-- is the player in creative mode?
local in_creative_mode = (minetest.settings:get_bool("creative_mode")
or minetest.check_player_privs(player_name, {creative=true}))
-- count how many blocks of each type the player has in his inventory
local counted_inv = {}
if(not(in_creative_mode)) then
local inv_main = player:get_inventory():get_list("main")
for i, v in ipairs(inv_main) do
local stack_name = v:get_name()
if(not(counted_inv[ stack_name ])) then
counted_inv[ stack_name ] = 0
end
counted_inv[ stack_name ] = counted_inv[ stack_name ] + v:get_count()
end
end
-- find out which mode the player has currently selected
local current_mode = 1
if(replacer.user_mode and replacer.user_mode[ player_name ]) then
current_mode = table.indexof(replacer.mode_names, replacer.user_mode[ player_name ])
if(current_mode == -1) then
current_mode = 1
end
end
local formspec = "size[18,10]"..
"label[6,0;Node Replacement Tool Setup and History]"..
"button_exit[8,9.4;2,0.8;quit;Exit]"..
"label[0.2,8.5;Note: Selected mode and history are reset on server restart.\n"..
"Note: The selected mode is valid for *all* replacers you use. "..
"The stored pattern is valid for *this particular* replacer only.]"..
"label[0.2,0.6;Select mode: When replacing (punching, left-click) or "..
"placing (right-click) a block, ..]"..
"dropdown[0.2,1.0;17;select_mode;"..
table.concat(replacer.mode_descriptions, ",")..
";"..tostring(current_mode)..";]"..
"label[0.2,2.1;Click here to set the replacer to a pattern you have stored before:]"..
"tablecolumns[color;"..
"text,align=right,tooltip=Amount of nodes of this type left in your inventory:"..
";color;text,align=left,tooltip=Stored pattern:]"..
"table[0.2,2.5;17,6;replacer_history;"
-- make sure all variables exist and the current entry is stored
replacer.add_to_hist(player_name, current_pattern)
local hist_entries = {}
local selected = 1
for i, v in ipairs(replacer.history[ player_name ]) do
if(v == current_pattern) then
selected = i
end
local amount_left = "#00FF00,infinite supply:,#00FF00"
if(not(in_creative_mode)) then
-- which item are we looking for?
local parts = v:split(" ")
if(not(parts) or #parts<1) then
parts = {"does not exist"}
-- TODO: handle this in a more general way
elseif(parts[1] == "default:dirt_with_grass" or parts[1] == "mcl_core:dirt_with_grass") then
parts[1] = replacer.default_dirt
end
if(counted_inv[ parts[1] ]) then
amount_left = "#00FF00,"..tostring(counted_inv[ parts[1] ]).." available:"..
",#00FF00"
else
amount_left = "#FF0000,none left!,#CFCFCF"
end
end
hist_entries[ i ] = tostring(amount_left)..","..
minetest.formspec_escape(replacer.human_readable_pattern(v).." ["..v.."]")
end
return formspec..table.concat(hist_entries, ",")..";"..tostring(selected).."]"
end
-- the player has interacted with our formspec
minetest.register_on_player_receive_fields( function(player, formname, fields)
if(not(formname) or formname ~= "replacer:menu") then
return false
end
local player_name = player:get_player_name()
-- the player clicked on an entry in the history
if(fields and fields.replacer_history
and replacer.history and replacer.history[ player_name ]) then
-- find out which line it was
local selected = minetest.explode_table_event(fields.replacer_history)
if(selected and (selected.type == "CHG" or selected.type == "DLC")
and selected.row <= #replacer.history[ player_name ]) then
local itemstack = player:get_wielded_item()
itemstack = replacer.set_to(player_name,
replacer.history[ player_name ][ selected.row ],
player, itemstack)
player:set_wielded_item(itemstack)
return true
end
end
-- the player selected a mode
if(fields and fields.select_mode) then
local index = table.indexof(replacer.mode_descriptions,
minetest.formspec_escape(fields.select_mode))
if(index and index > -1 and replacer.mode_names[ index ]) then
replacer.user_mode[ player_name ] = replacer.mode_names[ index ]
end
end
return true
end)

125
init.lua
View File

@ -21,6 +21,12 @@
-- Version 3.0
-- Changelog:
-- 02.09.2021 * Added a history of stored patterns (not saved over server restart)
-- * Added a menu to select a history entry. It is accessable via AUX1 + left click.
-- * Removed deprecated functions get/set_metadata(..) and renamed metadata to pattern
-- 29.09.2021 * AUX1 key works now same as SNEAK key for storing new pattern (=easier when flying)
-- * The description of the tool now shows which pattern is stored
-- * The description of the stored pattern is more human readable
-- 09.12.2017 * Got rid of outdated minetest.env
-- * Fixed error in protection function.
-- * Fixed minor bugs.
@ -42,6 +48,8 @@ dofile(minetest.get_modpath("replacer").."/check_owner.lua");
replacer = {};
replacer.default_dirt = "default:dirt"
replacer.blacklist = {};
-- playing with tnt and creative building are usually contradictory
@ -58,6 +66,13 @@ replacer.blacklist[ "protector:protect2"] = true;
-- adds a tool for inspecting nodes and entities
dofile(minetest.get_modpath("replacer").."/inspect.lua");
-- adds a formspec with a history function (accessible with AUX1 + left click)
dofile(minetest.get_modpath("replacer").."/fs_history.lua");
-- adds support for the circular saw from moreblocks and similar nodes
-- and allows to replace the *material* while keeping shape - or vice versa
dofile(minetest.get_modpath("replacer").."/mode_of_replacement.lua");
minetest.register_tool( "replacer:replacer",
{
description = "Node replacement tool",
@ -81,7 +96,6 @@ minetest.register_tool( "replacer:replacer",
},
--]]
node_placement_prediction = nil,
metadata = "default:dirt", -- default replacement: common dirt
on_place = function(itemstack, placer, pointed_thing)
@ -93,8 +107,8 @@ minetest.register_tool( "replacer:replacer",
local keys=placer:get_player_control();
-- just place the stored node if now new one is to be selected
if( not( keys["sneak"] )) then
-- just place the stored node if no new one is to be selected
if( not( keys["sneak"] ) and not( keys["aux1"])) then
return replacer.replace( itemstack, placer, pointed_thing, true ); end
@ -104,24 +118,18 @@ minetest.register_tool( "replacer:replacer",
return nil;
end
local pos = minetest.get_pointed_thing_position( pointed_thing, false );
local pos = minetest.get_pointed_thing_position( pointed_thing, false ); -- node under
local node = minetest.get_node_or_nil( pos );
--minetest.chat_send_player( name, " Target node: "..minetest.serialize( node ).." at pos "..minetest.serialize( pos )..".");
local metadata = "default:dirt 0 0";
local pattern = replacer.default_dirt.." 0 0";
if( node ~= nil and node.name ) then
metadata = node.name..' '..node.param1..' '..node.param2;
pattern = node.name..' '..node.param1..' '..node.param2;
end
itemstack:set_metadata( metadata );
minetest.chat_send_player( name, "Node replacement tool set to: '"..metadata.."'.");
return itemstack; -- nothing consumed but data changed
return replacer.set_to(name, pattern, placer, itemstack) -- nothing consumed but data changed
end,
-- on_drop = func(itemstack, dropper, pos),
on_use = function(itemstack, user, pointed_thing)
return replacer.replace( itemstack, user, pointed_thing, false );
@ -135,7 +143,6 @@ replacer.replace = function( itemstack, user, pointed_thing, mode )
return nil;
end
local name = user:get_player_name();
--minetest.chat_send_player( name, "You USED this on "..minetest.serialize( pointed_thing )..".");
if( pointed_thing.type ~= "node" ) then
minetest.chat_send_player( name, " Error: No node.");
@ -145,8 +152,6 @@ replacer.replace = function( itemstack, user, pointed_thing, mode )
local pos = minetest.get_pointed_thing_position( pointed_thing, mode );
local node = minetest.get_node_or_nil( pos );
--minetest.chat_send_player( name, " Target node: "..minetest.serialize( node ).." at pos "..minetest.serialize( pos )..".");
if( node == nil ) then
minetest.chat_send_player( name, "Error: Target node not yet loaded. Please wait a moment for the server to catch up.");
@ -154,15 +159,22 @@ replacer.replace = function( itemstack, user, pointed_thing, mode )
end
local item = itemstack:to_table();
local meta = itemstack:get_meta()
local pattern = meta:get_string("pattern")
-- make sure it is defined
if( not( item[ "metadata"] ) or item["metadata"]=="" ) then
item["metadata"] = "default:dirt 0 0";
if(not(pattern) or pattern == "") then
pattern = replacer.default_dirt.." 0 0";
end
local keys=user:get_player_control();
if( keys["aux1"]) then
minetest.show_formspec(name, "replacer:menu", replacer.get_formspec(name, pattern, user))
return nil
end
-- regain information about nodename, param1 and param2
local daten = item[ "metadata"]:split( " " );
local daten = pattern:split( " " );
-- the old format stored only the node name
if( #daten < 3 ) then
daten[2] = 0;
@ -175,6 +187,12 @@ replacer.replace = function( itemstack, user, pointed_thing, mode )
return nil;
end
local daten = replacer.get_new_node_data(node, daten, name)
-- nothing to replace
if(not(daten)) then
return
end
if( node.name and node.name ~= "" and replacer.blacklist[ node.name ]) then
minetest.chat_send_player( name, "Replacing blocks of the type '"..( node.name or "?" )..
"' is not allowed on this server. Replacement failed.");
@ -198,16 +216,15 @@ replacer.replace = function( itemstack, user, pointed_thing, mode )
return nil;
end
-- in survival mode, the player has to provide the node he wants to place
if( not(minetest.settings:get_bool("creative_mode") )
and not( minetest.check_player_privs( name, {creative=true}))) then
-- players usually don't carry dirt_with_grass around; it's safe to assume normal dirt here
-- fortunately, dirt and dirt_with_grass does not make use of rotation
if( daten[1] == "default:dirt_with_grass" ) then
daten[1] = "default:dirt";
item["metadata"] = "default:dirt 0 0";
if( daten[1] == "default:dirt_with_grass" or daten[1] == "mcl_core:dirt_with_grass") then
daten[1] = replacer.default_dirt
pattern = replacer.default_dirt.." 0 0";
end
-- does the player carry at least one of the desired nodes with him?
@ -219,20 +236,9 @@ replacer.replace = function( itemstack, user, pointed_thing, mode )
end
-- give the player the item by simulating digging if possible
if( node.name ~= "air"
and node.name ~= "ignore"
and node.name ~= "default:lava_source"
and node.name ~= "default:lava_flowing"
and node.name ~= "default:river_water_source"
and node.name ~= "default:river_water_flowing"
and node.name ~= "default:water_source"
and node.name ~= "default:water_flowing"
and node.name ~= "nalc:acid_source"
and node.name ~= "nalc:acid_flowing"
and node.name ~= "nalc:sand_source"
and node.name ~= "nalc:sand_flowing" ) then
if( node.name ~= "air"
and node.name ~= "ignore") then
minetest.node_dig( pos, node, user );
@ -240,33 +246,46 @@ replacer.replace = function( itemstack, user, pointed_thing, mode )
if( not( digged_node )
or digged_node.name == node.name ) then
minetest.chat_send_player( name, "Replacing '"..( node.name or "air" ).."' with '"..( item[ "metadata"] or "?" ).."' failed. Unable to remove old node.");
return nil;
-- some nodes - like liquids - cannot be digged. but they are buildable_to and
-- thus can be replaced
local node_def = minetest.registered_nodes[node.name]
if(not(node_def) or not(node_def.buildable_to)) then
minetest.chat_send_player( name, "Replacing '"..( node.name or "air" ).."' with '"..( pattern or "?" ).."' failed. Unable to remove old node.");
return nil;
end
end
end
-- consume the item
user:get_inventory():remove_item("main", daten[1].." 1");
--user:get_inventory():add_item( "main", node.name.." 1");
end
--minetest.chat_send_player( name, "Replacing node '"..( node.name or "air" ).."' with '"..( item[ "metadata"] or "?" ).."'.");
--minetest.place_node( pos, { name = item[ "metadata" ] } );
minetest.add_node( pos, { name = daten[1], param1 = daten[2], param2 = daten[3] } );
return nil; -- no item shall be removed from inventory
end
minetest.register_craft({
output = 'replacer:replacer',
recipe = {
{ 'default:chest', '', '' },
{ '', 'default:stick', '' },
{ '', '', 'default:chest' },
}
})
-- do the exception for MineClone first...
if(minetest.registered_nodes["mcl_chests:chest"]) then
replacer.default_dirt = "mcl_core:dirt"
minetest.register_craft({
output = 'replacer:replacer',
recipe = {
{ 'mcl_chests:chest', '', '' },
{ '', 'mcl_core:stick', '' },
{ '', '', 'mcl_chests:chest' },
}
})
-- then the normal receipe
elseif(minetest.registered_nodes["default:chest"]) then
minetest.register_craft({
output = 'replacer:replacer',
recipe = {
{ 'default:chest', '', '' },
{ '', 'default:stick', '' },
{ '', '', 'default:chest' },
}
})
end
minetest.log("action", "[replacer] loaded.")

View File

@ -7,14 +7,18 @@ if( minetest.get_modpath("trees")
and minetest.get_modpath("instruments")
and minetest.get_modpath("anvil")
and minetest.get_modpath("scribing_table")) then
replacer.image_replacements[ "group:planks" ] = "trees:pine_planks";
replacer.image_replacements[ "group:plank" ] = "trees:pine_plank";
replacer.image_replacements[ "group:wood" ] = "trees:pine_planks";
replacer.image_replacements[ "group:tree" ] = "trees:pine_log";
replacer.image_replacements[ "group:sapling"] = "trees:pine_sapling";
replacer.image_replacements[ "group:leaves" ] = "trees:pine_leaves";
-- replacer.image_replacements[ "group:planks" ] = "trees:pine_planks";
-- replacer.image_replacements[ "group:plank" ] = "trees:pine_plank";
-- replacer.image_replacements[ "group:wood" ] = "trees:pine_planks";
-- replacer.image_replacements[ "group:tree" ] = "trees:pine_log";
-- replacer.image_replacements[ "group:sapling"] = "trees:pine_sapling";
-- replacer.image_replacements[ "group:leaves" ] = "trees:pine_leaves";
replacer.image_replacements[ "default:furnace" ] = "oven:oven";
replacer.image_replacements[ "default:furnace_active" ] = "oven:oven_active";
-- MineClone
elseif(minetest.registered_items["mcl_furnaces:furnace"]) then
replacer.image_replacements[ "default:furnace" ] = "mcl_furnaces:furnace"
replacer.image_replacements[ "default:furnace_active" ] = "mcl_furnaces:furnace_active"
end
minetest.register_tool( "replacer:inspect",
@ -58,7 +62,7 @@ replacer.inspect = function( itemstack, user, pointed_thing, mode, show_receipe
end
local name = user:get_player_name();
local keys = user:get_player_control();
if( keys["sneak"] ) then
if( keys["sneak"] or keys["aux1"]) then
show_receipe = true;
end
@ -86,6 +90,10 @@ replacer.inspect = function( itemstack, user, pointed_thing, mode, show_receipe
if( sdata.age ) then
text = text..', dropped '..tostring( math.floor( sdata.age/60 ))..' minutes ago';
end
-- show the owner of an entity
if( sdata.owner ) then
text = text..' (owned by '..minetest.formspec_escape(sdata.owner)..")"
end
end
else
text = text..'object \"'..tostring( ref:get_entity_name() )..'\"';
@ -164,14 +172,38 @@ if( minetest.get_modpath("dye") and dye and dye.basecolors) then
end
end
-- find the first known member of a group as an example/representative
replacer.get_first_group_member = function(group_name)
for k,v in pairs(minetest.registered_items) do
if(v and v.groups and v.groups[group_name] and v.groups[group_name] > 0) then
return k
end
end
-- nothing found
return group_name
end
replacer.image_button_link = function( stack_string )
local group = '';
if( replacer.image_replacements[ stack_string ] ) then
stack_string = replacer.image_replacements[ stack_string ];
end
if( replacer.group_placeholder[ stack_string ] ) then
replacer.group_placeholder[ 'group:wood_slab'] = 'stairs:slab_wood';
-- if it is a group
if( string.sub(stack_string, 1, 6) == "group:"
-- ..and there is no known member of that group yet
and (not(replacer.group_placeholder[ stack_string ])
-- or the known member does not exist
or not(minetest.registered_items[ replacer.group_placeholder[ stack_string ]]))) then
-- find and store a suitable group member as example
replacer.group_placeholder[ stack_string ] = replacer.get_first_group_member(string.sub(stack_string, 7))
end
if(replacer.group_placeholder[ stack_string ] ) then
group = 'Group\n'..minetest.formspec_escape(string.sub(stack_string, 7))
stack_string = replacer.group_placeholder[ stack_string ];
group = 'G';
end
-- TODO: show information about other groups not handled above
local stack = ItemStack( stack_string );
@ -299,9 +331,9 @@ replacer.inspect_show_crafting = function( name, node_name, fields )
-- provide additional information regarding the node in particular that has been inspected
if( fields.pos ) then
formspec = formspec.."label[0.0,0.3;Located at "..
minetest.formspec_escape( minetest.pos_to_string( fields.pos ));
minetest.formspec_escape( minetest.pos_to_string( fields.pos )).."]"
if( fields.param2 ) then
formspec = formspec.." with param2="..tostring( fields.param2 );
formspec = formspec.."label[0.0,0.6;with param2="..tostring( fields.param2 )
end
if( fields.light ) then
formspec = formspec.." and receiving "..tostring( fields.light ).." light";
@ -405,10 +437,22 @@ end
minetest.register_on_player_receive_fields( replacer.form_input_handler );
minetest.register_craft({
output = 'replacer:inspect',
recipe = {
{ 'default:torch' },
{ 'default:stick' },
}
})
-- do the exception for MineClone first...
if(minetest.registered_items["mcl_core:stick"]) then
minetest.register_craft({
output = 'replacer:inspect',
recipe = {
{ 'mcl_torches:torch' },
{ 'mcl_core:stick' },
}
})
-- ..then the normal version
elseif(minetest.registered_nodes["default:torch"]) then
minetest.register_craft({
output = 'replacer:inspect',
recipe = {
{ 'default:torch' },
{ 'default:stick' },
}
})
end

5
mod.conf Normal file
View File

@ -0,0 +1,5 @@
name = replacer
description = Essential tool for builders. Allows to store material and rotation and place more blocks the same way without having to turn them all individually with the screwdriver.
optional_depends = default, dye, moreblocks, stairs, stairsplus, mcl_core, mcl_chests
author = Sokomine
title = Replacer - Node Replace Tool

226
mode_of_replacement.lua Normal file
View File

@ -0,0 +1,226 @@
-- store for each user which mode the user has selected
replacer.user_mode = {}
-- descriptions for the dropdown menu (accessible via AUX1 + left-click)
replacer.mode_descriptions = {
"[ normal ] replace material, shape and orientation according to the stored pattern",
"[ material ] replace the material only (if possible), while keeping shape and orientation",
"[ shape ] replace shape and orientation (if possible), while keeping the material"}
-- internal names for the above modes (will be stored in replacer.user_mode[ player_name ])
replacer.mode_names = {"normal", "material", "shape"}
-- make sure all mode descriptions are properly escaped for the dropdown menu
for i, v in ipairs(replacer.mode_descriptions) do
replacer.mode_descriptions[i] = minetest.formspec_escape(v)
end
-- sometimes some few node names for on or two materials do not follow the
-- pattern of node names for that type; offer a way to translate them here
replacer.node_name_alternatives = {}
replacer.node_name_alternatives[ "default:mese_post_light_wood" ] = "default:mese_post_light"
replacer.node_name_alternatives[ "walls:desert_cobble" ] = "walls:desertcobble"
-- support for the circular saw; table contains prefixes as indices plus
-- a list of known suffixes for that prefix
replacer.saw_prefixes = {}
-- populate replacer.saw_prefixes
if(minetest.global_exists("circular_saw") and circular_saw.names) then
-- this is fixed for the saw and does not depend on which actual nodes are
-- registered for it later on;
-- we build this table replacer.saw_prefixes for faster lookup because
-- many shapes share the same prefix
for i, v in ipairs(circular_saw.names) do
local prefix = v[1].."_"
if(not(replacer.saw_prefixes[ prefix ])) then
replacer.saw_prefixes[ prefix ] = {v[2]}
else
table.insert(replacer.saw_prefixes[ prefix ], v[2])
end
end
end
-- functions
-- does node_name match any name from the circular saw?
-- or is it a node from the cnc machine?
-- and if so, which material is it?
-- returns: {created_by_machine, source node name, mod_name, prefix, material, suffix}
-- (the last four form the name of the node)
replacer.identify_shape_and_material = function(full_node_name)
local parts = full_node_name:split(":")
if(not(parts) or #parts < 2) then
return nil
end
local mod_name = parts[1]
local node_name = parts[2]
-- it might be a full block - or any other drawtype not really covered here
local def = minetest.registered_nodes[ full_node_name ]
if(def and def.drawtype and def.drawtype == "normal") then
return {"normal", full_node_name,
mod_name, "", node_name, ""}
end
-- a wooden fence rail
local prefix = "fence_rail_"
if(mod_name == "default" and node_name:sub(1, #prefix) == prefix) then
local material = node_name:sub(#prefix + 1)
return {"fence_rail", "default:"..material, "default", prefix, material, ""}
end
-- a wooden fence
prefix = "fence_"
if(mod_name == "default" and node_name:sub(1, #prefix) == prefix) then
local material = node_name:sub(#prefix + 1)
return {"fence", "default:"..material, "default", prefix, material, ""}
end
-- gates come in diffrent wood types as well
prefix = "gate_"
if(mod_name == "doors" and node_name:sub(1, #prefix) == prefix) then
local gate_suffixes = {"_open", "_closed"}
for i, suffix in ipairs(gate_suffixes) do
if(node_name:sub(-#suffix) == suffix) then
local material = node_name:sub(#prefix+1, -#suffix-1)
return {"gate", "default:"..material, "doors", prefix, material, suffix}
end
end
end
-- a mese post (comes in diffrent wood types)
prefix = "mese_post_light_"
if(full_node_name == "default:mese_post_light") then
return {"mese_post", "default:wood", "default", "mese_post_light_", "", ""}
elseif(mod_name == "default" and node_name:sub(1, #prefix) == prefix) then
local material = node_name:sub(#prefix + 1)
return {"mese_post", "default:"..material, "default", prefix, material, ""}
end
-- walls (usually made out of stone)
if(full_node_name == "walls:cobble") then
return {"walls", "default:cobble", "walls", "", "cobble", ""}
elseif(full_node_name == "walls:mossycobble") then
return {"walls", "default:mossycobble", "walls", "", "mossycobble", ""}
elseif(full_node_name == "walls:desertcobble") then
return {"walls", "default:desert_cobble", "walls", "", "desert_cobble", ""}
end
-- it might be a regular stair (or similar node) from MinetestGame's stair mod
if(mod_name == "stairs") then
-- stair_inner_ and stair_outer_ need to be checked before stair_ is checked
-- because they are more specific
local stair_prefixes = {"stair_inner_", "stair_outer_", "stair_", "slab_"}
for i, prefix in ipairs(stair_prefixes) do
if(node_name:sub(1,#prefix) == prefix) then
local material = node_name:sub(#prefix+1)
return {"stairs", "default:"..material,
mod_name, prefix, material, ""}
end
end
return nil
end
-- check if we are dealing with a node from the circular saw from moreblocks
for prefix, suffixes in pairs(replacer.saw_prefixes) do
-- the prefix matches; does any suffix match?
if(node_name:sub(1, #prefix) == prefix) then
for i, suffix in ipairs(suffixes) do
if(suffix == "" or node_name:sub(-#suffix) == suffix) then
local material = node_name:sub(#prefix + 1, -#suffix-1)
for m_name, m_parts in pairs(circular_saw.known_nodes) do
if(m_parts[2] == material) then
return {"circular_saw", m_name,
m_parts[1], prefix, material, suffix}
end
end
end
end
-- no need to check the other prefixes; they won't match either
return ""
end
end
-- check if we are dealing with a node from the cnc machine
if(minetest.global_exists("technic_cnc") and technic_cnc.programs) then
for i, data in ipairs(technic_cnc.programs) do
if(full_node_name:sub(-#data.suffix-1) == "_"..data.suffix) then
local source_node = full_node_name:sub(1,-#data.suffix-2)
local p = source_node:split(":")
return {"tecnic_cnc", source_node,
p[1], "", p[2], "_"..data.suffix}
end
end
end
-- TODO: pkarcs, mymillworks etc. mods, pillars, castle, ..
return nil
end
-- try to convert the old node into the desired new node
-- the mode is stored in replacer.user_mode[ player_name ] (fallback: normal)
replacer.get_new_node_data = function(old_node, stored_pattern, player_name)
if(not(old_node) or not(stored_pattern) or not(old_node.name)) then
return nil
end
-- normal mode of operation: replace material, shape and orientation
if(not(replacer.user_mode)
or not(replacer.user_mode[ player_name ])
or replacer.user_mode[ player_name ] == "normal") then
return stored_pattern
end
-- what type of node does the stored pattern represent?
local new_data = replacer.identify_shape_and_material(stored_pattern[1])
-- if the type of the stored pattern cannot be identified, then abort here
if(not(new_data)) then
return nil
end
local old_data = replacer.identify_shape_and_material(old_node.name)
-- if the type of the node that is to be replaced cannot be identified, then abort here
if(not(old_data)) then
return nil
end
-- replace material, but keep shape and orientation
if(replacer.user_mode[ player_name ] == "material") then
-- now try to replace the *material* of the old node while keeping prefix and postfix
local new_name = old_data[3]..":"..old_data[4]..new_data[5]..old_data[6]
-- handle some exceptions like mese post light wood and desert_cobble walls
if(not(minetest.registered_nodes[ new_name ])
and replacer.node_name_alternatives[ new_name ]) then
new_name = replacer.node_name_alternatives[ new_name ]
end
-- perhaps we need to change the mod name to the new material as well
if(not(minetest.registered_nodes[ new_name ])) then
new_name = new_data[3]..":"..old_data[4]..new_data[5]..old_data[6]
end
-- if the node still doesn't exist: give up
if(not(minetest.registered_nodes[ new_name ])) then
return nil
end
-- keep param1 and param2, but change the node name
return {new_name, old_node.param1, old_node.param2}
-- replace shape and orientation, but keep material
elseif(replacer.user_mode[ player_name ] == "shape") then
-- now try to replace the *material* of the old node while keeping prefix and postfix
local new_name = new_data[3]..":"..new_data[4]..old_data[5]..new_data[6]
-- handle some exceptions like mese post light wood and desert_cobble walls
if(not(minetest.registered_nodes[ new_name ])
and replacer.node_name_alternatives[ new_name ]) then
new_name = replacer.node_name_alternatives[ new_name ]
end
-- perhaps we need to change the mod name to the new material as well
if(not(minetest.registered_nodes[ new_name ])) then
new_name = old_data[3]..":"..new_data[4]..old_data[5]..new_data[6]
end
-- if the node still doesn't exist: give up
if(not(minetest.registered_nodes[ new_name ])) then
return nil
end
-- keep material, but change shape and orientation
return {new_name, stored_pattern[2], stored_pattern[3]}
end
end