mirror of
https://github.com/sys4-fr/server-nalc.git
synced 2025-01-11 18:40:25 +01:00
Update markers mod
- Calculate 3D cube from multiple markers instead of requiring a rectangle of 4 - Sort area lists nearest first - Increased number of markers (20 to 50) - Crashfix
This commit is contained in:
parent
cd39ac77a7
commit
18118226ab
@ -1,6 +1,15 @@
|
||||
|
||||
-- TODO: offer teleport button?
|
||||
|
||||
-- Temporary compatibility function - see minetest PR#1180
|
||||
if not vector.interpolate then
|
||||
vector.interpolate = function(pos1, pos2)
|
||||
return {x = pos1.x + (pos2.x - pos1.x) * factor,
|
||||
y = pos1.y + (pos2.y - pos1.y) * factor,
|
||||
z = pos1.z + (pos2.z - pos1.z) * factor}
|
||||
end
|
||||
end
|
||||
|
||||
-- taken from mobf
|
||||
local COLOR_RED = "#FF0000";
|
||||
local COLOR_GREEN = "#00FF00";
|
||||
@ -28,10 +37,11 @@ end
|
||||
|
||||
|
||||
|
||||
-- ppos: current player (or marker stone) position - used for sorting the list
|
||||
-- mode: can be pos, player, all, subarea, main_areas
|
||||
-- mode_data: content depends on mode
|
||||
-- selected: display information about the area the player single-clicked on
|
||||
markers.get_area_list_formspec = function( player, mode, pos, mode_data, selected )
|
||||
markers.get_area_list_formspec = function(ppos, player, mode, pos, mode_data, selected )
|
||||
|
||||
|
||||
local id_list = {};
|
||||
@ -101,6 +111,12 @@ markers.get_area_list_formspec = function( player, mode, pos, mode_data, selecte
|
||||
end
|
||||
end
|
||||
|
||||
-- Sort the list of areas so the nearest comes first
|
||||
local nearsorter = function(a, b)
|
||||
return vector.distance(vector.interpolate(areas.areas[a].pos1, areas.areas[a].pos2, 0.5), ppos) <
|
||||
vector.distance(vector.interpolate(areas.areas[b].pos1, areas.areas[b].pos2, 0.5), ppos)
|
||||
end
|
||||
table.sort(id_list, nearsorter)
|
||||
|
||||
local formspec = 'size[10,9]';
|
||||
|
||||
@ -448,6 +464,7 @@ end
|
||||
markers.form_input_handler_areas = function( player, formname, fields)
|
||||
|
||||
local pname = player:get_player_name();
|
||||
local ppos = player:getpos()
|
||||
|
||||
if( formname ~= "markers:info"
|
||||
or not( player )
|
||||
@ -646,7 +663,7 @@ markers.form_input_handler_areas = function( player, formname, fields)
|
||||
areas:remove( menu_data.selected, false ); -- no recursive delete
|
||||
areas:save();
|
||||
-- show the list of areas owned by the previous owner
|
||||
formspec = markers.get_area_list_formspec( player, 'player', menu_data.pos, old_owner, nil );
|
||||
formspec = markers.get_area_list_formspec(ppos, player, 'player', menu_data.pos, old_owner, nil );
|
||||
end
|
||||
|
||||
|
||||
@ -663,24 +680,24 @@ markers.form_input_handler_areas = function( player, formname, fields)
|
||||
and menu_data.selected
|
||||
and areas.areas[ menu_data.selected ] ) then
|
||||
|
||||
formspec = markers.get_area_list_formspec( player, 'player', menu_data.pos, areas.areas[ menu_data.selected ].owner, nil );
|
||||
formspec = markers.get_area_list_formspec(ppos, player, 'player', menu_data.pos, areas.areas[ menu_data.selected ].owner, nil );
|
||||
|
||||
|
||||
elseif( fields.list_subareas
|
||||
and menu_data.selected
|
||||
and areas.areas[ menu_data.selected ] ) then
|
||||
|
||||
formspec = markers.get_area_list_formspec( player, 'subareas', menu_data.pos, menu_data.selected, nil );
|
||||
formspec = markers.get_area_list_formspec(ppos, player, 'subareas', menu_data.pos, menu_data.selected, nil );
|
||||
|
||||
|
||||
elseif( fields.list_main_areas ) then
|
||||
|
||||
formspec = markers.get_area_list_formspec( player, 'main_areas', menu_data.pos, nil, nil );
|
||||
formspec = markers.get_area_list_formspec(ppos, player, 'main_areas', menu_data.pos, nil, nil );
|
||||
|
||||
elseif( fields.list_areas_at
|
||||
and menu_data.pos ) then
|
||||
|
||||
formspec = markers.get_area_list_formspec( player, 'pos', menu_data.pos, menu_data.pos, nil );
|
||||
formspec = markers.get_area_list_formspec(ppos, player, 'pos', menu_data.pos, menu_data.pos, nil );
|
||||
|
||||
|
||||
elseif( fields.markers_area_list_selection
|
||||
@ -703,7 +720,7 @@ markers.form_input_handler_areas = function( player, formname, fields)
|
||||
else
|
||||
|
||||
-- on single click, just show the position of that particular area
|
||||
formspec = markers.get_area_list_formspec( player, menu_data.mode, menu_data.pos, menu_data.mode_data, selected );
|
||||
formspec = markers.get_area_list_formspec(ppos, player, menu_data.mode, menu_data.pos, menu_data.mode_data, selected );
|
||||
end
|
||||
|
||||
else
|
||||
@ -721,6 +738,7 @@ end
|
||||
markers.show_marker_stone_formspec = function( player, pos )
|
||||
|
||||
local pname = player:get_player_name();
|
||||
local ppos = pos
|
||||
|
||||
-- this table stores the list the player may have selected from; at the beginning, there is no list
|
||||
if( not( markers.menu_data_by_player[ pname ] )) then
|
||||
@ -825,7 +843,7 @@ markers.show_marker_stone_formspec = function( player, pos )
|
||||
-- find out which of the candidates he is intrested in; we list them all
|
||||
else
|
||||
|
||||
formspec = markers.get_area_list_formspec( player, 'pos', pos, pos, nil );
|
||||
formspec = markers.get_area_list_formspec(ppos, player, 'pos', pos, pos, nil );
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -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 and minetest.get_player_privs(name)["megabuilder"] ~= true) 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