save files are distributed among a few xml files, one for characters, one for worlds, more if i end up needing them. not sure how the packaging software i plan on using will end up working with that (if it will only bundle the *.py files into an .exe or if it will bundle the xml as well) so i am not sure how much actual "game data" will be editable outside the program.
my test materials are basically just mock ups of the "real thing", only written by hand before i code the functions that output them.
yes, i plan on a high degree of modabillity, dwarf fortress kind of being my template (one reason i went with xml, really)
as for the landnamegen source, here you go - if you wish to run it, you'll need to
Code:
import xml.etree.ElementTree as ET
import random
import re
this is the function from loadxml.py that formats drom a text string to a list
Code:
def eqParse(roughList):
#this function removes the empty data from the list returned by re.split during transfer of
#xml to gameobjects, specifically in player equipment.
#origionaly written for just player equipment, this function is called anywhere a list needs
#conversion to string
for rep in range(roughList.count("")):
roughList.remove("")
for rep in range(roughList.count(" ")):
roughList.remove(" ")
return roughList
this is the function that loads the descriptors from xml in loadxml.py
Code:
def loadDesc(cat, sel, gen):
#this function takes category, selection and descriptor type and returns a list
#of the possible descriptors
tree = ET.ElementTree(file="descriptors.xml")
doc = tree.getroot()
#load the elementtree
data = doc.find(cat).find(sel).find(gen)
#find the descriptor element
toparse = re.split("\W", data.text)
#reusing equipmentparse function to properly format the list
return eqParse(toparse)
and finally, here is the function in worlds.py that generates the name
Code:
def worldNameGen(char):
#generates planet names
desFilter = []
#filter, contains the "sel" part of loadxml.loadDesc's arguements
tdescrip = []
#temporary list of descriptors, each filter generates a list as a list item in this list
description = []
#final list of descriptors
fcop = ["interest", "aitem", "aspect", "celement"]
##filter, contains the "cat" part of loadxml.loadDesc's arguements
desFilter.append(char.interest.strip("[]"))
desFilter.append(char.aitem.strip("[]"))
desFilter.append(char.aspect.strip("[]"))
desFilter.append(char.element.strip("[]"))
#fill the filter with the proper data. strip is required because i am an idiot and
#did not check to see what characters are illegal in xml.
for stuff, thing in zip(desFilter, fcop):
#loop over two things at once
tdescrip.append(loadxml.loadDesc(thing, stuff, "world"))
#loads the descriptors into the temp list
for item in tdescrip:
for des in item:
description.append(des)
#the above loop just removes sublists, so that we have one list
#with a string for each item
if char.aspect != "[SPACE]":
#let's make sure space players have planets "of frogs"
nameSel = random.sample(description, 2)
#randomly chose two descriptors
name = "Land of {} and {}".format(nameSel[0], nameSel[1])
#create the name of the world
while nameSel[0] == nameSel[1]:
nameSel = random.sample(description, 2)
#above ensures we dont get a world with the same descriptor in both slots
else:
nameSel = random.sample(description, 1)
while nameSel[0] == "Frogs":
nameSel = random.sample(description, 1)
#and do the same with space players. the land of frogs and frogs is silly
name = "Land of {} and Frogs".format(nameSel[0])
return name
oh, you'll need character data, this loads it from xml
Code:
def loadChars():
global slotlist
slotlist = []
tree = ET.ElementTree(file="charstats.xml") #parses xml character data
doc = tree.getroot() #sets doc to the root of the parsed data
parlist = list(doc) #loads individual parties as a list
for i in parlist: #fills slotlist with characters in slot order
slotlist.append(i)
and this imports it into game data
Code:
def importChars(slotlist, slot):
#this function transfers character data from xml to game objects, to be sent to the main module
loadChars()
parList = slotlist
#we copy slotlist internally, so we dont fuck it up
playerList = []
#this is the list that will be returned
for party in parList:
if party.attrib["slot"] == slot:
#this ensures we only deal with the party the user selected in the load menu
for player in party:
if player.tag != "inv":
#without this check, we'll hit an out of index error
a = chars.PlayerData()
#ensure a new instance every iteration
for item in player.iter(tag=None):
inp = item.tag
#copy tag for comparisons
if inp == "cname":
#i may convert this following block into a loop
a.playerName = item.text
#this, like the rest of this long block sets the attrivutes of the PlayerData
#class instance we'll be returning in the list
elif inp == "gender":
a.gender = item.text
elif inp == "ele":
a.element = item.text
elif inp == "class":
a.playerClass = item.text
elif inp == "aspect":
a.aspect = item.text
elif inp == "weapon":
a.weapon = item.text
elif inp == "interest":
a.interest = item.text
elif inp == "aitem":
a.aitem = item.text
elif inp == "level":
a.level = item.text
elif inp == "str":
a.strength = item.text
elif inp == "tgh":
a.toughness = item.text
elif inp == "qck":
a.quickness = item.text
elif inp == "int":
a.intelligence = item.text
elif inp == "wll":
a.willpower = item.text
elif inp == "exp":
a.exp = item.text
elif inp == "tnlexp":
a.tnlexp = item.text
elif inp == "tech":
tkToString = item.text
toParse = re.split("([[].+?[]])?", tkToString)
pTK = eqParse(toParse)
a.techs = pTK
elif inp == "eq":
#the following converts text to a list usable by the game object
eqToString = item.text
toParse = re.split("([[].+?[]])?", eqToString)
pEQ = eqParse(toParse)
a.equipment = pEQ
playerList.append(a)
elif player.tag == "inv":
inv = []
#inventory is it's own list within the list
for elm in list(player):
inv.append(elm.text)
playerList.append(inv)
else:
pass
return (playerList)
and this is the player object -
Code:
class PlayerData():
def __init__(self):
self.playerName = ""
self.playerClass = ""
self.gender = ""
self.aspect = ""
self.weapon = ""
self.interest = ""
self.aitem = ""
self.element = ""
self.level = 1
self.strength = 5
self.toughness = 5
self.quickness = 5
self.intelligence = 5
self.willpower = 5
self.curHp = 50
self.totHp = 50
self.exp = 0
self.tnlexp = 100
self.techs = []
self.equipment = []