foePrep() is one of the more complicated functions in this class.
I’ll start by discussing the parameters and then go through everything the function does to accomplish its goal: adding extra buddies and pets to the fight.
Parameters
def foePrep(self, autoSlected= ""):
The only optional parameter is autoSelected which allows you to start the fight with a specific monster already in mind.
The rundown
luck = int(random.random() * 100)
while luck < 1:
luck = int(random.random() * 100)
if autoSlected == "":
for figther in self.listing:
if isinstance(self.listing[figther], Monster):
starter = self.listing[figther]
else:
enemyCatalog = []
f = open("monsterStats.csv")
fReader = csv.reader(f)
for line in fReader:
if line == []:
continue
enemyCatalog.append(line)
for enemy in enemyCatalog:
if autoSlected == enemy[0]:
starter = Monster(attack=int(enemy[2]), health=int(enemy[1]), defense=int(enemy[3]), displayName=autoSlected, experince=int(enemy[7]), petRate=int(enemy[4]), buddyRate=int(enemy[5]), weapon=enemy[6])
self.intiativeRoll(starter)
break
f.close()
First, I start by generating a random number and making it an integer so I can use it later. Then if there was no autoSelected, I will find the first monster in the listing and use that monster as our starter. Otherwise, I will find the autoSlected monster in the monsterStats.csv and use the stats found there to instantiate a new monster as the starter and roll initiative for the new monster.
dividers = []
if starter.buddyRate > 0:
dividers.append(starter.buddyRate)
dividers.append(starter.buddyRate + starter.petRate)
else:
dividers.append(0)
dividers.append(starter.petRate)
Second, I set up dividers to make the selection process of a buddy or pet easier. The dividers a set up based on the stats of the monster (monster.buddyRate and monster.petRate). Some monsters don’t have a buddy rate, so for those only one divider is needed.
if luck < dividers[0]:
buddy = Monster(attack= starter.attack, health= starter.health, defense= starter.defense, displayName= starter.displayName + "(0)", experince= starter.experience, petRate=starter.petRate, buddyRate= starter.buddyRate, weapon=starter.weapon )
self.intiativeRoll(buddy)
flag = True
counter = 1
while flag:
luck = int(random.random() * 100)
if luck < dividers[0]:
buddy = Monster(attack=starter.attack, health=starter.health, defense=starter.defense,
displayName=starter.displayName + "(" + str(counter) + ")", experince=starter.experience,
petRate=starter.petRate, buddyRate=starter.buddyRate, weapon=starter.weapon)
self.intiativeRoll(buddy)
else:
flag = False
counter += 1
if counter == 6:
flag = False
If the random number from earlier (luck) is less than the first divider then I go into buddy generation. Where I continue to make copies of the monster and add them to the fight until either a number higher than the divider is rolled or 6 buddies are generated.
if luck < dividers[1]:
f = open("encounters.csv")
reader = csv.reader(f)
regionCounter = 0
endFlag = False
for line in reader:
if line == []:
continue
for item in line:
if starter.displayName == item:
endFlag = True
break
if endFlag:
break
regionCounter += 1
f.close()
f = open("pets.csv")
reader = csv.reader(f)
pets = []
for line in reader:
if line == []:
continue
if int(line[0]) == regionCounter:
for pet in line:
try:
int(pet)
continue
except ValueError:
pets.append(pet)
division = int(100/len(pets))
dividers = []
while division < 100:
dividers.append(division)
division += division
dividers.append(100)
luck = int(random.random() * 100)
while luck < 1:
luck = int(random.random() * 100)
counter = 0
for divider in dividers:
if luck < divider:
break
counter += 1
self.foePrep(autoSlected= pets[counter])
else:
pass
If luck is less than the second divider. then there are pets, I check what types of pets are available by first getting a region id by checking the starter name against its location in the csv. Each line in the file is a region in the game starting with the first line being zero and the last line being 9. Then it goes to pets.csv and goes to the line with the region’s id as the first entry. It then takes the pets in that line to fill in the options and set up evenly distributed dividers. It then generates another random number and then uses the random number to find to select a pet. The pet chosen is the one in-between the range that the random number falls in. It then calls foePrep() with the pet name as the autoSelected to instantiate the pet and see if it has buddies.
Once this function has been run, combat is almost ready
to begin. Next up is combatantOrganizer().
Comments
Post a Comment