moved analyze_file to seperate file

master
Sokomine 2018-07-29 19:05:09 +02:00
parent 400f8f2aea
commit dcbab92521
3 changed files with 138 additions and 136 deletions

134
analyze_file.lua Normal file
View File

@ -0,0 +1,134 @@
-- read .mts (minetest schematic), .we (WorldEdit) and .schematic (MC schematic format) files
-- file_name: file name with full path but without file name exstension
-- origin_offset: .we files may have this defined if their start is not at 0,0,0
-- store_as_mts: if set to true: convert files of any other types and store them
-- as .mts files additionally (speeds up usage at later server starts)
-- building_data: Table that may contain additional information about the building; in particular:
-- orients: list of allowed rotations for this schematic; may be {0,1,2,3}
-- for a building that looks at a street ok no matter how it is rotated;
-- may be set to i.e. {2} if the building is initially rotated by 180
-- degree and has a front door
-- yoff: How deep is the building burried? Automaticly determined for .mts
-- files created by this mod but otherwise ought to be provided.
-- Entries of this table are returned in the return value if possible.
-- no_build_chest_entry: If set to true: Do not add an entry for this building in the build_chest.
-- Useful when manually adding diffrently structured entries (as in i.e. mg_villages)
-- or when the entry would be temporal only.
-- Returns a table that contains the necessary information for spawning the building.
-- Returns nil if reading of the file failed.
handle_schematics.analyze_file = function( file_name, origin_offset, store_as_mts, building_data,no_build_chest_entry)
if( not( building_data )) then
building_data = {};
end
-- determine the file_name from building_data if possible
if( not( file_name ) and building_data.mts_path and building_data.scm ) then
file_name = building_data.mts_path .. building_data.scm;
elseif( not( file_name )) then
print("[handle_schematics] ERROR: No file name given to analyze.");
return;
end
local res = handle_schematics.analyze_mts_file( file_name );
-- alternatively, read the mts file
if( not( res )) then
res = handle_schematics.analyze_we_file( file_name, origin_offset );
if( not( res )) then
res = handle_schematics.analyze_mc_schematic_file( file_name );
end
-- print error message only if all import methods failed
if( not( res )) then
print('[handle_schematics] ERROR: Failed to import file \"'..tostring( file_name )..'\"[.mts|.we|.wem|.schematic]');
-- convert to .mts for later usage
elseif( store_as_mts ) then
handle_schematics.store_mts_file( store_as_mts, res );
end
-- .we and .schematic do not provide on_construct/after_palce_node
-- (they have metadata instead)
res.on_constr = {};
res.after_place_node = {};
for _, name_text in res.nodenames do
local node_def = handle_schematics.node_defined( name_text );
if( node_def and node_def.on_construct) then
table.insert( on_constr, name_text );
end
if( node_def and node_def.after_place_node) then
table.insert( after_place_node, name_text );
end
end
end
-- the building cannot be used if its size remains unknown
if( not( res ) or not(res.size) or not(res.size.x)) then
return nil;
end
-- the file has to be placed with minetest.place_schematic(...)
res.is_mts = 1;
-- the actual functions for placing use these for accessing the size
res.sizex = res.size.x;
res.sizez = res.size.z;
res.ysize = res.size.y;
-- these values remain unchanged
-- res.bed_count: How many beds does the building contain?
-- res.bed_list: And where are the beds placed in the original schematic?
-- res.rotated: Was the building stored in a rotated way?
-- res.nodenames: Which nodes are part of the building?
-- res.on_constr: For which nodes (nodenames only) do we need to call on_constuct?
-- res.after_place_node: For which nodes (nodenames only) do we need to call after_palce_node?
-- res.door_a: Where can doors of type _a be found?
-- res.door_b: Where can doors of type _b be found?
-- res.metadata: Metadata; Only provided by .we files.
-- some buildings may be rotated
if( not( building_data.orients ) and res.rotated ) then
res.orients = {};
if( res.rotated == 0 ) then
res.orients[1] = 0;
elseif( res.rotated == 90 ) then
res.axis = 1; -- important when mirroring
res.orients[1] = 1;
elseif( res.rotated == 180 ) then
res.orients[1] = 2;
elseif( res.rotated == 270 ) then
res.orients[1] = 3;
res.axis = 1; -- important when mirroring
end
end
if( not( building_data.yoff ) and res.burried ) then
res.yoff = 1-res.burried;
end
-- the file has been read already
if( res.scm_data_cache ) then
res.is_mts = 0;
end
-- copy all the values from building_data over to res for later use; that
-- might i.e. be information of where/how this building can be used by the mod
-- (mod internal information)
for k,v in pairs( building_data ) do
-- do not overwrite any values
if( not( res[ k ])) then
res[ k ] = v;
end
end
-- make the building as such available for the build_chest;
-- cache all data (including res.scm_data_cache)
-- TODO: what if the building was already stored by another mod?
-- TODO: really cache all data?
if( build_chest and build_chest.add_building ) then
build_chest.add_building( file_name, res );
end
-- add the building to the menu list for the build chest
if( build_chest and build_chest.add_entry and not(no_build_chest_entry) and building_data.scm) then
local modname = minetest.get_current_modname();
build_chest.add_entry( {'main', modname, modname, building_data.scm, file_name });
end
return res;
end

View File

