-
Notifications
You must be signed in to change notification settings - Fork 5
/
pq_characters.py
368 lines (342 loc) · 16 KB
/
pq_characters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"""
Module for Percival's Quest
Declaration for Character object
"""
#
# pq_characters.py
# Part of Percival's Quest RPG
from pq_namegen import web_namegen
from pq_utilities import choose_from_list, collapse_stringlist, color, send_to_console
from pq_equipment import pq_gear, pq_magic, pq_item_type, \
pq_item_rating, pq_item_worth
import random, textwrap, json
with open('data/pq_classes.json') as f:
pq_classes = json.load(f)
with open('data/pq_races.json') as f:
pq_races = json.load(f)
pq_feats = {'PowerAttack':0, 'StoutDefense':1, 'LightningReflexes':2,
'Toughness':3, 'IronWill':4, 'Prodigy':5, 'ImprovedInitiative':-1}
pq_stats_short = {0:'Atk', 1:'Def', 2:'Ref', 3:'Frt', 4:'Mnd', 5:'Skl'}
class PQ_Character(object):
"""Class for the Character object, aka the PC"""
def __init__(self, the_rpg):
"""Initialize Character instance"""
self.name = ["", ""] #character, player
self.level = [0, 1] #exp, level
self.hitpoints = [1, 1] #current, max
self.skillpoints = [1, 1] #current, max
self.raceclass = ["", ""] #race, class
self.skill = []
self.stats = [0, 0, 0, 0, 0, 0]
self.feats = []
self.combat = {'initiative':0, 'atk':[0, 0], 'dfn':[0, 0]}
self.gear = {"armor":{'name':'', 'rating':0}, "weapon":{'name':'',
'rating':0}, "ring":""}
self.loot = {"quest":'', "gp":0, "items":[]}
self.temp = {'stats':{}, 'statturns':{}, 'condition':{}}
self.dead = False
self.queststatus = "inactive"
self.charsheetstring = []
self.rpg = the_rpg
def chargen(self, player):
"""
Generate a new character using (possibly random) race, class,
and feat choices.
"""
send_to_console(textwrap.fill("It's time to generate a character! At any " \
"of the prompts below, enter 'random' (no quotes) to use " \
"a random choice."))
send_to_console("Available races: " + ", ".join(sorted(pq_races.keys())))
race = choose_from_list("Race> ", pq_races.keys(), rand=True, \
character=self, allowed=['sheet', 'help'])
send_to_console("Available classes: " + ", ".join(sorted(pq_classes.keys())))
clas = choose_from_list("Class> ", pq_classes.keys(), rand=True, \
character=self, allowed=['sheet', 'help'])
nfeat = 2 if race.lower() == "human" else 1
send_to_console("Available feats: " + ", ".join(sorted(pq_feats.keys())))
feats = []
for i in range(nfeat):
feat = choose_from_list("Feat> ", pq_feats.keys(), rand=True, \
character=self, allowed=['sheet', 'help'])
feats.append(feat)
self.chargenerate(race, clas, feats, player)
self.tellchar()
def chargenerate(self, race, clas, feats, player):
self.name = [web_namegen(5, 12, 2).replace('\n', ' '), player]
self.level = [0, 1]
self.raceclass = [race, clas]
self.loot["gp"] = 0
for i in range(0, 6):
statroll = random.choice([random.randint(1, 4) \
for j in range(0, 6)])
self.stats[i] = statroll + \
pq_classes[self.raceclass[1]]['stat'][i] + \
pq_races[self.raceclass[0]]['stat'][i]
self.feats = feats
for i in feats:
for j in pq_feats.keys():
if i.lower() == 'improvedinitiative':
self.combat['initiative'] += 2
elif i.lower() == j.lower():
self.stats[pq_feats[j]] += 2
break
self.hitpoints = [self.stats[3], self.stats[3]]
self.skillpoints = [self.stats[5], self.stats[5]]
self.gear['armor']['name'] = pq_gear['rarmor'][random.choice \
(pq_gear['rarmor'].keys())]['0']
self.gear['armor']['rating'] = 0
self.gear['weapon']['name'] = pq_gear['rweapon'][random.choice \
(pq_gear['rweapon'].keys())]['0']
self.gear['weapon']['rating'] = 0
self.gear['ring'] = ''
self.combat['atk'] = [self.gear['weapon']['rating'], self.stats[0]]
self.combat['dfn'] = [self.gear['armor']['rating'], self.stats[1]]
self.skill = []
self.skill.append(pq_classes[self.raceclass[1]]['skill'])
def tellchar(self):
"""Print out the character sheet"""
send_to_console(color.BOLD + color.GREEN + self.name[0] + color.END + \
' (Player: ' + self.name[1] + ')')
send_to_console(color.BOLD + ' '.join([self.raceclass[0].capitalize(), \
self.raceclass[1].capitalize(), str(self.level[1])]) + color.END)
statstring = sum([[color.BOLD + pq_stats_short[i] + color.END, \
str(self.stats[i])] for i in range(0,6)], [])
feats = collapse_stringlist(self.feats, True, True)
feats = ', '.join(sorted(feats))
send_to_console('; '.join([' '.join(statstring), color.BOLD + 'hp ' + \
color.END + str(self.hitpoints[0]) + '/' + \
str(self.hitpoints[1]), color.BOLD + 'sp ' + color.END + \
str(self.skillpoints[0]) + '/' + str(self.skillpoints[1]), \
color.BOLD + 'exp ' + color.END + str(self.level[0]) + '/' + \
str(self.level[1]*10)]))
send_to_console(textwrap.fill(feats))
if not self.gear['armor']['name']:
armor = color.BOLD + 'Armor:' + color.END + ' None (0)'
else:
armor = color.BOLD + 'Armor:' + color.END + ' ' + \
self.gear['armor']['name'] + ' (' + \
str(self.gear['armor']['rating']) + ')'
if not self.gear['weapon']['name']:
weapon = color.BOLD + 'Weapon:' + color.END + ' None (-1)'
else:
weapon = color.BOLD + 'Weapon:' + color.END + ' '+ \
self.gear['weapon']['name'] + ' (' + \
str(self.gear['weapon']['rating']) + ')'
if not self.gear['ring']:
ring = color.BOLD + 'Ring:' + color.END + ' None'
else:
ring = color.BOLD + 'Ring:' + color.END + ' ' + self.gear['ring']
send_to_console('; '.join([color.BOLD + 'Skills: ' + color.END + \
', '.join(self.skill), armor, weapon, ring]))
lootbag = collapse_stringlist(self.loot['items'], True, True)
for i, f in enumerate(lootbag):
for l in f.split():
if l.lower() in [x.lower() for x in pq_magic['ring'].keys()]:
lootbag[i] = "Ring of " + lootbag[i]
if not lootbag:
lootbag = 'None'
else:
lootbag = ', '.join(lootbag)
send_to_console(textwrap.fill(str(self.loot['gp']) + \
' gp; loot: ' + lootbag)+'\n')
def sheetstring(self):
"""Return a string containing the character sheet"""
self.charsheetstring = []
self.charsheetstring.append(color.BOLD + color.GREEN + self.name[0] + \
color.END + ' (Player: ' + self.name[1] + ')')
self.charsheetstring.append(color.BOLD + ' '.join([self.raceclass[ \
0].capitalize(), self.raceclass[1].capitalize(), \
str(self.level[1])]) + color.END)
statstring = sum([[color.BOLD + pq_stats_short[i] + color.END, \
str(self.stats[i])] for i in range(0,6)], [])
feats = collapse_stringlist(self.feats, True, True)
feats = ', '.join(sorted(feats))
self.charsheetstring.append('; '.join([' '.join(statstring), color.BOLD + \
'hp ' + color.END + str(self.hitpoints[0]) + '/' + \
str(self.hitpoints[1]), color.BOLD + 'sp ' + color.END + \
str(self.skillpoints[0]) + '/' + str(self.skillpoints[1]), \
color.BOLD + 'exp ' + color.END + str(self.level[0]) + '/' + \
str(self.level[1]*10)]))
self.charsheetstring.append(textwrap.fill(feats))
if not self.gear['armor']['name']:
armor = color.BOLD + 'Armor:' + color.END + ' None (0)'
else:
armor = color.BOLD + 'Armor:' + color.END + ' ' + \
self.gear['armor']['name'] + ' (' + \
str(self.gear['armor']['rating']) + ')'
if not self.gear['weapon']['name']:
weapon = color.BOLD + 'Weapon:' + color.END + ' None (-1)'
else:
weapon = color.BOLD + 'Weapon:' + color.END + ' '+ \
self.gear['weapon']['name'] + ' (' + \
str(self.gear['weapon']['rating']) + ')'
if not self.gear['ring']:
ring = color.BOLD + 'Ring:' + color.END + ' None'
else:
ring = color.BOLD + 'Ring:' + color.END + ' ' + self.gear['ring']
self.charsheetstring.append('; '.join([color.BOLD + 'Skills: ' +
color.END + ', '.join(self.skill), armor, weapon, ring]))
lootbag = collapse_stringlist(self.loot['items'], True, True)
for i, f in enumerate(lootbag):
for l in f.split():
if l.lower() in [x.lower() for x in pq_magic['ring'].keys()]:
lootbag[i] = "Ring of " + lootbag[i]
if not lootbag:
lootbag = 'None'
else:
lootbag = ', '.join(lootbag)
self.charsheetstring.append(textwrap.fill(str(self.loot['gp']) + \
' gp; loot: ' + lootbag))
return self.charsheetstring
def levelup(self):
"""Increasing level, including the feat choice
that you get every level."""
self.level[0] -= self.level[1] * 10
self.level[1] += 1
send_to_console("You have leveled up! Please choose a feat; if you would like " \
"one randomly chosen for you, enter Random.")
send_to_console("Available feats: " + ", ".join(pq_feats.keys()))
feat_choice = choose_from_list("Feat> ", pq_feats.keys(), rand=True, \
character=self, allowed=['sheet', 'help', 'equip'])
if feat_choice.lower() in 'improvedinitiative':
self.combat['initiative'] += 2
self.feats.append('ImprovedInitiative')
else:
for i in pq_feats.keys():
if i.lower() == feat_choice.lower():
self.stats[pq_feats[i]] += 2
self.feats.append(i)
self.hitpoints[1] += random.choice([random.randint(max([1, \
self.stats[3] / 2]), 1 + self.stats[3]) for j in range(0, 6)])
self.skillpoints[1] += 2
self.combat['atk'] = [self.gear['weapon']['rating'], self.stats[0]]
self.combat['dfn'] = [self.gear['armor']['rating'], self.stats[1]]
self.rpg.addshopitem()
send_to_console("Level up complete!")
self.tellchar()
def sleep(self):
"""Regain all hp and sp due to snoozage."""
for i in self.temp:
self.temp[i] = {}
self.hitpoints[0] = self.hitpoints[1]
self.skillpoints[0] = self.skillpoints[1]
def ouch(self, damage):
"""Deal damage to self. OW"""
self.hitpoints[0] -= damage
def huh(self, damage):
"""Deal damage to self's skill points. WHUT"""
self.skillpoints[0] -= damage
def cure(self, damage):
"""Heal self. YAY"""
self.hitpoints[0] = self.hitpoints[1] if self.hitpoints[0] + damage > \
self.hitpoints[1] else self.hitpoints[0] + damage
def sammich(self, level):
"""Eat a sandwich slowly in an empty room, regain some sp and hp."""
for i in self.temp:
self.temp[i] = {}
self.cure(level)
self.skillpoints[0] = self.skillpoints[1] if self.skillpoints[0] + \
level > self.skillpoints[1] else self.skillpoints[0] + level
def temp_bonus(self, stat, bonus, turns):
"""Apply a temporary bonus or penalty to self."""
for i in stat:
if self.temp.get(i, False):
sign = -1 if bonus < 0 else 1
bonus = sign * max([abs(self.temp[i]), abs(bonus)])
self.temp['stats'][i] = bonus
self.temp['statturns'][i] = turns
def equip(self):
"""Equip an item, unequipping extant item if necessary."""
if not self.loot['items']:
send_to_console("You have nothing to equip!")
return
send_to_console("What would you like to equip?")
lootbag = ['Ring of ' + i if i in pq_magic['ring'].keys() \
else i for i in self.loot['items']]
lootbag_basic = collapse_stringlist(lootbag, sortit=True, \
addcounts=False)
send_to_console(textwrap.fill("Lootbag: " + \
", ".join(collapse_stringlist(lootbag, sortit=True, \
addcounts=True))))
equipment = choose_from_list("Equip> ", lootbag_basic, rand=False, \
character=self, allowed=['sheet', 'help'])
oldequip = self.changequip(equipment)
send_to_console(equipment + " equipped!")
if oldequip:
send_to_console(oldequip + " unequipped!")
return
def changequip(self, equipment):
equipment = equipment.replace('Ring of ', '')
if equipment not in self.loot['items']:
send_to_console(equipment, self.loot['items'])
return
itemtype = pq_item_type(equipment)
oldequip = ''
if itemtype[0] == "ring":
for i in pq_magic['ring'].keys():
if equipment.lower() == i.lower():
equipment = i
break
if self.gear['ring']:
self.skill.remove(pq_magic['ring'][self.gear['ring']])
self.loot['items'].append(self.gear['ring'])
oldequip = self.gear['ring']
self.skill.append(pq_magic['ring'][equipment])
self.gear['ring'] = equipment
self.loot['items'].remove(equipment)
else:
new_rating = pq_item_rating(itemtype, equipment)
if self.gear[itemtype[0]]['name']:
self.loot['items'].append(self.gear[itemtype[0]]['name'])
oldequip = self.gear[itemtype[0]]['name']
self.gear[itemtype[0]]['name'] = equipment
self.gear[itemtype[0]]['rating'] = new_rating
self.loot['items'].remove(equipment)
self.combat['atk'] = [self.gear['weapon']['rating'], self.stats[0]]
self.combat['dfn'] = [self.gear['armor']['rating'], self.stats[1]]
return oldequip
def complete_quest(self, experience, gold):
"""Get xp and gold for completing a quest;
also, reset quest item slot"""
self.loot['quest'] = ''
self.level[0] += experience
self.loot['gp'] += gold
self.queststatus = "active"
def sell_loot(self, sell, count = 1):
"""Character-side loot sale (removing item
from lootbag, adding gp)"""
if type(sell) is list:
sell = sell[0]
lowitems = [x.lower() for x in self.loot['items']]
if sell.lower() not in lowitems:
return False
value = max([pq_item_worth(sell)/2, 1])
loot = []
for j in sorted(self.loot['items']):
if sell.lower() == j.lower() and count > 0:
self.loot['gp'] += value
count -= 1
else:
loot.append(j)
self.loot['items'] = loot
return True
def buy_loot(self, buy, cost):
"""Character-side loot purchase (adding item
to lootbag, subtracting gp)"""
if type(buy) is list:
buy = buy[0]
buy = ' '.join([i.lower().capitalize() for i in buy.split()])
self.loot['items'].append(buy)
self.loot['gp'] -= cost
def defeat_enemy(self, exp, loot):
"""Get loot and exp for defeating an enemy, or some other reason."""
for i in self.temp:
self.temp[i] = {}
self.level[0] += exp
for i in loot.keys():
if i == 'gp':
self.loot['gp'] += loot[i]
elif i == 'quest' and not self.loot['quest']:
self.loot['quest'] = loot['quest']
elif loot[i]:
self.loot['items'].append(loot[i])