oldschool-toontown/toontown/makeatoon/NameGenerator.py

496 lines
19 KiB
Python
Raw Normal View History

from pandac.PandaModules import *
2019-11-02 17:27:54 -05:00
import random
import string
import copy
from toontown.toonbase import ToontownGlobals
from toontown.toonbase import TTLocalizer
import os
from direct.showbase import AppRunnerGlobal
2019-11-02 17:27:54 -05:00
from direct.directnotify import DirectNotifyGlobal
2019-11-02 17:27:54 -05:00
class NameGenerator:
# A TextNode we will use to measure the lengths of names
2019-11-02 17:27:54 -05:00
text = TextNode('text')
text.setFont(ToontownGlobals.getInterfaceFont())
notify = DirectNotifyGlobal.directNotify.newCategory("NameGenerator")
2019-11-02 17:27:54 -05:00
boyTitles = []
girlTitles = []
neutralTitles = []
boyFirsts = []
girlFirsts = []
neutralFirsts = []
capPrefixes = []
lastPrefixes = []
lastSuffixes = []
def __init__(self):
self.generateLists()
return
2019-11-02 17:27:54 -05:00
def generateLists(self):
""" This method looks in a text file specified in the localizer and loads
in all the names into the 8 lists as well as populating self.nameDictionary
which has uniqueIDs mapped to a tuple of category and name
"""
2019-11-02 17:27:54 -05:00
self.boyTitles = []
self.girlTitles = []
self.neutralTitles = []
self.boyFirsts = []
self.girlFirsts = []
self.neutralFirsts = []
self.capPrefixes = []
self.lastPrefixes = []
self.lastSuffixes = []
self.nameDictionary = {}
# Look for the name master file and read it in.
2019-11-02 17:27:54 -05:00
searchPath = DSearchPath()
2019-11-10 20:32:17 -06:00
if __debug__:
searchPath.appendDirectory(Filename('resources/phase_3/etc'))
if AppRunnerGlobal.appRunner:
# In the web-publish runtime, it will always be here:
searchPath.appendDirectory(Filename.expandFrom('$TT_3_ROOT/phase_3/etc'))
else:
# In other environments, including the dev environment, look here:
searchPath.appendDirectory(Filename('phase_3/etc'))
base = os.path.expandvars('$TOONTOWN') or './toontown'
searchPath.appendDirectory(Filename.fromOsSpecific(os.path.expandvars(base + '/src/configfiles')))
# RobotToonManager needs to look for file in current directory
searchPath.appendDirectory(Filename('.'))
2019-11-02 17:27:54 -05:00
filename = Filename(TTLocalizer.NameShopNameMaster)
found = vfs.resolveFilename(filename, searchPath)
2019-11-02 17:27:54 -05:00
if not found:
self.notify.error("NameGenerator: Error opening name list text file '%s.'" % TTLocalizer.NameShopNameMaster)
2019-11-02 17:27:54 -05:00
input = StreamReader(vfs.openReadFile(filename, 1), 1)
2019-11-02 17:27:54 -05:00
currentLine = input.readline()
while currentLine:
2019-12-30 14:37:11 -06:00
if currentLine.lstrip()[0:1] != b'#':
a1 = currentLine.find(b'*')
a2 = currentLine.find(b'*', a1 + 1)
self.nameDictionary[int(currentLine[0:a1])] = (int(currentLine[a1 + 1:a2]),
currentLine[a2 + 1:].rstrip().decode('utf-8'))
2019-11-02 17:27:54 -05:00
currentLine = input.readline()
masterList = [self.boyTitles, self.girlTitles, self.neutralTitles,
self.boyFirsts, self.girlFirsts, self.neutralFirsts,
self.capPrefixes, self.lastPrefixes, self.lastSuffixes]
for tu in list(self.nameDictionary.values()):
2019-11-02 17:27:54 -05:00
masterList[tu[0]].append(tu[1])
return 1
def _getNameParts(self, cat2part):
# returns list of dict of string->index, one dict per name part
nameParts = [{}, {}, {}, {}]
# cat2part is mapping of NameMasterEnglish.txt category -> namePart index
for id, tpl in self.nameDictionary.items():
2019-11-02 17:27:54 -05:00
cat, str = tpl
if cat in cat2part:
nameParts[cat2part[cat]][str] = id
return nameParts
def getAllNameParts(self):
2019-11-02 17:27:54 -05:00
return self._getNameParts({0: 0,
1: 0,
2: 0,
3: 1,
4: 1,
5: 1,
6: 2,
7: 2,
8: 3,
})
2019-11-02 17:27:54 -05:00
def getLastNamePrefixesCapped(self):
return self.capPrefixes
def returnUniqueID(self, name, listnumber):
""" This is a helper function which accepts a name string, and a listnumber of
type 0 = title, 1 = firstname, 2 = prefix, 3 = suffix
It then makes a list of search tuples newtu and searches nameDictionary.
If successful it returns the uniqueID, if not then -1
"""
2019-11-02 17:27:54 -05:00
newtu = [(), (), ()]
if listnumber == 0:
# Looking for a title
2019-11-02 17:27:54 -05:00
newtu[0] = (0, name)
newtu[1] = (1, name)
newtu[2] = (2, name)
elif listnumber == 1:
# Looking for a first name
2019-11-02 17:27:54 -05:00
newtu[0] = (3, name)
newtu[1] = (4, name)
newtu[2] = (5, name)
elif listnumber == 2:
# Looking for a prefix
2019-11-02 17:27:54 -05:00
newtu[0] = (6, name)
newtu[1] = (7, name)
else:
newtu[0] = (8, name)
for tu in list(self.nameDictionary.items()):
2019-11-02 17:27:54 -05:00
for g in newtu:
if tu[1] == g:
return tu[0]
return -1
def findWidestInList(self, text, nameList):
maxWidth = 0
maxName = ""
2019-11-02 17:27:54 -05:00
for name in nameList:
width = text.calcWidth(name)
if width > maxWidth:
maxWidth = text.calcWidth(name)
maxName = name
print(maxName + " " + str(maxWidth))
2019-11-02 17:27:54 -05:00
return maxName
def findWidestName(self):
longestBoyTitle = self.findWidestInList(self.text,
self.boyTitles +
self.neutralTitles)
longestGirlTitle = self.findWidestInList(self.text,
self.girlTitles +
self.neutralTitles)
longestBoyFirst = self.findWidestInList(self.text,
self.boyFirsts +
self.neutralFirsts)
longestGirlFirst = self.findWidestInList(self.text,
self.girlFirsts +
self.neutralFirsts)
longestLastPrefix = self.findWidestInList(self.text,
self.lastPrefixes)
longestLastSuffix = self.findWidestInList(self.text,
self.lastSuffixes)
longestBoyFront = self.findWidestInList(self.text,
[longestBoyTitle,
longestBoyFirst])
longestGirlFront = self.findWidestInList(self.text,
[longestGirlTitle,
longestGirlFirst])
longestBoyName = (longestBoyTitle + " " + longestBoyFirst + " " +
longestLastPrefix + longestLastSuffix)
longestGirlName = (longestGirlTitle + " " + longestGirlFirst + " " +
longestLastPrefix + longestLastSuffix)
longestName = self.findWidestInList(self.text,
[longestBoyName, longestGirlName])
2019-11-02 17:27:54 -05:00
return longestName
def findWidestTitleFirst(self):
longestBoyTitle = self.findWidestInList(self.text,
self.boyTitles +
self.neutralTitles)
longestGirlTitle = self.findWidestInList(self.text,
self.girlTitles +
self.neutralTitles)
longestBoyFirst = self.findWidestInList(self.text,
self.boyFirsts +
self.neutralFirsts)
longestGirlFirst = self.findWidestInList(self.text,
self.girlFirsts +
self.neutralFirsts)
longestBoyName = (longestBoyTitle + " " + longestBoyFirst)
longestGirlName = (longestGirlTitle + " " + longestGirlFirst)
longestName = self.findWidestInList(self.text,
[longestBoyName, longestGirlName])
2019-11-02 17:27:54 -05:00
def findWidestTitle(self):
widestTitle = self.findWidestInList(self.text,
self.neutralTitles +
self.boyTitles +
self.girlTitles)
2019-11-02 17:27:54 -05:00
return widestTitle
def findWidestFirstName(self):
widestFirst = self.findWidestInList(self.text,
self.neutralFirsts +
self.boyFirsts +
self.girlFirsts)
2019-11-02 17:27:54 -05:00
return widestFirst
def findWidestLastName(self):
longestLastPrefix = self.findWidestInList(self.text,
self.lastPrefixes)
longestLastSuffix = self.findWidestInList(self.text,
self.lastSuffixes)
2019-11-02 17:27:54 -05:00
longestLastName = longestLastPrefix + longestLastSuffix
return longestLastName
def findWidestNameWord(self):
widestWord = self.findWidestInList(self.text,
[self.findWidestTitle(),
self.findWidestFirstName(),
self.findWidestLastName()])
2019-11-02 17:27:54 -05:00
return widestWord
def findWidestNameWidth(self):
name = self.findWidestName()
return self.text.calcWidth(name)
def printWidestName(self):
name = self.findWidestName()
width = self.text.calcWidth(name)
widthStr = str(width)
print(("The widest name is: " + name + " (" +
widthStr + " units)"))
2019-11-02 17:27:54 -05:00
def printWidestLastName(self):
name = self.findWidestLastName()
width = self.text.calcWidth(name)
widthStr = str(width)
print(("The widest last name is: " + name + " (" +
widthStr + " units)"))
2019-11-02 17:27:54 -05:00
def randomName(self, boy=0, girl=0):
""" This method is outdated for current uses in Toontown, but good for
general debugging. You probably want to use randomNameMoreinfo
"""
2019-11-02 17:27:54 -05:00
if boy and girl:
self.error("A name can't be both boy and girl!")
if (not boy) and (not girl):
# Randomly pick the name sex
2019-11-02 17:27:54 -05:00
boy = random.choice([0, 1])
girl = not boy
# Five types of name combos
uberFlag = random.choice(["title-first", "title-last",
"first", "last", "first-last", "title-first-last"])
2019-11-02 17:27:54 -05:00
titleFlag = 0
if ((uberFlag == "title-first") or
(uberFlag == "title-last") or
(uberFlag == "title-first-last")):
2019-11-02 17:27:54 -05:00
titleFlag = 1
2019-11-02 17:27:54 -05:00
firstFlag = 0
if ((uberFlag == "title-first") or
(uberFlag == "first") or
(uberFlag == "first-last") or
(uberFlag == "title-first-last")):
2019-11-02 17:27:54 -05:00
firstFlag = 1
2019-11-02 17:27:54 -05:00
lastFlag = 0
if ((uberFlag == "title-last") or
(uberFlag == "last") or
(uberFlag == "first-last") or
(uberFlag == "title-first-last")
):
2019-11-02 17:27:54 -05:00
lastFlag = 1
retString = ""
2019-11-02 17:27:54 -05:00
if titleFlag:
# Shallow copy, since we will be altering the list
2019-11-02 17:27:54 -05:00
titleList = self.neutralTitles[:]
if boy:
titleList += self.boyTitles
elif girl:
titleList += self.girlTitles
else:
self.error("Must be boy or girl.")
# Put a space because there will surely be another name.
retString += random.choice(titleList) + " "
2019-11-02 17:27:54 -05:00
if firstFlag:
# Shallow copy, since we will be altering the list
2019-11-02 17:27:54 -05:00
firstList = self.neutralFirsts[:]
if boy:
firstList += self.boyFirsts
elif girl:
firstList += self.girlFirsts
else:
self.error("Must be boy or girl.")
2019-11-02 17:27:54 -05:00
retString += random.choice(firstList)
# Put a space if there is going to be a last name.
2019-11-02 17:27:54 -05:00
if lastFlag:
retString += " "
2019-11-02 17:27:54 -05:00
if lastFlag:
lastPrefix = random.choice(self.lastPrefixes)
lastSuffix = random.choice(self.lastSuffixes)
if lastPrefix in self.capPrefixes:
lastSuffix = lastSuffix.capitalize()
retString += lastPrefix + lastSuffix
2019-11-02 17:27:54 -05:00
return retString
def randomNameMoreinfo(self):
"""This is just like randomName only it returns a list where the first three
values are titleFlag, firstFlag, and lastFlag and the next four values are
the title, firstname, and lastname (if applicable, '' if not)
"""
# Five types of name combos
uberFlag = random.choice(["title-first", "title-last",
"first", "last", "first-last", "title-first-last"])
2019-11-02 17:27:54 -05:00
titleFlag = 0
if ((uberFlag == "title-first") or
(uberFlag == "title-last") or
(uberFlag == "title-first-last")):
2019-11-02 17:27:54 -05:00
titleFlag = 1
2019-11-02 17:27:54 -05:00
firstFlag = 0
if ((uberFlag == "title-first") or
(uberFlag == "first") or
(uberFlag == "first-last") or
(uberFlag == "title-first-last")):
2019-11-02 17:27:54 -05:00
firstFlag = 1
2019-11-02 17:27:54 -05:00
lastFlag = 0
if ((uberFlag == "title-last") or
(uberFlag == "last") or
(uberFlag == "first-last") or
(uberFlag == "title-first-last")):
2019-11-02 17:27:54 -05:00
lastFlag = 1
retString = ""
uberReturn = [0, 0, 0, '', '', '', '']
2019-11-02 17:27:54 -05:00
uberReturn[0] = titleFlag
uberReturn[1] = firstFlag
uberReturn[2] = lastFlag
# Choose random names in each slot even if we won't be using
# them. That way, if the user activates a previously deactive
# slot, s/he'll start at a random point in the list instead of
# always at the top.
# Shallow copy, since we will be altering the list
2019-11-02 17:27:54 -05:00
titleList = self.neutralTitles[:]
titleList += self.boyTitles
titleList += self.girlTitles
2019-11-02 17:27:54 -05:00
uberReturn[3] = random.choice(titleList)
# Shallow copy, since we will be altering the list
2019-11-02 17:27:54 -05:00
firstList = self.neutralFirsts[:]
firstList += self.boyFirsts
firstList += self.girlFirsts
2019-11-02 17:27:54 -05:00
uberReturn[4] = random.choice(firstList)
2019-11-02 17:27:54 -05:00
lastPrefix = random.choice(self.lastPrefixes)
lastSuffix = random.choice(self.lastSuffixes)
if lastPrefix in self.capPrefixes:
lastSuffix = lastSuffix.capitalize()
uberReturn[5] = lastPrefix
uberReturn[6] = lastSuffix
# Put a space because there will surely be another name.
2019-11-02 17:27:54 -05:00
if titleFlag:
retString += uberReturn[3] + " "
2019-11-02 17:27:54 -05:00
if firstFlag:
retString += uberReturn[4]
# Put a space if there is going to be a last name.
2019-11-02 17:27:54 -05:00
if lastFlag:
retString += " "
2019-11-02 17:27:54 -05:00
if lastFlag:
retString += uberReturn[5] + uberReturn[6]
2019-11-02 17:27:54 -05:00
uberReturn.append(retString)
return uberReturn
def printRandomNames(self, boy=0, girl=0, total=1):
2019-11-02 17:27:54 -05:00
i = 0
origBoy = boy
origGirl = girl
while i < total:
if (not origBoy) and (not origGirl):
# Randomly pick the name sex
2019-11-02 17:27:54 -05:00
boy = random.choice([0, 1])
girl = not boy
2019-11-02 17:27:54 -05:00
name = self.randomName(boy, girl)
width = self.text.calcWidth(name)
widthStr = str(width)
if boy:
print("Boy: " + name + " (" + widthStr + " units)")
2019-11-02 17:27:54 -05:00
if girl:
print("Girl: " + name + " (" + widthStr + " units)")
2019-11-02 17:27:54 -05:00
i += 1
def percentOver(self, limit=9.0, samples=1000):
2019-11-02 17:27:54 -05:00
i = 0
over = 0
while i < samples:
name = self.randomName()
width = self.text.calcWidth(name)
if width > limit:
over += 1
i += 1
percent = (float(over) / float(samples)) * 100
print(("Samples: " + str(samples) + " Over: " +
str(over) + " Percent: " + str(percent)))
2019-11-02 17:27:54 -05:00
def totalNames(self):
# Firsts only
firsts = (len(self.boyFirsts) + len(self.girlFirsts) +
len(self.neutralFirsts))
print("Total firsts: " + str(firsts))
# Lasts only
2019-11-02 17:27:54 -05:00
lasts = len(self.lastPrefixes) * len(self.lastSuffixes)
print("Total lasts: " + str(lasts))
# Title plus first
2019-11-02 17:27:54 -05:00
neutralTitleFirsts = len(self.neutralTitles) * len(self.neutralFirsts)
boyTitleFirsts = ((len(self.boyTitles) *
(len(self.neutralFirsts) + len(self.boyFirsts))) +
((len(self.neutralTitles) *
len(self.boyFirsts))))
girlTitleFirsts = ((len(self.girlTitles) *
(len(self.neutralFirsts) + len(self.girlFirsts))) +
((len(self.neutralTitles) *
len(self.girlFirsts))))
totalTitleFirsts = (neutralTitleFirsts + boyTitleFirsts +
girlTitleFirsts)
print("Total title firsts: " + str(totalTitleFirsts))
# Title plus last
2019-11-02 17:27:54 -05:00
neutralTitleLasts = len(self.neutralTitles) * lasts
boyTitleLasts = (len(self.boyTitles) *
lasts)
girlTitleLasts = (len(self.girlTitles) *
lasts)
totalTitleLasts = (neutralTitleLasts + boyTitleFirsts +
girlTitleLasts)
print("Total title lasts: " + str(totalTitleLasts))
# First plus last
2019-11-02 17:27:54 -05:00
neutralFirstLasts = len(self.neutralFirsts) * lasts
boyFirstLasts = len(self.boyFirsts) * lasts
girlFirstLasts = len(self.girlFirsts) * lasts
totalFirstLasts = (neutralFirstLasts + boyFirstLasts +
girlFirstLasts)
print("Total first lasts: " + str(totalFirstLasts))
# Title plus first plus last
2019-11-02 17:27:54 -05:00
neutralTitleFirstLasts = neutralTitleFirsts * lasts
boyTitleFirstLasts = boyTitleFirsts * lasts
girlTitleFirstLasts = girlTitleFirsts * lasts
totalTitleFirstLasts = (neutralTitleFirstLasts + boyTitleFirstLasts + girlTitleFirstLasts)
print("Total title first lasts: " + str(totalTitleFirstLasts))
# Total
totalNames = (firsts + lasts + totalTitleFirsts +
totalTitleLasts + totalFirstLasts + totalTitleFirstLasts)
print("Total Names: " + str(totalNames))