Module:Random world names

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

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] = require('Module:GameText/db-' .. lang) else db[lang] = require('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)

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

-- default strings for the generic composition displayed in the left cell, -- must be defined in a before invoking this module local arg_adj = frame:callParserFunction('#dplvar', 'adjective') local arg_loc = frame:callParserFunction('#dplvar', 'location') local arg_nou = frame:callParserFunction('#dplvar', '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 = 1, 8 do		if composition ~= 2 and composition ~= 4 then -- skip compositions 2 and 4 as they are equal to 1 and 3, respectively

-- 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:callParserFunction('#dplvar', composition .. ':adjective') x_Adj = x_Adj == '' and arg_adj or x_Adj local x_Loc = frame:callParserFunction('#dplvar', composition .. ':location') x_Loc = x_Loc == '' and arg_loc or x_Loc local x_Nou = frame:callParserFunction('#dplvar', composition .. ':noun') x_Nou = x_Nou == '' and arg_nou or x_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, frame.args['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, frame.args['lang'], replaceArgs) end until worldname ~= '' and #worldname <= 27

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

end

output = output .. ' ' end end

return output

end

}