luckums_wormsnacker

DCSS Name Generator v0.1.0

Dec 4th, 2016
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.20 KB | None | 0 0
  1. #READTHIS!
  2. #This is the code for DCSS Name Generator version 0.1.0 (future versions maybe to come I have no clue)
  3. #To use: Paste this into Python, in a folder with the following text files:
  4. #   http://pastebin.com/tRiEZpV1 (SPECIES FILE)
  5. #   http://pastebin.com/iDtHTMXU (BACKGROUND FILE)
  6. #If you want cool 'alliterative' names set the boolean ALLITERATIVE to True below
  7. #(it only works about 50% of the time since most letters don't have valid last name words associated with them)
  8.  
  9.  
  10. import random
  11. SPECIES_FILE = 'species.txt'
  12. BG_FILE = 'backgrounds.txt'
  13. ALLITERATIVE = False
  14.  
  15. #From file f, returns one of the name formats associated with key
  16. def getNameFormat(f, key):
  17.     i = 0
  18.     while ("!" + key != f[i].strip()):
  19.         i += 1
  20.         if i == len(f): return None
  21.  
  22.     #Extract the words for the file
  23.     while (f[i].strip() != '}'):
  24.         line = f[i].strip()
  25.         if (line[0] != '#' and len(line) > 1):
  26.         #Formatting the name
  27.             if (line[0:6] == 'form=['):
  28.                 forms = iterateCategory(f,i)
  29.                 return random.choice(forms[0])      
  30.         i += 1
  31.         if i == len(f):
  32.             return None
  33.  
  34. #From file f, adds words associated with key to dictionary. Returns 'None' if
  35. #the call is unsuccessful, or '1' if it was
  36. def addWords(f, key, dictionary):
  37.     #Find the right species in the file
  38.     i = 0
  39.     while ("!" + key != f[i].strip()):
  40.         i += 1
  41.         if i == len(f): return None
  42.        
  43.     #Extract the words for the file
  44.     line = f[i].strip()
  45.     while (line != '}'):
  46.         if (len(line) > 1):
  47.             if(line[0] != '#'):
  48.                 #Adding a category of words
  49.                 if ('=' in line):   #line[1] == '='):
  50.                     cat = line.split('=')[0]
  51.                     a = iterateCategory(f, i)
  52.                     if (cat == 'EXTEND'):
  53.                         for key in a[0]:
  54.                             addWords(f, key, dictionary)
  55.                     elif (cat == 'P'):
  56.                         dictionary['P'] = int(a[0][0])
  57.                     else:
  58.                         if cat not in dictionary:
  59.                             dictionary[cat] = []
  60.                         dictionary[cat] += a[0]
  61.                     i = a[1]
  62.         i += 1
  63.         if i == len(f): return None
  64.         line = f[i].strip()
  65.     return True
  66.  
  67. def generateName(form, words, spF=None, bgF=None,initial=None):
  68.     name = ''
  69.     for letter in form:
  70.         if (letter != ' '):
  71.             if (letter == 'L'):
  72.                 lastForm = getNameFormat(spF, letter)
  73.                 if (ALLITERATIVE and len(name) > 0):
  74.                     firstLetter = name[0]
  75.                 else:
  76.                     firstLetter = None
  77.                 name += generateName(lastForm, words, initial=firstLetter)
  78.             elif letter in words:
  79.                 #try to make it alliterate if possible
  80.                 if (initial is not None):
  81.                     random.shuffle(words[letter])
  82.                     i = 0
  83.                     while (i < len(words[letter]) - 1 and words[letter][i][0] != initial):
  84.                            i += 1
  85.                     name += words[letter][i]
  86.                 else:
  87.                     name += random.choice(words[letter])
  88.             initial = False
  89.         else:
  90.             name += ' '
  91.             initial = True
  92.     if (name != ''):
  93.         return name.title()
  94.     else:
  95.         return "Bugerror Jones"
  96.  
  97. #A list of lines of a file f, and a position i, returns a list of all terms
  98. #in between two brackets as well as a file position
  99. #Will result in error if the first line does not contain '['
  100. def iterateCategory(f, i):
  101.     line = f[i].split('[')[1].strip()
  102.     words = ""
  103.     while (']' not in line):
  104.         words += line
  105.         i += 1
  106.         line = f[i].strip()
  107.     words += line.split(']')[0]
  108.     wordsFixed = []
  109.     for word in words.split(','):
  110.         if len(word) > 0:
  111.             wordsFixed.append(word)
  112.     return(wordsFixed, i)
  113.  
  114.  
  115. #the part were stuff happens
  116. spF = list(open(SPECIES_FILE))
  117. bgF = list(open(BG_FILE))
  118. species = 'hu'
  119. background = 'fi'
  120.  
  121. while(True):
  122.     if (species is None):
  123.         species = 'human'
  124.     if background is None:
  125.         background = 'fighter'
  126.     newsp = raw_input('Species abbreviation (right now: ' + species + ')?  >').lower()
  127.     newbg = raw_input('Background abbreviation (right now: ' + background + ')?  >').lower()
  128.     if newsp != "":
  129.         species = newsp
  130.     if newbg != "":
  131.         background = newbg
  132.     words = dict()
  133.     words['P'] = 0 #chance of there being a generic, vs species, name
  134.     name = ''
  135.     nameFormat = getNameFormat(spF, species)
  136.     if nameFormat is not None:
  137.         addWords(spF, species, words)
  138.         if(random.randint(100,100) < words['P']):
  139.             addWords(spF, 'genf', words)
  140.         test= addWords(bgF, background, words)
  141.         if test is not None:
  142.             a = "\nThou shalt henceforth be known as " + generateName(nameFormat, words, spF, bgF) + " the " + species + " " + background + ".\n"
  143.             print(a)
  144.         else:
  145.             print "\nI don't know what a " + background + " is. Try again?\n"
  146.     else:
  147.         print "\nI don't know what a " + species + " is. Try again?\n"
Add Comment
Please, Sign In to add comment