Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #READTHIS!
- #This is the code for DCSS Name Generator version 0.1.0 (future versions maybe to come I have no clue)
- #To use: Paste this into Python, in a folder with the following text files:
- # http://pastebin.com/tRiEZpV1 (SPECIES FILE)
- # http://pastebin.com/iDtHTMXU (BACKGROUND FILE)
- #If you want cool 'alliterative' names set the boolean ALLITERATIVE to True below
- #(it only works about 50% of the time since most letters don't have valid last name words associated with them)
- import random
- SPECIES_FILE = 'species.txt'
- BG_FILE = 'backgrounds.txt'
- ALLITERATIVE = False
- #From file f, returns one of the name formats associated with key
- def getNameFormat(f, key):
- i = 0
- while ("!" + key != f[i].strip()):
- i += 1
- if i == len(f): return None
- #Extract the words for the file
- while (f[i].strip() != '}'):
- line = f[i].strip()
- if (line[0] != '#' and len(line) > 1):
- #Formatting the name
- if (line[0:6] == 'form=['):
- forms = iterateCategory(f,i)
- return random.choice(forms[0])
- i += 1
- if i == len(f):
- return None
- #From file f, adds words associated with key to dictionary. Returns 'None' if
- #the call is unsuccessful, or '1' if it was
- def addWords(f, key, dictionary):
- #Find the right species in the file
- i = 0
- while ("!" + key != f[i].strip()):
- i += 1
- if i == len(f): return None
- #Extract the words for the file
- line = f[i].strip()
- while (line != '}'):
- if (len(line) > 1):
- if(line[0] != '#'):
- #Adding a category of words
- if ('=' in line): #line[1] == '='):
- cat = line.split('=')[0]
- a = iterateCategory(f, i)
- if (cat == 'EXTEND'):
- for key in a[0]:
- addWords(f, key, dictionary)
- elif (cat == 'P'):
- dictionary['P'] = int(a[0][0])
- else:
- if cat not in dictionary:
- dictionary[cat] = []
- dictionary[cat] += a[0]
- i = a[1]
- i += 1
- if i == len(f): return None
- line = f[i].strip()
- return True
- def generateName(form, words, spF=None, bgF=None,initial=None):
- name = ''
- for letter in form:
- if (letter != ' '):
- if (letter == 'L'):
- lastForm = getNameFormat(spF, letter)
- if (ALLITERATIVE and len(name) > 0):
- firstLetter = name[0]
- else:
- firstLetter = None
- name += generateName(lastForm, words, initial=firstLetter)
- elif letter in words:
- #try to make it alliterate if possible
- if (initial is not None):
- random.shuffle(words[letter])
- i = 0
- while (i < len(words[letter]) - 1 and words[letter][i][0] != initial):
- i += 1
- name += words[letter][i]
- else:
- name += random.choice(words[letter])
- initial = False
- else:
- name += ' '
- initial = True
- if (name != ''):
- return name.title()
- else:
- return "Bugerror Jones"
- #A list of lines of a file f, and a position i, returns a list of all terms
- #in between two brackets as well as a file position
- #Will result in error if the first line does not contain '['
- def iterateCategory(f, i):
- line = f[i].split('[')[1].strip()
- words = ""
- while (']' not in line):
- words += line
- i += 1
- line = f[i].strip()
- words += line.split(']')[0]
- wordsFixed = []
- for word in words.split(','):
- if len(word) > 0:
- wordsFixed.append(word)
- return(wordsFixed, i)
- #the part were stuff happens
- spF = list(open(SPECIES_FILE))
- bgF = list(open(BG_FILE))
- species = 'hu'
- background = 'fi'
- while(True):
- if (species is None):
- species = 'human'
- if background is None:
- background = 'fighter'
- newsp = raw_input('Species abbreviation (right now: ' + species + ')? >').lower()
- newbg = raw_input('Background abbreviation (right now: ' + background + ')? >').lower()
- if newsp != "":
- species = newsp
- if newbg != "":
- background = newbg
- words = dict()
- words['P'] = 0 #chance of there being a generic, vs species, name
- name = ''
- nameFormat = getNameFormat(spF, species)
- if nameFormat is not None:
- addWords(spF, species, words)
- if(random.randint(100,100) < words['P']):
- addWords(spF, 'genf', words)
- test= addWords(bgF, background, words)
- if test is not None:
- a = "\nThou shalt henceforth be known as " + generateName(nameFormat, words, spF, bgF) + " the " + species + " " + background + ".\n"
- print(a)
- else:
- print "\nI don't know what a " + background + " is. Try again?\n"
- else:
- print "\nI don't know what a " + species + " is. Try again?\n"
Add Comment
Please, Sign In to add comment