Ready to be published
parent
a5919c7a34
commit
b84ab500c3
39
api.lua
39
api.lua
|
@ -8,22 +8,36 @@ namegen.graphs = {}
|
|||
|
||||
namegen.load = function( generator_name , graph_file )
|
||||
if not graph_file then
|
||||
graph_file = namegen.path .. "/graphs/" + generator_name + ".json"
|
||||
graph_file = namegen.path .. "/graphs/" .. generator_name .. ".json"
|
||||
end
|
||||
|
||||
local file = io.open( graph_file , "r+" )
|
||||
local file = io.open( graph_file , "r" )
|
||||
|
||||
if not file then return false end
|
||||
|
||||
local str = file:read( "*a" )
|
||||
local graph_data = minetest.parse_json( str )
|
||||
namegen.graphs[ generator_name ] = graph_data
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
|
||||
namegen.generate = function( generator_name )
|
||||
namegen.generate = function( generator_name , can_load )
|
||||
local graph_data = namegen.graphs[ generator_name ]
|
||||
|
||||
if not graph_data then
|
||||
error( "Generator " .. generator_name .. " not found." ) ;
|
||||
if can_load then
|
||||
namegen.load( generator_name )
|
||||
if not namegen.load( generator_name ) then
|
||||
--error( "Generator " .. generator_name .. " not found (can_load=true)." )
|
||||
return nil
|
||||
end
|
||||
graph_data = namegen.graphs[ generator_name ]
|
||||
else
|
||||
--error( "Generator " .. generator_name .. " not found." )
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
return namegen.generate_from_graph( graph_data )
|
||||
|
@ -44,7 +58,7 @@ end
|
|||
function table_has_at_least( src , count )
|
||||
for k,v in pairs( src ) do
|
||||
count = count - 1
|
||||
if count <= 0 return true
|
||||
if count <= 0 then return true end
|
||||
end
|
||||
|
||||
return false
|
||||
|
@ -61,7 +75,10 @@ namegen.generate_from_graph = function( data , options )
|
|||
|
||||
while true do
|
||||
leaf = table_path( graph , chain )
|
||||
if not leaf then error( 'unexpected error, leaf not found...' ) end
|
||||
if not leaf then
|
||||
--error( 'unexpected error, leaf not found...' )
|
||||
return nil
|
||||
end
|
||||
|
||||
--if atomCount < data.atomMin and leaf.next[""] and ( table_has_at_least( leaf.next , 2 ) or rebranchCount ) then
|
||||
if atomCount < data.atomMin and leaf.next[""] and table_has_at_least( leaf.next , 2 ) then
|
||||
|
@ -78,7 +95,10 @@ namegen.generate_from_graph = function( data , options )
|
|||
end
|
||||
end
|
||||
|
||||
if r > 0 then error( "unexpected internal error: 'r' is still positive" ) end
|
||||
if r > 0 then
|
||||
error( "unexpected internal error: 'r' is still positive" )
|
||||
return nil
|
||||
end
|
||||
elseif atomCount >= data.atomMax and leaf.next[""] then
|
||||
-- We want to exit and it is possible
|
||||
key = "" ;
|
||||
|
@ -92,7 +112,10 @@ namegen.generate_from_graph = function( data , options )
|
|||
if r <= 0 then break end
|
||||
end
|
||||
|
||||
if r > 0 then error( "unexpected internal error: 'r' is still positive" ) end
|
||||
if r > 0 then
|
||||
--error( "unexpected internal error: 'r' is still positive" )
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
if str == "" then
|
||||
|
|
|
@ -7,8 +7,12 @@ minetest.register_chatcommand( "test_namegen", {
|
|||
description = S("Test the name generator."),
|
||||
params = S("[<generator name>]"),
|
||||
func = function( player_name , param )
|
||||
local name = "noop"
|
||||
return false, S("Name: @1", name)
|
||||
local name = namegen.generate( param , true )
|
||||
if not name then
|
||||
return false, S("Can't generate a name using generator @1", param)
|
||||
end
|
||||
|
||||
return true, S("Name: @1", name)
|
||||
end
|
||||
} )
|
||||
|
||||
|
|
Loading…
Reference in New Issue