Module:NameGenerator: Difference between revisions

From Caves of Qud Wiki
Jump to navigation Jump to search
(name gen that isn't crab specific, i hope?)
 
(don't need cargo)
 
(16 intermediate revisions by one other user not shown)
Line 1: Line 1:
local p = {}
local p = {}
local cargo = mw.ext.cargo


function p.main(frame)
function p.main(frame)
local namingtable = require'Module:NameGenerator/NamingTable'
local naming = namingtable.naming
local defaultvars = namingtable.defaultvars
local prefixes = ""
local prefixes = ""
local infixes = ""
local infixes = ""
Line 15: Line 17:
local result = {}
local result = {}
local seed = frame.args.seed
local seed = frame.args.seed
local style = frame.args.style or 'Qudish'
local style = frame.args.style:lower()


if seed ~= nil and seed ~= ''then
if seed ~= nil and seed ~= ''then
Line 21: Line 23:
end
end


local prefixtable = naming[style]['Prefixes']
local infixtable = naming[style]['Infixes']
local postfixtable = naming[style]['Postfixes']
local hyphenationchance = naming[style]['HyphenationChance'] or 0
local twonamechance = naming[style]['TwoNameChance'] or 0
minprefix=naming[style]['MinPrefixAmount']
maxprefix=naming[style]['MaxPrefixAmount']
local namecount
if math.random(0, 100) < twonamechance then
namecount = 2
else
namecount = 1
end
local heroname = ''
for i = 1,namecount do
prefixes = ''
infixes = ''
postfixes = ''
--[get min/maxprefix stuff]--
for i=1,math.random(minprefix,maxprefix) do
  prefixes = prefixes.. prefixtable[math.random(table.getn(prefixtable))]
  if math.random(0, 100) < hyphenationchance then
    prefixes = prefixes .. '-'
  end
end


--[get cargo row]--
mininfix=naming[style]['MinInfixAmount']
local tables= 'HeroNames'
maxinfix=naming[style]['MaxInfixAmount']
local fields= 'Prefixes, MinPrefixAmount, MaxPrefixAmount,Infixes, MinInfixAmount,MaxInfixAmount,Postfixes ,MinPostfixAmount,MaxPostfixAmount,TitleTemplate'  
for i=1,math.random(mininfix,maxinfix) do
local args= {
  infixes = infixes.. infixtable[math.random(table.getn(infixtable))]
where= 'BaseType="' .. style .. '"'
  if math.random(0, 100) < hyphenationchance then
}
    infixes = infixes .. '-'
result = cargo.query(tables,fields,args)
  end
if not next(result) then
end
error('no base type of that kind found.')
minpostfix=naming[style]['MinPostfixAmount']
end
maxpostfix=naming[style]['MaxPostfixAmount']
--[get prefixes array]--
for i=1,math.random(minpostfix,maxpostfix) do
local resultstring = ''
  postfixes = postfixes.. postfixtable[math.random(table.getn(postfixtable))]
resultstring = result[1]['Prefixes']
  if math.random(0, 100) < hyphenationchance then
local prefixtable= {}
    postfixes = postfixes .. '-'
local i = 1
  end
for j in string.gmatch(resultstring,"%s*(%w+),?%s*") do
end
prefixtable[i] = j
i= i+1
end
--[get infixes array]--
local resultstring = ''
resultstring = result[1]['Infixes']
local infixtable= {}
local i = 1
for j in string.gmatch(resultstring,"%s*(%w+),?%s*") do
infixtable[i] = j
i= i+1
end
--[get postfixes array]--
local resultstring = ''
resultstring = result[1]['Postfixes']
local postfixtable= {}
local i = 1
for j in string.gmatch(resultstring,"%s*(%w+),?%s*") do
postfixtable[i] = j
i= i+1
end


--[get min/maxprefix stuff]--
minprefix=result[1]['MinPrefixAmount']
maxprefix=result[1]['MaxPrefixAmount']
for i=1,math.random(minprefix,maxprefix) do
  prefixes = prefixes.. prefixtable[math.random(table.getn(prefixtable))]
end


mininfix=result[1]['MinInfixAmount']
--[name fitting]--
maxinfix=result[1]['MaxInfixAmount']
heroname = heroname .. ((prefixes..infixes..postfixes):gsub("^%l", string.upper):gsub("-$", '')) .. ' '
for i=1,math.random(mininfix,maxinfix) do
  infixes = infixes.. infixtable[math.random(table.getn(infixtable))]
end
minpostfix=result[1]['MinPostfixAmount']
maxpostfix=result[1]['MaxPostfixAmount']
for i=1,math.random(minpostfix,maxpostfix) do
  postfixes = postfixes.. postfixtable[math.random(table.getn(postfixtable))]
end
end
 
heroname = heroname:gsub("%s*$", '')
--[title template]--
--[title template]--
titletemplate=result[1]['TitleTemplate']


--[name fitting]--
local b
local heroname= ((prefixes..infixes..postfixes):gsub("^%l", string.upper))
local templatetable = naming[style]['TitleTemplates']
local a = string.gsub(titletemplate,'NAME',tostring(heroname))
if templatetable ~= nil then
local b = string.gsub(a,('BASETYPE'), style)
local titletemplate = templatetable[math.random(table.getn(templatetable))]
local varstable = defaultvars
if naming[style]['TemplateVars'] ~= nil then
  for k,v in pairs(naming[style]['TemplateVars']) do
  varstable[k] = v
  end
end
if varstable == nil then
  varstable = {}
end
varstable['%*Name%*'] = { heroname }
varstable['%*Rings%*'] = { 'Three-ringed', 'Four-ringed', 'Five-ringed', 'Seven-ringed', 'Eight-ringed', 'Nine-ringed', 'Twelve-ringed'}
-- not gonna bother doing every number in the 1d10+2 range and don't know how to handle that automatically
for var, values in pairs(varstable) do
  titletemplate = titletemplate:gsub(var, values[math.random(table.getn(values))])
end
b = titletemplate
else
b = heroname
end


return b
return b
end
end


function p.a(seed)
  math.randomseed(tonumber(seed))
  local count = cargo.query('Characters=C, GeneralData=GD', 'TRIM(COUNT(C._pageName))=Number', {join='C.JoinKey=GD.JoinKey', where='C._pageNamespace="0" AND GD.Categories <> "Walls"'})[1]['Number']
  local offsetin = math.random(0 , tonumber(count))
  local tables = 'Characters=C, GeneralData=GD'
  local fields = 'GD.PlainName=Name, GD._pageName=Page'
  local args ={join='C._pageName=GD._pageName', where='GD._pageNamespace="0" AND GD.Categories <> "Walls"', offset=offsetin,limit = 1}
  local result = cargo.query(tables, fields, args)
  if not next(result) then
    error('?????')
  end
  return '[[' .. result[1]['Page'] .. '|'.. result[1]['Name'] .. ']]'
end
function p.getRandomChara(frame)
  return p.a(frame.args.seed)
end
function p.test(seed)
  return p.a(tostring(seed))
end
return p
return p

Latest revision as of 16:52, 19 January 2023

Documentation for this module may be created at Module:NameGenerator/doc

local p = {}

function p.main(frame)
local namingtable = require'Module:NameGenerator/NamingTable'
local naming = namingtable.naming
local defaultvars = namingtable.defaultvars
local prefixes = ""
local infixes = ""
local postfixes = ""
local maxprefix
local minprefix
local maxinfix
local mininfix
local maxpostfix
local minpostfix
local titletemplate
local result = {}
local seed = frame.args.seed
local style = frame.args.style:lower()

if seed ~= nil and seed ~= ''then
 math.randomseed(seed:gsub("%w",string.byte))
end

local prefixtable = naming[style]['Prefixes']
local infixtable = naming[style]['Infixes']
local postfixtable = naming[style]['Postfixes']
local hyphenationchance = naming[style]['HyphenationChance'] or 0
local twonamechance = naming[style]['TwoNameChance'] or 0
minprefix=naming[style]['MinPrefixAmount']
maxprefix=naming[style]['MaxPrefixAmount']
local namecount
if math.random(0, 100) < twonamechance then
 namecount = 2
else
 namecount = 1
end
local heroname = ''
for i = 1,namecount do
 prefixes = ''
 infixes = ''
 postfixes = ''
 --[get min/maxprefix stuff]--
 for i=1,math.random(minprefix,maxprefix) do
   prefixes = prefixes.. prefixtable[math.random(table.getn(prefixtable))]
   if math.random(0, 100) < hyphenationchance then
    prefixes = prefixes .. '-'
   end
 end

 mininfix=naming[style]['MinInfixAmount']
 maxinfix=naming[style]['MaxInfixAmount']
 for i=1,math.random(mininfix,maxinfix) do
   infixes = infixes.. infixtable[math.random(table.getn(infixtable))]
   if math.random(0, 100) < hyphenationchance then
    infixes = infixes .. '-'
   end
 end
 minpostfix=naming[style]['MinPostfixAmount']
 maxpostfix=naming[style]['MaxPostfixAmount']
 for i=1,math.random(minpostfix,maxpostfix) do
   postfixes = postfixes.. postfixtable[math.random(table.getn(postfixtable))]
   if math.random(0, 100) < hyphenationchance then
    postfixes = postfixes .. '-'
   end
 end


 --[name fitting]--
 heroname = heroname .. ((prefixes..infixes..postfixes):gsub("^%l", string.upper):gsub("-$", '')) .. ' '
end
heroname = heroname:gsub("%s*$", '')
--[title template]--

local b
local templatetable = naming[style]['TitleTemplates']
if templatetable ~= nil then
 local titletemplate = templatetable[math.random(table.getn(templatetable))]
 local varstable = defaultvars
 if naming[style]['TemplateVars'] ~= nil then
  for k,v in pairs(naming[style]['TemplateVars']) do
   varstable[k] = v
  end
 end
 if varstable == nil then
  varstable = {}
 end
 varstable['%*Name%*'] = { heroname }
 varstable['%*Rings%*'] = { 'Three-ringed', 'Four-ringed', 'Five-ringed', 'Seven-ringed', 'Eight-ringed', 'Nine-ringed', 'Twelve-ringed'}
 -- not gonna bother doing every number in the 1d10+2 range and don't know how to handle that automatically
 
 for var, values in pairs(varstable) do
  titletemplate = titletemplate:gsub(var, values[math.random(table.getn(values))])
 end
 b = titletemplate
else
 b = heroname
end 

return b
end

return p