Alyssa

namegen

Nov 26th, 2017
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.30 KB | None | 0 0
  1. local con = {"b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z"}
  2. local vow = {"a","e","i","o","u"}
  3. local maxLength = 8
  4.  
  5. local function hasValue (tab, val)
  6.     for index, value in ipairs(tab) do
  7.         if value == val then
  8.             return true
  9.         end
  10.     end
  11.     return false
  12. end
  13.  
  14. local function checkValid(name)
  15.     for i = 1, #name do
  16.         chr = string.sub(name,i,i)
  17.         if i > 2 then
  18.             --Get the previous 2 characters
  19.             chrBefore = string.sub(name,i-1,i-1)
  20.             chrBeforeB = string.sub(name,i-2,i-2)
  21.         elseif i > 1 then
  22.             chrBefore = string.sub(name,i-1,i-1)
  23.             chrBeforeB = ""
  24.         else
  25.             chrBefore = ""
  26.             chrBeforeB = ""
  27.         end
  28.         if hasValue(vow, chr) then
  29.             --Its a vowel
  30.             if hasValue(vow, chrBefore) then
  31.                 --Two vowels in a row GET OUTTA HERE
  32.                 return false
  33.             end
  34.         elseif hasValue(con, chr) then
  35.             --It's a connsonnant
  36.             if hasValue(con, chrBefore) and hasValue(con, chrBeforeB) then
  37.                 --Three connsonnants in a row GET OUTTA HERE
  38.                 return false
  39.             end
  40.         else
  41.             --It's some weird character GET OUTTA HERE
  42.             return false
  43.         end
  44.     end
  45.     return true
  46. end
  47.  
  48. function mutate(prevName)
  49.     local newName = false
  50.     local ops = {"vow","con"}
  51.     if #prevName > 4 then
  52.         delPoints = {}
  53.         for i = 1, #prevName do
  54.             local possibility = string.sub(prevName,0,i-1) .. string.sub(prevName,i+1,#prevName)
  55.             if checkValid(possibility) then
  56.                 table.insert(delPoints, i)
  57.             end
  58.         end
  59.         if #delPoints > 0 then
  60.             table.insert(ops, "del")
  61.         end
  62.     end
  63.     if #prevName < maxLength then
  64.         insPoints = {}
  65.         for i = 1, #prevName+1 do
  66.             local possibility = string.sub(prevName,0,i-1) .. con[1] .. string.sub(prevName,i,#prevName)
  67.             if checkValid(possibility) then
  68.                 table.insert(insPoints, {i, "con"})
  69.             end
  70.             local possibility = string.sub(prevName,0,i-1) .. vow[1] .. string.sub(prevName,i,#prevName)
  71.             if checkValid(possibility) then
  72.                 table.insert(insPoints, {i, "vow"})
  73.             end
  74.         end
  75.         if #insPoints > 0 then
  76.             table.insert(ops, "ins")
  77.         end
  78.     end
  79.     op = ops[math.random(#ops)]
  80.     if op == "del" then
  81.         local delPoint = delPoints[math.random(#delPoints)]
  82.         --print(tostring(prevName) .. " / " .. tostring(delPoint) .. " / " .. tostring(#prevName))
  83.         newName = string.sub(prevName,0,delPoint-1) .. string.sub(prevName, delPoint+1,#prevName)
  84.     elseif op == "ins" then
  85.         local insPoint = insPoints[math.random(#insPoints)]
  86.         if insPoint[2] == "con" then
  87.             newName = string.sub(prevName,0,insPoint[1]-1) .. con[math.random(#con)] .. string.sub(prevName,insPoint[1],#prevName)
  88.         else
  89.             newName = string.sub(prevName,0,insPoint[1]-1) .. vow[math.random(#vow)] .. string.sub(prevName,insPoint[1],#prevName)
  90.         end
  91.     elseif op == "vow" then
  92.         modPoints = {}
  93.         for i = 1, #prevName do
  94.             if hasValue(vow, string.sub(prevName,i,i)) then
  95.                 table.insert(modPoints, i)
  96.             end
  97.         end
  98.         modPoint = modPoints[math.random(#modPoints)]
  99.         curChar = string.sub(prevName,modPoint,modPoint)
  100.         avail = {}
  101.         for k,v in pairs(vow) do
  102.             if v ~= curChar then
  103.                 table.insert(avail, v)
  104.             end
  105.         end
  106.         newChar = avail[math.random(#avail)]
  107.         newName = string.sub(prevName,0,modPoint-1) .. newChar .. string.sub(prevName,modPoint+1,#prevName)
  108.     elseif op == "con" then
  109.         modPoints = {}
  110.         for i = 1, #prevName do
  111.             if hasValue(con, string.sub(prevName,i,i)) then
  112.                 table.insert(modPoints, i)
  113.             end
  114.         end
  115.         modPoint = modPoints[math.random(#modPoints)]
  116.         curChar = string.sub(prevName,modPoint,modPoint)
  117.         avail = {}
  118.         for k,v in pairs(con) do
  119.             if v ~= curChar then
  120.                 table.insert(avail, v)
  121.             end
  122.         end
  123.         newChar = avail[math.random(#avail)]
  124.         newName = string.sub(prevName,0,modPoint-1) .. newChar .. string.sub(prevName,modPoint+1,#prevName)
  125.     end
  126.     return newName
  127. end
  128.  
  129. function create()
  130.     local nameLength = math.random(4,maxLength)
  131.     local name = ""
  132.     local chrBefore = ""
  133.     local chrBeforeB = ""
  134.     for i = 1, nameLength do
  135.         local ops = {}
  136.         if not hasValue(vow, chrBefore) then
  137.             table.insert(ops, "vow")
  138.         end
  139.         if not (hasValue(con, chrBefore) and hasValue(con, chrBeforeB)) then
  140.             table.insert(ops, "con")
  141.         end
  142.         op = ops[math.random(#ops)]
  143.         local chr = ""
  144.         if op == "vow" then
  145.             chr = vow[math.random(#vow)]
  146.         else
  147.             chr = con[math.random(#con)]
  148.         end
  149.         name = name .. chr
  150.         chrBeforeB = chrBefore
  151.         chrBefore = chr
  152.     end
  153.     return name
  154. end
Advertisement
Add Comment
Please, Sign In to add comment