forked from mtcontrib/markers
Merge pull request #7 from Kilarin/master
Calculate 3D cube from multiple markers instead of requiring a rectangle of 4
This commit is contained in:
commit
dc45b5ca23
139
init.lua
139
init.lua
@ -9,7 +9,7 @@ markers = {}
|
||||
markers.positions = {}
|
||||
|
||||
-- store the positions of that many markers for each player (until server restart)
|
||||
markers.MAX_MARKERS = 10;
|
||||
markers.MAX_MARKERS = 50;
|
||||
|
||||
-- the protection against digging of the marker by other players expires after this time
|
||||
markers.EXPIRE_AFTER = 60*60*24;
|
||||
@ -144,46 +144,6 @@ markers.marker_placed = function( pos, placer, itemstack )
|
||||
tostring( d )..' m '..dir_name..'.');
|
||||
end
|
||||
|
||||
-- do the last 4 markers placed form an area?
|
||||
if( #markers.positions[ name ] > 3 ) then
|
||||
local count_x_coord = {};
|
||||
local count_z_coord = {};
|
||||
local anz_x_coord = 0;
|
||||
local anz_z_coord = 0;
|
||||
local check_failed = 0;
|
||||
for i=0,3 do
|
||||
|
||||
local v = markers.positions[ name ][ n-i ].x;
|
||||
|
||||
if( not( count_x_coord[ v ] )) then
|
||||
count_x_coord[ v ] = 1;
|
||||
anz_x_coord = anz_x_coord + 1;
|
||||
elseif( count_x_coord[ v ] == 2 ) then
|
||||
check_failed = 1;
|
||||
else
|
||||
count_x_coord[ v ] = count_x_coord[ v ] + 1;
|
||||
end
|
||||
|
||||
v = markers.positions[ name ][ n-i ].z;
|
||||
if( not( count_z_coord[ v ] )) then
|
||||
count_z_coord[ v ] = 1;
|
||||
anz_z_coord = anz_z_coord + 1;
|
||||
elseif( count_z_coord[ v ] == 2 ) then
|
||||
check_failed = 1;
|
||||
else
|
||||
count_z_coord[ v ] = count_z_coord[ v ] + 1;
|
||||
end
|
||||
end
|
||||
|
||||
if( anz_x_coord == 2
|
||||
and anz_z_coord == 2
|
||||
and check_failed == 0 ) then
|
||||
|
||||
minetest.chat_send_player( name, 'The last four markers you placed form a rectangle. '..
|
||||
'Right-click on this marker here in order to protect the area.');
|
||||
end
|
||||
end
|
||||
|
||||
-- make sure the list does not grow too large
|
||||
if( n > markers.MAX_MARKERS ) then
|
||||
table.remove( markers.positions[ name ], 1 );
|
||||
@ -266,7 +226,32 @@ markers.marker_after_dig_node = function(pos, oldnode, oldmetadata, digger)
|
||||
end
|
||||
|
||||
|
||||
|
||||
--this function returns a min_pos and max_pos that are the corners
|
||||
--of a box that contains ALL of the players active markers.
|
||||
markers.get_box_from_markers = function(name)
|
||||
if (not name) or (not (markers.positions[ name ][ 1 ] )) then
|
||||
return {x=0,y=0,z=0},{x=1,y=1,z=1}
|
||||
end
|
||||
local min_pos={}
|
||||
min_pos.x = markers.positions[ name ][ 1 ].x
|
||||
min_pos.y = markers.positions[ name ][ 1 ].y
|
||||
min_pos.z = markers.positions[ name ][ 1 ].z
|
||||
local max_pos={}
|
||||
max_pos.x = markers.positions[ name ][ 1 ].x
|
||||
max_pos.y = markers.positions[ name ][ 1 ].y
|
||||
max_pos.z = markers.positions[ name ][ 1 ].z
|
||||
for i,p in ipairs( markers.positions[ name ] ) do
|
||||
if p.x < min_pos.x then min_pos.x = p.x end
|
||||
if p.x > max_pos.x then max_pos.x = p.x end
|
||||
if p.y < min_pos.y then min_pos.y = p.y end
|
||||
if p.y > max_pos.y then max_pos.y = p.y end
|
||||
if p.z < min_pos.z then min_pos.z = p.z end
|
||||
if p.z > max_pos.z then max_pos.z = p.z end
|
||||
end
|
||||
--print("getbox: min_pos.x="..min_pos.x.." y="..min_pos.y.." z="..min_pos.z)
|
||||
--print(" : max_pos.x="..max_pos.x.." y="..max_pos.y.." z="..max_pos.z)
|
||||
return min_pos, max_pos
|
||||
end --get_box_from_markers
|
||||
|
||||
|
||||
|
||||
@ -291,69 +276,19 @@ markers.get_marker_formspec = function(player, pos, error_msg)
|
||||
|
||||
local n = #markers.positions[ name ];
|
||||
|
||||
if( markers.positions[ name ][ n ].x ~= pos.x
|
||||
or markers.positions[ name ][ n ].y ~= pos.y
|
||||
or markers.positions[ name ][ n ].z ~= pos.z ) then
|
||||
|
||||
return formspec_info.."Please use the marker\nyou placed last\n"..
|
||||
"for accessing this menu.\nYou can find said marker at\n"..
|
||||
minetest.pos_to_string( markers.positions[ name ][ n ] )..'.]';
|
||||
end
|
||||
|
||||
if( n < 4 ) then
|
||||
return formspec_info.."Please place 4 markers\n - one in each corner\n of your area - first.]";
|
||||
if ( n < 2 ) then
|
||||
return formspec_info.."Please place 2 or more markers\n - at least one in each corner\n of your area first]";
|
||||
end
|
||||
|
||||
|
||||
local coords_raw = meta:get_string( 'coords' );
|
||||
local coords = {};
|
||||
if( coords_raw ~= nil and coords_raw ~= '' ) then
|
||||
coords = minetest.deserialize( coords_raw );
|
||||
end
|
||||
|
||||
|
||||
local opposite = n;
|
||||
-- the last 4 markers placed ought to form the area
|
||||
if( true or #coords ~= 2 ) then -- TODO
|
||||
|
||||
|
||||
for i=1,3 do
|
||||
|
||||
-- if both coordinates are diffrent, then this may be the opposite marker
|
||||
if( ( markers.positions[ name ][ n-i ].x ~=
|
||||
markers.positions[ name ][ n ].x )
|
||||
and ( markers.positions[ name ][ n-i ].z ~=
|
||||
markers.positions[ name ][ n ].z )) then
|
||||
|
||||
opposite = n-i;
|
||||
coords = { markers.positions[ name ][ n ],
|
||||
markers.positions[ name ][ n-i ] };
|
||||
end
|
||||
end
|
||||
|
||||
-- check if they fit
|
||||
for i=1,3 do
|
||||
|
||||
if(not( ((n-i) == opposite )
|
||||
or not(markers.positions[ name ][ n-i ] )
|
||||
or not( coords ) or not( coords[1]) or not( coords[2])
|
||||
or not(markers.positions[ name ] )
|
||||
or not(markers.positions[ name ][ n-i ] )
|
||||
or ( markers.positions[ name ][ n-i ].x == coords[ 1 ].x
|
||||
and markers.positions[ name ][ n-i ].z == coords[ 2 ].z )
|
||||
or ( markers.positions[ name ][ n-i ].x == coords[ 2 ].x
|
||||
and markers.positions[ name ][ n-i ].z == coords[ 1 ].z ))) then
|
||||
|
||||
return formspec_info.."Error: The last 4 markers\nyou placed do not form\na rectangle.]";
|
||||
end
|
||||
|
||||
end
|
||||
local coords={}
|
||||
coords[1],coords[2] = markers.get_box_from_markers(name)
|
||||
|
||||
-- save data
|
||||
meta:set_string( 'coords', minetest.serialize( coords ) );
|
||||
end
|
||||
|
||||
if( not( coords ) or #coords < 2 or not( coords[1] ) or not( coords[2] )) then
|
||||
return formspec_info.."Error: The last 4 markers\nyou placed do not form\na rectangle.]";
|
||||
return formspec_info.."Error in markers.]";
|
||||
end
|
||||
|
||||
-- the coordinates are set; we may present an input form now
|
||||
@ -402,11 +337,11 @@ markers.get_marker_formspec = function(player, pos, error_msg)
|
||||
'label[0.5,3.0;Your area ought to go..]'..
|
||||
'label[0.5,3.5;this many blocks up:]'..
|
||||
'field[5.0,4.0;1,0.5;add_height;;40]'..
|
||||
'label[6.0,3.5;(relative to this marker)]'..
|
||||
'label[6.0,3.5;(above '..coords[2].y..' )]'..
|
||||
|
||||
'label[0.5,4.0;and this many blocks down:]'..
|
||||
'field[5.0,4.5;1,0.5;add_depth;;10]'..
|
||||
'label[6.0,4.0;(relative to this marker)]'..
|
||||
'label[6.0,4.0;(below '..coords[1].y..' )]'..
|
||||
|
||||
'label[0.5,4.5;The area shall be named]'..
|
||||
'field[5.0,5.0;6,0.5;set_area_name;;please enter a name]'..
|
||||
@ -493,8 +428,8 @@ markers.marker_on_receive_fields = function(pos, formname, fields, sender)
|
||||
local pos1 = coords[1];
|
||||
local pos2 = coords[2];
|
||||
-- apply height values from the formspeck
|
||||
pos1.y = pos.y + add_height;
|
||||
pos2.y = pos.y - add_depth;
|
||||
pos1.y = pos1.y - add_depth;
|
||||
pos2.y = pos2.y + add_height;
|
||||
|
||||
pos1, pos2 = areas:sortPos( pos1, pos2 );
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user