Organized code.
parent
39e1e3781c
commit
555230d22c
|
@ -0,0 +1,4 @@
|
|||
The first three inventory slots determine how this tool behaves.
|
||||
Slot one, two and three determine X, Y and Z dimensions, respectively.
|
||||
Shift + Left click sets which nodes to replace, and Shift + Right click sets which nodes to replace it with. You can select air by clicking anywhere there isn't a block.
|
||||
Left-clicking replaces nodes, right-clicking adds nodes.
|
|
@ -0,0 +1,74 @@
|
|||
multiplacer.axis_place = function(stack, player, pointed_thing, mode)
|
||||
if( player == nil or pointed_thing == nil) then
|
||||
return nil;
|
||||
end
|
||||
|
||||
local has, missing = minetest.check_player_privs(player, {multiplacer = true})
|
||||
if not has then
|
||||
if missing["multiplacer"] then
|
||||
minetest.chat_send_player( player:get_player_name(), "Hey! You can't use this tool because you do not have the \"multiplacer\" privilege.");
|
||||
return nil;
|
||||
end
|
||||
end
|
||||
|
||||
local meta = stack:get_meta():to_table();
|
||||
local to_delete = meta["fields"]["multiplacer:delete"];
|
||||
local to_place = meta["fields"]["multiplacer:place"];
|
||||
if not to_delete or to_delete == "" then
|
||||
to_delete = "default:air 0 0"
|
||||
end
|
||||
if not to_place or to_place == "" then
|
||||
to_place = "default:dirt 0 0"
|
||||
end
|
||||
to_delete = to_delete:split(" ");
|
||||
if( #to_delete < 3 ) then
|
||||
to_delete[2] = 0;
|
||||
to_delete[3] = 0;
|
||||
end
|
||||
to_place = to_place:split(" ");
|
||||
if( #to_place < 3 ) then
|
||||
to_place[2] = 0;
|
||||
to_place[3] = 0;
|
||||
end
|
||||
|
||||
local inv = player:get_inventory();
|
||||
local w = inv:get_stack("main", 1):get_count();
|
||||
local h = inv:get_stack("main", 2):get_count();
|
||||
local l = inv:get_stack("main", 3):get_count();
|
||||
local blocks_placed = 0;
|
||||
|
||||
if( pointed_thing.type ~= "node" ) then
|
||||
minetest.chat_send_player(player:get_player_name(), " Error: No node selected.");
|
||||
return nil;
|
||||
end
|
||||
|
||||
local pos
|
||||
local look_dir = player:get_look_dir()
|
||||
local yaw = player:get_look_horizontal()
|
||||
local start = minetest.get_pointed_thing_position(pointed_thing, mode);
|
||||
local axis_dir = {x=look_dir.x/math.abs(look_dir.x),y=1,z=look_dir.z/math.abs(look_dir.z)}
|
||||
for ih = 0, h-1 do
|
||||
for iw = 0, w-1 do
|
||||
for il = 0, l-1 do
|
||||
if yaw < math.pi/4 or yaw > 7*math.pi/4 or (yaw > 3*math.pi/4 and yaw < 5*math.pi/4) then
|
||||
pos = add_3dx2(start, mul_3dx2(axis_dir, {x=il, y=ih, z=iw}))
|
||||
else
|
||||
pos = add_3dx2(start, mul_3dx2(axis_dir, {x=iw, y=ih, z=il}))
|
||||
end
|
||||
local node = minetest.get_node_or_nil(pos)
|
||||
if not minetest.is_protected(pos, player:get_player_name()) then
|
||||
if node ~= nil then
|
||||
if node.name == to_delete[1] then --and node.param1 == to_delete[2] and node.param2 == to_delete[3]
|
||||
minetest.set_node( pos, { name = to_place[1], param1 = to_place[2], param2 = to_place[3] } );
|
||||
end
|
||||
end
|
||||
end
|
||||
blocks_placed = blocks_placed + 1;
|
||||
if blocks_placed > multiplacer.max_blocks and multiplacer.max_blocks ~= 0 then
|
||||
return nil;
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return nil;
|
||||
end
|
|
@ -0,0 +1,11 @@
|
|||
add_3dx2 = function(a,b)
|
||||
return {x=a.x+b.x,y=a.y+b.y,z=a.z+b.z}
|
||||
end
|
||||
|
||||
sub_3dx2 = function(a,b)
|
||||
return {x=a.x-b.x,y=a.y-b.y,z=a.z-b.z}
|
||||
end
|
||||
|
||||
mul_3dx2 = function(a,b)
|
||||
return {x=a.x*b.x,y=a.y*b.y,z=a.z*b.z}
|
||||
end
|
|
@ -0,0 +1,17 @@
|
|||
multiplacer.get_node = function(player, pointed_thing)
|
||||
local pos = minetest.get_pointed_thing_position(pointed_thing, false);
|
||||
local node;
|
||||
if pointed_thing.type == "node" then
|
||||
node = minetest.get_node_or_nil(pos);
|
||||
end
|
||||
local block = "air"..' '.."0"..' '.."0";
|
||||
if( node ~= nil and node.name ) then
|
||||
block = node.name..' '..node.param1..' '..node.param2;
|
||||
end
|
||||
return block
|
||||
end
|
||||
|
||||
multiplacer.get_node_pos = function(pointed_thing)
|
||||
local pos = minetest.get_pointed_thing_position(pointed_thing, false);
|
||||
return pos
|
||||
end
|
105
init.lua
105
init.lua
|
@ -1,102 +1,9 @@
|
|||
local add_3dx2 = function(a,b)
|
||||
return {x=a.x+b.x,y=a.y+b.y,z=a.z+b.z}
|
||||
end
|
||||
|
||||
local mul_3dx2 = function(a,b)
|
||||
return {x=a.x*b.x,y=a.y*b.y,z=a.z*b.z}
|
||||
end
|
||||
|
||||
multiplacer = {}
|
||||
|
||||
multiplacer.max_blocks = 0 --set to 0 for unlimited
|
||||
|
||||
multiplacer.activate = function(stack, player, pointed_thing, mode)
|
||||
if( player == nil or pointed_thing == nil) then
|
||||
return nil;
|
||||
end
|
||||
|
||||
local has, missing = minetest.check_player_privs(player, {multiplacer = true})
|
||||
if not has then
|
||||
if missing["multiplacer"] then
|
||||
minetest.chat_send_player( player:get_player_name(), "Hey! You can't use this tool because you do not have the \"multiplacer\" privilege.");
|
||||
return nil;
|
||||
end
|
||||
end
|
||||
|
||||
local meta = stack:get_meta():to_table();
|
||||
local to_delete = meta["fields"]["multiplacer:delete"];
|
||||
local to_place = meta["fields"]["multiplacer:place"];
|
||||
if not to_delete or to_delete == "" then
|
||||
to_delete = "default:air 0 0"
|
||||
end
|
||||
if not to_place or to_place == "" then
|
||||
to_place = "default:dirt 0 0"
|
||||
end
|
||||
to_delete = to_delete:split(" ");
|
||||
if( #to_delete < 3 ) then
|
||||
to_delete[2] = 0;
|
||||
to_delete[3] = 0;
|
||||
end
|
||||
to_place = to_place:split(" ");
|
||||
if( #to_place < 3 ) then
|
||||
to_place[2] = 0;
|
||||
to_place[3] = 0;
|
||||
end
|
||||
|
||||
local inv = player:get_inventory();
|
||||
local w = inv:get_stack("main", 1):get_count();
|
||||
local h = inv:get_stack("main", 2):get_count();
|
||||
local l = inv:get_stack("main", 3):get_count();
|
||||
local blocks_placed = 0;
|
||||
|
||||
if( pointed_thing.type ~= "node" ) then
|
||||
minetest.chat_send_player(player:get_player_name(), " Error: No node selected.");
|
||||
return nil;
|
||||
end
|
||||
|
||||
local pos
|
||||
local look_dir = player:get_look_dir()
|
||||
local yaw = player:get_look_horizontal()
|
||||
local start = minetest.get_pointed_thing_position(pointed_thing, mode);
|
||||
local axis_dir = {x=look_dir.x/math.abs(look_dir.x),y=1,z=look_dir.z/math.abs(look_dir.z)}
|
||||
for ih = 0, h-1 do
|
||||
for iw = 0, w-1 do
|
||||
for il = 0, l-1 do
|
||||
if yaw < math.pi/4 or yaw > 7*math.pi/4 or (yaw > 3*math.pi/4 and yaw < 5*math.pi/4) then
|
||||
pos = add_3dx2(start, mul_3dx2(axis_dir, {x=il, y=ih, z=iw}))
|
||||
else
|
||||
pos = add_3dx2(start, mul_3dx2(axis_dir, {x=iw, y=ih, z=il}))
|
||||
end
|
||||
local node = minetest.get_node_or_nil(pos)
|
||||
if not minetest.is_protected(pos, player:get_player_name()) then
|
||||
if node ~= nil then
|
||||
if node.name == to_delete[1] then --and node.param1 == to_delete[2] and node.param2 == to_delete[3]
|
||||
minetest.set_node( pos, { name = to_place[1], param1 = to_place[2], param2 = to_place[3] } );
|
||||
end
|
||||
end
|
||||
end
|
||||
blocks_placed = blocks_placed + 1;
|
||||
if blocks_placed > multiplacer.max_blocks and multiplacer.max_blocks ~= 0 then
|
||||
return nil;
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return nil;
|
||||
end
|
||||
|
||||
multiplacer.get_node = function(player, pointed_thing)
|
||||
local pos = minetest.get_pointed_thing_position(pointed_thing, false);
|
||||
local node;
|
||||
if pointed_thing.type == "node" then
|
||||
node = minetest.get_node_or_nil(pos);
|
||||
end
|
||||
local block = "air"..' '.."0"..' '.."0";
|
||||
if( node ~= nil and node.name ) then
|
||||
block = node.name..' '..node.param1..' '..node.param2;
|
||||
end
|
||||
return block
|
||||
end
|
||||
dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/axis_place.lua");
|
||||
dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/basic_3d_math.lua");
|
||||
dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/functions.lua");
|
||||
dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/settings.lua");
|
||||
|
||||
minetest.register_privilege("multiplacer", {
|
||||
description = "Can use the multiplacer tools",
|
||||
|
@ -137,7 +44,7 @@ minetest.register_tool("multiplacer:multiplacer", {
|
|||
minetest.chat_send_player(player:get_player_name(), "Multiplacer tool set to place '"..block.."' blocks.");
|
||||
return stack;
|
||||
end
|
||||
return multiplacer.activate(stack, player, pointed_thing, true);
|
||||
return multiplacer.axis_place(stack, player, pointed_thing, true);
|
||||
end,
|
||||
on_use = function(stack, player, pointed_thing)
|
||||
local keys = player:get_player_control()
|
||||
|
@ -148,6 +55,6 @@ minetest.register_tool("multiplacer:multiplacer", {
|
|||
minetest.chat_send_player(player:get_player_name(), "Multiplacer tool set to delete '"..block.."' blocks.");
|
||||
return stack;
|
||||
end
|
||||
return multiplacer.activate(stack, player, pointed_thing, false);
|
||||
return multiplacer.axis_place(stack, player, pointed_thing, false);
|
||||
end,
|
||||
})
|
|
@ -0,0 +1 @@
|
|||
multiplacer.max_blocks = 0 -- Sets the maximum number of blocks that will be placed. Set to 0 for unlimited.
|
Loading…
Reference in New Issue