Module:Random world names

local replacementArgs = require('Module:GameText').replacementArgs local gameText = require('Module:GameText').getText

-- credit: http://richard.warburton.it -- this version is with trim. local function explode(div,str) if (div=='') then return false end local pos,arr = 0,{} -- for each divider found for st,sp in function return string.find(str,div,pos,true) end do		table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider pos = sp + 1 -- Jump past current divider end table.insert(arr, string.sub(str,pos)) -- Attach chars right of last divider return arr end

local db = {}

local function getList(key, lang) local lang = lang or 'en' if not db[lang] then if lang == 'de' or lang == 'es' or lang == 'fr' or lang == 'it' or lang == 'pl' or lang == 'pt' or lang == 'ru' or lang =='zh' then db[lang] = mw.loadData('Module:GameText/db-' .. lang) else db[lang] = mw.loadData('Module:GameText/db-en') end end

if key then local valuelist = {} for _, v in pairs(db[lang][key]) do			table.insert(valuelist, v)		end return valuelist end end

return {

go = function(frame)

local lang = frame.args['lang']

-- load lists of all available strings local adjectivelist = getList('RandomWorldName_Adjective', lang) local locationlist = getList('RandomWorldName_Location', lang) local nounlist = getList('RandomWorldName_Noun', lang)

-- default strings for the generic composition displayed in the left cell, local arg_adj = frame.args['adjective'] local arg_loc = frame.args['location'] local arg_nou = frame.args['noun']

-- basis for the randomizer: current date and time, changing each second local seed = string.sub(os.time, -6)

local output = ' Random world names seed: ' .. seed .. ' (' .. os.date .. ') '

for _,composition in pairs(explode(',', frame.args['compositions'])) do		-- print a table row for each composition output = output .. ' '

-- left cell: generic composition

-- use the default strings for adjective, location, and noun as defined above, -- but overwrite where there is a definition for this specific composition -- (e.g. ) for the adjective of composition 3 local x_Adj = frame.args[composition .. ':adjective'] or arg_adj local x_Loc = frame.args[composition .. ':location'] or arg_loc local x_Nou = frame.args[composition .. ':noun'] or arg_nou local replaceArgs = replacementArgs({			x_Adjective = &lt;' .. x_Adj .. '&gt;,			x_Location = &lt;' .. x_Loc .. '&gt;,			x_Noun = &lt;' .. x_Nou .. '&gt;		})

output = output .. gameText('RandomWorldName_Composition.' .. composition, lang, replaceArgs) output = output .. ' '

-- right cell: list of three random examples for the composition

for worldid = 1, 3 do -- "world id": first, second, or third world name in this composition local worldname = '' local i = 1 -- will be decreased after generating a world name to ensure the next world name is new local factor = 0.075 -- controls the randomization; values of >0.075 cause the last compositions to have very poor randomization

-- keep generating random world names until one is found that is at max 27 chars in length -- (this is the same behavior as in-game) repeat local rand = (seed^(factor*(composition*3-worldid)+1))*i -- RNG: create a random number for this composition and world id				i = i * (1-factor)

-- select the adjective, location, and noun based on the random number local arg_adj = adjectivelist[math.floor(rand % #adjectivelist) + 1] local arg_loc = locationlist[math.floor(rand % #locationlist) + 1] local arg_nou = nounlist[math.floor(rand % #nounlist) + 1] if arg_adj and arg_loc and arg_nou then -- make world name local replaceArgs = replacementArgs({x_Adjective = arg_adj, x_Location = arg_loc, x_Noun = arg_nou}) worldname = gameText('RandomWorldName_Composition.' .. composition, lang, replaceArgs) end until worldname ~= '' and #worldname <= 27

output = output .. '' .. worldname .. ''

end

output = output .. ' ' end

return output

end

}