@ -259,139 +259,3 @@ handle_schematics.store_mts_file = function( path, data )
file.close(file);
print('SAVING '..path..'.mts (converted from .we).');
end
-- read .mts (minetest schematic), .we (WorldEdit) and .schematic (MC schematic format) files
-- file_name: file name with full path but without file name exstension
-- origin_offset: .we files may have this defined if their start is not at 0,0,0
-- store_as_mts: if set to true: convert files of any other types and store them
-- as .mts files additionally (speeds up usage at later server starts)
-- building_data: Table that may contain additional information about the building; in particular:
-- orients: list of allowed rotations for this schematic; may be {0,1,2,3}
-- for a building that looks at a street ok no matter how it is rotated;
-- may be set to i.e. {2} if the building is initially rotated by 180
-- degree and has a front door
-- yoff: How deep is the building burried? Automaticly determined for .mts
-- files created by this mod but otherwise ought to be provided.
-- Entries of this table are returned in the return value if possible.
-- no_build_chest_entry: If set to true: Do not add an entry for this building in the build_chest.
-- Useful when manually adding diffrently structured entries (as in i.e. mg_villages)
-- or when the entry would be temporal only.
-- Returns a table that contains the necessary information for spawning the building.
-- Returns nil if reading of the file failed.
handle_schematics.analyze_file = function( file_name, origin_offset, store_as_mts, building_data,no_build_chest_entry)
if( not( building_data )) then
building_data = {};
end
-- determine the file_name from building_data if possible
if( not( file_name ) and building_data.mts_path and building_data.scm ) then
file_name = building_data.mts_path .. building_data.scm;
elseif( not( file_name )) then
print("[handle_schematics] ERROR: No file name given to analyze.");
return;
end
local res = handle_schematics.analyze_mts_file( file_name );
-- alternatively, read the mts file
if( not( res )) then
res = handle_schematics.analyze_we_file( file_name, origin_offset );
if( not( res )) then
res = handle_schematics.analyze_mc_schematic_file( file_name );
end
-- print error message only if all import methods failed
if( not( res )) then
print('[handle_schematics] ERROR: Failed to import file \"'..tostring( file_name )..'\"[.mts|.we|.wem|.schematic]');
-- convert to .mts for later usage
elseif( store_as_mts ) then
handle_schematics.store_mts_file( store_as_mts, res );
end
-- .we and .schematic do not provide on_construct/after_palce_node
-- (they have metadata instead)
res.on_constr = {};
res.after_place_node = {};
for _, name_text in res.nodenames do
local node_def = handle_schematics.node_defined( name_text );
if( node_def and node_def.on_construct) then
table.insert( on_constr, name_text );
end
if( node_def and node_def.after_place_node) then
table.insert( after_place_node, name_text );
end
end
end
-- the building cannot be used if its size remains unknown
if( not( res ) or not(res.size) or not(res.size.x)) then
return nil;
end
-- the file has to be placed with minetest.place_schematic(...)
res.is_mts = 1;
-- the actual functions for placing use these for accessing the size
res.sizex = res.size.x;
res.sizez = res.size.z;
res.ysize = res.size.y;
-- these values remain unchanged
-- res.bed_count: How many beds does the building contain?
-- res.bed_list: And where are the beds placed in the original schematic?
-- res.rotated: Was the building stored in a rotated way?
-- res.nodenames: Which nodes are part of the building?
-- res.on_constr: For which nodes (nodenames only) do we need to call on_constuct?
-- res.after_place_node: For which nodes (nodenames only) do we need to call after_palce_node?
-- res.door_a: Where can doors of type _a be found?
-- res.door_b: Where can doors of type _b be found?
-- res.metadata: Metadata; Only provided by .we files.
-- some buildings may be rotated
if( not( building_data.orients ) and res.rotated ) then
res.orients = {};
if( res.rotated == 0 ) then
res.orients[1] = 0;
elseif( res.rotated == 90 ) then
res.axis = 1; -- important when mirroring
res.orients[1] = 1;
elseif( res.rotated == 180 ) then
res.orients[1] = 2;
elseif( res.rotated == 270 ) then
res.orients[1] = 3;
res.axis = 1; -- important when mirroring
end
end
if( not( building_data.yoff ) and res.burried ) then
res.yoff = 1-res.burried;
end
-- the file has been read already
if( res.scm_data_cache ) then
res.is_mts = 0;
end
-- copy all the values from building_data over to res for later use; that
-- might i.e. be information of where/how this building can be used by the mod
-- (mod internal information)
for k,v in pairs( building_data ) do
-- do not overwrite any values
if( not( res[ k ])) then
res[ k ] = v;
end
end
-- make the building as such available for the build_chest;
-- cache all data (including res.scm_data_cache)
-- TODO: what if the building was already stored by another mod?
-- TODO: really cache all data?
if( build_chest and build_chest.add_building ) then
build_chest.add_building( file_name, res );
end
-- add the building to the menu list for the build chest
if( build_chest and build_chest.add_entry and not(no_build_chest_entry) and building_data.scm) then
local modname = minetest.get_current_modname();
build_chest.add_entry( {'main', modname, modname, building_data.scm, file_name });
end
return res;
end

View File

@ -47,6 +47,10 @@ dofile(handle_schematics.modpath.."/analyze_we_file.lua")
-- reads and analyzes Minecraft schematic files
dofile(handle_schematics.modpath.."/translate_nodenames_for_mc_schematic.lua")
dofile(handle_schematics.modpath.."/analyze_mc_schematic_file.lua")
-- contains a general function to analyze any of the above files
dofile(handle_schematics.modpath.."/analyze_file.lua")
-- handles rotation and mirroring
dofile(handle_schematics.modpath.."/rotate.lua")
-- count nodes, take param2 into account for rotation etc.