Contact me to be added to this repository. Push the creations you make for D&D, and they will be displayed in a nice website. See the website for how to contribute.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

110 lines
3.5 KiB

MONSTER_PROPS = ["CR","Size","Type","Align","AC","HP","Speed","Str","Dex","Con","Int","Wis","Cha","Senses","Resist","Immune","Weak"] #properties must appear in this order... for now
def getProperty(strs,prop):
lineData = strs[0].split(":")
if lineData[0] == prop:
return lineData[1]
return None
class MonsterAction(object):
def __init__(self):
self.name = ""
self.hit = ""
self.reach = -1
self.damage = ""
self.special = ""
def parse(self,text):
#assumes first line is first opening brace of action
#and last line is last closing brace of action
lines = [line.strip() for line in text.split("\n") if line]
self.name = lines[0][:-1].strip()
self.hit = lines[1].split(" ")[1:]
self.reach = lines[2].split(" ")[1]
self.damage = lines[3].split(" ")[1]
if len(lines) > 4:
if "Special" in lines[4]:
i = 1
while not "}" in lines[i]:
self.special += lines[i] + "\n"
i += 1
def __str__(self):
return str(self.__dict__)
class Monster(object):
def __init__(self,fpath):
with open(fpath,'r') as f:
text = list(filter(bool,f.read().split("\n")))
self.name = text.pop(0)
text.pop(0) #clear ------
for p in MONSTER_PROPS:
prop = getProperty(text,p)
if prop:
text.pop(0)
setattr(self,p,prop)
#now for brace parsing
self.descript = ""
if "Descript" in text[0]:
text.pop(0)
while not "}" in text[0]:
self.descript += text[0] + "\n"
text.pop(0)
text.pop(0) #remove last }
if "Actions" in text[0]:
self.actions = []
#print("Debug: parsing actions")
text.pop(0)
parenCount = 1
actionBuffer = ""
while parenCount >= 1:
actionBuffer += text[0]+"\n"
if "{" in text[0]:
parenCount += 1
#print("Upping paren count")
if "}" in text[0]:
parenCount -= 1
#print("Downing paren count")
if parenCount == 1:
#print("Making action")
a = MonsterAction()
a.parse(actionBuffer)
self.actions.append(a)
#print("Made action out of")
#print(''.join(actionBuffer))
actionBuffer = ""
#print("Reading line")
text.pop(0)
#we do not need to remove last brace here, as it is consumed by the loop this time
if "Notes" in text[0]:
text.pop(0)
self.notes = ""
while not "}" in text[0]:
self.notes += text[0] + "\n"
text.pop(0)
text.pop(0) #remove last }
if len(text) > 0:
print("Unparsed: ")
print(text)
def __str__(self):
return str(self.__dict__).replace("\',","\'\n")
#driver code
#m = Monster("../Monsters/reverse_mimic.txt")
#print(m)
#m = Monster("../Monsters/apocrypha.txt")
#print(m)
#m = Monster("../Monsters/matryoshka.txt")
#print(m)
#m = Monster("../Monsters/pirate_skirmisher.txt")
#print(m)